| 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, | 
| 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 | 
| 421                                            weak_ptr_factory_.GetWeakPtr(), | 432   scoped_ptr<KeysContext> keys_context( | 
| 422                                            pending_callback)); | 433       new KeysContext(std::move(request), match_params, pending_callback)); | 
|  | 434   scheduler_->ScheduleOperation( | 
|  | 435       base::Bind(&CacheStorageCache::KeysImpl, weak_ptr_factory_.GetWeakPtr(), | 
|  | 436                  base::Passed(std::move(keys_context)))); | 
| 423 } | 437 } | 
| 424 | 438 | 
| 425 void CacheStorageCache::Close(const base::Closure& callback) { | 439 void CacheStorageCache::Close(const base::Closure& callback) { | 
| 426   DCHECK_NE(BACKEND_CLOSED, backend_state_) | 440   DCHECK_NE(BACKEND_CLOSED, backend_state_) | 
| 427       << "Was CacheStorageCache::Close() called twice?"; | 441       << "Was CacheStorageCache::Close() called twice?"; | 
| 428 | 442 | 
| 429   base::Closure pending_callback = | 443   base::Closure pending_callback = | 
| 430       base::Bind(&CacheStorageCache::PendingClosure, | 444       base::Bind(&CacheStorageCache::PendingClosure, | 
| 431                  weak_ptr_factory_.GetWeakPtr(), callback); | 445                  weak_ptr_factory_.GetWeakPtr(), callback); | 
| 432 | 446 | 
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1111   DCHECK(entry_ptr); | 1125   DCHECK(entry_ptr); | 
| 1112   disk_cache::ScopedEntryPtr entry(*entry_ptr); | 1126   disk_cache::ScopedEntryPtr entry(*entry_ptr); | 
| 1113 | 1127 | 
| 1114   entry->Doom(); | 1128   entry->Doom(); | 
| 1115   entry.reset(); | 1129   entry.reset(); | 
| 1116 | 1130 | 
| 1117   UpdateCacheSize(); | 1131   UpdateCacheSize(); | 
| 1118   callback.Run(CACHE_STORAGE_OK); | 1132   callback.Run(CACHE_STORAGE_OK); | 
| 1119 } | 1133 } | 
| 1120 | 1134 | 
| 1121 void CacheStorageCache::KeysImpl(const RequestsCallback& callback) { | 1135 void CacheStorageCache::KeysImpl(scoped_ptr<KeysContext> keys_context) { | 
| 1122   DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 1136   DCHECK_NE(BACKEND_UNINITIALIZED, backend_state_); | 
| 1123   if (backend_state_ != BACKEND_OPEN) { | 1137   if (backend_state_ != BACKEND_OPEN) { | 
| 1124     callback.Run(CACHE_STORAGE_ERROR_STORAGE, scoped_ptr<Requests>()); | 1138     keys_context->original_callback.Run(CACHE_STORAGE_ERROR_STORAGE, | 
|  | 1139                                         scoped_ptr<Requests>()); | 
| 1125     return; | 1140     return; | 
| 1126   } | 1141   } | 
| 1127 | 1142 | 
| 1128   // 1. Iterate through all of the entries, open them, and add them to a vector. | 1143   // 1. Iterate through all of the entries, open them, and add them to a vector. | 
| 1129   // 2. For each open entry: | 1144   // 2. For each open entry: | 
| 1130   //  2.1. Read the headers into a protobuf. | 1145   //  2.1. Read the headers into a protobuf. | 
| 1131   //  2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | 1146   //  2.2. Copy the protobuf into a ServiceWorkerFetchRequest (a "key"). | 
| 1132   //  2.3. Push the response into a vector of requests to be returned. | 1147   //  2.3. Push the response into a vector of requests to be returned. | 
| 1133   // 3. Return the vector of requests (keys). | 1148   // 3. Return the vector of requests (keys). | 
| 1134 | 1149 | 
| 1135   // The entries have to be loaded into a vector first because enumeration loops | 1150   // The entries have to be loaded into a vector first because enumeration loops | 
| 1136   // forever if you read data from a cache entry while enumerating. | 1151   // forever if you read data from a cache entry while enumerating. | 
| 1137 | 1152 | 
| 1138   OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, | 1153   OpenAllEntries(base::Bind(&CacheStorageCache::KeysDidOpenAllEntries, | 
| 1139                             weak_ptr_factory_.GetWeakPtr(), callback)); | 1154                             weak_ptr_factory_.GetWeakPtr(), | 
|  | 1155                             base::Passed(std::move(keys_context)))); | 
| 1140 } | 1156 } | 
| 1141 | 1157 | 
| 1142 void CacheStorageCache::KeysDidOpenAllEntries( | 1158 void CacheStorageCache::KeysDidOpenAllEntries( | 
| 1143     const RequestsCallback& callback, | 1159     scoped_ptr<KeysContext> keys_context, | 
| 1144     scoped_ptr<OpenAllEntriesContext> entries_context, | 1160     scoped_ptr<OpenAllEntriesContext> entries_context, | 
| 1145     CacheStorageError error) { | 1161     CacheStorageError error) { | 
| 1146   if (error != CACHE_STORAGE_OK) { | 1162   if (error != CACHE_STORAGE_OK) { | 
| 1147     callback.Run(error, scoped_ptr<Requests>()); | 1163     keys_context->original_callback.Run(error, scoped_ptr<Requests>()); | 
| 1148     return; | 1164     return; | 
| 1149   } | 1165   } | 
| 1150 | 1166 | 
| 1151   scoped_ptr<KeysContext> keys_context(new KeysContext(callback)); |  | 
| 1152   keys_context->entries_context.swap(entries_context); | 1167   keys_context->entries_context.swap(entries_context); | 
| 1153   Entries::iterator iter = keys_context->entries_context->entries.begin(); | 1168   Entries::iterator iter = keys_context->entries_context->entries.begin(); | 
| 1154   KeysProcessNextEntry(std::move(keys_context), iter); | 1169   KeysProcessNextEntry(std::move(keys_context), iter); | 
| 1155 } | 1170 } | 
| 1156 | 1171 | 
| 1157 void CacheStorageCache::KeysProcessNextEntry( | 1172 void CacheStorageCache::KeysProcessNextEntry( | 
| 1158     scoped_ptr<KeysContext> keys_context, | 1173     scoped_ptr<KeysContext> keys_context, | 
| 1159     const Entries::iterator& iter) { | 1174     const Entries::iterator& iter) { | 
| 1160   if (iter == keys_context->entries_context->entries.end()) { | 1175   if (iter == keys_context->entries_context->entries.end()) { | 
| 1161     // All done. Return all of the keys. | 1176     // All done. Return all of the keys. | 
| 1162     keys_context->original_callback.Run(CACHE_STORAGE_OK, | 1177     keys_context->original_callback.Run(CACHE_STORAGE_OK, | 
| 1163                                         std::move(keys_context->out_keys)); | 1178                                         std::move(keys_context->out_keys)); | 
| 1164     return; | 1179     return; | 
| 1165   } | 1180   } | 
| 1166 | 1181 | 
|  | 1182   if (keys_context->options.ignore_search) { | 
|  | 1183     DCHECK(keys_context->request); | 
|  | 1184     disk_cache::Entry* entry(*iter); | 
|  | 1185     if (RemoveQueryParam(keys_context->request->url) != | 
|  | 1186         RemoveQueryParam(GURL(entry->GetKey()))) { | 
|  | 1187       KeysProcessNextEntry(std::move(keys_context), iter + 1); | 
|  | 1188       return; | 
|  | 1189     } | 
|  | 1190   } | 
|  | 1191 | 
| 1167   ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, | 1192   ReadMetadata(*iter, base::Bind(&CacheStorageCache::KeysDidReadMetadata, | 
| 1168                                  weak_ptr_factory_.GetWeakPtr(), | 1193                                  weak_ptr_factory_.GetWeakPtr(), | 
| 1169                                  base::Passed(std::move(keys_context)), iter)); | 1194                                  base::Passed(std::move(keys_context)), iter)); | 
| 1170 } | 1195 } | 
| 1171 | 1196 | 
| 1172 void CacheStorageCache::KeysDidReadMetadata( | 1197 void CacheStorageCache::KeysDidReadMetadata( | 
| 1173     scoped_ptr<KeysContext> keys_context, | 1198     scoped_ptr<KeysContext> keys_context, | 
| 1174     const Entries::iterator& iter, | 1199     const Entries::iterator& iter, | 
| 1175     scoped_ptr<CacheMetadata> metadata) { | 1200     scoped_ptr<CacheMetadata> metadata) { | 
| 1176   disk_cache::Entry* entry = *iter; | 1201   disk_cache::Entry* entry = *iter; | 
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1392   storage::BlobDataBuilder blob_data(response->blob_uuid); | 1417   storage::BlobDataBuilder blob_data(response->blob_uuid); | 
| 1393 | 1418 | 
| 1394   disk_cache::Entry* temp_entry = entry.get(); | 1419   disk_cache::Entry* temp_entry = entry.get(); | 
| 1395   blob_data.AppendDiskCacheEntry( | 1420   blob_data.AppendDiskCacheEntry( | 
| 1396       new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 1421       new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 
| 1397       INDEX_RESPONSE_BODY); | 1422       INDEX_RESPONSE_BODY); | 
| 1398   return blob_storage_context_->AddFinishedBlob(&blob_data); | 1423   return blob_storage_context_->AddFinishedBlob(&blob_data); | 
| 1399 } | 1424 } | 
| 1400 | 1425 | 
| 1401 }  // namespace content | 1426 }  // namespace content | 
| OLD | NEW | 
|---|