| 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 13 matching lines...) Expand all Loading... |
| 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(disk_cache::ScopedEntryPtr, bool)> | 34 // This class ensures that the cache and the entry have a lifetime as long as |
| 35 EntryBoolCallback; | 35 // the blob that is created to contain them. |
| 36 class CacheStorageCacheDataHandle |
| 37 : public storage::BlobDataBuilder::DataHandle { |
| 38 public: |
| 39 CacheStorageCacheDataHandle(const scoped_refptr<CacheStorageCache>& cache, |
| 40 disk_cache::ScopedEntryPtr entry) |
| 41 : cache_(cache), entry_(entry.Pass()) {} |
| 42 |
| 43 private: |
| 44 ~CacheStorageCacheDataHandle() override {} |
| 45 |
| 46 scoped_refptr<CacheStorageCache> cache_; |
| 47 disk_cache::ScopedEntryPtr entry_; |
| 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle); |
| 50 }; |
| 51 |
| 36 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; | 52 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; |
| 37 | 53 |
| 38 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 54 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
| 39 | 55 |
| 40 // The maximum size of an individual cache. Ultimately cache size is controlled | 56 // The maximum size of an individual cache. Ultimately cache size is controlled |
| 41 // per-origin. | 57 // per-origin. |
| 42 const int kMaxCacheBytes = 512 * 1024 * 1024; | 58 const int kMaxCacheBytes = 512 * 1024 * 1024; |
| 43 | 59 |
| 44 // Buffer size for cache and blob reading/writing. | 60 // Buffer size for cache and blob reading/writing. |
| 45 const int kBufferSize = 1024 * 512; | 61 const int kBufferSize = 1024 * 512; |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 // The output of the Keys function. | 315 // The output of the Keys function. |
| 300 scoped_ptr<CacheStorageCache::Requests> out_keys; | 316 scoped_ptr<CacheStorageCache::Requests> out_keys; |
| 301 | 317 |
| 302 // Used for enumerating cache entries. | 318 // Used for enumerating cache entries. |
| 303 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 319 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
| 304 disk_cache::Entry* enumerated_entry; | 320 disk_cache::Entry* enumerated_entry; |
| 305 | 321 |
| 306 DISALLOW_COPY_AND_ASSIGN(KeysContext); | 322 DISALLOW_COPY_AND_ASSIGN(KeysContext); |
| 307 }; | 323 }; |
| 308 | 324 |
| 309 struct CacheStorageCache::MatchContext { | |
| 310 MatchContext(scoped_ptr<ServiceWorkerFetchRequest> request, | |
| 311 const CacheStorageCache::ResponseCallback& callback, | |
| 312 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) | |
| 313 : request(request.Pass()), | |
| 314 original_callback(callback), | |
| 315 blob_storage_context(blob_storage_context), | |
| 316 entry(nullptr), | |
| 317 total_bytes_read(0) {} | |
| 318 | |
| 319 ~MatchContext() { | |
| 320 if (entry) | |
| 321 entry->Close(); | |
| 322 } | |
| 323 | |
| 324 // Input | |
| 325 scoped_ptr<ServiceWorkerFetchRequest> request; | |
| 326 CacheStorageCache::ResponseCallback original_callback; | |
| 327 base::WeakPtr<storage::BlobStorageContext> blob_storage_context; | |
| 328 disk_cache::Entry* entry; | |
| 329 | |
| 330 // Output | |
| 331 scoped_ptr<ServiceWorkerResponse> response; | |
| 332 scoped_ptr<storage::BlobDataBuilder> blob_data; | |
| 333 | |
| 334 // For reading the cache entry data into a blob. | |
| 335 scoped_refptr<net::IOBufferWithSize> response_body_buffer; | |
| 336 size_t total_bytes_read; | |
| 337 | |
| 338 DISALLOW_COPY_AND_ASSIGN(MatchContext); | |
| 339 }; | |
| 340 | |
| 341 // The state needed to pass between CacheStorageCache::Put callbacks. | 325 // The state needed to pass between CacheStorageCache::Put callbacks. |
| 342 struct CacheStorageCache::PutContext { | 326 struct CacheStorageCache::PutContext { |
| 343 PutContext( | 327 PutContext( |
| 344 const GURL& origin, | 328 const GURL& origin, |
| 345 scoped_ptr<ServiceWorkerFetchRequest> request, | 329 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 346 scoped_ptr<ServiceWorkerResponse> response, | 330 scoped_ptr<ServiceWorkerResponse> response, |
| 347 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 331 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
| 348 const CacheStorageCache::ErrorCallback& callback, | 332 const CacheStorageCache::ErrorCallback& callback, |
| 349 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 333 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 350 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) | 334 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) |
| 351 : origin(origin), | 335 : origin(origin), |
| 352 request(request.Pass()), | 336 request(request.Pass()), |
| 353 response(response.Pass()), | 337 response(response.Pass()), |
| 354 blob_data_handle(blob_data_handle.Pass()), | 338 blob_data_handle(blob_data_handle.Pass()), |
| 355 callback(callback), | 339 callback(callback), |
| 356 request_context_getter(request_context_getter), | 340 request_context_getter(request_context_getter), |
| 357 quota_manager_proxy(quota_manager_proxy), | 341 quota_manager_proxy(quota_manager_proxy) {} |
| 358 cache_entry(NULL) {} | |
| 359 ~PutContext() { | |
| 360 if (cache_entry) | |
| 361 cache_entry->Close(); | |
| 362 } | |
| 363 | 342 |
| 364 // Input parameters to the Put function. | 343 // Input parameters to the Put function. |
| 365 GURL origin; | 344 GURL origin; |
| 366 scoped_ptr<ServiceWorkerFetchRequest> request; | 345 scoped_ptr<ServiceWorkerFetchRequest> request; |
| 367 scoped_ptr<ServiceWorkerResponse> response; | 346 scoped_ptr<ServiceWorkerResponse> response; |
| 368 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 347 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
| 369 CacheStorageCache::ErrorCallback callback; | 348 CacheStorageCache::ErrorCallback callback; |
| 370 scoped_refptr<net::URLRequestContextGetter> request_context_getter; | 349 scoped_refptr<net::URLRequestContextGetter> request_context_getter; |
| 371 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; | 350 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; |
| 372 | 351 disk_cache::ScopedEntryPtr cache_entry; |
| 373 // This isn't a scoped_ptr because the disk_cache needs an Entry** as input to | |
| 374 // CreateEntry. | |
| 375 disk_cache::Entry* cache_entry; | |
| 376 | 352 |
| 377 DISALLOW_COPY_AND_ASSIGN(PutContext); | 353 DISALLOW_COPY_AND_ASSIGN(PutContext); |
| 378 }; | 354 }; |
| 379 | 355 |
| 380 // static | 356 // static |
| 381 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( | 357 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( |
| 382 const GURL& origin, | 358 const GURL& origin, |
| 383 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 359 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 384 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 360 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 385 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 361 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 553 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 578 const ResponseCallback& callback) { | 554 const ResponseCallback& callback) { |
| 579 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 555 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
| 580 if (backend_state_ != BACKEND_OPEN) { | 556 if (backend_state_ != BACKEND_OPEN) { |
| 581 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 557 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 582 scoped_ptr<ServiceWorkerResponse>(), | 558 scoped_ptr<ServiceWorkerResponse>(), |
| 583 scoped_ptr<storage::BlobDataHandle>()); | 559 scoped_ptr<storage::BlobDataHandle>()); |
| 584 return; | 560 return; |
| 585 } | 561 } |
| 586 | 562 |
| 587 scoped_ptr<MatchContext> match_context( | 563 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); |
| 588 new MatchContext(request.Pass(), callback, blob_storage_context_)); | 564 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); |
| 565 ServiceWorkerFetchRequest* request_ptr = request.get(); |
| 589 | 566 |
| 590 disk_cache::Entry** entry_ptr = &match_context->entry; | 567 net::CompletionCallback open_entry_callback = |
| 591 ServiceWorkerFetchRequest* request_ptr = match_context->request.get(); | 568 base::Bind(&CacheStorageCache::MatchDidOpenEntry, |
| 592 | 569 weak_ptr_factory_.GetWeakPtr(), base::Passed(request.Pass()), |
| 593 net::CompletionCallback open_entry_callback = base::Bind( | 570 callback, base::Passed(scoped_entry_ptr.Pass())); |
| 594 &CacheStorageCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), | |
| 595 base::Passed(match_context.Pass())); | |
| 596 | 571 |
| 597 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, | 572 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, |
| 598 open_entry_callback); | 573 open_entry_callback); |
| 599 if (rv != net::ERR_IO_PENDING) | 574 if (rv != net::ERR_IO_PENDING) |
| 600 open_entry_callback.Run(rv); | 575 open_entry_callback.Run(rv); |
| 601 } | 576 } |
| 602 | 577 |
| 603 void CacheStorageCache::MatchDidOpenEntry( | 578 void CacheStorageCache::MatchDidOpenEntry( |
| 604 scoped_ptr<MatchContext> match_context, | 579 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 580 const ResponseCallback& callback, |
| 581 scoped_ptr<disk_cache::Entry*> entry_ptr, |
| 605 int rv) { | 582 int rv) { |
| 606 if (rv != net::OK) { | 583 if (rv != net::OK) { |
| 607 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 584 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 608 scoped_ptr<ServiceWorkerResponse>(), | 585 scoped_ptr<ServiceWorkerResponse>(), |
| 609 scoped_ptr<storage::BlobDataHandle>()); | 586 scoped_ptr<storage::BlobDataHandle>()); |
| 587 return; |
| 588 } |
| 589 disk_cache::ScopedEntryPtr entry(*entry_ptr); |
| 590 |
| 591 MetadataCallback headers_callback = base::Bind( |
| 592 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), |
| 593 base::Passed(request.Pass()), callback, base::Passed(entry.Pass())); |
| 594 |
| 595 ReadMetadata(*entry_ptr, headers_callback); |
| 596 } |
| 597 |
| 598 void CacheStorageCache::MatchDidReadMetadata( |
| 599 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 600 const ResponseCallback& callback, |
| 601 disk_cache::ScopedEntryPtr entry, |
| 602 scoped_ptr<CacheMetadata> metadata) { |
| 603 if (!metadata) { |
| 604 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 605 scoped_ptr<ServiceWorkerResponse>(), |
| 606 scoped_ptr<storage::BlobDataHandle>()); |
| 610 return; | 607 return; |
| 611 } | 608 } |
| 612 | 609 |
| 613 // Copy the entry pointer before passing it in base::Bind. | 610 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse( |
| 614 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | 611 request->url, metadata->response().status_code(), |
| 615 DCHECK(tmp_entry_ptr); | |
| 616 | |
| 617 MetadataCallback headers_callback = base::Bind( | |
| 618 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(), | |
| 619 base::Passed(match_context.Pass())); | |
| 620 | |
| 621 ReadMetadata(tmp_entry_ptr, headers_callback); | |
| 622 } | |
| 623 | |
| 624 void CacheStorageCache::MatchDidReadMetadata( | |
| 625 scoped_ptr<MatchContext> match_context, | |
| 626 scoped_ptr<CacheMetadata> metadata) { | |
| 627 if (!metadata) { | |
| 628 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
| 629 scoped_ptr<ServiceWorkerResponse>(), | |
| 630 scoped_ptr<storage::BlobDataHandle>()); | |
| 631 return; | |
| 632 } | |
| 633 | |
| 634 match_context->response.reset(new ServiceWorkerResponse( | |
| 635 match_context->request->url, metadata->response().status_code(), | |
| 636 metadata->response().status_text(), | 612 metadata->response().status_text(), |
| 637 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), | 613 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), |
| 638 ServiceWorkerHeaderMap(), "", 0, GURL())); | 614 ServiceWorkerHeaderMap(), "", 0, GURL())); |
| 639 | 615 |
| 640 ServiceWorkerResponse* response = match_context->response.get(); | |
| 641 | |
| 642 if (metadata->response().has_url()) | 616 if (metadata->response().has_url()) |
| 643 response->url = GURL(metadata->response().url()); | 617 response->url = GURL(metadata->response().url()); |
| 644 | 618 |
| 645 for (int i = 0; i < metadata->response().headers_size(); ++i) { | 619 for (int i = 0; i < metadata->response().headers_size(); ++i) { |
| 646 const CacheHeaderMap header = metadata->response().headers(i); | 620 const CacheHeaderMap header = metadata->response().headers(i); |
| 647 DCHECK_EQ(std::string::npos, header.name().find('\0')); | 621 DCHECK_EQ(std::string::npos, header.name().find('\0')); |
| 648 DCHECK_EQ(std::string::npos, header.value().find('\0')); | 622 DCHECK_EQ(std::string::npos, header.value().find('\0')); |
| 649 response->headers.insert(std::make_pair(header.name(), header.value())); | 623 response->headers.insert(std::make_pair(header.name(), header.value())); |
| 650 } | 624 } |
| 651 | 625 |
| 652 ServiceWorkerHeaderMap cached_request_headers; | 626 ServiceWorkerHeaderMap cached_request_headers; |
| 653 for (int i = 0; i < metadata->request().headers_size(); ++i) { | 627 for (int i = 0; i < metadata->request().headers_size(); ++i) { |
| 654 const CacheHeaderMap header = metadata->request().headers(i); | 628 const CacheHeaderMap header = metadata->request().headers(i); |
| 655 DCHECK_EQ(std::string::npos, header.name().find('\0')); | 629 DCHECK_EQ(std::string::npos, header.name().find('\0')); |
| 656 DCHECK_EQ(std::string::npos, header.value().find('\0')); | 630 DCHECK_EQ(std::string::npos, header.value().find('\0')); |
| 657 cached_request_headers[header.name()] = header.value(); | 631 cached_request_headers[header.name()] = header.value(); |
| 658 } | 632 } |
| 659 | 633 |
| 660 if (!VaryMatches(match_context->request->headers, cached_request_headers, | 634 if (!VaryMatches(request->headers, cached_request_headers, |
| 661 response->headers)) { | 635 response->headers)) { |
| 662 match_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 636 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 663 scoped_ptr<ServiceWorkerResponse>(), | 637 scoped_ptr<ServiceWorkerResponse>(), |
| 664 scoped_ptr<storage::BlobDataHandle>()); | 638 scoped_ptr<storage::BlobDataHandle>()); |
| 665 return; | 639 return; |
| 666 } | 640 } |
| 667 | 641 |
| 668 if (match_context->entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { | 642 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { |
| 669 match_context->original_callback.Run(CACHE_STORAGE_OK, | 643 callback.Run(CACHE_STORAGE_OK, response.Pass(), |
| 670 match_context->response.Pass(), | 644 scoped_ptr<storage::BlobDataHandle>()); |
| 671 scoped_ptr<storage::BlobDataHandle>()); | |
| 672 return; | 645 return; |
| 673 } | 646 } |
| 674 | 647 |
| 675 // Stream the response body into a blob. | 648 if (!blob_storage_context_) { |
| 676 if (!match_context->blob_storage_context) { | 649 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 677 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 650 scoped_ptr<ServiceWorkerResponse>(), |
| 678 scoped_ptr<ServiceWorkerResponse>(), | 651 scoped_ptr<storage::BlobDataHandle>()); |
| 679 scoped_ptr<storage::BlobDataHandle>()); | |
| 680 return; | 652 return; |
| 681 } | 653 } |
| 682 | 654 |
| 655 // Create a blob with the response body data. |
| 656 response->blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); |
| 683 response->blob_uuid = base::GenerateGUID(); | 657 response->blob_uuid = base::GenerateGUID(); |
| 658 storage::BlobDataBuilder blob_data(response->blob_uuid); |
| 684 | 659 |
| 685 match_context->blob_data.reset( | 660 disk_cache::Entry* tmp_entry = entry.get(); |
| 686 new storage::BlobDataBuilder(response->blob_uuid)); | 661 blob_data.AppendDiskCacheEntry( |
| 687 match_context->response_body_buffer = new net::IOBufferWithSize(kBufferSize); | 662 new CacheStorageCacheDataHandle(this, entry.Pass()), tmp_entry, |
| 688 | 663 INDEX_RESPONSE_BODY); |
| 689 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | |
| 690 net::IOBufferWithSize* response_body_buffer = | |
| 691 match_context->response_body_buffer.get(); | |
| 692 | |
| 693 net::CompletionCallback read_callback = base::Bind( | |
| 694 &CacheStorageCache::MatchDidReadResponseBodyData, | |
| 695 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); | |
| 696 | |
| 697 int read_rv = | |
| 698 tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, 0, response_body_buffer, | |
| 699 response_body_buffer->size(), read_callback); | |
| 700 | |
| 701 if (read_rv != net::ERR_IO_PENDING) | |
| 702 read_callback.Run(read_rv); | |
| 703 } | |
| 704 | |
| 705 void CacheStorageCache::MatchDidReadResponseBodyData( | |
| 706 scoped_ptr<MatchContext> match_context, | |
| 707 int rv) { | |
| 708 if (rv < 0) { | |
| 709 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
| 710 scoped_ptr<ServiceWorkerResponse>(), | |
| 711 scoped_ptr<storage::BlobDataHandle>()); | |
| 712 return; | |
| 713 } | |
| 714 | |
| 715 if (rv == 0) { | |
| 716 match_context->response->blob_uuid = match_context->blob_data->uuid(); | |
| 717 match_context->response->blob_size = match_context->total_bytes_read; | |
| 718 MatchDoneWithBody(match_context.Pass()); | |
| 719 return; | |
| 720 } | |
| 721 | |
| 722 // TODO(jkarlin): This copying of the the entire cache response into memory is | |
| 723 // awful. Create a new interface around SimpleCache that provides access the | |
| 724 // data directly from the file. See bug http://crbug.com/403493. | |
| 725 match_context->blob_data->AppendData( | |
| 726 match_context->response_body_buffer->data(), rv); | |
| 727 match_context->total_bytes_read += rv; | |
| 728 int total_bytes_read = match_context->total_bytes_read; | |
| 729 | |
| 730 // Grab some pointers before passing match_context in bind. | |
| 731 net::IOBufferWithSize* buffer = match_context->response_body_buffer.get(); | |
| 732 disk_cache::Entry* tmp_entry_ptr = match_context->entry; | |
| 733 | |
| 734 net::CompletionCallback read_callback = base::Bind( | |
| 735 &CacheStorageCache::MatchDidReadResponseBodyData, | |
| 736 weak_ptr_factory_.GetWeakPtr(), base::Passed(match_context.Pass())); | |
| 737 | |
| 738 int read_rv = tmp_entry_ptr->ReadData(INDEX_RESPONSE_BODY, total_bytes_read, | |
| 739 buffer, buffer->size(), read_callback); | |
| 740 | |
| 741 if (read_rv != net::ERR_IO_PENDING) | |
| 742 read_callback.Run(read_rv); | |
| 743 } | |
| 744 | |
| 745 void CacheStorageCache::MatchDoneWithBody( | |
| 746 scoped_ptr<MatchContext> match_context) { | |
| 747 if (!match_context->blob_storage_context) { | |
| 748 match_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
| 749 scoped_ptr<ServiceWorkerResponse>(), | |
| 750 scoped_ptr<storage::BlobDataHandle>()); | |
| 751 return; | |
| 752 } | |
| 753 | |
| 754 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | 664 scoped_ptr<storage::BlobDataHandle> blob_data_handle( |
| 755 match_context->blob_storage_context->AddFinishedBlob( | 665 blob_storage_context_->AddFinishedBlob(&blob_data)); |
| 756 match_context->blob_data.get())); | 666 callback.Run(CACHE_STORAGE_OK, response.Pass(), blob_data_handle.Pass()); |
| 757 | |
| 758 match_context->original_callback.Run(CACHE_STORAGE_OK, | |
| 759 match_context->response.Pass(), | |
| 760 blob_data_handle.Pass()); | |
| 761 } | 667 } |
| 762 | 668 |
| 763 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, | 669 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, |
| 764 const ErrorCallback& callback) { | 670 const ErrorCallback& callback) { |
| 765 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); | 671 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); |
| 766 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); | 672 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); |
| 767 | 673 |
| 768 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( | 674 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( |
| 769 operation.request.url, operation.request.method, | 675 operation.request.url, operation.request.method, |
| 770 operation.request.headers, operation.request.referrer, | 676 operation.request.headers, operation.request.referrer, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 base::Passed(put_context.Pass()))); | 727 base::Passed(put_context.Pass()))); |
| 822 } | 728 } |
| 823 | 729 |
| 824 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, | 730 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, |
| 825 CacheStorageError delete_error) { | 731 CacheStorageError delete_error) { |
| 826 if (backend_state_ != BACKEND_OPEN) { | 732 if (backend_state_ != BACKEND_OPEN) { |
| 827 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 733 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 828 return; | 734 return; |
| 829 } | 735 } |
| 830 | 736 |
| 831 disk_cache::Entry** entry_ptr = &put_context->cache_entry; | 737 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); |
| 738 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); |
| 832 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); | 739 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); |
| 833 disk_cache::Backend* backend_ptr = backend_.get(); | 740 disk_cache::Backend* backend_ptr = backend_.get(); |
| 834 | 741 |
| 835 net::CompletionCallback create_entry_callback = base::Bind( | 742 net::CompletionCallback create_entry_callback = base::Bind( |
| 836 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), | 743 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), |
| 837 base::Passed(put_context.Pass())); | 744 base::Passed(scoped_entry_ptr.Pass()), base::Passed(put_context.Pass())); |
| 838 | 745 |
| 839 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, | 746 int create_rv = backend_ptr->CreateEntry(request_ptr->url.spec(), entry_ptr, |
| 840 create_entry_callback); | 747 create_entry_callback); |
| 841 | 748 |
| 842 if (create_rv != net::ERR_IO_PENDING) | 749 if (create_rv != net::ERR_IO_PENDING) |
| 843 create_entry_callback.Run(create_rv); | 750 create_entry_callback.Run(create_rv); |
| 844 } | 751 } |
| 845 | 752 |
| 846 void CacheStorageCache::PutDidCreateEntry(scoped_ptr<PutContext> put_context, | 753 void CacheStorageCache::PutDidCreateEntry( |
| 847 int rv) { | 754 scoped_ptr<disk_cache::Entry*> entry_ptr, |
| 755 scoped_ptr<PutContext> put_context, |
| 756 int rv) { |
| 848 if (rv != net::OK) { | 757 if (rv != net::OK) { |
| 849 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); | 758 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); |
| 850 return; | 759 return; |
| 851 } | 760 } |
| 852 | 761 put_context->cache_entry.reset(*entry_ptr); |
| 853 DCHECK(put_context->cache_entry); | |
| 854 | 762 |
| 855 CacheMetadata metadata; | 763 CacheMetadata metadata; |
| 856 CacheRequest* request_metadata = metadata.mutable_request(); | 764 CacheRequest* request_metadata = metadata.mutable_request(); |
| 857 request_metadata->set_method(put_context->request->method); | 765 request_metadata->set_method(put_context->request->method); |
| 858 for (ServiceWorkerHeaderMap::const_iterator it = | 766 for (ServiceWorkerHeaderMap::const_iterator it = |
| 859 put_context->request->headers.begin(); | 767 put_context->request->headers.begin(); |
| 860 it != put_context->request->headers.end(); ++it) { | 768 it != put_context->request->headers.end(); ++it) { |
| 861 DCHECK_EQ(std::string::npos, it->first.find('\0')); | 769 DCHECK_EQ(std::string::npos, it->first.find('\0')); |
| 862 DCHECK_EQ(std::string::npos, it->second.find('\0')); | 770 DCHECK_EQ(std::string::npos, it->second.find('\0')); |
| 863 CacheHeaderMap* header_map = request_metadata->add_headers(); | 771 CacheHeaderMap* header_map = request_metadata->add_headers(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 884 scoped_ptr<std::string> serialized(new std::string()); | 792 scoped_ptr<std::string> serialized(new std::string()); |
| 885 if (!metadata.SerializeToString(serialized.get())) { | 793 if (!metadata.SerializeToString(serialized.get())) { |
| 886 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 794 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 887 return; | 795 return; |
| 888 } | 796 } |
| 889 | 797 |
| 890 scoped_refptr<net::StringIOBuffer> buffer( | 798 scoped_refptr<net::StringIOBuffer> buffer( |
| 891 new net::StringIOBuffer(serialized.Pass())); | 799 new net::StringIOBuffer(serialized.Pass())); |
| 892 | 800 |
| 893 // Get a temporary copy of the entry pointer before passing it in base::Bind. | 801 // Get a temporary copy of the entry pointer before passing it in base::Bind. |
| 894 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry; | 802 disk_cache::Entry* tmp_entry_ptr = put_context->cache_entry.get(); |
| 895 | 803 |
| 896 net::CompletionCallback write_headers_callback = base::Bind( | 804 net::CompletionCallback write_headers_callback = base::Bind( |
| 897 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), | 805 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), |
| 898 base::Passed(put_context.Pass()), buffer->size()); | 806 base::Passed(put_context.Pass()), buffer->size()); |
| 899 | 807 |
| 900 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), | 808 rv = tmp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), |
| 901 buffer->size(), write_headers_callback, | 809 buffer->size(), write_headers_callback, |
| 902 true /* truncate */); | 810 true /* truncate */); |
| 903 | 811 |
| 904 if (rv != net::ERR_IO_PENDING) | 812 if (rv != net::ERR_IO_PENDING) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 924 storage::kStorageTypeTemporary, | 832 storage::kStorageTypeTemporary, |
| 925 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); | 833 put_context->cache_entry->GetDataSize(INDEX_HEADERS)); |
| 926 } | 834 } |
| 927 | 835 |
| 928 put_context->callback.Run(CACHE_STORAGE_OK); | 836 put_context->callback.Run(CACHE_STORAGE_OK); |
| 929 return; | 837 return; |
| 930 } | 838 } |
| 931 | 839 |
| 932 DCHECK(put_context->blob_data_handle); | 840 DCHECK(put_context->blob_data_handle); |
| 933 | 841 |
| 934 disk_cache::ScopedEntryPtr entry(put_context->cache_entry); | 842 disk_cache::ScopedEntryPtr entry(put_context->cache_entry.Pass()); |
| 935 put_context->cache_entry = NULL; | |
| 936 scoped_ptr<BlobReader> reader(new BlobReader()); | 843 scoped_ptr<BlobReader> reader(new BlobReader()); |
| 937 BlobReader* reader_ptr = reader.get(); | 844 BlobReader* reader_ptr = reader.get(); |
| 938 | 845 |
| 939 // Grab some pointers before passing put_context in Bind. | 846 // Grab some pointers before passing put_context in Bind. |
| 940 scoped_refptr<net::URLRequestContextGetter> request_context_getter = | 847 scoped_refptr<net::URLRequestContextGetter> request_context_getter = |
| 941 put_context->request_context_getter; | 848 put_context->request_context_getter; |
| 942 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 849 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
| 943 put_context->blob_data_handle.Pass(); | 850 put_context->blob_data_handle.Pass(); |
| 944 | 851 |
| 945 reader_ptr->StreamBlobToCache( | 852 reader_ptr->StreamBlobToCache( |
| 946 entry.Pass(), request_context_getter, blob_data_handle.Pass(), | 853 entry.Pass(), request_context_getter, blob_data_handle.Pass(), |
| 947 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, | 854 base::Bind(&CacheStorageCache::PutDidWriteBlobToCache, |
| 948 weak_ptr_factory_.GetWeakPtr(), | 855 weak_ptr_factory_.GetWeakPtr(), |
| 949 base::Passed(put_context.Pass()), | 856 base::Passed(put_context.Pass()), |
| 950 base::Passed(reader.Pass()))); | 857 base::Passed(reader.Pass()))); |
| 951 } | 858 } |
| 952 | 859 |
| 953 void CacheStorageCache::PutDidWriteBlobToCache( | 860 void CacheStorageCache::PutDidWriteBlobToCache( |
| 954 scoped_ptr<PutContext> put_context, | 861 scoped_ptr<PutContext> put_context, |
| 955 scoped_ptr<BlobReader> blob_reader, | 862 scoped_ptr<BlobReader> blob_reader, |
| 956 disk_cache::ScopedEntryPtr entry, | 863 disk_cache::ScopedEntryPtr entry, |
| 957 bool success) { | 864 bool success) { |
| 958 DCHECK(entry); | 865 DCHECK(entry); |
| 959 put_context->cache_entry = entry.release(); | 866 put_context->cache_entry = entry.Pass(); |
| 960 | 867 |
| 961 if (!success) { | 868 if (!success) { |
| 962 put_context->cache_entry->Doom(); | 869 put_context->cache_entry->Doom(); |
| 963 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 870 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 964 return; | 871 return; |
| 965 } | 872 } |
| 966 | 873 |
| 967 if (put_context->quota_manager_proxy.get()) { | 874 if (put_context->quota_manager_proxy.get()) { |
| 968 put_context->quota_manager_proxy->NotifyStorageModified( | 875 put_context->quota_manager_proxy->NotifyStorageModified( |
| 969 storage::QuotaClient::kServiceWorkerCache, put_context->origin, | 876 storage::QuotaClient::kServiceWorkerCache, put_context->origin, |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1271 CacheStorageError error, | 1178 CacheStorageError error, |
| 1272 scoped_ptr<Requests> requests) { | 1179 scoped_ptr<Requests> requests) { |
| 1273 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1180 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
| 1274 | 1181 |
| 1275 callback.Run(error, requests.Pass()); | 1182 callback.Run(error, requests.Pass()); |
| 1276 if (cache) | 1183 if (cache) |
| 1277 scheduler_->CompleteOperationAndRunNext(); | 1184 scheduler_->CompleteOperationAndRunNext(); |
| 1278 } | 1185 } |
| 1279 | 1186 |
| 1280 } // namespace content | 1187 } // namespace content |
| OLD | NEW |