Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: content/browser/cache_storage/cache_storage_cache.cc

Issue 2242883002: [CacheStorage] Use QueryCache everywhere (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 : request(std::move(request)), 241 : request(std::move(request)),
242 options(options), 242 options(options),
243 callback(callback), 243 callback(callback),
244 out_requests(new Requests), 244 out_requests(new Requests),
245 out_responses(new Responses), 245 out_responses(new Responses),
246 out_blob_data_handles(new BlobDataHandles) {} 246 out_blob_data_handles(new BlobDataHandles) {}
247 247
248 std::unique_ptr<ServiceWorkerFetchRequest> request; 248 std::unique_ptr<ServiceWorkerFetchRequest> request;
249 CacheStorageCacheQueryParams options; 249 CacheStorageCacheQueryParams options;
250 QueryCacheResultsCallback callback; 250 QueryCacheResultsCallback callback;
251 QueryCacheType query_type;
251 252
252 std::unique_ptr<Requests> out_requests; 253 std::unique_ptr<Requests> out_requests;
253 std::unique_ptr<Responses> out_responses; 254 std::unique_ptr<Responses> out_responses;
254 std::unique_ptr<BlobDataHandles> out_blob_data_handles; 255 std::unique_ptr<BlobDataHandles> out_blob_data_handles;
256 std::vector<disk_cache::ScopedEntryPtr> out_entries;
255 257
256 std::unique_ptr<OpenAllEntriesContext> entries_context; 258 std::unique_ptr<OpenAllEntriesContext> entries_context;
257 259
258 private: 260 private:
259 DISALLOW_COPY_AND_ASSIGN(QueryCacheResults); 261 DISALLOW_COPY_AND_ASSIGN(QueryCacheResults);
260 }; 262 };
261 263
262 // static 264 // static
263 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 265 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
264 const GURL& origin, 266 const GURL& origin,
(...skipping 26 matching lines...) Expand all
291 cache->InitBackend(); 293 cache->InitBackend();
292 return base::WrapUnique(cache); 294 return base::WrapUnique(cache);
293 } 295 }
294 296
295 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() { 297 base::WeakPtr<CacheStorageCache> CacheStorageCache::AsWeakPtr() {
296 return weak_ptr_factory_.GetWeakPtr(); 298 return weak_ptr_factory_.GetWeakPtr();
297 } 299 }
298 300
299 void CacheStorageCache::Match( 301 void CacheStorageCache::Match(
300 std::unique_ptr<ServiceWorkerFetchRequest> request, 302 std::unique_ptr<ServiceWorkerFetchRequest> request,
303 const CacheStorageCacheQueryParams& match_params,
301 const ResponseCallback& callback) { 304 const ResponseCallback& callback) {
302 if (backend_state_ == BACKEND_CLOSED) { 305 if (backend_state_ == BACKEND_CLOSED) {
303 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 306 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
304 std::unique_ptr<ServiceWorkerResponse>(), 307 std::unique_ptr<ServiceWorkerResponse>(),
305 std::unique_ptr<storage::BlobDataHandle>()); 308 std::unique_ptr<storage::BlobDataHandle>());
306 return; 309 return;
307 } 310 }
308 311
309 scheduler_->ScheduleOperation( 312 scheduler_->ScheduleOperation(
310 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), 313 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(),
311 base::Passed(std::move(request)), 314 base::Passed(std::move(request)), match_params,
312 scheduler_->WrapCallbackToRunNext(callback))); 315 scheduler_->WrapCallbackToRunNext(callback)));
313 } 316 }
314 317
315 void CacheStorageCache::MatchAll( 318 void CacheStorageCache::MatchAll(
316 std::unique_ptr<ServiceWorkerFetchRequest> request, 319 std::unique_ptr<ServiceWorkerFetchRequest> request,
317 const CacheStorageCacheQueryParams& match_params, 320 const CacheStorageCacheQueryParams& match_params,
318 const ResponsesCallback& callback) { 321 const ResponsesCallback& callback) {
319 if (backend_state_ == BACKEND_CLOSED) { 322 if (backend_state_ == BACKEND_CLOSED) {
320 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 323 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
321 std::unique_ptr<BlobDataHandles>()); 324 std::unique_ptr<BlobDataHandles>());
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 584
582 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); 585 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback);
583 586
584 if (rv != net::ERR_IO_PENDING) 587 if (rv != net::ERR_IO_PENDING)
585 open_entry_callback.Run(rv); 588 open_entry_callback.Run(rv);
586 } 589 }
587 590
588 void CacheStorageCache::QueryCache( 591 void CacheStorageCache::QueryCache(
589 std::unique_ptr<ServiceWorkerFetchRequest> request, 592 std::unique_ptr<ServiceWorkerFetchRequest> request,
590 const CacheStorageCacheQueryParams& options, 593 const CacheStorageCacheQueryParams& options,
594 QueryCacheType query_type,
591 const QueryCacheResultsCallback& callback) { 595 const QueryCacheResultsCallback& callback) {
592 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 596 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
593 if (backend_state_ != BACKEND_OPEN) { 597 if (backend_state_ != BACKEND_OPEN) {
594 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 598 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
595 std::unique_ptr<QueryCacheResults>()); 599 std::unique_ptr<QueryCacheResults>());
596 return; 600 return;
597 } 601 }
598 602
599 if (!options.ignore_method && request && !request->method.empty() && 603 if (!options.ignore_method && request && !request->method.empty() &&
600 request->method != "GET") { 604 request->method != "GET") {
601 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>( 605 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<QueryCacheResults>(
602 std::move(request), options, callback)); 606 std::move(request), options, callback));
603 return; 607 return;
604 } 608 }
605 609
610 ServiceWorkerFetchRequest* request_ptr = request.get();
611
606 std::unique_ptr<QueryCacheResults> query_cache_results( 612 std::unique_ptr<QueryCacheResults> query_cache_results(
607 new QueryCacheResults(std::move(request), options, callback)); 613 new QueryCacheResults(std::move(request), options, callback));
614 query_cache_results->query_type = query_type;
615
616 if (query_cache_results->request &&
617 !query_cache_results->request->url.is_empty() && !options.ignore_search) {
618 // There is no need to scan the entire backend, just search for the exact
619 // URL.
620 std::unique_ptr<disk_cache::Entry*> entry =
621 base::MakeUnique<disk_cache::Entry*>();
622 disk_cache::Entry** entry_ptr = entry.get();
623
624 net::CompletionCallback open_entry_callback =
625 base::Bind(&CacheStorageCache::QueryCacheDidOpenEntry,
626 weak_ptr_factory_.GetWeakPtr(),
627 base::Passed(std::move(query_cache_results)),
628 base::Passed(std::move(entry)));
629 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr,
630 open_entry_callback);
631 if (rv != net::ERR_IO_PENDING)
632 open_entry_callback.Run(rv);
633 return;
634 }
635
608 OpenAllEntries(base::Bind(&CacheStorageCache::QueryCacheDidOpenAllEntries, 636 OpenAllEntries(base::Bind(&CacheStorageCache::QueryCacheDidOpenAllEntries,
609 weak_ptr_factory_.GetWeakPtr(), 637 weak_ptr_factory_.GetWeakPtr(),
610 base::Passed(std::move(query_cache_results)))); 638 base::Passed(std::move(query_cache_results))));
611 } 639 }
612 640
641 void CacheStorageCache::QueryCacheDidOpenEntry(
642 std::unique_ptr<QueryCacheResults> query_cache_results,
643 std::unique_ptr<disk_cache::Entry*> entry,
644 int rv) {
645 if (rv != net::OK) {
646 QueryCacheResults* results = query_cache_results.get();
647 results->callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results));
648 return;
649 }
650
651 std::unique_ptr<OpenAllEntriesContext> entries_context =
652 base::MakeUnique<OpenAllEntriesContext>();
653 entries_context->entries.push_back(*entry.get());
654 QueryCacheDidOpenAllEntries(std::move(query_cache_results),
655 std::move(entries_context), CACHE_STORAGE_OK);
656 }
657
613 void CacheStorageCache::QueryCacheDidOpenAllEntries( 658 void CacheStorageCache::QueryCacheDidOpenAllEntries(
614 std::unique_ptr<QueryCacheResults> query_cache_results, 659 std::unique_ptr<QueryCacheResults> query_cache_results,
615 std::unique_ptr<OpenAllEntriesContext> entries_context, 660 std::unique_ptr<OpenAllEntriesContext> entries_context,
616 CacheStorageError error) { 661 CacheStorageError error) {
617 if (error != CACHE_STORAGE_OK) { 662 if (error != CACHE_STORAGE_OK) {
618 query_cache_results->callback.Run(error, 663 query_cache_results->callback.Run(error,
619 std::unique_ptr<QueryCacheResults>()); 664 std::unique_ptr<QueryCacheResults>());
620 return; 665 return;
621 } 666 }
622 667
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 *iter = nullptr; 711 *iter = nullptr;
667 712
668 if (!metadata) { 713 if (!metadata) {
669 entry->Doom(); 714 entry->Doom();
670 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1); 715 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
671 return; 716 return;
672 } 717 }
673 718
674 ServiceWorkerFetchRequest request; 719 ServiceWorkerFetchRequest request;
675 PopulateRequestFromMetadata(*metadata, GURL(entry->GetKey()), &request); 720 PopulateRequestFromMetadata(*metadata, GURL(entry->GetKey()), &request);
676 query_cache_results->out_requests->push_back(request);
677 721
678 ServiceWorkerResponse response; 722 ServiceWorkerResponse response;
679 PopulateResponseMetadata(*metadata, &response); 723 PopulateResponseMetadata(*metadata, &response);
680 724
725 if (query_cache_results->request &&
726 !query_cache_results->options.ignore_vary &&
727 !VaryMatches(query_cache_results->request->headers, request.headers,
728 response.headers)) {
729 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
730 return;
731 }
732
733 if (query_cache_results->query_type == QueryCacheType::CACHE_ENTRIES) {
734 query_cache_results->out_entries.push_back(std::move(entry));
735 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
736 return;
737 }
738
nhiroki 2016/08/18 00:05:42 DCHECK_EQ(REQUESTS_AND_RESPONSES, query_cache_resu
jkarlin 2016/08/18 14:08:23 Done.
739 query_cache_results->out_requests->push_back(request);
740
681 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { 741 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
682 query_cache_results->out_responses->push_back(response); 742 query_cache_results->out_responses->push_back(response);
683 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1); 743 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
684 return; 744 return;
685 } 745 }
686 746
687 if (!blob_storage_context_) { 747 if (!blob_storage_context_) {
688 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_STORAGE, 748 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_STORAGE,
689 std::unique_ptr<QueryCacheResults>()); 749 std::unique_ptr<QueryCacheResults>());
690 return; 750 return;
691 } 751 }
692 752
693 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 753 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
694 PopulateResponseBody(std::move(entry), &response); 754 PopulateResponseBody(std::move(entry), &response);
695 755
696 query_cache_results->out_responses->push_back(response); 756 query_cache_results->out_responses->push_back(response);
697 query_cache_results->out_blob_data_handles->push_back(*blob_data_handle); 757 query_cache_results->out_blob_data_handles->push_back(*blob_data_handle);
698 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1); 758 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
699 } 759 }
700 760
701 void CacheStorageCache::MatchImpl( 761 void CacheStorageCache::MatchImpl(
702 std::unique_ptr<ServiceWorkerFetchRequest> request, 762 std::unique_ptr<ServiceWorkerFetchRequest> request,
763 const CacheStorageCacheQueryParams& match_params,
703 const ResponseCallback& callback) { 764 const ResponseCallback& callback) {
704 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 765 MatchAllImpl(std::move(request), match_params,
705 if (backend_state_ != BACKEND_OPEN) { 766 base::Bind(&CacheStorageCache::MatchDidMatchAll,
706 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 767 weak_ptr_factory_.GetWeakPtr(), callback));
707 std::unique_ptr<ServiceWorkerResponse>(), 768 }
769
770 void CacheStorageCache::MatchDidMatchAll(
771 const ResponseCallback& callback,
772 CacheStorageError match_all_error,
773 std::unique_ptr<Responses> match_all_responses,
774 std::unique_ptr<BlobDataHandles> match_all_handles) {
775 if (match_all_error != CACHE_STORAGE_OK) {
776 callback.Run(match_all_error, std::unique_ptr<ServiceWorkerResponse>(),
708 std::unique_ptr<storage::BlobDataHandle>()); 777 std::unique_ptr<storage::BlobDataHandle>());
709 return; 778 return;
710 } 779 }
711 780
712 if (!request->method.empty() && request->method != "GET") { 781 if (match_all_responses->empty()) {
713 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, 782 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
714 std::unique_ptr<ServiceWorkerResponse>(), 783 std::unique_ptr<ServiceWorkerResponse>(),
715 std::unique_ptr<storage::BlobDataHandle>()); 784 std::unique_ptr<storage::BlobDataHandle>());
716 return; 785 return;
717 } 786 }
718 787
719 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 788 std::unique_ptr<storage::BlobDataHandle> data_handle;
720 new disk_cache::Entry*()); 789 if (match_all_responses->at(0).blob_size > 0) {
nhiroki 2016/08/18 00:05:42 OPTIONAL: For this case, QueryCache() doesn't have
jkarlin 2016/08/18 14:08:23 Ack. The reason I didn't add that optimization is
721 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 790 // NOTE: This assumes that MatchAll returns the handles in the same order
722 ServiceWorkerFetchRequest* request_ptr = request.get(); 791 // as the responses.
723 792 data_handle =
724 net::CompletionCallback open_entry_callback = base::Bind( 793 base::MakeUnique<storage::BlobDataHandle>(match_all_handles->at(0));
725 &CacheStorageCache::MatchDidOpenEntry, weak_ptr_factory_.GetWeakPtr(),
726 base::Passed(std::move(request)), callback,
727 base::Passed(std::move(scoped_entry_ptr)));
728
729 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr,
730 open_entry_callback);
731 if (rv != net::ERR_IO_PENDING)
732 open_entry_callback.Run(rv);
733 }
734
735 void CacheStorageCache::MatchDidOpenEntry(
736 std::unique_ptr<ServiceWorkerFetchRequest> request,
737 const ResponseCallback& callback,
738 std::unique_ptr<disk_cache::Entry*> entry_ptr,
739 int rv) {
740 if (rv != net::OK) {
741 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
742 std::unique_ptr<ServiceWorkerResponse>(),
743 std::unique_ptr<storage::BlobDataHandle>());
744 return;
745 }
746 disk_cache::ScopedEntryPtr entry(*entry_ptr);
747
748 MetadataCallback headers_callback = base::Bind(
749 &CacheStorageCache::MatchDidReadMetadata, weak_ptr_factory_.GetWeakPtr(),
750 base::Passed(std::move(request)), callback,
751 base::Passed(std::move(entry)));
752
753 ReadMetadata(*entry_ptr, headers_callback);
754 }
755
756 void CacheStorageCache::MatchDidReadMetadata(
757 std::unique_ptr<ServiceWorkerFetchRequest> request,
758 const ResponseCallback& callback,
759 disk_cache::ScopedEntryPtr entry,
760 std::unique_ptr<CacheMetadata> metadata) {
761 if (!metadata) {
762 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
763 std::unique_ptr<ServiceWorkerResponse>(),
764 std::unique_ptr<storage::BlobDataHandle>());
765 return;
766 } 794 }
767 795
768 std::unique_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse); 796 callback.Run(CACHE_STORAGE_OK, base::MakeUnique<ServiceWorkerResponse>(
769 PopulateResponseMetadata(*metadata, response.get()); 797 match_all_responses->at(0)),
nhiroki 2016/08/18 00:05:42 Indent looks broken.
jkarlin 2016/08/18 14:08:23 Done.
770 798 std::move(data_handle));
771 ServiceWorkerHeaderMap cached_request_headers;
772 for (int i = 0; i < metadata->request().headers_size(); ++i) {
773 const CacheHeaderMap header = metadata->request().headers(i);
774 DCHECK_EQ(std::string::npos, header.name().find('\0'));
775 DCHECK_EQ(std::string::npos, header.value().find('\0'));
776 cached_request_headers[header.name()] = header.value();
777 }
778
779 if (!VaryMatches(request->headers, cached_request_headers,
780 response->headers)) {
781 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND,
782 std::unique_ptr<ServiceWorkerResponse>(),
783 std::unique_ptr<storage::BlobDataHandle>());
784 return;
785 }
786
787 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
788 callback.Run(CACHE_STORAGE_OK, std::move(response),
789 std::unique_ptr<storage::BlobDataHandle>());
790 return;
791 }
792
793 if (!blob_storage_context_) {
794 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
795 std::unique_ptr<ServiceWorkerResponse>(),
796 std::unique_ptr<storage::BlobDataHandle>());
797 return;
798 }
799
800 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
801 PopulateResponseBody(std::move(entry), response.get());
802 callback.Run(CACHE_STORAGE_OK, std::move(response),
803 std::move(blob_data_handle));
804 } 799 }
805 800
806 void CacheStorageCache::MatchAllImpl( 801 void CacheStorageCache::MatchAllImpl(
807 std::unique_ptr<ServiceWorkerFetchRequest> request, 802 std::unique_ptr<ServiceWorkerFetchRequest> request,
808 const CacheStorageCacheQueryParams& options, 803 const CacheStorageCacheQueryParams& options,
809 const ResponsesCallback& callback) { 804 const ResponsesCallback& callback) {
810 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 805 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
811 if (backend_state_ != BACKEND_OPEN) { 806 if (backend_state_ != BACKEND_OPEN) {
812 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 807 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
813 std::unique_ptr<BlobDataHandles>()); 808 std::unique_ptr<BlobDataHandles>());
814 return; 809 return;
815 } 810 }
816 811
817 QueryCache(std::move(request), options, 812 QueryCache(std::move(request), options,
813 QueryCacheType::REQUESTS_AND_RESPONSES,
818 base::Bind(&CacheStorageCache::MatchAllDidQueryCache, 814 base::Bind(&CacheStorageCache::MatchAllDidQueryCache,
819 weak_ptr_factory_.GetWeakPtr(), callback)); 815 weak_ptr_factory_.GetWeakPtr(), callback));
820 } 816 }
821 817
822 void CacheStorageCache::MatchAllDidQueryCache( 818 void CacheStorageCache::MatchAllDidQueryCache(
823 const ResponsesCallback& callback, 819 const ResponsesCallback& callback,
824 CacheStorageError error, 820 CacheStorageError error,
825 std::unique_ptr<QueryCacheResults> query_cache_results) { 821 std::unique_ptr<QueryCacheResults> query_cache_results) {
826 if (error != CACHE_STORAGE_OK) { 822 if (error != CACHE_STORAGE_OK) {
827 callback.Run(error, std::unique_ptr<Responses>(), 823 callback.Run(error, std::unique_ptr<Responses>(),
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
998 base::Passed(std::move(put_context)))); 994 base::Passed(std::move(put_context))));
999 } 995 }
1000 996
1001 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) { 997 void CacheStorageCache::PutImpl(std::unique_ptr<PutContext> put_context) {
1002 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 998 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1003 if (backend_state_ != BACKEND_OPEN) { 999 if (backend_state_ != BACKEND_OPEN) {
1004 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1000 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1005 return; 1001 return;
1006 } 1002 }
1007 1003
1008 std::unique_ptr<ServiceWorkerFetchRequest> request_copy( 1004 std::string key = put_context->request->url.spec();
1009 new ServiceWorkerFetchRequest(*put_context->request));
1010 1005
1011 DeleteImpl(std::move(request_copy), CacheStorageCacheQueryParams(), 1006 net::CompletionCallback callback = base::Bind(
1012 base::Bind(&CacheStorageCache::PutDidDelete, 1007 &CacheStorageCache::PutDidDoomEntry, weak_ptr_factory_.GetWeakPtr(),
1013 weak_ptr_factory_.GetWeakPtr(), 1008 base::Passed(std::move(put_context)));
1014 base::Passed(std::move(put_context)))); 1009
1010 int rv = backend_->DoomEntry(key, callback);
1011 if (rv != net::ERR_IO_PENDING)
1012 callback.Run(rv);
1015 } 1013 }
1016 1014
1017 void CacheStorageCache::PutDidDelete(std::unique_ptr<PutContext> put_context, 1015 void CacheStorageCache::PutDidDoomEntry(std::unique_ptr<PutContext> put_context,
1018 CacheStorageError delete_error) { 1016 int rv) {
1019 if (backend_state_ != BACKEND_OPEN) { 1017 if (backend_state_ != BACKEND_OPEN) {
1020 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1018 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1021 return; 1019 return;
1022 } 1020 }
1023 1021
1022 // |rv| is ignored as doom entry can fail if the entry doesn't exist.
1023
1024 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr( 1024 std::unique_ptr<disk_cache::Entry*> scoped_entry_ptr(
1025 new disk_cache::Entry*()); 1025 new disk_cache::Entry*());
1026 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); 1026 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get();
1027 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); 1027 ServiceWorkerFetchRequest* request_ptr = put_context->request.get();
1028 disk_cache::Backend* backend_ptr = backend_.get(); 1028 disk_cache::Backend* backend_ptr = backend_.get();
1029 1029
1030 net::CompletionCallback create_entry_callback = base::Bind( 1030 net::CompletionCallback create_entry_callback = base::Bind(
1031 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), 1031 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(),
1032 base::Passed(std::move(scoped_entry_ptr)), 1032 base::Passed(std::move(scoped_entry_ptr)),
1033 base::Passed(std::move(put_context))); 1033 base::Passed(std::move(put_context)));
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1214 void CacheStorageCache::DeleteImpl( 1214 void CacheStorageCache::DeleteImpl(
1215 std::unique_ptr<ServiceWorkerFetchRequest> request, 1215 std::unique_ptr<ServiceWorkerFetchRequest> request,
1216 const CacheStorageCacheQueryParams& match_params, 1216 const CacheStorageCacheQueryParams& match_params,
1217 const ErrorCallback& callback) { 1217 const ErrorCallback& callback) {
1218 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1218 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1219 if (backend_state_ != BACKEND_OPEN) { 1219 if (backend_state_ != BACKEND_OPEN) {
1220 callback.Run(CACHE_STORAGE_ERROR_STORAGE); 1220 callback.Run(CACHE_STORAGE_ERROR_STORAGE);
1221 return; 1221 return;
1222 } 1222 }
1223 1223
1224 if (match_params.ignore_search) { 1224 QueryCache(std::move(request), match_params, QueryCacheType::CACHE_ENTRIES,
1225 OpenAllEntries(base::Bind(&CacheStorageCache::DeleteDidOpenAllEntries, 1225 base::Bind(&CacheStorageCache::DeleteDidQueryCache,
1226 weak_ptr_factory_.GetWeakPtr(), 1226 weak_ptr_factory_.GetWeakPtr(), callback));
1227 base::Passed(std::move(request)), callback));
1228 return;
1229 }
1230
1231 std::unique_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*);
1232
1233 disk_cache::Entry** entry_ptr = entry.get();
1234
1235 ServiceWorkerFetchRequest* request_ptr = request.get();
1236
1237 net::CompletionCallback open_entry_callback = base::Bind(
1238 &CacheStorageCache::DeleteDidOpenEntry, weak_ptr_factory_.GetWeakPtr(),
1239 origin_, base::Passed(std::move(request)), callback,
1240 base::Passed(std::move(entry)));
1241
1242 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr,
1243 open_entry_callback);
1244 if (rv != net::ERR_IO_PENDING)
1245 open_entry_callback.Run(rv);
1246 } 1227 }
1247 1228
1248 void CacheStorageCache::DeleteDidOpenAllEntries( 1229 void CacheStorageCache::DeleteDidQueryCache(
1249 std::unique_ptr<ServiceWorkerFetchRequest> request,
1250 const ErrorCallback& callback, 1230 const ErrorCallback& callback,
1251 std::unique_ptr<OpenAllEntriesContext> entries_context, 1231 CacheStorageError error,
1252 CacheStorageError error) { 1232 std::unique_ptr<QueryCacheResults> query_cache_results) {
1253 if (error != CACHE_STORAGE_OK) { 1233 if (error != CACHE_STORAGE_OK) {
1254 callback.Run(error); 1234 callback.Run(error);
1255 return; 1235 return;
1256 } 1236 }
1257 1237
1258 GURL request_url_without_query = RemoveQueryParam(request->url); 1238 if (query_cache_results->out_entries.empty()) {
1259 for (Entries::iterator iter = entries_context->entries.begin();
1260 iter != entries_context->entries.end(); iter++) {
1261 disk_cache::Entry* entry(*iter);
1262 if (request_url_without_query == RemoveQueryParam(GURL(entry->GetKey())))
1263 entry->Doom();
1264 }
1265
1266 entries_context.reset();
1267
1268 UpdateCacheSize();
1269 callback.Run(CACHE_STORAGE_OK);
1270 }
1271
1272 void CacheStorageCache::DeleteDidOpenEntry(
1273 const GURL& origin,
1274 std::unique_ptr<ServiceWorkerFetchRequest> request,
1275 const CacheStorageCache::ErrorCallback& callback,
1276 std::unique_ptr<disk_cache::Entry*> entry_ptr,
1277 int rv) {
1278 if (rv != net::OK) {
1279 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND); 1239 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND);
1280 return; 1240 return;
1281 } 1241 }
1282 1242
1283 DCHECK(entry_ptr); 1243 for (auto& entry : query_cache_results->out_entries) {
1284 disk_cache::ScopedEntryPtr entry(*entry_ptr); 1244 entry->Doom();
1285 1245 entry.reset();
1286 entry->Doom(); 1246 }
1287 entry.reset();
1288 1247
1289 UpdateCacheSize(); 1248 UpdateCacheSize();
1290 callback.Run(CACHE_STORAGE_OK); 1249 callback.Run(CACHE_STORAGE_OK);
1291 } 1250 }
1292 1251
1293 void CacheStorageCache::KeysImpl( 1252 void CacheStorageCache::KeysImpl(
1294 std::unique_ptr<ServiceWorkerFetchRequest> request, 1253 std::unique_ptr<ServiceWorkerFetchRequest> request,
1295 const CacheStorageCacheQueryParams& options, 1254 const CacheStorageCacheQueryParams& options,
1296 const RequestsCallback& callback) { 1255 const RequestsCallback& callback) {
1297 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1256 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1298 if (backend_state_ != BACKEND_OPEN) { 1257 if (backend_state_ != BACKEND_OPEN) {
1299 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 1258 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>());
1300 return; 1259 return;
1301 } 1260 }
1302 1261
1303 QueryCache(std::move(request), options, 1262 QueryCache(std::move(request), options,
1263 QueryCacheType::REQUESTS_AND_RESPONSES,
1304 base::Bind(&CacheStorageCache::KeysDidQueryCache, 1264 base::Bind(&CacheStorageCache::KeysDidQueryCache,
1305 weak_ptr_factory_.GetWeakPtr(), callback)); 1265 weak_ptr_factory_.GetWeakPtr(), callback));
1306 } 1266 }
1307 1267
1308 void CacheStorageCache::KeysDidQueryCache( 1268 void CacheStorageCache::KeysDidQueryCache(
1309 const RequestsCallback& callback, 1269 const RequestsCallback& callback,
1310 CacheStorageError error, 1270 CacheStorageError error,
1311 std::unique_ptr<QueryCacheResults> query_cache_results) { 1271 std::unique_ptr<QueryCacheResults> query_cache_results) {
1312 if (error != CACHE_STORAGE_OK) { 1272 if (error != CACHE_STORAGE_OK) {
1313 callback.Run(error, std::unique_ptr<Requests>()); 1273 callback.Run(error, std::unique_ptr<Requests>());
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
1479 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1439 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1480 return blob_storage_context_->AddFinishedBlob(&blob_data); 1440 return blob_storage_context_->AddFinishedBlob(&blob_data);
1481 } 1441 }
1482 1442
1483 std::unique_ptr<CacheStorageCacheHandle> 1443 std::unique_ptr<CacheStorageCacheHandle>
1484 CacheStorageCache::CreateCacheHandle() { 1444 CacheStorageCache::CreateCacheHandle() {
1485 return cache_storage_->CreateCacheHandle(this); 1445 return cache_storage_->CreateCacheHandle(this);
1486 } 1446 }
1487 1447
1488 } // namespace content 1448 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698