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

Side by Side Diff: content/browser/cache_storage/cache_storage.cc

Issue 1039763002: Cache Storage: Move files to content/*/cache_storage, rename classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: GN fix Created 5 years, 9 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_cache_storage.h" 5 #include "content/browser/cache_storage/cache_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/barrier_closure.h" 9 #include "base/barrier_closure.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/memory_mapped_file.h" 11 #include "base/files/memory_mapped_file.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
14 #include "base/sha1.h" 14 #include "base/sha1.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "content/browser/service_worker/service_worker_cache.h" 18 #include "content/browser/cache_storage/cache_storage.pb.h"
19 #include "content/browser/service_worker/service_worker_cache.pb.h" 19 #include "content/browser/cache_storage/cache_storage_cache.h"
20 #include "content/browser/service_worker/service_worker_cache_scheduler.h" 20 #include "content/browser/cache_storage/cache_storage_scheduler.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "net/base/directory_lister.h" 22 #include "net/base/directory_lister.h"
23 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
24 #include "storage/browser/blob/blob_storage_context.h" 24 #include "storage/browser/blob/blob_storage_context.h"
25 #include "storage/browser/quota/quota_manager_proxy.h" 25 #include "storage/browser/quota/quota_manager_proxy.h"
26 26
27 namespace content { 27 namespace content {
28 28
29 namespace { 29 namespace {
30 30
31 void CloseAllCachesDidCloseCache(const scoped_refptr<ServiceWorkerCache>& cache, 31 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache,
32 const base::Closure& barrier_closure) { 32 const base::Closure& barrier_closure) {
33 barrier_closure.Run(); 33 barrier_closure.Run();
34 } 34 }
35 35
36 } // namespace 36 } // namespace
37 37
38 const char ServiceWorkerCacheStorage::kIndexFileName[] = "index.txt"; 38 const char CacheStorage::kIndexFileName[] = "index.txt";
39 39
40 // Handles the loading and clean up of ServiceWorkerCache objects. The 40 // Handles the loading and clean up of CacheStorageCache objects. The
41 // callback of every public method is guaranteed to be called. 41 // callback of every public method is guaranteed to be called.
42 class ServiceWorkerCacheStorage::CacheLoader { 42 class CacheStorage::CacheLoader {
43 public: 43 public:
44 typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&)> 44 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&)>
45 CacheCallback; 45 CacheCallback;
46 typedef base::Callback<void(bool)> BoolCallback; 46 typedef base::Callback<void(bool)> BoolCallback;
47 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> 47 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)>
48 StringVectorCallback; 48 StringVectorCallback;
49 49
50 CacheLoader( 50 CacheLoader(
51 base::SequencedTaskRunner* cache_task_runner, 51 base::SequencedTaskRunner* cache_task_runner,
52 net::URLRequestContext* request_context, 52 net::URLRequestContext* request_context,
53 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 53 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
54 base::WeakPtr<storage::BlobStorageContext> blob_context, 54 base::WeakPtr<storage::BlobStorageContext> blob_context,
55 const GURL& origin) 55 const GURL& origin)
56 : cache_task_runner_(cache_task_runner), 56 : cache_task_runner_(cache_task_runner),
57 request_context_(request_context), 57 request_context_(request_context),
58 quota_manager_proxy_(quota_manager_proxy), 58 quota_manager_proxy_(quota_manager_proxy),
59 blob_context_(blob_context), 59 blob_context_(blob_context),
60 origin_(origin) { 60 origin_(origin) {
61 DCHECK(!origin_.is_empty()); 61 DCHECK(!origin_.is_empty());
62 } 62 }
63 63
64 virtual ~CacheLoader() {} 64 virtual ~CacheLoader() {}
65 65
66 // Creates a ServiceWorkerCache with the given name. It does not attempt to 66 // Creates a CacheStorageCache with the given name. It does not attempt to
67 // load the backend, that happens lazily when the cache is used. 67 // load the backend, that happens lazily when the cache is used.
68 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( 68 virtual scoped_refptr<CacheStorageCache> CreateCache(
69 const std::string& cache_name) = 0; 69 const std::string& cache_name) = 0;
70 70
71 // Deletes any pre-existing cache of the same name and then loads it. 71 // Deletes any pre-existing cache of the same name and then loads it.
72 virtual void CreateCache(const std::string& cache_name, 72 virtual void CreateCache(const std::string& cache_name,
73 const CacheCallback& callback) = 0; 73 const CacheCallback& callback) = 0;
74 74
75 // After the backend has been deleted, do any extra house keeping such as 75 // After the backend has been deleted, do any extra house keeping such as
76 // removing the cache's directory. 76 // removing the cache's directory.
77 virtual void CleanUpDeletedCache(const std::string& key, 77 virtual void CleanUpDeletedCache(const std::string& key,
78 const BoolCallback& callback) = 0; 78 const BoolCallback& callback) = 0;
79 79
80 // Writes the cache names (and sizes) to disk if applicable. 80 // Writes the cache names (and sizes) to disk if applicable.
81 virtual void WriteIndex(const StringVector& cache_names, 81 virtual void WriteIndex(const StringVector& cache_names,
82 const BoolCallback& callback) = 0; 82 const BoolCallback& callback) = 0;
83 83
84 // Loads the cache names from disk if applicable. 84 // Loads the cache names from disk if applicable.
85 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, 85 virtual void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names,
86 const StringVectorCallback& callback) = 0; 86 const StringVectorCallback& callback) = 0;
87 87
88 protected: 88 protected:
89 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; 89 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
90 net::URLRequestContext* request_context_; 90 net::URLRequestContext* request_context_;
91 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; 91 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
92 base::WeakPtr<storage::BlobStorageContext> blob_context_; 92 base::WeakPtr<storage::BlobStorageContext> blob_context_;
93 GURL origin_; 93 GURL origin_;
94 }; 94 };
95 95
96 // Creates memory-only ServiceWorkerCaches. Because these caches have no 96 // Creates memory-only ServiceWorkerCaches. Because these caches have no
97 // persistent storage it is not safe to free them from memory if they might be 97 // persistent storage it is not safe to free them from memory if they might be
98 // used again. Therefore this class holds a reference to each cache until the 98 // used again. Therefore this class holds a reference to each cache until the
99 // cache is deleted. 99 // cache is deleted.
100 class ServiceWorkerCacheStorage::MemoryLoader 100 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader {
101 : public ServiceWorkerCacheStorage::CacheLoader {
102 public: 101 public:
103 MemoryLoader( 102 MemoryLoader(
104 base::SequencedTaskRunner* cache_task_runner, 103 base::SequencedTaskRunner* cache_task_runner,
105 net::URLRequestContext* request_context, 104 net::URLRequestContext* request_context,
106 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 105 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
107 base::WeakPtr<storage::BlobStorageContext> blob_context, 106 base::WeakPtr<storage::BlobStorageContext> blob_context,
108 const GURL& origin) 107 const GURL& origin)
109 : CacheLoader(cache_task_runner, 108 : CacheLoader(cache_task_runner,
110 request_context, 109 request_context,
111 quota_manager_proxy, 110 quota_manager_proxy,
112 blob_context, 111 blob_context,
113 origin) {} 112 origin) {}
114 113
115 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( 114 scoped_refptr<CacheStorageCache> CreateCache(
116 const std::string& cache_name) override { 115 const std::string& cache_name) override {
117 return ServiceWorkerCache::CreateMemoryCache( 116 return CacheStorageCache::CreateMemoryCache(
118 origin_, request_context_, quota_manager_proxy_, blob_context_); 117 origin_, request_context_, quota_manager_proxy_, blob_context_);
119 } 118 }
120 119
121 void CreateCache(const std::string& cache_name, 120 void CreateCache(const std::string& cache_name,
122 const CacheCallback& callback) override { 121 const CacheCallback& callback) override {
123 scoped_refptr<ServiceWorkerCache> cache = 122 scoped_refptr<CacheStorageCache> cache = CreateCache(cache_name);
124 CreateServiceWorkerCache(cache_name);
125 cache_refs_.insert(std::make_pair(cache_name, cache)); 123 cache_refs_.insert(std::make_pair(cache_name, cache));
126 callback.Run(cache); 124 callback.Run(cache);
127 } 125 }
128 126
129 void CleanUpDeletedCache(const std::string& cache_name, 127 void CleanUpDeletedCache(const std::string& cache_name,
130 const BoolCallback& callback) override { 128 const BoolCallback& callback) override {
131 CacheRefMap::iterator it = cache_refs_.find(cache_name); 129 CacheRefMap::iterator it = cache_refs_.find(cache_name);
132 DCHECK(it != cache_refs_.end()); 130 DCHECK(it != cache_refs_.end());
133 cache_refs_.erase(it); 131 cache_refs_.erase(it);
134 callback.Run(true); 132 callback.Run(true);
135 } 133 }
136 134
137 void WriteIndex(const StringVector& cache_names, 135 void WriteIndex(const StringVector& cache_names,
138 const BoolCallback& callback) override { 136 const BoolCallback& callback) override {
139 callback.Run(false); 137 callback.Run(false);
140 } 138 }
141 139
142 void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names, 140 void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names,
143 const StringVectorCallback& callback) override { 141 const StringVectorCallback& callback) override {
144 callback.Run(cache_names.Pass()); 142 callback.Run(cache_names.Pass());
145 } 143 }
146 144
147 private: 145 private:
148 typedef std::map<std::string, scoped_refptr<ServiceWorkerCache> > CacheRefMap; 146 typedef std::map<std::string, scoped_refptr<CacheStorageCache>> CacheRefMap;
149 ~MemoryLoader() override {} 147 ~MemoryLoader() override {}
150 148
151 // Keep a reference to each cache to ensure that it's not freed before the 149 // Keep a reference to each cache to ensure that it's not freed before the
152 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is 150 // client calls CacheStorage::Delete or the CacheStorage is
153 // freed. 151 // freed.
154 CacheRefMap cache_refs_; 152 CacheRefMap cache_refs_;
155 }; 153 };
156 154
157 class ServiceWorkerCacheStorage::SimpleCacheLoader 155 class CacheStorage::SimpleCacheLoader : public CacheStorage::CacheLoader {
158 : public ServiceWorkerCacheStorage::CacheLoader {
159 public: 156 public:
160 SimpleCacheLoader( 157 SimpleCacheLoader(
161 const base::FilePath& origin_path, 158 const base::FilePath& origin_path,
162 base::SequencedTaskRunner* cache_task_runner, 159 base::SequencedTaskRunner* cache_task_runner,
163 net::URLRequestContext* request_context, 160 net::URLRequestContext* request_context,
164 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 161 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
165 base::WeakPtr<storage::BlobStorageContext> blob_context, 162 base::WeakPtr<storage::BlobStorageContext> blob_context,
166 const GURL& origin) 163 const GURL& origin)
167 : CacheLoader(cache_task_runner, 164 : CacheLoader(cache_task_runner,
168 request_context, 165 request_context,
169 quota_manager_proxy, 166 quota_manager_proxy,
170 blob_context, 167 blob_context,
171 origin), 168 origin),
172 origin_path_(origin_path), 169 origin_path_(origin_path),
173 weak_ptr_factory_(this) {} 170 weak_ptr_factory_(this) {}
174 171
175 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( 172 scoped_refptr<CacheStorageCache> CreateCache(
176 const std::string& cache_name) override { 173 const std::string& cache_name) override {
177 DCHECK_CURRENTLY_ON(BrowserThread::IO); 174 DCHECK_CURRENTLY_ON(BrowserThread::IO);
178 175
179 return ServiceWorkerCache::CreatePersistentCache( 176 return CacheStorageCache::CreatePersistentCache(
180 origin_, 177 origin_, CreatePersistentCachePath(origin_path_, cache_name),
181 CreatePersistentCachePath(origin_path_, cache_name), 178 request_context_, quota_manager_proxy_, blob_context_);
182 request_context_,
183 quota_manager_proxy_,
184 blob_context_);
185 } 179 }
186 180
187 void CreateCache(const std::string& cache_name, 181 void CreateCache(const std::string& cache_name,
188 const CacheCallback& callback) override { 182 const CacheCallback& callback) override {
189 DCHECK_CURRENTLY_ON(BrowserThread::IO); 183 DCHECK_CURRENTLY_ON(BrowserThread::IO);
190 184
191 // 1. Delete the cache's directory if it exists. 185 // 1. Delete the cache's directory if it exists.
192 // (CreateCacheDeleteFilesInPool) 186 // (CreateCacheDeleteFilesInPool)
193 // 2. Load the cache. (LoadCreateDirectoryInPool) 187 // 2. Load the cache. (LoadCreateDirectoryInPool)
194 188
195 base::FilePath cache_path = 189 base::FilePath cache_path =
196 CreatePersistentCachePath(origin_path_, cache_name); 190 CreatePersistentCachePath(origin_path_, cache_name);
197 191
198 PostTaskAndReplyWithResult( 192 PostTaskAndReplyWithResult(
199 cache_task_runner_.get(), 193 cache_task_runner_.get(), FROM_HERE,
200 FROM_HERE,
201 base::Bind(&SimpleCacheLoader::CreateCachePrepDirInPool, cache_path), 194 base::Bind(&SimpleCacheLoader::CreateCachePrepDirInPool, cache_path),
202 base::Bind(&SimpleCacheLoader::CreateCachePreppedDir, 195 base::Bind(&SimpleCacheLoader::CreateCachePreppedDir, cache_name,
203 cache_name, 196 callback, weak_ptr_factory_.GetWeakPtr()));
204 callback,
205 weak_ptr_factory_.GetWeakPtr()));
206 } 197 }
207 198
208 static bool CreateCachePrepDirInPool(const base::FilePath& cache_path) { 199 static bool CreateCachePrepDirInPool(const base::FilePath& cache_path) {
209 if (base::PathExists(cache_path)) 200 if (base::PathExists(cache_path))
210 base::DeleteFile(cache_path, /* recursive */ true); 201 base::DeleteFile(cache_path, /* recursive */ true);
211 return base::CreateDirectory(cache_path); 202 return base::CreateDirectory(cache_path);
212 } 203 }
213 204
214 static void CreateCachePreppedDir(const std::string& cache_name, 205 static void CreateCachePreppedDir(const std::string& cache_name,
215 const CacheCallback& callback, 206 const CacheCallback& callback,
216 base::WeakPtr<SimpleCacheLoader> loader, 207 base::WeakPtr<SimpleCacheLoader> loader,
217 bool success) { 208 bool success) {
218 if (!success || !loader) { 209 if (!success || !loader) {
219 callback.Run(scoped_refptr<ServiceWorkerCache>()); 210 callback.Run(scoped_refptr<CacheStorageCache>());
220 return; 211 return;
221 } 212 }
222 213
223 callback.Run(loader->CreateServiceWorkerCache(cache_name)); 214 callback.Run(loader->CreateCache(cache_name));
224 } 215 }
225 216
226 void CleanUpDeletedCache(const std::string& cache_name, 217 void CleanUpDeletedCache(const std::string& cache_name,
227 const BoolCallback& callback) override { 218 const BoolCallback& callback) override {
228 DCHECK_CURRENTLY_ON(BrowserThread::IO); 219 DCHECK_CURRENTLY_ON(BrowserThread::IO);
229 220
230 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) 221 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool)
231 222
232 base::FilePath cache_path = 223 base::FilePath cache_path =
233 CreatePersistentCachePath(origin_path_, cache_name); 224 CreatePersistentCachePath(origin_path_, cache_name);
234 cache_task_runner_->PostTask( 225 cache_task_runner_->PostTask(
235 FROM_HERE, 226 FROM_HERE,
236 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, 227 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, cache_path,
237 cache_path, 228 callback, base::MessageLoopProxy::current()));
238 callback,
239 base::MessageLoopProxy::current()));
240 } 229 }
241 230
242 static void CleanUpDeleteCacheDirInPool( 231 static void CleanUpDeleteCacheDirInPool(
243 const base::FilePath& cache_path, 232 const base::FilePath& cache_path,
244 const BoolCallback& callback, 233 const BoolCallback& callback,
245 const scoped_refptr<base::MessageLoopProxy>& original_loop) { 234 const scoped_refptr<base::MessageLoopProxy>& original_loop) {
246 bool rv = base::DeleteFile(cache_path, true); 235 bool rv = base::DeleteFile(cache_path, true);
247 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); 236 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv));
248 } 237 }
249 238
250 void WriteIndex(const StringVector& cache_names, 239 void WriteIndex(const StringVector& cache_names,
251 const BoolCallback& callback) override { 240 const BoolCallback& callback) override {
252 DCHECK_CURRENTLY_ON(BrowserThread::IO); 241 DCHECK_CURRENTLY_ON(BrowserThread::IO);
253 242
254 // 1. Create the index file as a string. (WriteIndex) 243 // 1. Create the index file as a string. (WriteIndex)
255 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) 244 // 2. Write the file to disk. (WriteIndexWriteToFileInPool)
256 245
257 ServiceWorkerCacheStorageIndex index; 246 CacheStorageIndex index;
258 index.set_origin(origin_.spec()); 247 index.set_origin(origin_.spec());
259 248
260 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { 249 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) {
261 ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache(); 250 CacheStorageIndex::Cache* index_cache = index.add_cache();
262 index_cache->set_name(cache_names[i]); 251 index_cache->set_name(cache_names[i]);
263 } 252 }
264 253
265 std::string serialized; 254 std::string serialized;
266 bool success = index.SerializeToString(&serialized); 255 bool success = index.SerializeToString(&serialized);
267 DCHECK(success); 256 DCHECK(success);
268 257
269 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); 258 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp");
270 base::FilePath index_path = 259 base::FilePath index_path =
271 origin_path_.AppendASCII(ServiceWorkerCacheStorage::kIndexFileName); 260 origin_path_.AppendASCII(CacheStorage::kIndexFileName);
272 261
273 cache_task_runner_->PostTask( 262 cache_task_runner_->PostTask(
274 FROM_HERE, 263 FROM_HERE, base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool,
275 base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, 264 tmp_path, index_path, serialized, callback,
276 tmp_path, 265 base::MessageLoopProxy::current()));
277 index_path,
278 serialized,
279 callback,
280 base::MessageLoopProxy::current()));
281 } 266 }
282 267
283 static void WriteIndexWriteToFileInPool( 268 static void WriteIndexWriteToFileInPool(
284 const base::FilePath& tmp_path, 269 const base::FilePath& tmp_path,
285 const base::FilePath& index_path, 270 const base::FilePath& index_path,
286 const std::string& data, 271 const std::string& data,
287 const BoolCallback& callback, 272 const BoolCallback& callback,
288 const scoped_refptr<base::MessageLoopProxy>& original_loop) { 273 const scoped_refptr<base::MessageLoopProxy>& original_loop) {
289 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); 274 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size());
290 if (bytes_written != implicit_cast<int>(data.size())) { 275 if (bytes_written != implicit_cast<int>(data.size())) {
291 base::DeleteFile(tmp_path, /* recursive */ false); 276 base::DeleteFile(tmp_path, /* recursive */ false);
292 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); 277 original_loop->PostTask(FROM_HERE, base::Bind(callback, false));
293 } 278 }
294 279
295 // Atomically rename the temporary index file to become the real one. 280 // Atomically rename the temporary index file to become the real one.
296 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); 281 bool rv = base::ReplaceFile(tmp_path, index_path, NULL);
297 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); 282 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv));
298 } 283 }
299 284
300 void LoadIndex(scoped_ptr<std::vector<std::string>> names, 285 void LoadIndex(scoped_ptr<std::vector<std::string>> names,
301 const StringVectorCallback& callback) override { 286 const StringVectorCallback& callback) override {
302 DCHECK_CURRENTLY_ON(BrowserThread::IO); 287 DCHECK_CURRENTLY_ON(BrowserThread::IO);
303 288
304 // 1. Read the file from disk. (LoadIndexReadFileInPool) 289 // 1. Read the file from disk. (LoadIndexReadFileInPool)
305 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) 290 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile)
306 291
307 base::FilePath index_path = 292 base::FilePath index_path =
308 origin_path_.AppendASCII(ServiceWorkerCacheStorage::kIndexFileName); 293 origin_path_.AppendASCII(CacheStorage::kIndexFileName);
309 294
310 cache_task_runner_->PostTask( 295 cache_task_runner_->PostTask(
311 FROM_HERE, 296 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool,
312 base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, 297 index_path, base::Passed(names.Pass()), callback,
313 index_path, 298 base::MessageLoopProxy::current()));
314 base::Passed(names.Pass()),
315 callback,
316 base::MessageLoopProxy::current()));
317 } 299 }
318 300
319 static void LoadIndexReadFileInPool( 301 static void LoadIndexReadFileInPool(
320 const base::FilePath& index_path, 302 const base::FilePath& index_path,
321 scoped_ptr<std::vector<std::string> > names, 303 scoped_ptr<std::vector<std::string>> names,
322 const StringVectorCallback& callback, 304 const StringVectorCallback& callback,
323 const scoped_refptr<base::MessageLoopProxy>& original_loop) { 305 const scoped_refptr<base::MessageLoopProxy>& original_loop) {
324 std::string body; 306 std::string body;
325 base::ReadFileToString(index_path, &body); 307 base::ReadFileToString(index_path, &body);
326 308
327 original_loop->PostTask(FROM_HERE, 309 original_loop->PostTask(
328 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, 310 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile,
329 base::Passed(names.Pass()), 311 base::Passed(names.Pass()), callback, body));
330 callback,
331 body));
332 } 312 }
333 313
334 static void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string> > names, 314 static void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names,
335 const StringVectorCallback& callback, 315 const StringVectorCallback& callback,
336 const std::string& serialized) { 316 const std::string& serialized) {
337 DCHECK_CURRENTLY_ON(BrowserThread::IO); 317 DCHECK_CURRENTLY_ON(BrowserThread::IO);
338 318
339 ServiceWorkerCacheStorageIndex index; 319 CacheStorageIndex index;
340 if (index.ParseFromString(serialized)) { 320 if (index.ParseFromString(serialized)) {
341 for (int i = 0, max = index.cache_size(); i < max; ++i) { 321 for (int i = 0, max = index.cache_size(); i < max; ++i) {
342 const ServiceWorkerCacheStorageIndex::Cache& cache = index.cache(i); 322 const CacheStorageIndex::Cache& cache = index.cache(i);
343 names->push_back(cache.name()); 323 names->push_back(cache.name());
344 } 324 }
345 } 325 }
346 326
347 // TODO(jkarlin): Delete caches that are in the directory and not returned 327 // TODO(jkarlin): Delete caches that are in the directory and not returned
348 // in LoadIndex. 328 // in LoadIndex.
349 callback.Run(names.Pass()); 329 callback.Run(names.Pass());
350 } 330 }
351 331
352 private: 332 private:
(...skipping 10 matching lines...) Expand all
363 const base::FilePath& origin_path, 343 const base::FilePath& origin_path,
364 const std::string& cache_name) { 344 const std::string& cache_name) {
365 return origin_path.AppendASCII(HexedHash(cache_name)); 345 return origin_path.AppendASCII(HexedHash(cache_name));
366 } 346 }
367 347
368 const base::FilePath origin_path_; 348 const base::FilePath origin_path_;
369 349
370 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; 350 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_;
371 }; 351 };
372 352
373 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( 353 CacheStorage::CacheStorage(
374 const base::FilePath& path, 354 const base::FilePath& path,
375 bool memory_only, 355 bool memory_only,
376 base::SequencedTaskRunner* cache_task_runner, 356 base::SequencedTaskRunner* cache_task_runner,
377 net::URLRequestContext* request_context, 357 net::URLRequestContext* request_context,
378 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 358 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
379 base::WeakPtr<storage::BlobStorageContext> blob_context, 359 base::WeakPtr<storage::BlobStorageContext> blob_context,
380 const GURL& origin) 360 const GURL& origin)
381 : initialized_(false), 361 : initialized_(false),
382 initializing_(false), 362 initializing_(false),
383 scheduler_(new ServiceWorkerCacheScheduler()), 363 scheduler_(new CacheStorageScheduler()),
384 origin_path_(path), 364 origin_path_(path),
385 cache_task_runner_(cache_task_runner), 365 cache_task_runner_(cache_task_runner),
386 memory_only_(memory_only), 366 memory_only_(memory_only),
387 weak_factory_(this) { 367 weak_factory_(this) {
388 if (memory_only) 368 if (memory_only)
389 cache_loader_.reset(new MemoryLoader(cache_task_runner_.get(), 369 cache_loader_.reset(new MemoryLoader(cache_task_runner_.get(),
390 request_context, 370 request_context, quota_manager_proxy,
391 quota_manager_proxy, 371 blob_context, origin));
392 blob_context,
393 origin));
394 else 372 else
395 cache_loader_.reset(new SimpleCacheLoader(origin_path_, 373 cache_loader_.reset(new SimpleCacheLoader(
396 cache_task_runner_.get(), 374 origin_path_, cache_task_runner_.get(), request_context,
397 request_context, 375 quota_manager_proxy, blob_context, origin));
398 quota_manager_proxy,
399 blob_context,
400 origin));
401 } 376 }
402 377
403 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { 378 CacheStorage::~CacheStorage() {
404 } 379 }
405 380
406 void ServiceWorkerCacheStorage::OpenCache( 381 void CacheStorage::OpenCache(const std::string& cache_name,
407 const std::string& cache_name, 382 const CacheAndErrorCallback& callback) {
408 const CacheAndErrorCallback& callback) {
409 DCHECK_CURRENTLY_ON(BrowserThread::IO); 383 DCHECK_CURRENTLY_ON(BrowserThread::IO);
410 384
411 if (!initialized_) 385 if (!initialized_)
412 LazyInit(); 386 LazyInit();
413 387
414 CacheAndErrorCallback pending_callback = 388 CacheAndErrorCallback pending_callback =
415 base::Bind(&ServiceWorkerCacheStorage::PendingCacheAndErrorCallback, 389 base::Bind(&CacheStorage::PendingCacheAndErrorCallback,
416 weak_factory_.GetWeakPtr(), callback); 390 weak_factory_.GetWeakPtr(), callback);
417 scheduler_->ScheduleOperation( 391 scheduler_->ScheduleOperation(base::Bind(&CacheStorage::OpenCacheImpl,
418 base::Bind(&ServiceWorkerCacheStorage::OpenCacheImpl, 392 weak_factory_.GetWeakPtr(),
419 weak_factory_.GetWeakPtr(), cache_name, pending_callback)); 393 cache_name, pending_callback));
420 } 394 }
421 395
422 void ServiceWorkerCacheStorage::HasCache(const std::string& cache_name, 396 void CacheStorage::HasCache(const std::string& cache_name,
423 const BoolAndErrorCallback& callback) { 397 const BoolAndErrorCallback& callback) {
424 DCHECK_CURRENTLY_ON(BrowserThread::IO); 398 DCHECK_CURRENTLY_ON(BrowserThread::IO);
425 399
426 if (!initialized_) 400 if (!initialized_)
427 LazyInit(); 401 LazyInit();
428 402
429 BoolAndErrorCallback pending_callback = 403 BoolAndErrorCallback pending_callback =
430 base::Bind(&ServiceWorkerCacheStorage::PendingBoolAndErrorCallback, 404 base::Bind(&CacheStorage::PendingBoolAndErrorCallback,
431 weak_factory_.GetWeakPtr(), callback); 405 weak_factory_.GetWeakPtr(), callback);
432 scheduler_->ScheduleOperation( 406 scheduler_->ScheduleOperation(base::Bind(&CacheStorage::HasCacheImpl,
433 base::Bind(&ServiceWorkerCacheStorage::HasCacheImpl, 407 weak_factory_.GetWeakPtr(),
434 weak_factory_.GetWeakPtr(), cache_name, pending_callback)); 408 cache_name, pending_callback));
435 } 409 }
436 410
437 void ServiceWorkerCacheStorage::DeleteCache( 411 void CacheStorage::DeleteCache(const std::string& cache_name,
438 const std::string& cache_name, 412 const BoolAndErrorCallback& callback) {
439 const BoolAndErrorCallback& callback) {
440 DCHECK_CURRENTLY_ON(BrowserThread::IO); 413 DCHECK_CURRENTLY_ON(BrowserThread::IO);
441 414
442 if (!initialized_) 415 if (!initialized_)
443 LazyInit(); 416 LazyInit();
444 417
445 BoolAndErrorCallback pending_callback = 418 BoolAndErrorCallback pending_callback =
446 base::Bind(&ServiceWorkerCacheStorage::PendingBoolAndErrorCallback, 419 base::Bind(&CacheStorage::PendingBoolAndErrorCallback,
447 weak_factory_.GetWeakPtr(), callback); 420 weak_factory_.GetWeakPtr(), callback);
448 scheduler_->ScheduleOperation( 421 scheduler_->ScheduleOperation(base::Bind(&CacheStorage::DeleteCacheImpl,
449 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheImpl, 422 weak_factory_.GetWeakPtr(),
450 weak_factory_.GetWeakPtr(), cache_name, pending_callback)); 423 cache_name, pending_callback));
451 } 424 }
452 425
453 void ServiceWorkerCacheStorage::EnumerateCaches( 426 void CacheStorage::EnumerateCaches(const StringsAndErrorCallback& callback) {
454 const StringsAndErrorCallback& callback) {
455 DCHECK_CURRENTLY_ON(BrowserThread::IO); 427 DCHECK_CURRENTLY_ON(BrowserThread::IO);
456 428
457 if (!initialized_) 429 if (!initialized_)
458 LazyInit(); 430 LazyInit();
459 431
460 StringsAndErrorCallback pending_callback = 432 StringsAndErrorCallback pending_callback =
461 base::Bind(&ServiceWorkerCacheStorage::PendingStringsAndErrorCallback, 433 base::Bind(&CacheStorage::PendingStringsAndErrorCallback,
462 weak_factory_.GetWeakPtr(), callback); 434 weak_factory_.GetWeakPtr(), callback);
463 scheduler_->ScheduleOperation( 435 scheduler_->ScheduleOperation(base::Bind(&CacheStorage::EnumerateCachesImpl,
464 base::Bind(&ServiceWorkerCacheStorage::EnumerateCachesImpl, 436 weak_factory_.GetWeakPtr(),
465 weak_factory_.GetWeakPtr(), pending_callback)); 437 pending_callback));
466 } 438 }
467 439
468 void ServiceWorkerCacheStorage::MatchCache( 440 void CacheStorage::MatchCache(
469 const std::string& cache_name, 441 const std::string& cache_name,
470 scoped_ptr<ServiceWorkerFetchRequest> request, 442 scoped_ptr<ServiceWorkerFetchRequest> request,
471 const ServiceWorkerCache::ResponseCallback& callback) { 443 const CacheStorageCache::ResponseCallback& callback) {
472 DCHECK_CURRENTLY_ON(BrowserThread::IO); 444 DCHECK_CURRENTLY_ON(BrowserThread::IO);
473 445
474 if (!initialized_) 446 if (!initialized_)
475 LazyInit(); 447 LazyInit();
476 448
477 ServiceWorkerCache::ResponseCallback pending_callback = 449 CacheStorageCache::ResponseCallback pending_callback =
478 base::Bind(&ServiceWorkerCacheStorage::PendingResponseCallback, 450 base::Bind(&CacheStorage::PendingResponseCallback,
479 weak_factory_.GetWeakPtr(), callback); 451 weak_factory_.GetWeakPtr(), callback);
480 scheduler_->ScheduleOperation(base::Bind( 452 scheduler_->ScheduleOperation(
481 &ServiceWorkerCacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(), 453 base::Bind(&CacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(),
482 cache_name, base::Passed(request.Pass()), pending_callback)); 454 cache_name, base::Passed(request.Pass()), pending_callback));
483 } 455 }
484 456
485 void ServiceWorkerCacheStorage::MatchAllCaches( 457 void CacheStorage::MatchAllCaches(
486 scoped_ptr<ServiceWorkerFetchRequest> request, 458 scoped_ptr<ServiceWorkerFetchRequest> request,
487 const ServiceWorkerCache::ResponseCallback& callback) { 459 const CacheStorageCache::ResponseCallback& callback) {
488 DCHECK_CURRENTLY_ON(BrowserThread::IO); 460 DCHECK_CURRENTLY_ON(BrowserThread::IO);
489 461
490 if (!initialized_) 462 if (!initialized_)
491 LazyInit(); 463 LazyInit();
492 464
493 ServiceWorkerCache::ResponseCallback pending_callback = 465 CacheStorageCache::ResponseCallback pending_callback =
494 base::Bind(&ServiceWorkerCacheStorage::PendingResponseCallback, 466 base::Bind(&CacheStorage::PendingResponseCallback,
495 weak_factory_.GetWeakPtr(), callback); 467 weak_factory_.GetWeakPtr(), callback);
496 scheduler_->ScheduleOperation( 468 scheduler_->ScheduleOperation(
497 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesImpl, 469 base::Bind(&CacheStorage::MatchAllCachesImpl, weak_factory_.GetWeakPtr(),
498 weak_factory_.GetWeakPtr(), base::Passed(request.Pass()), 470 base::Passed(request.Pass()), pending_callback));
499 pending_callback));
500 } 471 }
501 472
502 void ServiceWorkerCacheStorage::CloseAllCaches(const base::Closure& callback) { 473 void CacheStorage::CloseAllCaches(const base::Closure& callback) {
503 DCHECK_CURRENTLY_ON(BrowserThread::IO); 474 DCHECK_CURRENTLY_ON(BrowserThread::IO);
504 475
505 if (!initialized_) { 476 if (!initialized_) {
506 callback.Run(); 477 callback.Run();
507 return; 478 return;
508 } 479 }
509 480
510 base::Closure pending_callback = 481 base::Closure pending_callback = base::Bind(
511 base::Bind(&ServiceWorkerCacheStorage::PendingClosure, 482 &CacheStorage::PendingClosure, weak_factory_.GetWeakPtr(), callback);
512 weak_factory_.GetWeakPtr(), callback); 483 scheduler_->ScheduleOperation(base::Bind(&CacheStorage::CloseAllCachesImpl,
513 scheduler_->ScheduleOperation( 484 weak_factory_.GetWeakPtr(),
514 base::Bind(&ServiceWorkerCacheStorage::CloseAllCachesImpl, 485 pending_callback));
515 weak_factory_.GetWeakPtr(), pending_callback));
516 } 486 }
517 487
518 int64 ServiceWorkerCacheStorage::MemoryBackedSize() const { 488 int64 CacheStorage::MemoryBackedSize() const {
519 DCHECK_CURRENTLY_ON(BrowserThread::IO); 489 DCHECK_CURRENTLY_ON(BrowserThread::IO);
520 490
521 if (!initialized_ || !memory_only_) 491 if (!initialized_ || !memory_only_)
522 return 0; 492 return 0;
523 493
524 int64 sum = 0; 494 int64 sum = 0;
525 for (auto& key_value : cache_map_) { 495 for (auto& key_value : cache_map_) {
526 if (key_value.second) 496 if (key_value.second)
527 sum += key_value.second->MemoryBackedSize(); 497 sum += key_value.second->MemoryBackedSize();
528 } 498 }
529 return sum; 499 return sum;
530 } 500 }
531 501
532 void ServiceWorkerCacheStorage::StartAsyncOperationForTesting() { 502 void CacheStorage::StartAsyncOperationForTesting() {
533 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing)); 503 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing));
534 } 504 }
535 505
536 void ServiceWorkerCacheStorage::CompleteAsyncOperationForTesting() { 506 void CacheStorage::CompleteAsyncOperationForTesting() {
537 scheduler_->CompleteOperationAndRunNext(); 507 scheduler_->CompleteOperationAndRunNext();
538 } 508 }
539 509
540 // Init is run lazily so that it is called on the proper MessageLoop. 510 // Init is run lazily so that it is called on the proper MessageLoop.
541 void ServiceWorkerCacheStorage::LazyInit() { 511 void CacheStorage::LazyInit() {
542 DCHECK_CURRENTLY_ON(BrowserThread::IO); 512 DCHECK_CURRENTLY_ON(BrowserThread::IO);
543 DCHECK(!initialized_); 513 DCHECK(!initialized_);
544 514
545 if (initializing_) 515 if (initializing_)
546 return; 516 return;
547 517
548 DCHECK(!scheduler_->ScheduledOperations()); 518 DCHECK(!scheduler_->ScheduledOperations());
549 519
550 initializing_ = true; 520 initializing_ = true;
551 scheduler_->ScheduleOperation(base::Bind( 521 scheduler_->ScheduleOperation(
552 &ServiceWorkerCacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); 522 base::Bind(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr()));
553 } 523 }
554 524
555 void ServiceWorkerCacheStorage::LazyInitImpl() { 525 void CacheStorage::LazyInitImpl() {
556 DCHECK_CURRENTLY_ON(BrowserThread::IO); 526 DCHECK_CURRENTLY_ON(BrowserThread::IO);
557 DCHECK(!initialized_); 527 DCHECK(!initialized_);
558 DCHECK(initializing_); 528 DCHECK(initializing_);
559 529
560 // 1. Get the list of cache names (async call) 530 // 1. Get the list of cache names (async call)
561 // 2. For each cache name, load the cache (async call) 531 // 2. For each cache name, load the cache (async call)
562 // 3. Once each load is complete, update the map variables. 532 // 3. Once each load is complete, update the map variables.
563 // 4. Call the list of waiting callbacks. 533 // 4. Call the list of waiting callbacks.
564 534
565 scoped_ptr<std::vector<std::string> > indexed_cache_names( 535 scoped_ptr<std::vector<std::string>> indexed_cache_names(
566 new std::vector<std::string>()); 536 new std::vector<std::string>());
567 537
568 cache_loader_->LoadIndex( 538 cache_loader_->LoadIndex(indexed_cache_names.Pass(),
569 indexed_cache_names.Pass(), 539 base::Bind(&CacheStorage::LazyInitDidLoadIndex,
570 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, 540 weak_factory_.GetWeakPtr()));
571 weak_factory_.GetWeakPtr()));
572 } 541 }
573 542
574 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( 543 void CacheStorage::LazyInitDidLoadIndex(
575 scoped_ptr<std::vector<std::string> > indexed_cache_names) { 544 scoped_ptr<std::vector<std::string>> indexed_cache_names) {
576 DCHECK_CURRENTLY_ON(BrowserThread::IO); 545 DCHECK_CURRENTLY_ON(BrowserThread::IO);
577 546
578 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) { 547 for (size_t i = 0u, max = indexed_cache_names->size(); i < max; ++i) {
579 cache_map_.insert(std::make_pair(indexed_cache_names->at(i), 548 cache_map_.insert(std::make_pair(indexed_cache_names->at(i),
580 base::WeakPtr<ServiceWorkerCache>())); 549 base::WeakPtr<CacheStorageCache>()));
581 ordered_cache_names_.push_back(indexed_cache_names->at(i)); 550 ordered_cache_names_.push_back(indexed_cache_names->at(i));
582 } 551 }
583 552
584 initializing_ = false; 553 initializing_ = false;
585 initialized_ = true; 554 initialized_ = true;
586 555
587 scheduler_->CompleteOperationAndRunNext(); 556 scheduler_->CompleteOperationAndRunNext();
588 } 557 }
589 558
590 void ServiceWorkerCacheStorage::OpenCacheImpl( 559 void CacheStorage::OpenCacheImpl(const std::string& cache_name,
591 const std::string& cache_name, 560 const CacheAndErrorCallback& callback) {
592 const CacheAndErrorCallback& callback) { 561 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name);
593 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name);
594 if (cache.get()) { 562 if (cache.get()) {
595 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); 563 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR);
596 return; 564 return;
597 } 565 }
598 566
599 cache_loader_->CreateCache( 567 cache_loader_->CreateCache(
600 cache_name, 568 cache_name, base::Bind(&CacheStorage::CreateCacheDidCreateCache,
601 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidCreateCache, 569 weak_factory_.GetWeakPtr(), cache_name, callback));
602 weak_factory_.GetWeakPtr(), cache_name, callback));
603 } 570 }
604 571
605 void ServiceWorkerCacheStorage::CreateCacheDidCreateCache( 572 void CacheStorage::CreateCacheDidCreateCache(
606 const std::string& cache_name, 573 const std::string& cache_name,
607 const CacheAndErrorCallback& callback, 574 const CacheAndErrorCallback& callback,
608 const scoped_refptr<ServiceWorkerCache>& cache) { 575 const scoped_refptr<CacheStorageCache>& cache) {
609 DCHECK_CURRENTLY_ON(BrowserThread::IO); 576 DCHECK_CURRENTLY_ON(BrowserThread::IO);
610 577
611 if (!cache.get()) { 578 if (!cache.get()) {
612 callback.Run(scoped_refptr<ServiceWorkerCache>(), 579 callback.Run(scoped_refptr<CacheStorageCache>(),
613 CACHE_STORAGE_ERROR_CLOSING); 580 CACHE_STORAGE_ERROR_CLOSING);
614 return; 581 return;
615 } 582 }
616 583
617 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult", 584 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult",
618 cache != nullptr); 585 cache != nullptr);
619 586
620 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr())); 587 cache_map_.insert(std::make_pair(cache_name, cache->AsWeakPtr()));
621 ordered_cache_names_.push_back(cache_name); 588 ordered_cache_names_.push_back(cache_name);
622 589
623 cache_loader_->WriteIndex( 590 cache_loader_->WriteIndex(
624 ordered_cache_names_, 591 ordered_cache_names_,
625 base::Bind(&ServiceWorkerCacheStorage::CreateCacheDidWriteIndex, 592 base::Bind(&CacheStorage::CreateCacheDidWriteIndex,
626 weak_factory_.GetWeakPtr(), 593 weak_factory_.GetWeakPtr(), callback, cache));
627 callback,
628 cache));
629 } 594 }
630 595
631 void ServiceWorkerCacheStorage::CreateCacheDidWriteIndex( 596 void CacheStorage::CreateCacheDidWriteIndex(
632 const CacheAndErrorCallback& callback, 597 const CacheAndErrorCallback& callback,
633 const scoped_refptr<ServiceWorkerCache>& cache, 598 const scoped_refptr<CacheStorageCache>& cache,
634 bool success) { 599 bool success) {
635 DCHECK_CURRENTLY_ON(BrowserThread::IO); 600 DCHECK_CURRENTLY_ON(BrowserThread::IO);
636 DCHECK(cache.get()); 601 DCHECK(cache.get());
637 602
638 // TODO(jkarlin): Handle !success. 603 // TODO(jkarlin): Handle !success.
639 604
640 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR); 605 callback.Run(cache, CACHE_STORAGE_ERROR_NO_ERROR);
641 } 606 }
642 607
643 void ServiceWorkerCacheStorage::HasCacheImpl( 608 void CacheStorage::HasCacheImpl(const std::string& cache_name,
644 const std::string& cache_name, 609 const BoolAndErrorCallback& callback) {
645 const BoolAndErrorCallback& callback) {
646 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); 610 bool has_cache = cache_map_.find(cache_name) != cache_map_.end();
647 611
648 callback.Run(has_cache, CACHE_STORAGE_ERROR_NO_ERROR); 612 callback.Run(has_cache, CACHE_STORAGE_ERROR_NO_ERROR);
649 } 613 }
650 614
651 void ServiceWorkerCacheStorage::DeleteCacheImpl( 615 void CacheStorage::DeleteCacheImpl(const std::string& cache_name,
652 const std::string& cache_name, 616 const BoolAndErrorCallback& callback) {
653 const BoolAndErrorCallback& callback) {
654 CacheMap::iterator it = cache_map_.find(cache_name); 617 CacheMap::iterator it = cache_map_.find(cache_name);
655 if (it == cache_map_.end()) { 618 if (it == cache_map_.end()) {
656 callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND); 619 callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND);
657 return; 620 return;
658 } 621 }
659 622
660 base::WeakPtr<ServiceWorkerCache> cache = it->second; 623 base::WeakPtr<CacheStorageCache> cache = it->second;
661 cache_map_.erase(it); 624 cache_map_.erase(it);
662 625
663 // Delete the name from ordered_cache_names_. 626 // Delete the name from ordered_cache_names_.
664 StringVector::iterator iter = std::find( 627 StringVector::iterator iter = std::find(
665 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name); 628 ordered_cache_names_.begin(), ordered_cache_names_.end(), cache_name);
666 DCHECK(iter != ordered_cache_names_.end()); 629 DCHECK(iter != ordered_cache_names_.end());
667 ordered_cache_names_.erase(iter); 630 ordered_cache_names_.erase(iter);
668 631
669 base::Closure closure = 632 base::Closure closure =
670 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidClose, 633 base::Bind(&CacheStorage::DeleteCacheDidClose, weak_factory_.GetWeakPtr(),
671 weak_factory_.GetWeakPtr(), cache_name, callback, 634 cache_name, callback, ordered_cache_names_,
672 ordered_cache_names_, make_scoped_refptr(cache.get())); 635 make_scoped_refptr(cache.get()));
673 636
674 if (cache) { 637 if (cache) {
675 cache->Close(closure); 638 cache->Close(closure);
676 return; 639 return;
677 } 640 }
678 641
679 closure.Run(); 642 closure.Run();
680 } 643 }
681 644
682 void ServiceWorkerCacheStorage::DeleteCacheDidClose( 645 void CacheStorage::DeleteCacheDidClose(
683 const std::string& cache_name, 646 const std::string& cache_name,
684 const BoolAndErrorCallback& callback, 647 const BoolAndErrorCallback& callback,
685 const StringVector& ordered_cache_names, 648 const StringVector& ordered_cache_names,
686 const scoped_refptr<ServiceWorkerCache>& cache /* might be null */) { 649 const scoped_refptr<CacheStorageCache>& cache /* might be null */) {
687 cache_loader_->WriteIndex( 650 cache_loader_->WriteIndex(
688 ordered_cache_names, 651 ordered_cache_names,
689 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex, 652 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex,
690 weak_factory_.GetWeakPtr(), cache_name, callback)); 653 weak_factory_.GetWeakPtr(), cache_name, callback));
691 } 654 }
692 655
693 void ServiceWorkerCacheStorage::DeleteCacheDidWriteIndex( 656 void CacheStorage::DeleteCacheDidWriteIndex(
694 const std::string& cache_name, 657 const std::string& cache_name,
695 const BoolAndErrorCallback& callback, 658 const BoolAndErrorCallback& callback,
696 bool success) { 659 bool success) {
697 DCHECK_CURRENTLY_ON(BrowserThread::IO); 660 DCHECK_CURRENTLY_ON(BrowserThread::IO);
698 661
699 cache_loader_->CleanUpDeletedCache( 662 cache_loader_->CleanUpDeletedCache(
700 cache_name, 663 cache_name, base::Bind(&CacheStorage::DeleteCacheDidCleanUp,
701 base::Bind(&ServiceWorkerCacheStorage::DeleteCacheDidCleanUp, 664 weak_factory_.GetWeakPtr(), callback));
702 weak_factory_.GetWeakPtr(),
703 callback));
704 } 665 }
705 666
706 void ServiceWorkerCacheStorage::DeleteCacheDidCleanUp( 667 void CacheStorage::DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback,
707 const BoolAndErrorCallback& callback, 668 bool success) {
708 bool success) {
709 DCHECK_CURRENTLY_ON(BrowserThread::IO); 669 DCHECK_CURRENTLY_ON(BrowserThread::IO);
710 670
711 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR); 671 callback.Run(true, CACHE_STORAGE_ERROR_NO_ERROR);
712 } 672 }
713 673
714 void ServiceWorkerCacheStorage::EnumerateCachesImpl( 674 void CacheStorage::EnumerateCachesImpl(
715 const StringsAndErrorCallback& callback) { 675 const StringsAndErrorCallback& callback) {
716 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR); 676 callback.Run(ordered_cache_names_, CACHE_STORAGE_ERROR_NO_ERROR);
717 } 677 }
718 678
719 void ServiceWorkerCacheStorage::MatchCacheImpl( 679 void CacheStorage::MatchCacheImpl(
720 const std::string& cache_name, 680 const std::string& cache_name,
721 scoped_ptr<ServiceWorkerFetchRequest> request, 681 scoped_ptr<ServiceWorkerFetchRequest> request,
722 const ServiceWorkerCache::ResponseCallback& callback) { 682 const CacheStorageCache::ResponseCallback& callback) {
723 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); 683 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name);
724 684
725 if (!cache.get()) { 685 if (!cache.get()) {
726 callback.Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, 686 callback.Run(CacheStorageCache::ERROR_TYPE_NOT_FOUND,
727 scoped_ptr<ServiceWorkerResponse>(), 687 scoped_ptr<ServiceWorkerResponse>(),
728 scoped_ptr<storage::BlobDataHandle>()); 688 scoped_ptr<storage::BlobDataHandle>());
729 return; 689 return;
730 } 690 }
731 691
732 // Pass the cache along to the callback to keep the cache open until match is 692 // Pass the cache along to the callback to keep the cache open until match is
733 // done. 693 // done.
734 cache->Match(request.Pass(), 694 cache->Match(request.Pass(),
735 base::Bind(&ServiceWorkerCacheStorage::MatchCacheDidMatch, 695 base::Bind(&CacheStorage::MatchCacheDidMatch,
736 weak_factory_.GetWeakPtr(), cache, callback)); 696 weak_factory_.GetWeakPtr(), cache, callback));
737 } 697 }
738 698
739 void ServiceWorkerCacheStorage::MatchCacheDidMatch( 699 void CacheStorage::MatchCacheDidMatch(
740 const scoped_refptr<ServiceWorkerCache>& cache, 700 const scoped_refptr<CacheStorageCache>& cache,
741 const ServiceWorkerCache::ResponseCallback& callback, 701 const CacheStorageCache::ResponseCallback& callback,
742 ServiceWorkerCache::ErrorType error, 702 CacheStorageCache::ErrorType error,
743 scoped_ptr<ServiceWorkerResponse> response, 703 scoped_ptr<ServiceWorkerResponse> response,
744 scoped_ptr<storage::BlobDataHandle> handle) { 704 scoped_ptr<storage::BlobDataHandle> handle) {
745 callback.Run(error, response.Pass(), handle.Pass()); 705 callback.Run(error, response.Pass(), handle.Pass());
746 } 706 }
747 707
748 void ServiceWorkerCacheStorage::MatchAllCachesImpl( 708 void CacheStorage::MatchAllCachesImpl(
749 scoped_ptr<ServiceWorkerFetchRequest> request, 709 scoped_ptr<ServiceWorkerFetchRequest> request,
750 const ServiceWorkerCache::ResponseCallback& callback) { 710 const CacheStorageCache::ResponseCallback& callback) {
751 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback_copy( 711 scoped_ptr<CacheStorageCache::ResponseCallback> callback_copy(
752 new ServiceWorkerCache::ResponseCallback(callback)); 712 new CacheStorageCache::ResponseCallback(callback));
753 713
754 ServiceWorkerCache::ResponseCallback* callback_ptr = callback_copy.get(); 714 CacheStorageCache::ResponseCallback* callback_ptr = callback_copy.get();
755 base::Closure barrier_closure = base::BarrierClosure( 715 base::Closure barrier_closure =
756 ordered_cache_names_.size(), 716 base::BarrierClosure(ordered_cache_names_.size(),
757 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll, 717 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll,
758 weak_factory_.GetWeakPtr(), 718 weak_factory_.GetWeakPtr(),
759 base::Passed(callback_copy.Pass()))); 719 base::Passed(callback_copy.Pass())));
760 720
761 for (const std::string& cache_name : ordered_cache_names_) { 721 for (const std::string& cache_name : ordered_cache_names_) {
762 scoped_refptr<ServiceWorkerCache> cache = GetLoadedCache(cache_name); 722 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name);
763 DCHECK(cache.get()); 723 DCHECK(cache.get());
764 724
765 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)), 725 cache->Match(make_scoped_ptr(new ServiceWorkerFetchRequest(*request)),
766 base::Bind(&ServiceWorkerCacheStorage::MatchAllCachesDidMatch, 726 base::Bind(&CacheStorage::MatchAllCachesDidMatch,
767 weak_factory_.GetWeakPtr(), cache, barrier_closure, 727 weak_factory_.GetWeakPtr(), cache, barrier_closure,
768 callback_ptr)); 728 callback_ptr));
769 } 729 }
770 } 730 }
771 731
772 void ServiceWorkerCacheStorage::MatchAllCachesDidMatch( 732 void CacheStorage::MatchAllCachesDidMatch(
773 scoped_refptr<ServiceWorkerCache> cache, 733 scoped_refptr<CacheStorageCache> cache,
774 const base::Closure& barrier_closure, 734 const base::Closure& barrier_closure,
775 ServiceWorkerCache::ResponseCallback* callback, 735 CacheStorageCache::ResponseCallback* callback,
776 ServiceWorkerCache::ErrorType error, 736 CacheStorageCache::ErrorType error,
777 scoped_ptr<ServiceWorkerResponse> response, 737 scoped_ptr<ServiceWorkerResponse> response,
778 scoped_ptr<storage::BlobDataHandle> handle) { 738 scoped_ptr<storage::BlobDataHandle> handle) {
779 if (callback->is_null() || 739 if (callback->is_null() || error == CacheStorageCache::ERROR_TYPE_NOT_FOUND) {
780 error == ServiceWorkerCache::ERROR_TYPE_NOT_FOUND) {
781 barrier_closure.Run(); 740 barrier_closure.Run();
782 return; 741 return;
783 } 742 }
784 callback->Run(error, response.Pass(), handle.Pass()); 743 callback->Run(error, response.Pass(), handle.Pass());
785 callback->Reset(); // Only call the callback once. 744 callback->Reset(); // Only call the callback once.
786 745
787 barrier_closure.Run(); 746 barrier_closure.Run();
788 } 747 }
789 748
790 void ServiceWorkerCacheStorage::MatchAllCachesDidMatchAll( 749 void CacheStorage::MatchAllCachesDidMatchAll(
791 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback) { 750 scoped_ptr<CacheStorageCache::ResponseCallback> callback) {
792 if (!callback->is_null()) { 751 if (!callback->is_null()) {
793 callback->Run(ServiceWorkerCache::ERROR_TYPE_NOT_FOUND, 752 callback->Run(CacheStorageCache::ERROR_TYPE_NOT_FOUND,
794 scoped_ptr<ServiceWorkerResponse>(), 753 scoped_ptr<ServiceWorkerResponse>(),
795 scoped_ptr<storage::BlobDataHandle>()); 754 scoped_ptr<storage::BlobDataHandle>());
796 } 755 }
797 } 756 }
798 757
799 scoped_refptr<ServiceWorkerCache> ServiceWorkerCacheStorage::GetLoadedCache( 758 scoped_refptr<CacheStorageCache> CacheStorage::GetLoadedCache(
800 const std::string& cache_name) { 759 const std::string& cache_name) {
801 DCHECK_CURRENTLY_ON(BrowserThread::IO); 760 DCHECK_CURRENTLY_ON(BrowserThread::IO);
802 DCHECK(initialized_); 761 DCHECK(initialized_);
803 762
804 CacheMap::iterator map_iter = cache_map_.find(cache_name); 763 CacheMap::iterator map_iter = cache_map_.find(cache_name);
805 if (map_iter == cache_map_.end()) 764 if (map_iter == cache_map_.end())
806 return scoped_refptr<ServiceWorkerCache>(); 765 return scoped_refptr<CacheStorageCache>();
807 766
808 base::WeakPtr<ServiceWorkerCache> cache = map_iter->second; 767 base::WeakPtr<CacheStorageCache> cache = map_iter->second;
809 768
810 if (!cache) { 769 if (!cache) {
811 scoped_refptr<ServiceWorkerCache> new_cache = 770 scoped_refptr<CacheStorageCache> new_cache =
812 cache_loader_->CreateServiceWorkerCache(cache_name); 771 cache_loader_->CreateCache(cache_name);
813 map_iter->second = new_cache->AsWeakPtr(); 772 map_iter->second = new_cache->AsWeakPtr();
814 return new_cache; 773 return new_cache;
815 } 774 }
816 775
817 return make_scoped_refptr(cache.get()); 776 return make_scoped_refptr(cache.get());
818 } 777 }
819 778
820 void ServiceWorkerCacheStorage::CloseAllCachesImpl( 779 void CacheStorage::CloseAllCachesImpl(const base::Closure& callback) {
821 const base::Closure& callback) {
822 int live_cache_count = 0; 780 int live_cache_count = 0;
823 for (const auto& key_value : cache_map_) { 781 for (const auto& key_value : cache_map_) {
824 if (key_value.second) 782 if (key_value.second)
825 live_cache_count += 1; 783 live_cache_count += 1;
826 } 784 }
827 785
828 if (live_cache_count == 0) { 786 if (live_cache_count == 0) {
829 callback.Run(); 787 callback.Run();
830 return; 788 return;
831 } 789 }
832 790
833 // The closure might modify this object so delay calling it until after 791 // The closure might modify this object so delay calling it until after
834 // iterating through cache_map_ by adding one to the barrier. 792 // iterating through cache_map_ by adding one to the barrier.
835 base::Closure barrier_closure = 793 base::Closure barrier_closure =
836 base::BarrierClosure(live_cache_count + 1, base::Bind(callback)); 794 base::BarrierClosure(live_cache_count + 1, base::Bind(callback));
837 795
838 for (auto& key_value : cache_map_) { 796 for (auto& key_value : cache_map_) {
839 if (key_value.second) { 797 if (key_value.second) {
840 key_value.second->Close(base::Bind( 798 key_value.second->Close(base::Bind(
841 CloseAllCachesDidCloseCache, 799 CloseAllCachesDidCloseCache,
842 make_scoped_refptr(key_value.second.get()), barrier_closure)); 800 make_scoped_refptr(key_value.second.get()), barrier_closure));
843 } 801 }
844 } 802 }
845 803
846 barrier_closure.Run(); 804 barrier_closure.Run();
847 } 805 }
848 806
849 void ServiceWorkerCacheStorage::PendingClosure(const base::Closure& callback) { 807 void CacheStorage::PendingClosure(const base::Closure& callback) {
850 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = 808 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
851 weak_factory_.GetWeakPtr();
852 809
853 callback.Run(); 810 callback.Run();
854 if (cache_storage) 811 if (cache_storage)
855 scheduler_->CompleteOperationAndRunNext(); 812 scheduler_->CompleteOperationAndRunNext();
856 } 813 }
857 814
858 void ServiceWorkerCacheStorage::PendingBoolAndErrorCallback( 815 void CacheStorage::PendingBoolAndErrorCallback(
859 const BoolAndErrorCallback& callback, 816 const BoolAndErrorCallback& callback,
860 bool found, 817 bool found,
861 CacheStorageError error) { 818 CacheStorageError error) {
862 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = 819 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
863 weak_factory_.GetWeakPtr();
864 820
865 callback.Run(found, error); 821 callback.Run(found, error);
866 if (cache_storage) 822 if (cache_storage)
867 scheduler_->CompleteOperationAndRunNext(); 823 scheduler_->CompleteOperationAndRunNext();
868 } 824 }
869 825
870 void ServiceWorkerCacheStorage::PendingCacheAndErrorCallback( 826 void CacheStorage::PendingCacheAndErrorCallback(
871 const CacheAndErrorCallback& callback, 827 const CacheAndErrorCallback& callback,
872 const scoped_refptr<ServiceWorkerCache>& cache, 828 const scoped_refptr<CacheStorageCache>& cache,
873 CacheStorageError error) { 829 CacheStorageError error) {
874 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = 830 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
875 weak_factory_.GetWeakPtr();
876 831
877 callback.Run(cache, error); 832 callback.Run(cache, error);
878 if (cache_storage) 833 if (cache_storage)
879 scheduler_->CompleteOperationAndRunNext(); 834 scheduler_->CompleteOperationAndRunNext();
880 } 835 }
881 836
882 void ServiceWorkerCacheStorage::PendingStringsAndErrorCallback( 837 void CacheStorage::PendingStringsAndErrorCallback(
883 const StringsAndErrorCallback& callback, 838 const StringsAndErrorCallback& callback,
884 const StringVector& strings, 839 const StringVector& strings,
885 CacheStorageError error) { 840 CacheStorageError error) {
886 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = 841 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
887 weak_factory_.GetWeakPtr();
888 842
889 callback.Run(strings, error); 843 callback.Run(strings, error);
890 if (cache_storage) 844 if (cache_storage)
891 scheduler_->CompleteOperationAndRunNext(); 845 scheduler_->CompleteOperationAndRunNext();
892 } 846 }
893 847
894 void ServiceWorkerCacheStorage::PendingResponseCallback( 848 void CacheStorage::PendingResponseCallback(
895 const ServiceWorkerCache::ResponseCallback& callback, 849 const CacheStorageCache::ResponseCallback& callback,
896 ServiceWorkerCache::ErrorType error, 850 CacheStorageCache::ErrorType error,
897 scoped_ptr<ServiceWorkerResponse> response, 851 scoped_ptr<ServiceWorkerResponse> response,
898 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { 852 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
899 base::WeakPtr<ServiceWorkerCacheStorage> cache_storage = 853 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
900 weak_factory_.GetWeakPtr();
901 854
902 callback.Run(error, response.Pass(), blob_data_handle.Pass()); 855 callback.Run(error, response.Pass(), blob_data_handle.Pass());
903 if (cache_storage) 856 if (cache_storage)
904 scheduler_->CompleteOperationAndRunNext(); 857 scheduler_->CompleteOperationAndRunNext();
905 } 858 }
906 859
907 } // namespace content 860 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage.h ('k') | content/browser/cache_storage/cache_storage.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698