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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 // Loads the given cache_name, the cache is NULL if it fails. If the cache |
61 // doesn't exist a new one is created. | 61 // doesn't exist a new one is created. |
michaeln
2014/09/09 03:27:44
can you update the comment, looks like this can no
jkarlin
2014/09/09 19:18:49
Done.
| |
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 Loading... | |
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 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
580 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, | 539 base::Bind(&ServiceWorkerCacheStorage::LazyInitDidLoadIndex, |
581 weak_factory_.GetWeakPtr(), | 540 weak_factory_.GetWeakPtr(), |
582 callback)); | 541 callback)); |
583 } | 542 } |
584 | 543 |
585 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( | 544 void ServiceWorkerCacheStorage::LazyInitDidLoadIndex( |
586 const base::Closure& callback, | 545 const base::Closure& callback, |
587 scoped_ptr<std::vector<std::string> > indexed_cache_names) { | 546 scoped_ptr<std::vector<std::string> > indexed_cache_names) { |
588 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 547 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
589 | 548 |
590 if (indexed_cache_names->empty()) { | 549 for (std::vector<std::string>::iterator it = indexed_cache_names->begin(); |
591 LazyInitDone(); | 550 it != indexed_cache_names->end(); |
592 return; | 551 ++it) { |
552 scoped_ptr<ServiceWorkerCache> cache = | |
553 cache_loader_->CreateServiceWorkerCache(*it); | |
554 if (!cache) | |
555 continue; | |
556 AddCacheToMaps(*it, cache.Pass()); | |
593 } | 557 } |
594 | 558 |
595 std::vector<std::string>::const_iterator iter = indexed_cache_names->begin(); | |
596 std::vector<std::string>::const_iterator iter_next = iter + 1; | |
597 | |
598 cache_loader_->LoadCache( | |
599 *iter, | |
600 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName, | |
601 weak_factory_.GetWeakPtr(), | |
602 callback, | |
603 base::Passed(indexed_cache_names.Pass()), | |
604 iter_next, | |
605 *iter)); | |
606 } | |
607 | |
608 void ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName( | |
609 const base::Closure& callback, | |
610 scoped_ptr<std::vector<std::string> > indexed_cache_names, | |
611 const std::vector<std::string>::const_iterator& iter, | |
612 const std::string& cache_name, | |
613 scoped_ptr<ServiceWorkerCache> cache) { | |
614 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
615 | |
616 if (cache) | |
617 AddCacheToMaps(cache_name, cache.Pass()); | |
618 | |
619 if (iter == indexed_cache_names->end()) { | |
620 LazyInitDone(); | |
621 return; | |
622 } | |
623 | |
624 std::vector<std::string>::const_iterator iter_next = iter + 1; | |
625 cache_loader_->LoadCache( | |
626 *iter, | |
627 base::Bind(&ServiceWorkerCacheStorage::LazyInitIterateAndLoadCacheName, | |
628 weak_factory_.GetWeakPtr(), | |
629 callback, | |
630 base::Passed(indexed_cache_names.Pass()), | |
631 iter_next, | |
632 *iter)); | |
633 } | |
634 | |
635 void ServiceWorkerCacheStorage::LazyInitDone() { | |
636 initialized_ = true; | 559 initialized_ = true; |
637 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); | 560 for (std::vector<base::Closure>::iterator it = init_callbacks_.begin(); |
638 it != init_callbacks_.end(); | 561 it != init_callbacks_.end(); |
639 ++it) { | 562 ++it) { |
640 it->Run(); | 563 it->Run(); |
641 } | 564 } |
642 init_callbacks_.clear(); | 565 init_callbacks_.clear(); |
643 } | 566 } |
644 | 567 |
645 ServiceWorkerCacheStorage::CacheContext* | 568 ServiceWorkerCacheStorage::CacheContext* |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
724 NameMap::const_iterator name_iter = name_map_.find(cache_name); | 647 NameMap::const_iterator name_iter = name_map_.find(cache_name); |
725 if (name_iter == name_map_.end()) | 648 if (name_iter == name_map_.end()) |
726 return NULL; | 649 return NULL; |
727 | 650 |
728 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); | 651 CacheMap::const_iterator map_iter = cache_map_.find(name_iter->second); |
729 DCHECK(map_iter != cache_map_.end()); | 652 DCHECK(map_iter != cache_map_.end()); |
730 return map_iter->second; | 653 return map_iter->second; |
731 } | 654 } |
732 | 655 |
733 } // namespace content | 656 } // namespace content |
OLD | NEW |