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/service_worker/service_worker_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/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "content/browser/service_worker/service_worker_cache.pb.h" | 13 #include "content/browser/service_worker/service_worker_cache.pb.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
19 #include "storage/browser/blob/blob_data_handle.h" | 19 #include "storage/browser/blob/blob_data_handle.h" |
20 #include "storage/browser/blob/blob_storage_context.h" | 20 #include "storage/browser/blob/blob_storage_context.h" |
21 #include "storage/browser/blob/blob_url_request_job_factory.h" | 21 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 22 #include "storage/browser/quota/quota_manager_proxy.h" |
22 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" | 23 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" |
23 | 24 |
24 namespace content { | 25 namespace content { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; | 29 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; |
29 typedef base::Callback<void(bool)> BoolCallback; | 30 typedef base::Callback<void(bool)> BoolCallback; |
30 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> | 31 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> |
31 EntryBoolCallback; | 32 EntryBoolCallback; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 scoped_refptr<net::IOBufferWithSize> buffer; | 87 scoped_refptr<net::IOBufferWithSize> buffer; |
87 scoped_refptr<storage::BlobData> blob_data; | 88 scoped_refptr<storage::BlobData> blob_data; |
88 int total_bytes_read; | 89 int total_bytes_read; |
89 | 90 |
90 DISALLOW_COPY_AND_ASSIGN(ResponseReadContext); | 91 DISALLOW_COPY_AND_ASSIGN(ResponseReadContext); |
91 }; | 92 }; |
92 | 93 |
93 // Streams data from a blob and writes it to a given disk_cache::Entry. | 94 // Streams data from a blob and writes it to a given disk_cache::Entry. |
94 class BlobReader : public net::URLRequest::Delegate { | 95 class BlobReader : public net::URLRequest::Delegate { |
95 public: | 96 public: |
96 typedef base::Callback<void(bool)> BoolCallback; | 97 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> |
| 98 EntryAndBoolCallback; |
97 | 99 |
98 BlobReader(disk_cache::ScopedEntryPtr entry) | 100 BlobReader() |
99 : cache_entry_offset_(0), | 101 : cache_entry_offset_(0), |
100 buffer_(new net::IOBufferWithSize(kBufferSize)), | 102 buffer_(new net::IOBufferWithSize(kBufferSize)), |
101 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) {} |
| 104 |
| 105 // |entry| is passed to the callback once complete. |
| 106 void StreamBlobToCache(disk_cache::ScopedEntryPtr entry, |
| 107 net::URLRequestContext* request_context, |
| 108 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 109 const EntryAndBoolCallback& callback) { |
102 DCHECK(entry); | 110 DCHECK(entry); |
103 entry_ = entry.Pass(); | 111 entry_ = entry.Pass(); |
104 } | |
105 | |
106 void StreamBlobToCache(net::URLRequestContext* request_context, | |
107 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | |
108 const BoolCallback& callback) { | |
109 callback_ = callback; | 112 callback_ = callback; |
110 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( | 113 blob_request_ = storage::BlobProtocolHandler::CreateBlobRequest( |
111 blob_data_handle.Pass(), request_context, this); | 114 blob_data_handle.Pass(), request_context, this); |
112 blob_request_->Start(); | 115 blob_request_->Start(); |
113 } | 116 } |
114 | 117 |
115 // net::URLRequest::Delegate overrides for reading blobs. | 118 // net::URLRequest::Delegate overrides for reading blobs. |
116 void OnReceivedRedirect(net::URLRequest* request, | 119 void OnReceivedRedirect(net::URLRequest* request, |
117 const net::RedirectInfo& redirect_info, | 120 const net::RedirectInfo& redirect_info, |
118 bool* defer_redirect) override { | 121 bool* defer_redirect) override { |
(...skipping 12 matching lines...) Expand all Loading... |
131 const net::SSLInfo& ssl_info, | 134 const net::SSLInfo& ssl_info, |
132 bool fatal) override { | 135 bool fatal) override { |
133 NOTREACHED(); | 136 NOTREACHED(); |
134 } | 137 } |
135 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override { | 138 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override { |
136 NOTREACHED(); | 139 NOTREACHED(); |
137 } | 140 } |
138 | 141 |
139 void OnResponseStarted(net::URLRequest* request) override { | 142 void OnResponseStarted(net::URLRequest* request) override { |
140 if (!request->status().is_success()) { | 143 if (!request->status().is_success()) { |
141 callback_.Run(false); | 144 callback_.Run(entry_.Pass(), false); |
142 return; | 145 return; |
143 } | 146 } |
144 ReadFromBlob(); | 147 ReadFromBlob(); |
145 } | 148 } |
146 | 149 |
147 virtual void ReadFromBlob() { | 150 virtual void ReadFromBlob() { |
148 int bytes_read = 0; | 151 int bytes_read = 0; |
149 bool done = | 152 bool done = |
150 blob_request_->Read(buffer_.get(), buffer_->size(), &bytes_read); | 153 blob_request_->Read(buffer_.get(), buffer_->size(), &bytes_read); |
151 if (done) | 154 if (done) |
152 OnReadCompleted(blob_request_.get(), bytes_read); | 155 OnReadCompleted(blob_request_.get(), bytes_read); |
153 } | 156 } |
154 | 157 |
155 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { | 158 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { |
156 if (!request->status().is_success()) { | 159 if (!request->status().is_success()) { |
157 callback_.Run(false); | 160 callback_.Run(entry_.Pass(), false); |
158 return; | 161 return; |
159 } | 162 } |
160 | 163 |
161 if (bytes_read == 0) { | 164 if (bytes_read == 0) { |
162 callback_.Run(true); | 165 callback_.Run(entry_.Pass(), true); |
163 return; | 166 return; |
164 } | 167 } |
165 | 168 |
166 net::CompletionCallback cache_write_callback = | 169 net::CompletionCallback cache_write_callback = |
167 base::Bind(&BlobReader::DidWriteDataToEntry, | 170 base::Bind(&BlobReader::DidWriteDataToEntry, |
168 weak_ptr_factory_.GetWeakPtr(), | 171 weak_ptr_factory_.GetWeakPtr(), |
169 bytes_read); | 172 bytes_read); |
170 | 173 |
171 int rv = entry_->WriteData(INDEX_RESPONSE_BODY, | 174 int rv = entry_->WriteData(INDEX_RESPONSE_BODY, |
172 cache_entry_offset_, | 175 cache_entry_offset_, |
173 buffer_.get(), | 176 buffer_.get(), |
174 bytes_read, | 177 bytes_read, |
175 cache_write_callback, | 178 cache_write_callback, |
176 true /* truncate */); | 179 true /* truncate */); |
177 if (rv != net::ERR_IO_PENDING) | 180 if (rv != net::ERR_IO_PENDING) |
178 cache_write_callback.Run(rv); | 181 cache_write_callback.Run(rv); |
179 } | 182 } |
180 | 183 |
181 void DidWriteDataToEntry(int expected_bytes, int rv) { | 184 void DidWriteDataToEntry(int expected_bytes, int rv) { |
182 if (rv != expected_bytes) { | 185 if (rv != expected_bytes) { |
183 callback_.Run(false); | 186 callback_.Run(entry_.Pass(), false); |
184 return; | 187 return; |
185 } | 188 } |
186 | 189 |
187 cache_entry_offset_ += rv; | 190 cache_entry_offset_ += rv; |
188 ReadFromBlob(); | 191 ReadFromBlob(); |
189 } | 192 } |
190 | 193 |
191 private: | 194 private: |
192 int cache_entry_offset_; | 195 int cache_entry_offset_; |
193 disk_cache::ScopedEntryPtr entry_; | 196 disk_cache::ScopedEntryPtr entry_; |
194 scoped_ptr<net::URLRequest> blob_request_; | 197 scoped_ptr<net::URLRequest> blob_request_; |
195 BoolCallback callback_; | 198 EntryAndBoolCallback callback_; |
196 scoped_refptr<net::IOBufferWithSize> buffer_; | 199 scoped_refptr<net::IOBufferWithSize> buffer_; |
197 base::WeakPtrFactory<BlobReader> weak_ptr_factory_; | 200 base::WeakPtrFactory<BlobReader> weak_ptr_factory_; |
198 }; | 201 }; |
199 | 202 |
200 // The state needed to pass between ServiceWorkerCache::Put callbacks. | 203 // The state needed to pass between ServiceWorkerCache::Put callbacks. |
201 struct PutContext { | 204 struct PutContext { |
202 PutContext(scoped_ptr<ServiceWorkerFetchRequest> request, | 205 PutContext( |
203 scoped_ptr<ServiceWorkerResponse> response, | 206 const GURL& origin, |
204 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 207 scoped_ptr<ServiceWorkerFetchRequest> request, |
205 const ServiceWorkerCache::ResponseCallback& callback, | 208 scoped_ptr<ServiceWorkerResponse> response, |
206 net::URLRequestContext* request_context) | 209 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
207 : request(request.Pass()), | 210 const ServiceWorkerCache::ResponseCallback& callback, |
| 211 net::URLRequestContext* request_context, |
| 212 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) |
| 213 : origin(origin), |
| 214 request(request.Pass()), |
208 response(response.Pass()), | 215 response(response.Pass()), |
209 blob_data_handle(blob_data_handle.Pass()), | 216 blob_data_handle(blob_data_handle.Pass()), |
210 callback(callback), | 217 callback(callback), |
211 request_context(request_context), | 218 request_context(request_context), |
| 219 quota_manager_proxy(quota_manager_proxy), |
212 cache_entry(NULL) {} | 220 cache_entry(NULL) {} |
213 ~PutContext() { | 221 ~PutContext() { |
214 if (cache_entry) | 222 if (cache_entry) |
215 cache_entry->Close(); | 223 cache_entry->Close(); |
216 } | 224 } |
217 | 225 |
218 // Input parameters to the Put function. | 226 // Input parameters to the Put function. |
| 227 GURL origin; |
219 scoped_ptr<ServiceWorkerFetchRequest> request; | 228 scoped_ptr<ServiceWorkerFetchRequest> request; |
220 scoped_ptr<ServiceWorkerResponse> response; | 229 scoped_ptr<ServiceWorkerResponse> response; |
221 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 230 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
222 ServiceWorkerCache::ResponseCallback callback; | 231 ServiceWorkerCache::ResponseCallback callback; |
223 | |
224 net::URLRequestContext* request_context; | 232 net::URLRequestContext* request_context; |
| 233 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; |
225 | 234 |
226 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to | 235 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to |
227 // CreateEntry. | 236 // CreateEntry. |
228 disk_cache::Entry* cache_entry; | 237 disk_cache::Entry* cache_entry; |
229 | 238 |
230 // The BlobDataHandle for the output ServiceWorkerResponse. | 239 // The BlobDataHandle for the output ServiceWorkerResponse. |
231 scoped_ptr<storage::BlobDataHandle> out_blob_data_handle; | 240 scoped_ptr<storage::BlobDataHandle> out_blob_data_handle; |
232 | 241 |
233 DISALLOW_COPY_AND_ASSIGN(PutContext); | 242 DISALLOW_COPY_AND_ASSIGN(PutContext); |
234 }; | 243 }; |
235 | 244 |
236 // Put callbacks | 245 // Put callbacks |
237 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); | 246 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); |
238 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, | 247 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, |
239 int expected_bytes, | 248 int expected_bytes, |
240 int rv); | 249 int rv); |
241 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, | 250 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, |
242 scoped_ptr<BlobReader> blob_reader, | 251 scoped_ptr<BlobReader> blob_reader, |
| 252 disk_cache::ScopedEntryPtr entry, |
243 bool success); | 253 bool success); |
244 | 254 |
245 // Match callbacks | 255 // Match callbacks |
246 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, | 256 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, |
247 const ServiceWorkerCache::ResponseCallback& callback, | 257 const ServiceWorkerCache::ResponseCallback& callback, |
248 base::WeakPtr<storage::BlobStorageContext> blob_storage, | 258 base::WeakPtr<storage::BlobStorageContext> blob_storage, |
249 scoped_ptr<disk_cache::Entry*> entryptr, | 259 scoped_ptr<disk_cache::Entry*> entryptr, |
250 int rv); | 260 int rv); |
251 void MatchDidReadHeaderData( | 261 void MatchDidReadHeaderData( |
252 scoped_ptr<ServiceWorkerFetchRequest> request, | 262 scoped_ptr<ServiceWorkerFetchRequest> request, |
253 const ServiceWorkerCache::ResponseCallback& callback, | 263 const ServiceWorkerCache::ResponseCallback& callback, |
254 base::WeakPtr<storage::BlobStorageContext> blob_storage, | 264 base::WeakPtr<storage::BlobStorageContext> blob_storage, |
255 disk_cache::ScopedEntryPtr entry, | 265 disk_cache::ScopedEntryPtr entry, |
256 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); | 266 scoped_ptr<ServiceWorkerRequestResponseHeaders> headers); |
257 void MatchDidReadResponseBodyData( | 267 void MatchDidReadResponseBodyData( |
258 scoped_ptr<ServiceWorkerFetchRequest> request, | 268 scoped_ptr<ServiceWorkerFetchRequest> request, |
259 const ServiceWorkerCache::ResponseCallback& callback, | 269 const ServiceWorkerCache::ResponseCallback& callback, |
260 base::WeakPtr<storage::BlobStorageContext> blob_storage, | 270 base::WeakPtr<storage::BlobStorageContext> blob_storage, |
261 disk_cache::ScopedEntryPtr entry, | 271 disk_cache::ScopedEntryPtr entry, |
262 scoped_ptr<ServiceWorkerResponse> response, | 272 scoped_ptr<ServiceWorkerResponse> response, |
263 scoped_ptr<ResponseReadContext> response_context, | 273 scoped_ptr<ResponseReadContext> response_context, |
264 int rv); | 274 int rv); |
265 void MatchDoneWithBody(scoped_ptr<ServiceWorkerFetchRequest> request, | 275 void MatchDoneWithBody(scoped_ptr<ServiceWorkerFetchRequest> request, |
266 const ServiceWorkerCache::ResponseCallback& callback, | 276 const ServiceWorkerCache::ResponseCallback& callback, |
267 base::WeakPtr<storage::BlobStorageContext> blob_storage, | 277 base::WeakPtr<storage::BlobStorageContext> blob_storage, |
268 scoped_ptr<ServiceWorkerResponse> response, | 278 scoped_ptr<ServiceWorkerResponse> response, |
269 scoped_ptr<ResponseReadContext> response_context); | 279 scoped_ptr<ResponseReadContext> response_context); |
270 | 280 |
271 // Delete callbacks | 281 // Delete callbacks |
272 void DeleteDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, | 282 void DeleteDidOpenEntry( |
273 const ServiceWorkerCache::ErrorCallback& callback, | 283 const GURL& origin, |
274 scoped_ptr<disk_cache::Entry*> entryptr, | 284 scoped_ptr<ServiceWorkerFetchRequest> request, |
275 int rv); | 285 const ServiceWorkerCache::ErrorCallback& callback, |
| 286 scoped_ptr<disk_cache::Entry*> entryptr, |
| 287 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 288 int rv); |
276 | 289 |
277 // Copy headers out of a cache entry and into a protobuf. The callback is | 290 // Copy headers out of a cache entry and into a protobuf. The callback is |
278 // guaranteed to be run. | 291 // guaranteed to be run. |
279 void ReadHeaders(disk_cache::Entry* entry, const HeadersCallback& callback); | 292 void ReadHeaders(disk_cache::Entry* entry, const HeadersCallback& callback); |
280 void ReadHeadersDidReadHeaderData( | 293 void ReadHeadersDidReadHeaderData( |
281 disk_cache::Entry* entry, | 294 disk_cache::Entry* entry, |
282 const HeadersCallback& callback, | 295 const HeadersCallback& callback, |
283 const scoped_refptr<net::IOBufferWithSize>& buffer, | 296 const scoped_refptr<net::IOBufferWithSize>& buffer, |
284 int rv); | 297 int rv); |
285 | 298 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, | 374 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, |
362 scoped_ptr<ServiceWorkerResponse>(), | 375 scoped_ptr<ServiceWorkerResponse>(), |
363 scoped_ptr<storage::BlobDataHandle>()); | 376 scoped_ptr<storage::BlobDataHandle>()); |
364 return; | 377 return; |
365 } | 378 } |
366 | 379 |
367 // The metadata is written, now for the response content. The data is streamed | 380 // The metadata is written, now for the response content. The data is streamed |
368 // from the blob into the cache entry. | 381 // from the blob into the cache entry. |
369 | 382 |
370 if (put_context->response->blob_uuid.empty()) { | 383 if (put_context->response->blob_uuid.empty()) { |
| 384 if (put_context->quota_manager_proxy.get()) { |
| 385 put_context->quota_manager_proxy->NotifyStorageModified( |
| 386 storage::QuotaClient::kServiceWorkerCache, |
| 387 put_context->origin, |
| 388 storage::kStorageTypeTemporary, |
| 389 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); |
| 390 } |
| 391 |
371 put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK, | 392 put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK, |
372 put_context->response.Pass(), | 393 put_context->response.Pass(), |
373 scoped_ptr<storage::BlobDataHandle>()); | 394 scoped_ptr<storage::BlobDataHandle>()); |
374 return; | 395 return; |
375 } | 396 } |
376 | 397 |
377 DCHECK(put_context->blob_data_handle); | 398 DCHECK(put_context->blob_data_handle); |
378 | 399 |
379 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); | 400 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); |
380 put_context->cache_entry = NULL; | 401 put_context->cache_entry = NULL; |
381 scoped_ptr<BlobReader> reader(new BlobReader(entry.Pass())); | 402 scoped_ptr<BlobReader> reader(new BlobReader()); |
382 | |
383 BlobReader* reader_ptr = reader.get(); | 403 BlobReader* reader_ptr = reader.get(); |
384 | 404 |
385 // Grab some pointers before passing put_context in Bind. | 405 // Grab some pointers before passing put_context in Bind. |
386 net::URLRequestContext* request_context = put_context->request_context; | 406 net::URLRequestContext* request_context = put_context->request_context; |
387 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 407 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
388 put_context->blob_data_handle.Pass(); | 408 put_context->blob_data_handle.Pass(); |
389 | 409 |
390 reader_ptr->StreamBlobToCache(request_context, | 410 reader_ptr->StreamBlobToCache(entry.Pass(), |
| 411 request_context, |
391 blob_data_handle.Pass(), | 412 blob_data_handle.Pass(), |
392 base::Bind(PutDidWriteBlobToCache, | 413 base::Bind(PutDidWriteBlobToCache, |
393 base::Passed(put_context.Pass()), | 414 base::Passed(put_context.Pass()), |
394 base::Passed(reader.Pass()))); | 415 base::Passed(reader.Pass()))); |
395 } | 416 } |
396 | 417 |
397 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, | 418 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, |
398 scoped_ptr<BlobReader> blob_reader, | 419 scoped_ptr<BlobReader> blob_reader, |
| 420 disk_cache::ScopedEntryPtr entry, |
399 bool success) { | 421 bool success) { |
| 422 DCHECK(entry); |
| 423 put_context->cache_entry = entry.release(); |
| 424 |
400 if (!success) { | 425 if (!success) { |
401 put_context->cache_entry->Doom(); | 426 put_context->cache_entry->Doom(); |
402 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, | 427 put_context->callback.Run(ServiceWorkerCache::ErrorTypeStorage, |
403 scoped_ptr<ServiceWorkerResponse>(), | 428 scoped_ptr<ServiceWorkerResponse>(), |
404 scoped_ptr<storage::BlobDataHandle>()); | 429 scoped_ptr<storage::BlobDataHandle>()); |
405 return; | 430 return; |
406 } | 431 } |
407 | 432 |
| 433 if (put_context->quota_manager_proxy.get()) { |
| 434 put_context->quota_manager_proxy->NotifyStorageModified( |
| 435 storage::QuotaClient::kServiceWorkerCache, |
| 436 put_context->origin, |
| 437 storage::kStorageTypeTemporary, |
| 438 put_context->cache_entry->GetDataSize(INDEX_HEADERS) + |
| 439 put_context->cache_entry->GetDataSize(INDEX_RESPONSE_BODY)); |
| 440 } |
| 441 |
408 put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK, | 442 put_context->callback.Run(ServiceWorkerCache::ErrorTypeOK, |
409 put_context->response.Pass(), | 443 put_context->response.Pass(), |
410 put_context->out_blob_data_handle.Pass()); | 444 put_context->out_blob_data_handle.Pass()); |
411 } | 445 } |
412 | 446 |
413 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, | 447 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, |
414 const ServiceWorkerCache::ResponseCallback& callback, | 448 const ServiceWorkerCache::ResponseCallback& callback, |
415 base::WeakPtr<storage::BlobStorageContext> blob_storage, | 449 base::WeakPtr<storage::BlobStorageContext> blob_storage, |
416 scoped_ptr<disk_cache::Entry*> entryptr, | 450 scoped_ptr<disk_cache::Entry*> entryptr, |
417 int rv) { | 451 int rv) { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 } | 666 } |
633 | 667 |
634 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | 668 scoped_ptr<storage::BlobDataHandle> blob_data_handle( |
635 blob_storage->AddFinishedBlob(response_context->blob_data.get())); | 669 blob_storage->AddFinishedBlob(response_context->blob_data.get())); |
636 | 670 |
637 callback.Run(ServiceWorkerCache::ErrorTypeOK, | 671 callback.Run(ServiceWorkerCache::ErrorTypeOK, |
638 response.Pass(), | 672 response.Pass(), |
639 blob_data_handle.Pass()); | 673 blob_data_handle.Pass()); |
640 } | 674 } |
641 | 675 |
642 void DeleteDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, | 676 void DeleteDidOpenEntry( |
643 const ServiceWorkerCache::ErrorCallback& callback, | 677 const GURL& origin, |
644 scoped_ptr<disk_cache::Entry*> entryptr, | 678 scoped_ptr<ServiceWorkerFetchRequest> request, |
645 int rv) { | 679 const ServiceWorkerCache::ErrorCallback& callback, |
| 680 scoped_ptr<disk_cache::Entry*> entryptr, |
| 681 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 682 int rv) { |
646 if (rv != net::OK) { | 683 if (rv != net::OK) { |
647 callback.Run(ServiceWorkerCache::ErrorTypeNotFound); | 684 callback.Run(ServiceWorkerCache::ErrorTypeNotFound); |
648 return; | 685 return; |
649 } | 686 } |
650 | 687 |
651 DCHECK(entryptr); | 688 DCHECK(entryptr); |
652 disk_cache::ScopedEntryPtr entry(*entryptr); | 689 disk_cache::ScopedEntryPtr entry(*entryptr); |
653 | 690 |
| 691 if (quota_manager_proxy.get()) { |
| 692 quota_manager_proxy->NotifyStorageModified( |
| 693 storage::QuotaClient::kServiceWorkerCache, |
| 694 origin, |
| 695 storage::kStorageTypeTemporary, |
| 696 -1 * (entry->GetDataSize(INDEX_HEADERS) + |
| 697 entry->GetDataSize(INDEX_RESPONSE_BODY))); |
| 698 } |
| 699 |
654 entry->Doom(); | 700 entry->Doom(); |
655 callback.Run(ServiceWorkerCache::ErrorTypeOK); | 701 callback.Run(ServiceWorkerCache::ErrorTypeOK); |
656 } | 702 } |
657 | 703 |
658 void ReadHeaders(disk_cache::Entry* entry, const HeadersCallback& callback) { | 704 void ReadHeaders(disk_cache::Entry* entry, const HeadersCallback& callback) { |
659 DCHECK(entry); | 705 DCHECK(entry); |
660 | 706 |
661 scoped_refptr<net::IOBufferWithSize> buffer( | 707 scoped_refptr<net::IOBufferWithSize> buffer( |
662 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); | 708 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); |
663 | 709 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 | 783 |
738 // Used for enumerating cache entries. | 784 // Used for enumerating cache entries. |
739 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 785 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
740 disk_cache::Entry* enumerated_entry; | 786 disk_cache::Entry* enumerated_entry; |
741 | 787 |
742 DISALLOW_COPY_AND_ASSIGN(KeysContext); | 788 DISALLOW_COPY_AND_ASSIGN(KeysContext); |
743 }; | 789 }; |
744 | 790 |
745 // static | 791 // static |
746 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( | 792 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreateMemoryCache( |
| 793 const GURL& origin, |
747 net::URLRequestContext* request_context, | 794 net::URLRequestContext* request_context, |
| 795 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
748 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 796 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
749 return make_scoped_refptr( | 797 return make_scoped_refptr(new ServiceWorkerCache(origin, |
750 new ServiceWorkerCache(base::FilePath(), request_context, blob_context)); | 798 base::FilePath(), |
| 799 request_context, |
| 800 quota_manager_proxy, |
| 801 blob_context)); |
751 } | 802 } |
752 | 803 |
753 // static | 804 // static |
754 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache( | 805 scoped_refptr<ServiceWorkerCache> ServiceWorkerCache::CreatePersistentCache( |
| 806 const GURL& origin, |
755 const base::FilePath& path, | 807 const base::FilePath& path, |
756 net::URLRequestContext* request_context, | 808 net::URLRequestContext* request_context, |
| 809 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
757 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 810 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
758 return make_scoped_refptr( | 811 return make_scoped_refptr(new ServiceWorkerCache( |
759 new ServiceWorkerCache(path, request_context, blob_context)); | 812 origin, path, request_context, quota_manager_proxy, blob_context)); |
760 } | 813 } |
761 | 814 |
762 ServiceWorkerCache::~ServiceWorkerCache() { | 815 ServiceWorkerCache::~ServiceWorkerCache() { |
763 } | 816 } |
764 | 817 |
765 base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() { | 818 base::WeakPtr<ServiceWorkerCache> ServiceWorkerCache::AsWeakPtr() { |
766 return weak_ptr_factory_.GetWeakPtr(); | 819 return weak_ptr_factory_.GetWeakPtr(); |
767 } | 820 } |
768 | 821 |
769 void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 822 void ServiceWorkerCache::Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 } | 906 } |
854 | 907 |
855 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); | 908 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); |
856 | 909 |
857 disk_cache::Entry** entry_ptr = entry.get(); | 910 disk_cache::Entry** entry_ptr = entry.get(); |
858 | 911 |
859 ServiceWorkerFetchRequest* request_ptr = request.get(); | 912 ServiceWorkerFetchRequest* request_ptr = request.get(); |
860 | 913 |
861 net::CompletionCallback open_entry_callback = | 914 net::CompletionCallback open_entry_callback = |
862 base::Bind(DeleteDidOpenEntry, | 915 base::Bind(DeleteDidOpenEntry, |
| 916 origin_, |
863 base::Passed(request.Pass()), | 917 base::Passed(request.Pass()), |
864 callback, | 918 callback, |
865 base::Passed(entry.Pass())); | 919 base::Passed(entry.Pass()), |
| 920 quota_manager_proxy_); |
866 | 921 |
867 int rv = backend_->OpenEntry( | 922 int rv = backend_->OpenEntry( |
868 request_ptr->url.spec(), entry_ptr, open_entry_callback); | 923 request_ptr->url.spec(), entry_ptr, open_entry_callback); |
869 if (rv != net::ERR_IO_PENDING) | 924 if (rv != net::ERR_IO_PENDING) |
870 open_entry_callback.Run(rv); | 925 open_entry_callback.Run(rv); |
871 } | 926 } |
872 | 927 |
873 void ServiceWorkerCache::Keys(const RequestsCallback& callback) { | 928 void ServiceWorkerCache::Keys(const RequestsCallback& callback) { |
874 if (!initialized_) { | 929 if (!initialized_) { |
875 Init(base::Bind( | 930 Init(base::Bind( |
(...skipping 29 matching lines...) Expand all Loading... |
905 | 960 |
906 if (rv != net::ERR_IO_PENDING) | 961 if (rv != net::ERR_IO_PENDING) |
907 open_entry_callback.Run(rv); | 962 open_entry_callback.Run(rv); |
908 } | 963 } |
909 | 964 |
910 void ServiceWorkerCache::Close() { | 965 void ServiceWorkerCache::Close() { |
911 backend_.reset(); | 966 backend_.reset(); |
912 } | 967 } |
913 | 968 |
914 ServiceWorkerCache::ServiceWorkerCache( | 969 ServiceWorkerCache::ServiceWorkerCache( |
| 970 const GURL& origin, |
915 const base::FilePath& path, | 971 const base::FilePath& path, |
916 net::URLRequestContext* request_context, | 972 net::URLRequestContext* request_context, |
| 973 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
917 base::WeakPtr<storage::BlobStorageContext> blob_context) | 974 base::WeakPtr<storage::BlobStorageContext> blob_context) |
918 : path_(path), | 975 : origin_(origin), |
| 976 path_(path), |
919 request_context_(request_context), | 977 request_context_(request_context), |
| 978 quota_manager_proxy_(quota_manager_proxy), |
920 blob_storage_context_(blob_context), | 979 blob_storage_context_(blob_context), |
921 initialized_(false), | 980 initialized_(false), |
922 weak_ptr_factory_(this) { | 981 weak_ptr_factory_(this) { |
923 } | 982 } |
924 | 983 |
925 void ServiceWorkerCache::PutImpl( | 984 void ServiceWorkerCache::PutImpl( |
926 scoped_ptr<ServiceWorkerFetchRequest> request, | 985 scoped_ptr<ServiceWorkerFetchRequest> request, |
927 scoped_ptr<ServiceWorkerResponse> response, | 986 scoped_ptr<ServiceWorkerResponse> response, |
928 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 987 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
929 const ResponseCallback& callback) { | 988 const ResponseCallback& callback) { |
930 if (!backend_) { | 989 if (!backend_) { |
931 callback.Run(ErrorTypeStorage, | 990 callback.Run(ErrorTypeStorage, |
932 scoped_ptr<ServiceWorkerResponse>(), | 991 scoped_ptr<ServiceWorkerResponse>(), |
933 scoped_ptr<storage::BlobDataHandle>()); | 992 scoped_ptr<storage::BlobDataHandle>()); |
934 return; | 993 return; |
935 } | 994 } |
936 | 995 |
937 scoped_ptr<PutContext> put_context(new PutContext(request.Pass(), | 996 scoped_ptr<PutContext> put_context(new PutContext(origin_, |
| 997 request.Pass(), |
938 response.Pass(), | 998 response.Pass(), |
939 blob_data_handle.Pass(), | 999 blob_data_handle.Pass(), |
940 callback, | 1000 callback, |
941 request_context_)); | 1001 request_context_, |
| 1002 quota_manager_proxy_)); |
942 | 1003 |
943 if (put_context->blob_data_handle) { | 1004 if (put_context->blob_data_handle) { |
944 // Grab another handle to the blob for the callback response. | 1005 // Grab another handle to the blob for the callback response. |
945 put_context->out_blob_data_handle = | 1006 put_context->out_blob_data_handle = |
946 blob_storage_context_->GetBlobDataFromUUID( | 1007 blob_storage_context_->GetBlobDataFromUUID( |
947 put_context->response->blob_uuid); | 1008 put_context->response->blob_uuid); |
948 } | 1009 } |
949 | 1010 |
950 disk_cache::Entry** entry_ptr = &put_context->cache_entry; | 1011 disk_cache::Entry** entry_ptr = &put_context->cache_entry; |
951 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); | 1012 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1097 initialized_ = true; | 1158 initialized_ = true; |
1098 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 1159 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); |
1099 it != init_callbacks_.end(); | 1160 it != init_callbacks_.end(); |
1100 ++it) { | 1161 ++it) { |
1101 it->Run(); | 1162 it->Run(); |
1102 } | 1163 } |
1103 init_callbacks_.clear(); | 1164 init_callbacks_.clear(); |
1104 } | 1165 } |
1105 | 1166 |
1106 } // namespace content | 1167 } // namespace content |
OLD | NEW |