Chromium Code Reviews| Index: content/browser/cache_storage/cache_storage_cache.cc |
| diff --git a/content/browser/cache_storage/cache_storage_cache.cc b/content/browser/cache_storage/cache_storage_cache.cc |
| index 1e114d9d4166aacee49b8a9999c6e92f98398686..28fe235c15660b03fd35cbdda3109b9dfe32825a 100644 |
| --- a/content/browser/cache_storage/cache_storage_cache.cc |
| +++ b/content/browser/cache_storage/cache_storage_cache.cc |
| @@ -243,10 +243,19 @@ struct CacheStorageCache::MatchAllContext { |
| // The state needed to pass between CacheStorageCache::Keys callbacks. |
| struct CacheStorageCache::KeysContext { |
| - explicit KeysContext(const CacheStorageCache::RequestsCallback& callback) |
| - : original_callback(callback), out_keys(new Requests()) {} |
| + explicit KeysContext(scoped_ptr<ServiceWorkerFetchRequest> request, |
|
jkarlin
2016/03/04 14:39:09
Remove explicit
|
| + const CacheStorageCacheQueryParams& match_params, |
| + const CacheStorageCache::RequestsCallback& callback) |
| + : request(std::move(request)), |
| + options(match_params), |
| + original_callback(callback), |
| + out_keys(new Requests()) {} |
| ~KeysContext() {} |
| + scoped_ptr<ServiceWorkerFetchRequest> request; |
| + |
| + CacheStorageCacheQueryParams options; |
| + |
| // The callback passed to the Keys() function. |
| RequestsCallback original_callback; |
| @@ -408,7 +417,9 @@ void CacheStorageCache::BatchDidAllOperations( |
| callback->Run(CACHE_STORAGE_OK); |
| } |
| -void CacheStorageCache::Keys(const RequestsCallback& callback) { |
| +void CacheStorageCache::Keys(scoped_ptr<ServiceWorkerFetchRequest> request, |
| + const CacheStorageCacheQueryParams& match_params, |
| + const RequestsCallback& callback) { |
| if (!LazyInitialize()) { |
| callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); |
| return; |
| @@ -417,9 +428,9 @@ void CacheStorageCache::Keys(const RequestsCallback& callback) { |
| RequestsCallback pending_callback = |
| base::Bind(&CacheStorageCache::PendingRequestsCallback, |
| weak_ptr_factory_.GetWeakPtr(), callback); |
| - scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::KeysImpl, |
| - weak_ptr_factory_.GetWeakPtr(), |
| - pending_callback)); |
| + scheduler_->ScheduleOperation(base::Bind( |
| + &CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(std::move(request)), match_params, pending_callback)); |
| } |
| void CacheStorageCache::Close(const base::Closure& callback) { |
| @@ -1120,37 +1131,110 @@ void CacheStorageCache::DeleteDidOpenEntry( |
| callback.Run(CACHE_STORAGE_OK); |
| } |
| -void CacheStorageCache::KeysImpl(const RequestsCallback& callback) { |
| +void CacheStorageCache::KeysImpl( |
| + scoped_ptr<ServiceWorkerFetchRequest> request, |
| + const CacheStorageCacheQueryParams& match_params, |
| + const RequestsCallback& callback) { |
| DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
| if (backend_state_ != BACKEND_OPEN) { |
| callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); |
| return; |
| } |
| - // 1. Iterate through all of the entries, open them, and add them to a vector. |
| - // 2. For each open entry: |
| - // 2.1. Read the headers into a protobuf. |
| - // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). |
| - // 2.3. Push the response into a vector of requests to be returned. |
| - // 3. Return the vector of requests (keys). |
| + if (request->url.is_empty() || match_params.ignore_search) { |
| + scoped_ptr<KeysContext> keys_context( |
| + new KeysContext(std::move(request), match_params, callback)); |
| + |
| + // 1. Iterate through all of the entries, open them, and add them to a |
| + // vector. |
| + // 2. For each open entry: |
| + // 2.1. Read the headers into a protobuf. |
| + // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). |
| + // 2.3. Push the response into a vector of requests to be returned. |
| + // 3. Return the vector of requests (keys). |
| + |
| + // The entries have to be loaded into a vector first because enumeration |
| + // loops |
|
jkarlin
2016/03/04 14:39:09
comment formatting, you have an extra newline here
|
| + // forever if you read data from a cache entry while enumerating. |
| + |
| + OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(std::move(keys_context)))); |
| + return; |
| + } |
| + |
| + scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); |
| + disk_cache::Entry** entry_ptr = entry.get(); |
| + ServiceWorkerFetchRequest* request_ptr = request.get(); |
| - // The entries have to be loaded into a vector first because enumeration loops |
| - // forever if you read data from a cache entry while enumerating. |
| + net::CompletionCallback open_entry_callback = base::Bind( |
| + &CacheStorageCache::KeyDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(std::move(request)), callback, |
| + base::Passed(std::move(entry))); |
| - OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, |
| - weak_ptr_factory_.GetWeakPtr(), callback)); |
| + int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, |
| + open_entry_callback); |
| + if (rv != net::ERR_IO_PENDING) |
| + open_entry_callback.Run(rv); |
| } |
| -void CacheStorageCache::KeysDidOpenAllEntries( |
| +void CacheStorageCache::KeyDidOpenEntry( |
|
jkarlin
2016/03/04 14:39:09
Should be KeysDidOpenEntry to show that it's a con
|
| + scoped_ptr<ServiceWorkerFetchRequest> request, |
| const RequestsCallback& callback, |
| + scoped_ptr<disk_cache::Entry*> entry_ptr, |
| + int rv) { |
| + if (rv != net::OK) { |
| + // If there is no matched request, then return empty array. |
| + scoped_ptr<Requests> requests(new Requests()); |
| + callback.Run(CACHE_STORAGE_OK, std::move(requests)); |
| + return; |
| + } |
| + |
| + disk_cache::ScopedEntryPtr entry(*entry_ptr); |
| + |
| + ReadMetadata(*entry_ptr, |
| + base::Bind(&CacheStorageCache::KeyDidReadMetadata, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + base::Passed(std::move(request)), callback, |
| + base::Passed(std::move(entry)))); |
| +} |
| + |
| +void CacheStorageCache::KeyDidReadMetadata( |
|
jkarlin
2016/03/04 14:39:09
Should be KeysDidReadMetadata
|
| + scoped_ptr<ServiceWorkerFetchRequest> request, |
| + const RequestsCallback& callback, |
| + disk_cache::ScopedEntryPtr entry, |
| + scoped_ptr<CacheMetadata> metadata) { |
| + scoped_ptr<Requests> requests(new Requests()); |
| + |
| + if (metadata) { |
|
jkarlin
2016/03/04 14:39:09
Start with the failed case and return early:
e.g.
|
| + requests->push_back(ServiceWorkerFetchRequest( |
| + GURL(entry->GetKey()), metadata->request().method(), |
| + ServiceWorkerHeaderMap(), Referrer(), false)); |
| + |
| + ServiceWorkerHeaderMap& req_headers = requests->back().headers; |
| + |
| + for (int i = 0; i < metadata->request().headers_size(); ++i) { |
| + const CacheHeaderMap header = metadata->request().headers(i); |
| + DCHECK_EQ(std::string::npos, header.name().find('\0')); |
| + DCHECK_EQ(std::string::npos, header.value().find('\0')); |
| + req_headers.insert(std::make_pair(header.name(), header.value())); |
| + } |
|
jkarlin
2016/03/04 14:39:09
Lines 1210-1221 could be put in a new
PopulateFet
|
| + } else { |
| + entry->Doom(); |
| + } |
| + |
| + callback.Run(CACHE_STORAGE_OK, std::move(requests)); |
| +} |
| + |
| +void CacheStorageCache::KeysDidOpenAllEntries( |
| + scoped_ptr<KeysContext> keys_context, |
| scoped_ptr<OpenAllEntriesContext> entries_context, |
| CacheStorageError error) { |
| if (error != CACHE_STORAGE_OK) { |
| - callback.Run(error, scoped_ptr<Requests>()); |
| + keys_context->original_callback.Run(error, scoped_ptr<Requests>()); |
| return; |
| } |
| - scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); |
| keys_context->entries_context.swap(entries_context); |
| Entries::iterator iter = keys_context->entries_context->entries.begin(); |
| KeysProcessNextEntry(std::move(keys_context), iter); |
| @@ -1166,6 +1250,16 @@ void CacheStorageCache::KeysProcessNextEntry( |
| return; |
| } |
| + if (keys_context->options.ignore_search) { |
|
jkarlin
2016/03/04 14:39:09
This CL adds what is essentially a third implement
|
| + DCHECK(keys_context->request); |
| + disk_cache::Entry* entry(*iter); |
| + if (RemoveQueryParam(keys_context->request->url) != |
| + RemoveQueryParam(GURL(entry->GetKey()))) { |
| + KeysProcessNextEntry(std::move(keys_context), iter + 1); |
| + return; |
| + } |
| + } |
| + |
| ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, |
| weak_ptr_factory_.GetWeakPtr(), |
| base::Passed(std::move(keys_context)), iter)); |