Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: content/browser/service_worker/service_worker_cache_storage.cc

Issue 444333006: Rename ServiceWorkerFetchStore/FetchStores/FetchStoresManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fetch_stores3
Patch Set: Rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/service_worker/service_worker_fetch_stores.h" 5 #include "content/browser/service_worker/service_worker_cache_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/memory_mapped_file.h" 10 #include "base/files/memory_mapped_file.h"
11 #include "base/sha1.h" 11 #include "base/sha1.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "content/browser/service_worker/service_worker_cache.h"
14 #include "content/browser/service_worker/service_worker_cache.pb.h" 15 #include "content/browser/service_worker/service_worker_cache.pb.h"
15 #include "content/browser/service_worker/service_worker_fetch_store.h"
16 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
17 #include "net/base/directory_lister.h" 17 #include "net/base/directory_lister.h"
18 18
19 namespace content { 19 namespace content {
20 20
21 // Handles the loading of ServiceWorkerFetchStore and any extra clean up other 21 // Handles the loading and clean up of ServiceWorkerCache objects.
22 // than deleting the ServiceWorkerFetchStore object. 22 class ServiceWorkerCacheStorage::CacheLoader {
23 class ServiceWorkerFetchStores::StoresLoader {
24 public: 23 public:
25 virtual ~StoresLoader() {}; 24 virtual ~CacheLoader() {};
26 virtual ServiceWorkerFetchStore* LoadStore(const std::string& key) = 0; 25 virtual ServiceWorkerCache* LoadCache(const std::string& cache_name) = 0;
27 // Creates a new store, deleting any pre-existing store of the same name. 26 // Creates a new cache, deleting any pre-existing cache of the same name.
28 virtual ServiceWorkerFetchStore* CreateStore(const std::string& key) = 0; 27 virtual ServiceWorkerCache* CreateCache(const std::string& cache_name) = 0;
29 virtual bool CleanUpDeletedStore(const std::string& key) = 0; 28 virtual bool CleanUpDeletedCache(const std::string& key) = 0;
30 virtual bool WriteIndex(StoreMap* stores) = 0; 29 virtual bool WriteIndex(CacheMap* caches) = 0;
31 virtual void LoadIndex(std::vector<std::string>* names) = 0; 30 virtual void LoadIndex(std::vector<std::string>* cache_names) = 0;
32 }; 31 };
33 32
34 class ServiceWorkerFetchStores::MemoryLoader 33 class ServiceWorkerCacheStorage::MemoryLoader
35 : public ServiceWorkerFetchStores::StoresLoader { 34 : public ServiceWorkerCacheStorage::CacheLoader {
36 public: 35 public:
37 virtual content::ServiceWorkerFetchStore* LoadStore( 36 virtual content::ServiceWorkerCache* LoadCache(
38 const std::string& key) OVERRIDE { 37 const std::string& cache_name) OVERRIDE {
39 NOTREACHED(); 38 NOTREACHED();
40 return NULL; 39 return NULL;
41 } 40 }
42 41
43 virtual ServiceWorkerFetchStore* CreateStore( 42 virtual ServiceWorkerCache* CreateCache(
44 const std::string& key) OVERRIDE { 43 const std::string& cache_name) OVERRIDE {
45 return ServiceWorkerFetchStore::CreateMemoryStore(key); 44 return ServiceWorkerCache::CreateMemoryCache(cache_name);
46 } 45 }
47 46
48 virtual bool CleanUpDeletedStore(const std::string& key) OVERRIDE { 47 virtual bool CleanUpDeletedCache(const std::string& cache_name) OVERRIDE {
49 return true; 48 return true;
50 } 49 }
51 50
52 virtual bool WriteIndex(StoreMap* stores) OVERRIDE { return false; } 51 virtual bool WriteIndex(CacheMap* caches) OVERRIDE { return false; }
53 52
54 virtual void LoadIndex(std::vector<std::string>* names) OVERRIDE { return; } 53 virtual void LoadIndex(std::vector<std::string>* cache_names) OVERRIDE {
54 return;
55 }
55 }; 56 };
56 57
57 class ServiceWorkerFetchStores::SimpleCacheLoader 58 class ServiceWorkerCacheStorage::SimpleCacheLoader
58 : public ServiceWorkerFetchStores::StoresLoader { 59 : public ServiceWorkerCacheStorage::CacheLoader {
59 public: 60 public:
60 explicit SimpleCacheLoader(const base::FilePath& origin_path) 61 explicit SimpleCacheLoader(const base::FilePath& origin_path)
61 : origin_path_(origin_path) {} 62 : origin_path_(origin_path) {}
62 63
63 virtual ServiceWorkerFetchStore* LoadStore(const std::string& key) OVERRIDE { 64 virtual ServiceWorkerCache* LoadCache(
64 base::CreateDirectory(CreatePersistentStorePath(origin_path_, key)); 65 const std::string& cache_name) OVERRIDE {
65 return ServiceWorkerFetchStore::CreatePersistentStore( 66 base::CreateDirectory(CreatePersistentCachePath(origin_path_, cache_name));
66 CreatePersistentStorePath(origin_path_, key), key); 67 return ServiceWorkerCache::CreatePersistentCache(
68 CreatePersistentCachePath(origin_path_, cache_name), cache_name);
67 } 69 }
68 70
69 virtual ServiceWorkerFetchStore* CreateStore( 71 virtual ServiceWorkerCache* CreateCache(
70 const std::string& key) OVERRIDE { 72 const std::string& cache_name) OVERRIDE {
71 base::FilePath store_path = CreatePersistentStorePath(origin_path_, key); 73 base::FilePath cache_path =
72 if (base::PathExists(store_path)) 74 CreatePersistentCachePath(origin_path_, cache_name);
73 base::DeleteFile(store_path, /* recursive */ true); 75 if (base::PathExists(cache_path))
74 return LoadStore(key); 76 base::DeleteFile(cache_path, /* recursive */ true);
77 return LoadCache(cache_name);
75 } 78 }
76 79
77 virtual bool CleanUpDeletedStore(const std::string& key) OVERRIDE { 80 virtual bool CleanUpDeletedCache(const std::string& cache_name) OVERRIDE {
78 return base::DeleteFile(CreatePersistentStorePath(origin_path_, key), true); 81 return base::DeleteFile(CreatePersistentCachePath(origin_path_, cache_name),
82 true);
79 } 83 }
80 84
81 virtual bool WriteIndex(StoreMap* stores) OVERRIDE { 85 virtual bool WriteIndex(CacheMap* caches) OVERRIDE {
82 ServiceWorkerCacheStorageIndex index; 86 ServiceWorkerCacheStorageIndex index;
83 87
84 for (StoreMap::const_iterator iter(stores); !iter.IsAtEnd(); 88 for (CacheMap::const_iterator iter(caches); !iter.IsAtEnd();
85 iter.Advance()) { 89 iter.Advance()) {
86 const ServiceWorkerFetchStore* store = iter.GetCurrentValue(); 90 const ServiceWorkerCache* cache = iter.GetCurrentValue();
87 ServiceWorkerCacheStorageIndex::Cache* cache = index.add_cache(); 91 ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache();
88 cache->set_name(store->name()); 92 index_cache->set_name(cache->name());
89 cache->set_size(0); // TODO(jkarlin): Make this real. 93 index_cache->set_size(0); // TODO(jkarlin): Make this real.
90 } 94 }
91 95
92 std::string serialized; 96 std::string serialized;
93 bool success = index.SerializeToString(&serialized); 97 bool success = index.SerializeToString(&serialized);
94 DCHECK(success); 98 DCHECK(success);
95 99
96 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); 100 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp");
97 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); 101 base::FilePath index_path = origin_path_.AppendASCII("index.txt");
98 102
99 int bytes_written = 103 int bytes_written =
(...skipping 15 matching lines...) Expand all
115 return; 119 return;
116 120
117 ServiceWorkerCacheStorageIndex index; 121 ServiceWorkerCacheStorageIndex index;
118 index.ParseFromString(serialized); 122 index.ParseFromString(serialized);
119 123
120 for (int i = 0, max = index.cache_size(); i < max; ++i) { 124 for (int i = 0, max = index.cache_size(); i < max; ++i) {
121 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i); 125 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i);
122 names->push_back(cache.name()); 126 names->push_back(cache.name());
123 } 127 }
124 128
125 // TODO(jkarlin): Delete stores that are in the directory and not returned 129 // TODO(jkarlin): Delete caches that are in the directory and not returned
126 // in LoadIndex. 130 // in LoadIndex.
127 return; 131 return;
128 } 132 }
129 133
130 private: 134 private:
131 std::string HexedHash(const std::string& value) { 135 std::string HexedHash(const std::string& value) {
132 std::string value_hash = base::SHA1HashString(value); 136 std::string value_hash = base::SHA1HashString(value);
133 std::string valued_hexed_hash = base::StringToLowerASCII( 137 std::string valued_hexed_hash = base::StringToLowerASCII(
134 base::HexEncode(value_hash.c_str(), value_hash.length())); 138 base::HexEncode(value_hash.c_str(), value_hash.length()));
135 return valued_hexed_hash; 139 return valued_hexed_hash;
136 } 140 }
137 141
138 base::FilePath CreatePersistentStorePath(const base::FilePath& origin_path, 142 base::FilePath CreatePersistentCachePath(const base::FilePath& origin_path,
139 const std::string& store_name) { 143 const std::string& cache_name) {
140 return origin_path.AppendASCII(HexedHash(store_name)); 144 return origin_path.AppendASCII(HexedHash(cache_name));
141 } 145 }
142 146
143 const base::FilePath origin_path_; 147 const base::FilePath origin_path_;
144 }; 148 };
145 149
146 ServiceWorkerFetchStores::ServiceWorkerFetchStores( 150 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage(
147 const base::FilePath& path, 151 const base::FilePath& path,
148 bool memory_only, 152 bool memory_only,
149 const scoped_refptr<base::MessageLoopProxy>& callback_loop) 153 const scoped_refptr<base::MessageLoopProxy>& callback_loop)
150 : initialized_(false), origin_path_(path), callback_loop_(callback_loop) { 154 : initialized_(false), origin_path_(path), callback_loop_(callback_loop) {
151 if (memory_only) 155 if (memory_only)
152 stores_loader_.reset(new MemoryLoader()); 156 cache_loader_.reset(new MemoryLoader());
153 else 157 else
154 stores_loader_.reset(new SimpleCacheLoader(origin_path_)); 158 cache_loader_.reset(new SimpleCacheLoader(origin_path_));
155 } 159 }
156 160
157 ServiceWorkerFetchStores::~ServiceWorkerFetchStores() { 161 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() {
158 } 162 }
159 163
160 void ServiceWorkerFetchStores::CreateStore( 164 void ServiceWorkerCacheStorage::CreateCache(
161 const std::string& store_name, 165 const std::string& cache_name,
162 const StoreAndErrorCallback& callback) { 166 const CacheAndErrorCallback& callback) {
163 LazyInit(); 167 LazyInit();
164 168
165 if (store_name.empty()) { 169 if (cache_name.empty()) {
166 callback_loop_->PostTask( 170 callback_loop_->PostTask(
167 FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EMPTY_KEY)); 171 FROM_HERE, base::Bind(callback, 0, CACHE_STORAGE_ERROR_EMPTY_KEY));
168 return; 172 return;
169 } 173 }
170 174
171 if (GetLoadedStore(store_name)) { 175 if (GetLoadedCache(cache_name)) {
172 callback_loop_->PostTask( 176 callback_loop_->PostTask(
173 FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EXISTS)); 177 FROM_HERE, base::Bind(callback, 0, CACHE_STORAGE_ERROR_EXISTS));
174 return; 178 return;
175 } 179 }
176 180
177 ServiceWorkerFetchStore* store = stores_loader_->CreateStore(store_name); 181 ServiceWorkerCache* cache = cache_loader_->CreateCache(cache_name);
178 182
179 if (!store) { 183 if (!cache) {
180 callback_loop_->PostTask( 184 callback_loop_->PostTask(
181 FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_STORAGE)); 185 FROM_HERE, base::Bind(callback, 0, CACHE_STORAGE_ERROR_STORAGE));
182 return; 186 return;
183 } 187 }
184 188
185 InitStore(store); 189 InitCache(cache);
186 190
187 stores_loader_->WriteIndex(&store_map_); 191 cache_loader_->WriteIndex(&cache_map_);
188 192
189 store->CreateBackend( 193 cache->CreateBackend(
190 base::Bind(&ServiceWorkerFetchStores::InitializeStoreCallback, 194 base::Bind(&ServiceWorkerCacheStorage::InitializeCacheCallback,
191 base::Unretained(this), 195 base::Unretained(this),
192 store, 196 cache,
193 callback)); 197 callback));
194 } 198 }
195 199
196 void ServiceWorkerFetchStores::GetStore(const std::string& store_name, 200 void ServiceWorkerCacheStorage::GetCache(
197 const StoreAndErrorCallback& callback) { 201 const std::string& cache_name,
202 const CacheAndErrorCallback& callback) {
198 LazyInit(); 203 LazyInit();
199 204
200 if (store_name.empty()) { 205 if (cache_name.empty()) {
201 callback_loop_->PostTask( 206 callback_loop_->PostTask(
202 FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_EMPTY_KEY)); 207 FROM_HERE, base::Bind(callback, 0, CACHE_STORAGE_ERROR_EMPTY_KEY));
203 return; 208 return;
204 } 209 }
205 210
206 ServiceWorkerFetchStore* store = GetLoadedStore(store_name); 211 ServiceWorkerCache* cache = GetLoadedCache(cache_name);
207 if (!store) { 212 if (!cache) {
208 callback_loop_->PostTask( 213 callback_loop_->PostTask(
209 FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_NOT_FOUND)); 214 FROM_HERE, base::Bind(callback, 0, CACHE_STORAGE_ERROR_NOT_FOUND));
210 return; 215 return;
211 } 216 }
212 217
213 store->CreateBackend( 218 cache->CreateBackend(
214 base::Bind(&ServiceWorkerFetchStores::InitializeStoreCallback, 219 base::Bind(&ServiceWorkerCacheStorage::InitializeCacheCallback,
215 base::Unretained(this), 220 base::Unretained(this),
216 store, 221 cache,
217 callback)); 222 callback));
218 } 223 }
219 224
220 void ServiceWorkerFetchStores::HasStore(const std::string& store_name, 225 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name,
221 const BoolAndErrorCallback& callback) { 226 const BoolAndErrorCallback& callback) {
222 LazyInit(); 227 LazyInit();
223 228
224 if (store_name.empty()) { 229 if (cache_name.empty()) {
225 callback_loop_->PostTask( 230 callback_loop_->PostTask(
226 FROM_HERE, base::Bind(callback, false, FETCH_STORES_ERROR_EMPTY_KEY)); 231 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_EMPTY_KEY));
227 return; 232 return;
228 } 233 }
229 234
230 bool has_store = GetLoadedStore(store_name) != NULL; 235 bool has_cache = GetLoadedCache(cache_name) != NULL;
231 236
232 callback_loop_->PostTask( 237 callback_loop_->PostTask(
233 FROM_HERE, 238 FROM_HERE,
234 base::Bind( 239 base::Bind(
235 callback, has_store, FETCH_STORES_ERROR_NO_ERROR)); 240 callback, has_cache, CACHE_STORAGE_ERROR_NO_ERROR));
236 } 241 }
237 242
238 void ServiceWorkerFetchStores::DeleteStore( 243 void ServiceWorkerCacheStorage::DeleteCache(
239 const std::string& store_name, 244 const std::string& cache_name,
240 const BoolAndErrorCallback& callback) { 245 const BoolAndErrorCallback& callback) {
241 LazyInit(); 246 LazyInit();
242 247
243 if (store_name.empty()) { 248 if (cache_name.empty()) {
244 callback_loop_->PostTask( 249 callback_loop_->PostTask(
245 FROM_HERE, base::Bind(callback, false, FETCH_STORES_ERROR_EMPTY_KEY)); 250 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_EMPTY_KEY));
246 return; 251 return;
247 } 252 }
248 253
249 ServiceWorkerFetchStore* store = GetLoadedStore(store_name); 254 ServiceWorkerCache* cache = GetLoadedCache(cache_name);
250 if (!store) { 255 if (!cache) {
251 callback_loop_->PostTask( 256 callback_loop_->PostTask(
252 FROM_HERE, base::Bind(callback, false, FETCH_STORES_ERROR_NOT_FOUND)); 257 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND));
253 return; 258 return;
254 } 259 }
255 260
256 name_map_.erase(store_name); 261 name_map_.erase(cache_name);
257 store_map_.Remove(store->id()); // deletes store 262 cache_map_.Remove(cache->id()); // deletes cache
258 263
259 stores_loader_->WriteIndex(&store_map_); // Update the index. 264 cache_loader_->WriteIndex(&cache_map_); // Update the index.
260 265
261 stores_loader_->CleanUpDeletedStore(store_name); 266 cache_loader_->CleanUpDeletedCache(cache_name);
262 267
263 callback_loop_->PostTask( 268 callback_loop_->PostTask(
264 FROM_HERE, base::Bind(callback, true, FETCH_STORES_ERROR_NO_ERROR)); 269 FROM_HERE, base::Bind(callback, true, CACHE_STORAGE_ERROR_NO_ERROR));
265 } 270 }
266 271
267 void ServiceWorkerFetchStores::EnumerateStores( 272 void ServiceWorkerCacheStorage::EnumerateCaches(
268 const StringsAndErrorCallback& callback) { 273 const StringsAndErrorCallback& callback) {
269 LazyInit(); 274 LazyInit();
270 275
271 std::vector<std::string> names; 276 std::vector<std::string> names;
272 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end(); 277 for (NameMap::const_iterator it = name_map_.begin(); it != name_map_.end();
273 ++it) { 278 ++it) {
274 names.push_back(it->first); 279 names.push_back(it->first);
275 } 280 }
276 281
277 callback_loop_->PostTask( 282 callback_loop_->PostTask(
278 FROM_HERE, base::Bind(callback, names, FETCH_STORES_ERROR_NO_ERROR)); 283 FROM_HERE, base::Bind(callback, names, CACHE_STORAGE_ERROR_NO_ERROR));
279 } 284 }
280 285
281 void ServiceWorkerFetchStores::InitializeStoreCallback( 286 void ServiceWorkerCacheStorage::InitializeCacheCallback(
282 const ServiceWorkerFetchStore* store, 287 const ServiceWorkerCache* cache,
283 const StoreAndErrorCallback& callback, 288 const CacheAndErrorCallback& callback,
284 bool success) { 289 bool success) {
285 if (!success) { 290 if (!success) {
286 // TODO(jkarlin): This should delete the directory and try again in case 291 // TODO(jkarlin): This should delete the directory and try again in case
287 // the cache is simply corrupt. 292 // the cache is simply corrupt.
288 callback_loop_->PostTask( 293 callback_loop_->PostTask(
289 FROM_HERE, base::Bind(callback, 0, FETCH_STORES_ERROR_STORAGE)); 294 FROM_HERE, base::Bind(callback, 0, CACHE_STORAGE_ERROR_STORAGE));
290 return; 295 return;
291 } 296 }
292 callback_loop_->PostTask( 297 callback_loop_->PostTask(
293 FROM_HERE, 298 FROM_HERE,
294 base::Bind(callback, store->id(), FETCH_STORES_ERROR_NO_ERROR)); 299 base::Bind(callback, cache->id(), CACHE_STORAGE_ERROR_NO_ERROR));
295 } 300 }
296 301
297 // Init is run lazily so that it is called on the proper MessageLoop. 302 // Init is run lazily so that it is called on the proper MessageLoop.
298 void ServiceWorkerFetchStores::LazyInit() { 303 void ServiceWorkerCacheStorage::LazyInit() {
299 if (initialized_) 304 if (initialized_)
300 return; 305 return;
301 306
302 std::vector<std::string> indexed_store_names; 307 std::vector<std::string> indexed_cache_names;
303 stores_loader_->LoadIndex(&indexed_store_names); 308 cache_loader_->LoadIndex(&indexed_cache_names);
304 309
305 for (std::vector<std::string>::const_iterator it = 310 for (std::vector<std::string>::const_iterator it =
306 indexed_store_names.begin(); 311 indexed_cache_names.begin();
307 it != indexed_store_names.end(); 312 it != indexed_cache_names.end();
308 ++it) { 313 ++it) {
309 ServiceWorkerFetchStore* store = stores_loader_->LoadStore(*it); 314 ServiceWorkerCache* cache = cache_loader_->LoadCache(*it);
310 InitStore(store); 315 InitCache(cache);
311 } 316 }
312 initialized_ = true; 317 initialized_ = true;
313 } 318 }
314 319
315 void ServiceWorkerFetchStores::InitStore(ServiceWorkerFetchStore* store) { 320 void ServiceWorkerCacheStorage::InitCache(ServiceWorkerCache* cache) {
316 StoreID id = store_map_.Add(store); 321 CacheID id = cache_map_.Add(cache);
317 name_map_.insert(std::make_pair(store->name(), id)); 322 name_map_.insert(std::make_pair(cache->name(), id));
318 store->set_id(id); 323 cache->set_id(id);
319 } 324 }
320 325
321 ServiceWorkerFetchStore* ServiceWorkerFetchStores::GetLoadedStore( 326 ServiceWorkerCache* ServiceWorkerCacheStorage::GetLoadedCache(
322 const std::string& key) const { 327 const std::string& cache_name) const {
323 DCHECK(initialized_); 328 DCHECK(initialized_);
324 329
325 NameMap::const_iterator it = name_map_.find(key); 330 NameMap::const_iterator it = name_map_.find(cache_name);
326 if (it == name_map_.end()) 331 if (it == name_map_.end())
327 return NULL; 332 return NULL;
328 333
329 ServiceWorkerFetchStore* store = store_map_.Lookup(it->second); 334 ServiceWorkerCache* cache = cache_map_.Lookup(it->second);
330 DCHECK(store); 335 DCHECK(cache);
331 return store; 336 return cache;
332 } 337 }
333 338
334 } // namespace content 339 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698