| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/service_worker/service_worker_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/profiler/scoped_tracker.h" | 13 #include "base/profiler/scoped_tracker.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "content/browser/service_worker/service_worker_cache.pb.h" | 15 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 16 #include "content/browser/service_worker/service_worker_cache_scheduler.h" | 16 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/common/referrer.h" | 18 #include "content/public/common/referrer.h" |
| 19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/disk_cache/disk_cache.h" | 21 #include "net/disk_cache/disk_cache.h" |
| 22 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
| 23 #include "storage/browser/blob/blob_data_builder.h" | 23 #include "storage/browser/blob/blob_data_builder.h" |
| 24 #include "storage/browser/blob/blob_data_handle.h" | 24 #include "storage/browser/blob/blob_data_handle.h" |
| 25 #include "storage/browser/blob/blob_storage_context.h" | 25 #include "storage/browser/blob/blob_storage_context.h" |
| 26 #include "storage/browser/blob/blob_url_request_job_factory.h" | 26 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 27 #include "storage/browser/quota/quota_manager_proxy.h" | 27 #include "storage/browser/quota/quota_manager_proxy.h" |
| 28 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" | 28 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 typedef base::Callback<void(bool)> BoolCallback; | 34 typedef base::Callback<void(bool)> BoolCallback; |
| 35 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> | 35 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> |
| 36 EntryBoolCallback; | 36 EntryBoolCallback; |
| 37 typedef base::Callback<void(scoped_ptr<ServiceWorkerCacheMetadata>)> | 37 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; |
| 38 MetadataCallback; | |
| 39 | 38 |
| 40 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 39 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 41 | 40 |
| 42 // The maximum size of an individual cache. Ultimately cache size is controlled | 41 // The maximum size of an individual cache. Ultimately cache size is controlled |
| 43 // per-origin. | 42 // per-origin. |
| 44 const int kMaxCacheBytes = 512 * 1024 * 1024; | 43 const int kMaxCacheBytes = 512 * 1024 * 1024; |
| 45 | 44 |
| 46 // Buffer size for cache and blob reading/writing. | 45 // Buffer size for cache and blob reading/writing. |
| 47 const int kBufferSize = 1024 * 512; | 46 const int kBufferSize = 1024 * 512; |
| 48 | 47 |
| 49 void NotReachedCompletionCallback(int rv) { | 48 void NotReachedCompletionCallback(int rv) { |
| 50 NOTREACHED(); | 49 NOTREACHED(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( | 52 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( |
| 54 ServiceWorkerCacheResponse::ResponseType response_type) { | 53 CacheResponse::ResponseType response_type) { |
| 55 switch (response_type) { | 54 switch (response_type) { |
| 56 case ServiceWorkerCacheResponse::BASIC_TYPE: | 55 case CacheResponse::BASIC_TYPE: |
| 57 return blink::WebServiceWorkerResponseTypeBasic; | 56 return blink::WebServiceWorkerResponseTypeBasic; |
| 58 case ServiceWorkerCacheResponse::CORS_TYPE: | 57 case CacheResponse::CORS_TYPE: |
| 59 return blink::WebServiceWorkerResponseTypeCORS; | 58 return blink::WebServiceWorkerResponseTypeCORS; |
| 60 case ServiceWorkerCacheResponse::DEFAULT_TYPE: | 59 case CacheResponse::DEFAULT_TYPE: |
| 61 return blink::WebServiceWorkerResponseTypeDefault; | 60 return blink::WebServiceWorkerResponseTypeDefault; |
| 62 case ServiceWorkerCacheResponse::ERROR_TYPE: | 61 case CacheResponse::ERROR_TYPE: |
| 63 return blink::WebServiceWorkerResponseTypeError; | 62 return blink::WebServiceWorkerResponseTypeError; |
| 64 case ServiceWorkerCacheResponse::OPAQUE_TYPE: | 63 case CacheResponse::OPAQUE_TYPE: |
| 65 return blink::WebServiceWorkerResponseTypeOpaque; | 64 return blink::WebServiceWorkerResponseTypeOpaque; |
| 66 } | 65 } |
| 67 NOTREACHED(); | 66 NOTREACHED(); |
| 68 return blink::WebServiceWorkerResponseTypeOpaque; | 67 return blink::WebServiceWorkerResponseTypeOpaque; |
| 69 } | 68 } |
| 70 | 69 |
| 71 ServiceWorkerCacheResponse::ResponseType WebResponseTypeToProtoResponseType( | 70 CacheResponse::ResponseType WebResponseTypeToProtoResponseType( |
| 72 blink::WebServiceWorkerResponseType response_type) { | 71 blink::WebServiceWorkerResponseType response_type) { |
| 73 switch (response_type) { | 72 switch (response_type) { |
| 74 case blink::WebServiceWorkerResponseTypeBasic: | 73 case blink::WebServiceWorkerResponseTypeBasic: |
| 75 return ServiceWorkerCacheResponse::BASIC_TYPE; | 74 return CacheResponse::BASIC_TYPE; |
| 76 case blink::WebServiceWorkerResponseTypeCORS: | 75 case blink::WebServiceWorkerResponseTypeCORS: |
| 77 return ServiceWorkerCacheResponse::CORS_TYPE; | 76 return CacheResponse::CORS_TYPE; |
| 78 case blink::WebServiceWorkerResponseTypeDefault: | 77 case blink::WebServiceWorkerResponseTypeDefault: |
| 79 return ServiceWorkerCacheResponse::DEFAULT_TYPE; | 78 return CacheResponse::DEFAULT_TYPE; |
| 80 case blink::WebServiceWorkerResponseTypeError: | 79 case blink::WebServiceWorkerResponseTypeError: |
| 81 return ServiceWorkerCacheResponse::ERROR_TYPE; | 80 return CacheResponse::ERROR_TYPE; |
| 82 case blink::WebServiceWorkerResponseTypeOpaque: | 81 case blink::WebServiceWorkerResponseTypeOpaque: |
| 83 return ServiceWorkerCacheResponse::OPAQUE_TYPE; | 82 return CacheResponse::OPAQUE_TYPE; |
| 84 } | 83 } |
| 85 NOTREACHED(); | 84 NOTREACHED(); |
| 86 return ServiceWorkerCacheResponse::OPAQUE_TYPE; | 85 return CacheResponse::OPAQUE_TYPE; |
| 87 } | 86 } |
| 88 | 87 |
| 89 // Copy headers out of a cache entry and into a protobuf. The callback is | 88 // Copy headers out of a cache entry and into a protobuf. The callback is |
| 90 // guaranteed to be run. | 89 // guaranteed to be run. |
| 91 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback); | 90 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback); |
| 92 void ReadMetadataDidReadMetadata( | 91 void ReadMetadataDidReadMetadata( |
| 93 disk_cache::Entry* entry, | 92 disk_cache::Entry* entry, |
| 94 const MetadataCallback& callback, | 93 const MetadataCallback& callback, |
| 95 const scoped_refptr<net::IOBufferWithSize>& buffer, | 94 const scoped_refptr<net::IOBufferWithSize>& buffer, |
| 96 int rv); | 95 int rv); |
| 97 | 96 |
| 98 | |
| 99 bool VaryMatches(const ServiceWorkerHeaderMap& request, | 97 bool VaryMatches(const ServiceWorkerHeaderMap& request, |
| 100 const ServiceWorkerHeaderMap& cached_request, | 98 const ServiceWorkerHeaderMap& cached_request, |
| 101 const ServiceWorkerHeaderMap& response) { | 99 const ServiceWorkerHeaderMap& response) { |
| 102 ServiceWorkerHeaderMap::const_iterator vary_iter = response.find("vary"); | 100 ServiceWorkerHeaderMap::const_iterator vary_iter = response.find("vary"); |
| 103 if (vary_iter == response.end()) | 101 if (vary_iter == response.end()) |
| 104 return true; | 102 return true; |
| 105 | 103 |
| 106 std::vector<std::string> vary_keys; | 104 std::vector<std::string> vary_keys; |
| 107 Tokenize(vary_iter->second, ",", &vary_keys); | 105 Tokenize(vary_iter->second, ",", &vary_keys); |
| 108 for (std::vector<std::string>::const_iterator it = vary_keys.begin(); | 106 for (std::vector<std::string>::const_iterator it = vary_keys.begin(); |
| 109 it != vary_keys.end(); | 107 it != vary_keys.end(); ++it) { |
| 110 ++it) { | |
| 111 std::string trimmed; | 108 std::string trimmed; |
| 112 base::TrimWhitespaceASCII(*it, base::TRIM_ALL, &trimmed); | 109 base::TrimWhitespaceASCII(*it, base::TRIM_ALL, &trimmed); |
| 113 if (trimmed == "*") | 110 if (trimmed == "*") |
| 114 return false; | 111 return false; |
| 115 | 112 |
| 116 ServiceWorkerHeaderMap::const_iterator request_iter = request.find(trimmed); | 113 ServiceWorkerHeaderMap::const_iterator request_iter = request.find(trimmed); |
| 117 ServiceWorkerHeaderMap::const_iterator cached_request_iter = | 114 ServiceWorkerHeaderMap::const_iterator cached_request_iter = |
| 118 cached_request.find(trimmed); | 115 cached_request.find(trimmed); |
| 119 | 116 |
| 120 // If the header exists in one but not the other, no match. | 117 // If the header exists in one but not the other, no match. |
| 121 if ((request_iter == request.end()) != | 118 if ((request_iter == request.end()) != |
| 122 (cached_request_iter == cached_request.end())) | 119 (cached_request_iter == cached_request.end())) |
| 123 return false; | 120 return false; |
| 124 | 121 |
| 125 // If the header exists in one, it exists in both. Verify that the values | 122 // If the header exists in one, it exists in both. Verify that the values |
| 126 // are equal. | 123 // are equal. |
| 127 if (request_iter != request.end() && | 124 if (request_iter != request.end() && |
| 128 request_iter->second != cached_request_iter->second) | 125 request_iter->second != cached_request_iter->second) |
| 129 return false; | 126 return false; |
| 130 } | 127 } |
| 131 | 128 |
| 132 return true; | 129 return true; |
| 133 } | 130 } |
| 134 | 131 |
| 135 | |
| 136 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { | 132 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { |
| 137 DCHECK(entry); | 133 DCHECK(entry); |
| 138 | 134 |
| 139 scoped_refptr<net::IOBufferWithSize> buffer( | 135 scoped_refptr<net::IOBufferWithSize> buffer( |
| 140 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); | 136 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); |
| 141 | 137 |
| 142 net::CompletionCallback read_header_callback = | 138 net::CompletionCallback read_header_callback = |
| 143 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); | 139 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); |
| 144 | 140 |
| 145 int read_rv = entry->ReadData( | 141 int read_rv = entry->ReadData( |
| 146 INDEX_HEADERS, 0, buffer.get(), buffer->size(), | 142 INDEX_HEADERS, 0, buffer.get(), buffer->size(), |
| 147 tracked_objects::ScopedTracker::TrackCallback( | 143 tracked_objects::ScopedTracker::TrackCallback( |
| 148 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 ReadMetadata"), | 144 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 ReadMetadata"), |
| 149 read_header_callback)); | 145 read_header_callback)); |
| 150 | 146 |
| 151 if (read_rv != net::ERR_IO_PENDING) | 147 if (read_rv != net::ERR_IO_PENDING) |
| 152 read_header_callback.Run(read_rv); | 148 read_header_callback.Run(read_rv); |
| 153 } | 149 } |
| 154 | 150 |
| 155 void ReadMetadataDidReadMetadata( | 151 void ReadMetadataDidReadMetadata( |
| 156 disk_cache::Entry* entry, | 152 disk_cache::Entry* entry, |
| 157 const MetadataCallback& callback, | 153 const MetadataCallback& callback, |
| 158 const scoped_refptr<net::IOBufferWithSize>& buffer, | 154 const scoped_refptr<net::IOBufferWithSize>& buffer, |
| 159 int rv) { | 155 int rv) { |
| 160 if (rv != buffer->size()) { | 156 if (rv != buffer->size()) { |
| 161 callback.Run(scoped_ptr<ServiceWorkerCacheMetadata>()); | 157 callback.Run(scoped_ptr<CacheMetadata>()); |
| 162 return; | 158 return; |
| 163 } | 159 } |
| 164 | 160 |
| 165 scoped_ptr<ServiceWorkerCacheMetadata> metadata( | 161 scoped_ptr<CacheMetadata> metadata(new CacheMetadata()); |
| 166 new ServiceWorkerCacheMetadata()); | |
| 167 | 162 |
| 168 if (!metadata->ParseFromArray(buffer->data(), buffer->size())) { | 163 if (!metadata->ParseFromArray(buffer->data(), buffer->size())) { |
| 169 callback.Run(scoped_ptr<ServiceWorkerCacheMetadata>()); | 164 callback.Run(scoped_ptr<CacheMetadata>()); |
| 170 return; | 165 return; |
| 171 } | 166 } |
| 172 | 167 |
| 173 callback.Run(metadata.Pass()); | 168 callback.Run(metadata.Pass()); |
| 174 } | 169 } |
| 175 | 170 |
| 176 } // namespace | 171 } // namespace |
| 177 | 172 |
| 178 // Streams data from a blob and writes it to a given disk_cache::Entry. | 173 // Streams data from a blob and writes it to a given disk_cache::Entry. |
| 179 class ServiceWorkerCache::BlobReader : public net::URLRequest::Delegate { | 174 class CacheStorageCache::BlobReader : public net::URLRequest::Delegate { |
| 180 public: | 175 public: |
| 181 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> | 176 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> |
| 182 EntryAndBoolCallback; | 177 EntryAndBoolCallback; |
| 183 | 178 |
| 184 BlobReader() | 179 BlobReader() |
| 185 : cache_entry_offset_(0), | 180 : cache_entry_offset_(0), |
| 186 buffer_(new net::IOBufferWithSize(kBufferSize)), | 181 buffer_(new net::IOBufferWithSize(kBufferSize)), |
| 187 weak_ptr_factory_(this) {} | 182 weak_ptr_factory_(this) {} |
| 188 | 183 |
| 189 // |entry| is passed to the callback once complete. | 184 // |entry| is passed to the callback once complete. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 220 NOTREACHED(); | 215 NOTREACHED(); |
| 221 } | 216 } |
| 222 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override { | 217 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override { |
| 223 NOTREACHED(); | 218 NOTREACHED(); |
| 224 } | 219 } |
| 225 | 220 |
| 226 void OnResponseStarted(net::URLRequest* request) override { | 221 void OnResponseStarted(net::URLRequest* request) override { |
| 227 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | 222 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 228 tracked_objects::ScopedTracker tracking_profile( | 223 tracked_objects::ScopedTracker tracking_profile( |
| 229 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 224 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 230 "423948 ServiceWorkerCache::BlobReader::OnResponseStarted")); | 225 "423948 CacheStorageCache::BlobReader::OnResponseStarted")); |
| 231 | 226 |
| 232 if (!request->status().is_success()) { | 227 if (!request->status().is_success()) { |
| 233 callback_.Run(entry_.Pass(), false); | 228 callback_.Run(entry_.Pass(), false); |
| 234 return; | 229 return; |
| 235 } | 230 } |
| 236 ReadFromBlob(); | 231 ReadFromBlob(); |
| 237 } | 232 } |
| 238 | 233 |
| 239 virtual void ReadFromBlob() { | 234 virtual void ReadFromBlob() { |
| 240 int bytes_read = 0; | 235 int bytes_read = 0; |
| 241 bool done = | 236 bool done = |
| 242 blob_request_->Read(buffer_.get(), buffer_->size(), &bytes_read); | 237 blob_request_->Read(buffer_.get(), buffer_->size(), &bytes_read); |
| 243 if (done) | 238 if (done) |
| 244 OnReadCompleted(blob_request_.get(), bytes_read); | 239 OnReadCompleted(blob_request_.get(), bytes_read); |
| 245 } | 240 } |
| 246 | 241 |
| 247 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { | 242 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { |
| 248 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | 243 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 249 tracked_objects::ScopedTracker tracking_profile( | 244 tracked_objects::ScopedTracker tracking_profile( |
| 250 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 245 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 251 "423948 ServiceWorkerCache::BlobReader::OnReadCompleted")); | 246 "423948 CacheStorageCache::BlobReader::OnReadCompleted")); |
| 252 | 247 |
| 253 if (!request->status().is_success()) { | 248 if (!request->status().is_success()) { |
| 254 callback_.Run(entry_.Pass(), false); | 249 callback_.Run(entry_.Pass(), false); |
| 255 return; | 250 return; |
| 256 } | 251 } |
| 257 | 252 |
| 258 if (bytes_read == 0) { | 253 if (bytes_read == 0) { |
| 259 callback_.Run(entry_.Pass(), true); | 254 callback_.Run(entry_.Pass(), true); |
| 260 return; | 255 return; |
| 261 } | 256 } |
| 262 | 257 |
| 263 net::CompletionCallback cache_write_callback = | 258 net::CompletionCallback cache_write_callback = |
| 264 base::Bind(&BlobReader::DidWriteDataToEntry, | 259 base::Bind(&BlobReader::DidWriteDataToEntry, |
| 265 weak_ptr_factory_.GetWeakPtr(), | 260 weak_ptr_factory_.GetWeakPtr(), bytes_read); |
| 266 bytes_read); | |
| 267 | 261 |
| 268 int rv = entry_->WriteData(INDEX_RESPONSE_BODY, | 262 int rv = entry_->WriteData(INDEX_RESPONSE_BODY, cache_entry_offset_, |
| 269 cache_entry_offset_, | 263 buffer_.get(), bytes_read, cache_write_callback, |
| 270 buffer_.get(), | |
| 271 bytes_read, | |
| 272 cache_write_callback, | |
| 273 true /* truncate */); | 264 true /* truncate */); |
| 274 if (rv != net::ERR_IO_PENDING) | 265 if (rv != net::ERR_IO_PENDING) |
| 275 cache_write_callback.Run(rv); | 266 cache_write_callback.Run(rv); |
| 276 } | 267 } |
| 277 | 268 |
| 278 void DidWriteDataToEntry(int expected_bytes, int rv) { | 269 void DidWriteDataToEntry(int expected_bytes, int rv) { |
| 279 if (rv != expected_bytes) { | 270 if (rv != expected_bytes) { |
| 280 callback_.Run(entry_.Pass(), false); | 271 callback_.Run(entry_.Pass(), false); |
| 281 return; | 272 return; |
| 282 } | 273 } |
| 283 | 274 |
| 284 cache_entry_offset_ += rv; | 275 cache_entry_offset_ += rv; |
| 285 ReadFromBlob(); | 276 ReadFromBlob(); |
| 286 } | 277 } |
| 287 | 278 |
| 288 private: | 279 private: |
| 289 int cache_entry_offset_; | 280 int cache_entry_offset_; |
| 290 disk_cache::ScopedEntryPtr entry_; | 281 disk_cache::ScopedEntryPtr entry_; |
| 291 scoped_ptr<net::URLRequest> blob_request_; | 282 scoped_ptr<net::URLRequest> blob_request_; |
| 292 EntryAndBoolCallback callback_; | 283 EntryAndBoolCallback callback_; |
| 293 scoped_refptr<net::IOBufferWithSize> buffer_; | 284 scoped_refptr<net::IOBufferWithSize> buffer_; |
| 294 base::WeakPtrFactory<BlobReader> weak_ptr_factory_; | 285 base::WeakPtrFactory<BlobReader> weak_ptr_factory_; |
| 295 }; | 286 }; |
| 296 | 287 |
| 297 // The state needed to pass between ServiceWorkerCache::Keys callbacks. | 288 // The state needed to pass between CacheStorageCache::Keys callbacks. |
| 298 struct ServiceWorkerCache::KeysContext { | 289 struct CacheStorageCache::KeysContext { |
| 299 KeysContext(const ServiceWorkerCache::RequestsCallback& callback) | 290 KeysContext(const CacheStorageCache::RequestsCallback& callback) |
| 300 : original_callback(callback), | 291 : original_callback(callback), |
| 301 out_keys(new ServiceWorkerCache::Requests()), | 292 out_keys(new CacheStorageCache::Requests()), |
| 302 enumerated_entry(NULL) {} | 293 enumerated_entry(NULL) {} |
| 303 | 294 |
| 304 ~KeysContext() { | 295 ~KeysContext() { |
| 305 for (size_t i = 0, max = entries.size(); i < max; ++i) | 296 for (size_t i = 0, max = entries.size(); i < max; ++i) |
| 306 entries[i]->Close(); | 297 entries[i]->Close(); |
| 307 if (enumerated_entry) | 298 if (enumerated_entry) |
| 308 enumerated_entry->Close(); | 299 enumerated_entry->Close(); |
| 309 } | 300 } |
| 310 | 301 |
| 311 // The callback passed to the Keys() function. | 302 // The callback passed to the Keys() function. |
| 312 ServiceWorkerCache::RequestsCallback original_callback; | 303 CacheStorageCache::RequestsCallback original_callback; |
| 313 | 304 |
| 314 // The vector of open entries in the backend. | 305 // The vector of open entries in the backend. |
| 315 Entries entries; | 306 Entries entries; |
| 316 | 307 |
| 317 // The output of the Keys function. | 308 // The output of the Keys function. |
| 318 scoped_ptr<ServiceWorkerCache::Requests> out_keys; | 309 scoped_ptr<CacheStorageCache::Requests> out_keys; |
| 319 | 310 |
| 320 // Used for enumerating cache entries. | 311 // Used for enumerating cache entries. |
| 321 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 312 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
| 322 disk_cache::Entry* enumerated_entry; | 313 disk_cache::Entry* enumerated_entry; |
| 323 | 314 |
| 324 DISALLOW_COPY_AND_ASSIGN(KeysContext); | 315 DISALLOW_COPY_AND_ASSIGN(KeysContext); |
| 325 }; | 316 }; |
| 326 | 317 |
| 327 struct ServiceWorkerCache::MatchContext { | 318 struct CacheStorageCache::MatchContext { |
| 328 MatchContext(scoped_ptr<ServiceWorkerFetchRequest> request, | 319 MatchContext(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 329 const ServiceWorkerCache::ResponseCallback& callback, | 320 const CacheStorageCache::ResponseCallback& callback, |
| 330 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) | 321 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) |
| 331 : request(request.Pass()), | 322 : request(request.Pass()), |
| 332 original_callback(callback), | 323 original_callback(callback), |
| 333 blob_storage_context(blob_storage_context), | 324 blob_storage_context(blob_storage_context), |
| 334 entry(nullptr), | 325 entry(nullptr), |
| 335 total_bytes_read(0) {} | 326 total_bytes_read(0) {} |
| 336 | 327 |
| 337 ~MatchContext() { | 328 ~MatchContext() { |
| 338 if (entry) | 329 if (entry) |
| 339 entry->Close(); | 330 entry->Close(); |
| 340 } | 331 } |
| 341 | 332 |
| 342 // Input | 333 // Input |
| 343 scoped_ptr<ServiceWorkerFetchRequest> request; | 334 scoped_ptr<ServiceWorkerFetchRequest> request; |
| 344 ServiceWorkerCache::ResponseCallback original_callback; | 335 CacheStorageCache::ResponseCallback original_callback; |
| 345 base::WeakPtr<storage::BlobStorageContext> blob_storage_context; | 336 base::WeakPtr<storage::BlobStorageContext> blob_storage_context; |
| 346 disk_cache::Entry* entry; | 337 disk_cache::Entry* entry; |
| 347 | 338 |
| 348 // Output | 339 // Output |
| 349 scoped_ptr<ServiceWorkerResponse> response; | 340 scoped_ptr<ServiceWorkerResponse> response; |
| 350 scoped_ptr<storage::BlobDataBuilder> blob_data; | 341 scoped_ptr<storage::BlobDataBuilder> blob_data; |
| 351 | 342 |
| 352 // For reading the cache entry data into a blob. | 343 // For reading the cache entry data into a blob. |
| 353 scoped_refptr<net::IOBufferWithSize> response_body_buffer; | 344 scoped_refptr<net::IOBufferWithSize> response_body_buffer; |
| 354 size_t total_bytes_read; | 345 size_t total_bytes_read; |
| 355 | 346 |
| 356 DISALLOW_COPY_AND_ASSIGN(MatchContext); | 347 DISALLOW_COPY_AND_ASSIGN(MatchContext); |
| 357 }; | 348 }; |
| 358 | 349 |
| 359 // The state needed to pass between ServiceWorkerCache::Put callbacks. | 350 // The state needed to pass between CacheStorageCache::Put callbacks. |
| 360 struct ServiceWorkerCache::PutContext { | 351 struct CacheStorageCache::PutContext { |
| 361 PutContext( | 352 PutContext( |
| 362 const GURL& origin, | 353 const GURL& origin, |
| 363 scoped_ptr<ServiceWorkerFetchRequest> request, | 354 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 364 scoped_ptr<ServiceWorkerResponse> response, | 355 scoped_ptr<ServiceWorkerResponse> response, |
| 365 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 356 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 366 const ServiceWorkerCache::ResponseCallback& callback, | 357 const CacheStorageCache::ResponseCallback& callback, |
| 367 net::URLRequestContext* request_context, | 358 net::URLRequestContext* request_context, |
| 368 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) | 359 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) |
| 369 : origin(origin), | 360 : origin(origin), |
| 370 request(request.Pass()), | 361 request(request.Pass()), |
| 371 response(response.Pass()), | 362 response(response.Pass()), |
| 372 blob_data_handle(blob_data_handle.Pass()), | 363 blob_data_handle(blob_data_handle.Pass()), |
| 373 callback(callback), | 364 callback(callback), |
| 374 request_context(request_context), | 365 request_context(request_context), |
| 375 quota_manager_proxy(quota_manager_proxy), | 366 quota_manager_proxy(quota_manager_proxy), |
| 376 cache_entry(NULL) {} | 367 cache_entry(NULL) {} |
| 377 ~PutContext() { | 368 ~PutContext() { |
| 378 if (cache_entry) | 369 if (cache_entry) |
| 379 cache_entry->Close(); | 370 cache_entry->Close(); |
| 380 } | 371 } |
| 381 | 372 |
| 382 // Input parameters to the Put function. | 373 // Input parameters to the Put function. |
| 383 GURL origin; | 374 GURL origin; |
| 384 scoped_ptr<ServiceWorkerFetchRequest> request; | 375 scoped_ptr<ServiceWorkerFetchRequest> request; |
| 385 scoped_ptr<ServiceWorkerResponse> response; | 376 scoped_ptr<ServiceWorkerResponse> response; |
| 386 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 377 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
| 387 ServiceWorkerCache::ResponseCallback callback; | 378 CacheStorageCache::ResponseCallback callback; |
| 388 net::URLRequestContext* request_context; | 379 net::URLRequestContext* request_context; |
| 389 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; | 380 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; |
| 390 | 381 |
| 391 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to | 382 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to |
| 392 // CreateEntry. | 383 // CreateEntry. |
| 393 disk_cache::Entry* cache_entry; | 384 disk_cache::Entry* cache_entry; |
| 394 | 385 |
| 395 // The BlobDataHandle for the output ServiceWorkerResponse. | 386 // The BlobDataHandle for the output ServiceWorkerResponse. |
| 396 scoped_ptr<storage::BlobDataHandle> out_blob_data_handle; | 387 scoped_ptr<storage::BlobDataHandle> out_blob_data_handle; |
| 397 | 388 |
| 398 DISALLOW_COPY_AND_ASSIGN(PutContext); | 389 DISALLOW_COPY_AND_ASSIGN(PutContext); |
| 399 }; | 390 }; |
| 400 | 391 |
| 401 // static | 392 // static |
| 402 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( | 393 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( |
| 403 const GURL& origin, | 394 const GURL& origin, |
| 404 net::URLRequestContext* request_context, | 395 net::URLRequestContext* request_context, |
| 405 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 396 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 406 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 397 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
| 407 return make_scoped_refptr(new ServiceWorkerCache(origin, | 398 return make_scoped_refptr( |
| 408 base::FilePath(), | 399 new CacheStorageCache(origin, base::FilePath(), request_context, |
| 409 request_context, | 400 quota_manager_proxy, blob_context)); |
| 410 quota_manager_proxy, | |
| 411 blob_context)); | |
| 412 } | 401 } |
| 413 | 402 |
| 414 // static | 403 // static |
| 415 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache( | 404 scoped_refptr<CacheStorageCache> CacheStorageCache::CreatePersistentCache( |
| 416 const GURL& origin, | 405 const GURL& origin, |
| 417 const base::FilePath& path, | 406 const base::FilePath& path, |
| 418 net::URLRequestContext* request_context, | 407 net::URLRequestContext* request_context, |
| 419 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 408 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 420 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 409 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
| 421 return make_scoped_refptr(new ServiceWorkerCache( | 410 return make_scoped_refptr(new CacheStorageCache( |
| 422 origin, path, request_context, quota_manager_proxy, blob_context)); | 411 origin, path, request_context, quota_manager_proxy, blob_context)); |
| 423 } | 412 } |
| 424 | 413 |
| 425 ServiceWorkerCache::~ServiceWorkerCache() { | 414 CacheStorageCache::~CacheStorageCache() { |
| 426 } | 415 } |
| 427 | 416 |
| 428 base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() { | 417 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { |
| 429 return weak_ptr_factory_.GetWeakPtr(); | 418 return weak_ptr_factory_.GetWeakPtr(); |
| 430 } | 419 } |
| 431 | 420 |
| 432 void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 421 void CacheStorageCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 433 scoped_ptr<ServiceWorkerResponse> response, | 422 scoped_ptr<ServiceWorkerResponse> response, |
| 434 const ResponseCallback& callback) { | 423 const ResponseCallback& callback) { |
| 435 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 424 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
| 436 | 425 |
| 437 if (!response->blob_uuid.empty()) { | 426 if (!response->blob_uuid.empty()) { |
| 438 if (!blob_storage_context_) { | 427 if (!blob_storage_context_) { |
| 439 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), | 428 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), |
| 440 scoped_ptr<storage::BlobDataHandle>()); | 429 scoped_ptr<storage::BlobDataHandle>()); |
| 441 return; | 430 return; |
| 442 } | 431 } |
| 443 blob_data_handle = | 432 blob_data_handle = |
| 444 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); | 433 blob_storage_context_->GetBlobDataFromUUID(response->blob_uuid); |
| 445 if (!blob_data_handle) { | 434 if (!blob_data_handle) { |
| 446 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), | 435 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), |
| 447 scoped_ptr<storage::BlobDataHandle>()); | 436 scoped_ptr<storage::BlobDataHandle>()); |
| 448 return; | 437 return; |
| 449 } | 438 } |
| 450 } | 439 } |
| 451 | 440 |
| 452 ResponseCallback pending_callback = | 441 ResponseCallback pending_callback = |
| 453 base::Bind(&ServiceWorkerCache::PendingResponseCallback, | 442 base::Bind(&CacheStorageCache::PendingResponseCallback, |
| 454 weak_ptr_factory_.GetWeakPtr(), callback); | 443 weak_ptr_factory_.GetWeakPtr(), callback); |
| 455 | 444 |
| 456 scoped_ptr<PutContext> put_context(new PutContext( | 445 scoped_ptr<PutContext> put_context(new PutContext( |
| 457 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(), | 446 origin_, request.Pass(), response.Pass(), blob_data_handle.Pass(), |
| 458 pending_callback, request_context_, quota_manager_proxy_)); | 447 pending_callback, request_context_, quota_manager_proxy_)); |
| 459 | 448 |
| 460 if (put_context->blob_data_handle) { | 449 if (put_context->blob_data_handle) { |
| 461 // Grab another handle to the blob for the callback response. | 450 // Grab another handle to the blob for the callback response. |
| 462 put_context->out_blob_data_handle = | 451 put_context->out_blob_data_handle = |
| 463 blob_storage_context_->GetBlobDataFromUUID( | 452 blob_storage_context_->GetBlobDataFromUUID( |
| 464 put_context->response->blob_uuid); | 453 put_context->response->blob_uuid); |
| 465 } | 454 } |
| 466 | 455 |
| 467 if (backend_state_ == BACKEND_UNINITIALIZED) | 456 if (backend_state_ == BACKEND_UNINITIALIZED) |
| 468 InitBackend(); | 457 InitBackend(); |
| 469 | 458 |
| 470 scheduler_->ScheduleOperation(base::Bind(&ServiceWorkerCache::PutImpl, | 459 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::PutImpl, |
| 471 weak_ptr_factory_.GetWeakPtr(), | 460 weak_ptr_factory_.GetWeakPtr(), |
| 472 base::Passed(put_context.Pass()))); | 461 base::Passed(put_context.Pass()))); |
| 473 } | 462 } |
| 474 | 463 |
| 475 void ServiceWorkerCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 464 void CacheStorageCache::Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 476 const ResponseCallback& callback) { | 465 const ResponseCallback& callback) { |
| 477 switch (backend_state_) { | 466 switch (backend_state_) { |
| 478 case BACKEND_UNINITIALIZED: | 467 case BACKEND_UNINITIALIZED: |
| 479 InitBackend(); | 468 InitBackend(); |
| 480 break; | 469 break; |
| 481 case BACKEND_CLOSED: | 470 case BACKEND_CLOSED: |
| 482 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), | 471 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), |
| 483 scoped_ptr<storage::BlobDataHandle>()); | 472 scoped_ptr<storage::BlobDataHandle>()); |
| 484 return; | 473 return; |
| 485 case BACKEND_OPEN: | 474 case BACKEND_OPEN: |
| 486 DCHECK(backend_); | 475 DCHECK(backend_); |
| 487 break; | 476 break; |
| 488 } | 477 } |
| 489 | 478 |
| 490 ResponseCallback pending_callback = | 479 ResponseCallback pending_callback = |
| 491 base::Bind(&ServiceWorkerCache::PendingResponseCallback, | 480 base::Bind(&CacheStorageCache::PendingResponseCallback, |
| 492 weak_ptr_factory_.GetWeakPtr(), callback); | 481 weak_ptr_factory_.GetWeakPtr(), callback); |
| 493 scheduler_->ScheduleOperation( | 482 scheduler_->ScheduleOperation( |
| 494 base::Bind(&ServiceWorkerCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), | 483 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), |
| 495 base::Passed(request.Pass()), pending_callback)); | 484 base::Passed(request.Pass()), pending_callback)); |
| 496 } | 485 } |
| 497 | 486 |
| 498 void ServiceWorkerCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 487 void CacheStorageCache::Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 499 const ErrorCallback& callback) { | 488 const ErrorCallback& callback) { |
| 500 switch (backend_state_) { | 489 switch (backend_state_) { |
| 501 case BACKEND_UNINITIALIZED: | 490 case BACKEND_UNINITIALIZED: |
| 502 InitBackend(); | 491 InitBackend(); |
| 503 break; | 492 break; |
| 504 case BACKEND_CLOSED: | 493 case BACKEND_CLOSED: |
| 505 callback.Run(ERROR_TYPE_STORAGE); | 494 callback.Run(ERROR_TYPE_STORAGE); |
| 506 return; | 495 return; |
| 507 case BACKEND_OPEN: | 496 case BACKEND_OPEN: |
| 508 DCHECK(backend_); | 497 DCHECK(backend_); |
| 509 break; | 498 break; |
| 510 } | 499 } |
| 511 ErrorCallback pending_callback = | 500 ErrorCallback pending_callback = |
| 512 base::Bind(&ServiceWorkerCache::PendingErrorCallback, | 501 base::Bind(&CacheStorageCache::PendingErrorCallback, |
| 513 weak_ptr_factory_.GetWeakPtr(), callback); | 502 weak_ptr_factory_.GetWeakPtr(), callback); |
| 514 scheduler_->ScheduleOperation(base::Bind( | 503 scheduler_->ScheduleOperation( |
| 515 &ServiceWorkerCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), | 504 base::Bind(&CacheStorageCache::DeleteImpl, weak_ptr_factory_.GetWeakPtr(), |
| 516 base::Passed(request.Pass()), pending_callback)); | 505 base::Passed(request.Pass()), pending_callback)); |
| 517 } | 506 } |
| 518 | 507 |
| 519 void ServiceWorkerCache::Keys(const RequestsCallback& callback) { | 508 void CacheStorageCache::Keys(const RequestsCallback& callback) { |
| 520 switch (backend_state_) { | 509 switch (backend_state_) { |
| 521 case BACKEND_UNINITIALIZED: | 510 case BACKEND_UNINITIALIZED: |
| 522 InitBackend(); | 511 InitBackend(); |
| 523 break; | 512 break; |
| 524 case BACKEND_CLOSED: | 513 case BACKEND_CLOSED: |
| 525 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<Requests>()); | 514 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<Requests>()); |
| 526 return; | 515 return; |
| 527 case BACKEND_OPEN: | 516 case BACKEND_OPEN: |
| 528 DCHECK(backend_); | 517 DCHECK(backend_); |
| 529 break; | 518 break; |
| 530 } | 519 } |
| 531 | 520 |
| 532 RequestsCallback pending_callback = | 521 RequestsCallback pending_callback = |
| 533 base::Bind(&ServiceWorkerCache::PendingRequestsCallback, | 522 base::Bind(&CacheStorageCache::PendingRequestsCallback, |
| 534 weak_ptr_factory_.GetWeakPtr(), callback); | 523 weak_ptr_factory_.GetWeakPtr(), callback); |
| 535 scheduler_->ScheduleOperation(base::Bind(&ServiceWorkerCache::KeysImpl, | 524 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::KeysImpl, |
| 536 weak_ptr_factory_.GetWeakPtr(), | 525 weak_ptr_factory_.GetWeakPtr(), |
| 537 pending_callback)); | 526 pending_callback)); |
| 538 } | 527 } |
| 539 | 528 |
| 540 void ServiceWorkerCache::Close(const base::Closure& callback) { | 529 void CacheStorageCache::Close(const base::Closure& callback) { |
| 541 DCHECK(backend_state_ != BACKEND_CLOSED) | 530 DCHECK(backend_state_ != BACKEND_CLOSED) |
| 542 << "Don't call ServiceWorkerCache::Close() twice."; | 531 << "Don't call CacheStorageCache::Close() twice."; |
| 543 | 532 |
| 544 base::Closure pending_callback = | 533 base::Closure pending_callback = |
| 545 base::Bind(&ServiceWorkerCache::PendingClosure, | 534 base::Bind(&CacheStorageCache::PendingClosure, |
| 546 weak_ptr_factory_.GetWeakPtr(), callback); | 535 weak_ptr_factory_.GetWeakPtr(), callback); |
| 547 | 536 |
| 548 scheduler_->ScheduleOperation(base::Bind(&ServiceWorkerCache::CloseImpl, | 537 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::CloseImpl, |
| 549 weak_ptr_factory_.GetWeakPtr(), | 538 weak_ptr_factory_.GetWeakPtr(), |
| 550 pending_callback)); | 539 pending_callback)); |
| 551 } | 540 } |
| 552 | 541 |
| 553 int64 ServiceWorkerCache::MemoryBackedSize() const { | 542 int64 CacheStorageCache::MemoryBackedSize() const { |
| 554 if (backend_state_ != BACKEND_OPEN || !memory_only_) | 543 if (backend_state_ != BACKEND_OPEN || !memory_only_) |
| 555 return 0; | 544 return 0; |
| 556 | 545 |
| 557 scoped_ptr<disk_cache::Backend::Iterator> backend_iter = | 546 scoped_ptr<disk_cache::Backend::Iterator> backend_iter = |
| 558 backend_->CreateIterator(); | 547 backend_->CreateIterator(); |
| 559 disk_cache::Entry* entry = nullptr; | 548 disk_cache::Entry* entry = nullptr; |
| 560 | 549 |
| 561 int64 sum = 0; | 550 int64 sum = 0; |
| 562 | 551 |
| 563 std::vector<disk_cache::Entry*> entries; | 552 std::vector<disk_cache::Entry*> entries; |
| 564 int rv = net::OK; | 553 int rv = net::OK; |
| 565 while ((rv = backend_iter->OpenNextEntry( | 554 while ((rv = backend_iter->OpenNextEntry( |
| 566 &entry, base::Bind(NotReachedCompletionCallback))) == net::OK) { | 555 &entry, base::Bind(NotReachedCompletionCallback))) == net::OK) { |
| 567 entries.push_back(entry); // Open the entries without mutating them. | 556 entries.push_back(entry); // Open the entries without mutating them. |
| 568 } | 557 } |
| 569 DCHECK(rv != | 558 DCHECK(rv != |
| 570 net::ERR_IO_PENDING); // Expect all memory ops to be synchronous. | 559 net::ERR_IO_PENDING); // Expect all memory ops to be synchronous. |
| 571 | 560 |
| 572 for (disk_cache::Entry* entry : entries) { | 561 for (disk_cache::Entry* entry : entries) { |
| 573 sum += entry->GetDataSize(INDEX_HEADERS) + | 562 sum += entry->GetDataSize(INDEX_HEADERS) + |
| 574 entry->GetDataSize(INDEX_RESPONSE_BODY); | 563 entry->GetDataSize(INDEX_RESPONSE_BODY); |
| 575 entry->Close(); | 564 entry->Close(); |
| 576 } | 565 } |
| 577 | 566 |
| 578 return sum; | 567 return sum; |
| 579 } | 568 } |
| 580 | 569 |
| 581 ServiceWorkerCache::ServiceWorkerCache( | 570 CacheStorageCache::CacheStorageCache( |
| 582 const GURL& origin, | 571 const GURL& origin, |
| 583 const base::FilePath& path, | 572 const base::FilePath& path, |
| 584 net::URLRequestContext* request_context, | 573 net::URLRequestContext* request_context, |
| 585 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 574 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 586 base::WeakPtr<storage::BlobStorageContext> blob_context) | 575 base::WeakPtr<storage::BlobStorageContext> blob_context) |
| 587 : origin_(origin), | 576 : origin_(origin), |
| 588 path_(path), | 577 path_(path), |
| 589 request_context_(request_context), | 578 request_context_(request_context), |
| 590 quota_manager_proxy_(quota_manager_proxy), | 579 quota_manager_proxy_(quota_manager_proxy), |
| 591 blob_storage_context_(blob_context), | 580 blob_storage_context_(blob_context), |
| 592 backend_state_(BACKEND_UNINITIALIZED), | 581 backend_state_(BACKEND_UNINITIALIZED), |
| 593 scheduler_(new ServiceWorkerCacheScheduler()), | 582 scheduler_(new CacheStorageScheduler()), |
| 594 initializing_(false), | 583 initializing_(false), |
| 595 memory_only_(path.empty()), | 584 memory_only_(path.empty()), |
| 596 weak_ptr_factory_(this) { | 585 weak_ptr_factory_(this) { |
| 597 } | 586 } |
| 598 | 587 |
| 599 void ServiceWorkerCache::MatchImpl( | 588 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 600 scoped_ptr<ServiceWorkerFetchRequest> request, | 589 const ResponseCallback& callback) { |
| 601 const ResponseCallback& callback) { | |
| 602 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); | 590 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); |
| 603 if (backend_state_ != BACKEND_OPEN) { | 591 if (backend_state_ != BACKEND_OPEN) { |
| 604 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), | 592 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<ServiceWorkerResponse>(), |
| 605 scoped_ptr<storage::BlobDataHandle>()); | 593 scoped_ptr<storage::BlobDataHandle>()); |
| 606 return; | 594 return; |
| 607 } | 595 } |
| 608 | 596 |
| 609 scoped_ptr<MatchContext> match_context( | 597 scoped_ptr<MatchContext> match_context( |
| 610 new MatchContext(request.Pass(), callback, blob_storage_context_)); | 598 new MatchContext(request.Pass(), callback, blob_storage_context_)); |
| 611 | 599 |
| 612 disk_cache::Entry** entry_ptr = &match_context->entry; | 600 disk_cache::Entry** entry_ptr = &match_context->entry; |
| 613 ServiceWorkerFetchRequest* request_ptr = match_context->request.get(); | 601 ServiceWorkerFetchRequest* request_ptr = match_context->request.get(); |
| 614 | 602 |
| 615 net::CompletionCallback open_entry_callback = base::Bind( | 603 net::CompletionCallback open_entry_callback = base::Bind( |
| 616 &ServiceWorkerCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), | 604 &CacheStorageCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), |
| 617 base::Passed(match_context.Pass())); | 605 base::Passed(match_context.Pass())); |
| 618 | 606 |
| 619 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, | 607 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, |
| 620 open_entry_callback); | 608 open_entry_callback); |
| 621 if (rv != net::ERR_IO_PENDING) | 609 if (rv != net::ERR_IO_PENDING) |
| 622 open_entry_callback.Run(rv); | 610 open_entry_callback.Run(rv); |
| 623 } | 611 } |
| 624 | 612 |
| 625 void ServiceWorkerCache::MatchDidOpenEntry( | 613 void CacheStorageCache::MatchDidOpenEntry( |
| 626 scoped_ptr<MatchContext> match_context, | 614 scoped_ptr<MatchContext> match_context, |
| 627 int rv) { | 615 int rv) { |
| 628 if (rv != net::OK) { | 616 if (rv != net::OK) { |
| 629 match_context->original_callback.Run( | 617 match_context->original_callback.Run( |
| 630 ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, | 618 CacheStorageCache::ERROR_TYPE_NOT_FOUND, |
| 631 scoped_ptr<ServiceWorkerResponse>(), | 619 scoped_ptr<ServiceWorkerResponse>(), |
| 632 scoped_ptr<storage::BlobDataHandle>()); | 620 scoped_ptr<storage::BlobDataHandle>()); |
| 633 return; | 621 return; |
| 634 } | 622 } |
| 635 | 623 |
| 636 // Copy the entry pointer before passing it in base::Bind. | 624 // Copy the entry pointer before passing it in base::Bind. |
| 637 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | 625 disk_cache::Entry* tmp_entry_ptr = match_context->entry; |
| 638 DCHECK(tmp_entry_ptr); | 626 DCHECK(tmp_entry_ptr); |
| 639 | 627 |
| 640 MetadataCallback headers_callback = base::Bind( | 628 MetadataCallback headers_callback = base::Bind( |
| 641 &ServiceWorkerCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), | 629 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), |
| 642 base::Passed(match_context.Pass())); | 630 base::Passed(match_context.Pass())); |
| 643 | 631 |
| 644 ReadMetadata(tmp_entry_ptr, headers_callback); | 632 ReadMetadata(tmp_entry_ptr, headers_callback); |
| 645 } | 633 } |
| 646 | 634 |
| 647 void ServiceWorkerCache::MatchDidReadMetadata( | 635 void CacheStorageCache::MatchDidReadMetadata( |
| 648 scoped_ptr<MatchContext> match_context, | 636 scoped_ptr<MatchContext> match_context, |
| 649 scoped_ptr<ServiceWorkerCacheMetadata> metadata) { | 637 scoped_ptr<CacheMetadata> metadata) { |
| 650 if (!metadata) { | 638 if (!metadata) { |
| 651 match_context->original_callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 639 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 652 scoped_ptr<ServiceWorkerResponse>(), | 640 scoped_ptr<ServiceWorkerResponse>(), |
| 653 scoped_ptr<storage::BlobDataHandle>()); | 641 scoped_ptr<storage::BlobDataHandle>()); |
| 654 return; | 642 return; |
| 655 } | 643 } |
| 656 | 644 |
| 657 match_context->response.reset(new ServiceWorkerResponse( | 645 match_context->response.reset(new ServiceWorkerResponse( |
| 658 match_context->request->url, metadata->response().status_code(), | 646 match_context->request->url, metadata->response().status_code(), |
| 659 metadata->response().status_text(), | 647 metadata->response().status_text(), |
| 660 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), | 648 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), |
| 661 ServiceWorkerHeaderMap(), "", 0, GURL())); | 649 ServiceWorkerHeaderMap(), "", 0, GURL())); |
| 662 | 650 |
| 663 ServiceWorkerResponse* response = match_context->response.get(); | 651 ServiceWorkerResponse* response = match_context->response.get(); |
| 664 | 652 |
| 665 if (metadata->response().has_url()) | 653 if (metadata->response().has_url()) |
| 666 response->url = GURL(metadata->response().url()); | 654 response->url = GURL(metadata->response().url()); |
| 667 | 655 |
| 668 for (int i = 0; i < metadata->response().headers_size(); ++i) { | 656 for (int i = 0; i < metadata->response().headers_size(); ++i) { |
| 669 const ServiceWorkerCacheHeaderMap header = metadata->response().headers(i); | 657 const CacheHeaderMap header = metadata->response().headers(i); |
| 670 DCHECK(header.name().find('\0') == std::string::npos); | 658 DCHECK(header.name().find('\0') == std::string::npos); |
| 671 DCHECK(header.value().find('\0') == std::string::npos); | 659 DCHECK(header.value().find('\0') == std::string::npos); |
| 672 response->headers.insert(std::make_pair(header.name(), header.value())); | 660 response->headers.insert(std::make_pair(header.name(), header.value())); |
| 673 } | 661 } |
| 674 | 662 |
| 675 ServiceWorkerHeaderMap cached_request_headers; | 663 ServiceWorkerHeaderMap cached_request_headers; |
| 676 for (int i = 0; i < metadata->request().headers_size(); ++i) { | 664 for (int i = 0; i < metadata->request().headers_size(); ++i) { |
| 677 const ServiceWorkerCacheHeaderMap header = metadata->request().headers(i); | 665 const CacheHeaderMap header = metadata->request().headers(i); |
| 678 DCHECK(header.name().find('\0') == std::string::npos); | 666 DCHECK(header.name().find('\0') == std::string::npos); |
| 679 DCHECK(header.value().find('\0') == std::string::npos); | 667 DCHECK(header.value().find('\0') == std::string::npos); |
| 680 cached_request_headers[header.name()] = header.value(); | 668 cached_request_headers[header.name()] = header.value(); |
| 681 } | 669 } |
| 682 | 670 |
| 683 if (!VaryMatches(match_context->request->headers, cached_request_headers, | 671 if (!VaryMatches(match_context->request->headers, cached_request_headers, |
| 684 response->headers)) { | 672 response->headers)) { |
| 685 match_context->original_callback.Run( | 673 match_context->original_callback.Run( |
| 686 ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, | 674 CacheStorageCache::ERROR_TYPE_NOT_FOUND, |
| 687 scoped_ptr<ServiceWorkerResponse>(), | 675 scoped_ptr<ServiceWorkerResponse>(), |
| 688 scoped_ptr<storage::BlobDataHandle>()); | 676 scoped_ptr<storage::BlobDataHandle>()); |
| 689 return; | 677 return; |
| 690 } | 678 } |
| 691 | 679 |
| 692 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { | 680 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { |
| 693 match_context->original_callback.Run(ServiceWorkerCache::ERROR_TYPE_OK, | 681 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_OK, |
| 694 match_context->response.Pass(), | 682 match_context->response.Pass(), |
| 695 scoped_ptr<storage::BlobDataHandle>()); | 683 scoped_ptr<storage::BlobDataHandle>()); |
| 696 return; | 684 return; |
| 697 } | 685 } |
| 698 | 686 |
| 699 // Stream the response body into a blob. | 687 // Stream the response body into a blob. |
| 700 if (!match_context->blob_storage_context) { | 688 if (!match_context->blob_storage_context) { |
| 701 match_context->original_callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 689 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 702 scoped_ptr<ServiceWorkerResponse>(), | 690 scoped_ptr<ServiceWorkerResponse>(), |
| 703 scoped_ptr<storage::BlobDataHandle>()); | 691 scoped_ptr<storage::BlobDataHandle>()); |
| 704 return; | 692 return; |
| 705 } | 693 } |
| 706 | 694 |
| 707 response->blob_uuid = base::GenerateGUID(); | 695 response->blob_uuid = base::GenerateGUID(); |
| 708 | 696 |
| 709 match_context->blob_data.reset( | 697 match_context->blob_data.reset( |
| 710 new storage::BlobDataBuilder(response->blob_uuid)); | 698 new storage::BlobDataBuilder(response->blob_uuid)); |
| 711 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize); | 699 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize); |
| 712 | 700 |
| 713 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | 701 disk_cache::Entry* tmp_entry_ptr = match_context->entry; |
| 714 net::IOBufferWithSize* response_body_buffer = | 702 net::IOBufferWithSize* response_body_buffer = |
| 715 match_context->response_body_buffer.get(); | 703 match_context->response_body_buffer.get(); |
| 716 | 704 |
| 717 net::CompletionCallback read_callback = base::Bind( | 705 net::CompletionCallback read_callback = base::Bind( |
| 718 &ServiceWorkerCache::MatchDidReadResponseBodyData, | 706 &CacheStorageCache::MatchDidReadResponseBodyData, |
| 719 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); | 707 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); |
| 720 | 708 |
| 721 int read_rv = | 709 int read_rv = |
| 722 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer, | 710 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer, |
| 723 response_body_buffer->size(), read_callback); | 711 response_body_buffer->size(), read_callback); |
| 724 | 712 |
| 725 if (read_rv != net::ERR_IO_PENDING) | 713 if (read_rv != net::ERR_IO_PENDING) |
| 726 read_callback.Run(read_rv); | 714 read_callback.Run(read_rv); |
| 727 } | 715 } |
| 728 | 716 |
| 729 void ServiceWorkerCache::MatchDidReadResponseBodyData( | 717 void CacheStorageCache::MatchDidReadResponseBodyData( |
| 730 scoped_ptr<MatchContext> match_context, | 718 scoped_ptr<MatchContext> match_context, |
| 731 int rv) { | 719 int rv) { |
| 732 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. | 720 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed. |
| 733 tracked_objects::ScopedTracker tracking_profile( | 721 tracked_objects::ScopedTracker tracking_profile( |
| 734 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 722 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 735 "422516 ServiceWorkerCache::MatchDidReadResponseBodyData")); | 723 "422516 CacheStorageCache::MatchDidReadResponseBodyData")); |
| 736 | 724 |
| 737 if (rv < 0) { | 725 if (rv < 0) { |
| 738 match_context->original_callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 726 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 739 scoped_ptr<ServiceWorkerResponse>(), | 727 scoped_ptr<ServiceWorkerResponse>(), |
| 740 scoped_ptr<storage::BlobDataHandle>()); | 728 scoped_ptr<storage::BlobDataHandle>()); |
| 741 return; | 729 return; |
| 742 } | 730 } |
| 743 | 731 |
| 744 if (rv == 0) { | 732 if (rv == 0) { |
| 745 match_context->response->blob_uuid = match_context->blob_data->uuid(); | 733 match_context->response->blob_uuid = match_context->blob_data->uuid(); |
| 746 match_context->response->blob_size = match_context->total_bytes_read; | 734 match_context->response->blob_size = match_context->total_bytes_read; |
| 747 MatchDoneWithBody(match_context.Pass()); | 735 MatchDoneWithBody(match_context.Pass()); |
| 748 return; | 736 return; |
| 749 } | 737 } |
| 750 | 738 |
| 751 // TODO(jkarlin): This copying of the the entire cache response into memory is | 739 // TODO(jkarlin): This copying of the the entire cache response into memory is |
| 752 // awful. Create a new interface around SimpleCache that provides access the | 740 // awful. Create a new interface around SimpleCache that provides access the |
| 753 // data directly from the file. See bug http://crbug.com/403493. | 741 // data directly from the file. See bug http://crbug.com/403493. |
| 754 match_context->blob_data->AppendData( | 742 match_context->blob_data->AppendData( |
| 755 match_context->response_body_buffer->data(), rv); | 743 match_context->response_body_buffer->data(), rv); |
| 756 match_context->total_bytes_read += rv; | 744 match_context->total_bytes_read += rv; |
| 757 int total_bytes_read = match_context->total_bytes_read; | 745 int total_bytes_read = match_context->total_bytes_read; |
| 758 | 746 |
| 759 // Grab some pointers before passing match_context in bind. | 747 // Grab some pointers before passing match_context in bind. |
| 760 net::IOBufferWithSize* buffer = match_context->response_body_buffer.get(); | 748 net::IOBufferWithSize* buffer = match_context->response_body_buffer.get(); |
| 761 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | 749 disk_cache::Entry* tmp_entry_ptr = match_context->entry; |
| 762 | 750 |
| 763 net::CompletionCallback read_callback = base::Bind( | 751 net::CompletionCallback read_callback = base::Bind( |
| 764 &ServiceWorkerCache::MatchDidReadResponseBodyData, | 752 &CacheStorageCache::MatchDidReadResponseBodyData, |
| 765 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); | 753 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); |
| 766 | 754 |
| 767 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read, | 755 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read, |
| 768 buffer, buffer->size(), read_callback); | 756 buffer, buffer->size(), read_callback); |
| 769 | 757 |
| 770 if (read_rv != net::ERR_IO_PENDING) | 758 if (read_rv != net::ERR_IO_PENDING) |
| 771 read_callback.Run(read_rv); | 759 read_callback.Run(read_rv); |
| 772 } | 760 } |
| 773 | 761 |
| 774 void ServiceWorkerCache::MatchDoneWithBody( | 762 void CacheStorageCache::MatchDoneWithBody( |
| 775 scoped_ptr<MatchContext> match_context) { | 763 scoped_ptr<MatchContext> match_context) { |
| 776 if (!match_context->blob_storage_context) { | 764 if (!match_context->blob_storage_context) { |
| 777 match_context->original_callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 765 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 778 scoped_ptr<ServiceWorkerResponse>(), | 766 scoped_ptr<ServiceWorkerResponse>(), |
| 779 scoped_ptr<storage::BlobDataHandle>()); | 767 scoped_ptr<storage::BlobDataHandle>()); |
| 780 return; | 768 return; |
| 781 } | 769 } |
| 782 | 770 |
| 783 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | 771 scoped_ptr<storage::BlobDataHandle> blob_data_handle( |
| 784 match_context->blob_storage_context->AddFinishedBlob( | 772 match_context->blob_storage_context->AddFinishedBlob( |
| 785 match_context->blob_data.get())); | 773 match_context->blob_data.get())); |
| 786 | 774 |
| 787 match_context->original_callback.Run(ServiceWorkerCache::ERROR_TYPE_OK, | 775 match_context->original_callback.Run(CacheStorageCache::ERROR_TYPE_OK, |
| 788 match_context->response.Pass(), | 776 match_context->response.Pass(), |
| 789 blob_data_handle.Pass()); | 777 blob_data_handle.Pass()); |
| 790 } | 778 } |
| 791 | 779 |
| 792 void ServiceWorkerCache::PutImpl(scoped_ptr<PutContext> put_context) { | 780 void CacheStorageCache::PutImpl(scoped_ptr<PutContext> put_context) { |
| 793 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); | 781 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); |
| 794 if (backend_state_ != BACKEND_OPEN) { | 782 if (backend_state_ != BACKEND_OPEN) { |
| 795 put_context->callback.Run(ERROR_TYPE_STORAGE, | 783 put_context->callback.Run(ERROR_TYPE_STORAGE, |
| 796 scoped_ptr<ServiceWorkerResponse>(), | 784 scoped_ptr<ServiceWorkerResponse>(), |
| 797 scoped_ptr<storage::BlobDataHandle>()); | 785 scoped_ptr<storage::BlobDataHandle>()); |
| 798 return; | 786 return; |
| 799 } | 787 } |
| 800 | 788 |
| 801 scoped_ptr<ServiceWorkerFetchRequest> request_copy( | 789 scoped_ptr<ServiceWorkerFetchRequest> request_copy( |
| 802 new ServiceWorkerFetchRequest(*put_context->request)); | 790 new ServiceWorkerFetchRequest(*put_context->request)); |
| 803 | 791 |
| 804 DeleteImpl(request_copy.Pass(), base::Bind(&ServiceWorkerCache::PutDidDelete, | 792 DeleteImpl(request_copy.Pass(), base::Bind(&CacheStorageCache::PutDidDelete, |
| 805 weak_ptr_factory_.GetWeakPtr(), | 793 weak_ptr_factory_.GetWeakPtr(), |
| 806 base::Passed(put_context.Pass()))); | 794 base::Passed(put_context.Pass()))); |
| 807 } | 795 } |
| 808 | 796 |
| 809 void ServiceWorkerCache::PutDidDelete(scoped_ptr<PutContext> put_context, | 797 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, |
| 810 ErrorType delete_error) { | 798 ErrorType delete_error) { |
| 811 if (backend_state_ != BACKEND_OPEN) { | 799 if (backend_state_ != BACKEND_OPEN) { |
| 812 put_context->callback.Run(ERROR_TYPE_STORAGE, | 800 put_context->callback.Run(ERROR_TYPE_STORAGE, |
| 813 scoped_ptr<ServiceWorkerResponse>(), | 801 scoped_ptr<ServiceWorkerResponse>(), |
| 814 scoped_ptr<storage::BlobDataHandle>()); | 802 scoped_ptr<storage::BlobDataHandle>()); |
| 815 return; | 803 return; |
| 816 } | 804 } |
| 817 | 805 |
| 818 disk_cache::Entry** entry_ptr = &put_context->cache_entry; | 806 disk_cache::Entry** entry_ptr = &put_context->cache_entry; |
| 819 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); | 807 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); |
| 820 disk_cache::Backend* backend_ptr = backend_.get(); | 808 disk_cache::Backend* backend_ptr = backend_.get(); |
| 821 | 809 |
| 822 net::CompletionCallback create_entry_callback = base::Bind( | 810 net::CompletionCallback create_entry_callback = base::Bind( |
| 823 &ServiceWorkerCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), | 811 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), |
| 824 base::Passed(put_context.Pass())); | 812 base::Passed(put_context.Pass())); |
| 825 | 813 |
| 826 int create_rv = backend_ptr->CreateEntry( | 814 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, |
| 827 request_ptr->url.spec(), entry_ptr, create_entry_callback); | 815 create_entry_callback); |
| 828 | 816 |
| 829 if (create_rv != net::ERR_IO_PENDING) | 817 if (create_rv != net::ERR_IO_PENDING) |
| 830 create_entry_callback.Run(create_rv); | 818 create_entry_callback.Run(create_rv); |
| 831 } | 819 } |
| 832 | 820 |
| 833 void ServiceWorkerCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context, | 821 void CacheStorageCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context, |
| 834 int rv) { | 822 int rv) { |
| 835 if (rv != net::OK) { | 823 if (rv != net::OK) { |
| 836 put_context->callback.Run(ServiceWorkerCache::ERROR_TYPE_EXISTS, | 824 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_EXISTS, |
| 837 scoped_ptr<ServiceWorkerResponse>(), | 825 scoped_ptr<ServiceWorkerResponse>(), |
| 838 scoped_ptr<storage::BlobDataHandle>()); | 826 scoped_ptr<storage::BlobDataHandle>()); |
| 839 return; | 827 return; |
| 840 } | 828 } |
| 841 | 829 |
| 842 DCHECK(put_context->cache_entry); | 830 DCHECK(put_context->cache_entry); |
| 843 | 831 |
| 844 ServiceWorkerCacheMetadata metadata; | 832 CacheMetadata metadata; |
| 845 ServiceWorkerCacheRequest* request_metadata = metadata.mutable_request(); | 833 CacheRequest* request_metadata = metadata.mutable_request(); |
| 846 request_metadata->set_method(put_context->request->method); | 834 request_metadata->set_method(put_context->request->method); |
| 847 for (ServiceWorkerHeaderMap::const_iterator it = | 835 for (ServiceWorkerHeaderMap::const_iterator it = |
| 848 put_context->request->headers.begin(); | 836 put_context->request->headers.begin(); |
| 849 it != put_context->request->headers.end(); | 837 it != put_context->request->headers.end(); ++it) { |
| 850 ++it) { | |
| 851 DCHECK(it->first.find('\0') == std::string::npos); | 838 DCHECK(it->first.find('\0') == std::string::npos); |
| 852 DCHECK(it->second.find('\0') == std::string::npos); | 839 DCHECK(it->second.find('\0') == std::string::npos); |
| 853 ServiceWorkerCacheHeaderMap* header_map = request_metadata->add_headers(); | 840 CacheHeaderMap* header_map = request_metadata->add_headers(); |
| 854 header_map->set_name(it->first); | 841 header_map->set_name(it->first); |
| 855 header_map->set_value(it->second); | 842 header_map->set_value(it->second); |
| 856 } | 843 } |
| 857 | 844 |
| 858 ServiceWorkerCacheResponse* response_metadata = metadata.mutable_response(); | 845 CacheResponse* response_metadata = metadata.mutable_response(); |
| 859 response_metadata->set_status_code(put_context->response->status_code); | 846 response_metadata->set_status_code(put_context->response->status_code); |
| 860 response_metadata->set_status_text(put_context->response->status_text); | 847 response_metadata->set_status_text(put_context->response->status_text); |
| 861 response_metadata->set_response_type( | 848 response_metadata->set_response_type( |
| 862 WebResponseTypeToProtoResponseType(put_context->response->response_type)); | 849 WebResponseTypeToProtoResponseType(put_context->response->response_type)); |
| 863 response_metadata->set_url(put_context->response->url.spec()); | 850 response_metadata->set_url(put_context->response->url.spec()); |
| 864 for (ServiceWorkerHeaderMap::const_iterator it = | 851 for (ServiceWorkerHeaderMap::const_iterator it = |
| 865 put_context->response->headers.begin(); | 852 put_context->response->headers.begin(); |
| 866 it != put_context->response->headers.end(); | 853 it != put_context->response->headers.end(); ++it) { |
| 867 ++it) { | |
| 868 DCHECK(it->first.find('\0') == std::string::npos); | 854 DCHECK(it->first.find('\0') == std::string::npos); |
| 869 DCHECK(it->second.find('\0') == std::string::npos); | 855 DCHECK(it->second.find('\0') == std::string::npos); |
| 870 ServiceWorkerCacheHeaderMap* header_map = response_metadata->add_headers(); | 856 CacheHeaderMap* header_map = response_metadata->add_headers(); |
| 871 header_map->set_name(it->first); | 857 header_map->set_name(it->first); |
| 872 header_map->set_value(it->second); | 858 header_map->set_value(it->second); |
| 873 } | 859 } |
| 874 | 860 |
| 875 scoped_ptr<std::string> serialized(new std::string()); | 861 scoped_ptr<std::string> serialized(new std::string()); |
| 876 if (!metadata.SerializeToString(serialized.get())) { | 862 if (!metadata.SerializeToString(serialized.get())) { |
| 877 put_context->callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 863 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 878 scoped_ptr<ServiceWorkerResponse>(), | 864 scoped_ptr<ServiceWorkerResponse>(), |
| 879 scoped_ptr<storage::BlobDataHandle>()); | 865 scoped_ptr<storage::BlobDataHandle>()); |
| 880 return; | 866 return; |
| 881 } | 867 } |
| 882 | 868 |
| 883 scoped_refptr<net::StringIOBuffer> buffer( | 869 scoped_refptr<net::StringIOBuffer> buffer( |
| 884 new net::StringIOBuffer(serialized.Pass())); | 870 new net::StringIOBuffer(serialized.Pass())); |
| 885 | 871 |
| 886 // Get a temporary copy of the entry pointer before passing it in base::Bind. | 872 // Get a temporary copy of the entry pointer before passing it in base::Bind. |
| 887 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry; | 873 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry; |
| 888 | 874 |
| 889 net::CompletionCallback write_headers_callback = base::Bind( | 875 net::CompletionCallback write_headers_callback = base::Bind( |
| 890 &ServiceWorkerCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), | 876 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), |
| 891 base::Passed(put_context.Pass()), buffer->size()); | 877 base::Passed(put_context.Pass()), buffer->size()); |
| 892 | 878 |
| 893 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, | 879 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), |
| 894 0 /* offset */, | 880 buffer->size(), write_headers_callback, |
| 895 buffer.get(), | |
| 896 buffer->size(), | |
| 897 write_headers_callback, | |
| 898 true /* truncate */); | 881 true /* truncate */); |
| 899 | 882 |
| 900 if (rv != net::ERR_IO_PENDING) | 883 if (rv != net::ERR_IO_PENDING) |
| 901 write_headers_callback.Run(rv); | 884 write_headers_callback.Run(rv); |
| 902 } | 885 } |
| 903 | 886 |
| 904 void ServiceWorkerCache::PutDidWriteHeaders(scoped_ptr<PutContext> put_context, | 887 void CacheStorageCache::PutDidWriteHeaders(scoped_ptr<PutContext> put_context, |
| 905 int expected_bytes, | 888 int expected_bytes, |
| 906 int rv) { | 889 int rv) { |
| 907 if (rv != expected_bytes) { | 890 if (rv != expected_bytes) { |
| 908 put_context->cache_entry->Doom(); | 891 put_context->cache_entry->Doom(); |
| 909 put_context->callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 892 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 910 scoped_ptr<ServiceWorkerResponse>(), | 893 scoped_ptr<ServiceWorkerResponse>(), |
| 911 scoped_ptr<storage::BlobDataHandle>()); | 894 scoped_ptr<storage::BlobDataHandle>()); |
| 912 return; | 895 return; |
| 913 } | 896 } |
| 914 | 897 |
| 915 // The metadata is written, now for the response content. The data is streamed | 898 // The metadata is written, now for the response content. The data is streamed |
| 916 // from the blob into the cache entry. | 899 // from the blob into the cache entry. |
| 917 | 900 |
| 918 if (put_context->response->blob_uuid.empty()) { | 901 if (put_context->response->blob_uuid.empty()) { |
| 919 if (put_context->quota_manager_proxy.get()) { | 902 if (put_context->quota_manager_proxy.get()) { |
| 920 put_context->quota_manager_proxy->NotifyStorageModified( | 903 put_context->quota_manager_proxy->NotifyStorageModified( |
| 921 storage::QuotaClient::kServiceWorkerCache, | 904 storage::QuotaClient::kServiceWorkerCache, put_context->origin, |
| 922 put_context->origin, | |
| 923 storage::kStorageTypeTemporary, | 905 storage::kStorageTypeTemporary, |
| 924 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); | 906 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); |
| 925 } | 907 } |
| 926 | 908 |
| 927 put_context->callback.Run(ServiceWorkerCache::ERROR_TYPE_OK, | 909 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_OK, |
| 928 put_context->response.Pass(), | 910 put_context->response.Pass(), |
| 929 scoped_ptr<storage::BlobDataHandle>()); | 911 scoped_ptr<storage::BlobDataHandle>()); |
| 930 return; | 912 return; |
| 931 } | 913 } |
| 932 | 914 |
| 933 DCHECK(put_context->blob_data_handle); | 915 DCHECK(put_context->blob_data_handle); |
| 934 | 916 |
| 935 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); | 917 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); |
| 936 put_context->cache_entry = NULL; | 918 put_context->cache_entry = NULL; |
| 937 scoped_ptr<BlobReader> reader(new BlobReader()); | 919 scoped_ptr<BlobReader> reader(new BlobReader()); |
| 938 BlobReader* reader_ptr = reader.get(); | 920 BlobReader* reader_ptr = reader.get(); |
| 939 | 921 |
| 940 // Grab some pointers before passing put_context in Bind. | 922 // Grab some pointers before passing put_context in Bind. |
| 941 net::URLRequestContext* request_context = put_context->request_context; | 923 net::URLRequestContext* request_context = put_context->request_context; |
| 942 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 924 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
| 943 put_context->blob_data_handle.Pass(); | 925 put_context->blob_data_handle.Pass(); |
| 944 | 926 |
| 945 reader_ptr->StreamBlobToCache( | 927 reader_ptr->StreamBlobToCache( |
| 946 entry.Pass(), request_context, blob_data_handle.Pass(), | 928 entry.Pass(), request_context, blob_data_handle.Pass(), |
| 947 base::Bind(&ServiceWorkerCache::PutDidWriteBlobToCache, | 929 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, |
| 948 weak_ptr_factory_.GetWeakPtr(), | 930 weak_ptr_factory_.GetWeakPtr(), |
| 949 base::Passed(put_context.Pass()), | 931 base::Passed(put_context.Pass()), |
| 950 base::Passed(reader.Pass()))); | 932 base::Passed(reader.Pass()))); |
| 951 } | 933 } |
| 952 | 934 |
| 953 void ServiceWorkerCache::PutDidWriteBlobToCache( | 935 void CacheStorageCache::PutDidWriteBlobToCache( |
| 954 scoped_ptr<PutContext> put_context, | 936 scoped_ptr<PutContext> put_context, |
| 955 scoped_ptr<BlobReader> blob_reader, | 937 scoped_ptr<BlobReader> blob_reader, |
| 956 disk_cache::ScopedEntryPtr entry, | 938 disk_cache::ScopedEntryPtr entry, |
| 957 bool success) { | 939 bool success) { |
| 958 DCHECK(entry); | 940 DCHECK(entry); |
| 959 put_context->cache_entry = entry.release(); | 941 put_context->cache_entry = entry.release(); |
| 960 | 942 |
| 961 if (!success) { | 943 if (!success) { |
| 962 put_context->cache_entry->Doom(); | 944 put_context->cache_entry->Doom(); |
| 963 put_context->callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE, | 945 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE, |
| 964 scoped_ptr<ServiceWorkerResponse>(), | 946 scoped_ptr<ServiceWorkerResponse>(), |
| 965 scoped_ptr<storage::BlobDataHandle>()); | 947 scoped_ptr<storage::BlobDataHandle>()); |
| 966 return; | 948 return; |
| 967 } | 949 } |
| 968 | 950 |
| 969 if (put_context->quota_manager_proxy.get()) { | 951 if (put_context->quota_manager_proxy.get()) { |
| 970 put_context->quota_manager_proxy->NotifyStorageModified( | 952 put_context->quota_manager_proxy->NotifyStorageModified( |
| 971 storage::QuotaClient::kServiceWorkerCache, | 953 storage::QuotaClient::kServiceWorkerCache, put_context->origin, |
| 972 put_context->origin, | |
| 973 storage::kStorageTypeTemporary, | 954 storage::kStorageTypeTemporary, |
| 974 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + | 955 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + |
| 975 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); | 956 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); |
| 976 } | 957 } |
| 977 | 958 |
| 978 put_context->callback.Run(ServiceWorkerCache::ERROR_TYPE_OK, | 959 put_context->callback.Run(CacheStorageCache::ERROR_TYPE_OK, |
| 979 put_context->response.Pass(), | 960 put_context->response.Pass(), |
| 980 put_context->out_blob_data_handle.Pass()); | 961 put_context->out_blob_data_handle.Pass()); |
| 981 } | 962 } |
| 982 | 963 |
| 983 void ServiceWorkerCache::DeleteImpl( | 964 void CacheStorageCache::DeleteImpl( |
| 984 scoped_ptr<ServiceWorkerFetchRequest> request, | 965 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 985 const ErrorCallback& callback) { | 966 const ErrorCallback& callback) { |
| 986 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); | 967 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); |
| 987 if (backend_state_ != BACKEND_OPEN) { | 968 if (backend_state_ != BACKEND_OPEN) { |
| 988 callback.Run(ERROR_TYPE_STORAGE); | 969 callback.Run(ERROR_TYPE_STORAGE); |
| 989 return; | 970 return; |
| 990 } | 971 } |
| 991 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); | 972 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); |
| 992 | 973 |
| 993 disk_cache::Entry** entry_ptr = entry.get(); | 974 disk_cache::Entry** entry_ptr = entry.get(); |
| 994 | 975 |
| 995 ServiceWorkerFetchRequest* request_ptr = request.get(); | 976 ServiceWorkerFetchRequest* request_ptr = request.get(); |
| 996 | 977 |
| 997 net::CompletionCallback open_entry_callback = base::Bind( | 978 net::CompletionCallback open_entry_callback = base::Bind( |
| 998 &ServiceWorkerCache::DeleteDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), | 979 &CacheStorageCache::DeleteDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), |
| 999 origin_, base::Passed(request.Pass()), callback, | 980 origin_, base::Passed(request.Pass()), callback, |
| 1000 base::Passed(entry.Pass()), quota_manager_proxy_); | 981 base::Passed(entry.Pass()), quota_manager_proxy_); |
| 1001 | 982 |
| 1002 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, | 983 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, |
| 1003 open_entry_callback); | 984 open_entry_callback); |
| 1004 if (rv != net::ERR_IO_PENDING) | 985 if (rv != net::ERR_IO_PENDING) |
| 1005 open_entry_callback.Run(rv); | 986 open_entry_callback.Run(rv); |
| 1006 } | 987 } |
| 1007 | 988 |
| 1008 void ServiceWorkerCache::DeleteDidOpenEntry( | 989 void CacheStorageCache::DeleteDidOpenEntry( |
| 1009 const GURL& origin, | 990 const GURL& origin, |
| 1010 scoped_ptr<ServiceWorkerFetchRequest> request, | 991 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 1011 const ServiceWorkerCache::ErrorCallback& callback, | 992 const CacheStorageCache::ErrorCallback& callback, |
| 1012 scoped_ptr<disk_cache::Entry*> entry_ptr, | 993 scoped_ptr<disk_cache::Entry*> entry_ptr, |
| 1013 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 994 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 1014 int rv) { | 995 int rv) { |
| 1015 if (rv != net::OK) { | 996 if (rv != net::OK) { |
| 1016 callback.Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND); | 997 callback.Run(CacheStorageCache::ERROR_TYPE_NOT_FOUND); |
| 1017 return; | 998 return; |
| 1018 } | 999 } |
| 1019 | 1000 |
| 1020 DCHECK(entry_ptr); | 1001 DCHECK(entry_ptr); |
| 1021 disk_cache::ScopedEntryPtr entry(*entry_ptr); | 1002 disk_cache::ScopedEntryPtr entry(*entry_ptr); |
| 1022 | 1003 |
| 1023 if (quota_manager_proxy.get()) { | 1004 if (quota_manager_proxy.get()) { |
| 1024 quota_manager_proxy->NotifyStorageModified( | 1005 quota_manager_proxy->NotifyStorageModified( |
| 1025 storage::QuotaClient::kServiceWorkerCache, origin, | 1006 storage::QuotaClient::kServiceWorkerCache, origin, |
| 1026 storage::kStorageTypeTemporary, | 1007 storage::kStorageTypeTemporary, |
| 1027 -1 * (entry->GetDataSize(INDEX_HEADERS) + | 1008 -1 * (entry->GetDataSize(INDEX_HEADERS) + |
| 1028 entry->GetDataSize(INDEX_RESPONSE_BODY))); | 1009 entry->GetDataSize(INDEX_RESPONSE_BODY))); |
| 1029 } | 1010 } |
| 1030 | 1011 |
| 1031 entry->Doom(); | 1012 entry->Doom(); |
| 1032 callback.Run(ServiceWorkerCache::ERROR_TYPE_OK); | 1013 callback.Run(CacheStorageCache::ERROR_TYPE_OK); |
| 1033 } | 1014 } |
| 1034 | 1015 |
| 1035 void ServiceWorkerCache::KeysImpl(const RequestsCallback& callback) { | 1016 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) { |
| 1036 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); | 1017 DCHECK(backend_state_ != BACKEND_UNINITIALIZED); |
| 1037 if (backend_state_ != BACKEND_OPEN) { | 1018 if (backend_state_ != BACKEND_OPEN) { |
| 1038 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<Requests>()); | 1019 callback.Run(ERROR_TYPE_STORAGE, scoped_ptr<Requests>()); |
| 1039 return; | 1020 return; |
| 1040 } | 1021 } |
| 1041 | 1022 |
| 1042 // 1. Iterate through all of the entries, open them, and add them to a vector. | 1023 // 1. Iterate through all of the entries, open them, and add them to a vector. |
| 1043 // 2. For each open entry: | 1024 // 2. For each open entry: |
| 1044 // 2.1. Read the headers into a protobuf. | 1025 // 2.1. Read the headers into a protobuf. |
| 1045 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | 1026 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). |
| 1046 // 2.3. Push the response into a vector of requests to be returned. | 1027 // 2.3. Push the response into a vector of requests to be returned. |
| 1047 // 3. Return the vector of requests (keys). | 1028 // 3. Return the vector of requests (keys). |
| 1048 | 1029 |
| 1049 // The entries have to be loaded into a vector first because enumeration loops | 1030 // The entries have to be loaded into a vector first because enumeration loops |
| 1050 // forever if you read data from a cache entry while enumerating. | 1031 // forever if you read data from a cache entry while enumerating. |
| 1051 | 1032 |
| 1052 scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); | 1033 scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); |
| 1053 | 1034 |
| 1054 keys_context->backend_iterator = backend_->CreateIterator(); | 1035 keys_context->backend_iterator = backend_->CreateIterator(); |
| 1055 disk_cache::Backend::Iterator& iterator = *keys_context->backend_iterator; | 1036 disk_cache::Backend::Iterator& iterator = *keys_context->backend_iterator; |
| 1056 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | 1037 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; |
| 1057 | 1038 |
| 1058 net::CompletionCallback open_entry_callback = base::Bind( | 1039 net::CompletionCallback open_entry_callback = base::Bind( |
| 1059 &ServiceWorkerCache::KeysDidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), | 1040 &CacheStorageCache::KeysDidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), |
| 1060 base::Passed(keys_context.Pass())); | 1041 base::Passed(keys_context.Pass())); |
| 1061 | 1042 |
| 1062 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | 1043 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); |
| 1063 | 1044 |
| 1064 if (rv != net::ERR_IO_PENDING) | 1045 if (rv != net::ERR_IO_PENDING) |
| 1065 open_entry_callback.Run(rv); | 1046 open_entry_callback.Run(rv); |
| 1066 } | 1047 } |
| 1067 | 1048 |
| 1068 void ServiceWorkerCache::KeysDidOpenNextEntry( | 1049 void CacheStorageCache::KeysDidOpenNextEntry( |
| 1069 scoped_ptr<KeysContext> keys_context, | 1050 scoped_ptr<KeysContext> keys_context, |
| 1070 int rv) { | 1051 int rv) { |
| 1071 if (rv == net::ERR_FAILED) { | 1052 if (rv == net::ERR_FAILED) { |
| 1072 DCHECK(!keys_context->enumerated_entry); | 1053 DCHECK(!keys_context->enumerated_entry); |
| 1073 // Enumeration is complete, extract the requests from the entries. | 1054 // Enumeration is complete, extract the requests from the entries. |
| 1074 Entries::iterator iter = keys_context->entries.begin(); | 1055 Entries::iterator iter = keys_context->entries.begin(); |
| 1075 KeysProcessNextEntry(keys_context.Pass(), iter); | 1056 KeysProcessNextEntry(keys_context.Pass(), iter); |
| 1076 return; | 1057 return; |
| 1077 } | 1058 } |
| 1078 | 1059 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1089 } | 1070 } |
| 1090 | 1071 |
| 1091 // Store the entry. | 1072 // Store the entry. |
| 1092 keys_context->entries.push_back(keys_context->enumerated_entry); | 1073 keys_context->entries.push_back(keys_context->enumerated_entry); |
| 1093 keys_context->enumerated_entry = NULL; | 1074 keys_context->enumerated_entry = NULL; |
| 1094 | 1075 |
| 1095 // Enumerate the next entry. | 1076 // Enumerate the next entry. |
| 1096 disk_cache::Backend::Iterator& iterator = *keys_context->backend_iterator; | 1077 disk_cache::Backend::Iterator& iterator = *keys_context->backend_iterator; |
| 1097 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | 1078 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; |
| 1098 net::CompletionCallback open_entry_callback = base::Bind( | 1079 net::CompletionCallback open_entry_callback = base::Bind( |
| 1099 &ServiceWorkerCache::KeysDidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), | 1080 &CacheStorageCache::KeysDidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), |
| 1100 base::Passed(keys_context.Pass())); | 1081 base::Passed(keys_context.Pass())); |
| 1101 | 1082 |
| 1102 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | 1083 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); |
| 1103 | 1084 |
| 1104 if (rv != net::ERR_IO_PENDING) | 1085 if (rv != net::ERR_IO_PENDING) |
| 1105 open_entry_callback.Run(rv); | 1086 open_entry_callback.Run(rv); |
| 1106 } | 1087 } |
| 1107 | 1088 |
| 1108 void ServiceWorkerCache::KeysProcessNextEntry( | 1089 void CacheStorageCache::KeysProcessNextEntry( |
| 1109 scoped_ptr<KeysContext> keys_context, | 1090 scoped_ptr<KeysContext> keys_context, |
| 1110 const Entries::iterator& iter) { | 1091 const Entries::iterator& iter) { |
| 1111 if (iter == keys_context->entries.end()) { | 1092 if (iter == keys_context->entries.end()) { |
| 1112 // All done. Return all of the keys. | 1093 // All done. Return all of the keys. |
| 1113 keys_context->original_callback.Run(ERROR_TYPE_OK, | 1094 keys_context->original_callback.Run(ERROR_TYPE_OK, |
| 1114 keys_context->out_keys.Pass()); | 1095 keys_context->out_keys.Pass()); |
| 1115 return; | 1096 return; |
| 1116 } | 1097 } |
| 1117 | 1098 |
| 1118 ReadMetadata(*iter, base::Bind(&ServiceWorkerCache::KeysDidReadMetadata, | 1099 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, |
| 1119 weak_ptr_factory_.GetWeakPtr(), | 1100 weak_ptr_factory_.GetWeakPtr(), |
| 1120 base::Passed(keys_context.Pass()), iter)); | 1101 base::Passed(keys_context.Pass()), iter)); |
| 1121 } | 1102 } |
| 1122 | 1103 |
| 1123 void ServiceWorkerCache::KeysDidReadMetadata( | 1104 void CacheStorageCache::KeysDidReadMetadata( |
| 1124 scoped_ptr<KeysContext> keys_context, | 1105 scoped_ptr<KeysContext> keys_context, |
| 1125 const Entries::iterator& iter, | 1106 const Entries::iterator& iter, |
| 1126 scoped_ptr<ServiceWorkerCacheMetadata> metadata) { | 1107 scoped_ptr<CacheMetadata> metadata) { |
| 1127 disk_cache::Entry* entry = *iter; | 1108 disk_cache::Entry* entry = *iter; |
| 1128 | 1109 |
| 1129 if (metadata) { | 1110 if (metadata) { |
| 1130 keys_context->out_keys->push_back( | 1111 keys_context->out_keys->push_back(ServiceWorkerFetchRequest( |
| 1131 ServiceWorkerFetchRequest(GURL(entry->GetKey()), | 1112 GURL(entry->GetKey()), metadata->request().method(), |
| 1132 metadata->request().method(), | 1113 ServiceWorkerHeaderMap(), Referrer(), false)); |
| 1133 ServiceWorkerHeaderMap(), | |
| 1134 Referrer(), | |
| 1135 false)); | |
| 1136 | 1114 |
| 1137 ServiceWorkerHeaderMap& req_headers = | 1115 ServiceWorkerHeaderMap& req_headers = |
| 1138 keys_context->out_keys->back().headers; | 1116 keys_context->out_keys->back().headers; |
| 1139 | 1117 |
| 1140 for (int i = 0; i < metadata->request().headers_size(); ++i) { | 1118 for (int i = 0; i < metadata->request().headers_size(); ++i) { |
| 1141 const ServiceWorkerCacheHeaderMap header = metadata->request().headers(i); | 1119 const CacheHeaderMap header = metadata->request().headers(i); |
| 1142 DCHECK(header.name().find('\0') == std::string::npos); | 1120 DCHECK(header.name().find('\0') == std::string::npos); |
| 1143 DCHECK(header.value().find('\0') == std::string::npos); | 1121 DCHECK(header.value().find('\0') == std::string::npos); |
| 1144 req_headers.insert(std::make_pair(header.name(), header.value())); | 1122 req_headers.insert(std::make_pair(header.name(), header.value())); |
| 1145 } | 1123 } |
| 1146 } else { | 1124 } else { |
| 1147 entry->Doom(); | 1125 entry->Doom(); |
| 1148 } | 1126 } |
| 1149 | 1127 |
| 1150 KeysProcessNextEntry(keys_context.Pass(), iter + 1); | 1128 KeysProcessNextEntry(keys_context.Pass(), iter + 1); |
| 1151 } | 1129 } |
| 1152 | 1130 |
| 1153 void ServiceWorkerCache::CloseImpl(const base::Closure& callback) { | 1131 void CacheStorageCache::CloseImpl(const base::Closure& callback) { |
| 1154 DCHECK(backend_state_ != BACKEND_CLOSED); | 1132 DCHECK(backend_state_ != BACKEND_CLOSED); |
| 1155 | 1133 |
| 1156 backend_state_ = BACKEND_CLOSED; | 1134 backend_state_ = BACKEND_CLOSED; |
| 1157 backend_.reset(); | 1135 backend_.reset(); |
| 1158 callback.Run(); | 1136 callback.Run(); |
| 1159 } | 1137 } |
| 1160 | 1138 |
| 1161 void ServiceWorkerCache::CreateBackend(const ErrorCallback& callback) { | 1139 void CacheStorageCache::CreateBackend(const ErrorCallback& callback) { |
| 1162 DCHECK(!backend_); | 1140 DCHECK(!backend_); |
| 1163 | 1141 |
| 1164 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. | 1142 // Use APP_CACHE as opposed to DISK_CACHE to prevent cache eviction. |
| 1165 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE; | 1143 net::CacheType cache_type = memory_only_ ? net::MEMORY_CACHE : net::APP_CACHE; |
| 1166 | 1144 |
| 1167 scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); | 1145 scoped_ptr<ScopedBackendPtr> backend_ptr(new ScopedBackendPtr()); |
| 1168 | 1146 |
| 1169 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. | 1147 // Temporary pointer so that backend_ptr can be Pass()'d in Bind below. |
| 1170 ScopedBackendPtr* backend = backend_ptr.get(); | 1148 ScopedBackendPtr* backend = backend_ptr.get(); |
| 1171 | 1149 |
| 1172 net::CompletionCallback create_cache_callback = | 1150 net::CompletionCallback create_cache_callback = |
| 1173 base::Bind(&ServiceWorkerCache::CreateBackendDidCreate, | 1151 base::Bind(&CacheStorageCache::CreateBackendDidCreate, |
| 1174 weak_ptr_factory_.GetWeakPtr(), callback, | 1152 weak_ptr_factory_.GetWeakPtr(), callback, |
| 1175 base::Passed(backend_ptr.Pass())); | 1153 base::Passed(backend_ptr.Pass())); |
| 1176 | 1154 |
| 1177 // TODO(jkarlin): Use the cache MessageLoopProxy that ServiceWorkerCacheCore | 1155 // TODO(jkarlin): Use the cache MessageLoopProxy that ServiceWorkerCacheCore |
| 1178 // has for disk caches. | 1156 // has for disk caches. |
| 1179 int rv = disk_cache::CreateCacheBackend( | 1157 int rv = disk_cache::CreateCacheBackend( |
| 1180 cache_type, | 1158 cache_type, net::CACHE_BACKEND_SIMPLE, path_, kMaxCacheBytes, |
| 1181 net::CACHE_BACKEND_SIMPLE, | |
| 1182 path_, | |
| 1183 kMaxCacheBytes, | |
| 1184 false, /* force */ | 1159 false, /* force */ |
| 1185 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(), | 1160 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::CACHE).get(), |
| 1186 NULL, | 1161 NULL, backend, create_cache_callback); |
| 1187 backend, | |
| 1188 create_cache_callback); | |
| 1189 if (rv != net::ERR_IO_PENDING) | 1162 if (rv != net::ERR_IO_PENDING) |
| 1190 create_cache_callback.Run(rv); | 1163 create_cache_callback.Run(rv); |
| 1191 } | 1164 } |
| 1192 | 1165 |
| 1193 void ServiceWorkerCache::CreateBackendDidCreate( | 1166 void CacheStorageCache::CreateBackendDidCreate( |
| 1194 const ServiceWorkerCache::ErrorCallback& callback, | 1167 const CacheStorageCache::ErrorCallback& callback, |
| 1195 scoped_ptr<ScopedBackendPtr> backend_ptr, | 1168 scoped_ptr<ScopedBackendPtr> backend_ptr, |
| 1196 int rv) { | 1169 int rv) { |
| 1197 if (rv != net::OK) { | 1170 if (rv != net::OK) { |
| 1198 callback.Run(ServiceWorkerCache::ERROR_TYPE_STORAGE); | 1171 callback.Run(CacheStorageCache::ERROR_TYPE_STORAGE); |
| 1199 return; | 1172 return; |
| 1200 } | 1173 } |
| 1201 | 1174 |
| 1202 backend_ = backend_ptr->Pass(); | 1175 backend_ = backend_ptr->Pass(); |
| 1203 callback.Run(ServiceWorkerCache::ERROR_TYPE_OK); | 1176 callback.Run(CacheStorageCache::ERROR_TYPE_OK); |
| 1204 } | 1177 } |
| 1205 | 1178 |
| 1206 void ServiceWorkerCache::InitBackend() { | 1179 void CacheStorageCache::InitBackend() { |
| 1207 DCHECK(backend_state_ == BACKEND_UNINITIALIZED); | 1180 DCHECK(backend_state_ == BACKEND_UNINITIALIZED); |
| 1208 | 1181 |
| 1209 if (initializing_) | 1182 if (initializing_) |
| 1210 return; | 1183 return; |
| 1211 | 1184 |
| 1212 DCHECK(!scheduler_->ScheduledOperations()); | 1185 DCHECK(!scheduler_->ScheduledOperations()); |
| 1213 initializing_ = true; | 1186 initializing_ = true; |
| 1214 | 1187 |
| 1215 scheduler_->ScheduleOperation(base::Bind( | 1188 scheduler_->ScheduleOperation(base::Bind( |
| 1216 &ServiceWorkerCache::CreateBackend, weak_ptr_factory_.GetWeakPtr(), | 1189 &CacheStorageCache::CreateBackend, weak_ptr_factory_.GetWeakPtr(), |
| 1217 base::Bind(&ServiceWorkerCache::InitDone, | 1190 base::Bind(&CacheStorageCache::InitDone, |
| 1218 weak_ptr_factory_.GetWeakPtr()))); | 1191 weak_ptr_factory_.GetWeakPtr()))); |
| 1219 } | 1192 } |
| 1220 | 1193 |
| 1221 void ServiceWorkerCache::InitDone(ErrorType error) { | 1194 void CacheStorageCache::InitDone(ErrorType error) { |
| 1222 initializing_ = false; | 1195 initializing_ = false; |
| 1223 backend_state_ = (error == ERROR_TYPE_OK && backend_ && | 1196 backend_state_ = (error == ERROR_TYPE_OK && backend_ && |
| 1224 backend_state_ == BACKEND_UNINITIALIZED) | 1197 backend_state_ == BACKEND_UNINITIALIZED) |
| 1225 ? BACKEND_OPEN | 1198 ? BACKEND_OPEN |
| 1226 : BACKEND_CLOSED; | 1199 : BACKEND_CLOSED; |
| 1227 | 1200 |
| 1228 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", error, | 1201 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", error, |
| 1229 ErrorType::ERROR_TYPE_LAST + 1); | 1202 ErrorType::ERROR_TYPE_LAST + 1); |
| 1230 | 1203 |
| 1231 scheduler_->CompleteOperationAndRunNext(); | 1204 scheduler_->CompleteOperationAndRunNext(); |
| 1232 } | 1205 } |
| 1233 | 1206 |
| 1234 void ServiceWorkerCache::PendingClosure(const base::Closure& callback) { | 1207 void CacheStorageCache::PendingClosure(const base::Closure& callback) { |
| 1235 base::WeakPtr<ServiceWorkerCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1208 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
| 1236 | 1209 |
| 1237 callback.Run(); | 1210 callback.Run(); |
| 1238 if (cache) | 1211 if (cache) |
| 1239 scheduler_->CompleteOperationAndRunNext(); | 1212 scheduler_->CompleteOperationAndRunNext(); |
| 1240 } | 1213 } |
| 1241 | 1214 |
| 1242 void ServiceWorkerCache::PendingErrorCallback(const ErrorCallback& callback, | 1215 void CacheStorageCache::PendingErrorCallback(const ErrorCallback& callback, |
| 1243 ErrorType error) { | 1216 ErrorType error) { |
| 1244 base::WeakPtr<ServiceWorkerCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1217 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
| 1245 | 1218 |
| 1246 callback.Run(error); | 1219 callback.Run(error); |
| 1247 if (cache) | 1220 if (cache) |
| 1248 scheduler_->CompleteOperationAndRunNext(); | 1221 scheduler_->CompleteOperationAndRunNext(); |
| 1249 } | 1222 } |
| 1250 | 1223 |
| 1251 void ServiceWorkerCache::PendingResponseCallback( | 1224 void CacheStorageCache::PendingResponseCallback( |
| 1252 const ResponseCallback& callback, | 1225 const ResponseCallback& callback, |
| 1253 ErrorType error, | 1226 ErrorType error, |
| 1254 scoped_ptr<ServiceWorkerResponse> response, | 1227 scoped_ptr<ServiceWorkerResponse> response, |
| 1255 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 1228 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 1256 base::WeakPtr<ServiceWorkerCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1229 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
| 1257 | 1230 |
| 1258 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 1231 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
| 1259 if (cache) | 1232 if (cache) |
| 1260 scheduler_->CompleteOperationAndRunNext(); | 1233 scheduler_->CompleteOperationAndRunNext(); |
| 1261 } | 1234 } |
| 1262 | 1235 |
| 1263 void ServiceWorkerCache::PendingRequestsCallback( | 1236 void CacheStorageCache::PendingRequestsCallback( |
| 1264 const RequestsCallback& callback, | 1237 const RequestsCallback& callback, |
| 1265 ErrorType error, | 1238 ErrorType error, |
| 1266 scoped_ptr<Requests> requests) { | 1239 scoped_ptr<Requests> requests) { |
| 1267 base::WeakPtr<ServiceWorkerCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1240 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
| 1268 | 1241 |
| 1269 callback.Run(error, requests.Pass()); | 1242 callback.Run(error, requests.Pass()); |
| 1270 if (cache) | 1243 if (cache) |
| 1271 scheduler_->CompleteOperationAndRunNext(); | 1244 scheduler_->CompleteOperationAndRunNext(); |
| 1272 } | 1245 } |
| 1273 | 1246 |
| 1274 } // namespace content | 1247 } // namespace content |
| OLD | NEW |