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" |
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/fileapi/chrome_blob_storage_context.h" |
14 #include "content/browser/service_worker/service_worker_cache.h" | 15 #include "content/browser/service_worker/service_worker_cache.h" |
15 #include "content/browser/service_worker/service_worker_cache.pb.h" | 16 #include "content/browser/service_worker/service_worker_cache.pb.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "net/base/directory_lister.h" | 18 #include "net/base/directory_lister.h" |
18 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 // Handles the loading and clean up of ServiceWorkerCache objects. | 23 // Handles the loading and clean up of ServiceWorkerCache objects. |
23 class ServiceWorkerCacheStorage::CacheLoader { | 24 class ServiceWorkerCacheStorage::CacheLoader { |
24 public: | 25 public: |
25 typedef base::Callback<void(ServiceWorkerCache*)> CacheCallback; | 26 typedef base::Callback<void(ServiceWorkerCache*)> CacheCallback; |
26 typedef base::Callback<void(bool)> BoolCallback; | 27 typedef base::Callback<void(bool)> BoolCallback; |
27 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> | 28 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> |
28 StringsCallback; | 29 StringsCallback; |
29 | 30 |
30 explicit CacheLoader(base::SequencedTaskRunner* cache_task_runner) | 31 explicit CacheLoader(base::SequencedTaskRunner* cache_task_runner, |
31 : cache_task_runner_(cache_task_runner) {} | 32 net::URLRequestContext* request_context, |
| 33 ChromeBlobStorageContext* blob_context) |
| 34 : cache_task_runner_(cache_task_runner), |
| 35 request_context_(request_context), |
| 36 blob_context_(blob_context) {} |
32 | 37 |
33 virtual ~CacheLoader() {}; | 38 virtual ~CacheLoader() {}; |
34 | 39 |
35 // Loads the given cache_name, the cache is NULL if it fails. If the cache | 40 // Loads the given cache_name, the cache is NULL if it fails. If the cache |
36 // doesn't exist a new one is created. | 41 // doesn't exist a new one is created. |
37 virtual void LoadCache(const std::string& cache_name, | 42 virtual void LoadCache(const std::string& cache_name, |
38 const CacheCallback& callback) = 0; | 43 const CacheCallback& callback) = 0; |
39 | 44 |
40 // Deletes any pre-existing cache of the same name and then loads it. | 45 // Deletes any pre-existing cache of the same name and then loads it. |
41 virtual void CreateCache(const std::string& cache_name, | 46 virtual void CreateCache(const std::string& cache_name, |
42 const CacheCallback& callback) = 0; | 47 const CacheCallback& callback) = 0; |
43 | 48 |
44 // After the backend has been deleted, do any extra house keeping such as | 49 // After the backend has been deleted, do any extra house keeping such as |
45 // removing the cache's directory. | 50 // removing the cache's directory. |
46 virtual void CleanUpDeletedCache(const std::string& key, | 51 virtual void CleanUpDeletedCache(const std::string& key, |
47 const BoolCallback& callback) = 0; | 52 const BoolCallback& callback) = 0; |
48 | 53 |
49 // Writes the cache names to disk if applicable. | 54 // Writes the cache names to disk if applicable. |
50 virtual void WriteIndex(CacheMap* caches, const BoolCallback& callback) = 0; | 55 virtual void WriteIndex(CacheMap* caches, const BoolCallback& callback) = 0; |
51 | 56 |
52 // Loads the cache names from disk if applicable. | 57 // Loads the cache names from disk if applicable. |
53 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 58 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
54 const StringsCallback& callback) = 0; | 59 const StringsCallback& callback) = 0; |
55 | 60 |
56 protected: | 61 protected: |
57 virtual void LoadCacheImpl(const std::string&) {} | 62 virtual void LoadCacheImpl(const std::string&) {} |
58 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 63 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 64 net::URLRequestContext* request_context_; |
| 65 ChromeBlobStorageContext* blob_context_; |
59 }; | 66 }; |
60 | 67 |
61 class ServiceWorkerCacheStorage::MemoryLoader | 68 class ServiceWorkerCacheStorage::MemoryLoader |
62 : public ServiceWorkerCacheStorage::CacheLoader { | 69 : public ServiceWorkerCacheStorage::CacheLoader { |
63 public: | 70 public: |
64 explicit MemoryLoader(base::SequencedTaskRunner* cache_task_runner) | 71 explicit MemoryLoader(base::SequencedTaskRunner* cache_task_runner, |
65 : CacheLoader(cache_task_runner) {} | 72 net::URLRequestContext* request_context, |
| 73 ChromeBlobStorageContext* blob_context) |
| 74 : CacheLoader(cache_task_runner, request_context, blob_context) {} |
66 virtual void LoadCache(const std::string& cache_name, | 75 virtual void LoadCache(const std::string& cache_name, |
67 const CacheCallback& callback) OVERRIDE { | 76 const CacheCallback& callback) OVERRIDE { |
68 NOTREACHED(); | 77 NOTREACHED(); |
69 } | 78 } |
70 | 79 |
71 virtual void CreateCache(const std::string& cache_name, | 80 virtual void CreateCache(const std::string& cache_name, |
72 const CacheCallback& callback) OVERRIDE { | 81 const CacheCallback& callback) OVERRIDE { |
73 ServiceWorkerCache* cache = | 82 ServiceWorkerCache* cache = ServiceWorkerCache::CreateMemoryCache( |
74 ServiceWorkerCache::CreateMemoryCache(cache_name); | 83 cache_name, request_context_, blob_context_); |
75 callback.Run(cache); | 84 callback.Run(cache); |
76 } | 85 } |
77 | 86 |
78 virtual void CleanUpDeletedCache(const std::string& cache_name, | 87 virtual void CleanUpDeletedCache(const std::string& cache_name, |
79 const BoolCallback& callback) OVERRIDE { | 88 const BoolCallback& callback) OVERRIDE { |
80 callback.Run(true); | 89 callback.Run(true); |
81 } | 90 } |
82 | 91 |
83 virtual void WriteIndex(CacheMap* caches, | 92 virtual void WriteIndex(CacheMap* caches, |
84 const BoolCallback& callback) OVERRIDE { | 93 const BoolCallback& callback) OVERRIDE { |
85 callback.Run(false); | 94 callback.Run(false); |
86 } | 95 } |
87 | 96 |
88 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 97 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
89 const StringsCallback& callback) OVERRIDE { | 98 const StringsCallback& callback) OVERRIDE { |
90 callback.Run(cache_names.Pass()); | 99 callback.Run(cache_names.Pass()); |
91 } | 100 } |
92 }; | 101 }; |
93 | 102 |
94 class ServiceWorkerCacheStorage::SimpleCacheLoader | 103 class ServiceWorkerCacheStorage::SimpleCacheLoader |
95 : public ServiceWorkerCacheStorage::CacheLoader { | 104 : public ServiceWorkerCacheStorage::CacheLoader { |
96 public: | 105 public: |
97 SimpleCacheLoader(const base::FilePath& origin_path, | 106 SimpleCacheLoader(const base::FilePath& origin_path, |
98 base::SequencedTaskRunner* cache_task_runner) | 107 base::SequencedTaskRunner* cache_task_runner, |
99 : CacheLoader(cache_task_runner), origin_path_(origin_path) {} | 108 net::URLRequestContext* request_context, |
| 109 ChromeBlobStorageContext* blob_context) |
| 110 : CacheLoader(cache_task_runner, request_context, blob_context), |
| 111 origin_path_(origin_path) {} |
100 | 112 |
101 virtual void LoadCache(const std::string& cache_name, | 113 virtual void LoadCache(const std::string& cache_name, |
102 const CacheCallback& callback) OVERRIDE { | 114 const CacheCallback& callback) OVERRIDE { |
103 DCHECK(thread_checker_.CalledOnValidThread()); | 115 DCHECK(thread_checker_.CalledOnValidThread()); |
104 | 116 |
105 // 1. Create the cache's directory if necessary. (LoadCreateDirectory) | 117 // 1. Create the cache's directory if necessary. (LoadCreateDirectory) |
106 // 2. Create the cache object. (LoadDidCreateDirectory) | 118 // 2. Create the cache object. (LoadDidCreateDirectory) |
107 | 119 |
108 cache_task_runner_->PostTask( | 120 cache_task_runner_->PostTask( |
109 FROM_HERE, | 121 FROM_HERE, |
(...skipping 23 matching lines...) Expand all Loading... |
133 void LoadDidCreateDirectory(const std::string& cache_name, | 145 void LoadDidCreateDirectory(const std::string& cache_name, |
134 const CacheCallback& callback, | 146 const CacheCallback& callback, |
135 bool dir_rv) { | 147 bool dir_rv) { |
136 DCHECK(thread_checker_.CalledOnValidThread()); | 148 DCHECK(thread_checker_.CalledOnValidThread()); |
137 | 149 |
138 if (!dir_rv) { | 150 if (!dir_rv) { |
139 callback.Run(NULL); | 151 callback.Run(NULL); |
140 return; | 152 return; |
141 } | 153 } |
142 ServiceWorkerCache* cache = ServiceWorkerCache::CreatePersistentCache( | 154 ServiceWorkerCache* cache = ServiceWorkerCache::CreatePersistentCache( |
143 CreatePersistentCachePath(origin_path_, cache_name), cache_name); | 155 CreatePersistentCachePath(origin_path_, cache_name), |
| 156 cache_name, |
| 157 request_context_, |
| 158 blob_context_); |
144 callback.Run(cache); | 159 callback.Run(cache); |
145 } | 160 } |
146 | 161 |
147 virtual void CreateCache(const std::string& cache_name, | 162 virtual void CreateCache(const std::string& cache_name, |
148 const CacheCallback& callback) OVERRIDE { | 163 const CacheCallback& callback) OVERRIDE { |
149 DCHECK(thread_checker_.CalledOnValidThread()); | 164 DCHECK(thread_checker_.CalledOnValidThread()); |
150 | 165 |
151 // 1. Delete the cache's directory if it exists. (CreateDeleteFiles) | 166 // 1. Delete the cache's directory if it exists. (CreateDeleteFiles) |
152 // 2. Load the cache. (LoadCache) | 167 // 2. Load the cache. (LoadCache) |
153 | 168 |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 return origin_path.AppendASCII(HexedHash(cache_name)); | 339 return origin_path.AppendASCII(HexedHash(cache_name)); |
325 } | 340 } |
326 | 341 |
327 base::ThreadChecker thread_checker_; | 342 base::ThreadChecker thread_checker_; |
328 const base::FilePath origin_path_; | 343 const base::FilePath origin_path_; |
329 }; | 344 }; |
330 | 345 |
331 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( | 346 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( |
332 const base::FilePath& path, | 347 const base::FilePath& path, |
333 bool memory_only, | 348 bool memory_only, |
334 base::SequencedTaskRunner* cache_task_runner) | 349 base::SequencedTaskRunner* cache_task_runner, |
| 350 net::URLRequestContext* request_context, |
| 351 ChromeBlobStorageContext* blob_context) |
335 : initialized_(false), | 352 : initialized_(false), |
336 origin_path_(path), | 353 origin_path_(path), |
337 cache_task_runner_(cache_task_runner) { | 354 cache_task_runner_(cache_task_runner) { |
338 if (memory_only) | 355 if (memory_only) |
339 cache_loader_.reset(new MemoryLoader(cache_task_runner_)); | 356 cache_loader_.reset( |
| 357 new MemoryLoader(cache_task_runner_, request_context, blob_context)); |
340 else | 358 else |
341 cache_loader_.reset( | 359 cache_loader_.reset(new SimpleCacheLoader( |
342 new SimpleCacheLoader(origin_path_, cache_task_runner_)); | 360 origin_path_, cache_task_runner_, request_context, blob_context)); |
343 } | 361 } |
344 | 362 |
345 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { | 363 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { |
346 } | 364 } |
347 | 365 |
348 void ServiceWorkerCacheStorage::CreateCache( | 366 void ServiceWorkerCacheStorage::CreateCache( |
349 const std::string& cache_name, | 367 const std::string& cache_name, |
350 const CacheAndErrorCallback& callback) { | 368 const CacheAndErrorCallback& callback) { |
351 if (!initialized_) { | 369 if (!initialized_) { |
352 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache, | 370 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache, |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 NameMap::const_iterator it = name_map_.find(cache_name); | 665 NameMap::const_iterator it = name_map_.find(cache_name); |
648 if (it == name_map_.end()) | 666 if (it == name_map_.end()) |
649 return NULL; | 667 return NULL; |
650 | 668 |
651 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); | 669 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); |
652 DCHECK(cache); | 670 DCHECK(cache); |
653 return cache; | 671 return cache; |
654 } | 672 } |
655 | 673 |
656 } // namespace content | 674 } // namespace content |
OLD | NEW |