| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // cache is deleted. | 82 // cache is deleted. |
| 83 class ServiceWorkerCacheStorage::MemoryLoader | 83 class ServiceWorkerCacheStorage::MemoryLoader |
| 84 : public ServiceWorkerCacheStorage::CacheLoader { | 84 : public ServiceWorkerCacheStorage::CacheLoader { |
| 85 public: | 85 public: |
| 86 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, | 86 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, |
| 87 net::URLRequestContext* request_context, | 87 net::URLRequestContext* request_context, |
| 88 base::WeakPtr<storage::BlobStorageContext> blob_context, | 88 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 89 const GURL& origin) | 89 const GURL& origin) |
| 90 : CacheLoader(cache_task_runner, request_context, blob_context, origin) {} | 90 : CacheLoader(cache_task_runner, request_context, blob_context, origin) {} |
| 91 | 91 |
| 92 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 92 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
| 93 const std::string& cache_name) override { | 93 const std::string& cache_name) override { |
| 94 return ServiceWorkerCache::CreateMemoryCache(request_context_, | 94 return ServiceWorkerCache::CreateMemoryCache(request_context_, |
| 95 blob_context_); | 95 blob_context_); |
| 96 } | 96 } |
| 97 | 97 |
| 98 virtual void CreateCache(const std::string& cache_name, | 98 void CreateCache(const std::string& cache_name, |
| 99 const CacheCallback& callback) override { | 99 const CacheCallback& callback) override { |
| 100 scoped_refptr<ServiceWorkerCache> cache = | 100 scoped_refptr<ServiceWorkerCache> cache = |
| 101 ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_); | 101 ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_); |
| 102 cache_refs_.insert(std::make_pair(cache_name, cache)); | 102 cache_refs_.insert(std::make_pair(cache_name, cache)); |
| 103 callback.Run(cache); | 103 callback.Run(cache); |
| 104 } | 104 } |
| 105 | 105 |
| 106 virtual void CleanUpDeletedCache(const std::string& cache_name, | 106 void CleanUpDeletedCache(const std::string& cache_name, |
| 107 const BoolCallback& callback) override { | 107 const BoolCallback& callback) override { |
| 108 CacheRefMap::iterator it = cache_refs_.find(cache_name); | 108 CacheRefMap::iterator it = cache_refs_.find(cache_name); |
| 109 DCHECK(it != cache_refs_.end()); | 109 DCHECK(it != cache_refs_.end()); |
| 110 cache_refs_.erase(it); | 110 cache_refs_.erase(it); |
| 111 callback.Run(true); | 111 callback.Run(true); |
| 112 } | 112 } |
| 113 | 113 |
| 114 virtual void WriteIndex(const StringVector& cache_names, | 114 void WriteIndex(const StringVector& cache_names, |
| 115 const BoolCallback& callback) override { | 115 const BoolCallback& callback) override { |
| 116 callback.Run(false); | 116 callback.Run(false); |
| 117 } | 117 } |
| 118 | 118 |
| 119 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 119 void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names, |
| 120 const StringVectorCallback& callback) override { | 120 const StringVectorCallback& callback) override { |
| 121 callback.Run(cache_names.Pass()); | 121 callback.Run(cache_names.Pass()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 private: | 124 private: |
| 125 typedef std::map<std::string, scoped_refptr<ServiceWorkerCache> > CacheRefMap; | 125 typedef std::map<std::string, scoped_refptr<ServiceWorkerCache> > CacheRefMap; |
| 126 virtual ~MemoryLoader() {} | 126 ~MemoryLoader() override {} |
| 127 | 127 |
| 128 // Keep a reference to each cache to ensure that it's not freed before the | 128 // Keep a reference to each cache to ensure that it's not freed before the |
| 129 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is | 129 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is |
| 130 // freed. | 130 // freed. |
| 131 CacheRefMap cache_refs_; | 131 CacheRefMap cache_refs_; |
| 132 }; | 132 }; |
| 133 | 133 |
| 134 class ServiceWorkerCacheStorage::SimpleCacheLoader | 134 class ServiceWorkerCacheStorage::SimpleCacheLoader |
| 135 : public ServiceWorkerCacheStorage::CacheLoader { | 135 : public ServiceWorkerCacheStorage::CacheLoader { |
| 136 public: | 136 public: |
| 137 SimpleCacheLoader(const base::FilePath& origin_path, | 137 SimpleCacheLoader(const base::FilePath& origin_path, |
| 138 base::SequencedTaskRunner* cache_task_runner, | 138 base::SequencedTaskRunner* cache_task_runner, |
| 139 net::URLRequestContext* request_context, | 139 net::URLRequestContext* request_context, |
| 140 base::WeakPtr<storage::BlobStorageContext> blob_context, | 140 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 141 const GURL& origin) | 141 const GURL& origin) |
| 142 : CacheLoader(cache_task_runner, request_context, blob_context, origin), | 142 : CacheLoader(cache_task_runner, request_context, blob_context, origin), |
| 143 origin_path_(origin_path), | 143 origin_path_(origin_path), |
| 144 weak_ptr_factory_(this) {} | 144 weak_ptr_factory_(this) {} |
| 145 | 145 |
| 146 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 146 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
| 147 const std::string& cache_name) override { | 147 const std::string& cache_name) override { |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 149 | 149 |
| 150 return ServiceWorkerCache::CreatePersistentCache( | 150 return ServiceWorkerCache::CreatePersistentCache( |
| 151 CreatePersistentCachePath(origin_path_, cache_name), | 151 CreatePersistentCachePath(origin_path_, cache_name), |
| 152 request_context_, | 152 request_context_, |
| 153 blob_context_); | 153 blob_context_); |
| 154 } | 154 } |
| 155 | 155 |
| 156 virtual void CreateCache(const std::string& cache_name, | 156 void CreateCache(const std::string& cache_name, |
| 157 const CacheCallback& callback) override { | 157 const CacheCallback& callback) override { |
| 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 159 | 159 |
| 160 // 1. Delete the cache's directory if it exists. | 160 // 1. Delete the cache's directory if it exists. |
| 161 // (CreateCacheDeleteFilesInPool) | 161 // (CreateCacheDeleteFilesInPool) |
| 162 // 2. Load the cache. (LoadCreateDirectoryInPool) | 162 // 2. Load the cache. (LoadCreateDirectoryInPool) |
| 163 | 163 |
| 164 base::FilePath cache_path = | 164 base::FilePath cache_path = |
| 165 CreatePersistentCachePath(origin_path_, cache_name); | 165 CreatePersistentCachePath(origin_path_, cache_name); |
| 166 | 166 |
| 167 PostTaskAndReplyWithResult( | 167 PostTaskAndReplyWithResult( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 185 base::WeakPtr<SimpleCacheLoader> loader, | 185 base::WeakPtr<SimpleCacheLoader> loader, |
| 186 bool success) { | 186 bool success) { |
| 187 if (!success || !loader) { | 187 if (!success || !loader) { |
| 188 callback.Run(scoped_refptr<ServiceWorkerCache>()); | 188 callback.Run(scoped_refptr<ServiceWorkerCache>()); |
| 189 return; | 189 return; |
| 190 } | 190 } |
| 191 | 191 |
| 192 callback.Run(loader->CreateServiceWorkerCache(cache_name)); | 192 callback.Run(loader->CreateServiceWorkerCache(cache_name)); |
| 193 } | 193 } |
| 194 | 194 |
| 195 virtual void CleanUpDeletedCache(const std::string& cache_name, | 195 void CleanUpDeletedCache(const std::string& cache_name, |
| 196 const BoolCallback& callback) override { | 196 const BoolCallback& callback) override { |
| 197 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 197 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 198 | 198 |
| 199 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) | 199 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) |
| 200 | 200 |
| 201 base::FilePath cache_path = | 201 base::FilePath cache_path = |
| 202 CreatePersistentCachePath(origin_path_, cache_name); | 202 CreatePersistentCachePath(origin_path_, cache_name); |
| 203 cache_task_runner_->PostTask( | 203 cache_task_runner_->PostTask( |
| 204 FROM_HERE, | 204 FROM_HERE, |
| 205 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, | 205 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, |
| 206 cache_path, | 206 cache_path, |
| 207 callback, | 207 callback, |
| 208 base::MessageLoopProxy::current())); | 208 base::MessageLoopProxy::current())); |
| 209 } | 209 } |
| 210 | 210 |
| 211 static void CleanUpDeleteCacheDirInPool( | 211 static void CleanUpDeleteCacheDirInPool( |
| 212 const base::FilePath& cache_path, | 212 const base::FilePath& cache_path, |
| 213 const BoolCallback& callback, | 213 const BoolCallback& callback, |
| 214 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 214 const scoped_refptr<base::MessageLoopProxy>& original_loop) { |
| 215 bool rv = base::DeleteFile(cache_path, true); | 215 bool rv = base::DeleteFile(cache_path, true); |
| 216 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 216 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); |
| 217 } | 217 } |
| 218 | 218 |
| 219 virtual void WriteIndex(const StringVector& cache_names, | 219 void WriteIndex(const StringVector& cache_names, |
| 220 const BoolCallback& callback) override { | 220 const BoolCallback& callback) override { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 222 | 222 |
| 223 // 1. Create the index file as a string. (WriteIndex) | 223 // 1. Create the index file as a string. (WriteIndex) |
| 224 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 224 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
| 225 | 225 |
| 226 ServiceWorkerCacheStorageIndex index; | 226 ServiceWorkerCacheStorageIndex index; |
| 227 index.set_origin(origin_.spec()); | 227 index.set_origin(origin_.spec()); |
| 228 | 228 |
| 229 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { | 229 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { |
| 230 ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache(); | 230 ServiceWorkerCacheStorageIndex::Cache* index_cache = index.add_cache(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 260 if (bytes_written != implicit_cast<int>(data.size())) { | 260 if (bytes_written != implicit_cast<int>(data.size())) { |
| 261 base::DeleteFile(tmp_path, /* recursive */ false); | 261 base::DeleteFile(tmp_path, /* recursive */ false); |
| 262 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); | 262 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); |
| 263 } | 263 } |
| 264 | 264 |
| 265 // Atomically rename the temporary index file to become the real one. | 265 // Atomically rename the temporary index file to become the real one. |
| 266 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); | 266 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); |
| 267 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 267 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); |
| 268 } | 268 } |
| 269 | 269 |
| 270 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > names, | 270 void LoadIndex(scoped_ptr<std::vector<std::string>> names, |
| 271 const StringVectorCallback& callback) override { | 271 const StringVectorCallback& callback) override { |
| 272 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 272 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 273 | 273 |
| 274 // 1. Read the file from disk. (LoadIndexReadFileInPool) | 274 // 1. Read the file from disk. (LoadIndexReadFileInPool) |
| 275 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) | 275 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) |
| 276 | 276 |
| 277 base::FilePath index_path = | 277 base::FilePath index_path = |
| 278 origin_path_.AppendASCII(ServiceWorkerCacheStorage::kIndexFileName); | 278 origin_path_.AppendASCII(ServiceWorkerCacheStorage::kIndexFileName); |
| 279 | 279 |
| 280 cache_task_runner_->PostTask( | 280 cache_task_runner_->PostTask( |
| 281 FROM_HERE, | 281 FROM_HERE, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 names->push_back(cache.name()); | 313 names->push_back(cache.name()); |
| 314 } | 314 } |
| 315 } | 315 } |
| 316 | 316 |
| 317 // TODO(jkarlin): Delete caches that are in the directory and not returned | 317 // TODO(jkarlin): Delete caches that are in the directory and not returned |
| 318 // in LoadIndex. | 318 // in LoadIndex. |
| 319 callback.Run(names.Pass()); | 319 callback.Run(names.Pass()); |
| 320 } | 320 } |
| 321 | 321 |
| 322 private: | 322 private: |
| 323 virtual ~SimpleCacheLoader() {} | 323 ~SimpleCacheLoader() override {} |
| 324 | 324 |
| 325 static std::string HexedHash(const std::string& value) { | 325 static std::string HexedHash(const std::string& value) { |
| 326 std::string value_hash = base::SHA1HashString(value); | 326 std::string value_hash = base::SHA1HashString(value); |
| 327 std::string valued_hexed_hash = base::StringToLowerASCII( | 327 std::string valued_hexed_hash = base::StringToLowerASCII( |
| 328 base::HexEncode(value_hash.c_str(), value_hash.length())); | 328 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 329 return valued_hexed_hash; | 329 return valued_hexed_hash; |
| 330 } | 330 } |
| 331 | 331 |
| 332 static base::FilePath CreatePersistentCachePath( | 332 static base::FilePath CreatePersistentCachePath( |
| 333 const base::FilePath& origin_path, | 333 const base::FilePath& origin_path, |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 scoped_refptr<ServiceWorkerCache> new_cache = | 592 scoped_refptr<ServiceWorkerCache> new_cache = |
| 593 cache_loader_->CreateServiceWorkerCache(cache_name); | 593 cache_loader_->CreateServiceWorkerCache(cache_name); |
| 594 map_iter->second = new_cache->AsWeakPtr(); | 594 map_iter->second = new_cache->AsWeakPtr(); |
| 595 return new_cache; | 595 return new_cache; |
| 596 } | 596 } |
| 597 | 597 |
| 598 return make_scoped_refptr(cache.get()); | 598 return make_scoped_refptr(cache.get()); |
| 599 } | 599 } |
| 600 | 600 |
| 601 } // namespace content | 601 } // namespace content |
| OLD | NEW |