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 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 | 236 |
237 // The context holding open entries. | 237 // The context holding open entries. |
238 scoped_ptr<OpenAllEntriesContext> entries_context; | 238 scoped_ptr<OpenAllEntriesContext> entries_context; |
239 | 239 |
240 private: | 240 private: |
241 DISALLOW_COPY_AND_ASSIGN(MatchAllContext); | 241 DISALLOW_COPY_AND_ASSIGN(MatchAllContext); |
242 }; | 242 }; |
243 | 243 |
244 // The state needed to pass between CacheStorageCache::Keys callbacks. | 244 // The state needed to pass between CacheStorageCache::Keys callbacks. |
245 struct CacheStorageCache::KeysContext { | 245 struct CacheStorageCache::KeysContext { |
246 explicit KeysContext(const CacheStorageCache::RequestsCallback& callback) | 246 explicit KeysContext(scoped_ptr<ServiceWorkerFetchRequest> request, |
jkarlin
2016/03/04 14:39:09
Remove explicit
| |
247 : original_callback(callback), out_keys(new Requests()) {} | 247 const CacheStorageCacheQueryParams& match_params, |
248 const CacheStorageCache::RequestsCallback& callback) | |
249 : request(std::move(request)), | |
250 options(match_params), | |
251 original_callback(callback), | |
252 out_keys(new Requests()) {} | |
248 ~KeysContext() {} | 253 ~KeysContext() {} |
249 | 254 |
255 scoped_ptr<ServiceWorkerFetchRequest> request; | |
256 | |
257 CacheStorageCacheQueryParams options; | |
258 | |
250 // The callback passed to the Keys() function. | 259 // The callback passed to the Keys() function. |
251 RequestsCallback original_callback; | 260 RequestsCallback original_callback; |
252 | 261 |
253 // The output of the Keys function. | 262 // The output of the Keys function. |
254 scoped_ptr<Requests> out_keys; | 263 scoped_ptr<Requests> out_keys; |
255 | 264 |
256 // The context holding open entries. | 265 // The context holding open entries. |
257 scoped_ptr<OpenAllEntriesContext> entries_context; | 266 scoped_ptr<OpenAllEntriesContext> entries_context; |
258 | 267 |
259 private: | 268 private: |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
401 barrier_closure.Run(); | 410 barrier_closure.Run(); |
402 } | 411 } |
403 | 412 |
404 void CacheStorageCache::BatchDidAllOperations( | 413 void CacheStorageCache::BatchDidAllOperations( |
405 scoped_ptr<ErrorCallback> callback) { | 414 scoped_ptr<ErrorCallback> callback) { |
406 if (callback->is_null()) | 415 if (callback->is_null()) |
407 return; | 416 return; |
408 callback->Run(CACHE_STORAGE_OK); | 417 callback->Run(CACHE_STORAGE_OK); |
409 } | 418 } |
410 | 419 |
411 void CacheStorageCache::Keys(const RequestsCallback& callback) { | 420 void CacheStorageCache::Keys(scoped_ptr<ServiceWorkerFetchRequest> request, |
421 const CacheStorageCacheQueryParams& match_params, | |
422 const RequestsCallback& callback) { | |
412 if (!LazyInitialize()) { | 423 if (!LazyInitialize()) { |
413 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); | 424 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); |
414 return; | 425 return; |
415 } | 426 } |
416 | 427 |
417 RequestsCallback pending_callback = | 428 RequestsCallback pending_callback = |
418 base::Bind(&CacheStorageCache::PendingRequestsCallback, | 429 base::Bind(&CacheStorageCache::PendingRequestsCallback, |
419 weak_ptr_factory_.GetWeakPtr(), callback); | 430 weak_ptr_factory_.GetWeakPtr(), callback); |
420 scheduler_->ScheduleOperation(base::Bind(&CacheStorageCache::KeysImpl, | 431 scheduler_->ScheduleOperation(base::Bind( |
421 weak_ptr_factory_.GetWeakPtr(), | 432 &CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(), |
422 pending_callback)); | 433 base::Passed(std::move(request)), match_params, pending_callback)); |
423 } | 434 } |
424 | 435 |
425 void CacheStorageCache::Close(const base::Closure& callback) { | 436 void CacheStorageCache::Close(const base::Closure& callback) { |
426 DCHECK_NE(BACKEND_CLOSED, backend_state_) | 437 DCHECK_NE(BACKEND_CLOSED, backend_state_) |
427 << "Was CacheStorageCache::Close() called twice?"; | 438 << "Was CacheStorageCache::Close() called twice?"; |
428 | 439 |
429 base::Closure pending_callback = | 440 base::Closure pending_callback = |
430 base::Bind(&CacheStorageCache::PendingClosure, | 441 base::Bind(&CacheStorageCache::PendingClosure, |
431 weak_ptr_factory_.GetWeakPtr(), callback); | 442 weak_ptr_factory_.GetWeakPtr(), callback); |
432 | 443 |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1113 DCHECK(entry_ptr); | 1124 DCHECK(entry_ptr); |
1114 disk_cache::ScopedEntryPtr entry(*entry_ptr); | 1125 disk_cache::ScopedEntryPtr entry(*entry_ptr); |
1115 | 1126 |
1116 entry->Doom(); | 1127 entry->Doom(); |
1117 entry.reset(); | 1128 entry.reset(); |
1118 | 1129 |
1119 UpdateCacheSize(); | 1130 UpdateCacheSize(); |
1120 callback.Run(CACHE_STORAGE_OK); | 1131 callback.Run(CACHE_STORAGE_OK); |
1121 } | 1132 } |
1122 | 1133 |
1123 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) { | 1134 void CacheStorageCache::KeysImpl( |
1135 scoped_ptr<ServiceWorkerFetchRequest> request, | |
1136 const CacheStorageCacheQueryParams& match_params, | |
1137 const RequestsCallback& callback) { | |
1124 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 1138 DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); |
1125 if (backend_state_ != BACKEND_OPEN) { | 1139 if (backend_state_ != BACKEND_OPEN) { |
1126 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); | 1140 callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); |
1127 return; | 1141 return; |
1128 } | 1142 } |
1129 | 1143 |
1130 // 1. Iterate through all of the entries, open them, and add them to a vector. | 1144 if (request->url.is_empty() || match_params.ignore_search) { |
1131 // 2. For each open entry: | 1145 scoped_ptr<KeysContext> keys_context( |
1132 // 2.1. Read the headers into a protobuf. | 1146 new KeysContext(std::move(request), match_params, callback)); |
1133 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | |
1134 // 2.3. Push the response into a vector of requests to be returned. | |
1135 // 3. Return the vector of requests (keys). | |
1136 | 1147 |
1137 // The entries have to be loaded into a vector first because enumeration loops | 1148 // 1. Iterate through all of the entries, open them, and add them to a |
1138 // forever if you read data from a cache entry while enumerating. | 1149 // vector. |
1150 // 2. For each open entry: | |
1151 // 2.1. Read the headers into a protobuf. | |
1152 // 2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | |
1153 // 2.3. Push the response into a vector of requests to be returned. | |
1154 // 3. Return the vector of requests (keys). | |
1139 | 1155 |
1140 OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, | 1156 // The entries have to be loaded into a vector first because enumeration |
1141 weak_ptr_factory_.GetWeakPtr(), callback)); | 1157 // loops |
jkarlin
2016/03/04 14:39:09
comment formatting, you have an extra newline here
| |
1158 // forever if you read data from a cache entry while enumerating. | |
1159 | |
1160 OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, | |
1161 weak_ptr_factory_.GetWeakPtr(), | |
1162 base::Passed(std::move(keys_context)))); | |
1163 return; | |
1164 } | |
1165 | |
1166 scoped_ptr<disk_cache::Entry*> entry(new disk_cache::Entry*); | |
1167 disk_cache::Entry** entry_ptr = entry.get(); | |
1168 ServiceWorkerFetchRequest* request_ptr = request.get(); | |
1169 | |
1170 net::CompletionCallback open_entry_callback = base::Bind( | |
1171 &CacheStorageCache::KeyDidOpenEntry, weak_ptr_factory_.GetWeakPtr(), | |
1172 base::Passed(std::move(request)), callback, | |
1173 base::Passed(std::move(entry))); | |
1174 | |
1175 int rv = backend_->OpenEntry(request_ptr->url.spec(), entry_ptr, | |
1176 open_entry_callback); | |
1177 if (rv != net::ERR_IO_PENDING) | |
1178 open_entry_callback.Run(rv); | |
1179 } | |
1180 | |
1181 void CacheStorageCache::KeyDidOpenEntry( | |
jkarlin
2016/03/04 14:39:09
Should be KeysDidOpenEntry to show that it's a con
| |
1182 scoped_ptr<ServiceWorkerFetchRequest> request, | |
1183 const RequestsCallback& callback, | |
1184 scoped_ptr<disk_cache::Entry*> entry_ptr, | |
1185 int rv) { | |
1186 if (rv != net::OK) { | |
1187 // If there is no matched request, then return empty array. | |
1188 scoped_ptr<Requests> requests(new Requests()); | |
1189 callback.Run(CACHE_STORAGE_OK, std::move(requests)); | |
1190 return; | |
1191 } | |
1192 | |
1193 disk_cache::ScopedEntryPtr entry(*entry_ptr); | |
1194 | |
1195 ReadMetadata(*entry_ptr, | |
1196 base::Bind(&CacheStorageCache::KeyDidReadMetadata, | |
1197 weak_ptr_factory_.GetWeakPtr(), | |
1198 base::Passed(std::move(request)), callback, | |
1199 base::Passed(std::move(entry)))); | |
1200 } | |
1201 | |
1202 void CacheStorageCache::KeyDidReadMetadata( | |
jkarlin
2016/03/04 14:39:09
Should be KeysDidReadMetadata
| |
1203 scoped_ptr<ServiceWorkerFetchRequest> request, | |
1204 const RequestsCallback& callback, | |
1205 disk_cache::ScopedEntryPtr entry, | |
1206 scoped_ptr<CacheMetadata> metadata) { | |
1207 scoped_ptr<Requests> requests(new Requests()); | |
1208 | |
1209 if (metadata) { | |
jkarlin
2016/03/04 14:39:09
Start with the failed case and return early:
e.g.
| |
1210 requests->push_back(ServiceWorkerFetchRequest( | |
1211 GURL(entry->GetKey()), metadata->request().method(), | |
1212 ServiceWorkerHeaderMap(), Referrer(), false)); | |
1213 | |
1214 ServiceWorkerHeaderMap& req_headers = requests->back().headers; | |
1215 | |
1216 for (int i = 0; i < metadata->request().headers_size(); ++i) { | |
1217 const CacheHeaderMap header = metadata->request().headers(i); | |
1218 DCHECK_EQ(std::string::npos, header.name().find('\0')); | |
1219 DCHECK_EQ(std::string::npos, header.value().find('\0')); | |
1220 req_headers.insert(std::make_pair(header.name(), header.value())); | |
1221 } | |
jkarlin
2016/03/04 14:39:09
Lines 1210-1221 could be put in a new
PopulateFet
| |
1222 } else { | |
1223 entry->Doom(); | |
1224 } | |
1225 | |
1226 callback.Run(CACHE_STORAGE_OK, std::move(requests)); | |
1142 } | 1227 } |
1143 | 1228 |
1144 void CacheStorageCache::KeysDidOpenAllEntries( | 1229 void CacheStorageCache::KeysDidOpenAllEntries( |
1145 const RequestsCallback& callback, | 1230 scoped_ptr<KeysContext> keys_context, |
1146 scoped_ptr<OpenAllEntriesContext> entries_context, | 1231 scoped_ptr<OpenAllEntriesContext> entries_context, |
1147 CacheStorageError error) { | 1232 CacheStorageError error) { |
1148 if (error != CACHE_STORAGE_OK) { | 1233 if (error != CACHE_STORAGE_OK) { |
1149 callback.Run(error, scoped_ptr<Requests>()); | 1234 keys_context->original_callback.Run(error, scoped_ptr<Requests>()); |
1150 return; | 1235 return; |
1151 } | 1236 } |
1152 | 1237 |
1153 scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); | |
1154 keys_context->entries_context.swap(entries_context); | 1238 keys_context->entries_context.swap(entries_context); |
1155 Entries::iterator iter = keys_context->entries_context->entries.begin(); | 1239 Entries::iterator iter = keys_context->entries_context->entries.begin(); |
1156 KeysProcessNextEntry(std::move(keys_context), iter); | 1240 KeysProcessNextEntry(std::move(keys_context), iter); |
1157 } | 1241 } |
1158 | 1242 |
1159 void CacheStorageCache::KeysProcessNextEntry( | 1243 void CacheStorageCache::KeysProcessNextEntry( |
1160 scoped_ptr<KeysContext> keys_context, | 1244 scoped_ptr<KeysContext> keys_context, |
1161 const Entries::iterator& iter) { | 1245 const Entries::iterator& iter) { |
1162 if (iter == keys_context->entries_context->entries.end()) { | 1246 if (iter == keys_context->entries_context->entries.end()) { |
1163 // All done. Return all of the keys. | 1247 // All done. Return all of the keys. |
1164 keys_context->original_callback.Run(CACHE_STORAGE_OK, | 1248 keys_context->original_callback.Run(CACHE_STORAGE_OK, |
1165 std::move(keys_context->out_keys)); | 1249 std::move(keys_context->out_keys)); |
1166 return; | 1250 return; |
1167 } | 1251 } |
1168 | 1252 |
1253 if (keys_context->options.ignore_search) { | |
jkarlin
2016/03/04 14:39:09
This CL adds what is essentially a third implement
| |
1254 DCHECK(keys_context->request); | |
1255 disk_cache::Entry* entry(*iter); | |
1256 if (RemoveQueryParam(keys_context->request->url) != | |
1257 RemoveQueryParam(GURL(entry->GetKey()))) { | |
1258 KeysProcessNextEntry(std::move(keys_context), iter + 1); | |
1259 return; | |
1260 } | |
1261 } | |
1262 | |
1169 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, | 1263 ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, |
1170 weak_ptr_factory_.GetWeakPtr(), | 1264 weak_ptr_factory_.GetWeakPtr(), |
1171 base::Passed(std::move(keys_context)), iter)); | 1265 base::Passed(std::move(keys_context)), iter)); |
1172 } | 1266 } |
1173 | 1267 |
1174 void CacheStorageCache::KeysDidReadMetadata( | 1268 void CacheStorageCache::KeysDidReadMetadata( |
1175 scoped_ptr<KeysContext> keys_context, | 1269 scoped_ptr<KeysContext> keys_context, |
1176 const Entries::iterator& iter, | 1270 const Entries::iterator& iter, |
1177 scoped_ptr<CacheMetadata> metadata) { | 1271 scoped_ptr<CacheMetadata> metadata) { |
1178 disk_cache::Entry* entry = *iter; | 1272 disk_cache::Entry* entry = *iter; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1395 storage::BlobDataBuilder blob_data(response->blob_uuid); | 1489 storage::BlobDataBuilder blob_data(response->blob_uuid); |
1396 | 1490 |
1397 disk_cache::Entry* temp_entry = entry.get(); | 1491 disk_cache::Entry* temp_entry = entry.get(); |
1398 blob_data.AppendDiskCacheEntry( | 1492 blob_data.AppendDiskCacheEntry( |
1399 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 1493 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, |
1400 INDEX_RESPONSE_BODY); | 1494 INDEX_RESPONSE_BODY); |
1401 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1495 return blob_storage_context_->AddFinishedBlob(&blob_data); |
1402 } | 1496 } |
1403 | 1497 |
1404 } // namespace content | 1498 } // namespace content |
OLD | NEW |