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/service_worker/service_worker_cache_storage.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" |
(...skipping 21 matching lines...) Expand all Loading... |
32 struct ServiceWorkerCacheStorage::CacheContext { | 32 struct ServiceWorkerCacheStorage::CacheContext { |
33 CacheContext(const std::string& name, | 33 CacheContext(const std::string& name, |
34 CacheID id, | 34 CacheID id, |
35 scoped_ptr<ServiceWorkerCache> cache) | 35 scoped_ptr<ServiceWorkerCache> cache) |
36 : name(name), id(id), cache(cache.Pass()) {} | 36 : name(name), id(id), cache(cache.Pass()) {} |
37 std::string name; | 37 std::string name; |
38 CacheID id; | 38 CacheID id; |
39 scoped_ptr<ServiceWorkerCache> cache; | 39 scoped_ptr<ServiceWorkerCache> cache; |
40 }; | 40 }; |
41 | 41 |
42 // Handles the loading and clean up of ServiceWorkerCache objects. | 42 // Handles the loading and clean up of ServiceWorkerCache objects. The |
43 class ServiceWorkerCacheStorage::CacheLoader | 43 // callback of every public method is guaranteed to be called. |
44 : public base::RefCountedThreadSafe< | 44 class ServiceWorkerCacheStorage::CacheLoader { |
45 ServiceWorkerCacheStorage::CacheLoader> { | |
46 public: | 45 public: |
47 typedef base::Callback<void(scoped_ptr<ServiceWorkerCache>)> CacheCallback; | 46 typedef base::Callback<void(scoped_ptr<ServiceWorkerCache>)> CacheCallback; |
48 typedef base::Callback<void(bool)> BoolCallback; | 47 typedef base::Callback<void(bool)> BoolCallback; |
49 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> | 48 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> |
50 StringsCallback; | 49 StringsCallback; |
51 | 50 |
52 CacheLoader(base::SequencedTaskRunner* cache_task_runner, | 51 CacheLoader(base::SequencedTaskRunner* cache_task_runner, |
53 net::URLRequestContext* request_context, | 52 net::URLRequestContext* request_context, |
54 base::WeakPtr<storage::BlobStorageContext> blob_context) | 53 base::WeakPtr<storage::BlobStorageContext> blob_context) |
55 : cache_task_runner_(cache_task_runner), | 54 : cache_task_runner_(cache_task_runner), |
56 request_context_(request_context), | 55 request_context_(request_context), |
57 blob_context_(blob_context) {} | 56 blob_context_(blob_context) {} |
58 | 57 |
| 58 virtual ~CacheLoader() {} |
| 59 |
59 // Loads the given cache_name, the cache is NULL if it fails. If the cache | 60 // Loads the given cache_name, the cache is NULL if it fails. If the cache |
60 // doesn't exist a new one is created. | 61 // doesn't exist a new one is created. |
61 virtual void LoadCache(const std::string& cache_name, | 62 virtual void LoadCache(const std::string& cache_name, |
62 const CacheCallback& callback) = 0; | 63 const CacheCallback& callback) = 0; |
63 | 64 |
64 // Deletes any pre-existing cache of the same name and then loads it. | 65 // Deletes any pre-existing cache of the same name and then loads it. |
65 virtual void CreateCache(const std::string& cache_name, | 66 virtual void CreateCache(const std::string& cache_name, |
66 const CacheCallback& callback) = 0; | 67 const CacheCallback& callback) = 0; |
67 | 68 |
68 // After the backend has been deleted, do any extra house keeping such as | 69 // After the backend has been deleted, do any extra house keeping such as |
69 // removing the cache's directory. | 70 // removing the cache's directory. |
70 virtual void CleanUpDeletedCache(const std::string& key, | 71 virtual void CleanUpDeletedCache(const std::string& key, |
71 const BoolCallback& callback) = 0; | 72 const BoolCallback& callback) = 0; |
72 | 73 |
73 // Writes the cache names (and sizes) to disk if applicable. | 74 // Writes the cache names (and sizes) to disk if applicable. |
74 virtual void WriteIndex(const CacheMap& caches, | 75 virtual void WriteIndex(const CacheMap& caches, |
75 const BoolCallback& callback) = 0; | 76 const BoolCallback& callback) = 0; |
76 | 77 |
77 // Loads the cache names from disk if applicable. | 78 // Loads the cache names from disk if applicable. |
78 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 79 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
79 const StringsCallback& callback) = 0; | 80 const StringsCallback& callback) = 0; |
80 | 81 |
81 protected: | 82 protected: |
82 friend class base::RefCountedThreadSafe< | |
83 ServiceWorkerCacheStorage::CacheLoader>; | |
84 | |
85 virtual ~CacheLoader() {} | |
86 virtual void LoadCacheImpl(const std::string&) {} | 83 virtual void LoadCacheImpl(const std::string&) {} |
87 | 84 |
88 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 85 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
89 net::URLRequestContext* request_context_; | 86 net::URLRequestContext* request_context_; |
90 base::WeakPtr<storage::BlobStorageContext> blob_context_; | 87 base::WeakPtr<storage::BlobStorageContext> blob_context_; |
91 }; | 88 }; |
92 | 89 |
93 class ServiceWorkerCacheStorage::MemoryLoader | 90 class ServiceWorkerCacheStorage::MemoryLoader |
94 : public ServiceWorkerCacheStorage::CacheLoader { | 91 : public ServiceWorkerCacheStorage::CacheLoader { |
95 public: | 92 public: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 }; | 126 }; |
130 | 127 |
131 class ServiceWorkerCacheStorage::SimpleCacheLoader | 128 class ServiceWorkerCacheStorage::SimpleCacheLoader |
132 : public ServiceWorkerCacheStorage::CacheLoader { | 129 : public ServiceWorkerCacheStorage::CacheLoader { |
133 public: | 130 public: |
134 SimpleCacheLoader(const base::FilePath& origin_path, | 131 SimpleCacheLoader(const base::FilePath& origin_path, |
135 base::SequencedTaskRunner* cache_task_runner, | 132 base::SequencedTaskRunner* cache_task_runner, |
136 net::URLRequestContext* request_context, | 133 net::URLRequestContext* request_context, |
137 base::WeakPtr<storage::BlobStorageContext> blob_context) | 134 base::WeakPtr<storage::BlobStorageContext> blob_context) |
138 : CacheLoader(cache_task_runner, request_context, blob_context), | 135 : CacheLoader(cache_task_runner, request_context, blob_context), |
139 origin_path_(origin_path) {} | 136 origin_path_(origin_path), |
| 137 weak_ptr_factory_(this) {} |
140 | 138 |
141 virtual void LoadCache(const std::string& cache_name, | 139 virtual void LoadCache(const std::string& cache_name, |
142 const CacheCallback& callback) OVERRIDE { | 140 const CacheCallback& callback) OVERRIDE { |
143 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
144 | 142 |
145 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) | 143 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) |
146 // 2. Create the cache object. (LoadDidCreateDirectory) | 144 // 2. Create the cache object. (LoadDidCreateDirectory) |
147 | 145 |
148 cache_task_runner_->PostTask( | 146 cache_task_runner_->PostTask( |
149 FROM_HERE, | 147 FROM_HERE, |
150 base::Bind(&SimpleCacheLoader::LoadCreateDirectoryInPool, | 148 base::Bind(&SimpleCacheLoader::LoadCreateDirectoryInPool, |
151 this, | |
152 CreatePersistentCachePath(origin_path_, cache_name), | 149 CreatePersistentCachePath(origin_path_, cache_name), |
153 cache_name, | 150 cache_name, |
154 callback, | 151 callback, |
| 152 weak_ptr_factory_.GetWeakPtr(), |
155 base::MessageLoopProxy::current())); | 153 base::MessageLoopProxy::current())); |
156 } | 154 } |
157 | 155 |
158 void LoadCreateDirectoryInPool( | 156 static void LoadCreateDirectoryInPool( |
159 const base::FilePath& path, | 157 const base::FilePath& path, |
160 const std::string& cache_name, | 158 const std::string& cache_name, |
161 const CacheCallback& callback, | 159 const CacheCallback& callback, |
| 160 base::WeakPtr<SimpleCacheLoader> loader, |
162 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 161 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
163 DCHECK(cache_task_runner_->RunsTasksOnCurrentThread()); | |
164 | |
165 bool rv = base::CreateDirectory(path); | 162 bool rv = base::CreateDirectory(path); |
166 original_loop->PostTask( | 163 original_loop->PostTask( |
167 FROM_HERE, | 164 FROM_HERE, |
168 base::Bind(&SimpleCacheLoader::LoadDidCreateDirectory, | 165 base::Bind(&SimpleCacheLoader::LoadDidCreateDirectory, |
169 this, | |
170 cache_name, | 166 cache_name, |
171 callback, | 167 callback, |
| 168 loader, |
172 rv)); | 169 rv)); |
173 } | 170 } |
174 | 171 |
175 void LoadDidCreateDirectory(const std::string& cache_name, | 172 static void LoadDidCreateDirectory(const std::string& cache_name, |
176 const CacheCallback& callback, | 173 const CacheCallback& callback, |
177 bool dir_rv) { | 174 base::WeakPtr<SimpleCacheLoader> loader, |
| 175 bool dir_rv) { |
178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 176 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
179 | 177 |
180 if (!dir_rv) { | 178 if (!dir_rv || !loader) { |
181 callback.Run(scoped_ptr<ServiceWorkerCache>()); | 179 callback.Run(scoped_ptr<ServiceWorkerCache>()); |
182 return; | 180 return; |
183 } | 181 } |
184 | 182 |
185 scoped_ptr<ServiceWorkerCache> cache = | 183 scoped_ptr<ServiceWorkerCache> cache = |
186 ServiceWorkerCache::CreatePersistentCache( | 184 ServiceWorkerCache::CreatePersistentCache( |
187 CreatePersistentCachePath(origin_path_, cache_name), | 185 CreatePersistentCachePath(loader->origin_path_, cache_name), |
188 request_context_, | 186 loader->request_context_, |
189 blob_context_); | 187 loader->blob_context_); |
190 callback.Run(cache.Pass()); | 188 callback.Run(cache.Pass()); |
191 } | 189 } |
192 | 190 |
193 virtual void CreateCache(const std::string& cache_name, | 191 virtual void CreateCache(const std::string& cache_name, |
194 const CacheCallback& callback) OVERRIDE { | 192 const CacheCallback& callback) OVERRIDE { |
195 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 193 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
196 | 194 |
197 // 1. Delete the cache's directory if it exists. | 195 // 1. Delete the cache's directory if it exists. |
198 // (CreateCacheDeleteFilesInPool) | 196 // (CreateCacheDeleteFilesInPool) |
199 // 2. Load the cache. (LoadCreateDirectoryInPool) | 197 // 2. Load the cache. (LoadCreateDirectoryInPool) |
200 | 198 |
201 base::FilePath cache_path = | 199 base::FilePath cache_path = |
202 CreatePersistentCachePath(origin_path_, cache_name); | 200 CreatePersistentCachePath(origin_path_, cache_name); |
203 | 201 |
204 cache_task_runner_->PostTask( | 202 cache_task_runner_->PostTask( |
205 FROM_HERE, | 203 FROM_HERE, |
206 base::Bind(&SimpleCacheLoader::CreateCacheDeleteFilesInPool, | 204 base::Bind(&SimpleCacheLoader::CreateCacheDeleteFilesInPool, |
207 this, | |
208 cache_path, | 205 cache_path, |
209 cache_name, | 206 cache_name, |
210 callback, | 207 callback, |
| 208 weak_ptr_factory_.GetWeakPtr(), |
211 base::MessageLoopProxy::current())); | 209 base::MessageLoopProxy::current())); |
212 } | 210 } |
213 | 211 |
214 void CreateCacheDeleteFilesInPool( | 212 static void CreateCacheDeleteFilesInPool( |
215 const base::FilePath& cache_path, | 213 const base::FilePath& cache_path, |
216 const std::string& cache_name, | 214 const std::string& cache_name, |
217 const CacheCallback& callback, | 215 const CacheCallback& callback, |
| 216 base::WeakPtr<SimpleCacheLoader> loader, |
218 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 217 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
219 DCHECK(cache_task_runner_->RunsTasksOnCurrentThread()); | |
220 | |
221 base::FilePath path(cache_path); | 218 base::FilePath path(cache_path); |
222 if (base::PathExists(path)) | 219 if (base::PathExists(path)) |
223 base::DeleteFile(path, /* recursive */ true); | 220 base::DeleteFile(path, /* recursive */ true); |
224 | 221 |
225 // Jump straight into LoadCache on the same thread. | 222 // Jump straight into LoadCache on the same thread. |
226 cache_task_runner_->PostTask( | 223 LoadCreateDirectoryInPool( |
227 FROM_HERE, | 224 cache_path, cache_name, callback, loader, original_loop); |
228 base::Bind(&SimpleCacheLoader::LoadCreateDirectoryInPool, | |
229 this, | |
230 cache_path, | |
231 cache_name, | |
232 callback, | |
233 original_loop)); | |
234 } | 225 } |
235 | 226 |
236 virtual void CleanUpDeletedCache(const std::string& cache_name, | 227 virtual void CleanUpDeletedCache(const std::string& cache_name, |
237 const BoolCallback& callback) OVERRIDE { | 228 const BoolCallback& callback) OVERRIDE { |
238 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 229 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
239 | 230 |
240 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) | 231 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) |
241 | 232 |
242 base::FilePath cache_path = | 233 base::FilePath cache_path = |
243 CreatePersistentCachePath(origin_path_, cache_name); | 234 CreatePersistentCachePath(origin_path_, cache_name); |
244 cache_task_runner_->PostTask( | 235 cache_task_runner_->PostTask( |
245 FROM_HERE, | 236 FROM_HERE, |
246 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, | 237 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, |
247 this, | |
248 cache_path, | 238 cache_path, |
249 callback, | 239 callback, |
250 base::MessageLoopProxy::current())); | 240 base::MessageLoopProxy::current())); |
251 } | 241 } |
252 | 242 |
253 void CleanUpDeleteCacheDirInPool( | 243 static void CleanUpDeleteCacheDirInPool( |
254 const base::FilePath& cache_path, | 244 const base::FilePath& cache_path, |
255 const BoolCallback& callback, | 245 const BoolCallback& callback, |
256 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 246 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
257 DCHECK(cache_task_runner_->RunsTasksOnCurrentThread()); | |
258 | |
259 bool rv = base::DeleteFile(cache_path, true); | 247 bool rv = base::DeleteFile(cache_path, true); |
260 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 248 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); |
261 } | 249 } |
262 | 250 |
263 virtual void WriteIndex(const CacheMap& caches, | 251 virtual void WriteIndex(const CacheMap& caches, |
264 const BoolCallback& callback) OVERRIDE { | 252 const BoolCallback& callback) OVERRIDE { |
265 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 253 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
266 | 254 |
267 // 1. Create the index file as a string. (WriteIndex) | 255 // 1. Create the index file as a string. (WriteIndex) |
268 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 256 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
(...skipping 11 matching lines...) Expand all Loading... |
280 std::string serialized; | 268 std::string serialized; |
281 bool success = index.SerializeToString(&serialized); | 269 bool success = index.SerializeToString(&serialized); |
282 DCHECK(success); | 270 DCHECK(success); |
283 | 271 |
284 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 272 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
285 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); | 273 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); |
286 | 274 |
287 cache_task_runner_->PostTask( | 275 cache_task_runner_->PostTask( |
288 FROM_HERE, | 276 FROM_HERE, |
289 base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, | 277 base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, |
290 this, | |
291 tmp_path, | 278 tmp_path, |
292 index_path, | 279 index_path, |
293 serialized, | 280 serialized, |
294 callback, | 281 callback, |
295 base::MessageLoopProxy::current())); | 282 base::MessageLoopProxy::current())); |
296 } | 283 } |
297 | 284 |
298 void WriteIndexWriteToFileInPool( | 285 static void WriteIndexWriteToFileInPool( |
299 const base::FilePath& tmp_path, | 286 const base::FilePath& tmp_path, |
300 const base::FilePath& index_path, | 287 const base::FilePath& index_path, |
301 const std::string& data, | 288 const std::string& data, |
302 const BoolCallback& callback, | 289 const BoolCallback& callback, |
303 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 290 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
304 DCHECK(cache_task_runner_->RunsTasksOnCurrentThread()); | |
305 | |
306 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); | 291 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); |
307 if (bytes_written != implicit_cast<int>(data.size())) { | 292 if (bytes_written != implicit_cast<int>(data.size())) { |
308 base::DeleteFile(tmp_path, /* recursive */ false); | 293 base::DeleteFile(tmp_path, /* recursive */ false); |
309 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); | 294 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); |
310 } | 295 } |
311 | 296 |
312 // Atomically rename the temporary index file to become the real one. | 297 // Atomically rename the temporary index file to become the real one. |
313 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); | 298 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); |
314 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 299 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); |
315 } | 300 } |
316 | 301 |
317 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > names, | 302 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > names, |
318 const StringsCallback& callback) OVERRIDE { | 303 const StringsCallback& callback) OVERRIDE { |
319 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
320 | 305 |
321 // 1. Read the file from disk. (LoadIndexReadFileInPool) | 306 // 1. Read the file from disk. (LoadIndexReadFileInPool) |
322 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) | 307 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) |
323 | 308 |
324 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); | 309 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); |
325 | 310 |
326 cache_task_runner_->PostTask( | 311 cache_task_runner_->PostTask( |
327 FROM_HERE, | 312 FROM_HERE, |
328 base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, | 313 base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, |
329 this, | |
330 index_path, | 314 index_path, |
331 base::Passed(names.Pass()), | 315 base::Passed(names.Pass()), |
332 callback, | 316 callback, |
333 base::MessageLoopProxy::current())); | 317 base::MessageLoopProxy::current())); |
334 } | 318 } |
335 | 319 |
336 void LoadIndexReadFileInPool( | 320 static void LoadIndexReadFileInPool( |
337 const base::FilePath& index_path, | 321 const base::FilePath& index_path, |
338 scoped_ptr<std::vector<std::string> > names, | 322 scoped_ptr<std::vector<std::string> > names, |
339 const StringsCallback& callback, | 323 const StringsCallback& callback, |
340 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 324 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
341 DCHECK(cache_task_runner_->RunsTasksOnCurrentThread()); | |
342 | |
343 std::string body; | 325 std::string body; |
344 base::ReadFileToString(index_path, &body); | 326 base::ReadFileToString(index_path, &body); |
345 | 327 |
346 original_loop->PostTask(FROM_HERE, | 328 original_loop->PostTask(FROM_HERE, |
347 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, | 329 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, |
348 this, | |
349 base::Passed(names.Pass()), | 330 base::Passed(names.Pass()), |
350 callback, | 331 callback, |
351 body)); | 332 body)); |
352 } | 333 } |
353 | 334 |
354 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string> > names, | 335 static void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string> > names, |
355 const StringsCallback& callback, | 336 const StringsCallback& callback, |
356 const std::string& serialized) { | 337 const std::string& serialized) { |
357 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 338 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
358 | 339 |
359 ServiceWorkerCacheStorageIndex index; | 340 ServiceWorkerCacheStorageIndex index; |
360 if (index.ParseFromString(serialized)) { | 341 if (index.ParseFromString(serialized)) { |
361 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 342 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
362 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i); | 343 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i); |
363 names->push_back(cache.name()); | 344 names->push_back(cache.name()); |
364 } | 345 } |
365 } | 346 } |
366 | 347 |
367 // TODO(jkarlin): Delete caches that are in the directory and not returned | 348 // TODO(jkarlin): Delete caches that are in the directory and not returned |
368 // in LoadIndex. | 349 // in LoadIndex. |
369 callback.Run(names.Pass()); | 350 callback.Run(names.Pass()); |
370 } | 351 } |
371 | 352 |
372 private: | 353 private: |
373 virtual ~SimpleCacheLoader() {} | 354 virtual ~SimpleCacheLoader() {} |
374 | 355 |
375 std::string HexedHash(const std::string& value) { | 356 static std::string HexedHash(const std::string& value) { |
376 std::string value_hash = base::SHA1HashString(value); | 357 std::string value_hash = base::SHA1HashString(value); |
377 std::string valued_hexed_hash = base::StringToLowerASCII( | 358 std::string valued_hexed_hash = base::StringToLowerASCII( |
378 base::HexEncode(value_hash.c_str(), value_hash.length())); | 359 base::HexEncode(value_hash.c_str(), value_hash.length())); |
379 return valued_hexed_hash; | 360 return valued_hexed_hash; |
380 } | 361 } |
381 | 362 |
382 base::FilePath CreatePersistentCachePath(const base::FilePath& origin_path, | 363 static base::FilePath CreatePersistentCachePath( |
383 const std::string& cache_name) { | 364 const base::FilePath& origin_path, |
| 365 const std::string& cache_name) { |
384 return origin_path.AppendASCII(HexedHash(cache_name)); | 366 return origin_path.AppendASCII(HexedHash(cache_name)); |
385 } | 367 } |
386 | 368 |
387 const base::FilePath origin_path_; | 369 const base::FilePath origin_path_; |
| 370 |
| 371 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; |
388 }; | 372 }; |
389 | 373 |
390 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( | 374 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( |
391 const base::FilePath& path, | 375 const base::FilePath& path, |
392 bool memory_only, | 376 bool memory_only, |
393 base::SequencedTaskRunner* cache_task_runner, | 377 base::SequencedTaskRunner* cache_task_runner, |
394 net::URLRequestContext* request_context, | 378 net::URLRequestContext* request_context, |
395 base::WeakPtr<storage::BlobStorageContext> blob_context) | 379 base::WeakPtr<storage::BlobStorageContext> blob_context) |
396 : initialized_(false), | 380 : initialized_(false), |
397 next_cache_id_(0), | 381 next_cache_id_(0), |
398 origin_path_(path), | 382 origin_path_(path), |
399 cache_task_runner_(cache_task_runner), | 383 cache_task_runner_(cache_task_runner), |
400 weak_factory_(this) { | 384 weak_factory_(this) { |
401 if (memory_only) | 385 if (memory_only) |
402 cache_loader_ = new MemoryLoader( | 386 cache_loader_.reset(new MemoryLoader( |
403 cache_task_runner_.get(), request_context, blob_context); | 387 cache_task_runner_.get(), request_context, blob_context)); |
404 else | 388 else |
405 cache_loader_ = new SimpleCacheLoader( | 389 cache_loader_.reset(new SimpleCacheLoader( |
406 origin_path_, cache_task_runner_.get(), request_context, blob_context); | 390 origin_path_, cache_task_runner_.get(), request_context, blob_context)); |
407 } | 391 } |
408 | 392 |
409 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { | 393 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { |
410 STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end()); | 394 STLDeleteContainerPairSecondPointers(cache_map_.begin(), cache_map_.end()); |
411 } | 395 } |
412 | 396 |
413 void ServiceWorkerCacheStorage::CreateCache( | 397 void ServiceWorkerCacheStorage::CreateCache( |
414 const std::string& cache_name, | 398 const std::string& cache_name, |
415 const CacheAndErrorCallback& callback) { | 399 const CacheAndErrorCallback& callback) { |
416 if (!initialized_) { | 400 if (!initialized_) { |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 NameMap::const_iterator name_iter = name_map_.find(cache_name); | 724 NameMap::const_iterator name_iter = name_map_.find(cache_name); |
741 if (name_iter == name_map_.end()) | 725 if (name_iter == name_map_.end()) |
742 return NULL; | 726 return NULL; |
743 | 727 |
744 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); | 728 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); |
745 DCHECK(map_iter != cache_map_.end()); | 729 DCHECK(map_iter != cache_map_.end()); |
746 return map_iter->second; | 730 return map_iter->second; |
747 } | 731 } |
748 | 732 |
749 } // namespace content | 733 } // namespace content |
OLD | NEW |