sandbox/linux/bpf_dsl: eliminate implicit dependency on C++ compiler behavior
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_script_cache_map.cc
blob16931391663b12a1d2749258769979e6d8df931c
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_script_cache_map.h"
7 #include "base/logging.h"
8 #include "content/browser/service_worker/service_worker_context_core.h"
9 #include "content/browser/service_worker/service_worker_disk_cache.h"
10 #include "content/browser/service_worker/service_worker_storage.h"
11 #include "content/browser/service_worker/service_worker_version.h"
12 #include "content/common/service_worker/service_worker_types.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
16 namespace content {
18 ServiceWorkerScriptCacheMap::ServiceWorkerScriptCacheMap(
19 ServiceWorkerVersion* owner,
20 base::WeakPtr<ServiceWorkerContextCore> context)
21 : owner_(owner), context_(context), weak_factory_(this) {
24 ServiceWorkerScriptCacheMap::~ServiceWorkerScriptCacheMap() {
27 int64 ServiceWorkerScriptCacheMap::LookupResourceId(const GURL& url) {
28 ResourceMap::const_iterator found = resource_map_.find(url);
29 if (found == resource_map_.end())
30 return kInvalidServiceWorkerResponseId;
31 return found->second.resource_id;
34 int64 ServiceWorkerScriptCacheMap::LookupResourceSize(const GURL& url) {
35 ResourceMap::const_iterator found = resource_map_.find(url);
36 if (found == resource_map_.end())
37 return kInvalidServiceWorkerResponseId;
38 return found->second.size_bytes;
41 void ServiceWorkerScriptCacheMap::NotifyStartedCaching(
42 const GURL& url, int64 resource_id) {
43 DCHECK_EQ(kInvalidServiceWorkerResponseId, LookupResourceId(url));
44 DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
45 owner_->status() == ServiceWorkerVersion::INSTALLING)
46 << owner_->status();
47 if (!context_)
48 return; // Our storage has been wiped via DeleteAndStartOver.
49 resource_map_[url] =
50 ServiceWorkerDatabase::ResourceRecord(resource_id, url, -1);
51 context_->storage()->StoreUncommittedResponseId(resource_id);
54 void ServiceWorkerScriptCacheMap::NotifyFinishedCaching(
55 const GURL& url,
56 int64 size_bytes,
57 const net::URLRequestStatus& status,
58 const std::string& status_message) {
59 DCHECK_NE(kInvalidServiceWorkerResponseId, LookupResourceId(url));
60 DCHECK(owner_->status() == ServiceWorkerVersion::NEW ||
61 owner_->status() == ServiceWorkerVersion::INSTALLING ||
62 owner_->status() == ServiceWorkerVersion::REDUNDANT);
63 if (!context_)
64 return; // Our storage has been wiped via DeleteAndStartOver.
65 if (!status.is_success()) {
66 context_->storage()->DoomUncommittedResponse(LookupResourceId(url));
67 resource_map_.erase(url);
68 if (owner_->script_url() == url) {
69 main_script_status_ = status;
70 main_script_status_message_ = status_message;
72 } else {
73 resource_map_[url].size_bytes = size_bytes;
77 void ServiceWorkerScriptCacheMap::GetResources(
78 std::vector<ServiceWorkerDatabase::ResourceRecord>* resources) {
79 DCHECK(resources->empty());
80 for (ResourceMap::const_iterator it = resource_map_.begin();
81 it != resource_map_.end();
82 ++it) {
83 resources->push_back(it->second);
87 void ServiceWorkerScriptCacheMap::SetResources(
88 const std::vector<ServiceWorkerDatabase::ResourceRecord>& resources) {
89 DCHECK(resource_map_.empty());
90 typedef std::vector<ServiceWorkerDatabase::ResourceRecord> RecordVector;
91 for (RecordVector::const_iterator it = resources.begin();
92 it != resources.end(); ++it) {
93 resource_map_[it->url] = *it;
97 void ServiceWorkerScriptCacheMap::WriteMetadata(
98 const GURL& url,
99 const std::vector<char>& data,
100 const net::CompletionCallback& callback) {
101 ResourceMap::iterator found = resource_map_.find(url);
102 if (found == resource_map_.end() ||
103 found->second.resource_id == kInvalidServiceWorkerResponseId) {
104 callback.Run(net::ERR_FILE_NOT_FOUND);
105 return;
107 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(data.size()));
108 if (data.size())
109 memmove(buffer->data(), &data[0], data.size());
110 scoped_ptr<ServiceWorkerResponseMetadataWriter> writer;
111 writer = context_->storage()->CreateResponseMetadataWriter(
112 found->second.resource_id);
113 ServiceWorkerResponseMetadataWriter* raw_writer = writer.get();
114 raw_writer->WriteMetadata(
115 buffer.get(), data.size(),
116 base::Bind(&ServiceWorkerScriptCacheMap::OnMetadataWritten,
117 weak_factory_.GetWeakPtr(), Passed(&writer), callback));
120 void ServiceWorkerScriptCacheMap::ClearMetadata(
121 const GURL& url,
122 const net::CompletionCallback& callback) {
123 WriteMetadata(url, std::vector<char>(), callback);
126 void ServiceWorkerScriptCacheMap::OnMetadataWritten(
127 scoped_ptr<ServiceWorkerResponseMetadataWriter> writer,
128 const net::CompletionCallback& callback,
129 int result) {
130 callback.Run(result);
133 } // namespace content