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/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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // cache is deleted. | 76 // cache is deleted. |
77 class ServiceWorkerCacheStorage::MemoryLoader | 77 class ServiceWorkerCacheStorage::MemoryLoader |
78 : public ServiceWorkerCacheStorage::CacheLoader { | 78 : public ServiceWorkerCacheStorage::CacheLoader { |
79 public: | 79 public: |
80 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, | 80 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, |
81 net::URLRequestContext* request_context, | 81 net::URLRequestContext* request_context, |
82 base::WeakPtr<storage::BlobStorageContext> blob_context) | 82 base::WeakPtr<storage::BlobStorageContext> blob_context) |
83 : CacheLoader(cache_task_runner, request_context, blob_context) {} | 83 : CacheLoader(cache_task_runner, request_context, blob_context) {} |
84 | 84 |
85 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 85 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
86 const std::string& cache_name) OVERRIDE { | 86 const std::string& cache_name) override { |
87 return ServiceWorkerCache::CreateMemoryCache(request_context_, | 87 return ServiceWorkerCache::CreateMemoryCache(request_context_, |
88 blob_context_); | 88 blob_context_); |
89 } | 89 } |
90 | 90 |
91 virtual void CreateCache(const std::string& cache_name, | 91 virtual void CreateCache(const std::string& cache_name, |
92 const CacheCallback& callback) OVERRIDE { | 92 const CacheCallback& callback) override { |
93 scoped_refptr<ServiceWorkerCache> cache = | 93 scoped_refptr<ServiceWorkerCache> cache = |
94 ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_); | 94 ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_); |
95 cache_refs_.insert(std::make_pair(cache_name, cache)); | 95 cache_refs_.insert(std::make_pair(cache_name, cache)); |
96 callback.Run(cache); | 96 callback.Run(cache); |
97 } | 97 } |
98 | 98 |
99 virtual void CleanUpDeletedCache(const std::string& cache_name, | 99 virtual void CleanUpDeletedCache(const std::string& cache_name, |
100 const BoolCallback& callback) OVERRIDE { | 100 const BoolCallback& callback) override { |
101 CacheRefMap::iterator it = cache_refs_.find(cache_name); | 101 CacheRefMap::iterator it = cache_refs_.find(cache_name); |
102 DCHECK(it != cache_refs_.end()); | 102 DCHECK(it != cache_refs_.end()); |
103 cache_refs_.erase(it); | 103 cache_refs_.erase(it); |
104 callback.Run(true); | 104 callback.Run(true); |
105 } | 105 } |
106 | 106 |
107 virtual void WriteIndex(const StringVector& cache_names, | 107 virtual void WriteIndex(const StringVector& cache_names, |
108 const BoolCallback& callback) OVERRIDE { | 108 const BoolCallback& callback) override { |
109 callback.Run(false); | 109 callback.Run(false); |
110 } | 110 } |
111 | 111 |
112 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 112 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
113 const StringVectorCallback& callback) OVERRIDE { | 113 const StringVectorCallback& callback) override { |
114 callback.Run(cache_names.Pass()); | 114 callback.Run(cache_names.Pass()); |
115 } | 115 } |
116 | 116 |
117 private: | 117 private: |
118 typedef std::map<std::string, scoped_refptr<ServiceWorkerCache> > CacheRefMap; | 118 typedef std::map<std::string, scoped_refptr<ServiceWorkerCache> > CacheRefMap; |
119 virtual ~MemoryLoader() {} | 119 virtual ~MemoryLoader() {} |
120 | 120 |
121 // Keep a reference to each cache to ensure that it's not freed before the | 121 // Keep a reference to each cache to ensure that it's not freed before the |
122 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is | 122 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is |
123 // freed. | 123 // freed. |
124 CacheRefMap cache_refs_; | 124 CacheRefMap cache_refs_; |
125 }; | 125 }; |
126 | 126 |
127 class ServiceWorkerCacheStorage::SimpleCacheLoader | 127 class ServiceWorkerCacheStorage::SimpleCacheLoader |
128 : public ServiceWorkerCacheStorage::CacheLoader { | 128 : public ServiceWorkerCacheStorage::CacheLoader { |
129 public: | 129 public: |
130 SimpleCacheLoader(const base::FilePath& origin_path, | 130 SimpleCacheLoader(const base::FilePath& origin_path, |
131 base::SequencedTaskRunner* cache_task_runner, | 131 base::SequencedTaskRunner* cache_task_runner, |
132 net::URLRequestContext* request_context, | 132 net::URLRequestContext* request_context, |
133 base::WeakPtr<storage::BlobStorageContext> blob_context) | 133 base::WeakPtr<storage::BlobStorageContext> blob_context) |
134 : CacheLoader(cache_task_runner, request_context, blob_context), | 134 : CacheLoader(cache_task_runner, request_context, blob_context), |
135 origin_path_(origin_path), | 135 origin_path_(origin_path), |
136 weak_ptr_factory_(this) {} | 136 weak_ptr_factory_(this) {} |
137 | 137 |
138 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 138 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
139 const std::string& cache_name) OVERRIDE { | 139 const std::string& cache_name) override { |
140 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
141 | 141 |
142 return ServiceWorkerCache::CreatePersistentCache( | 142 return ServiceWorkerCache::CreatePersistentCache( |
143 CreatePersistentCachePath(origin_path_, cache_name), | 143 CreatePersistentCachePath(origin_path_, cache_name), |
144 request_context_, | 144 request_context_, |
145 blob_context_); | 145 blob_context_); |
146 } | 146 } |
147 | 147 |
148 virtual void CreateCache(const std::string& cache_name, | 148 virtual void CreateCache(const std::string& cache_name, |
149 const CacheCallback& callback) OVERRIDE { | 149 const CacheCallback& callback) override { |
150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
151 | 151 |
152 // 1. Delete the cache's directory if it exists. | 152 // 1. Delete the cache's directory if it exists. |
153 // (CreateCacheDeleteFilesInPool) | 153 // (CreateCacheDeleteFilesInPool) |
154 // 2. Load the cache. (LoadCreateDirectoryInPool) | 154 // 2. Load the cache. (LoadCreateDirectoryInPool) |
155 | 155 |
156 base::FilePath cache_path = | 156 base::FilePath cache_path = |
157 CreatePersistentCachePath(origin_path_, cache_name); | 157 CreatePersistentCachePath(origin_path_, cache_name); |
158 | 158 |
159 PostTaskAndReplyWithResult( | 159 PostTaskAndReplyWithResult( |
(...skipping 18 matching lines...) Expand all Loading... |
178 bool success) { | 178 bool success) { |
179 if (!success || !loader) { | 179 if (!success || !loader) { |
180 callback.Run(scoped_refptr<ServiceWorkerCache>()); | 180 callback.Run(scoped_refptr<ServiceWorkerCache>()); |
181 return; | 181 return; |
182 } | 182 } |
183 | 183 |
184 callback.Run(loader->CreateServiceWorkerCache(cache_name)); | 184 callback.Run(loader->CreateServiceWorkerCache(cache_name)); |
185 } | 185 } |
186 | 186 |
187 virtual void CleanUpDeletedCache(const std::string& cache_name, | 187 virtual void CleanUpDeletedCache(const std::string& cache_name, |
188 const BoolCallback& callback) OVERRIDE { | 188 const BoolCallback& callback) override { |
189 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
190 | 190 |
191 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) | 191 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) |
192 | 192 |
193 base::FilePath cache_path = | 193 base::FilePath cache_path = |
194 CreatePersistentCachePath(origin_path_, cache_name); | 194 CreatePersistentCachePath(origin_path_, cache_name); |
195 cache_task_runner_->PostTask( | 195 cache_task_runner_->PostTask( |
196 FROM_HERE, | 196 FROM_HERE, |
197 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, | 197 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, |
198 cache_path, | 198 cache_path, |
199 callback, | 199 callback, |
200 base::MessageLoopProxy::current())); | 200 base::MessageLoopProxy::current())); |
201 } | 201 } |
202 | 202 |
203 static void CleanUpDeleteCacheDirInPool( | 203 static void CleanUpDeleteCacheDirInPool( |
204 const base::FilePath& cache_path, | 204 const base::FilePath& cache_path, |
205 const BoolCallback& callback, | 205 const BoolCallback& callback, |
206 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 206 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
207 bool rv = base::DeleteFile(cache_path, true); | 207 bool rv = base::DeleteFile(cache_path, true); |
208 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 208 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); |
209 } | 209 } |
210 | 210 |
211 virtual void WriteIndex(const StringVector& cache_names, | 211 virtual void WriteIndex(const StringVector& cache_names, |
212 const BoolCallback& callback) OVERRIDE { | 212 const BoolCallback& callback) override { |
213 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 213 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
214 | 214 |
215 // 1. Create the index file as a string. (WriteIndex) | 215 // 1. Create the index file as a string. (WriteIndex) |
216 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 216 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
217 | 217 |
218 ServiceWorkerCacheStorageIndex index; | 218 ServiceWorkerCacheStorageIndex index; |
219 | 219 |
220 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { | 220 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { |
221 ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache(); | 221 ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache(); |
222 index_cache->set_name(cache_names[i]); | 222 index_cache->set_name(cache_names[i]); |
(...skipping 28 matching lines...) Expand all Loading... |
251 base::DeleteFile(tmp_path, /* recursive */ false); | 251 base::DeleteFile(tmp_path, /* recursive */ false); |
252 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); | 252 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); |
253 } | 253 } |
254 | 254 |
255 // Atomically rename the temporary index file to become the real one. | 255 // Atomically rename the temporary index file to become the real one. |
256 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); | 256 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); |
257 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 257 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); |
258 } | 258 } |
259 | 259 |
260 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > names, | 260 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > names, |
261 const StringVectorCallback& callback) OVERRIDE { | 261 const StringVectorCallback& callback) override { |
262 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
263 | 263 |
264 // 1. Read the file from disk. (LoadIndexReadFileInPool) | 264 // 1. Read the file from disk. (LoadIndexReadFileInPool) |
265 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) | 265 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) |
266 | 266 |
267 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); | 267 base::FilePath index_path = origin_path_.AppendASCII("index.txt"); |
268 | 268 |
269 cache_task_runner_->PostTask( | 269 cache_task_runner_->PostTask( |
270 FROM_HERE, | 270 FROM_HERE, |
271 base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, | 271 base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 scoped_refptr<ServiceWorkerCache> new_cache = | 586 scoped_refptr<ServiceWorkerCache> new_cache = |
587 cache_loader_->CreateServiceWorkerCache(cache_name); | 587 cache_loader_->CreateServiceWorkerCache(cache_name); |
588 map_iter->second = new_cache->AsWeakPtr(); | 588 map_iter->second = new_cache->AsWeakPtr(); |
589 return new_cache; | 589 return new_cache; |
590 } | 590 } |
591 | 591 |
592 return make_scoped_refptr(cache.get()); | 592 return make_scoped_refptr(cache.get()); |
593 } | 593 } |
594 | 594 |
595 } // namespace content | 595 } // namespace content |
OLD | NEW |