| 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/cache_storage/cache_storage_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/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "storage/browser/blob/blob_data_handle.h" | 25 #include "storage/browser/blob/blob_data_handle.h" |
| 26 #include "storage/browser/blob/blob_storage_context.h" | 26 #include "storage/browser/blob/blob_storage_context.h" |
| 27 #include "storage/browser/blob/blob_url_request_job_factory.h" | 27 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 28 #include "storage/browser/quota/quota_manager_proxy.h" | 28 #include "storage/browser/quota/quota_manager_proxy.h" |
| 29 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" | 29 #include "third_party/WebKit/public/platform/WebServiceWorkerResponseType.h" |
| 30 | 30 |
| 31 namespace content { | 31 namespace content { |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 typedef base::Callback<void(disk_cache::ScopedEntryPtr, bool)> | 35 // This class ensures that the cache and the entry have a lifetime as long as |
| 36 EntryBoolCallback; | 36 // the blob that is created to contain them. |
| 37 class CacheStorageCacheDataHandle |
| 38 : public storage::BlobDataBuilder::DataHandle { |
| 39 public: |
| 40 CacheStorageCacheDataHandle(const scoped_refptr<CacheStorageCache>& cache, |
| 41 disk_cache::ScopedEntryPtr entry) |
| 42 : cache_(cache), entry_(entry.Pass()) {} |
| 43 |
| 44 private: |
| 45 ~CacheStorageCacheDataHandle() override {} |
| 46 |
| 47 scoped_refptr<CacheStorageCache> cache_; |
| 48 disk_cache::ScopedEntryPtr entry_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle); |
| 51 }; |
| 52 |
| 37 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; | 53 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; |
| 38 | 54 |
| 39 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 55 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 40 | 56 |
| 41 // The maximum size of an individual cache. Ultimately cache size is controlled | 57 // The maximum size of an individual cache. Ultimately cache size is controlled |
| 42 // per-origin. | 58 // per-origin. |
| 43 const int kMaxCacheBytes = 512 * 1024 * 1024; | 59 const int kMaxCacheBytes = 512 * 1024 * 1024; |
| 44 | 60 |
| 45 // Buffer size for cache and blob reading/writing. | |
| 46 const int kBufferSize = 1024 * 512; | |
| 47 | |
| 48 void NotReachedCompletionCallback(int rv) { | 61 void NotReachedCompletionCallback(int rv) { |
| 49 NOTREACHED(); | 62 NOTREACHED(); |
| 50 } | 63 } |
| 51 | 64 |
| 52 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( | 65 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( |
| 53 CacheResponse::ResponseType response_type) { | 66 CacheResponse::ResponseType response_type) { |
| 54 switch (response_type) { | 67 switch (response_type) { |
| 55 case CacheResponse::BASIC_TYPE: | 68 case CacheResponse::BASIC_TYPE: |
| 56 return blink::WebServiceWorkerResponseTypeBasic; | 69 return blink::WebServiceWorkerResponseTypeBasic; |
| 57 case CacheResponse::CORS_TYPE: | 70 case CacheResponse::CORS_TYPE: |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 scoped_ptr<CacheStorageCache::Requests> out_keys; | 204 scoped_ptr<CacheStorageCache::Requests> out_keys; |
| 192 | 205 |
| 193 // Used for enumerating cache entries. | 206 // Used for enumerating cache entries. |
| 194 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 207 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
| 195 disk_cache::Entry* enumerated_entry; | 208 disk_cache::Entry* enumerated_entry; |
| 196 | 209 |
| 197 private: | 210 private: |
| 198 DISALLOW_COPY_AND_ASSIGN(KeysContext); | 211 DISALLOW_COPY_AND_ASSIGN(KeysContext); |
| 199 }; | 212 }; |
| 200 | 213 |
| 201 struct CacheStorageCache::MatchContext { | |
| 202 MatchContext(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 203 const CacheStorageCache::ResponseCallback& callback, | |
| 204 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) | |
| 205 : request(request.Pass()), | |
| 206 original_callback(callback), | |
| 207 blob_storage_context(blob_storage_context), | |
| 208 entry(nullptr), | |
| 209 total_bytes_read(0) {} | |
| 210 | |
| 211 ~MatchContext() { | |
| 212 if (entry) | |
| 213 entry->Close(); | |
| 214 } | |
| 215 | |
| 216 // Input | |
| 217 scoped_ptr<ServiceWorkerFetchRequest> request; | |
| 218 CacheStorageCache::ResponseCallback original_callback; | |
| 219 base::WeakPtr<storage::BlobStorageContext> blob_storage_context; | |
| 220 disk_cache::Entry* entry; | |
| 221 | |
| 222 // Output | |
| 223 scoped_ptr<ServiceWorkerResponse> response; | |
| 224 scoped_ptr<storage::BlobDataBuilder> blob_data; | |
| 225 | |
| 226 // For reading the cache entry data into a blob. | |
| 227 scoped_refptr<net::IOBufferWithSize> response_body_buffer; | |
| 228 size_t total_bytes_read; | |
| 229 | |
| 230 private: | |
| 231 DISALLOW_COPY_AND_ASSIGN(MatchContext); | |
| 232 }; | |
| 233 | |
| 234 // The state needed to pass between CacheStorageCache::Put callbacks. | 214 // The state needed to pass between CacheStorageCache::Put callbacks. |
| 235 struct CacheStorageCache::PutContext { | 215 struct CacheStorageCache::PutContext { |
| 236 PutContext( | 216 PutContext( |
| 237 const GURL& origin, | 217 const GURL& origin, |
| 238 scoped_ptr<ServiceWorkerFetchRequest> request, | 218 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 239 scoped_ptr<ServiceWorkerResponse> response, | 219 scoped_ptr<ServiceWorkerResponse> response, |
| 240 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 220 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 241 const CacheStorageCache::ErrorCallback& callback, | 221 const CacheStorageCache::ErrorCallback& callback, |
| 242 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 222 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 243 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) | 223 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) |
| 244 : origin(origin), | 224 : origin(origin), |
| 245 request(request.Pass()), | 225 request(request.Pass()), |
| 246 response(response.Pass()), | 226 response(response.Pass()), |
| 247 blob_data_handle(blob_data_handle.Pass()), | 227 blob_data_handle(blob_data_handle.Pass()), |
| 248 callback(callback), | 228 callback(callback), |
| 249 request_context_getter(request_context_getter), | 229 request_context_getter(request_context_getter), |
| 250 quota_manager_proxy(quota_manager_proxy), | 230 quota_manager_proxy(quota_manager_proxy) {} |
| 251 cache_entry(NULL) {} | |
| 252 ~PutContext() { | |
| 253 if (cache_entry) | |
| 254 cache_entry->Close(); | |
| 255 } | |
| 256 | 231 |
| 257 // Input parameters to the Put function. | 232 // Input parameters to the Put function. |
| 258 GURL origin; | 233 GURL origin; |
| 259 scoped_ptr<ServiceWorkerFetchRequest> request; | 234 scoped_ptr<ServiceWorkerFetchRequest> request; |
| 260 scoped_ptr<ServiceWorkerResponse> response; | 235 scoped_ptr<ServiceWorkerResponse> response; |
| 261 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 236 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
| 262 CacheStorageCache::ErrorCallback callback; | 237 CacheStorageCache::ErrorCallback callback; |
| 263 scoped_refptr<net::URLRequestContextGetter> request_context_getter; | 238 scoped_refptr<net::URLRequestContextGetter> request_context_getter; |
| 264 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; | 239 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; |
| 265 | 240 disk_cache::ScopedEntryPtr cache_entry; |
| 266 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to | |
| 267 // CreateEntry. | |
| 268 disk_cache::Entry* cache_entry; | |
| 269 | 241 |
| 270 private: | 242 private: |
| 271 DISALLOW_COPY_AND_ASSIGN(PutContext); | 243 DISALLOW_COPY_AND_ASSIGN(PutContext); |
| 272 }; | 244 }; |
| 273 | 245 |
| 274 // static | 246 // static |
| 275 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( | 247 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( |
| 276 const GURL& origin, | 248 const GURL& origin, |
| 277 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 249 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 278 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 250 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 443 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 472 const ResponseCallback& callback) { | 444 const ResponseCallback& callback) { |
| 473 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 445 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
| 474 if (backend_state_ != BACKEND_OPEN) { | 446 if (backend_state_ != BACKEND_OPEN) { |
| 475 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 447 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 476 scoped_ptr<ServiceWorkerResponse>(), | 448 scoped_ptr<ServiceWorkerResponse>(), |
| 477 scoped_ptr<storage::BlobDataHandle>()); | 449 scoped_ptr<storage::BlobDataHandle>()); |
| 478 return; | 450 return; |
| 479 } | 451 } |
| 480 | 452 |
| 481 scoped_ptr<MatchContext> match_context( | 453 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); |
| 482 new MatchContext(request.Pass(), callback, blob_storage_context_)); | 454 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); |
| 455 ServiceWorkerFetchRequest* request_ptr = request.get(); |
| 483 | 456 |
| 484 disk_cache::Entry** entry_ptr = &match_context->entry; | 457 net::CompletionCallback open_entry_callback = |
| 485 ServiceWorkerFetchRequest* request_ptr = match_context->request.get(); | 458 base::Bind(&CacheStorageCache::MatchDidOpenEntry, |
| 486 | 459 weak_ptr_factory_.GetWeakPtr(), base::Passed(request.Pass()), |
| 487 net::CompletionCallback open_entry_callback = base::Bind( | 460 callback, base::Passed(scoped_entry_ptr.Pass())); |
| 488 &CacheStorageCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), | |
| 489 base::Passed(match_context.Pass())); | |
| 490 | 461 |
| 491 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, | 462 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, |
| 492 open_entry_callback); | 463 open_entry_callback); |
| 493 if (rv != net::ERR_IO_PENDING) | 464 if (rv != net::ERR_IO_PENDING) |
| 494 open_entry_callback.Run(rv); | 465 open_entry_callback.Run(rv); |
| 495 } | 466 } |
| 496 | 467 |
| 497 void CacheStorageCache::MatchDidOpenEntry( | 468 void CacheStorageCache::MatchDidOpenEntry( |
| 498 scoped_ptr<MatchContext> match_context, | 469 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 470 const ResponseCallback& callback, |
| 471 scoped_ptr<disk_cache::Entry*> entry_ptr, |
| 499 int rv) { | 472 int rv) { |
| 500 if (rv != net::OK) { | 473 if (rv != net::OK) { |
| 501 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 474 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 502 scoped_ptr<ServiceWorkerResponse>(), | 475 scoped_ptr<ServiceWorkerResponse>(), |
| 503 scoped_ptr<storage::BlobDataHandle>()); | 476 scoped_ptr<storage::BlobDataHandle>()); |
| 477 return; |
| 478 } |
| 479 disk_cache::ScopedEntryPtr entry(*entry_ptr); |
| 480 |
| 481 MetadataCallback headers_callback = base::Bind( |
| 482 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), |
| 483 base::Passed(request.Pass()), callback, base::Passed(entry.Pass())); |
| 484 |
| 485 ReadMetadata(*entry_ptr, headers_callback); |
| 486 } |
| 487 |
| 488 void CacheStorageCache::MatchDidReadMetadata( |
| 489 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 490 const ResponseCallback& callback, |
| 491 disk_cache::ScopedEntryPtr entry, |
| 492 scoped_ptr<CacheMetadata> metadata) { |
| 493 if (!metadata) { |
| 494 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 495 scoped_ptr<ServiceWorkerResponse>(), |
| 496 scoped_ptr<storage::BlobDataHandle>()); |
| 504 return; | 497 return; |
| 505 } | 498 } |
| 506 | 499 |
| 507 // Copy the entry pointer before passing it in base::Bind. | 500 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse( |
| 508 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | 501 request->url, metadata->response().status_code(), |
| 509 DCHECK(tmp_entry_ptr); | |
| 510 | |
| 511 MetadataCallback headers_callback = base::Bind( | |
| 512 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), | |
| 513 base::Passed(match_context.Pass())); | |
| 514 | |
| 515 ReadMetadata(tmp_entry_ptr, headers_callback); | |
| 516 } | |
| 517 | |
| 518 void CacheStorageCache::MatchDidReadMetadata( | |
| 519 scoped_ptr<MatchContext> match_context, | |
| 520 scoped_ptr<CacheMetadata> metadata) { | |
| 521 if (!metadata) { | |
| 522 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
| 523 scoped_ptr<ServiceWorkerResponse>(), | |
| 524 scoped_ptr<storage::BlobDataHandle>()); | |
| 525 return; | |
| 526 } | |
| 527 | |
| 528 match_context->response.reset(new ServiceWorkerResponse( | |
| 529 match_context->request->url, metadata->response().status_code(), | |
| 530 metadata->response().status_text(), | 502 metadata->response().status_text(), |
| 531 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), | 503 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), |
| 532 ServiceWorkerHeaderMap(), "", 0, GURL())); | 504 ServiceWorkerHeaderMap(), "", 0, GURL())); |
| 533 | 505 |
| 534 ServiceWorkerResponse* response = match_context->response.get(); | |
| 535 | |
| 536 if (metadata->response().has_url()) | 506 if (metadata->response().has_url()) |
| 537 response->url = GURL(metadata->response().url()); | 507 response->url = GURL(metadata->response().url()); |
| 538 | 508 |
| 539 for (int i = 0; i < metadata->response().headers_size(); ++i) { | 509 for (int i = 0; i < metadata->response().headers_size(); ++i) { |
| 540 const CacheHeaderMap header = metadata->response().headers(i); | 510 const CacheHeaderMap header = metadata->response().headers(i); |
| 541 DCHECK_EQ(std::string::npos, header.name().find('\0')); | 511 DCHECK_EQ(std::string::npos, header.name().find('\0')); |
| 542 DCHECK_EQ(std::string::npos, header.value().find('\0')); | 512 DCHECK_EQ(std::string::npos, header.value().find('\0')); |
| 543 response->headers.insert(std::make_pair(header.name(), header.value())); | 513 response->headers.insert(std::make_pair(header.name(), header.value())); |
| 544 } | 514 } |
| 545 | 515 |
| 546 ServiceWorkerHeaderMap cached_request_headers; | 516 ServiceWorkerHeaderMap cached_request_headers; |
| 547 for (int i = 0; i < metadata->request().headers_size(); ++i) { | 517 for (int i = 0; i < metadata->request().headers_size(); ++i) { |
| 548 const CacheHeaderMap header = metadata->request().headers(i); | 518 const CacheHeaderMap header = metadata->request().headers(i); |
| 549 DCHECK_EQ(std::string::npos, header.name().find('\0')); | 519 DCHECK_EQ(std::string::npos, header.name().find('\0')); |
| 550 DCHECK_EQ(std::string::npos, header.value().find('\0')); | 520 DCHECK_EQ(std::string::npos, header.value().find('\0')); |
| 551 cached_request_headers[header.name()] = header.value(); | 521 cached_request_headers[header.name()] = header.value(); |
| 552 } | 522 } |
| 553 | 523 |
| 554 if (!VaryMatches(match_context->request->headers, cached_request_headers, | 524 if (!VaryMatches(request->headers, cached_request_headers, |
| 555 response->headers)) { | 525 response->headers)) { |
| 556 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 526 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 557 scoped_ptr<ServiceWorkerResponse>(), | 527 scoped_ptr<ServiceWorkerResponse>(), |
| 558 scoped_ptr<storage::BlobDataHandle>()); | 528 scoped_ptr<storage::BlobDataHandle>()); |
| 559 return; | 529 return; |
| 560 } | 530 } |
| 561 | 531 |
| 562 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { | 532 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { |
| 563 match_context->original_callback.Run(CACHE_STORAGE_OK, | 533 callback.Run(CACHE_STORAGE_OK, response.Pass(), |
| 564 match_context->response.Pass(), | 534 scoped_ptr<storage::BlobDataHandle>()); |
| 565 scoped_ptr<storage::BlobDataHandle>()); | |
| 566 return; | 535 return; |
| 567 } | 536 } |
| 568 | 537 |
| 569 // Stream the response body into a blob. | 538 if (!blob_storage_context_) { |
| 570 if (!match_context->blob_storage_context) { | 539 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 571 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 540 scoped_ptr<ServiceWorkerResponse>(), |
| 572 scoped_ptr<ServiceWorkerResponse>(), | 541 scoped_ptr<storage::BlobDataHandle>()); |
| 573 scoped_ptr<storage::BlobDataHandle>()); | |
| 574 return; | 542 return; |
| 575 } | 543 } |
| 576 | 544 |
| 545 // Create a blob with the response body data. |
| 546 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); |
| 577 response->blob_uuid = base::GenerateGUID(); | 547 response->blob_uuid = base::GenerateGUID(); |
| 548 storage::BlobDataBuilder blob_data(response->blob_uuid); |
| 578 | 549 |
| 579 match_context->blob_data.reset( | 550 disk_cache::Entry* tmp_entry = entry.get(); |
| 580 new storage::BlobDataBuilder(response->blob_uuid)); | 551 blob_data.AppendDiskCacheEntry( |
| 581 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize); | 552 new CacheStorageCacheDataHandle(this, entry.Pass()), tmp_entry, |
| 582 | 553 INDEX_RESPONSE_BODY); |
| 583 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | |
| 584 net::IOBufferWithSize* response_body_buffer = | |
| 585 match_context->response_body_buffer.get(); | |
| 586 | |
| 587 net::CompletionCallback read_callback = base::Bind( | |
| 588 &CacheStorageCache::MatchDidReadResponseBodyData, | |
| 589 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); | |
| 590 | |
| 591 int read_rv = | |
| 592 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer, | |
| 593 response_body_buffer->size(), read_callback); | |
| 594 | |
| 595 if (read_rv != net::ERR_IO_PENDING) | |
| 596 read_callback.Run(read_rv); | |
| 597 } | |
| 598 | |
| 599 void CacheStorageCache::MatchDidReadResponseBodyData( | |
| 600 scoped_ptr<MatchContext> match_context, | |
| 601 int rv) { | |
| 602 if (rv < 0) { | |
| 603 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
| 604 scoped_ptr<ServiceWorkerResponse>(), | |
| 605 scoped_ptr<storage::BlobDataHandle>()); | |
| 606 return; | |
| 607 } | |
| 608 | |
| 609 if (rv == 0) { | |
| 610 match_context->response->blob_uuid = match_context->blob_data->uuid(); | |
| 611 match_context->response->blob_size = match_context->total_bytes_read; | |
| 612 MatchDoneWithBody(match_context.Pass()); | |
| 613 return; | |
| 614 } | |
| 615 | |
| 616 // TODO(jkarlin): This copying of the the entire cache response into memory is | |
| 617 // awful. Create a new interface around SimpleCache that provides access the | |
| 618 // data directly from the file. See bug http://crbug.com/403493. | |
| 619 match_context->blob_data->AppendData( | |
| 620 match_context->response_body_buffer->data(), rv); | |
| 621 match_context->total_bytes_read += rv; | |
| 622 int total_bytes_read = match_context->total_bytes_read; | |
| 623 | |
| 624 // Grab some pointers before passing match_context in bind. | |
| 625 net::IOBufferWithSize* buffer = match_context->response_body_buffer.get(); | |
| 626 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | |
| 627 | |
| 628 net::CompletionCallback read_callback = base::Bind( | |
| 629 &CacheStorageCache::MatchDidReadResponseBodyData, | |
| 630 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); | |
| 631 | |
| 632 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read, | |
| 633 buffer, buffer->size(), read_callback); | |
| 634 | |
| 635 if (read_rv != net::ERR_IO_PENDING) | |
| 636 read_callback.Run(read_rv); | |
| 637 } | |
| 638 | |
| 639 void CacheStorageCache::MatchDoneWithBody( | |
| 640 scoped_ptr<MatchContext> match_context) { | |
| 641 if (!match_context->blob_storage_context) { | |
| 642 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
| 643 scoped_ptr<ServiceWorkerResponse>(), | |
| 644 scoped_ptr<storage::BlobDataHandle>()); | |
| 645 return; | |
| 646 } | |
| 647 | |
| 648 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | 554 scoped_ptr<storage::BlobDataHandle> blob_data_handle( |
| 649 match_context->blob_storage_context->AddFinishedBlob( | 555 blob_storage_context_->AddFinishedBlob(&blob_data)); |
| 650 match_context->blob_data.get())); | 556 callback.Run(CACHE_STORAGE_OK, response.Pass(), blob_data_handle.Pass()); |
| 651 | |
| 652 match_context->original_callback.Run(CACHE_STORAGE_OK, | |
| 653 match_context->response.Pass(), | |
| 654 blob_data_handle.Pass()); | |
| 655 } | 557 } |
| 656 | 558 |
| 657 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, | 559 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, |
| 658 const ErrorCallback& callback) { | 560 const ErrorCallback& callback) { |
| 659 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); | 561 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); |
| 660 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); | 562 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); |
| 661 | 563 |
| 662 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( | 564 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( |
| 663 operation.request.url, operation.request.method, | 565 operation.request.url, operation.request.method, |
| 664 operation.request.headers, operation.request.referrer, | 566 operation.request.headers, operation.request.referrer, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 base::Passed(put_context.Pass()))); | 617 base::Passed(put_context.Pass()))); |
| 716 } | 618 } |
| 717 | 619 |
| 718 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, | 620 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, |
| 719 CacheStorageError delete_error) { | 621 CacheStorageError delete_error) { |
| 720 if (backend_state_ != BACKEND_OPEN) { | 622 if (backend_state_ != BACKEND_OPEN) { |
| 721 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 623 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 722 return; | 624 return; |
| 723 } | 625 } |
| 724 | 626 |
| 725 disk_cache::Entry** entry_ptr = &put_context->cache_entry; | 627 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); |
| 628 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); |
| 726 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); | 629 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); |
| 727 disk_cache::Backend* backend_ptr = backend_.get(); | 630 disk_cache::Backend* backend_ptr = backend_.get(); |
| 728 | 631 |
| 729 net::CompletionCallback create_entry_callback = base::Bind( | 632 net::CompletionCallback create_entry_callback = base::Bind( |
| 730 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), | 633 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), |
| 731 base::Passed(put_context.Pass())); | 634 base::Passed(scoped_entry_ptr.Pass()), base::Passed(put_context.Pass())); |
| 732 | 635 |
| 733 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, | 636 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, |
| 734 create_entry_callback); | 637 create_entry_callback); |
| 735 | 638 |
| 736 if (create_rv != net::ERR_IO_PENDING) | 639 if (create_rv != net::ERR_IO_PENDING) |
| 737 create_entry_callback.Run(create_rv); | 640 create_entry_callback.Run(create_rv); |
| 738 } | 641 } |
| 739 | 642 |
| 740 void CacheStorageCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context, | 643 void CacheStorageCache::PutDidCreateEntry( |
| 741 int rv) { | 644 scoped_ptr<disk_cache::Entry*> entry_ptr, |
| 645 scoped_ptr<PutContext> put_context, |
| 646 int rv) { |
| 742 if (rv != net::OK) { | 647 if (rv != net::OK) { |
| 743 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); | 648 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); |
| 744 return; | 649 return; |
| 745 } | 650 } |
| 746 | 651 put_context->cache_entry.reset(*entry_ptr); |
| 747 DCHECK(put_context->cache_entry); | |
| 748 | 652 |
| 749 CacheMetadata metadata; | 653 CacheMetadata metadata; |
| 750 CacheRequest* request_metadata = metadata.mutable_request(); | 654 CacheRequest* request_metadata = metadata.mutable_request(); |
| 751 request_metadata->set_method(put_context->request->method); | 655 request_metadata->set_method(put_context->request->method); |
| 752 for (ServiceWorkerHeaderMap::const_iterator it = | 656 for (ServiceWorkerHeaderMap::const_iterator it = |
| 753 put_context->request->headers.begin(); | 657 put_context->request->headers.begin(); |
| 754 it != put_context->request->headers.end(); ++it) { | 658 it != put_context->request->headers.end(); ++it) { |
| 755 DCHECK_EQ(std::string::npos, it->first.find('\0')); | 659 DCHECK_EQ(std::string::npos, it->first.find('\0')); |
| 756 DCHECK_EQ(std::string::npos, it->second.find('\0')); | 660 DCHECK_EQ(std::string::npos, it->second.find('\0')); |
| 757 CacheHeaderMap* header_map = request_metadata->add_headers(); | 661 CacheHeaderMap* header_map = request_metadata->add_headers(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 778 scoped_ptr<std::string> serialized(new std::string()); | 682 scoped_ptr<std::string> serialized(new std::string()); |
| 779 if (!metadata.SerializeToString(serialized.get())) { | 683 if (!metadata.SerializeToString(serialized.get())) { |
| 780 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 684 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 781 return; | 685 return; |
| 782 } | 686 } |
| 783 | 687 |
| 784 scoped_refptr<net::StringIOBuffer> buffer( | 688 scoped_refptr<net::StringIOBuffer> buffer( |
| 785 new net::StringIOBuffer(serialized.Pass())); | 689 new net::StringIOBuffer(serialized.Pass())); |
| 786 | 690 |
| 787 // Get a temporary copy of the entry pointer before passing it in base::Bind. | 691 // Get a temporary copy of the entry pointer before passing it in base::Bind. |
| 788 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry; | 692 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry.get(); |
| 789 | 693 |
| 790 net::CompletionCallback write_headers_callback = base::Bind( | 694 net::CompletionCallback write_headers_callback = base::Bind( |
| 791 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), | 695 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), |
| 792 base::Passed(put_context.Pass()), buffer->size()); | 696 base::Passed(put_context.Pass()), buffer->size()); |
| 793 | 697 |
| 794 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), | 698 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), |
| 795 buffer->size(), write_headers_callback, | 699 buffer->size(), write_headers_callback, |
| 796 true /* truncate */); | 700 true /* truncate */); |
| 797 | 701 |
| 798 if (rv != net::ERR_IO_PENDING) | 702 if (rv != net::ERR_IO_PENDING) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 818 storage::kStorageTypeTemporary, | 722 storage::kStorageTypeTemporary, |
| 819 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); | 723 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); |
| 820 } | 724 } |
| 821 | 725 |
| 822 put_context->callback.Run(CACHE_STORAGE_OK); | 726 put_context->callback.Run(CACHE_STORAGE_OK); |
| 823 return; | 727 return; |
| 824 } | 728 } |
| 825 | 729 |
| 826 DCHECK(put_context->blob_data_handle); | 730 DCHECK(put_context->blob_data_handle); |
| 827 | 731 |
| 828 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); | 732 disk_cache::ScopedEntryPtr entry(put_context->cache_entry.Pass()); |
| 829 put_context->cache_entry = NULL; | 733 put_context->cache_entry = NULL; |
| 830 scoped_ptr<CacheStorageBlobToDiskCache> reader( | 734 scoped_ptr<CacheStorageBlobToDiskCache> reader( |
| 831 new CacheStorageBlobToDiskCache()); | 735 new CacheStorageBlobToDiskCache()); |
| 832 CacheStorageBlobToDiskCache* reader_ptr = reader.get(); | 736 CacheStorageBlobToDiskCache* reader_ptr = reader.get(); |
| 833 | 737 |
| 834 // Grab some pointers before passing put_context in Bind. | 738 // Grab some pointers before passing put_context in Bind. |
| 835 scoped_refptr<net::URLRequestContextGetter> request_context_getter = | 739 scoped_refptr<net::URLRequestContextGetter> request_context_getter = |
| 836 put_context->request_context_getter; | 740 put_context->request_context_getter; |
| 837 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 741 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
| 838 put_context->blob_data_handle.Pass(); | 742 put_context->blob_data_handle.Pass(); |
| 839 | 743 |
| 840 reader_ptr->StreamBlobToCache( | 744 reader_ptr->StreamBlobToCache( |
| 841 entry.Pass(), INDEX_RESPONSE_BODY, request_context_getter, | 745 entry.Pass(), INDEX_RESPONSE_BODY, request_context_getter, |
| 842 blob_data_handle.Pass(), | 746 blob_data_handle.Pass(), |
| 843 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, | 747 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, |
| 844 weak_ptr_factory_.GetWeakPtr(), | 748 weak_ptr_factory_.GetWeakPtr(), |
| 845 base::Passed(put_context.Pass()), | 749 base::Passed(put_context.Pass()), |
| 846 base::Passed(reader.Pass()))); | 750 base::Passed(reader.Pass()))); |
| 847 } | 751 } |
| 848 | 752 |
| 849 void CacheStorageCache::PutDidWriteBlobToCache( | 753 void CacheStorageCache::PutDidWriteBlobToCache( |
| 850 scoped_ptr<PutContext> put_context, | 754 scoped_ptr<PutContext> put_context, |
| 851 scoped_ptr<CacheStorageBlobToDiskCache> blob_reader, | 755 scoped_ptr<CacheStorageBlobToDiskCache> blob_reader, |
| 852 disk_cache::ScopedEntryPtr entry, | 756 disk_cache::ScopedEntryPtr entry, |
| 853 bool success) { | 757 bool success) { |
| 854 DCHECK(entry); | 758 DCHECK(entry); |
| 855 put_context->cache_entry = entry.release(); | 759 put_context->cache_entry = entry.Pass(); |
| 856 | 760 |
| 857 if (!success) { | 761 if (!success) { |
| 858 put_context->cache_entry->Doom(); | 762 put_context->cache_entry->Doom(); |
| 859 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 763 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 860 return; | 764 return; |
| 861 } | 765 } |
| 862 | 766 |
| 863 if (put_context->quota_manager_proxy.get()) { | 767 if (put_context->quota_manager_proxy.get()) { |
| 864 put_context->quota_manager_proxy->NotifyStorageModified( | 768 put_context->quota_manager_proxy->NotifyStorageModified( |
| 865 storage::QuotaClient::kServiceWorkerCache, put_context->origin, | 769 storage::QuotaClient::kServiceWorkerCache, put_context->origin, |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1167 CacheStorageError error, | 1071 CacheStorageError error, |
| 1168 scoped_ptr<Requests> requests) { | 1072 scoped_ptr<Requests> requests) { |
| 1169 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1073 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
| 1170 | 1074 |
| 1171 callback.Run(error, requests.Pass()); | 1075 callback.Run(error, requests.Pass()); |
| 1172 if (cache) | 1076 if (cache) |
| 1173 scheduler_->CompleteOperationAndRunNext(); | 1077 scheduler_->CompleteOperationAndRunNext(); |
| 1174 } | 1078 } |
| 1175 | 1079 |
| 1176 } // namespace content | 1080 } // namespace content |
| OLD | NEW |