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

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

Issue 548533002: Make ServiceWorkerCacheStorage::CacheLoader::LoadCache synchronous (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@no_pointers_keys
Patch Set: Rebase Created 6 years, 3 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
« no previous file with comments | « content/browser/service_worker/service_worker_cache_storage.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/service_worker/service_worker_cache_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/files/memory_mapped_file.h" 10 #include "base/files/memory_mapped_file.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 50
51 CacheLoader(base::SequencedTaskRunner* cache_task_runner, 51 CacheLoader(base::SequencedTaskRunner* cache_task_runner,
52 net::URLRequestContext* request_context, 52 net::URLRequestContext* request_context,
53 base::WeakPtr<storage::BlobStorageContext> blob_context) 53 base::WeakPtr<storage::BlobStorageContext> blob_context)
54 : cache_task_runner_(cache_task_runner), 54 : cache_task_runner_(cache_task_runner),
55 request_context_(request_context), 55 request_context_(request_context),
56 blob_context_(blob_context) {} 56 blob_context_(blob_context) {}
57 57
58 virtual ~CacheLoader() {} 58 virtual ~CacheLoader() {}
59 59
60 // Loads the given cache_name, the cache is NULL if it fails. If the cache 60 // Creates a ServiceWorkerCache with the given name. It does not attempt to
61 // doesn't exist a new one is created. 61 // load the backend, that happens lazily when the cache is used.
62 virtual void LoadCache(const std::string& cache_name, 62 virtual scoped_ptr<ServiceWorkerCache> CreateServiceWorkerCache(
63 const CacheCallback& callback) = 0; 63 const std::string& cache_name) = 0;
64 64
65 // 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.
66 virtual void CreateCache(const std::string& cache_name, 66 virtual void CreateCache(const std::string& cache_name,
67 const CacheCallback& callback) = 0; 67 const CacheCallback& callback) = 0;
68 68
69 // 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
70 // removing the cache's directory. 70 // removing the cache's directory.
71 virtual void CleanUpDeletedCache(const std::string& key, 71 virtual void CleanUpDeletedCache(const std::string& key,
72 const BoolCallback& callback) = 0; 72 const BoolCallback& callback) = 0;
73 73
74 // Writes the cache names (and sizes) to disk if applicable. 74 // Writes the cache names (and sizes) to disk if applicable.
75 virtual void WriteIndex(const CacheMap& caches, 75 virtual void WriteIndex(const CacheMap& caches,
76 const BoolCallback& callback) = 0; 76 const BoolCallback& callback) = 0;
77 77
78 // Loads the cache names from disk if applicable. 78 // Loads the cache names from disk if applicable.
79 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, 79 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names,
80 const StringsCallback& callback) = 0; 80 const StringsCallback& callback) = 0;
81 81
82 protected: 82 protected:
83 virtual void LoadCacheImpl(const std::string&) {}
84
85 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; 83 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
86 net::URLRequestContext* request_context_; 84 net::URLRequestContext* request_context_;
87 base::WeakPtr<storage::BlobStorageContext> blob_context_; 85 base::WeakPtr<storage::BlobStorageContext> blob_context_;
88 }; 86 };
89 87
90 class ServiceWorkerCacheStorage::MemoryLoader 88 class ServiceWorkerCacheStorage::MemoryLoader
91 : public ServiceWorkerCacheStorage::CacheLoader { 89 : public ServiceWorkerCacheStorage::CacheLoader {
92 public: 90 public:
93 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, 91 MemoryLoader(base::SequencedTaskRunner* cache_task_runner,
94 net::URLRequestContext* request_context, 92 net::URLRequestContext* request_context,
95 base::WeakPtr<storage::BlobStorageContext> blob_context) 93 base::WeakPtr<storage::BlobStorageContext> blob_context)
96 : CacheLoader(cache_task_runner, request_context, blob_context) {} 94 : CacheLoader(cache_task_runner, request_context, blob_context) {}
97 virtual void LoadCache(const std::string& cache_name, 95
98 const CacheCallback& callback) OVERRIDE { 96 virtual scoped_ptr<ServiceWorkerCache> CreateServiceWorkerCache(
99 NOTREACHED(); 97 const std::string& cache_name) OVERRIDE {
98 return ServiceWorkerCache::CreateMemoryCache(request_context_,
99 blob_context_);
100 } 100 }
101 101
102 virtual void CreateCache(const std::string& cache_name, 102 virtual void CreateCache(const std::string& cache_name,
103 const CacheCallback& callback) OVERRIDE { 103 const CacheCallback& callback) OVERRIDE {
104 scoped_ptr<ServiceWorkerCache> cache = 104 callback.Run(CreateServiceWorkerCache(cache_name).Pass());
105 ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_);
106 callback.Run(cache.Pass());
107 } 105 }
108 106
109 virtual void CleanUpDeletedCache(const std::string& cache_name, 107 virtual void CleanUpDeletedCache(const std::string& cache_name,
110 const BoolCallback& callback) OVERRIDE { 108 const BoolCallback& callback) OVERRIDE {
111 callback.Run(true); 109 callback.Run(true);
112 } 110 }
113 111
114 virtual void WriteIndex(const CacheMap& caches, 112 virtual void WriteIndex(const CacheMap& caches,
115 const BoolCallback& callback) OVERRIDE { 113 const BoolCallback& callback) OVERRIDE {
116 callback.Run(false); 114 callback.Run(false);
(...skipping 12 matching lines...) Expand all
129 : public ServiceWorkerCacheStorage::CacheLoader { 127 : public ServiceWorkerCacheStorage::CacheLoader {
130 public: 128 public:
131 SimpleCacheLoader(const base::FilePath& origin_path, 129 SimpleCacheLoader(const base::FilePath& origin_path,
132 base::SequencedTaskRunner* cache_task_runner, 130 base::SequencedTaskRunner* cache_task_runner,
133 net::URLRequestContext* request_context, 131 net::URLRequestContext* request_context,
134 base::WeakPtr<storage::BlobStorageContext> blob_context) 132 base::WeakPtr<storage::BlobStorageContext> blob_context)
135 : CacheLoader(cache_task_runner, request_context, blob_context), 133 : CacheLoader(cache_task_runner, request_context, blob_context),
136 origin_path_(origin_path), 134 origin_path_(origin_path),
137 weak_ptr_factory_(this) {} 135 weak_ptr_factory_(this) {}
138 136
139 virtual void LoadCache(const std::string& cache_name, 137 virtual scoped_ptr<ServiceWorkerCache> CreateServiceWorkerCache(
140 const CacheCallback& callback) OVERRIDE { 138 const std::string& cache_name) OVERRIDE {
141 DCHECK_CURRENTLY_ON(BrowserThread::IO); 139 DCHECK_CURRENTLY_ON(BrowserThread::IO);
142 140
143 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) 141 return ServiceWorkerCache::CreatePersistentCache(
144 // 2. Create the cache object. (LoadDidCreateDirectory) 142 CreatePersistentCachePath(origin_path_, cache_name),
145 143 request_context_,
146 cache_task_runner_->PostTask( 144 blob_context_);
147 FROM_HERE,
148 base::Bind(&SimpleCacheLoader::LoadCreateDirectoryInPool,
149 CreatePersistentCachePath(origin_path_, cache_name),
150 cache_name,
151 callback,
152 weak_ptr_factory_.GetWeakPtr(),
153 base::MessageLoopProxy::current()));
154 }
155
156 static void LoadCreateDirectoryInPool(
157 const base::FilePath& path,
158 const std::string& cache_name,
159 const CacheCallback& callback,
160 base::WeakPtr<SimpleCacheLoader> loader,
161 const scoped_refptr<base::MessageLoopProxy>& original_loop) {
162 bool rv = base::CreateDirectory(path);
163 original_loop->PostTask(
164 FROM_HERE,
165 base::Bind(&SimpleCacheLoader::LoadDidCreateDirectory,
166 cache_name,
167 callback,
168 loader,
169 rv));
170 }
171
172 static void LoadDidCreateDirectory(const std::string& cache_name,
173 const CacheCallback& callback,
174 base::WeakPtr<SimpleCacheLoader> loader,
175 bool dir_rv) {
176 DCHECK_CURRENTLY_ON(BrowserThread::IO);
177
178 if (!dir_rv || !loader) {
179 callback.Run(scoped_ptr<ServiceWorkerCache>());
180 return;
181 }
182
183 scoped_ptr<ServiceWorkerCache> cache =
184 ServiceWorkerCache::CreatePersistentCache(
185 CreatePersistentCachePath(loader->origin_path_, cache_name),
186 loader->request_context_,
187 loader->blob_context_);
188 callback.Run(cache.Pass());
189 } 145 }
190 146
191 virtual void CreateCache(const std::string& cache_name, 147 virtual void CreateCache(const std::string& cache_name,
192 const CacheCallback& callback) OVERRIDE { 148 const CacheCallback& callback) OVERRIDE {
193 DCHECK_CURRENTLY_ON(BrowserThread::IO); 149 DCHECK_CURRENTLY_ON(BrowserThread::IO);
194 150
195 // 1. Delete the cache's directory if it exists. 151 // 1. Delete the cache's directory if it exists.
196 // (CreateCacheDeleteFilesInPool) 152 // (CreateCacheDeleteFilesInPool)
197 // 2. Load the cache. (LoadCreateDirectoryInPool) 153 // 2. Load the cache. (LoadCreateDirectoryInPool)
198 154
199 base::FilePath cache_path = 155 base::FilePath cache_path =
200 CreatePersistentCachePath(origin_path_, cache_name); 156 CreatePersistentCachePath(origin_path_, cache_name);
201 157
202 cache_task_runner_->PostTask( 158 PostTaskAndReplyWithResult(
159 cache_task_runner_.get(),
203 FROM_HERE, 160 FROM_HERE,
204 base::Bind(&SimpleCacheLoader::CreateCacheDeleteFilesInPool, 161 base::Bind(&SimpleCacheLoader::CreateCachePrepDirInPool, cache_path),
205 cache_path, 162 base::Bind(&SimpleCacheLoader::CreateCachePreppedDir,
206 cache_name, 163 cache_name,
207 callback, 164 callback,
208 weak_ptr_factory_.GetWeakPtr(), 165 weak_ptr_factory_.GetWeakPtr()));
209 base::MessageLoopProxy::current()));
210 } 166 }
211 167
212 static void CreateCacheDeleteFilesInPool( 168 static bool CreateCachePrepDirInPool(const base::FilePath& cache_path) {
213 const base::FilePath& cache_path, 169 if (base::PathExists(cache_path))
214 const std::string& cache_name, 170 base::DeleteFile(cache_path, /* recursive */ true);
215 const CacheCallback& callback, 171 return base::CreateDirectory(cache_path);
216 base::WeakPtr<SimpleCacheLoader> loader, 172 }
217 const scoped_refptr<base::MessageLoopProxy>& original_loop) {
218 base::FilePath path(cache_path);
219 if (base::PathExists(path))
220 base::DeleteFile(path, /* recursive */ true);
221 173
222 // Jump straight into LoadCache on the same thread. 174 static void CreateCachePreppedDir(const std::string& cache_name,
223 LoadCreateDirectoryInPool( 175 const CacheCallback& callback,
224 cache_path, cache_name, callback, loader, original_loop); 176 base::WeakPtr<SimpleCacheLoader> loader,
177 bool success) {
178 if (!success || !loader) {
179 callback.Run(scoped_ptr<ServiceWorkerCache>());
180 return;
181 }
182
183 callback.Run(loader->CreateServiceWorkerCache(cache_name));
225 } 184 }
226 185
227 virtual void CleanUpDeletedCache(const std::string& cache_name, 186 virtual void CleanUpDeletedCache(const std::string& cache_name,
228 const BoolCallback& callback) OVERRIDE { 187 const BoolCallback& callback) OVERRIDE {
229 DCHECK_CURRENTLY_ON(BrowserThread::IO); 188 DCHECK_CURRENTLY_ON(BrowserThread::IO);
230 189
231 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) 190 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool)
232 191
233 base::FilePath cache_path = 192 base::FilePath cache_path =
234 CreatePersistentCachePath(origin_path_, cache_name); 193 CreatePersistentCachePath(origin_path_, cache_name);
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, 493 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex,
535 weak_factory_.GetWeakPtr(), 494 weak_factory_.GetWeakPtr(),
536 callback)); 495 callback));
537 } 496 }
538 497
539 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( 498 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex(
540 const base::Closure& callback, 499 const base::Closure& callback,
541 scoped_ptr<std::vector<std::string> > indexed_cache_names) { 500 scoped_ptr<std::vector<std::string> > indexed_cache_names) {
542 DCHECK_CURRENTLY_ON(BrowserThread::IO); 501 DCHECK_CURRENTLY_ON(BrowserThread::IO);
543 502
544 if (indexed_cache_names->empty()) { 503 for (std::vector<std::string>::iterator it = indexed_cache_names->begin();
545 LazyInitDone(); 504 it != indexed_cache_names->end();
546 return; 505 ++it) {
506 scoped_ptr<ServiceWorkerCache> cache =
507 cache_loader_->CreateServiceWorkerCache(*it);
508 AddCacheToMaps(*it, cache.Pass());
547 } 509 }
548 510
549 std::vector<std::string>::const_iterator iter = indexed_cache_names->begin();
550 std::vector<std::string>::const_iterator iter_next = iter + 1;
551
552 cache_loader_->LoadCache(
553 *iter,
554 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName,
555 weak_factory_.GetWeakPtr(),
556 callback,
557 base::Passed(indexed_cache_names.Pass()),
558 iter_next,
559 *iter));
560 }
561
562 void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName(
563 const base::Closure& callback,
564 scoped_ptr<std::vector<std::string> > indexed_cache_names,
565 const std::vector<std::string>::const_iterator& iter,
566 const std::string& cache_name,
567 scoped_ptr<ServiceWorkerCache> cache) {
568 DCHECK_CURRENTLY_ON(BrowserThread::IO);
569
570 if (cache)
571 AddCacheToMaps(cache_name, cache.Pass());
572
573 if (iter == indexed_cache_names->end()) {
574 LazyInitDone();
575 return;
576 }
577
578 std::vector<std::string>::const_iterator iter_next = iter + 1;
579 cache_loader_->LoadCache(
580 *iter,
581 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName,
582 weak_factory_.GetWeakPtr(),
583 callback,
584 base::Passed(indexed_cache_names.Pass()),
585 iter_next,
586 *iter));
587 }
588
589 void ServiceWorkerCacheStorage::LazyInitDone() {
590 initialized_ = true; 511 initialized_ = true;
591 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); 512 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin();
592 it != init_callbacks_.end(); 513 it != init_callbacks_.end();
593 ++it) { 514 ++it) {
594 it->Run(); 515 it->Run();
595 } 516 }
596 init_callbacks_.clear(); 517 init_callbacks_.clear();
597 } 518 }
598 519
599 ServiceWorkerCacheStorage::CacheContext* 520 ServiceWorkerCacheStorage::CacheContext*
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 NameMap::const_iterator name_iter = name_map_.find(cache_name); 595 NameMap::const_iterator name_iter = name_map_.find(cache_name);
675 if (name_iter == name_map_.end()) 596 if (name_iter == name_map_.end())
676 return NULL; 597 return NULL;
677 598
678 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); 599 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second);
679 DCHECK(map_iter != cache_map_.end()); 600 DCHECK(map_iter != cache_map_.end());
680 return map_iter->second; 601 return map_iter->second;
681 } 602 }
682 603
683 } // namespace content 604 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_cache_storage.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698