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

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

Issue 2137833002: CacheStorage: Introduce QueryCache algorithm. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 Entries entries; 203 Entries entries;
204 204
205 // Used for enumerating cache entries. 205 // Used for enumerating cache entries.
206 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator; 206 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator;
207 disk_cache::Entry* enumerated_entry; 207 disk_cache::Entry* enumerated_entry;
208 208
209 private: 209 private:
210 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); 210 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext);
211 }; 211 };
212 212
213 // The state needed to pass between CacheStorageCache::MatchAll callbacks.
214 struct CacheStorageCache::MatchAllContext {
215 MatchAllContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
216 const CacheStorageCacheQueryParams& match_params,
217 const ResponsesCallback& callback)
218 : request(std::move(request)),
219 options(match_params),
220 original_callback(callback),
221 out_responses(new Responses),
222 out_blob_data_handles(new BlobDataHandles) {}
223 ~MatchAllContext() {}
224
225 std::unique_ptr<ServiceWorkerFetchRequest> request;
226
227 CacheStorageCacheQueryParams options;
228
229 // The callback passed to the MatchAll() function.
230 ResponsesCallback original_callback;
231
232 // The outputs of the MatchAll function.
233 std::unique_ptr<Responses> out_responses;
234 std::unique_ptr<BlobDataHandles> out_blob_data_handles;
235
236 // The context holding open entries.
237 std::unique_ptr<OpenAllEntriesContext> entries_context;
238
239 private:
240 DISALLOW_COPY_AND_ASSIGN(MatchAllContext);
241 };
242
243 // The state needed to pass between CacheStorageCache::Keys callbacks.
244 struct CacheStorageCache::KeysContext {
245 explicit KeysContext(const CacheStorageCache::RequestsCallback& callback)
246 : original_callback(callback), out_keys(new Requests()) {}
247 ~KeysContext() {}
248
249 // The callback passed to the Keys() function.
250 RequestsCallback original_callback;
251
252 // The output of the Keys function.
253 std::unique_ptr<Requests> out_keys;
254
255 // The context holding open entries.
256 std::unique_ptr<OpenAllEntriesContext> entries_context;
257
258 private:
259 DISALLOW_COPY_AND_ASSIGN(KeysContext);
260 };
261
262 // The state needed to pass between CacheStorageCache::Put callbacks. 213 // The state needed to pass between CacheStorageCache::Put callbacks.
263 struct CacheStorageCache::PutContext { 214 struct CacheStorageCache::PutContext {
264 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 215 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
265 std::unique_ptr<ServiceWorkerResponse> response, 216 std::unique_ptr<ServiceWorkerResponse> response,
266 std::unique_ptr<storage::BlobDataHandle> blob_data_handle, 217 std::unique_ptr<storage::BlobDataHandle> blob_data_handle,
267 const CacheStorageCache::ErrorCallback& callback) 218 const CacheStorageCache::ErrorCallback& callback)
268 : request(std::move(request)), 219 : request(std::move(request)),
269 response(std::move(response)), 220 response(std::move(response)),
270 blob_data_handle(std::move(blob_data_handle)), 221 blob_data_handle(std::move(blob_data_handle)),
271 callback(callback) {} 222 callback(callback) {}
272 223
273 // Input parameters to the Put function. 224 // Input parameters to the Put function.
274 std::unique_ptr<ServiceWorkerFetchRequest> request; 225 std::unique_ptr<ServiceWorkerFetchRequest> request;
275 std::unique_ptr<ServiceWorkerResponse> response; 226 std::unique_ptr<ServiceWorkerResponse> response;
276 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 227 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
277 CacheStorageCache::ErrorCallback callback; 228 CacheStorageCache::ErrorCallback callback;
278 disk_cache::ScopedEntryPtr cache_entry; 229 disk_cache::ScopedEntryPtr cache_entry;
279 230
280 private: 231 private:
281 DISALLOW_COPY_AND_ASSIGN(PutContext); 232 DISALLOW_COPY_AND_ASSIGN(PutContext);
282 }; 233 };
283 234
235 struct CacheStorageCache::QueryCacheResults {
236 QueryCacheResults(std::unique_ptr<ServiceWorkerFetchRequest> request,
237 const CacheStorageCacheQueryParams& options,
238 const QueryCacheResultsCallback& callback)
239 : request(std::move(request)),
240 options(options),
241 callback(callback),
242 out_requests(new Requests),
243 out_responses(new Responses),
244 out_blob_data_handles(new BlobDataHandles) {}
245
246 std::unique_ptr<ServiceWorkerFetchRequest> request;
247 CacheStorageCacheQueryParams options;
248 QueryCacheResultsCallback callback;
249
250 std::unique_ptr<Requests> out_requests;
251 std::unique_ptr<Responses> out_responses;
252 std::unique_ptr<BlobDataHandles> out_blob_data_handles;
253
254 std::unique_ptr<OpenAllEntriesContext> entries_context;
255
256 private:
257 DISALLOW_COPY_AND_ASSIGN(QueryCacheResults);
258 };
259
284 // static 260 // static
285 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 261 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
286 const GURL& origin, 262 const GURL& origin,
287 const std::string& cache_name, 263 const std::string& cache_name,
288 CacheStorage* cache_storage, 264 CacheStorage* cache_storage,
289 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 265 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
290 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 266 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
291 base::WeakPtr<storage::BlobStorageContext> blob_context) { 267 base::WeakPtr<storage::BlobStorageContext> blob_context) {
292 return std::unique_ptr<CacheStorageCache>( 268 return std::unique_ptr<CacheStorageCache>(
293 new CacheStorageCache(origin, cache_name, base::FilePath(), cache_storage, 269 new CacheStorageCache(origin, cache_name, base::FilePath(), cache_storage,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 if (!LazyInitialize()) { 315 if (!LazyInitialize()) {
340 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 316 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
341 std::unique_ptr<BlobDataHandles>()); 317 std::unique_ptr<BlobDataHandles>());
342 return; 318 return;
343 } 319 }
344 320
345 ResponsesCallback pending_callback = 321 ResponsesCallback pending_callback =
346 base::Bind(&CacheStorageCache::PendingResponsesCallback, 322 base::Bind(&CacheStorageCache::PendingResponsesCallback,
347 weak_ptr_factory_.GetWeakPtr(), callback); 323 weak_ptr_factory_.GetWeakPtr(), callback);
348 324
349 std::unique_ptr<MatchAllContext> context(
350 new MatchAllContext(std::move(request), match_params, pending_callback));
351 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, 325 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl,
352 weak_ptr_factory_.GetWeakPtr(), 326 weak_ptr_factory_.GetWeakPtr(),
353 base::Passed(std::move(context)))); 327 base::Passed(std::move(request)), mat ch_params, pending_callback));
nhiroki 2016/07/20 15:44:16 nit: please wrap at 80 columns
zino 2016/07/24 10:14:04 Done.
354 } 328 }
355 329
356 void CacheStorageCache::WriteSideData(const ErrorCallback& callback, 330 void CacheStorageCache::WriteSideData(const ErrorCallback& callback,
357 const GURL& url, 331 const GURL& url,
358 base::Time expected_response_time, 332 base::Time expected_response_time,
359 scoped_refptr<net::IOBuffer> buffer, 333 scoped_refptr<net::IOBuffer> buffer,
360 int buf_len) { 334 int buf_len) {
361 if (!LazyInitialize()) { 335 if (!LazyInitialize()) {
362 base::ThreadTaskRunnerHandle::Get()->PostTask( 336 base::ThreadTaskRunnerHandle::Get()->PostTask(
363 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_STORAGE)); 337 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_STORAGE));
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 net::CompletionCallback open_entry_callback = base::Bind( 602 net::CompletionCallback open_entry_callback = base::Bind(
629 &CacheStorageCache::DidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), 603 &CacheStorageCache::DidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(),
630 base::Passed(std::move(entries_context)), callback); 604 base::Passed(std::move(entries_context)), callback);
631 605
632 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); 606 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback);
633 607
634 if (rv != net::ERR_IO_PENDING) 608 if (rv != net::ERR_IO_PENDING)
635 open_entry_callback.Run(rv); 609 open_entry_callback.Run(rv);
636 } 610 }
637 611
612 void CacheStorageCache::QueryCache(
613 std::unique_ptr<ServiceWorkerFetchRequest> request,
614 const CacheStorageCacheQueryParams& options,
615 const QueryCacheResultsCallback& callback) {
616 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
617 if (backend_state_ != BACKEND_OPEN) {
618 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
619 std::unique_ptr<QueryCacheResults>());
620 return;
621 }
622
623 std::unique_ptr<QueryCacheResults> query_cache_results(
624 new QueryCacheResults(std::move(request), options, callback));
625 OpenAllEntries(base::Bind(&CacheStorageCache::QueryCacheDidOpenAllEntries,
626 weak_ptr_factory_.GetWeakPtr(),
627 base::Passed(std::move(query_cache_results))));
628 }
629
630 void CacheStorageCache::QueryCacheDidOpenAllEntries(
631 std::unique_ptr<QueryCacheResults> query_cache_results,
632 std::unique_ptr<OpenAllEntriesContext> entries_context,
633 CacheStorageError error) {
634 if (error != CACHE_STORAGE_OK) {
635 query_cache_results->callback.Run(error,
636 std::unique_ptr<QueryCacheResults>());
637 return;
638 }
639
640 query_cache_results->entries_context.swap(entries_context);
641 Entries::iterator iter =
642 query_cache_results->entries_context->entries.begin();
643 QueryCacheProcessNextEntry(std::move(query_cache_results), iter);
644 }
645
646 void CacheStorageCache::QueryCacheProcessNextEntry(
647 std::unique_ptr<QueryCacheResults> query_cache_results,
648 const Entries::iterator& iter) {
649 if (iter == query_cache_results->entries_context->entries.end()) {
650 query_cache_results->callback.Run(CACHE_STORAGE_OK,
651 std::move(query_cache_results));
652 return;
653 }
654
655 if (query_cache_results->options.ignore_search) {
656 DCHECK(query_cache_results->request);
657 disk_cache::Entry* entry(*iter);
658 if (RemoveQueryParam(query_cache_results->request->url) !=
659 RemoveQueryParam(GURL(entry->GetKey()))) {
660 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
661 return;
662 }
663 }
664
665 ReadMetadata(*iter,
666 base::Bind(&CacheStorageCache::QueryCacheDidReadMetadata,
667 weak_ptr_factory_.GetWeakPtr(),
668 base::Passed(std::move(query_cache_results)), iter));
669 }
670
671 void CacheStorageCache::QueryCacheDidReadMetadata(
672 std::unique_ptr<QueryCacheResults> query_cache_results,
673 const Entries::iterator& iter,
674 std::unique_ptr<CacheMetadata> metadata) {
675 disk_cache::ScopedEntryPtr entry(*iter);
676 *iter = nullptr;
677
678 if (!metadata) {
679 entry->Doom();
680 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
681 return;
682 }
683
684 ServiceWorkerFetchRequest request;
685 PopulateRequestFromMetadata(*metadata, & request);
nhiroki 2016/07/20 15:44:17 There is an extra space between "&" and "request".
zino 2016/07/24 10:14:04 Done.
686 query_cache_results->out_requests->push_back(request);
687
688 ServiceWorkerResponse response;
689 PopulateResponseMetadata(*metadata, &response);
690
691 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
692 query_cache_results->out_responses->push_back(response);
693 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
694 return;
695 }
696
697 if (!blob_storage_context_) {
698 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_STORAGE,
699 std::unique_ptr<QueryCacheResults>());
700 return;
701 }
702
703 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
704 PopulateResponseBody(std::move(entry), &response);
705
706 query_cache_results->out_responses->push_back(response);
707 query_cache_results->out_blob_data_handles->push_back(*blob_data_handle);
708 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
709 }
710
638 void CacheStorageCache::MatchImpl( 711 void CacheStorageCache::MatchImpl(
639 std::unique_ptr<ServiceWorkerFetchRequest> request, 712 std::unique_ptr<ServiceWorkerFetchRequest> request,
640 const ResponseCallback& callback) { 713 const ResponseCallback& callback) {
641 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 714 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
642 if (backend_state_ != BACKEND_OPEN) { 715 if (backend_state_ != BACKEND_OPEN) {
643 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 716 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
644 std::unique_ptr<ServiceWorkerResponse>(), 717 std::unique_ptr<ServiceWorkerResponse>(),
645 std::unique_ptr<storage::BlobDataHandle>()); 718 std::unique_ptr<storage::BlobDataHandle>());
646 return; 719 return;
647 } 720 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 std::unique_ptr<storage::BlobDataHandle>()); 799 std::unique_ptr<storage::BlobDataHandle>());
727 return; 800 return;
728 } 801 }
729 802
730 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 803 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
731 PopulateResponseBody(std::move(entry), response.get()); 804 PopulateResponseBody(std::move(entry), response.get());
732 callback.Run(CACHE_STORAGE_OK, std::move(response), 805 callback.Run(CACHE_STORAGE_OK, std::move(response),
733 std::move(blob_data_handle)); 806 std::move(blob_data_handle));
734 } 807 }
735 808
736 void CacheStorageCache::MatchAllImpl(std::unique_ptr<MatchAllContext> context) { 809 void CacheStorageCache::MatchAllImpl(std::unique_ptr<ServiceWorkerFetchRequest> request,
810 const CacheStorageCacheQueryParams& options,
811 const ResponsesCallback& callback) {
nhiroki 2016/07/20 15:44:17 Indents look broken.
zino 2016/07/24 10:14:04 Done.
737 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 812 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
738 if (backend_state_ != BACKEND_OPEN) { 813 if (backend_state_ != BACKEND_OPEN) {
739 context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, 814 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
740 std::unique_ptr<Responses>(), 815 std::unique_ptr<Responses>(),
741 std::unique_ptr<BlobDataHandles>()); 816 std::unique_ptr<BlobDataHandles>());
742 return; 817 return;
743 } 818 }
744 819
745 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, 820 QueryCache(
746 weak_ptr_factory_.GetWeakPtr(), 821 std::move(request), options,
747 base::Passed(std::move(context)))); 822 base::Bind(&CacheStorageCache::MatchAllDidQueryCache,
823 weak_ptr_factory_.GetWeakPtr(), callback));
748 } 824 }
749 825
750 void CacheStorageCache::MatchAllDidOpenAllEntries( 826 void CacheStorageCache::MatchAllDidQueryCache(
751 std::unique_ptr<MatchAllContext> context, 827 const ResponsesCallback& callback,
752 std::unique_ptr<OpenAllEntriesContext> entries_context, 828 CacheStorageError error,
753 CacheStorageError error) { 829 std::unique_ptr<QueryCacheResults> query_cache_results) {
754 if (error != CACHE_STORAGE_OK) { 830 if (error != CACHE_STORAGE_OK || !query_cache_results) {
nhiroki 2016/07/20 15:44:17 I suspect the latter condition |!query_cache_resul
zino 2016/07/24 10:14:04 Done.
755 context->original_callback.Run(error, std::unique_ptr<Responses>(), 831 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
nhiroki 2016/07/20 15:44:16 We should pass |error| to the callback in order no
zino 2016/07/24 10:14:04 Done.
756 std::unique_ptr<BlobDataHandles>()); 832 std::unique_ptr<BlobDataHandles>());
757 return; 833 return;
758 } 834 }
759 835
760 context->entries_context.swap(entries_context); 836 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results->out_responses),
761 Entries::iterator iter = context->entries_context->entries.begin(); 837 std::move(query_cache_results->out_blob_data_handles));
762 MatchAllProcessNextEntry(std::move(context), iter);
763 }
764
765 void CacheStorageCache::MatchAllProcessNextEntry(
766 std::unique_ptr<MatchAllContext> context,
767 const Entries::iterator& iter) {
768 if (iter == context->entries_context->entries.end()) {
769 // All done. Return all of the responses.
770 context->original_callback.Run(CACHE_STORAGE_OK,
771 std::move(context->out_responses),
772 std::move(context->out_blob_data_handles));
773 return;
774 }
775
776 if (context->options.ignore_search) {
777 DCHECK(context->request);
778 disk_cache::Entry* entry(*iter);
779 if (RemoveQueryParam(context->request->url) !=
780 RemoveQueryParam(GURL(entry->GetKey()))) {
781 // In this case, we don't need to read data.
782 MatchAllProcessNextEntry(std::move(context), iter + 1);
783 return;
784 }
785 }
786
787 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata,
788 weak_ptr_factory_.GetWeakPtr(),
789 base::Passed(std::move(context)), iter));
790 }
791
792 void CacheStorageCache::MatchAllDidReadMetadata(
793 std::unique_ptr<MatchAllContext> context,
794 const Entries::iterator& iter,
795 std::unique_ptr<CacheMetadata> metadata) {
796 // Move ownership of the entry from the context.
797 disk_cache::ScopedEntryPtr entry(*iter);
798 *iter = nullptr;
799
800 if (!metadata) {
801 entry->Doom();
802 MatchAllProcessNextEntry(std::move(context), iter + 1);
803 return;
804 }
805
806 ServiceWorkerResponse response;
807 PopulateResponseMetadata(*metadata, &response);
808
809 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
810 context->out_responses->push_back(response);
811 MatchAllProcessNextEntry(std::move(context), iter + 1);
812 return;
813 }
814
815 if (!blob_storage_context_) {
816 context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE,
817 std::unique_ptr<Responses>(),
818 std::unique_ptr<BlobDataHandles>());
819 return;
820 }
821
822 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
823 PopulateResponseBody(std::move(entry), &response);
824
825 context->out_responses->push_back(response);
826 context->out_blob_data_handles->push_back(*blob_data_handle);
827 MatchAllProcessNextEntry(std::move(context), iter + 1);
828 } 838 }
829 839
830 void CacheStorageCache::WriteSideDataDidGetQuota( 840 void CacheStorageCache::WriteSideDataDidGetQuota(
831 const ErrorCallback& callback, 841 const ErrorCallback& callback,
832 const GURL& url, 842 const GURL& url,
833 base::Time expected_response_time, 843 base::Time expected_response_time,
834 scoped_refptr<net::IOBuffer> buffer, 844 scoped_refptr<net::IOBuffer> buffer,
835 int buf_len, 845 int buf_len,
836 storage::QuotaStatusCode status_code, 846 storage::QuotaStatusCode status_code,
837 int64_t usage, 847 int64_t usage,
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
1294 callback.Run(CACHE_STORAGE_OK); 1304 callback.Run(CACHE_STORAGE_OK);
1295 } 1305 }
1296 1306
1297 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) { 1307 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) {
1298 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1308 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1299 if (backend_state_ != BACKEND_OPEN) { 1309 if (backend_state_ != BACKEND_OPEN) {
1300 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 1310 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>());
1301 return; 1311 return;
1302 } 1312 }
1303 1313
1304 // 1. Iterate through all of the entries, open them, and add them to a vector. 1314 std::unique_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchReque st);
nhiroki 2016/07/20 15:44:17 nit (80 columns)
zino 2016/07/24 10:14:04 Done.
1305 // 2. For each open entry: 1315 QueryCache(
1306 // 2.1. Read the headers into a protobuf. 1316 std::move(request), CacheStorageCacheQueryParams(),
1307 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). 1317 base::Bind(&CacheStorageCache::KeysDidQueryCache,
1308 // 2.3. Push the response into a vector of requests to be returned. 1318 weak_ptr_factory_.GetWeakPtr(), callback));
1309 // 3. Return the vector of requests (keys).
1310
1311 // The entries have to be loaded into a vector first because enumeration loops
1312 // forever if you read data from a cache entry while enumerating.
1313
1314 OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries,
1315 weak_ptr_factory_.GetWeakPtr(), callback));
1316 } 1319 }
1317 1320
1318 void CacheStorageCache::KeysDidOpenAllEntries( 1321 void CacheStorageCache::KeysDidQueryCache(
1319 const RequestsCallback& callback, 1322 const RequestsCallback& callback,
1320 std::unique_ptr<OpenAllEntriesContext> entries_context, 1323 CacheStorageError error,
1321 CacheStorageError error) { 1324 std::unique_ptr<QueryCacheResults> query_cache_results) {
1322 if (error != CACHE_STORAGE_OK) { 1325 if (error != CACHE_STORAGE_OK || !query_cache_results) {
1323 callback.Run(error, std::unique_ptr<Requests>()); 1326 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>());
nhiroki 2016/07/20 15:44:17 ditto (|!query_cache_results| and an error code)
zino 2016/07/24 10:14:04 Done.
1324 return; 1327 return;
1325 } 1328 }
1326 1329
1327 std::unique_ptr<KeysContext> keys_context(new KeysContext(callback)); 1330 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results->out_requests));
1328 keys_context->entries_context.swap(entries_context);
1329 Entries::iterator iter = keys_context->entries_context->entries.begin();
1330 KeysProcessNextEntry(std::move(keys_context), iter);
1331 }
1332
1333 void CacheStorageCache::KeysProcessNextEntry(
1334 std::unique_ptr<KeysContext> keys_context,
1335 const Entries::iterator& iter) {
1336 if (iter == keys_context->entries_context->entries.end()) {
1337 // All done. Return all of the keys.
1338 keys_context->original_callback.Run(CACHE_STORAGE_OK,
1339 std::move(keys_context->out_keys));
1340 return;
1341 }
1342
1343 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata,
1344 weak_ptr_factory_.GetWeakPtr(),
1345 base::Passed(std::move(keys_context)), iter));
1346 }
1347
1348 void CacheStorageCache::KeysDidReadMetadata(
1349 std::unique_ptr<KeysContext> keys_context,
1350 const Entries::iterator& iter,
1351 std::unique_ptr<CacheMetadata> metadata) {
1352 disk_cache::Entry* entry = *iter;
1353
1354 if (metadata) {
1355 keys_context->out_keys->push_back(ServiceWorkerFetchRequest(
1356 GURL(entry->GetKey()), metadata->request().method(),
1357 ServiceWorkerHeaderMap(), Referrer(), false));
1358
1359 ServiceWorkerHeaderMap& req_headers =
1360 keys_context->out_keys->back().headers;
1361
1362 for (int i = 0; i < metadata->request().headers_size(); ++i) {
1363 const CacheHeaderMap header = metadata->request().headers(i);
1364 DCHECK_EQ(std::string::npos, header.name().find('\0'));
1365 DCHECK_EQ(std::string::npos, header.value().find('\0'));
1366 req_headers.insert(std::make_pair(header.name(), header.value()));
1367 }
1368 } else {
1369 entry->Doom();
1370 }
1371
1372 KeysProcessNextEntry(std::move(keys_context), iter + 1);
1373 } 1331 }
1374 1332
1375 void CacheStorageCache::CloseImpl(const base::Closure& callback) { 1333 void CacheStorageCache::CloseImpl(const base::Closure& callback) {
1376 DCHECK_NE(BACKEND_CLOSED, backend_state_); 1334 DCHECK_NE(BACKEND_CLOSED, backend_state_);
1377 1335
1378 backend_state_ = BACKEND_CLOSED; 1336 backend_state_ = BACKEND_CLOSED;
1379 backend_.reset(); 1337 backend_.reset();
1380 callback.Run(); 1338 callback.Run();
1381 } 1339 }
1382 1340
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1532 1490
1533 void CacheStorageCache::PendingSizeCallback(const SizeCallback& callback, 1491 void CacheStorageCache::PendingSizeCallback(const SizeCallback& callback,
1534 int64_t size) { 1492 int64_t size) {
1535 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); 1493 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1536 1494
1537 callback.Run(size); 1495 callback.Run(size);
1538 if (cache) 1496 if (cache)
1539 scheduler_->CompleteOperationAndRunNext(); 1497 scheduler_->CompleteOperationAndRunNext();
1540 } 1498 }
1541 1499
1500 void CacheStorageCache::PendingQueryCacheResultsCallback(
1501 const QueryCacheResultsCallback& callback,
1502 CacheStorageError error,
1503 std::unique_ptr<QueryCacheResults> results) {
1504 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr();
1505
1506 callback.Run(error, std::move(results));
1507 if (cache)
1508 scheduler_->CompleteOperationAndRunNext();
1509 }
1510
1511 void CacheStorageCache::PopulateRequestFromMetadata(
1512 const CacheMetadata& metadata,
1513 ServiceWorkerFetchRequest* request) {
1514 *request = ServiceWorkerFetchRequest(
1515 GURL(metadata.response().url()), metadata.request().method(),
nhiroki 2016/07/20 15:44:17 Regarding the first argument of ServiceWorkerFetch
zino 2016/07/24 10:14:04 Done.
1516 ServiceWorkerHeaderMap(), Referrer(), false);
1517
1518 for (int i = 0; i < metadata.request().headers_size(); ++i) {
1519 const CacheHeaderMap header = metadata.request().headers(i);
1520 DCHECK_EQ(std::string::npos, header.name().find('\0'));
1521 DCHECK_EQ(std::string::npos, header.value().find('\0'));
1522 request->headers.insert(std::make_pair(header.name(), header.value()));
1523 }
1524 }
1525
1542 void CacheStorageCache::PopulateResponseMetadata( 1526 void CacheStorageCache::PopulateResponseMetadata(
1543 const CacheMetadata& metadata, 1527 const CacheMetadata& metadata,
1544 ServiceWorkerResponse* response) { 1528 ServiceWorkerResponse* response) {
1545 *response = ServiceWorkerResponse( 1529 *response = ServiceWorkerResponse(
1546 GURL(metadata.response().url()), metadata.response().status_code(), 1530 GURL(metadata.response().url()), metadata.response().status_code(),
1547 metadata.response().status_text(), 1531 metadata.response().status_text(),
1548 ProtoResponseTypeToWebResponseType(metadata.response().response_type()), 1532 ProtoResponseTypeToWebResponseType(metadata.response().response_type()),
1549 ServiceWorkerHeaderMap(), "", 0, GURL(), 1533 ServiceWorkerHeaderMap(), "", 0, GURL(),
1550 blink::WebServiceWorkerResponseErrorUnknown, 1534 blink::WebServiceWorkerResponseErrorUnknown,
1551 base::Time::FromInternalValue(metadata.response().response_time()), 1535 base::Time::FromInternalValue(metadata.response().response_time()),
(...skipping 26 matching lines...) Expand all
1578 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1562 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1579 return blob_storage_context_->AddFinishedBlob(&blob_data); 1563 return blob_storage_context_->AddFinishedBlob(&blob_data);
1580 } 1564 }
1581 1565
1582 std::unique_ptr<CacheStorageCacheHandle> 1566 std::unique_ptr<CacheStorageCacheHandle>
1583 CacheStorageCache::CreateCacheHandle() { 1567 CacheStorageCache::CreateCacheHandle() {
1584 return cache_storage_->CreateCacheHandle(this); 1568 return cache_storage_->CreateCacheHandle(this);
1585 } 1569 }
1586 1570
1587 } // namespace content 1571 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698