| 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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 // If the header exists in one, it exists in both. Verify that the values | 137 // If the header exists in one, it exists in both. Verify that the values |
| 138 // are equal. | 138 // are equal. |
| 139 if (request_iter != request.end() && | 139 if (request_iter != request.end() && |
| 140 request_iter->second != cached_request_iter->second) | 140 request_iter->second != cached_request_iter->second) |
| 141 return false; | 141 return false; |
| 142 } | 142 } |
| 143 | 143 |
| 144 return true; | 144 return true; |
| 145 } | 145 } |
| 146 | 146 |
| 147 GURL RemoveQueryParam(const GURL& url) { |
| 148 url::Replacements<char> replacements; |
| 149 replacements.ClearQuery(); |
| 150 return url.ReplaceComponents(replacements); |
| 151 } |
| 152 |
| 147 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { | 153 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { |
| 148 DCHECK(entry); | 154 DCHECK(entry); |
| 149 | 155 |
| 150 scoped_refptr<net::IOBufferWithSize> buffer( | 156 scoped_refptr<net::IOBufferWithSize> buffer( |
| 151 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); | 157 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); |
| 152 | 158 |
| 153 net::CompletionCallback read_header_callback = | 159 net::CompletionCallback read_header_callback = |
| 154 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); | 160 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); |
| 155 | 161 |
| 156 int read_rv = entry->ReadData(INDEX_HEADERS, 0, buffer.get(), buffer->size(), | 162 int read_rv = entry->ReadData(INDEX_HEADERS, 0, buffer.get(), buffer->size(), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 // Used for enumerating cache entries. | 212 // Used for enumerating cache entries. |
| 207 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 213 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
| 208 disk_cache::Entry* enumerated_entry; | 214 disk_cache::Entry* enumerated_entry; |
| 209 | 215 |
| 210 private: | 216 private: |
| 211 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); | 217 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); |
| 212 }; | 218 }; |
| 213 | 219 |
| 214 // The state needed to pass between CacheStorageCache::MatchAll callbacks. | 220 // The state needed to pass between CacheStorageCache::MatchAll callbacks. |
| 215 struct CacheStorageCache::MatchAllContext { | 221 struct CacheStorageCache::MatchAllContext { |
| 216 explicit MatchAllContext(const CacheStorageCache::ResponsesCallback& callback) | 222 MatchAllContext(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 217 : original_callback(callback), | 223 const CacheStorageCacheQueryParams& match_params, |
| 224 const ResponsesCallback& callback) |
| 225 : request(std::move(request)), |
| 226 options(match_params), |
| 227 original_callback(callback), |
| 218 out_responses(new Responses), | 228 out_responses(new Responses), |
| 219 out_blob_data_handles(new BlobDataHandles) {} | 229 out_blob_data_handles(new BlobDataHandles) {} |
| 220 ~MatchAllContext() {} | 230 ~MatchAllContext() {} |
| 221 | 231 |
| 232 scoped_ptr<ServiceWorkerFetchRequest> request; |
| 233 |
| 234 CacheStorageCacheQueryParams options; |
| 235 |
| 222 // The callback passed to the MatchAll() function. | 236 // The callback passed to the MatchAll() function. |
| 223 ResponsesCallback original_callback; | 237 ResponsesCallback original_callback; |
| 224 | 238 |
| 225 // The outputs of the MatchAll function. | 239 // The outputs of the MatchAll function. |
| 226 scoped_ptr<Responses> out_responses; | 240 scoped_ptr<Responses> out_responses; |
| 227 scoped_ptr<BlobDataHandles> out_blob_data_handles; | 241 scoped_ptr<BlobDataHandles> out_blob_data_handles; |
| 228 | 242 |
| 229 // The context holding open entries. | 243 // The context holding open entries. |
| 230 scoped_ptr<OpenAllEntriesContext> entries_context; | 244 scoped_ptr<OpenAllEntriesContext> entries_context; |
| 231 | 245 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 } | 338 } |
| 325 | 339 |
| 326 ResponseCallback pending_callback = | 340 ResponseCallback pending_callback = |
| 327 base::Bind(&CacheStorageCache::PendingResponseCallback, | 341 base::Bind(&CacheStorageCache::PendingResponseCallback, |
| 328 weak_ptr_factory_.GetWeakPtr(), callback); | 342 weak_ptr_factory_.GetWeakPtr(), callback); |
| 329 scheduler_->ScheduleOperation( | 343 scheduler_->ScheduleOperation( |
| 330 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), | 344 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), |
| 331 base::Passed(std::move(request)), pending_callback)); | 345 base::Passed(std::move(request)), pending_callback)); |
| 332 } | 346 } |
| 333 | 347 |
| 334 void CacheStorageCache::MatchAll(const ResponsesCallback& callback) { | 348 void CacheStorageCache::MatchAll( |
| 349 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 350 const CacheStorageCacheQueryParams& match_params, |
| 351 const ResponsesCallback& callback) { |
| 335 if (!LazyInitialize()) { | 352 if (!LazyInitialize()) { |
| 336 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), | 353 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), |
| 337 scoped_ptr<BlobDataHandles>()); | 354 scoped_ptr<BlobDataHandles>()); |
| 338 return; | 355 return; |
| 339 } | 356 } |
| 340 | 357 |
| 341 ResponsesCallback pending_callback = | 358 ResponsesCallback pending_callback = |
| 342 base::Bind(&CacheStorageCache::PendingResponsesCallback, | 359 base::Bind(&CacheStorageCache::PendingResponsesCallback, |
| 343 weak_ptr_factory_.GetWeakPtr(), callback); | 360 weak_ptr_factory_.GetWeakPtr(), callback); |
| 361 |
| 362 scoped_ptr<MatchAllContext> context( |
| 363 new MatchAllContext(std::move(request), match_params, pending_callback)); |
| 344 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, | 364 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, |
| 345 weak_ptr_factory_.GetWeakPtr(), | 365 weak_ptr_factory_.GetWeakPtr(), |
| 346 pending_callback)); | 366 base::Passed(std::move(context)))); |
| 347 } | 367 } |
| 348 | 368 |
| 349 void CacheStorageCache::BatchOperation( | 369 void CacheStorageCache::BatchOperation( |
| 350 const std::vector<CacheStorageBatchOperation>& operations, | 370 const std::vector<CacheStorageBatchOperation>& operations, |
| 351 const ErrorCallback& callback) { | 371 const ErrorCallback& callback) { |
| 352 if (!LazyInitialize()) { | 372 if (!LazyInitialize()) { |
| 353 callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 373 callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 354 return; | 374 return; |
| 355 } | 375 } |
| 356 | 376 |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 scoped_ptr<storage::BlobDataHandle>()); | 652 scoped_ptr<storage::BlobDataHandle>()); |
| 633 return; | 653 return; |
| 634 } | 654 } |
| 635 | 655 |
| 636 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 656 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
| 637 PopulateResponseBody(std::move(entry), response.get()); | 657 PopulateResponseBody(std::move(entry), response.get()); |
| 638 callback.Run(CACHE_STORAGE_OK, std::move(response), | 658 callback.Run(CACHE_STORAGE_OK, std::move(response), |
| 639 std::move(blob_data_handle)); | 659 std::move(blob_data_handle)); |
| 640 } | 660 } |
| 641 | 661 |
| 642 void CacheStorageCache::MatchAllImpl(const ResponsesCallback& callback) { | 662 void CacheStorageCache::MatchAllImpl(scoped_ptr<MatchAllContext> context) { |
| 643 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 663 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
| 644 if (backend_state_ != BACKEND_OPEN) { | 664 if (backend_state_ != BACKEND_OPEN) { |
| 645 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), | 665 context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
| 646 scoped_ptr<BlobDataHandles>()); | 666 scoped_ptr<Responses>(), |
| 667 scoped_ptr<BlobDataHandles>()); |
| 647 return; | 668 return; |
| 648 } | 669 } |
| 649 | 670 |
| 650 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, | 671 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, |
| 651 weak_ptr_factory_.GetWeakPtr(), callback)); | 672 weak_ptr_factory_.GetWeakPtr(), |
| 673 base::Passed(std::move(context)))); |
| 652 } | 674 } |
| 653 | 675 |
| 654 void CacheStorageCache::MatchAllDidOpenAllEntries( | 676 void CacheStorageCache::MatchAllDidOpenAllEntries( |
| 655 const ResponsesCallback& callback, | 677 scoped_ptr<MatchAllContext> context, |
| 656 scoped_ptr<OpenAllEntriesContext> entries_context, | 678 scoped_ptr<OpenAllEntriesContext> entries_context, |
| 657 CacheStorageError error) { | 679 CacheStorageError error) { |
| 658 if (error != CACHE_STORAGE_OK) { | 680 if (error != CACHE_STORAGE_OK) { |
| 659 callback.Run(error, scoped_ptr<Responses>(), scoped_ptr<BlobDataHandles>()); | 681 context->original_callback.Run(error, scoped_ptr<Responses>(), |
| 682 scoped_ptr<BlobDataHandles>()); |
| 660 return; | 683 return; |
| 661 } | 684 } |
| 662 | 685 |
| 663 scoped_ptr<MatchAllContext> context(new MatchAllContext(callback)); | |
| 664 context->entries_context.swap(entries_context); | 686 context->entries_context.swap(entries_context); |
| 665 Entries::iterator iter = context->entries_context->entries.begin(); | 687 Entries::iterator iter = context->entries_context->entries.begin(); |
| 666 MatchAllProcessNextEntry(std::move(context), iter); | 688 MatchAllProcessNextEntry(std::move(context), iter); |
| 667 } | 689 } |
| 668 | 690 |
| 669 void CacheStorageCache::MatchAllProcessNextEntry( | 691 void CacheStorageCache::MatchAllProcessNextEntry( |
| 670 scoped_ptr<MatchAllContext> context, | 692 scoped_ptr<MatchAllContext> context, |
| 671 const Entries::iterator& iter) { | 693 const Entries::iterator& iter) { |
| 672 if (iter == context->entries_context->entries.end()) { | 694 if (iter == context->entries_context->entries.end()) { |
| 673 // All done. Return all of the responses. | 695 // All done. Return all of the responses. |
| 674 context->original_callback.Run(CACHE_STORAGE_OK, | 696 context->original_callback.Run(CACHE_STORAGE_OK, |
| 675 std::move(context->out_responses), | 697 std::move(context->out_responses), |
| 676 std::move(context->out_blob_data_handles)); | 698 std::move(context->out_blob_data_handles)); |
| 677 return; | 699 return; |
| 678 } | 700 } |
| 679 | 701 |
| 702 if (context->options.ignore_search) { |
| 703 DCHECK(context->request); |
| 704 disk_cache::Entry* entry(*iter); |
| 705 if (RemoveQueryParam(context->request->url) != |
| 706 RemoveQueryParam(GURL(entry->GetKey()))) { |
| 707 // In this case, we don't need to read data. |
| 708 MatchAllProcessNextEntry(std::move(context), iter + 1); |
| 709 return; |
| 710 } |
| 711 } |
| 712 |
| 680 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata, | 713 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata, |
| 681 weak_ptr_factory_.GetWeakPtr(), | 714 weak_ptr_factory_.GetWeakPtr(), |
| 682 base::Passed(std::move(context)), iter)); | 715 base::Passed(std::move(context)), iter)); |
| 683 } | 716 } |
| 684 | 717 |
| 685 void CacheStorageCache::MatchAllDidReadMetadata( | 718 void CacheStorageCache::MatchAllDidReadMetadata( |
| 686 scoped_ptr<MatchAllContext> context, | 719 scoped_ptr<MatchAllContext> context, |
| 687 const Entries::iterator& iter, | 720 const Entries::iterator& iter, |
| 688 scoped_ptr<CacheMetadata> metadata) { | 721 scoped_ptr<CacheMetadata> metadata) { |
| 689 // Move ownership of the entry from the context. | 722 // Move ownership of the entry from the context. |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1313 storage::BlobDataBuilder blob_data(response->blob_uuid); | 1346 storage::BlobDataBuilder blob_data(response->blob_uuid); |
| 1314 | 1347 |
| 1315 disk_cache::Entry* temp_entry = entry.get(); | 1348 disk_cache::Entry* temp_entry = entry.get(); |
| 1316 blob_data.AppendDiskCacheEntry( | 1349 blob_data.AppendDiskCacheEntry( |
| 1317 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 1350 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, |
| 1318 INDEX_RESPONSE_BODY); | 1351 INDEX_RESPONSE_BODY); |
| 1319 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1352 return blob_storage_context_->AddFinishedBlob(&blob_data); |
| 1320 } | 1353 } |
| 1321 | 1354 |
| 1322 } // namespace content | 1355 } // namespace content |
| OLD | NEW |