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 <string> | 7 #include <string> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 scoped_ptr<CacheMetadata> metadata(new CacheMetadata()); | 169 scoped_ptr<CacheMetadata> metadata(new CacheMetadata()); |
170 | 170 |
171 if (!metadata->ParseFromArray(buffer->data(), buffer->size())) { | 171 if (!metadata->ParseFromArray(buffer->data(), buffer->size())) { |
172 callback.Run(scoped_ptr<CacheMetadata>()); | 172 callback.Run(scoped_ptr<CacheMetadata>()); |
173 return; | 173 return; |
174 } | 174 } |
175 | 175 |
176 callback.Run(metadata.Pass()); | 176 callback.Run(metadata.Pass()); |
177 } | 177 } |
178 | 178 |
179 void PopulateServiceWorkerResponse(const CacheMetadata& metadata, | |
jkarlin
2015/08/05 12:09:05
Perhaps rename to PopularResponseMetadata or Popul
nhiroki
2015/08/06 03:36:53
Good point. Renamed this.
| |
180 ServiceWorkerResponse* response) { | |
181 *response = ServiceWorkerResponse( | |
182 GURL(metadata.response().url()), metadata.response().status_code(), | |
183 metadata.response().status_text(), | |
184 ProtoResponseTypeToWebResponseType(metadata.response().response_type()), | |
185 ServiceWorkerHeaderMap(), "", 0, GURL(), | |
186 blink::WebServiceWorkerResponseErrorUnknown); | |
187 | |
188 for (int i = 0; i < metadata.response().headers_size(); ++i) { | |
189 const CacheHeaderMap header = metadata.response().headers(i); | |
190 DCHECK_EQ(std::string::npos, header.name().find('\0')); | |
191 DCHECK_EQ(std::string::npos, header.value().find('\0')); | |
192 response->headers.insert(std::make_pair(header.name(), header.value())); | |
193 } | |
194 } | |
195 | |
179 } // namespace | 196 } // namespace |
180 | 197 |
181 // The state needed to pass between CacheStorageCache::Keys callbacks. | 198 // The state needed to iterate all entries in the cache. |
182 struct CacheStorageCache::KeysContext { | 199 struct CacheStorageCache::OpenAllEntriesContext { |
183 explicit KeysContext(const CacheStorageCache::RequestsCallback& callback) | 200 OpenAllEntriesContext() : enumerated_entry(nullptr) {} |
184 : original_callback(callback), | 201 ~OpenAllEntriesContext() { |
185 out_keys(new CacheStorageCache::Requests()), | 202 for (size_t i = 0, max = entries.size(); i < max; ++i) { |
186 enumerated_entry(NULL) {} | 203 if (entries[i]) |
187 | 204 entries[i]->Close(); |
188 ~KeysContext() { | 205 if (enumerated_entry) |
jkarlin
2015/08/05 12:09:05
Why is this in the for loop now?
nhiroki
2015/08/06 03:36:53
Oh..., this is just a mistake. Fixed. Thanks!
| |
189 for (size_t i = 0, max = entries.size(); i < max; ++i) | 206 enumerated_entry->Close(); |
190 entries[i]->Close(); | 207 } |
191 if (enumerated_entry) | |
192 enumerated_entry->Close(); | |
193 } | 208 } |
194 | 209 |
195 // The callback passed to the Keys() function. | |
196 CacheStorageCache::RequestsCallback original_callback; | |
197 | |
198 // The vector of open entries in the backend. | 210 // The vector of open entries in the backend. |
199 Entries entries; | 211 Entries entries; |
200 | 212 |
201 // The output of the Keys function. | |
202 scoped_ptr<CacheStorageCache::Requests> out_keys; | |
203 | |
204 // Used for enumerating cache entries. | 213 // Used for enumerating cache entries. |
205 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; | 214 scoped_ptr<disk_cache::Backend::Iterator> backend_iterator; |
206 disk_cache::Entry* enumerated_entry; | 215 disk_cache::Entry* enumerated_entry; |
207 | 216 |
208 private: | 217 private: |
218 DISALLOW_COPY_AND_ASSIGN(OpenAllEntriesContext); | |
219 }; | |
220 | |
221 // The state needed to pass between CacheStorageCache::MatchAll callbacks. | |
222 struct CacheStorageCache::MatchAllContext { | |
223 explicit MatchAllContext(const CacheStorageCache::ResponsesCallback& callback) | |
224 : original_callback(callback) {} | |
225 ~MatchAllContext() {} | |
226 | |
227 // The callback passed to the MatchAll() function. | |
228 CacheStorageCache::ResponsesCallback original_callback; | |
229 | |
230 // The outputs of the MatchAll function. | |
231 std::vector<ServiceWorkerResponse> out_responses; | |
jkarlin
2015/08/05 12:09:05
scoped_ptr<std::vector<ServiceWorkerResponse>> to
nhiroki
2015/08/06 03:36:53
Done.
| |
232 ScopedVector<storage::BlobDataHandle> out_blob_data_handles; | |
233 | |
234 // The context holding open entries. | |
235 scoped_ptr<OpenAllEntriesContext> entries_context; | |
236 | |
237 private: | |
238 DISALLOW_COPY_AND_ASSIGN(MatchAllContext); | |
239 }; | |
240 | |
241 // The state needed to pass between CacheStorageCache::Keys callbacks. | |
242 struct CacheStorageCache::KeysContext { | |
243 explicit KeysContext(const CacheStorageCache::RequestsCallback& callback) | |
244 : original_callback(callback), | |
245 out_keys(new CacheStorageCache::Requests()) {} | |
246 ~KeysContext() {} | |
247 | |
248 // The callback passed to the Keys() function. | |
249 CacheStorageCache::RequestsCallback original_callback; | |
250 | |
251 // The output of the Keys function. | |
252 scoped_ptr<CacheStorageCache::Requests> out_keys; | |
253 | |
254 // The context holding open entries. | |
255 scoped_ptr<OpenAllEntriesContext> entries_context; | |
256 | |
257 private: | |
209 DISALLOW_COPY_AND_ASSIGN(KeysContext); | 258 DISALLOW_COPY_AND_ASSIGN(KeysContext); |
210 }; | 259 }; |
211 | 260 |
212 // The state needed to pass between CacheStorageCache::Put callbacks. | 261 // The state needed to pass between CacheStorageCache::Put callbacks. |
213 struct CacheStorageCache::PutContext { | 262 struct CacheStorageCache::PutContext { |
214 PutContext( | 263 PutContext( |
215 const GURL& origin, | 264 const GURL& origin, |
216 scoped_ptr<ServiceWorkerFetchRequest> request, | 265 scoped_ptr<ServiceWorkerFetchRequest> request, |
217 scoped_ptr<ServiceWorkerResponse> response, | 266 scoped_ptr<ServiceWorkerResponse> response, |
218 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 267 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 } | 329 } |
281 | 330 |
282 ResponseCallback pending_callback = | 331 ResponseCallback pending_callback = |
283 base::Bind(&CacheStorageCache::PendingResponseCallback, | 332 base::Bind(&CacheStorageCache::PendingResponseCallback, |
284 weak_ptr_factory_.GetWeakPtr(), callback); | 333 weak_ptr_factory_.GetWeakPtr(), callback); |
285 scheduler_->ScheduleOperation( | 334 scheduler_->ScheduleOperation( |
286 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), | 335 base::Bind(&CacheStorageCache::MatchImpl, weak_ptr_factory_.GetWeakPtr(), |
287 base::Passed(request.Pass()), pending_callback)); | 336 base::Passed(request.Pass()), pending_callback)); |
288 } | 337 } |
289 | 338 |
339 void CacheStorageCache::MatchAll(const ResponsesCallback& callback) { | |
340 if (!LazyInitialize()) { | |
341 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
342 std::vector<ServiceWorkerResponse>(), | |
343 ScopedVector<storage::BlobDataHandle>()); | |
344 return; | |
345 } | |
346 | |
347 ResponsesCallback pending_callback = | |
348 base::Bind(&CacheStorageCache::PendingResponsesCallback, | |
349 weak_ptr_factory_.GetWeakPtr(), callback); | |
350 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::MatchAllImpl, | |
351 weak_ptr_factory_.GetWeakPtr(), | |
352 pending_callback)); | |
353 } | |
354 | |
290 void CacheStorageCache::BatchOperation( | 355 void CacheStorageCache::BatchOperation( |
291 const std::vector<CacheStorageBatchOperation>& operations, | 356 const std::vector<CacheStorageBatchOperation>& operations, |
292 const ErrorCallback& callback) { | 357 const ErrorCallback& callback) { |
293 if (!LazyInitialize()) { | 358 if (!LazyInitialize()) { |
294 callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 359 callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
295 return; | 360 return; |
296 } | 361 } |
297 | 362 |
298 scoped_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback)); | 363 scoped_ptr<ErrorCallback> callback_copy(new ErrorCallback(callback)); |
299 ErrorCallback* callback_ptr = callback_copy.get(); | 364 ErrorCallback* callback_ptr = callback_copy.get(); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 case BACKEND_CLOSED: | 490 case BACKEND_CLOSED: |
426 return false; | 491 return false; |
427 case BACKEND_OPEN: | 492 case BACKEND_OPEN: |
428 DCHECK(backend_); | 493 DCHECK(backend_); |
429 return true; | 494 return true; |
430 } | 495 } |
431 NOTREACHED(); | 496 NOTREACHED(); |
432 return false; | 497 return false; |
433 } | 498 } |
434 | 499 |
500 void CacheStorageCache::OpenAllEntries(const OpenAllEntriesCallback& callback) { | |
501 scoped_ptr<OpenAllEntriesContext> entries_context(new OpenAllEntriesContext); | |
502 entries_context->backend_iterator = backend_->CreateIterator(); | |
503 disk_cache::Backend::Iterator& iterator = *entries_context->backend_iterator; | |
504 disk_cache::Entry** enumerated_entry = &entries_context->enumerated_entry; | |
505 | |
506 net::CompletionCallback open_entry_callback = base::Bind( | |
507 &CacheStorageCache::DidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), | |
508 base::Passed(entries_context.Pass()), callback); | |
509 | |
510 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | |
511 | |
512 if (rv != net::ERR_IO_PENDING) | |
513 open_entry_callback.Run(rv); | |
514 } | |
515 | |
516 void CacheStorageCache::DidOpenNextEntry( | |
517 scoped_ptr<OpenAllEntriesContext> entries_context, | |
518 const OpenAllEntriesCallback& callback, | |
519 int rv) { | |
520 if (rv == net::ERR_FAILED) { | |
521 DCHECK(!entries_context->enumerated_entry); | |
522 // Enumeration is complete, extract the requests from the entries. | |
523 callback.Run(entries_context.Pass(), CACHE_STORAGE_OK); | |
524 return; | |
525 } | |
526 | |
527 if (rv < 0) { | |
528 callback.Run(entries_context.Pass(), CACHE_STORAGE_ERROR_STORAGE); | |
529 return; | |
530 } | |
531 | |
532 if (backend_state_ != BACKEND_OPEN) { | |
533 callback.Run(entries_context.Pass(), CACHE_STORAGE_ERROR_NOT_FOUND); | |
534 return; | |
535 } | |
536 | |
537 // Store the entry. | |
538 entries_context->entries.push_back(entries_context->enumerated_entry); | |
539 entries_context->enumerated_entry = nullptr; | |
540 | |
541 // Enumerate the next entry. | |
542 disk_cache::Backend::Iterator& iterator = *entries_context->backend_iterator; | |
543 disk_cache::Entry** enumerated_entry = &entries_context->enumerated_entry; | |
544 net::CompletionCallback open_entry_callback = base::Bind( | |
545 &CacheStorageCache::DidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), | |
546 base::Passed(entries_context.Pass()), callback); | |
547 | |
548 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | |
549 | |
550 if (rv != net::ERR_IO_PENDING) | |
551 open_entry_callback.Run(rv); | |
552 } | |
553 | |
435 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 554 void CacheStorageCache::MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
436 const ResponseCallback& callback) { | 555 const ResponseCallback& callback) { |
437 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 556 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
438 if (backend_state_ != BACKEND_OPEN) { | 557 if (backend_state_ != BACKEND_OPEN) { |
439 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 558 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
440 scoped_ptr<ServiceWorkerResponse>(), | 559 scoped_ptr<ServiceWorkerResponse>(), |
441 scoped_ptr<storage::BlobDataHandle>()); | 560 scoped_ptr<storage::BlobDataHandle>()); |
442 return; | 561 return; |
443 } | 562 } |
444 | 563 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
482 const ResponseCallback& callback, | 601 const ResponseCallback& callback, |
483 disk_cache::ScopedEntryPtr entry, | 602 disk_cache::ScopedEntryPtr entry, |
484 scoped_ptr<CacheMetadata> metadata) { | 603 scoped_ptr<CacheMetadata> metadata) { |
485 if (!metadata) { | 604 if (!metadata) { |
486 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 605 callback.Run(CACHE_STORAGE_ERROR_STORAGE, |
487 scoped_ptr<ServiceWorkerResponse>(), | 606 scoped_ptr<ServiceWorkerResponse>(), |
488 scoped_ptr<storage::BlobDataHandle>()); | 607 scoped_ptr<storage::BlobDataHandle>()); |
489 return; | 608 return; |
490 } | 609 } |
491 | 610 |
492 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse( | 611 scoped_ptr<ServiceWorkerResponse> response(new ServiceWorkerResponse); |
493 request->url, metadata->response().status_code(), | 612 PopulateServiceWorkerResponse(*metadata, response.get()); |
494 metadata->response().status_text(), | |
495 ProtoResponseTypeToWebResponseType(metadata->response().response_type()), | |
496 ServiceWorkerHeaderMap(), "", 0, GURL(), | |
497 blink::WebServiceWorkerResponseErrorUnknown)); | |
498 | |
499 if (metadata->response().has_url()) | |
500 response->url = GURL(metadata->response().url()); | |
501 | |
502 for (int i = 0; i < metadata->response().headers_size(); ++i) { | |
503 const CacheHeaderMap header = metadata->response().headers(i); | |
504 DCHECK_EQ(std::string::npos, header.name().find('\0')); | |
505 DCHECK_EQ(std::string::npos, header.value().find('\0')); | |
506 response->headers.insert(std::make_pair(header.name(), header.value())); | |
507 } | |
508 | 613 |
509 ServiceWorkerHeaderMap cached_request_headers; | 614 ServiceWorkerHeaderMap cached_request_headers; |
510 for (int i = 0; i < metadata->request().headers_size(); ++i) { | 615 for (int i = 0; i < metadata->request().headers_size(); ++i) { |
511 const CacheHeaderMap header = metadata->request().headers(i); | 616 const CacheHeaderMap header = metadata->request().headers(i); |
512 DCHECK_EQ(std::string::npos, header.name().find('\0')); | 617 DCHECK_EQ(std::string::npos, header.name().find('\0')); |
513 DCHECK_EQ(std::string::npos, header.value().find('\0')); | 618 DCHECK_EQ(std::string::npos, header.value().find('\0')); |
514 cached_request_headers[header.name()] = header.value(); | 619 cached_request_headers[header.name()] = header.value(); |
515 } | 620 } |
516 | 621 |
517 if (!VaryMatches(request->headers, cached_request_headers, | 622 if (!VaryMatches(request->headers, cached_request_headers, |
(...skipping 24 matching lines...) Expand all Loading... | |
542 | 647 |
543 disk_cache::Entry* temp_entry = entry.get(); | 648 disk_cache::Entry* temp_entry = entry.get(); |
544 blob_data.AppendDiskCacheEntry( | 649 blob_data.AppendDiskCacheEntry( |
545 new CacheStorageCacheDataHandle(this, entry.Pass()), temp_entry, | 650 new CacheStorageCacheDataHandle(this, entry.Pass()), temp_entry, |
546 INDEX_RESPONSE_BODY); | 651 INDEX_RESPONSE_BODY); |
547 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | 652 scoped_ptr<storage::BlobDataHandle> blob_data_handle( |
548 blob_storage_context_->AddFinishedBlob(&blob_data)); | 653 blob_storage_context_->AddFinishedBlob(&blob_data)); |
549 callback.Run(CACHE_STORAGE_OK, response.Pass(), blob_data_handle.Pass()); | 654 callback.Run(CACHE_STORAGE_OK, response.Pass(), blob_data_handle.Pass()); |
550 } | 655 } |
551 | 656 |
657 void CacheStorageCache::MatchAllImpl(const ResponsesCallback& callback) { | |
658 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | |
659 if (backend_state_ != BACKEND_OPEN) { | |
660 callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
661 std::vector<ServiceWorkerResponse>(), | |
662 ScopedVector<storage::BlobDataHandle>()); | |
663 return; | |
664 } | |
665 | |
666 OpenAllEntries(base::Bind(&CacheStorageCache::MatchAllDidOpenAllEntries, | |
667 weak_ptr_factory_.GetWeakPtr(), callback)); | |
668 } | |
669 | |
670 void CacheStorageCache::MatchAllDidOpenAllEntries( | |
671 const ResponsesCallback& callback, | |
672 scoped_ptr<OpenAllEntriesContext> entries_context, | |
673 CacheStorageError error) { | |
674 if (error != CACHE_STORAGE_OK) { | |
675 callback.Run(error, std::vector<ServiceWorkerResponse>(), | |
676 ScopedVector<storage::BlobDataHandle>()); | |
677 return; | |
678 } | |
679 | |
680 scoped_ptr<MatchAllContext> context(new MatchAllContext(callback)); | |
681 context->entries_context.swap(entries_context); | |
682 Entries::iterator iter = context->entries_context->entries.begin(); | |
683 MatchAllProcessNextEntry(context.Pass(), iter); | |
684 } | |
685 | |
686 void CacheStorageCache::MatchAllProcessNextEntry( | |
687 scoped_ptr<MatchAllContext> context, | |
688 const Entries::iterator& iter) { | |
689 if (iter == context->entries_context->entries.end()) { | |
690 // All done. Return all of the responses. | |
691 context->original_callback.Run(CACHE_STORAGE_OK, context->out_responses, | |
692 context->out_blob_data_handles.Pass()); | |
693 return; | |
694 } | |
695 | |
696 ReadMetadata(*iter, base::Bind(&CacheStorageCache::MatchAllDidReadMetadata, | |
697 weak_ptr_factory_.GetWeakPtr(), | |
698 base::Passed(context.Pass()), iter)); | |
699 } | |
700 | |
701 void CacheStorageCache::MatchAllDidReadMetadata( | |
702 scoped_ptr<MatchAllContext> context, | |
703 const Entries::iterator& iter, | |
704 scoped_ptr<CacheMetadata> metadata) { | |
705 // Move ownership of the entry from the context. | |
706 disk_cache::ScopedEntryPtr entry(*iter); | |
707 *iter = nullptr; | |
708 | |
709 if (!metadata) { | |
710 entry->Doom(); | |
711 MatchAllProcessNextEntry(context.Pass(), iter + 1); | |
712 return; | |
713 } | |
714 | |
715 ServiceWorkerResponse response; | |
716 PopulateServiceWorkerResponse(*metadata, &response); | |
717 | |
718 if (entry->GetDataSize(INDEX_RESPONSE_BODY) == 0) { | |
719 context->out_responses.push_back(response); | |
720 context->out_blob_data_handles.push_back(nullptr); | |
721 MatchAllProcessNextEntry(context.Pass(), iter + 1); | |
722 return; | |
723 } | |
724 | |
725 if (!blob_storage_context_) { | |
726 context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | |
727 std::vector<ServiceWorkerResponse>(), | |
728 ScopedVector<storage::BlobDataHandle>()); | |
729 return; | |
730 } | |
731 | |
732 // Create a blob with the response body data. | |
jkarlin
2015/08/05 12:09:05
The block of code below is duplicated from MatchDi
nhiroki
2015/08/06 03:36:53
Factored out the block into PopulateResponseBody()
| |
733 response.blob_size = entry->GetDataSize(INDEX_RESPONSE_BODY); | |
734 response.blob_uuid = base::GenerateGUID(); | |
735 storage::BlobDataBuilder blob_data(response.blob_uuid); | |
736 | |
737 // Pass ownership of |entry| from the context to the handle. | |
738 disk_cache::Entry* entry_ptr = entry.get(); | |
739 blob_data.AppendDiskCacheEntry( | |
740 new CacheStorageCacheDataHandle(this, entry.Pass()), entry_ptr, | |
741 INDEX_RESPONSE_BODY); | |
742 scoped_ptr<storage::BlobDataHandle> blob_data_handle( | |
743 blob_storage_context_->AddFinishedBlob(&blob_data)); | |
744 | |
745 context->out_responses.push_back(response); | |
746 context->out_blob_data_handles.push_back(blob_data_handle.release()); | |
747 MatchAllProcessNextEntry(context.Pass(), iter + 1); | |
748 } | |
749 | |
552 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, | 750 void CacheStorageCache::Put(const CacheStorageBatchOperation& operation, |
553 const ErrorCallback& callback) { | 751 const ErrorCallback& callback) { |
554 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); | 752 DCHECK(BACKEND_OPEN == backend_state_ || initializing_); |
555 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); | 753 DCHECK_EQ(CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT, operation.operation_type); |
556 | 754 |
557 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( | 755 scoped_ptr<ServiceWorkerFetchRequest> request(new ServiceWorkerFetchRequest( |
558 operation.request.url, operation.request.method, | 756 operation.request.url, operation.request.method, |
559 operation.request.headers, operation.request.referrer, | 757 operation.request.headers, operation.request.referrer, |
560 operation.request.is_reload)); | 758 operation.request.is_reload)); |
561 | 759 |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
853 // 1. Iterate through all of the entries, open them, and add them to a vector. | 1051 // 1. Iterate through all of the entries, open them, and add them to a vector. |
854 // 2. For each open entry: | 1052 // 2. For each open entry: |
855 // 2.1. Read the headers into a protobuf. | 1053 // 2.1. Read the headers into a protobuf. |
856 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | 1054 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). |
857 // 2.3. Push the response into a vector of requests to be returned. | 1055 // 2.3. Push the response into a vector of requests to be returned. |
858 // 3. Return the vector of requests (keys). | 1056 // 3. Return the vector of requests (keys). |
859 | 1057 |
860 // The entries have to be loaded into a vector first because enumeration loops | 1058 // The entries have to be loaded into a vector first because enumeration loops |
861 // forever if you read data from a cache entry while enumerating. | 1059 // forever if you read data from a cache entry while enumerating. |
862 | 1060 |
863 scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); | 1061 OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, |
864 | 1062 weak_ptr_factory_.GetWeakPtr(), callback)); |
865 keys_context->backend_iterator = backend_->CreateIterator(); | |
866 disk_cache::Backend::Iterator& iterator = *keys_context->backend_iterator; | |
867 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | |
868 | |
869 net::CompletionCallback open_entry_callback = base::Bind( | |
870 &CacheStorageCache::KeysDidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), | |
871 base::Passed(keys_context.Pass())); | |
872 | |
873 int rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | |
874 | |
875 if (rv != net::ERR_IO_PENDING) | |
876 open_entry_callback.Run(rv); | |
877 } | 1063 } |
878 | 1064 |
879 void CacheStorageCache::KeysDidOpenNextEntry( | 1065 void CacheStorageCache::KeysDidOpenAllEntries( |
880 scoped_ptr<KeysContext> keys_context, | 1066 const RequestsCallback& callback, |
881 int rv) { | 1067 scoped_ptr<OpenAllEntriesContext> entries_context, |
882 if (rv == net::ERR_FAILED) { | 1068 CacheStorageError error) { |
883 DCHECK(!keys_context->enumerated_entry); | 1069 if (error != CACHE_STORAGE_OK) { |
884 // Enumeration is complete, extract the requests from the entries. | 1070 callback.Run(error, scoped_ptr<Requests>()); |
885 Entries::iterator iter = keys_context->entries.begin(); | |
886 KeysProcessNextEntry(keys_context.Pass(), iter); | |
887 return; | 1071 return; |
888 } | 1072 } |
889 | 1073 |
890 if (rv < 0) { | 1074 scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); |
891 keys_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 1075 keys_context->entries_context.swap(entries_context); |
892 scoped_ptr<Requests>()); | 1076 Entries::iterator iter = keys_context->entries_context->entries.begin(); |
893 return; | 1077 KeysProcessNextEntry(keys_context.Pass(), iter); |
894 } | |
895 | |
896 if (backend_state_ != BACKEND_OPEN) { | |
897 keys_context->original_callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | |
898 scoped_ptr<Requests>()); | |
899 return; | |
900 } | |
901 | |
902 // Store the entry. | |
903 keys_context->entries.push_back(keys_context->enumerated_entry); | |
904 keys_context->enumerated_entry = NULL; | |
905 | |
906 // Enumerate the next entry. | |
907 disk_cache::Backend::Iterator& iterator = *keys_context->backend_iterator; | |
908 disk_cache::Entry** enumerated_entry = &keys_context->enumerated_entry; | |
909 net::CompletionCallback open_entry_callback = base::Bind( | |
910 &CacheStorageCache::KeysDidOpenNextEntry, weak_ptr_factory_.GetWeakPtr(), | |
911 base::Passed(keys_context.Pass())); | |
912 | |
913 rv = iterator.OpenNextEntry(enumerated_entry, open_entry_callback); | |
914 | |
915 if (rv != net::ERR_IO_PENDING) | |
916 open_entry_callback.Run(rv); | |
917 } | 1078 } |
918 | 1079 |
919 void CacheStorageCache::KeysProcessNextEntry( | 1080 void CacheStorageCache::KeysProcessNextEntry( |
920 scoped_ptr<KeysContext> keys_context, | 1081 scoped_ptr<KeysContext> keys_context, |
921 const Entries::iterator& iter) { | 1082 const Entries::iterator& iter) { |
922 if (iter == keys_context->entries.end()) { | 1083 if (iter == keys_context->entries_context->entries.end()) { |
923 // All done. Return all of the keys. | 1084 // All done. Return all of the keys. |
924 keys_context->original_callback.Run(CACHE_STORAGE_OK, | 1085 keys_context->original_callback.Run(CACHE_STORAGE_OK, |
925 keys_context->out_keys.Pass()); | 1086 keys_context->out_keys.Pass()); |
926 return; | 1087 return; |
927 } | 1088 } |
928 | 1089 |
929 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, | 1090 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, |
930 weak_ptr_factory_.GetWeakPtr(), | 1091 weak_ptr_factory_.GetWeakPtr(), |
931 base::Passed(keys_context.Pass()), iter)); | 1092 base::Passed(keys_context.Pass()), iter)); |
932 } | 1093 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1056 CacheStorageError error, | 1217 CacheStorageError error, |
1057 scoped_ptr<ServiceWorkerResponse> response, | 1218 scoped_ptr<ServiceWorkerResponse> response, |
1058 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 1219 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
1059 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1220 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
1060 | 1221 |
1061 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 1222 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
1062 if (cache) | 1223 if (cache) |
1063 scheduler_->CompleteOperationAndRunNext(); | 1224 scheduler_->CompleteOperationAndRunNext(); |
1064 } | 1225 } |
1065 | 1226 |
1227 void CacheStorageCache::PendingResponsesCallback( | |
1228 const ResponsesCallback& callback, | |
1229 CacheStorageError error, | |
1230 const std::vector<ServiceWorkerResponse>& responses, | |
1231 ScopedVector<storage::BlobDataHandle> blob_data_handles) { | |
1232 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); | |
1233 | |
1234 callback.Run(error, responses, blob_data_handles.Pass()); | |
1235 if (cache) | |
1236 scheduler_->CompleteOperationAndRunNext(); | |
1237 } | |
1238 | |
1066 void CacheStorageCache::PendingRequestsCallback( | 1239 void CacheStorageCache::PendingRequestsCallback( |
1067 const RequestsCallback& callback, | 1240 const RequestsCallback& callback, |
1068 CacheStorageError error, | 1241 CacheStorageError error, |
1069 scoped_ptr<Requests> requests) { | 1242 scoped_ptr<Requests> requests) { |
1070 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); | 1243 base::WeakPtr<CacheStorageCache> cache = weak_ptr_factory_.GetWeakPtr(); |
1071 | 1244 |
1072 callback.Run(error, requests.Pass()); | 1245 callback.Run(error, requests.Pass()); |
1073 if (cache) | 1246 if (cache) |
1074 scheduler_->CompleteOperationAndRunNext(); | 1247 scheduler_->CompleteOperationAndRunNext(); |
1075 } | 1248 } |
1076 | 1249 |
1077 } // namespace content | 1250 } // namespace content |
OLD | NEW |