| 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/cache_storage/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/guid.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/numerics/safe_conversions.h" | 16 #include "base/numerics/safe_conversions.h" |
| 16 #include "base/sha1.h" | 17 #include "base/sha1.h" |
| 17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 21 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
| 22 #include "content/browser/cache_storage/cache_storage.pb.h" | 23 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 23 #include "content/browser/cache_storage/cache_storage_cache.h" | 24 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 24 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 25 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 #include "net/base/directory_lister.h" | 27 #include "net/base/directory_lister.h" |
| 27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 28 #include "net/url_request/url_request_context_getter.h" | 29 #include "net/url_request/url_request_context_getter.h" |
| 29 #include "storage/browser/blob/blob_storage_context.h" | 30 #include "storage/browser/blob/blob_storage_context.h" |
| 30 #include "storage/browser/quota/quota_manager_proxy.h" | 31 #include "storage/browser/quota/quota_manager_proxy.h" |
| 31 | 32 |
| 32 namespace content { | 33 namespace content { |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 37 std::string HexedHash(const std::string& value) { |
| 38 std::string value_hash = base::SHA1HashString(value); |
| 39 std::string valued_hexed_hash = base::ToLowerASCII( |
| 40 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 41 return valued_hexed_hash; |
| 42 } |
| 43 |
| 36 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache, | 44 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache, |
| 37 const base::Closure& barrier_closure) { | 45 const base::Closure& barrier_closure) { |
| 38 barrier_closure.Run(); | 46 barrier_closure.Run(); |
| 39 } | 47 } |
| 40 | 48 |
| 49 std::string MigrateCachesIfNecessaryInPool(const std::string& body, |
| 50 const base::FilePath& index_path) { |
| 51 CacheStorageIndex index; |
| 52 base::FilePath origin_path = index_path.DirName(); |
| 53 |
| 54 if (!index.ParseFromString(body)) |
| 55 return body; |
| 56 |
| 57 bool index_is_dirty = false; |
| 58 |
| 59 // Look for caches that have no cache_dir. Give any such caches a directory |
| 60 // with a random name and move them there. Then, rewrite the index file. |
| 61 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
| 62 const CacheStorageIndex::Cache& cache = index.cache(i); |
| 63 if (!cache.has_cache_dir()) { |
| 64 // find a new home for the cache. |
| 65 base::FilePath legacy_cache_path = |
| 66 origin_path.AppendASCII(HexedHash(cache.name())); |
| 67 std::string cache_dir; |
| 68 base::FilePath cache_path; |
| 69 do { |
| 70 cache_dir = base::GenerateGUID(); |
| 71 cache_path = origin_path.AppendASCII(cache_dir); |
| 72 } while (base::PathExists(cache_path)); |
| 73 |
| 74 if (!base::Move(legacy_cache_path, cache_path)) |
| 75 return ""; |
| 76 |
| 77 index.mutable_cache(0)->set_cache_dir(cache_dir); |
| 78 index_is_dirty = true; |
| 79 } |
| 80 } |
| 81 |
| 82 if (index_is_dirty) { |
| 83 std::string new_body; |
| 84 if (!index.SerializeToString(&new_body)) |
| 85 return ""; |
| 86 if (base::WriteFile(index_path, new_body.c_str(), new_body.size()) != |
| 87 base::checked_cast<int>(new_body.size())) |
| 88 return ""; |
| 89 return new_body; |
| 90 } |
| 91 |
| 92 return body; |
| 93 } |
| 94 |
| 95 std::string ReadAndMigrateIndexInPool(const base::FilePath& index_path) { |
| 96 std::string body; |
| 97 base::ReadFileToString(index_path, &body); |
| 98 |
| 99 return MigrateCachesIfNecessaryInPool(body, index_path); |
| 100 } |
| 101 |
| 41 } // namespace | 102 } // namespace |
| 42 | 103 |
| 104 class MigratedLegacyCacheDirectoryNameTest; |
| 105 |
| 43 const char CacheStorage::kIndexFileName[] = "index.txt"; | 106 const char CacheStorage::kIndexFileName[] = "index.txt"; |
| 44 | 107 |
| 45 // Handles the loading and clean up of CacheStorageCache objects. The | 108 // Handles the loading and clean up of CacheStorageCache objects. |
| 46 // callback of every public method is guaranteed to be called. | |
| 47 class CacheStorage::CacheLoader { | 109 class CacheStorage::CacheLoader { |
| 48 public: | 110 public: |
| 49 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&)> | 111 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&)> |
| 50 CacheCallback; | 112 CacheCallback; |
| 51 typedef base::Callback<void(bool)> BoolCallback; | 113 typedef base::Callback<void(bool)> BoolCallback; |
| 52 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)> | 114 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)> |
| 53 StringVectorCallback; | 115 StringVectorCallback; |
| 54 | 116 |
| 55 CacheLoader( | 117 CacheLoader( |
| 56 base::SequencedTaskRunner* cache_task_runner, | 118 base::SequencedTaskRunner* cache_task_runner, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 67 } | 129 } |
| 68 | 130 |
| 69 virtual ~CacheLoader() {} | 131 virtual ~CacheLoader() {} |
| 70 | 132 |
| 71 // Creates a CacheStorageCache with the given name. It does not attempt to | 133 // Creates a CacheStorageCache with the given name. It does not attempt to |
| 72 // load the backend, that happens lazily when the cache is used. | 134 // load the backend, that happens lazily when the cache is used. |
| 73 virtual scoped_refptr<CacheStorageCache> CreateCache( | 135 virtual scoped_refptr<CacheStorageCache> CreateCache( |
| 74 const std::string& cache_name) = 0; | 136 const std::string& cache_name) = 0; |
| 75 | 137 |
| 76 // Deletes any pre-existing cache of the same name and then loads it. | 138 // Deletes any pre-existing cache of the same name and then loads it. |
| 77 virtual void CreateCache(const std::string& cache_name, | 139 virtual void PrepareNewCacheDestination(const std::string& cache_name, |
| 78 const CacheCallback& callback) = 0; | 140 const CacheCallback& callback) = 0; |
| 79 | 141 |
| 80 // After the backend has been deleted, do any extra house keeping such as | 142 // After the backend has been deleted, do any extra house keeping such as |
| 81 // removing the cache's directory. | 143 // removing the cache's directory. |
| 82 virtual void CleanUpDeletedCache(const std::string& key, | 144 virtual void CleanUpDeletedCache(const std::string& key, |
| 83 const BoolCallback& callback) = 0; | 145 const BoolCallback& callback) = 0; |
| 84 | 146 |
| 85 // Writes the cache names (and sizes) to disk if applicable. | 147 // Writes the cache names (and sizes) to disk if applicable. |
| 86 virtual void WriteIndex(const StringVector& cache_names, | 148 virtual void WriteIndex(const StringVector& cache_names, |
| 87 const BoolCallback& callback) = 0; | 149 const BoolCallback& callback) = 0; |
| 88 | 150 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 115 quota_manager_proxy, | 177 quota_manager_proxy, |
| 116 blob_context, | 178 blob_context, |
| 117 origin) {} | 179 origin) {} |
| 118 | 180 |
| 119 scoped_refptr<CacheStorageCache> CreateCache( | 181 scoped_refptr<CacheStorageCache> CreateCache( |
| 120 const std::string& cache_name) override { | 182 const std::string& cache_name) override { |
| 121 return CacheStorageCache::CreateMemoryCache( | 183 return CacheStorageCache::CreateMemoryCache( |
| 122 origin_, request_context_getter_, quota_manager_proxy_, blob_context_); | 184 origin_, request_context_getter_, quota_manager_proxy_, blob_context_); |
| 123 } | 185 } |
| 124 | 186 |
| 125 void CreateCache(const std::string& cache_name, | 187 void PrepareNewCacheDestination(const std::string& cache_name, |
| 126 const CacheCallback& callback) override { | 188 const CacheCallback& callback) override { |
| 127 scoped_refptr<CacheStorageCache> cache = CreateCache(cache_name); | 189 scoped_refptr<CacheStorageCache> cache = CreateCache(cache_name); |
| 128 cache_refs_.insert(std::make_pair(cache_name, cache)); | 190 cache_refs_.insert(std::make_pair(cache_name, cache)); |
| 129 callback.Run(cache); | 191 callback.Run(cache); |
| 130 } | 192 } |
| 131 | 193 |
| 132 void CleanUpDeletedCache(const std::string& cache_name, | 194 void CleanUpDeletedCache(const std::string& cache_name, |
| 133 const BoolCallback& callback) override { | 195 const BoolCallback& callback) override { |
| 134 CacheRefMap::iterator it = cache_refs_.find(cache_name); | 196 CacheRefMap::iterator it = cache_refs_.find(cache_name); |
| 135 DCHECK(it != cache_refs_.end()); | 197 DCHECK(it != cache_refs_.end()); |
| 136 cache_refs_.erase(it); | 198 cache_refs_.erase(it); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 request_context, | 232 request_context, |
| 171 quota_manager_proxy, | 233 quota_manager_proxy, |
| 172 blob_context, | 234 blob_context, |
| 173 origin), | 235 origin), |
| 174 origin_path_(origin_path), | 236 origin_path_(origin_path), |
| 175 weak_ptr_factory_(this) {} | 237 weak_ptr_factory_(this) {} |
| 176 | 238 |
| 177 scoped_refptr<CacheStorageCache> CreateCache( | 239 scoped_refptr<CacheStorageCache> CreateCache( |
| 178 const std::string& cache_name) override { | 240 const std::string& cache_name) override { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 241 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 242 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 180 | 243 |
| 244 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 245 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 181 return CacheStorageCache::CreatePersistentCache( | 246 return CacheStorageCache::CreatePersistentCache( |
| 182 origin_, CreatePersistentCachePath(origin_path_, cache_name), | 247 origin_, cache_path, request_context_getter_, quota_manager_proxy_, |
| 183 request_context_getter_, quota_manager_proxy_, blob_context_); | 248 blob_context_); |
| 184 } | 249 } |
| 185 | 250 |
| 186 void CreateCache(const std::string& cache_name, | 251 void PrepareNewCacheDestination(const std::string& cache_name, |
| 187 const CacheCallback& callback) override { | 252 const CacheCallback& callback) override { |
| 188 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 253 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 189 | 254 |
| 190 // 1. Delete the cache's directory if it exists. | |
| 191 // (CreateCacheDeleteFilesInPool) | |
| 192 // 2. Load the cache. (LoadCreateDirectoryInPool) | |
| 193 | |
| 194 base::FilePath cache_path = | |
| 195 CreatePersistentCachePath(origin_path_, cache_name); | |
| 196 | |
| 197 PostTaskAndReplyWithResult( | 255 PostTaskAndReplyWithResult( |
| 198 cache_task_runner_.get(), FROM_HERE, | 256 cache_task_runner_.get(), FROM_HERE, |
| 199 base::Bind(&SimpleCacheLoader::CreateCachePrepDirInPool, cache_path), | 257 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 200 base::Bind(&SimpleCacheLoader::CreateCachePreppedDir, cache_name, | 258 origin_path_), |
| 201 callback, weak_ptr_factory_.GetWeakPtr())); | 259 base::Bind(&SimpleCacheLoader::PrepareNewCacheCreateCache, |
| 260 weak_ptr_factory_.GetWeakPtr(), cache_name, callback)); |
| 202 } | 261 } |
| 203 | 262 |
| 204 static bool CreateCachePrepDirInPool(const base::FilePath& cache_path) { | 263 // Runs on the cache_task_runner_. |
| 205 if (base::PathExists(cache_path)) | 264 static std::string PrepareNewCacheDirectoryInPool( |
| 206 base::DeleteFile(cache_path, /* recursive */ true); | 265 const base::FilePath& origin_path) { |
| 207 return base::CreateDirectory(cache_path); | 266 std::string cache_dir; |
| 267 base::FilePath cache_path; |
| 268 do { |
| 269 cache_dir = base::GenerateGUID(); |
| 270 cache_path = origin_path.AppendASCII(cache_dir); |
| 271 } while (base::PathExists(cache_path)); |
| 272 |
| 273 return base::CreateDirectory(cache_path) ? cache_dir : ""; |
| 208 } | 274 } |
| 209 | 275 |
| 210 static void CreateCachePreppedDir(const std::string& cache_name, | 276 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 211 const CacheCallback& callback, | 277 const CacheCallback& callback, |
| 212 base::WeakPtr<SimpleCacheLoader> loader, | 278 const std::string& cache_dir) { |
| 213 bool success) { | 279 if (cache_dir.empty()) { |
| 214 if (!success || !loader) { | |
| 215 callback.Run(scoped_refptr<CacheStorageCache>()); | 280 callback.Run(scoped_refptr<CacheStorageCache>()); |
| 216 return; | 281 return; |
| 217 } | 282 } |
| 218 | 283 |
| 219 callback.Run(loader->CreateCache(cache_name)); | 284 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 285 callback.Run(CreateCache(cache_name)); |
| 220 } | 286 } |
| 221 | 287 |
| 222 void CleanUpDeletedCache(const std::string& cache_name, | 288 void CleanUpDeletedCache(const std::string& cache_name, |
| 223 const BoolCallback& callback) override { | 289 const BoolCallback& callback) override { |
| 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 225 | 291 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 226 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) | |
| 227 | 292 |
| 228 base::FilePath cache_path = | 293 base::FilePath cache_path = |
| 229 CreatePersistentCachePath(origin_path_, cache_name); | 294 origin_path_.AppendASCII(cache_name_to_cache_dir_[cache_name]); |
| 295 cache_name_to_cache_dir_.erase(cache_name); |
| 296 |
| 230 cache_task_runner_->PostTask( | 297 cache_task_runner_->PostTask( |
| 231 FROM_HERE, | 298 FROM_HERE, |
| 232 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, cache_path, | 299 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, cache_path, |
| 233 callback, base::ThreadTaskRunnerHandle::Get())); | 300 callback, base::ThreadTaskRunnerHandle::Get())); |
| 234 } | 301 } |
| 235 | 302 |
| 236 static void CleanUpDeleteCacheDirInPool( | 303 static void CleanUpDeleteCacheDirInPool( |
| 237 const base::FilePath& cache_path, | 304 const base::FilePath& cache_path, |
| 238 const BoolCallback& callback, | 305 const BoolCallback& callback, |
| 239 const scoped_refptr<base::SingleThreadTaskRunner>& original_task_runner) { | 306 const scoped_refptr<base::SingleThreadTaskRunner>& original_task_runner) { |
| 240 bool rv = base::DeleteFile(cache_path, true); | 307 bool rv = base::DeleteFile(cache_path, true); |
| 241 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, rv)); | 308 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, rv)); |
| 242 } | 309 } |
| 243 | 310 |
| 244 void WriteIndex(const StringVector& cache_names, | 311 void WriteIndex(const StringVector& cache_names, |
| 245 const BoolCallback& callback) override { | 312 const BoolCallback& callback) override { |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 313 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 247 | 314 |
| 248 // 1. Create the index file as a string. (WriteIndex) | 315 // 1. Create the index file as a string. (WriteIndex) |
| 249 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 316 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
| 250 | 317 |
| 251 CacheStorageIndex index; | 318 CacheStorageIndex index; |
| 252 index.set_origin(origin_.spec()); | 319 index.set_origin(origin_.spec()); |
| 253 | 320 |
| 254 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { | 321 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { |
| 322 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_names[i])); |
| 323 |
| 255 CacheStorageIndex::Cache* index_cache = index.add_cache(); | 324 CacheStorageIndex::Cache* index_cache = index.add_cache(); |
| 256 index_cache->set_name(cache_names[i]); | 325 index_cache->set_name(cache_names[i]); |
| 326 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_names[i]]); |
| 257 } | 327 } |
| 258 | 328 |
| 259 std::string serialized; | 329 std::string serialized; |
| 260 bool success = index.SerializeToString(&serialized); | 330 bool success = index.SerializeToString(&serialized); |
| 261 DCHECK(success); | 331 DCHECK(success); |
| 262 | 332 |
| 263 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 333 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
| 264 base::FilePath index_path = | 334 base::FilePath index_path = |
| 265 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 335 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
| 266 | 336 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 290 void LoadIndex(scoped_ptr<std::vector<std::string>> names, | 360 void LoadIndex(scoped_ptr<std::vector<std::string>> names, |
| 291 const StringVectorCallback& callback) override { | 361 const StringVectorCallback& callback) override { |
| 292 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 362 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 293 | 363 |
| 294 // 1. Read the file from disk. (LoadIndexReadFileInPool) | 364 // 1. Read the file from disk. (LoadIndexReadFileInPool) |
| 295 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) | 365 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) |
| 296 | 366 |
| 297 base::FilePath index_path = | 367 base::FilePath index_path = |
| 298 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 368 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
| 299 | 369 |
| 300 cache_task_runner_->PostTask( | 370 PostTaskAndReplyWithResult( |
| 301 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, | 371 cache_task_runner_.get(), FROM_HERE, |
| 302 index_path, base::Passed(names.Pass()), callback, | 372 base::Bind(&ReadAndMigrateIndexInPool, index_path), |
| 303 base::ThreadTaskRunnerHandle::Get())); | 373 base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, |
| 374 weak_ptr_factory_.GetWeakPtr(), base::Passed(&names), |
| 375 callback)); |
| 304 } | 376 } |
| 305 | 377 |
| 306 static void LoadIndexReadFileInPool( | 378 void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, |
| 307 const base::FilePath& index_path, | 379 const StringVectorCallback& callback, |
| 308 scoped_ptr<std::vector<std::string>> names, | 380 const std::string& serialized) { |
| 309 const StringVectorCallback& callback, | |
| 310 const scoped_refptr<base::SingleThreadTaskRunner>& original_task_runner) { | |
| 311 std::string body; | |
| 312 base::ReadFileToString(index_path, &body); | |
| 313 | |
| 314 original_task_runner->PostTask( | |
| 315 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, | |
| 316 base::Passed(names.Pass()), callback, body)); | |
| 317 } | |
| 318 | |
| 319 static void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, | |
| 320 const StringVectorCallback& callback, | |
| 321 const std::string& serialized) { | |
| 322 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 381 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 323 | 382 |
| 324 CacheStorageIndex index; | 383 CacheStorageIndex index; |
| 325 if (index.ParseFromString(serialized)) { | 384 if (index.ParseFromString(serialized)) { |
| 326 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 385 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
| 327 const CacheStorageIndex::Cache& cache = index.cache(i); | 386 const CacheStorageIndex::Cache& cache = index.cache(i); |
| 387 DCHECK(cache.has_cache_dir()); |
| 328 names->push_back(cache.name()); | 388 names->push_back(cache.name()); |
| 389 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 329 } | 390 } |
| 330 } | 391 } |
| 331 | 392 |
| 332 // TODO(jkarlin): Delete caches that are in the directory and not returned | 393 // TODO(jkarlin): Delete caches that are in the directory and not returned |
| 333 // in LoadIndex. | 394 // in LoadIndex. |
| 334 callback.Run(names.Pass()); | 395 callback.Run(names.Pass()); |
| 335 } | 396 } |
| 336 | 397 |
| 337 private: | 398 private: |
| 399 friend class MigratedLegacyCacheDirectoryNameTest; |
| 338 ~SimpleCacheLoader() override {} | 400 ~SimpleCacheLoader() override {} |
| 339 | 401 |
| 340 static std::string HexedHash(const std::string& value) { | |
| 341 std::string value_hash = base::SHA1HashString(value); | |
| 342 std::string valued_hexed_hash = base::ToLowerASCII( | |
| 343 base::HexEncode(value_hash.c_str(), value_hash.length())); | |
| 344 return valued_hexed_hash; | |
| 345 } | |
| 346 | |
| 347 static base::FilePath CreatePersistentCachePath( | |
| 348 const base::FilePath& origin_path, | |
| 349 const std::string& cache_name) { | |
| 350 return origin_path.AppendASCII(HexedHash(cache_name)); | |
| 351 } | |
| 352 | |
| 353 const base::FilePath origin_path_; | 402 const base::FilePath origin_path_; |
| 403 std::map<std::string, std::string> cache_name_to_cache_dir_; |
| 354 | 404 |
| 355 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; | 405 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; |
| 356 }; | 406 }; |
| 357 | 407 |
| 358 CacheStorage::CacheStorage( | 408 CacheStorage::CacheStorage( |
| 359 const base::FilePath& path, | 409 const base::FilePath& path, |
| 360 bool memory_only, | 410 bool memory_only, |
| 361 base::SequencedTaskRunner* cache_task_runner, | 411 base::SequencedTaskRunner* cache_task_runner, |
| 362 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 412 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 363 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 413 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 } | 612 } |
| 563 | 613 |
| 564 void CacheStorage::OpenCacheImpl(const std::string& cache_name, | 614 void CacheStorage::OpenCacheImpl(const std::string& cache_name, |
| 565 const CacheAndErrorCallback& callback) { | 615 const CacheAndErrorCallback& callback) { |
| 566 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); | 616 scoped_refptr<CacheStorageCache> cache = GetLoadedCache(cache_name); |
| 567 if (cache.get()) { | 617 if (cache.get()) { |
| 568 callback.Run(cache, CACHE_STORAGE_OK); | 618 callback.Run(cache, CACHE_STORAGE_OK); |
| 569 return; | 619 return; |
| 570 } | 620 } |
| 571 | 621 |
| 572 cache_loader_->CreateCache( | 622 cache_loader_->PrepareNewCacheDestination( |
| 573 cache_name, base::Bind(&CacheStorage::CreateCacheDidCreateCache, | 623 cache_name, base::Bind(&CacheStorage::CreateCacheDidCreateCache, |
| 574 weak_factory_.GetWeakPtr(), cache_name, callback)); | 624 weak_factory_.GetWeakPtr(), cache_name, callback)); |
| 575 } | 625 } |
| 576 | 626 |
| 577 void CacheStorage::CreateCacheDidCreateCache( | 627 void CacheStorage::CreateCacheDidCreateCache( |
| 578 const std::string& cache_name, | 628 const std::string& cache_name, |
| 579 const CacheAndErrorCallback& callback, | 629 const CacheAndErrorCallback& callback, |
| 580 const scoped_refptr<CacheStorageCache>& cache) { | 630 const scoped_refptr<CacheStorageCache>& cache) { |
| 581 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 631 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 582 | 632 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 605 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 655 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 606 DCHECK(cache.get()); | 656 DCHECK(cache.get()); |
| 607 | 657 |
| 608 // TODO(jkarlin): Handle !success. | 658 // TODO(jkarlin): Handle !success. |
| 609 | 659 |
| 610 callback.Run(cache, CACHE_STORAGE_OK); | 660 callback.Run(cache, CACHE_STORAGE_OK); |
| 611 } | 661 } |
| 612 | 662 |
| 613 void CacheStorage::HasCacheImpl(const std::string& cache_name, | 663 void CacheStorage::HasCacheImpl(const std::string& cache_name, |
| 614 const BoolAndErrorCallback& callback) { | 664 const BoolAndErrorCallback& callback) { |
| 615 bool has_cache = cache_map_.find(cache_name) != cache_map_.end(); | 665 bool has_cache = ContainsKey(cache_map_, cache_name); |
| 616 | |
| 617 callback.Run(has_cache, CACHE_STORAGE_OK); | 666 callback.Run(has_cache, CACHE_STORAGE_OK); |
| 618 } | 667 } |
| 619 | 668 |
| 620 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, | 669 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, |
| 621 const BoolAndErrorCallback& callback) { | 670 const BoolAndErrorCallback& callback) { |
| 622 CacheMap::iterator it = cache_map_.find(cache_name); | 671 CacheMap::iterator it = cache_map_.find(cache_name); |
| 623 if (it == cache_map_.end()) { | 672 if (it == cache_map_.end()) { |
| 624 callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND); | 673 callback.Run(false, CACHE_STORAGE_ERROR_NOT_FOUND); |
| 625 return; | 674 return; |
| 626 } | 675 } |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 scoped_ptr<ServiceWorkerResponse> response, | 905 scoped_ptr<ServiceWorkerResponse> response, |
| 857 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 906 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
| 858 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 907 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
| 859 | 908 |
| 860 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 909 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
| 861 if (cache_storage) | 910 if (cache_storage) |
| 862 scheduler_->CompleteOperationAndRunNext(); | 911 scheduler_->CompleteOperationAndRunNext(); |
| 863 } | 912 } |
| 864 | 913 |
| 865 } // namespace content | 914 } // namespace content |
| OLD | NEW |