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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 // If the header exists in one, it exists in both. Verify that the values | 140 // If the header exists in one, it exists in both. Verify that the values |
141 // are equal. | 141 // are equal. |
142 if (request_iter != request.end() && | 142 if (request_iter != request.end() && |
143 request_iter->second != cached_request_iter->second) | 143 request_iter->second != cached_request_iter->second) |
144 return false; | 144 return false; |
145 } | 145 } |
146 | 146 |
147 return true; | 147 return true; |
148 } | 148 } |
149 | 149 |
150 bool URLMatchWithoutQuery(const GURL& request_url, const GURL& key) { | |
151 return request_url.scheme() == key.scheme() && | |
152 request_url.host() == key.host() && request_url.path() == key.path(); | |
nhiroki
2016/01/18 04:29:58
This match algorithm could be insufficient, for ex
zino
2016/01/19 13:46:38
I modified it but I couldn't write a test for it.
nhiroki
2016/01/20 08:56:02
Yes, the constructor rejects such a URL.
A Reques
| |
153 } | |
154 | |
150 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { | 155 void ReadMetadata(disk_cache::Entry* entry, const MetadataCallback& callback) { |
151 DCHECK(entry); | 156 DCHECK(entry); |
152 | 157 |
153 scoped_refptr<net::IOBufferWithSize> buffer( | 158 scoped_refptr<net::IOBufferWithSize> buffer( |
154 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); | 159 new net::IOBufferWithSize(entry->GetDataSize(INDEX_HEADERS))); |
155 | 160 |
156 net::CompletionCallback read_header_callback = | 161 net::CompletionCallback read_header_callback = |
157 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); | 162 base::Bind(ReadMetadataDidReadMetadata, entry, callback, buffer); |
158 | 163 |
159 int read_rv = entry->ReadData(INDEX_HEADERS, 0, buffer.get(), buffer->size(), | 164 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... | |
203 // Used for enumerating cache entries. | 208 // Used for enumerating cache entries. |
204 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 209 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
205 disk_cache::Entry* enumerated_entry; | 210 disk_cache::Entry* enumerated_entry; |
206 | 211 |
207 private: | 212 private: |
208 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); | 213 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); |
209 }; | 214 }; |
210 | 215 |
211 // The state needed to pass between CacheStorageCache::MatchAll callbacks. | 216 // The state needed to pass between CacheStorageCache::MatchAll callbacks. |
212 struct CacheStorageCache::MatchAllContext { | 217 struct CacheStorageCache::MatchAllContext { |
213 explicit MatchAllContext(const CacheStorageCache::ResponsesCallback& callback) | 218 explicit MatchAllContext(scoped_ptr<ServiceWorkerFetchRequest> request, |
nhiroki
2016/01/18 04:29:58
Remove "explicit"
zino
2016/01/19 13:46:38
Done.
| |
214 : original_callback(callback), | 219 const CacheStorageCache::ResponsesCallback& callback) |
nhiroki
2016/01/18 04:29:58
"CacheStorageCache::" prefix is not necessary.
zino
2016/01/19 13:46:38
Done.
| |
220 : request(std::move(request)), | |
221 original_callback(callback), | |
215 out_responses(new Responses), | 222 out_responses(new Responses), |
216 out_blob_data_handles(new BlobDataHandles) {} | 223 out_blob_data_handles(new BlobDataHandles) {} |
217 ~MatchAllContext() {} | 224 ~MatchAllContext() {} |
218 | 225 |
226 scoped_ptr<ServiceWorkerFetchRequest> request; | |
227 | |
219 // The callback passed to the MatchAll() function. | 228 // The callback passed to the MatchAll() function. |
220 ResponsesCallback original_callback; | 229 ResponsesCallback original_callback; |
221 | 230 |
222 // The outputs of the MatchAll function. | 231 // The outputs of the MatchAll function. |
223 scoped_ptr<Responses> out_responses; | 232 scoped_ptr<Responses> out_responses; |
224 scoped_ptr<BlobDataHandles> out_blob_data_handles; | 233 scoped_ptr<BlobDataHandles> out_blob_data_handles; |
225 | 234 |
226 // The context holding open entries. | 235 // The context holding open entries. |
227 scoped_ptr<OpenAllEntriesContext> entries_context; | 236 scoped_ptr<OpenAllEntriesContext> entries_context; |
228 | 237 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 } | 329 } |
321 | 330 |
322 ResponseCallback pending_callback = | 331 ResponseCallback pending_callback = |
323 base::Bind(&CacheStorageCache::PendingResponseCallback, | 332 base::Bind(&CacheStorageCache::PendingResponseCallback, |
324 weak_ptr_factory_.GetWeakPtr(), callback); | 333 weak_ptr_factory_.GetWeakPtr(), callback); |
325 scheduler_->ScheduleOperation( | 334 scheduler_->ScheduleOperation( |
326 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), | 335 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), |
327 base::Passed(std::move(request)), pending_callback)); | 336 base::Passed(std::move(request)), pending_callback)); |
328 } | 337 } |
329 | 338 |
330 void CacheStorageCache::MatchAll(const ResponsesCallback& callback) { | 339 void CacheStorageCache::MatchAll(const ResponsesCallback& callback) { |
nhiroki
2016/01/18 04:29:59
Could you remove this indirection and directly cal
zino
2016/01/19 13:46:38
Done.
| |
340 MatchAll(nullptr, callback); | |
341 } | |
342 | |
343 void CacheStorageCache::MatchAll(scoped_ptr<ServiceWorkerFetchRequest> request, | |
344 const ResponsesCallback& callback) { | |
331 if (!LazyInitialize()) { | 345 if (!LazyInitialize()) { |
332 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), | 346 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), |
333 scoped_ptr<BlobDataHandles>()); | 347 scoped_ptr<BlobDataHandles>()); |
334 return; | 348 return; |
335 } | 349 } |
336 | 350 |
337 ResponsesCallback pending_callback = | 351 ResponsesCallback pending_callback = |
338 base::Bind(&CacheStorageCache::PendingResponsesCallback, | 352 base::Bind(&CacheStorageCache::PendingResponsesCallback, |
339 weak_ptr_factory_.GetWeakPtr(), callback); | 353 weak_ptr_factory_.GetWeakPtr(), callback); |
340 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, | 354 scheduler_->ScheduleOperation(base::Bind( |
341 weak_ptr_factory_.GetWeakPtr(), | 355 &CacheStorageCache::MatchAllImpl, weak_ptr_factory_.GetWeakPtr(), |
342 pending_callback)); | 356 base::Passed(std::move(request)), pending_callback)); |
343 } | 357 } |
344 | 358 |
345 void CacheStorageCache::BatchOperation( | 359 void CacheStorageCache::BatchOperation( |
346 const std::vector<CacheStorageBatchOperation>& operations, | 360 const std::vector<CacheStorageBatchOperation>& operations, |
347 const ErrorCallback& callback) { | 361 const ErrorCallback& callback) { |
348 if (!LazyInitialize()) { | 362 if (!LazyInitialize()) { |
349 callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 363 callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
350 return; | 364 return; |
351 } | 365 } |
352 | 366 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
631 scoped_ptr<storage::BlobDataHandle>()); | 645 scoped_ptr<storage::BlobDataHandle>()); |
632 return; | 646 return; |
633 } | 647 } |
634 | 648 |
635 scoped_ptr<storage::BlobDataHandle> blob_data_handle = | 649 scoped_ptr<storage::BlobDataHandle> blob_data_handle = |
636 PopulateResponseBody(std::move(entry), response.get()); | 650 PopulateResponseBody(std::move(entry), response.get()); |
637 callback.Run(CACHE_STORAGE_OK, std::move(response), | 651 callback.Run(CACHE_STORAGE_OK, std::move(response), |
638 std::move(blob_data_handle)); | 652 std::move(blob_data_handle)); |
639 } | 653 } |
640 | 654 |
641 void CacheStorageCache::MatchAllImpl(const ResponsesCallback& callback) { | 655 void CacheStorageCache::MatchAllImpl( |
656 scoped_ptr<ServiceWorkerFetchRequest> request, | |
657 const ResponsesCallback& callback) { | |
642 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 658 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
643 if (backend_state_ != BACKEND_OPEN) { | 659 if (backend_state_ != BACKEND_OPEN) { |
644 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), | 660 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Responses>(), |
645 scoped_ptr<BlobDataHandles>()); | 661 scoped_ptr<BlobDataHandles>()); |
646 return; | 662 return; |
647 } | 663 } |
648 | 664 |
649 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, | 665 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, |
650 weak_ptr_factory_.GetWeakPtr(), callback)); | 666 weak_ptr_factory_.GetWeakPtr(), |
667 base::Passed(std::move(request)), callback)); | |
651 } | 668 } |
652 | 669 |
653 void CacheStorageCache::MatchAllDidOpenAllEntries( | 670 void CacheStorageCache::MatchAllDidOpenAllEntries( |
671 scoped_ptr<ServiceWorkerFetchRequest> request, | |
654 const ResponsesCallback& callback, | 672 const ResponsesCallback& callback, |
655 scoped_ptr<OpenAllEntriesContext> entries_context, | 673 scoped_ptr<OpenAllEntriesContext> entries_context, |
656 CacheStorageError error) { | 674 CacheStorageError error) { |
657 if (error != CACHE_STORAGE_OK) { | 675 if (error != CACHE_STORAGE_OK) { |
658 callback.Run(error, scoped_ptr<Responses>(), scoped_ptr<BlobDataHandles>()); | 676 callback.Run(error, scoped_ptr<Responses>(), scoped_ptr<BlobDataHandles>()); |
659 return; | 677 return; |
660 } | 678 } |
661 | 679 |
662 scoped_ptr<MatchAllContext> context(new MatchAllContext(callback)); | 680 scoped_ptr<MatchAllContext> context( |
681 new MatchAllContext(std::move(request), callback)); | |
663 context->entries_context.swap(entries_context); | 682 context->entries_context.swap(entries_context); |
664 Entries::iterator iter = context->entries_context->entries.begin(); | 683 Entries::iterator iter = context->entries_context->entries.begin(); |
665 MatchAllProcessNextEntry(std::move(context), iter); | 684 MatchAllProcessNextEntry(std::move(context), iter); |
666 } | 685 } |
667 | 686 |
668 void CacheStorageCache::MatchAllProcessNextEntry( | 687 void CacheStorageCache::MatchAllProcessNextEntry( |
669 scoped_ptr<MatchAllContext> context, | 688 scoped_ptr<MatchAllContext> context, |
670 const Entries::iterator& iter) { | 689 const Entries::iterator& iter) { |
671 if (iter == context->entries_context->entries.end()) { | 690 if (iter == context->entries_context->entries.end()) { |
672 // All done. Return all of the responses. | 691 // All done. Return all of the responses. |
673 context->original_callback.Run(CACHE_STORAGE_OK, | 692 context->original_callback.Run(CACHE_STORAGE_OK, |
674 std::move(context->out_responses), | 693 std::move(context->out_responses), |
675 std::move(context->out_blob_data_handles)); | 694 std::move(context->out_blob_data_handles)); |
676 return; | 695 return; |
677 } | 696 } |
678 | 697 |
679 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata, | 698 disk_cache::Entry* entry(*iter); |
680 weak_ptr_factory_.GetWeakPtr(), | 699 if (!context->request || |
681 base::Passed(std::move(context)), iter)); | 700 URLMatchWithoutQuery(context->request->url, GURL(entry->GetKey()))) { |
nhiroki
2016/01/18 04:29:59
This combined condition is a bit confusing to me.
zino
2016/01/19 13:46:38
Done.
| |
701 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata, | |
702 weak_ptr_factory_.GetWeakPtr(), | |
703 base::Passed(std::move(context)), iter)); | |
704 return; | |
705 } | |
706 | |
707 MatchAllProcessNextEntry(std::move(context), iter + 1); | |
682 } | 708 } |
683 | 709 |
684 void CacheStorageCache::MatchAllDidReadMetadata( | 710 void CacheStorageCache::MatchAllDidReadMetadata( |
685 scoped_ptr<MatchAllContext> context, | 711 scoped_ptr<MatchAllContext> context, |
686 const Entries::iterator& iter, | 712 const Entries::iterator& iter, |
687 scoped_ptr<CacheMetadata> metadata) { | 713 scoped_ptr<CacheMetadata> metadata) { |
688 // Move ownership of the entry from the context. | 714 // Move ownership of the entry from the context. |
689 disk_cache::ScopedEntryPtr entry(*iter); | 715 disk_cache::ScopedEntryPtr entry(*iter); |
690 *iter = nullptr; | 716 *iter = nullptr; |
691 | 717 |
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1256 storage::BlobDataBuilder blob_data(response->blob_uuid); | 1282 storage::BlobDataBuilder blob_data(response->blob_uuid); |
1257 | 1283 |
1258 disk_cache::Entry* temp_entry = entry.get(); | 1284 disk_cache::Entry* temp_entry = entry.get(); |
1259 blob_data.AppendDiskCacheEntry( | 1285 blob_data.AppendDiskCacheEntry( |
1260 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 1286 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, |
1261 INDEX_RESPONSE_BODY); | 1287 INDEX_RESPONSE_BODY); |
1262 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1288 return blob_storage_context_->AddFinishedBlob(&blob_data); |
1263 } | 1289 } |
1264 | 1290 |
1265 } // namespace content | 1291 } // namespace content |
OLD | NEW |