| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 std::unique_ptr<ServiceWorkerFetchRequest> request; | 207 std::unique_ptr<ServiceWorkerFetchRequest> request; |
| 208 std::unique_ptr<ServiceWorkerResponse> response; | 208 std::unique_ptr<ServiceWorkerResponse> response; |
| 209 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; | 209 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; |
| 210 CacheStorageCache::ErrorCallback callback; | 210 CacheStorageCache::ErrorCallback callback; |
| 211 disk_cache::ScopedEntryPtr cache_entry; | 211 disk_cache::ScopedEntryPtr cache_entry; |
| 212 | 212 |
| 213 private: | 213 private: |
| 214 DISALLOW_COPY_AND_ASSIGN(PutContext); | 214 DISALLOW_COPY_AND_ASSIGN(PutContext); |
| 215 }; | 215 }; |
| 216 | 216 |
| 217 struct CacheStorageCache::QueryCacheResults { | 217 struct CacheStorageCache::QueryCacheResult { |
| 218 QueryCacheResults(std::unique_ptr<ServiceWorkerFetchRequest> request, | 218 explicit QueryCacheResult(base::Time entry_time) : entry_time(entry_time) {} |
| 219 |
| 220 std::unique_ptr<ServiceWorkerFetchRequest> request; |
| 221 std::unique_ptr<ServiceWorkerResponse> response; |
| 222 std::unique_ptr<storage::BlobDataHandle> blob_handle; |
| 223 disk_cache::ScopedEntryPtr entry; |
| 224 base::Time entry_time; |
| 225 }; |
| 226 |
| 227 struct CacheStorageCache::QueryCacheContext { |
| 228 QueryCacheContext(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 219 const CacheStorageCacheQueryParams& options, | 229 const CacheStorageCacheQueryParams& options, |
| 220 const QueryCacheResultsCallback& callback) | 230 const QueryCacheCallback& callback) |
| 221 : request(std::move(request)), | 231 : request(std::move(request)), |
| 222 options(options), | 232 options(options), |
| 223 callback(callback), | 233 callback(callback), |
| 224 out_requests(new Requests), | 234 matches(base::MakeUnique<QueryCacheResults>()) {} |
| 225 out_responses(new Responses), | |
| 226 out_blob_data_handles(new BlobDataHandles) {} | |
| 227 | 235 |
| 236 // Input to QueryCache |
| 228 std::unique_ptr<ServiceWorkerFetchRequest> request; | 237 std::unique_ptr<ServiceWorkerFetchRequest> request; |
| 229 CacheStorageCacheQueryParams options; | 238 CacheStorageCacheQueryParams options; |
| 230 QueryCacheResultsCallback callback; | 239 QueryCacheCallback callback; |
| 231 QueryCacheType query_type; | 240 QueryCacheType query_type; |
| 232 size_t estimated_out_bytes = 0; | 241 size_t estimated_out_bytes = 0; |
| 233 | 242 |
| 234 std::unique_ptr<Requests> out_requests; | 243 // Iteration state |
| 235 std::unique_ptr<Responses> out_responses; | |
| 236 std::unique_ptr<BlobDataHandles> out_blob_data_handles; | |
| 237 std::vector<disk_cache::ScopedEntryPtr> out_entries; | |
| 238 | |
| 239 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator; | 244 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator; |
| 240 disk_cache::Entry* enumerated_entry = nullptr; | 245 disk_cache::Entry* enumerated_entry = nullptr; |
| 241 | 246 |
| 247 // Output of QueryCache |
| 248 std::unique_ptr<std::vector<QueryCacheResult>> matches; |
| 249 |
| 242 private: | 250 private: |
| 243 DISALLOW_COPY_AND_ASSIGN(QueryCacheResults); | 251 DISALLOW_COPY_AND_ASSIGN(QueryCacheContext); |
| 244 }; | 252 }; |
| 245 | 253 |
| 246 // static | 254 // static |
| 247 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( | 255 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( |
| 248 const GURL& origin, | 256 const GURL& origin, |
| 249 const std::string& cache_name, | 257 const std::string& cache_name, |
| 250 CacheStorage* cache_storage, | 258 CacheStorage* cache_storage, |
| 251 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 259 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 252 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 260 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 253 base::WeakPtr<storage::BlobStorageContext> blob_context) { | 261 base::WeakPtr<storage::BlobStorageContext> blob_context) { |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 DCHECK(!origin_.is_empty()); | 521 DCHECK(!origin_.is_empty()); |
| 514 DCHECK(quota_manager_proxy_.get()); | 522 DCHECK(quota_manager_proxy_.get()); |
| 515 | 523 |
| 516 quota_manager_proxy_->NotifyOriginInUse(origin_); | 524 quota_manager_proxy_->NotifyOriginInUse(origin_); |
| 517 } | 525 } |
| 518 | 526 |
| 519 void CacheStorageCache::QueryCache( | 527 void CacheStorageCache::QueryCache( |
| 520 std::unique_ptr<ServiceWorkerFetchRequest> request, | 528 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 521 const CacheStorageCacheQueryParams& options, | 529 const CacheStorageCacheQueryParams& options, |
| 522 QueryCacheType query_type, | 530 QueryCacheType query_type, |
| 523 const QueryCacheResultsCallback& callback) { | 531 const QueryCacheCallback& callback) { |
| 524 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 532 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
| 525 if (backend_state_ != BACKEND_OPEN) { | 533 if (backend_state_ != BACKEND_OPEN) { |
| 526 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 534 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 527 std::unique_ptr<QueryCacheResults>()); | 535 std::unique_ptr<QueryCacheResults>()); |
| 528 return; | 536 return; |
| 529 } | 537 } |
| 530 | 538 |
| 531 if (!options.ignore_method && request && !request->method.empty() && | 539 if (!options.ignore_method && request && !request->method.empty() && |
| 532 request->method != "GET") { | 540 request->method != "GET") { |
| 533 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>( | 541 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>()); |
| 534 std::move(request), options, callback)); | |
| 535 return; | 542 return; |
| 536 } | 543 } |
| 537 | 544 |
| 538 ServiceWorkerFetchRequest* request_ptr = request.get(); | 545 ServiceWorkerFetchRequest* request_ptr = request.get(); |
| 539 std::unique_ptr<QueryCacheResults> query_cache_results( | 546 std::unique_ptr<QueryCacheContext> query_cache_context( |
| 540 new QueryCacheResults(std::move(request), options, callback)); | 547 new QueryCacheContext(std::move(request), options, callback)); |
| 541 query_cache_results->query_type = query_type; | 548 query_cache_context->query_type = query_type; |
| 542 | 549 |
| 543 if (query_cache_results->request && | 550 if (query_cache_context->request && |
| 544 !query_cache_results->request->url.is_empty() && !options.ignore_search) { | 551 !query_cache_context->request->url.is_empty() && !options.ignore_search) { |
| 545 // There is no need to scan the entire backend, just open the exact | 552 // There is no need to scan the entire backend, just open the exact |
| 546 // URL. | 553 // URL. |
| 547 disk_cache::Entry** entry_ptr = &query_cache_results->enumerated_entry; | 554 disk_cache::Entry** entry_ptr = &query_cache_context->enumerated_entry; |
| 548 net::CompletionCallback open_entry_callback = | 555 net::CompletionCallback open_entry_callback = |
| 549 base::Bind(&CacheStorageCache::QueryCacheDidOpenFastPath, | 556 base::Bind(&CacheStorageCache::QueryCacheDidOpenFastPath, |
| 550 weak_ptr_factory_.GetWeakPtr(), | 557 weak_ptr_factory_.GetWeakPtr(), |
| 551 base::Passed(std::move(query_cache_results))); | 558 base::Passed(std::move(query_cache_context))); |
| 552 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, | 559 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, |
| 553 open_entry_callback); | 560 open_entry_callback); |
| 554 if (rv != net::ERR_IO_PENDING) | 561 if (rv != net::ERR_IO_PENDING) |
| 555 open_entry_callback.Run(rv); | 562 open_entry_callback.Run(rv); |
| 556 return; | 563 return; |
| 557 } | 564 } |
| 558 | 565 |
| 559 query_cache_results->backend_iterator = backend_->CreateIterator(); | 566 query_cache_context->backend_iterator = backend_->CreateIterator(); |
| 560 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 567 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 561 } | 568 } |
| 562 | 569 |
| 563 void CacheStorageCache::QueryCacheDidOpenFastPath( | 570 void CacheStorageCache::QueryCacheDidOpenFastPath( |
| 564 std::unique_ptr<QueryCacheResults> query_cache_results, | 571 std::unique_ptr<QueryCacheContext> query_cache_context, |
| 565 int rv) { | 572 int rv) { |
| 566 if (rv != net::OK) { | 573 if (rv != net::OK) { |
| 567 QueryCacheResults* results = query_cache_results.get(); | 574 QueryCacheContext* results = query_cache_context.get(); |
| 568 results->callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results)); | 575 results->callback.Run(CACHE_STORAGE_OK, |
| 576 std::move(query_cache_context->matches)); |
| 569 return; | 577 return; |
| 570 } | 578 } |
| 571 QueryCacheFilterEntry(std::move(query_cache_results), rv); | 579 QueryCacheFilterEntry(std::move(query_cache_context), rv); |
| 572 } | 580 } |
| 573 | 581 |
| 574 void CacheStorageCache::QueryCacheOpenNextEntry( | 582 void CacheStorageCache::QueryCacheOpenNextEntry( |
| 575 std::unique_ptr<QueryCacheResults> query_cache_results) { | 583 std::unique_ptr<QueryCacheContext> query_cache_context) { |
| 576 DCHECK_EQ(nullptr, query_cache_results->enumerated_entry); | 584 DCHECK_EQ(nullptr, query_cache_context->enumerated_entry); |
| 577 | 585 |
| 578 if (!query_cache_results->backend_iterator) { | 586 if (!query_cache_context->backend_iterator) { |
| 579 // Iteration is complete. | 587 // Iteration is complete. |
| 580 QueryCacheResultsCallback callback = query_cache_results->callback; | 588 std::sort(query_cache_context->matches->begin(), |
| 581 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results)); | 589 query_cache_context->matches->end(), QueryCacheResultCompare); |
| 590 |
| 591 query_cache_context->callback.Run(CACHE_STORAGE_OK, |
| 592 std::move(query_cache_context->matches)); |
| 582 return; | 593 return; |
| 583 } | 594 } |
| 584 | 595 |
| 585 disk_cache::Backend::Iterator& iterator = | 596 disk_cache::Backend::Iterator& iterator = |
| 586 *query_cache_results->backend_iterator; | 597 *query_cache_context->backend_iterator; |
| 587 disk_cache::Entry** enumerated_entry = &query_cache_results->enumerated_entry; | 598 disk_cache::Entry** enumerated_entry = &query_cache_context->enumerated_entry; |
| 588 net::CompletionCallback open_entry_callback = base::Bind( | 599 net::CompletionCallback open_entry_callback = base::Bind( |
| 589 &CacheStorageCache::QueryCacheFilterEntry, weak_ptr_factory_.GetWeakPtr(), | 600 &CacheStorageCache::QueryCacheFilterEntry, weak_ptr_factory_.GetWeakPtr(), |
| 590 base::Passed(std::move(query_cache_results))); | 601 base::Passed(std::move(query_cache_context))); |
| 591 | 602 |
| 592 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | 603 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); |
| 593 | 604 |
| 594 if (rv != net::ERR_IO_PENDING) | 605 if (rv != net::ERR_IO_PENDING) |
| 595 open_entry_callback.Run(rv); | 606 open_entry_callback.Run(rv); |
| 596 } | 607 } |
| 597 | 608 |
| 598 void CacheStorageCache::QueryCacheFilterEntry( | 609 void CacheStorageCache::QueryCacheFilterEntry( |
| 599 std::unique_ptr<QueryCacheResults> query_cache_results, | 610 std::unique_ptr<QueryCacheContext> query_cache_context, |
| 600 int rv) { | 611 int rv) { |
| 601 if (rv == net::ERR_FAILED) { | 612 if (rv == net::ERR_FAILED) { |
| 602 // This is the indicator that iteration is complete. | 613 // This is the indicator that iteration is complete. |
| 603 query_cache_results->backend_iterator.reset(); | 614 query_cache_context->backend_iterator.reset(); |
| 604 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 615 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 605 return; | 616 return; |
| 606 } | 617 } |
| 607 | 618 |
| 608 if (rv < 0) { | 619 if (rv < 0) { |
| 609 QueryCacheResultsCallback callback = query_cache_results->callback; | 620 QueryCacheCallback callback = query_cache_context->callback; |
| 610 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::move(query_cache_results)); | 621 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 622 std::move(query_cache_context->matches)); |
| 611 return; | 623 return; |
| 612 } | 624 } |
| 613 | 625 |
| 614 disk_cache::ScopedEntryPtr entry(query_cache_results->enumerated_entry); | 626 disk_cache::ScopedEntryPtr entry(query_cache_context->enumerated_entry); |
| 615 query_cache_results->enumerated_entry = nullptr; | 627 query_cache_context->enumerated_entry = nullptr; |
| 616 | 628 |
| 617 if (backend_state_ != BACKEND_OPEN) { | 629 if (backend_state_ != BACKEND_OPEN) { |
| 618 QueryCacheResultsCallback callback = query_cache_results->callback; | 630 QueryCacheCallback callback = query_cache_context->callback; |
| 619 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, std::move(query_cache_results)); | 631 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 632 std::move(query_cache_context->matches)); |
| 620 return; | 633 return; |
| 621 } | 634 } |
| 622 | 635 |
| 623 if (query_cache_results->request && | 636 if (query_cache_context->request && |
| 624 !query_cache_results->request->url.is_empty()) { | 637 !query_cache_context->request->url.is_empty()) { |
| 625 GURL requestURL = query_cache_results->request->url; | 638 GURL requestURL = query_cache_context->request->url; |
| 626 GURL cachedURL = GURL(entry->GetKey()); | 639 GURL cachedURL = GURL(entry->GetKey()); |
| 627 | 640 |
| 628 if (query_cache_results->options.ignore_search) { | 641 if (query_cache_context->options.ignore_search) { |
| 629 requestURL = RemoveQueryParam(requestURL); | 642 requestURL = RemoveQueryParam(requestURL); |
| 630 cachedURL = RemoveQueryParam(cachedURL); | 643 cachedURL = RemoveQueryParam(cachedURL); |
| 631 } | 644 } |
| 632 | 645 |
| 633 if (cachedURL != requestURL) { | 646 if (cachedURL != requestURL) { |
| 634 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 647 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 635 return; | 648 return; |
| 636 } | 649 } |
| 637 } | 650 } |
| 638 | 651 |
| 639 disk_cache::Entry* entry_ptr = entry.get(); | 652 disk_cache::Entry* entry_ptr = entry.get(); |
| 640 ReadMetadata(entry_ptr, | 653 ReadMetadata(entry_ptr, |
| 641 base::Bind(&CacheStorageCache::QueryCacheDidReadMetadata, | 654 base::Bind(&CacheStorageCache::QueryCacheDidReadMetadata, |
| 642 weak_ptr_factory_.GetWeakPtr(), | 655 weak_ptr_factory_.GetWeakPtr(), |
| 643 base::Passed(std::move(query_cache_results)), | 656 base::Passed(std::move(query_cache_context)), |
| 644 base::Passed(std::move(entry)))); | 657 base::Passed(std::move(entry)))); |
| 645 } | 658 } |
| 646 | 659 |
| 647 void CacheStorageCache::QueryCacheDidReadMetadata( | 660 void CacheStorageCache::QueryCacheDidReadMetadata( |
| 648 std::unique_ptr<QueryCacheResults> query_cache_results, | 661 std::unique_ptr<QueryCacheContext> query_cache_context, |
| 649 disk_cache::ScopedEntryPtr entry, | 662 disk_cache::ScopedEntryPtr entry, |
| 650 std::unique_ptr<CacheMetadata> metadata) { | 663 std::unique_ptr<CacheMetadata> metadata) { |
| 651 if (!metadata) { | 664 if (!metadata) { |
| 652 entry->Doom(); | 665 entry->Doom(); |
| 653 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 666 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 654 return; | 667 return; |
| 655 } | 668 } |
| 656 | 669 |
| 657 ServiceWorkerFetchRequest request; | 670 // If the entry was created before we started adding entry times, then |
| 658 PopulateRequestFromMetadata(*metadata, GURL(entry->GetKey()), &request); | 671 // default to using the Response object's time for sorting purposes. |
| 672 int64_t entry_time = metadata->has_entry_time() |
| 673 ? metadata->entry_time() |
| 674 : metadata->response().response_time(); |
| 659 | 675 |
| 660 ServiceWorkerResponse response; | 676 query_cache_context->matches->push_back( |
| 661 PopulateResponseMetadata(*metadata, &response); | 677 QueryCacheResult(base::Time::FromInternalValue(entry_time))); |
| 678 QueryCacheResult* match = &query_cache_context->matches->back(); |
| 679 match->request = base::MakeUnique<ServiceWorkerFetchRequest>(); |
| 680 match->response = base::MakeUnique<ServiceWorkerResponse>(); |
| 681 PopulateRequestFromMetadata(*metadata, GURL(entry->GetKey()), |
| 682 match->request.get()); |
| 683 PopulateResponseMetadata(*metadata, match->response.get()); |
| 662 | 684 |
| 663 if (query_cache_results->request && | 685 if (query_cache_context->request && |
| 664 !query_cache_results->options.ignore_vary && | 686 !query_cache_context->options.ignore_vary && |
| 665 !VaryMatches(query_cache_results->request->headers, request.headers, | 687 !VaryMatches(query_cache_context->request->headers, |
| 666 response.headers)) { | 688 match->request->headers, match->response->headers)) { |
| 667 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 689 query_cache_context->matches->pop_back(); |
| 690 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 668 return; | 691 return; |
| 669 } | 692 } |
| 670 | 693 |
| 671 if (query_cache_results->query_type == QueryCacheType::CACHE_ENTRIES) { | 694 if (query_cache_context->query_type == QueryCacheType::CACHE_ENTRIES) { |
| 672 query_cache_results->out_entries.push_back(std::move(entry)); | 695 match->request.reset(); |
| 673 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 696 match->response.reset(); |
| 697 match->entry = std::move(entry); |
| 698 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 674 return; | 699 return; |
| 675 } | 700 } |
| 676 | 701 |
| 677 query_cache_results->estimated_out_bytes += request.EstimatedStructSize(); | 702 query_cache_context->estimated_out_bytes += |
| 678 if (query_cache_results->estimated_out_bytes > max_query_size_bytes_) { | 703 match->request->EstimatedStructSize(); |
| 679 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, | 704 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { |
| 705 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, |
| 680 std::unique_ptr<QueryCacheResults>()); | 706 std::unique_ptr<QueryCacheResults>()); |
| 681 return; | 707 return; |
| 682 } | 708 } |
| 683 | 709 |
| 684 query_cache_results->out_requests->push_back(request); | 710 if (query_cache_context->query_type == QueryCacheType::REQUESTS) { |
| 685 if (query_cache_results->query_type == QueryCacheType::REQUESTS) { | 711 match->response.reset(); |
| 686 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 712 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 687 return; | 713 return; |
| 688 } | 714 } |
| 689 | 715 |
| 690 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES, | 716 DCHECK_EQ(QueryCacheType::REQUESTS_AND_RESPONSES, |
| 691 query_cache_results->query_type); | 717 query_cache_context->query_type); |
| 692 | 718 |
| 693 query_cache_results->estimated_out_bytes += response.EstimatedStructSize(); | 719 query_cache_context->estimated_out_bytes += |
| 694 if (query_cache_results->estimated_out_bytes > max_query_size_bytes_) { | 720 match->response->EstimatedStructSize(); |
| 695 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, | 721 if (query_cache_context->estimated_out_bytes > max_query_size_bytes_) { |
| 722 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_QUERY_TOO_LARGE, |
| 696 std::unique_ptr<QueryCacheResults>()); | 723 std::unique_ptr<QueryCacheResults>()); |
| 697 return; | 724 return; |
| 698 } | 725 } |
| 699 | 726 |
| 700 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { | 727 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { |
| 701 query_cache_results->out_responses->push_back(response); | 728 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 702 QueryCacheOpenNextEntry(std::move(query_cache_results)); | |
| 703 return; | 729 return; |
| 704 } | 730 } |
| 705 | 731 |
| 706 if (!blob_storage_context_) { | 732 if (!blob_storage_context_) { |
| 707 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 733 query_cache_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 708 std::unique_ptr<QueryCacheResults>()); | 734 base::MakeUnique<QueryCacheResults>()); |
| 709 return; | 735 return; |
| 710 } | 736 } |
| 711 | 737 |
| 712 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = | 738 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
| 713 PopulateResponseBody(std::move(entry), &response); | 739 PopulateResponseBody(std::move(entry), match->response.get()); |
| 740 match->blob_handle = std::move(blob_data_handle); |
| 714 | 741 |
| 715 query_cache_results->out_responses->push_back(response); | 742 QueryCacheOpenNextEntry(std::move(query_cache_context)); |
| 716 query_cache_results->out_blob_data_handles->push_back(*blob_data_handle); | 743 } |
| 717 QueryCacheOpenNextEntry(std::move(query_cache_results)); | 744 |
| 745 // static |
| 746 bool CacheStorageCache::QueryCacheResultCompare(const QueryCacheResult& lhs, |
| 747 const QueryCacheResult& rhs) { |
| 748 return lhs.entry_time < rhs.entry_time; |
| 718 } | 749 } |
| 719 | 750 |
| 720 void CacheStorageCache::MatchImpl( | 751 void CacheStorageCache::MatchImpl( |
| 721 std::unique_ptr<ServiceWorkerFetchRequest> request, | 752 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 722 const CacheStorageCacheQueryParams& match_params, | 753 const CacheStorageCacheQueryParams& match_params, |
| 723 const ResponseCallback& callback) { | 754 const ResponseCallback& callback) { |
| 724 MatchAllImpl(std::move(request), match_params, | 755 MatchAllImpl(std::move(request), match_params, |
| 725 base::Bind(&CacheStorageCache::MatchDidMatchAll, | 756 base::Bind(&CacheStorageCache::MatchDidMatchAll, |
| 726 weak_ptr_factory_.GetWeakPtr(), callback)); | 757 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 727 } | 758 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 740 if (match_all_responses->empty()) { | 771 if (match_all_responses->empty()) { |
| 741 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 772 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 742 std::unique_ptr<ServiceWorkerResponse>(), | 773 std::unique_ptr<ServiceWorkerResponse>(), |
| 743 std::unique_ptr<storage::BlobDataHandle>()); | 774 std::unique_ptr<storage::BlobDataHandle>()); |
| 744 return; | 775 return; |
| 745 } | 776 } |
| 746 | 777 |
| 747 std::unique_ptr<ServiceWorkerResponse> response = | 778 std::unique_ptr<ServiceWorkerResponse> response = |
| 748 base::MakeUnique<ServiceWorkerResponse>(match_all_responses->at(0)); | 779 base::MakeUnique<ServiceWorkerResponse>(match_all_responses->at(0)); |
| 749 | 780 |
| 750 std::unique_ptr<storage::BlobDataHandle> data_handle; | 781 callback.Run(CACHE_STORAGE_OK, std::move(response), |
| 751 if (response->blob_size > 0) { | 782 std::move(match_all_handles->at(0))); |
| 752 // NOTE: This assumes that MatchAll returns the handles in the same order | |
| 753 // as the responses. | |
| 754 data_handle = | |
| 755 base::MakeUnique<storage::BlobDataHandle>(match_all_handles->at(0)); | |
| 756 } | |
| 757 | |
| 758 callback.Run(CACHE_STORAGE_OK, std::move(response), std::move(data_handle)); | |
| 759 } | 783 } |
| 760 | 784 |
| 761 void CacheStorageCache::MatchAllImpl( | 785 void CacheStorageCache::MatchAllImpl( |
| 762 std::unique_ptr<ServiceWorkerFetchRequest> request, | 786 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 763 const CacheStorageCacheQueryParams& options, | 787 const CacheStorageCacheQueryParams& options, |
| 764 const ResponsesCallback& callback) { | 788 const ResponsesCallback& callback) { |
| 765 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 789 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
| 766 if (backend_state_ != BACKEND_OPEN) { | 790 if (backend_state_ != BACKEND_OPEN) { |
| 767 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), | 791 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), |
| 768 std::unique_ptr<BlobDataHandles>()); | 792 std::unique_ptr<BlobDataHandles>()); |
| 769 return; | 793 return; |
| 770 } | 794 } |
| 771 | 795 |
| 772 QueryCache(std::move(request), options, | 796 QueryCache(std::move(request), options, |
| 773 QueryCacheType::REQUESTS_AND_RESPONSES, | 797 QueryCacheType::REQUESTS_AND_RESPONSES, |
| 774 base::Bind(&CacheStorageCache::MatchAllDidQueryCache, | 798 base::Bind(&CacheStorageCache::MatchAllDidQueryCache, |
| 775 weak_ptr_factory_.GetWeakPtr(), callback)); | 799 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 776 } | 800 } |
| 777 | 801 |
| 778 void CacheStorageCache::MatchAllDidQueryCache( | 802 void CacheStorageCache::MatchAllDidQueryCache( |
| 779 const ResponsesCallback& callback, | 803 const ResponsesCallback& callback, |
| 780 CacheStorageError error, | 804 CacheStorageError error, |
| 781 std::unique_ptr<QueryCacheResults> query_cache_results) { | 805 std::unique_ptr<QueryCacheResults> query_cache_results) { |
| 782 if (error != CACHE_STORAGE_OK) { | 806 if (error != CACHE_STORAGE_OK) { |
| 783 callback.Run(error, std::unique_ptr<Responses>(), | 807 callback.Run(error, std::unique_ptr<Responses>(), |
| 784 std::unique_ptr<BlobDataHandles>()); | 808 std::unique_ptr<BlobDataHandles>()); |
| 785 return; | 809 return; |
| 786 } | 810 } |
| 787 | 811 |
| 788 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results->out_responses), | 812 std::unique_ptr<Responses> out_responses = base::MakeUnique<Responses>(); |
| 789 std::move(query_cache_results->out_blob_data_handles)); | 813 std::unique_ptr<BlobDataHandles> out_handles = |
| 814 base::MakeUnique<BlobDataHandles>(); |
| 815 out_responses->reserve(query_cache_results->size()); |
| 816 out_handles->reserve(query_cache_results->size()); |
| 817 |
| 818 for (auto& result : *query_cache_results) { |
| 819 out_responses->push_back(*result.response); |
| 820 out_handles->push_back(std::move(result.blob_handle)); |
| 821 } |
| 822 |
| 823 callback.Run(CACHE_STORAGE_OK, std::move(out_responses), |
| 824 std::move(out_handles)); |
| 790 } | 825 } |
| 791 | 826 |
| 792 void CacheStorageCache::WriteSideDataDidGetQuota( | 827 void CacheStorageCache::WriteSideDataDidGetQuota( |
| 793 const ErrorCallback& callback, | 828 const ErrorCallback& callback, |
| 794 const GURL& url, | 829 const GURL& url, |
| 795 base::Time expected_response_time, | 830 base::Time expected_response_time, |
| 796 scoped_refptr<net::IOBuffer> buffer, | 831 scoped_refptr<net::IOBuffer> buffer, |
| 797 int buf_len, | 832 int buf_len, |
| 798 storage::QuotaStatusCode status_code, | 833 storage::QuotaStatusCode status_code, |
| 799 int64_t usage, | 834 int64_t usage, |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1004 std::unique_ptr<PutContext> put_context, | 1039 std::unique_ptr<PutContext> put_context, |
| 1005 int rv) { | 1040 int rv) { |
| 1006 put_context->cache_entry.reset(*entry_ptr); | 1041 put_context->cache_entry.reset(*entry_ptr); |
| 1007 | 1042 |
| 1008 if (rv != net::OK) { | 1043 if (rv != net::OK) { |
| 1009 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); | 1044 put_context->callback.Run(CACHE_STORAGE_ERROR_EXISTS); |
| 1010 return; | 1045 return; |
| 1011 } | 1046 } |
| 1012 | 1047 |
| 1013 CacheMetadata metadata; | 1048 CacheMetadata metadata; |
| 1049 metadata.set_entry_time(base::Time::Now().ToInternalValue()); |
| 1014 CacheRequest* request_metadata = metadata.mutable_request(); | 1050 CacheRequest* request_metadata = metadata.mutable_request(); |
| 1015 request_metadata->set_method(put_context->request->method); | 1051 request_metadata->set_method(put_context->request->method); |
| 1016 for (ServiceWorkerHeaderMap::const_iterator it = | 1052 for (ServiceWorkerHeaderMap::const_iterator it = |
| 1017 put_context->request->headers.begin(); | 1053 put_context->request->headers.begin(); |
| 1018 it != put_context->request->headers.end(); ++it) { | 1054 it != put_context->request->headers.end(); ++it) { |
| 1019 DCHECK_EQ(std::string::npos, it->first.find('\0')); | 1055 DCHECK_EQ(std::string::npos, it->first.find('\0')); |
| 1020 DCHECK_EQ(std::string::npos, it->second.find('\0')); | 1056 DCHECK_EQ(std::string::npos, it->second.find('\0')); |
| 1021 CacheHeaderMap* header_map = request_metadata->add_headers(); | 1057 CacheHeaderMap* header_map = request_metadata->add_headers(); |
| 1022 header_map->set_name(it->first); | 1058 header_map->set_name(it->first); |
| 1023 header_map->set_value(it->second); | 1059 header_map->set_value(it->second); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1188 | 1224 |
| 1189 void CacheStorageCache::DeleteDidQueryCache( | 1225 void CacheStorageCache::DeleteDidQueryCache( |
| 1190 const ErrorCallback& callback, | 1226 const ErrorCallback& callback, |
| 1191 CacheStorageError error, | 1227 CacheStorageError error, |
| 1192 std::unique_ptr<QueryCacheResults> query_cache_results) { | 1228 std::unique_ptr<QueryCacheResults> query_cache_results) { |
| 1193 if (error != CACHE_STORAGE_OK) { | 1229 if (error != CACHE_STORAGE_OK) { |
| 1194 callback.Run(error); | 1230 callback.Run(error); |
| 1195 return; | 1231 return; |
| 1196 } | 1232 } |
| 1197 | 1233 |
| 1198 if (query_cache_results->out_entries.empty()) { | 1234 if (query_cache_results->empty()) { |
| 1199 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); | 1235 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); |
| 1200 return; | 1236 return; |
| 1201 } | 1237 } |
| 1202 | 1238 |
| 1203 for (auto& entry : query_cache_results->out_entries) { | 1239 for (auto& result : *query_cache_results) { |
| 1240 disk_cache::ScopedEntryPtr entry = std::move(result.entry); |
| 1204 entry->Doom(); | 1241 entry->Doom(); |
| 1205 entry.reset(); | |
| 1206 } | 1242 } |
| 1207 | 1243 |
| 1208 UpdateCacheSize(); | 1244 UpdateCacheSize(); |
| 1209 callback.Run(CACHE_STORAGE_OK); | 1245 callback.Run(CACHE_STORAGE_OK); |
| 1210 } | 1246 } |
| 1211 | 1247 |
| 1212 void CacheStorageCache::KeysImpl( | 1248 void CacheStorageCache::KeysImpl( |
| 1213 std::unique_ptr<ServiceWorkerFetchRequest> request, | 1249 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 1214 const CacheStorageCacheQueryParams& options, | 1250 const CacheStorageCacheQueryParams& options, |
| 1215 const RequestsCallback& callback) { | 1251 const RequestsCallback& callback) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1226 | 1262 |
| 1227 void CacheStorageCache::KeysDidQueryCache( | 1263 void CacheStorageCache::KeysDidQueryCache( |
| 1228 const RequestsCallback& callback, | 1264 const RequestsCallback& callback, |
| 1229 CacheStorageError error, | 1265 CacheStorageError error, |
| 1230 std::unique_ptr<QueryCacheResults> query_cache_results) { | 1266 std::unique_ptr<QueryCacheResults> query_cache_results) { |
| 1231 if (error != CACHE_STORAGE_OK) { | 1267 if (error != CACHE_STORAGE_OK) { |
| 1232 callback.Run(error, std::unique_ptr<Requests>()); | 1268 callback.Run(error, std::unique_ptr<Requests>()); |
| 1233 return; | 1269 return; |
| 1234 } | 1270 } |
| 1235 | 1271 |
| 1236 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results->out_requests)); | 1272 std::unique_ptr<Requests> out_requests = base::MakeUnique<Requests>(); |
| 1273 out_requests->reserve(query_cache_results->size()); |
| 1274 for (const auto& result : *query_cache_results) |
| 1275 out_requests->push_back(*result.request); |
| 1276 |
| 1277 callback.Run(CACHE_STORAGE_OK, std::move(out_requests)); |
| 1237 } | 1278 } |
| 1238 | 1279 |
| 1239 void CacheStorageCache::CloseImpl(const base::Closure& callback) { | 1280 void CacheStorageCache::CloseImpl(const base::Closure& callback) { |
| 1240 DCHECK_NE(BACKEND_CLOSED, backend_state_); | 1281 DCHECK_NE(BACKEND_CLOSED, backend_state_); |
| 1241 | 1282 |
| 1242 backend_state_ = BACKEND_CLOSED; | 1283 backend_state_ = BACKEND_CLOSED; |
| 1243 backend_.reset(); | 1284 backend_.reset(); |
| 1244 callback.Run(); | 1285 callback.Run(); |
| 1245 } | 1286 } |
| 1246 | 1287 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1398 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); | 1439 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); |
| 1399 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1440 return blob_storage_context_->AddFinishedBlob(&blob_data); |
| 1400 } | 1441 } |
| 1401 | 1442 |
| 1402 std::unique_ptr<CacheStorageCacheHandle> | 1443 std::unique_ptr<CacheStorageCacheHandle> |
| 1403 CacheStorageCache::CreateCacheHandle() { | 1444 CacheStorageCache::CreateCacheHandle() { |
| 1404 return cache_storage_->CreateCacheHandle(this); | 1445 return cache_storage_->CreateCacheHandle(this); |
| 1405 } | 1446 } |
| 1406 | 1447 |
| 1407 } // namespace content | 1448 } // namespace content |
| OLD | NEW |