| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 } | 189 } |
| 190 | 190 |
| 191 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, | 191 void LoadIndex(std::unique_ptr<std::vector<std::string>> cache_names, |
| 192 const StringVectorCallback& callback) override { | 192 const StringVectorCallback& callback) override { |
| 193 callback.Run(std::move(cache_names)); | 193 callback.Run(std::move(cache_names)); |
| 194 } | 194 } |
| 195 | 195 |
| 196 void NotifyCacheCreated( | 196 void NotifyCacheCreated( |
| 197 const std::string& cache_name, | 197 const std::string& cache_name, |
| 198 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 198 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 199 DCHECK(!ContainsKey(cache_handles_, cache_name)); | 199 DCHECK(!base::ContainsKey(cache_handles_, cache_name)); |
| 200 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); | 200 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 void NotifyCacheDoomed( | 203 void NotifyCacheDoomed( |
| 204 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 204 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 205 DCHECK(ContainsKey(cache_handles_, cache_handle->value()->cache_name())); | 205 DCHECK( |
| 206 base::ContainsKey(cache_handles_, cache_handle->value()->cache_name())); |
| 206 cache_handles_.erase(cache_handle->value()->cache_name()); | 207 cache_handles_.erase(cache_handle->value()->cache_name()); |
| 207 }; | 208 }; |
| 208 | 209 |
| 209 private: | 210 private: |
| 210 typedef std::map<std::string, std::unique_ptr<CacheStorageCacheHandle>> | 211 typedef std::map<std::string, std::unique_ptr<CacheStorageCacheHandle>> |
| 211 CacheHandles; | 212 CacheHandles; |
| 212 ~MemoryLoader() override {} | 213 ~MemoryLoader() override {} |
| 213 | 214 |
| 214 // Keep a reference to each cache to ensure that it's not freed before the | 215 // Keep a reference to each cache to ensure that it's not freed before the |
| 215 // client calls CacheStorage::Delete or the CacheStorage is | 216 // client calls CacheStorage::Delete or the CacheStorage is |
| (...skipping 15 matching lines...) Expand all Loading... |
| 231 quota_manager_proxy, | 232 quota_manager_proxy, |
| 232 blob_context, | 233 blob_context, |
| 233 cache_storage, | 234 cache_storage, |
| 234 origin), | 235 origin), |
| 235 origin_path_(origin_path), | 236 origin_path_(origin_path), |
| 236 weak_ptr_factory_(this) {} | 237 weak_ptr_factory_(this) {} |
| 237 | 238 |
| 238 std::unique_ptr<CacheStorageCache> CreateCache( | 239 std::unique_ptr<CacheStorageCache> CreateCache( |
| 239 const std::string& cache_name) override { | 240 const std::string& cache_name) override { |
| 240 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 241 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 241 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_name)); | 242 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 242 | 243 |
| 243 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; | 244 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 244 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); | 245 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 245 return CacheStorageCache::CreatePersistentCache( | 246 return CacheStorageCache::CreatePersistentCache( |
| 246 origin_, cache_name, cache_storage_, cache_path, | 247 origin_, cache_name, cache_storage_, cache_path, |
| 247 request_context_getter_, quota_manager_proxy_, blob_context_); | 248 request_context_getter_, quota_manager_proxy_, blob_context_); |
| 248 } | 249 } |
| 249 | 250 |
| 250 void PrepareNewCacheDestination(const std::string& cache_name, | 251 void PrepareNewCacheDestination(const std::string& cache_name, |
| 251 const CacheCallback& callback) override { | 252 const CacheCallback& callback) override { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 279 callback.Run(std::unique_ptr<CacheStorageCache>()); | 280 callback.Run(std::unique_ptr<CacheStorageCache>()); |
| 280 return; | 281 return; |
| 281 } | 282 } |
| 282 | 283 |
| 283 cache_name_to_cache_dir_[cache_name] = cache_dir; | 284 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 284 callback.Run(CreateCache(cache_name)); | 285 callback.Run(CreateCache(cache_name)); |
| 285 } | 286 } |
| 286 | 287 |
| 287 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 288 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 288 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 289 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 289 DCHECK(ContainsKey(doomed_cache_to_path_, cache)); | 290 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 290 | 291 |
| 291 base::FilePath cache_path = | 292 base::FilePath cache_path = |
| 292 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 293 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 293 doomed_cache_to_path_.erase(cache); | 294 doomed_cache_to_path_.erase(cache); |
| 294 | 295 |
| 295 cache_task_runner_->PostTask( | 296 cache_task_runner_->PostTask( |
| 296 FROM_HERE, base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, | 297 FROM_HERE, base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, |
| 297 cache_path)); | 298 cache_path)); |
| 298 } | 299 } |
| 299 | 300 |
| 300 static void CleanUpDeleteCacheDirInPool(const base::FilePath& cache_path) { | 301 static void CleanUpDeleteCacheDirInPool(const base::FilePath& cache_path) { |
| 301 base::DeleteFile(cache_path, true /* recursive */); | 302 base::DeleteFile(cache_path, true /* recursive */); |
| 302 } | 303 } |
| 303 | 304 |
| 304 void WriteIndex(const StringVector& cache_names, | 305 void WriteIndex(const StringVector& cache_names, |
| 305 const BoolCallback& callback) override { | 306 const BoolCallback& callback) override { |
| 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 307 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 307 | 308 |
| 308 // 1. Create the index file as a string. (WriteIndex) | 309 // 1. Create the index file as a string. (WriteIndex) |
| 309 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 310 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
| 310 | 311 |
| 311 CacheStorageIndex index; | 312 CacheStorageIndex index; |
| 312 index.set_origin(origin_.spec()); | 313 index.set_origin(origin_.spec()); |
| 313 | 314 |
| 314 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { | 315 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { |
| 315 DCHECK(ContainsKey(cache_name_to_cache_dir_, cache_names[i])); | 316 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_names[i])); |
| 316 | 317 |
| 317 CacheStorageIndex::Cache* index_cache = index.add_cache(); | 318 CacheStorageIndex::Cache* index_cache = index.add_cache(); |
| 318 index_cache->set_name(cache_names[i]); | 319 index_cache->set_name(cache_names[i]); |
| 319 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_names[i]]); | 320 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_names[i]]); |
| 320 } | 321 } |
| 321 | 322 |
| 322 std::string serialized; | 323 std::string serialized; |
| 323 bool success = index.SerializeToString(&serialized); | 324 bool success = index.SerializeToString(&serialized); |
| 324 DCHECK(success); | 325 DCHECK(success); |
| 325 | 326 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 } | 386 } |
| 386 | 387 |
| 387 cache_task_runner_->PostTask( | 388 cache_task_runner_->PostTask( |
| 388 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | 389 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, |
| 389 base::Passed(&cache_dirs))); | 390 base::Passed(&cache_dirs))); |
| 390 callback.Run(std::move(names)); | 391 callback.Run(std::move(names)); |
| 391 } | 392 } |
| 392 | 393 |
| 393 void NotifyCacheDoomed( | 394 void NotifyCacheDoomed( |
| 394 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 395 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 395 DCHECK(ContainsKey(cache_name_to_cache_dir_, | 396 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, |
| 396 cache_handle->value()->cache_name())); | 397 cache_handle->value()->cache_name())); |
| 397 auto iter = | 398 auto iter = |
| 398 cache_name_to_cache_dir_.find(cache_handle->value()->cache_name()); | 399 cache_name_to_cache_dir_.find(cache_handle->value()->cache_name()); |
| 399 doomed_cache_to_path_[cache_handle->value()] = iter->second; | 400 doomed_cache_to_path_[cache_handle->value()] = iter->second; |
| 400 cache_name_to_cache_dir_.erase(iter); | 401 cache_name_to_cache_dir_.erase(iter); |
| 401 }; | 402 }; |
| 402 | 403 |
| 403 private: | 404 private: |
| 404 friend class MigratedLegacyCacheDirectoryNameTest; | 405 friend class MigratedLegacyCacheDirectoryNameTest; |
| 405 ~SimpleCacheLoader() override {} | 406 ~SimpleCacheLoader() override {} |
| 406 | 407 |
| 407 // Iterates over the caches and deletes any directory not found in | 408 // Iterates over the caches and deletes any directory not found in |
| 408 // |cache_dirs|. Runs on cache_task_runner_ | 409 // |cache_dirs|. Runs on cache_task_runner_ |
| 409 static void DeleteUnreferencedCachesInPool( | 410 static void DeleteUnreferencedCachesInPool( |
| 410 const base::FilePath& cache_base_dir, | 411 const base::FilePath& cache_base_dir, |
| 411 std::unique_ptr<std::set<std::string>> cache_dirs) { | 412 std::unique_ptr<std::set<std::string>> cache_dirs) { |
| 412 base::FileEnumerator file_enum(cache_base_dir, false /* recursive */, | 413 base::FileEnumerator file_enum(cache_base_dir, false /* recursive */, |
| 413 base::FileEnumerator::DIRECTORIES); | 414 base::FileEnumerator::DIRECTORIES); |
| 414 std::vector<base::FilePath> dirs_to_delete; | 415 std::vector<base::FilePath> dirs_to_delete; |
| 415 base::FilePath cache_path; | 416 base::FilePath cache_path; |
| 416 while (!(cache_path = file_enum.Next()).empty()) { | 417 while (!(cache_path = file_enum.Next()).empty()) { |
| 417 if (!ContainsKey(*cache_dirs, cache_path.BaseName().AsUTF8Unsafe())) | 418 if (!base::ContainsKey(*cache_dirs, cache_path.BaseName().AsUTF8Unsafe())) |
| 418 dirs_to_delete.push_back(cache_path); | 419 dirs_to_delete.push_back(cache_path); |
| 419 } | 420 } |
| 420 | 421 |
| 421 for (const base::FilePath& cache_path : dirs_to_delete) | 422 for (const base::FilePath& cache_path : dirs_to_delete) |
| 422 base::DeleteFile(cache_path, true /* recursive */); | 423 base::DeleteFile(cache_path, true /* recursive */); |
| 423 } | 424 } |
| 424 | 425 |
| 425 // Runs on cache_task_runner_ | 426 // Runs on cache_task_runner_ |
| 426 static std::string MigrateCachesIfNecessaryInPool( | 427 static std::string MigrateCachesIfNecessaryInPool( |
| 427 const std::string& body, | 428 const std::string& body, |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 750 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 751 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 751 DCHECK(cache_handle); | 752 DCHECK(cache_handle); |
| 752 | 753 |
| 753 // TODO(jkarlin): Handle !success. | 754 // TODO(jkarlin): Handle !success. |
| 754 | 755 |
| 755 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK); | 756 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK); |
| 756 } | 757 } |
| 757 | 758 |
| 758 void CacheStorage::HasCacheImpl(const std::string& cache_name, | 759 void CacheStorage::HasCacheImpl(const std::string& cache_name, |
| 759 const BoolAndErrorCallback& callback) { | 760 const BoolAndErrorCallback& callback) { |
| 760 bool has_cache = ContainsKey(cache_map_, cache_name); | 761 bool has_cache = base::ContainsKey(cache_map_, cache_name); |
| 761 callback.Run(has_cache, CACHE_STORAGE_OK); | 762 callback.Run(has_cache, CACHE_STORAGE_OK); |
| 762 } | 763 } |
| 763 | 764 |
| 764 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, | 765 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, |
| 765 const BoolAndErrorCallback& callback) { | 766 const BoolAndErrorCallback& callback) { |
| 766 if (!GetLoadedCache(cache_name)) { | 767 if (!GetLoadedCache(cache_name)) { |
| 767 base::ThreadTaskRunnerHandle::Get()->PostTask( | 768 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 768 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); | 769 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); |
| 769 return; | 770 return; |
| 770 } | 771 } |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1029 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1029 GetLoadedCache(cache_name); | 1030 GetLoadedCache(cache_name); |
| 1030 CacheStorageCache* cache = cache_handle->value(); | 1031 CacheStorageCache* cache = cache_handle->value(); |
| 1031 cache->Size(base::Bind(&SizeRetrievedFromCache, | 1032 cache->Size(base::Bind(&SizeRetrievedFromCache, |
| 1032 base::Passed(std::move(cache_handle)), | 1033 base::Passed(std::move(cache_handle)), |
| 1033 barrier_closure, accumulator_ptr)); | 1034 barrier_closure, accumulator_ptr)); |
| 1034 } | 1035 } |
| 1035 } | 1036 } |
| 1036 | 1037 |
| 1037 } // namespace content | 1038 } // namespace content |
| OLD | NEW |