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