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

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: reupload 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
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 Entries entries; 205 Entries entries;
206 206
207 // Used for enumerating cache entries. 207 // Used for enumerating cache entries.
208 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator; 208 std::unique_ptr<disk_cache::Backend::Iterator> backend_iterator;
209 disk_cache::Entry* enumerated_entry; 209 disk_cache::Entry* enumerated_entry;
210 210
211 private: 211 private:
212 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); 212 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext);
213 }; 213 };
214 214
215 // The state needed to pass between CacheStorageCache::MatchAll callbacks.
216 struct CacheStorageCache::MatchAllContext {
217 MatchAllContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
218 const CacheStorageCacheQueryParams& match_params,
219 const ResponsesCallback& callback)
220 : request(std::move(request)),
221 options(match_params),
222 original_callback(callback),
223 out_responses(new Responses),
224 out_blob_data_handles(new BlobDataHandles) {}
225 ~MatchAllContext() {}
226
227 std::unique_ptr<ServiceWorkerFetchRequest> request;
228
229 CacheStorageCacheQueryParams options;
230
231 // The callback passed to the MatchAll() function.
232 ResponsesCallback original_callback;
233
234 // The outputs of the MatchAll function.
235 std::unique_ptr<Responses> out_responses;
236 std::unique_ptr<BlobDataHandles> out_blob_data_handles;
237
238 // The context holding open entries.
239 std::unique_ptr<OpenAllEntriesContext> entries_context;
240
241 private:
242 DISALLOW_COPY_AND_ASSIGN(MatchAllContext);
243 };
244
245 // The state needed to pass between CacheStorageCache::Keys callbacks.
246 struct CacheStorageCache::KeysContext {
247 explicit KeysContext(const CacheStorageCache::RequestsCallback& callback)
248 : original_callback(callback), out_keys(new Requests()) {}
249 ~KeysContext() {}
250
251 // The callback passed to the Keys() function.
252 RequestsCallback original_callback;
253
254 // The output of the Keys function.
255 std::unique_ptr<Requests> out_keys;
256
257 // The context holding open entries.
258 std::unique_ptr<OpenAllEntriesContext> entries_context;
259
260 private:
261 DISALLOW_COPY_AND_ASSIGN(KeysContext);
262 };
263
264 // The state needed to pass between CacheStorageCache::Put callbacks. 215 // The state needed to pass between CacheStorageCache::Put callbacks.
265 struct CacheStorageCache::PutContext { 216 struct CacheStorageCache::PutContext {
266 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request, 217 PutContext(std::unique_ptr<ServiceWorkerFetchRequest> request,
267 std::unique_ptr<ServiceWorkerResponse> response, 218 std::unique_ptr<ServiceWorkerResponse> response,
268 std::unique_ptr<storage::BlobDataHandle> blob_data_handle, 219 std::unique_ptr<storage::BlobDataHandle> blob_data_handle,
269 const CacheStorageCache::ErrorCallback& callback) 220 const CacheStorageCache::ErrorCallback& callback)
270 : request(std::move(request)), 221 : request(std::move(request)),
271 response(std::move(response)), 222 response(std::move(response)),
272 blob_data_handle(std::move(blob_data_handle)), 223 blob_data_handle(std::move(blob_data_handle)),
273 callback(callback) {} 224 callback(callback) {}
274 225
275 // Input parameters to the Put function. 226 // Input parameters to the Put function.
276 std::unique_ptr<ServiceWorkerFetchRequest> request; 227 std::unique_ptr<ServiceWorkerFetchRequest> request;
277 std::unique_ptr<ServiceWorkerResponse> response; 228 std::unique_ptr<ServiceWorkerResponse> response;
278 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 229 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
279 CacheStorageCache::ErrorCallback callback; 230 CacheStorageCache::ErrorCallback callback;
280 disk_cache::ScopedEntryPtr cache_entry; 231 disk_cache::ScopedEntryPtr cache_entry;
281 232
282 private: 233 private:
283 DISALLOW_COPY_AND_ASSIGN(PutContext); 234 DISALLOW_COPY_AND_ASSIGN(PutContext);
284 }; 235 };
285 236
237 struct CacheStorageCache::QueryCacheResults {
238 QueryCacheResults(std::unique_ptr<ServiceWorkerFetchRequest> request,
239 const CacheStorageCacheQueryParams& options,
240 const QueryCacheResultsCallback& callback)
241 : request(std::move(request)),
242 options(options),
243 callback(callback),
244 out_requests(new Requests),
245 out_responses(new Responses),
246 out_blob_data_handles(new BlobDataHandles) {}
247
248 std::unique_ptr<ServiceWorkerFetchRequest> request;
249 CacheStorageCacheQueryParams options;
250 QueryCacheResultsCallback callback;
251
252 std::unique_ptr<Requests> out_requests;
253 std::unique_ptr<Responses> out_responses;
254 std::unique_ptr<BlobDataHandles> out_blob_data_handles;
255
256 std::unique_ptr<OpenAllEntriesContext> entries_context;
257
258 private:
259 DISALLOW_COPY_AND_ASSIGN(QueryCacheResults);
260 };
261
286 // static 262 // static
287 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( 263 std::unique_ptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache(
288 const GURL& origin, 264 const GURL& origin,
289 const std::string& cache_name, 265 const std::string& cache_name,
290 CacheStorage* cache_storage, 266 CacheStorage* cache_storage,
291 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 267 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
292 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 268 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
293 base::WeakPtr<storage::BlobStorageContext> blob_context) { 269 base::WeakPtr<storage::BlobStorageContext> blob_context) {
294 CacheStorageCache* cache = 270 CacheStorageCache* cache =
295 new CacheStorageCache(origin, cache_name, base::FilePath(), cache_storage, 271 new CacheStorageCache(origin, cache_name, base::FilePath(), cache_storage,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 void CacheStorageCache::MatchAll( 315 void CacheStorageCache::MatchAll(
340 std::unique_ptr<ServiceWorkerFetchRequest> request, 316 std::unique_ptr<ServiceWorkerFetchRequest> request,
341 const CacheStorageCacheQueryParams& match_params, 317 const CacheStorageCacheQueryParams& match_params,
342 const ResponsesCallback& callback) { 318 const ResponsesCallback& callback) {
343 if (backend_state_ == BACKEND_CLOSED) { 319 if (backend_state_ == BACKEND_CLOSED) {
344 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(), 320 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
345 std::unique_ptr<BlobDataHandles>()); 321 std::unique_ptr<BlobDataHandles>());
346 return; 322 return;
347 } 323 }
348 324
349 std::unique_ptr<MatchAllContext> context( 325 scheduler_->ScheduleOperation(base::Bind(
350 new MatchAllContext(std::move(request), match_params, 326 &CacheStorageCache::MatchAllImpl, weak_ptr_factory_.GetWeakPtr(),
351 scheduler_->WrapCallbackToRunNext(callback))); 327 base::Passed(std::move(request)), match_params,
352 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, 328 scheduler_->WrapCallbackToRunNext(callback)));
353 weak_ptr_factory_.GetWeakPtr(),
354 base::Passed(std::move(context))));
355 } 329 }
356 330
357 void CacheStorageCache::WriteSideData(const ErrorCallback& callback, 331 void CacheStorageCache::WriteSideData(const ErrorCallback& callback,
358 const GURL& url, 332 const GURL& url,
359 base::Time expected_response_time, 333 base::Time expected_response_time,
360 scoped_refptr<net::IOBuffer> buffer, 334 scoped_refptr<net::IOBuffer> buffer,
361 int buf_len) { 335 int buf_len) {
362 if (backend_state_ == BACKEND_CLOSED) { 336 if (backend_state_ == BACKEND_CLOSED) {
363 base::ThreadTaskRunnerHandle::Get()->PostTask( 337 base::ThreadTaskRunnerHandle::Get()->PostTask(
364 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_STORAGE)); 338 FROM_HERE, base::Bind(callback, CACHE_STORAGE_ERROR_STORAGE));
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 net::CompletionCallback open_entry_callback = base::Bind( 574 net::CompletionCallback open_entry_callback = base::Bind(
601 &CacheStorageCache::DidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), 575 &CacheStorageCache::DidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(),
602 base::Passed(std::move(entries_context)), callback); 576 base::Passed(std::move(entries_context)), callback);
603 577
604 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); 578 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback);
605 579
606 if (rv != net::ERR_IO_PENDING) 580 if (rv != net::ERR_IO_PENDING)
607 open_entry_callback.Run(rv); 581 open_entry_callback.Run(rv);
608 } 582 }
609 583
584 void CacheStorageCache::QueryCache(
585 std::unique_ptr<ServiceWorkerFetchRequest> request,
586 const CacheStorageCacheQueryParams& options,
587 const QueryCacheResultsCallback& callback) {
588 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
589 if (backend_state_ != BACKEND_OPEN) {
590 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
591 std::unique_ptr<QueryCacheResults>());
592 return;
593 }
594
595 std::unique_ptr<QueryCacheResults> query_cache_results(
596 new QueryCacheResults(std::move(request), options, callback));
597 OpenAllEntries(base::Bind(&CacheStorageCache::QueryCacheDidOpenAllEntries,
598 weak_ptr_factory_.GetWeakPtr(),
599 base::Passed(std::move(query_cache_results))));
600 }
601
602 void CacheStorageCache::QueryCacheDidOpenAllEntries(
603 std::unique_ptr<QueryCacheResults> query_cache_results,
604 std::unique_ptr<OpenAllEntriesContext> entries_context,
605 CacheStorageError error) {
606 if (error != CACHE_STORAGE_OK) {
607 query_cache_results->callback.Run(error,
608 std::unique_ptr<QueryCacheResults>());
609 return;
610 }
611
612 query_cache_results->entries_context.swap(entries_context);
613 Entries::iterator iter =
614 query_cache_results->entries_context->entries.begin();
615 QueryCacheProcessNextEntry(std::move(query_cache_results), iter);
616 }
617
618 void CacheStorageCache::QueryCacheProcessNextEntry(
619 std::unique_ptr<QueryCacheResults> query_cache_results,
620 const Entries::iterator& iter) {
621 if (iter == query_cache_results->entries_context->entries.end()) {
622 query_cache_results->callback.Run(CACHE_STORAGE_OK,
623 std::move(query_cache_results));
jkarlin 2016/07/28 01:11:35 This is why the windows tests are failing. You're
624 return;
625 }
626
627 if (query_cache_results->options.ignore_search) {
628 DCHECK(query_cache_results->request);
629 disk_cache::Entry* entry(*iter);
630 if (RemoveQueryParam(query_cache_results->request->url) !=
631 RemoveQueryParam(GURL(entry->GetKey()))) {
632 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
633 return;
634 }
635 }
636
637 ReadMetadata(*iter,
638 base::Bind(&CacheStorageCache::QueryCacheDidReadMetadata,
639 weak_ptr_factory_.GetWeakPtr(),
640 base::Passed(std::move(query_cache_results)), iter));
641 }
642
643 void CacheStorageCache::QueryCacheDidReadMetadata(
644 std::unique_ptr<QueryCacheResults> query_cache_results,
645 const Entries::iterator& iter,
646 std::unique_ptr<CacheMetadata> metadata) {
647 disk_cache::ScopedEntryPtr entry(*iter);
648 *iter = nullptr;
649
650 if (!metadata) {
651 entry->Doom();
652 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
653 return;
654 }
655
656 ServiceWorkerFetchRequest request;
657 PopulateRequestFromMetadata(*metadata, GURL(entry->GetKey()), &request);
658 query_cache_results->out_requests->push_back(request);
659
660 ServiceWorkerResponse response;
661 PopulateResponseMetadata(*metadata, &response);
662
663 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
664 query_cache_results->out_responses->push_back(response);
665 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
666 return;
667 }
668
669 if (!blob_storage_context_) {
670 query_cache_results->callback.Run(CACHE_STORAGE_ERROR_STORAGE,
671 std::unique_ptr<QueryCacheResults>());
672 return;
673 }
674
675 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
676 PopulateResponseBody(std::move(entry), &response);
677
678 query_cache_results->out_responses->push_back(response);
679 query_cache_results->out_blob_data_handles->push_back(*blob_data_handle);
680 QueryCacheProcessNextEntry(std::move(query_cache_results), iter + 1);
681 }
682
610 void CacheStorageCache::MatchImpl( 683 void CacheStorageCache::MatchImpl(
611 std::unique_ptr<ServiceWorkerFetchRequest> request, 684 std::unique_ptr<ServiceWorkerFetchRequest> request,
612 const ResponseCallback& callback) { 685 const ResponseCallback& callback) {
613 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 686 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
614 if (backend_state_ != BACKEND_OPEN) { 687 if (backend_state_ != BACKEND_OPEN) {
615 callback.Run(CACHE_STORAGE_ERROR_STORAGE, 688 callback.Run(CACHE_STORAGE_ERROR_STORAGE,
616 std::unique_ptr<ServiceWorkerResponse>(), 689 std::unique_ptr<ServiceWorkerResponse>(),
617 std::unique_ptr<storage::BlobDataHandle>()); 690 std::unique_ptr<storage::BlobDataHandle>());
618 return; 691 return;
619 } 692 }
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 std::unique_ptr<storage::BlobDataHandle>()); 771 std::unique_ptr<storage::BlobDataHandle>());
699 return; 772 return;
700 } 773 }
701 774
702 std::unique_ptr<storage::BlobDataHandle> blob_data_handle = 775 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
703 PopulateResponseBody(std::move(entry), response.get()); 776 PopulateResponseBody(std::move(entry), response.get());
704 callback.Run(CACHE_STORAGE_OK, std::move(response), 777 callback.Run(CACHE_STORAGE_OK, std::move(response),
705 std::move(blob_data_handle)); 778 std::move(blob_data_handle));
706 } 779 }
707 780
708 void CacheStorageCache::MatchAllImpl(std::unique_ptr<MatchAllContext> context) { 781 void CacheStorageCache::MatchAllImpl(
782 std::unique_ptr<ServiceWorkerFetchRequest> request,
783 const CacheStorageCacheQueryParams& options,
784 const ResponsesCallback& callback) {
709 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 785 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
710 if (backend_state_ != BACKEND_OPEN) { 786 if (backend_state_ != BACKEND_OPEN) {
711 context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, 787 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Responses>(),
712 std::unique_ptr<Responses>(), 788 std::unique_ptr<BlobDataHandles>());
713 std::unique_ptr<BlobDataHandles>());
714 return; 789 return;
715 } 790 }
716 791
717 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, 792 QueryCache(std::move(request), options,
718 weak_ptr_factory_.GetWeakPtr(), 793 base::Bind(&CacheStorageCache::MatchAllDidQueryCache,
719 base::Passed(std::move(context)))); 794 weak_ptr_factory_.GetWeakPtr(), callback));
720 } 795 }
721 796
722 void CacheStorageCache::MatchAllDidOpenAllEntries( 797 void CacheStorageCache::MatchAllDidQueryCache(
723 std::unique_ptr<MatchAllContext> context, 798 const ResponsesCallback& callback,
724 std::unique_ptr<OpenAllEntriesContext> entries_context, 799 CacheStorageError error,
725 CacheStorageError error) { 800 std::unique_ptr<QueryCacheResults> query_cache_results) {
726 if (error != CACHE_STORAGE_OK) { 801 if (error != CACHE_STORAGE_OK) {
727 context->original_callback.Run(error, std::unique_ptr<Responses>(), 802 callback.Run(error, std::unique_ptr<Responses>(),
728 std::unique_ptr<BlobDataHandles>()); 803 std::unique_ptr<BlobDataHandles>());
729 return; 804 return;
730 } 805 }
731 806
732 context->entries_context.swap(entries_context); 807 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results->out_responses),
733 Entries::iterator iter = context->entries_context->entries.begin(); 808 std::move(query_cache_results->out_blob_data_handles));
734 MatchAllProcessNextEntry(std::move(context), iter);
735 }
736
737 void CacheStorageCache::MatchAllProcessNextEntry(
738 std::unique_ptr<MatchAllContext> context,
739 const Entries::iterator& iter) {
740 if (iter == context->entries_context->entries.end()) {
741 // All done. Return all of the responses.
742 context->original_callback.Run(CACHE_STORAGE_OK,
743 std::move(context->out_responses),
744 std::move(context->out_blob_data_handles));
745 return;
746 }
747
748 if (context->options.ignore_search) {
749 DCHECK(context->request);
750 disk_cache::Entry* entry(*iter);
751 if (RemoveQueryParam(context->request->url) !=
752 RemoveQueryParam(GURL(entry->GetKey()))) {
753 // In this case, we don't need to read data.
754 MatchAllProcessNextEntry(std::move(context), iter + 1);
755 return;
756 }
757 }
758
759 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata,
760 weak_ptr_factory_.GetWeakPtr(),
761 base::Passed(std::move(context)), iter));
762 }
763
764 void CacheStorageCache::MatchAllDidReadMetadata(
765 std::unique_ptr<MatchAllContext> context,
766 const Entries::iterator& iter,
767 std::unique_ptr<CacheMetadata> metadata) {
768 // Move ownership of the entry from the context.
769 disk_cache::ScopedEntryPtr entry(*iter);
770 *iter = nullptr;
771
772 if (!metadata) {
773 entry->Doom();
774 MatchAllProcessNextEntry(std::move(context), iter + 1);
775 return;
776 }
777
778 ServiceWorkerResponse response;
779 PopulateResponseMetadata(*metadata, &response);
780
781 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) {
782 context->out_responses->push_back(response);
783 MatchAllProcessNextEntry(std::move(context), iter + 1);
784 return;
785 }
786
787 if (!blob_storage_context_) {
788 context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE,
789 std::unique_ptr<Responses>(),
790 std::unique_ptr<BlobDataHandles>());
791 return;
792 }
793
794 std::unique_ptr<storage::BlobDataHandle> blob_data_handle =
795 PopulateResponseBody(std::move(entry), &response);
796
797 context->out_responses->push_back(response);
798 context->out_blob_data_handles->push_back(*blob_data_handle);
799 MatchAllProcessNextEntry(std::move(context), iter + 1);
800 } 809 }
801 810
802 void CacheStorageCache::WriteSideDataDidGetQuota( 811 void CacheStorageCache::WriteSideDataDidGetQuota(
803 const ErrorCallback& callback, 812 const ErrorCallback& callback,
804 const GURL& url, 813 const GURL& url,
805 base::Time expected_response_time, 814 base::Time expected_response_time,
806 scoped_refptr<net::IOBuffer> buffer, 815 scoped_refptr<net::IOBuffer> buffer,
807 int buf_len, 816 int buf_len,
808 storage::QuotaStatusCode status_code, 817 storage::QuotaStatusCode status_code,
809 int64_t usage, 818 int64_t usage,
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
1256 callback.Run(CACHE_STORAGE_OK); 1265 callback.Run(CACHE_STORAGE_OK);
1257 } 1266 }
1258 1267
1259 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) { 1268 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) {
1260 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); 1269 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_);
1261 if (backend_state_ != BACKEND_OPEN) { 1270 if (backend_state_ != BACKEND_OPEN) {
1262 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>()); 1271 callback.Run(CACHE_STORAGE_ERROR_STORAGE, std::unique_ptr<Requests>());
1263 return; 1272 return;
1264 } 1273 }
1265 1274
1266 // 1. Iterate through all of the entries, open them, and add them to a vector. 1275 std::unique_ptr<ServiceWorkerFetchRequest> request(
1267 // 2. For each open entry: 1276 new ServiceWorkerFetchRequest);
1268 // 2.1. Read the headers into a protobuf. 1277 QueryCache(std::move(request), CacheStorageCacheQueryParams(),
1269 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). 1278 base::Bind(&CacheStorageCache::KeysDidQueryCache,
1270 // 2.3. Push the response into a vector of requests to be returned. 1279 weak_ptr_factory_.GetWeakPtr(), callback));
1271 // 3. Return the vector of requests (keys).
1272
1273 // The entries have to be loaded into a vector first because enumeration loops
1274 // forever if you read data from a cache entry while enumerating.
1275
1276 OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries,
1277 weak_ptr_factory_.GetWeakPtr(), callback));
1278 } 1280 }
1279 1281
1280 void CacheStorageCache::KeysDidOpenAllEntries( 1282 void CacheStorageCache::KeysDidQueryCache(
1281 const RequestsCallback& callback, 1283 const RequestsCallback& callback,
1282 std::unique_ptr<OpenAllEntriesContext> entries_context, 1284 CacheStorageError error,
1283 CacheStorageError error) { 1285 std::unique_ptr<QueryCacheResults> query_cache_results) {
1284 if (error != CACHE_STORAGE_OK) { 1286 if (error != CACHE_STORAGE_OK) {
1285 callback.Run(error, std::unique_ptr<Requests>()); 1287 callback.Run(error, std::unique_ptr<Requests>());
1286 return; 1288 return;
1287 } 1289 }
1288 1290
1289 std::unique_ptr<KeysContext> keys_context(new KeysContext(callback)); 1291 callback.Run(CACHE_STORAGE_OK, std::move(query_cache_results->out_requests));
1290 keys_context->entries_context.swap(entries_context);
1291 Entries::iterator iter = keys_context->entries_context->entries.begin();
1292 KeysProcessNextEntry(std::move(keys_context), iter);
1293 }
1294
1295 void CacheStorageCache::KeysProcessNextEntry(
1296 std::unique_ptr<KeysContext> keys_context,
1297 const Entries::iterator& iter) {
1298 if (iter == keys_context->entries_context->entries.end()) {
1299 // All done. Return all of the keys.
1300 keys_context->original_callback.Run(CACHE_STORAGE_OK,
1301 std::move(keys_context->out_keys));
1302 return;
1303 }
1304
1305 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata,
1306 weak_ptr_factory_.GetWeakPtr(),
1307 base::Passed(std::move(keys_context)), iter));
1308 }
1309
1310 void CacheStorageCache::KeysDidReadMetadata(
1311 std::unique_ptr<KeysContext> keys_context,
1312 const Entries::iterator& iter,
1313 std::unique_ptr<CacheMetadata> metadata) {
1314 disk_cache::Entry* entry = *iter;
1315
1316 if (metadata) {
1317 keys_context->out_keys->push_back(ServiceWorkerFetchRequest(
1318 GURL(entry->GetKey()), metadata->request().method(),
1319 ServiceWorkerHeaderMap(), Referrer(), false));
1320
1321 ServiceWorkerHeaderMap& req_headers =
1322 keys_context->out_keys->back().headers;
1323
1324 for (int i = 0; i < metadata->request().headers_size(); ++i) {
1325 const CacheHeaderMap header = metadata->request().headers(i);
1326 DCHECK_EQ(std::string::npos, header.name().find('\0'));
1327 DCHECK_EQ(std::string::npos, header.value().find('\0'));
1328 req_headers.insert(std::make_pair(header.name(), header.value()));
1329 }
1330 } else {
1331 entry->Doom();
1332 }
1333
1334 KeysProcessNextEntry(std::move(keys_context), iter + 1);
1335 } 1292 }
1336 1293
1337 void CacheStorageCache::CloseImpl(const base::Closure& callback) { 1294 void CacheStorageCache::CloseImpl(const base::Closure& callback) {
1338 DCHECK_NE(BACKEND_CLOSED, backend_state_); 1295 DCHECK_NE(BACKEND_CLOSED, backend_state_);
1339 1296
1340 backend_state_ = BACKEND_CLOSED; 1297 backend_state_ = BACKEND_CLOSED;
1341 backend_.reset(); 1298 backend_.reset();
1342 callback.Run(); 1299 callback.Run();
1343 } 1300 }
1344 1301
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1434 backend_state_ == BACKEND_UNINITIALIZED) 1391 backend_state_ == BACKEND_UNINITIALIZED)
1435 ? BACKEND_OPEN 1392 ? BACKEND_OPEN
1436 : BACKEND_CLOSED; 1393 : BACKEND_CLOSED;
1437 1394
1438 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult", 1395 UMA_HISTOGRAM_ENUMERATION("ServiceWorkerCache.InitBackendResult",
1439 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1); 1396 cache_create_error, CACHE_STORAGE_ERROR_LAST + 1);
1440 1397
1441 callback.Run(); 1398 callback.Run();
1442 } 1399 }
1443 1400
1401 void CacheStorageCache::PopulateRequestFromMetadata(
1402 const CacheMetadata& metadata,
1403 GURL request_url,
nhiroki 2016/07/25 05:11:45 const GURL&
1404 ServiceWorkerFetchRequest* request) {
1405 *request =
1406 ServiceWorkerFetchRequest(request_url, metadata.request().method(),
1407 ServiceWorkerHeaderMap(), Referrer(), false);
1408
1409 for (int i = 0; i < metadata.request().headers_size(); ++i) {
1410 const CacheHeaderMap header = metadata.request().headers(i);
1411 DCHECK_EQ(std::string::npos, header.name().find('\0'));
1412 DCHECK_EQ(std::string::npos, header.value().find('\0'));
1413 request->headers.insert(std::make_pair(header.name(), header.value()));
1414 }
1415 }
1416
1444 void CacheStorageCache::PopulateResponseMetadata( 1417 void CacheStorageCache::PopulateResponseMetadata(
1445 const CacheMetadata& metadata, 1418 const CacheMetadata& metadata,
1446 ServiceWorkerResponse* response) { 1419 ServiceWorkerResponse* response) {
1447 *response = ServiceWorkerResponse( 1420 *response = ServiceWorkerResponse(
1448 GURL(metadata.response().url()), metadata.response().status_code(), 1421 GURL(metadata.response().url()), metadata.response().status_code(),
1449 metadata.response().status_text(), 1422 metadata.response().status_text(),
1450 ProtoResponseTypeToWebResponseType(metadata.response().response_type()), 1423 ProtoResponseTypeToWebResponseType(metadata.response().response_type()),
1451 ServiceWorkerHeaderMap(), "", 0, GURL(), 1424 ServiceWorkerHeaderMap(), "", 0, GURL(),
1452 blink::WebServiceWorkerResponseErrorUnknown, 1425 blink::WebServiceWorkerResponseErrorUnknown,
1453 base::Time::FromInternalValue(metadata.response().response_time()), 1426 base::Time::FromInternalValue(metadata.response().response_time()),
(...skipping 26 matching lines...) Expand all
1480 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA); 1453 temp_entry, INDEX_RESPONSE_BODY, INDEX_SIDE_DATA);
1481 return blob_storage_context_->AddFinishedBlob(&blob_data); 1454 return blob_storage_context_->AddFinishedBlob(&blob_data);
1482 } 1455 }
1483 1456
1484 std::unique_ptr<CacheStorageCacheHandle> 1457 std::unique_ptr<CacheStorageCacheHandle>
1485 CacheStorageCache::CreateCacheHandle() { 1458 CacheStorageCache::CreateCacheHandle() {
1486 return cache_storage_->CreateCacheHandle(this); 1459 return cache_storage_->CreateCacheHandle(this);
1487 } 1460 }
1488 1461
1489 } // namespace content 1462 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage_cache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698