| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 namespace { | 43 namespace { |
| 44 | 44 |
| 45 std::string HexedHash(const std::string& value) { | 45 std::string HexedHash(const std::string& value) { |
| 46 std::string value_hash = base::SHA1HashString(value); | 46 std::string value_hash = base::SHA1HashString(value); |
| 47 std::string valued_hexed_hash = base::ToLowerASCII( | 47 std::string valued_hexed_hash = base::ToLowerASCII( |
| 48 base::HexEncode(value_hash.c_str(), value_hash.length())); | 48 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 49 return valued_hexed_hash; | 49 return valued_hexed_hash; |
| 50 } | 50 } |
| 51 | 51 |
| 52 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, | 52 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, |
| 53 const CacheStorage::SizeCallback& callback) { | 53 CacheStorage::SizeCallback callback) { |
| 54 base::ThreadTaskRunnerHandle::Get()->PostTask( | 54 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 55 FROM_HERE, base::Bind(callback, *accumulator)); | 55 FROM_HERE, base::BindOnce(std::move(callback), *accumulator)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void DoNothingWithBool(bool success) {} | 58 void DoNothingWithBool(bool success) {} |
| 59 | 59 |
| 60 } // namespace | 60 } // namespace |
| 61 | 61 |
| 62 const char CacheStorage::kIndexFileName[] = "index.txt"; | 62 const char CacheStorage::kIndexFileName[] = "index.txt"; |
| 63 constexpr int64_t CacheStorage::kSizeUnknown; | 63 constexpr int64_t CacheStorage::kSizeUnknown; |
| 64 | 64 |
| 65 struct CacheStorage::CacheMatchResponse { | 65 struct CacheStorage::CacheMatchResponse { |
| 66 CacheMatchResponse() = default; | 66 CacheMatchResponse() = default; |
| 67 ~CacheMatchResponse() = default; | 67 ~CacheMatchResponse() = default; |
| 68 | 68 |
| 69 CacheStorageError error; | 69 CacheStorageError error; |
| 70 std::unique_ptr<ServiceWorkerResponse> service_worker_response; | 70 std::unique_ptr<ServiceWorkerResponse> service_worker_response; |
| 71 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; | 71 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 // Handles the loading and clean up of CacheStorageCache objects. | 74 // Handles the loading and clean up of CacheStorageCache objects. |
| 75 class CacheStorage::CacheLoader { | 75 class CacheStorage::CacheLoader { |
| 76 public: | 76 public: |
| 77 typedef base::Callback<void(std::unique_ptr<CacheStorageCache>)> | 77 using CacheCallback = |
| 78 CacheCallback; | 78 base::OnceCallback<void(std::unique_ptr<CacheStorageCache>)>; |
| 79 typedef base::Callback<void(bool)> BoolCallback; | 79 using BoolCallback = base::OnceCallback<void(bool)>; |
| 80 using CacheStorageIndexLoadCallback = | 80 using CacheStorageIndexLoadCallback = |
| 81 base::Callback<void(std::unique_ptr<CacheStorageIndex>)>; | 81 base::OnceCallback<void(std::unique_ptr<CacheStorageIndex>)>; |
| 82 | 82 |
| 83 CacheLoader( | 83 CacheLoader( |
| 84 base::SequencedTaskRunner* cache_task_runner, | 84 base::SequencedTaskRunner* cache_task_runner, |
| 85 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 85 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 86 storage::QuotaManagerProxy* quota_manager_proxy, | 86 storage::QuotaManagerProxy* quota_manager_proxy, |
| 87 base::WeakPtr<storage::BlobStorageContext> blob_context, | 87 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 88 CacheStorage* cache_storage, | 88 CacheStorage* cache_storage, |
| 89 const GURL& origin) | 89 const GURL& origin) |
| 90 : cache_task_runner_(cache_task_runner), | 90 : cache_task_runner_(cache_task_runner), |
| 91 request_context_getter_(request_context_getter), | 91 request_context_getter_(request_context_getter), |
| 92 quota_manager_proxy_(quota_manager_proxy), | 92 quota_manager_proxy_(quota_manager_proxy), |
| 93 blob_context_(blob_context), | 93 blob_context_(blob_context), |
| 94 cache_storage_(cache_storage), | 94 cache_storage_(cache_storage), |
| 95 origin_(origin) { | 95 origin_(origin) { |
| 96 DCHECK(!origin_.is_empty()); | 96 DCHECK(!origin_.is_empty()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 virtual ~CacheLoader() {} | 99 virtual ~CacheLoader() {} |
| 100 | 100 |
| 101 // Creates a CacheStorageCache with the given name. It does not attempt to | 101 // Creates a CacheStorageCache with the given name. It does not attempt to |
| 102 // load the backend, that happens lazily when the cache is used. | 102 // load the backend, that happens lazily when the cache is used. |
| 103 virtual std::unique_ptr<CacheStorageCache> CreateCache( | 103 virtual std::unique_ptr<CacheStorageCache> CreateCache( |
| 104 const std::string& cache_name, | 104 const std::string& cache_name, |
| 105 int64_t cache_size) = 0; | 105 int64_t cache_size) = 0; |
| 106 | 106 |
| 107 // Deletes any pre-existing cache of the same name and then loads it. | 107 // Deletes any pre-existing cache of the same name and then loads it. |
| 108 virtual void PrepareNewCacheDestination(const std::string& cache_name, | 108 virtual void PrepareNewCacheDestination(const std::string& cache_name, |
| 109 const CacheCallback& callback) = 0; | 109 CacheCallback callback) = 0; |
| 110 | 110 |
| 111 // After the backend has been deleted, do any extra house keeping such as | 111 // After the backend has been deleted, do any extra house keeping such as |
| 112 // removing the cache's directory. | 112 // removing the cache's directory. |
| 113 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; | 113 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; |
| 114 | 114 |
| 115 // Writes the cache index to disk if applicable. | 115 // Writes the cache index to disk if applicable. |
| 116 virtual void WriteIndex(const CacheStorageIndex& index, | 116 virtual void WriteIndex(const CacheStorageIndex& index, |
| 117 const BoolCallback& callback) = 0; | 117 BoolCallback callback) = 0; |
| 118 | 118 |
| 119 // Loads the cache index from disk if applicable. | 119 // Loads the cache index from disk if applicable. |
| 120 virtual void LoadIndex(const CacheStorageIndexLoadCallback& callback) = 0; | 120 virtual void LoadIndex(CacheStorageIndexLoadCallback callback) = 0; |
| 121 | 121 |
| 122 // Called when CacheStorage has created a cache. Used to hold onto a handle to | 122 // Called when CacheStorage has created a cache. Used to hold onto a handle to |
| 123 // the cache if necessary. | 123 // the cache if necessary. |
| 124 virtual void NotifyCacheCreated( | 124 virtual void NotifyCacheCreated( |
| 125 const std::string& cache_name, | 125 const std::string& cache_name, |
| 126 std::unique_ptr<CacheStorageCacheHandle> cache_handle) {} | 126 std::unique_ptr<CacheStorageCacheHandle> cache_handle) {} |
| 127 | 127 |
| 128 // Notification that the cache for |cache_handle| has been doomed. If the | 128 // Notification that the cache for |cache_handle| has been doomed. If the |
| 129 // loader is holding a handle to the cache, it should drop it now. | 129 // loader is holding a handle to the cache, it should drop it now. |
| 130 virtual void NotifyCacheDoomed( | 130 virtual void NotifyCacheDoomed( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 origin) {} | 165 origin) {} |
| 166 | 166 |
| 167 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, | 167 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, |
| 168 int64_t cache_size) override { | 168 int64_t cache_size) override { |
| 169 return CacheStorageCache::CreateMemoryCache( | 169 return CacheStorageCache::CreateMemoryCache( |
| 170 origin_, cache_name, cache_storage_, request_context_getter_, | 170 origin_, cache_name, cache_storage_, request_context_getter_, |
| 171 quota_manager_proxy_, blob_context_); | 171 quota_manager_proxy_, blob_context_); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void PrepareNewCacheDestination(const std::string& cache_name, | 174 void PrepareNewCacheDestination(const std::string& cache_name, |
| 175 const CacheCallback& callback) override { | 175 CacheCallback callback) override { |
| 176 std::unique_ptr<CacheStorageCache> cache = | 176 std::unique_ptr<CacheStorageCache> cache = |
| 177 CreateCache(cache_name, 0 /*cache_size*/); | 177 CreateCache(cache_name, 0 /*cache_size*/); |
| 178 callback.Run(std::move(cache)); | 178 std::move(callback).Run(std::move(cache)); |
| 179 } | 179 } |
| 180 | 180 |
| 181 void CleanUpDeletedCache(CacheStorageCache* cache) override {} | 181 void CleanUpDeletedCache(CacheStorageCache* cache) override {} |
| 182 | 182 |
| 183 void WriteIndex(const CacheStorageIndex& index, | 183 void WriteIndex(const CacheStorageIndex& index, |
| 184 const BoolCallback& callback) override { | 184 BoolCallback callback) override { |
| 185 callback.Run(true); | 185 std::move(callback).Run(true); |
| 186 } | 186 } |
| 187 | 187 |
| 188 void LoadIndex(const CacheStorageIndexLoadCallback& callback) override { | 188 void LoadIndex(CacheStorageIndexLoadCallback callback) override { |
| 189 callback.Run(base::MakeUnique<CacheStorageIndex>()); | 189 std::move(callback).Run(base::MakeUnique<CacheStorageIndex>()); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void NotifyCacheCreated( | 192 void NotifyCacheCreated( |
| 193 const std::string& cache_name, | 193 const std::string& cache_name, |
| 194 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 194 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 195 DCHECK(!base::ContainsKey(cache_handles_, cache_name)); | 195 DCHECK(!base::ContainsKey(cache_handles_, cache_name)); |
| 196 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); | 196 cache_handles_.insert(std::make_pair(cache_name, std::move(cache_handle))); |
| 197 }; | 197 }; |
| 198 | 198 |
| 199 void NotifyCacheDoomed( | 199 void NotifyCacheDoomed( |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 | 239 |
| 240 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; | 240 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 241 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); | 241 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 242 return CacheStorageCache::CreatePersistentCache( | 242 return CacheStorageCache::CreatePersistentCache( |
| 243 origin_, cache_name, cache_storage_, cache_path, | 243 origin_, cache_name, cache_storage_, cache_path, |
| 244 request_context_getter_, quota_manager_proxy_, blob_context_, | 244 request_context_getter_, quota_manager_proxy_, blob_context_, |
| 245 cache_size); | 245 cache_size); |
| 246 } | 246 } |
| 247 | 247 |
| 248 void PrepareNewCacheDestination(const std::string& cache_name, | 248 void PrepareNewCacheDestination(const std::string& cache_name, |
| 249 const CacheCallback& callback) override { | 249 CacheCallback callback) override { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 251 | 251 |
| 252 PostTaskAndReplyWithResult( | 252 PostTaskAndReplyWithResult( |
| 253 cache_task_runner_.get(), FROM_HERE, | 253 cache_task_runner_.get(), FROM_HERE, |
| 254 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, | 254 base::BindOnce(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 255 origin_path_), | 255 origin_path_), |
| 256 base::Bind(&SimpleCacheLoader::PrepareNewCacheCreateCache, | 256 base::BindOnce(&SimpleCacheLoader::PrepareNewCacheCreateCache, |
| 257 weak_ptr_factory_.GetWeakPtr(), cache_name, callback)); | 257 weak_ptr_factory_.GetWeakPtr(), cache_name, |
| 258 std::move(callback))); |
| 258 } | 259 } |
| 259 | 260 |
| 260 // Runs on the cache_task_runner_. | 261 // Runs on the cache_task_runner_. |
| 261 static std::string PrepareNewCacheDirectoryInPool( | 262 static std::string PrepareNewCacheDirectoryInPool( |
| 262 const base::FilePath& origin_path) { | 263 const base::FilePath& origin_path) { |
| 263 std::string cache_dir; | 264 std::string cache_dir; |
| 264 base::FilePath cache_path; | 265 base::FilePath cache_path; |
| 265 do { | 266 do { |
| 266 cache_dir = base::GenerateGUID(); | 267 cache_dir = base::GenerateGUID(); |
| 267 cache_path = origin_path.AppendASCII(cache_dir); | 268 cache_path = origin_path.AppendASCII(cache_dir); |
| 268 } while (base::PathExists(cache_path)); | 269 } while (base::PathExists(cache_path)); |
| 269 | 270 |
| 270 return base::CreateDirectory(cache_path) ? cache_dir : ""; | 271 return base::CreateDirectory(cache_path) ? cache_dir : ""; |
| 271 } | 272 } |
| 272 | 273 |
| 273 void PrepareNewCacheCreateCache(const std::string& cache_name, | 274 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 274 const CacheCallback& callback, | 275 CacheCallback callback, |
| 275 const std::string& cache_dir) { | 276 const std::string& cache_dir) { |
| 276 if (cache_dir.empty()) { | 277 if (cache_dir.empty()) { |
| 277 callback.Run(std::unique_ptr<CacheStorageCache>()); | 278 std::move(callback).Run(std::unique_ptr<CacheStorageCache>()); |
| 278 return; | 279 return; |
| 279 } | 280 } |
| 280 | 281 |
| 281 cache_name_to_cache_dir_[cache_name] = cache_dir; | 282 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 282 callback.Run(CreateCache(cache_name, CacheStorage::kSizeUnknown)); | 283 std::move(callback).Run( |
| 284 CreateCache(cache_name, CacheStorage::kSizeUnknown)); |
| 283 } | 285 } |
| 284 | 286 |
| 285 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 287 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 288 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 287 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); | 289 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 288 | 290 |
| 289 base::FilePath cache_path = | 291 base::FilePath cache_path = |
| 290 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 292 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 291 doomed_cache_to_path_.erase(cache); | 293 doomed_cache_to_path_.erase(cache); |
| 292 | 294 |
| 293 cache_task_runner_->PostTask( | 295 cache_task_runner_->PostTask( |
| 294 FROM_HERE, base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, | 296 FROM_HERE, |
| 295 cache_path)); | 297 base::BindOnce(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, |
| 298 cache_path)); |
| 296 } | 299 } |
| 297 | 300 |
| 298 static void CleanUpDeleteCacheDirInPool(const base::FilePath& cache_path) { | 301 static void CleanUpDeleteCacheDirInPool(const base::FilePath& cache_path) { |
| 299 base::DeleteFile(cache_path, true /* recursive */); | 302 base::DeleteFile(cache_path, true /* recursive */); |
| 300 } | 303 } |
| 301 | 304 |
| 302 void WriteIndex(const CacheStorageIndex& index, | 305 void WriteIndex(const CacheStorageIndex& index, |
| 303 const BoolCallback& callback) override { | 306 BoolCallback callback) override { |
| 304 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 307 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 305 | 308 |
| 306 // 1. Create the index file as a string. (WriteIndex) | 309 // 1. Create the index file as a string. (WriteIndex) |
| 307 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 310 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
| 308 | 311 |
| 309 proto::CacheStorageIndex protobuf_index; | 312 proto::CacheStorageIndex protobuf_index; |
| 310 protobuf_index.set_origin(origin_.spec()); | 313 protobuf_index.set_origin(origin_.spec()); |
| 311 | 314 |
| 312 for (const auto& cache_metadata : index.ordered_cache_metadata()) { | 315 for (const auto& cache_metadata : index.ordered_cache_metadata()) { |
| 313 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); | 316 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 324 std::string serialized; | 327 std::string serialized; |
| 325 bool success = protobuf_index.SerializeToString(&serialized); | 328 bool success = protobuf_index.SerializeToString(&serialized); |
| 326 DCHECK(success); | 329 DCHECK(success); |
| 327 | 330 |
| 328 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 331 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
| 329 base::FilePath index_path = | 332 base::FilePath index_path = |
| 330 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 333 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
| 331 | 334 |
| 332 PostTaskAndReplyWithResult( | 335 PostTaskAndReplyWithResult( |
| 333 cache_task_runner_.get(), FROM_HERE, | 336 cache_task_runner_.get(), FROM_HERE, |
| 334 base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, tmp_path, | 337 base::BindOnce(&SimpleCacheLoader::WriteIndexWriteToFileInPool, |
| 335 index_path, serialized), | 338 tmp_path, index_path, serialized), |
| 336 callback); | 339 std::move(callback)); |
| 337 } | 340 } |
| 338 | 341 |
| 339 static bool WriteIndexWriteToFileInPool(const base::FilePath& tmp_path, | 342 static bool WriteIndexWriteToFileInPool(const base::FilePath& tmp_path, |
| 340 const base::FilePath& index_path, | 343 const base::FilePath& index_path, |
| 341 const std::string& data) { | 344 const std::string& data) { |
| 342 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); | 345 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); |
| 343 if (bytes_written != base::checked_cast<int>(data.size())) { | 346 if (bytes_written != base::checked_cast<int>(data.size())) { |
| 344 base::DeleteFile(tmp_path, /* recursive */ false); | 347 base::DeleteFile(tmp_path, /* recursive */ false); |
| 345 return false; | 348 return false; |
| 346 } | 349 } |
| 347 | 350 |
| 348 // Atomically rename the temporary index file to become the real one. | 351 // Atomically rename the temporary index file to become the real one. |
| 349 return base::ReplaceFile(tmp_path, index_path, NULL); | 352 return base::ReplaceFile(tmp_path, index_path, NULL); |
| 350 } | 353 } |
| 351 | 354 |
| 352 void LoadIndex(const CacheStorageIndexLoadCallback& callback) override { | 355 void LoadIndex(CacheStorageIndexLoadCallback callback) override { |
| 353 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 356 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 354 | 357 |
| 355 PostTaskAndReplyWithResult( | 358 PostTaskAndReplyWithResult( |
| 356 cache_task_runner_.get(), FROM_HERE, | 359 cache_task_runner_.get(), FROM_HERE, |
| 357 base::Bind(&SimpleCacheLoader::ReadAndMigrateIndexInPool, origin_path_), | 360 base::BindOnce(&SimpleCacheLoader::ReadAndMigrateIndexInPool, |
| 358 base::Bind(&SimpleCacheLoader::LoadIndexDidReadIndex, | 361 origin_path_), |
| 359 weak_ptr_factory_.GetWeakPtr(), callback)); | 362 base::BindOnce(&SimpleCacheLoader::LoadIndexDidReadIndex, |
| 363 weak_ptr_factory_.GetWeakPtr(), std::move(callback))); |
| 360 } | 364 } |
| 361 | 365 |
| 362 void LoadIndexDidReadIndex(const CacheStorageIndexLoadCallback& callback, | 366 void LoadIndexDidReadIndex(CacheStorageIndexLoadCallback callback, |
| 363 proto::CacheStorageIndex protobuf_index) { | 367 proto::CacheStorageIndex protobuf_index) { |
| 364 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 368 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 365 | 369 |
| 366 std::unique_ptr<std::set<std::string>> cache_dirs( | 370 std::unique_ptr<std::set<std::string>> cache_dirs( |
| 367 new std::set<std::string>); | 371 new std::set<std::string>); |
| 368 | 372 |
| 369 auto index = base::MakeUnique<CacheStorageIndex>(); | 373 auto index = base::MakeUnique<CacheStorageIndex>(); |
| 370 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { | 374 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { |
| 371 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); | 375 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); |
| 372 DCHECK(cache.has_cache_dir()); | 376 DCHECK(cache.has_cache_dir()); |
| 373 int64_t cache_size = | 377 int64_t cache_size = |
| 374 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; | 378 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; |
| 375 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); | 379 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); |
| 376 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 380 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 377 cache_dirs->insert(cache.cache_dir()); | 381 cache_dirs->insert(cache.cache_dir()); |
| 378 } | 382 } |
| 379 | 383 |
| 380 cache_task_runner_->PostTask( | 384 cache_task_runner_->PostTask( |
| 381 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | 385 FROM_HERE, base::BindOnce(&DeleteUnreferencedCachesInPool, origin_path_, |
| 382 base::Passed(&cache_dirs))); | 386 base::Passed(&cache_dirs))); |
| 383 callback.Run(std::move(index)); | 387 std::move(callback).Run(std::move(index)); |
| 384 } | 388 } |
| 385 | 389 |
| 386 void NotifyCacheDoomed( | 390 void NotifyCacheDoomed( |
| 387 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { | 391 std::unique_ptr<CacheStorageCacheHandle> cache_handle) override { |
| 388 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, | 392 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, |
| 389 cache_handle->value()->cache_name())); | 393 cache_handle->value()->cache_name())); |
| 390 auto iter = | 394 auto iter = |
| 391 cache_name_to_cache_dir_.find(cache_handle->value()->cache_name()); | 395 cache_name_to_cache_dir_.find(cache_handle->value()->cache_name()); |
| 392 doomed_cache_to_path_[cache_handle->value()] = iter->second; | 396 doomed_cache_to_path_[cache_handle->value()] = iter->second; |
| 393 cache_name_to_cache_dir_.erase(iter); | 397 cache_name_to_cache_dir_.erase(iter); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 else | 521 else |
| 518 cache_loader_.reset(new SimpleCacheLoader( | 522 cache_loader_.reset(new SimpleCacheLoader( |
| 519 origin_path_, cache_task_runner_.get(), std::move(request_context), | 523 origin_path_, cache_task_runner_.get(), std::move(request_context), |
| 520 quota_manager_proxy.get(), blob_context, this, origin)); | 524 quota_manager_proxy.get(), blob_context, this, origin)); |
| 521 } | 525 } |
| 522 | 526 |
| 523 CacheStorage::~CacheStorage() { | 527 CacheStorage::~CacheStorage() { |
| 524 } | 528 } |
| 525 | 529 |
| 526 void CacheStorage::OpenCache(const std::string& cache_name, | 530 void CacheStorage::OpenCache(const std::string& cache_name, |
| 527 const CacheAndErrorCallback& callback) { | 531 CacheAndErrorCallback callback) { |
| 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 532 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 529 | 533 |
| 530 if (!initialized_) | 534 if (!initialized_) |
| 531 LazyInit(); | 535 LazyInit(); |
| 532 | 536 |
| 533 quota_manager_proxy_->NotifyStorageAccessed( | 537 quota_manager_proxy_->NotifyStorageAccessed( |
| 534 storage::QuotaClient::kServiceWorkerCache, origin_, | 538 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 535 storage::kStorageTypeTemporary); | 539 storage::kStorageTypeTemporary); |
| 536 | 540 |
| 537 scheduler_->ScheduleOperation( | 541 scheduler_->ScheduleOperation(base::BindOnce( |
| 538 base::Bind(&CacheStorage::OpenCacheImpl, weak_factory_.GetWeakPtr(), | 542 &CacheStorage::OpenCacheImpl, weak_factory_.GetWeakPtr(), cache_name, |
| 539 cache_name, scheduler_->WrapCallbackToRunNext(callback))); | 543 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 540 } | 544 } |
| 541 | 545 |
| 542 void CacheStorage::HasCache(const std::string& cache_name, | 546 void CacheStorage::HasCache(const std::string& cache_name, |
| 543 const BoolAndErrorCallback& callback) { | 547 BoolAndErrorCallback callback) { |
| 544 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 545 | 549 |
| 546 if (!initialized_) | 550 if (!initialized_) |
| 547 LazyInit(); | 551 LazyInit(); |
| 548 | 552 |
| 549 quota_manager_proxy_->NotifyStorageAccessed( | 553 quota_manager_proxy_->NotifyStorageAccessed( |
| 550 storage::QuotaClient::kServiceWorkerCache, origin_, | 554 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 551 storage::kStorageTypeTemporary); | 555 storage::kStorageTypeTemporary); |
| 552 | 556 |
| 553 scheduler_->ScheduleOperation( | 557 scheduler_->ScheduleOperation(base::BindOnce( |
| 554 base::Bind(&CacheStorage::HasCacheImpl, weak_factory_.GetWeakPtr(), | 558 &CacheStorage::HasCacheImpl, weak_factory_.GetWeakPtr(), cache_name, |
| 555 cache_name, scheduler_->WrapCallbackToRunNext(callback))); | 559 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 556 } | 560 } |
| 557 | 561 |
| 558 void CacheStorage::DeleteCache(const std::string& cache_name, | 562 void CacheStorage::DeleteCache(const std::string& cache_name, |
| 559 const BoolAndErrorCallback& callback) { | 563 BoolAndErrorCallback callback) { |
| 560 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 564 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 561 | 565 |
| 562 if (!initialized_) | 566 if (!initialized_) |
| 563 LazyInit(); | 567 LazyInit(); |
| 564 | 568 |
| 565 quota_manager_proxy_->NotifyStorageAccessed( | 569 quota_manager_proxy_->NotifyStorageAccessed( |
| 566 storage::QuotaClient::kServiceWorkerCache, origin_, | 570 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 567 storage::kStorageTypeTemporary); | 571 storage::kStorageTypeTemporary); |
| 568 | 572 |
| 569 scheduler_->ScheduleOperation( | 573 scheduler_->ScheduleOperation(base::BindOnce( |
| 570 base::Bind(&CacheStorage::DeleteCacheImpl, weak_factory_.GetWeakPtr(), | 574 &CacheStorage::DeleteCacheImpl, weak_factory_.GetWeakPtr(), cache_name, |
| 571 cache_name, scheduler_->WrapCallbackToRunNext(callback))); | 575 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 572 } | 576 } |
| 573 | 577 |
| 574 void CacheStorage::EnumerateCaches(const IndexCallback& callback) { | 578 void CacheStorage::EnumerateCaches(IndexCallback callback) { |
| 575 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 579 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 576 | 580 |
| 577 if (!initialized_) | 581 if (!initialized_) |
| 578 LazyInit(); | 582 LazyInit(); |
| 579 | 583 |
| 580 quota_manager_proxy_->NotifyStorageAccessed( | 584 quota_manager_proxy_->NotifyStorageAccessed( |
| 581 storage::QuotaClient::kServiceWorkerCache, origin_, | 585 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 582 storage::kStorageTypeTemporary); | 586 storage::kStorageTypeTemporary); |
| 583 | 587 |
| 584 scheduler_->ScheduleOperation( | 588 scheduler_->ScheduleOperation(base::BindOnce( |
| 585 base::Bind(&CacheStorage::EnumerateCachesImpl, weak_factory_.GetWeakPtr(), | 589 &CacheStorage::EnumerateCachesImpl, weak_factory_.GetWeakPtr(), |
| 586 scheduler_->WrapCallbackToRunNext(callback))); | 590 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 587 } | 591 } |
| 588 | 592 |
| 589 void CacheStorage::MatchCache( | 593 void CacheStorage::MatchCache( |
| 590 const std::string& cache_name, | 594 const std::string& cache_name, |
| 591 std::unique_ptr<ServiceWorkerFetchRequest> request, | 595 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 592 const CacheStorageCacheQueryParams& match_params, | 596 const CacheStorageCacheQueryParams& match_params, |
| 593 const CacheStorageCache::ResponseCallback& callback) { | 597 CacheStorageCache::ResponseCallback callback) { |
| 594 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 598 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 595 | 599 |
| 596 if (!initialized_) | 600 if (!initialized_) |
| 597 LazyInit(); | 601 LazyInit(); |
| 598 | 602 |
| 599 quota_manager_proxy_->NotifyStorageAccessed( | 603 quota_manager_proxy_->NotifyStorageAccessed( |
| 600 storage::QuotaClient::kServiceWorkerCache, origin_, | 604 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 601 storage::kStorageTypeTemporary); | 605 storage::kStorageTypeTemporary); |
| 602 | 606 |
| 603 scheduler_->ScheduleOperation( | 607 scheduler_->ScheduleOperation( |
| 604 base::Bind(&CacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(), | 608 base::BindOnce(&CacheStorage::MatchCacheImpl, weak_factory_.GetWeakPtr(), |
| 605 cache_name, base::Passed(std::move(request)), match_params, | 609 cache_name, base::Passed(std::move(request)), match_params, |
| 606 scheduler_->WrapCallbackToRunNext(callback))); | 610 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 607 } | 611 } |
| 608 | 612 |
| 609 void CacheStorage::MatchAllCaches( | 613 void CacheStorage::MatchAllCaches( |
| 610 std::unique_ptr<ServiceWorkerFetchRequest> request, | 614 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 611 const CacheStorageCacheQueryParams& match_params, | 615 const CacheStorageCacheQueryParams& match_params, |
| 612 const CacheStorageCache::ResponseCallback& callback) { | 616 CacheStorageCache::ResponseCallback callback) { |
| 613 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 617 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 614 | 618 |
| 615 if (!initialized_) | 619 if (!initialized_) |
| 616 LazyInit(); | 620 LazyInit(); |
| 617 | 621 |
| 618 quota_manager_proxy_->NotifyStorageAccessed( | 622 quota_manager_proxy_->NotifyStorageAccessed( |
| 619 storage::QuotaClient::kServiceWorkerCache, origin_, | 623 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 620 storage::kStorageTypeTemporary); | 624 storage::kStorageTypeTemporary); |
| 621 | 625 |
| 622 scheduler_->ScheduleOperation( | 626 scheduler_->ScheduleOperation(base::BindOnce( |
| 623 base::Bind(&CacheStorage::MatchAllCachesImpl, weak_factory_.GetWeakPtr(), | 627 &CacheStorage::MatchAllCachesImpl, weak_factory_.GetWeakPtr(), |
| 624 base::Passed(std::move(request)), match_params, | 628 base::Passed(std::move(request)), match_params, |
| 625 scheduler_->WrapCallbackToRunNext(callback))); | 629 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 626 } | 630 } |
| 627 | 631 |
| 628 void CacheStorage::GetSizeThenCloseAllCaches(const SizeCallback& callback) { | 632 void CacheStorage::GetSizeThenCloseAllCaches(SizeCallback callback) { |
| 629 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 633 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 630 | 634 |
| 631 if (!initialized_) | 635 if (!initialized_) |
| 632 LazyInit(); | 636 LazyInit(); |
| 633 | 637 |
| 634 scheduler_->ScheduleOperation(base::Bind( | 638 scheduler_->ScheduleOperation(base::BindOnce( |
| 635 &CacheStorage::GetSizeThenCloseAllCachesImpl, weak_factory_.GetWeakPtr(), | 639 &CacheStorage::GetSizeThenCloseAllCachesImpl, weak_factory_.GetWeakPtr(), |
| 636 scheduler_->WrapCallbackToRunNext(callback))); | 640 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 637 } | 641 } |
| 638 | 642 |
| 639 void CacheStorage::Size(const CacheStorage::SizeCallback& callback) { | 643 void CacheStorage::Size(CacheStorage::SizeCallback callback) { |
| 640 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 644 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 641 | 645 |
| 642 if (!initialized_) | 646 if (!initialized_) |
| 643 LazyInit(); | 647 LazyInit(); |
| 644 | 648 |
| 645 scheduler_->ScheduleOperation( | 649 scheduler_->ScheduleOperation( |
| 646 base::Bind(&CacheStorage::SizeImpl, weak_factory_.GetWeakPtr(), | 650 base::BindOnce(&CacheStorage::SizeImpl, weak_factory_.GetWeakPtr(), |
| 647 scheduler_->WrapCallbackToRunNext(callback))); | 651 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 648 } | 652 } |
| 649 | 653 |
| 650 void CacheStorage::ScheduleWriteIndex() { | 654 void CacheStorage::ScheduleWriteIndex() { |
| 651 static const int64_t kWriteIndexDelaySecs = 5; | 655 static const int64_t kWriteIndexDelaySecs = 5; |
| 652 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 656 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 653 index_write_task_.Reset(base::Bind(&CacheStorage::WriteIndex, | 657 index_write_task_.Reset(base::Bind(&CacheStorage::WriteIndex, |
| 654 weak_factory_.GetWeakPtr(), | 658 weak_factory_.GetWeakPtr(), |
| 655 base::Bind(&DoNothingWithBool))); | 659 base::Bind(&DoNothingWithBool))); |
| 656 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 660 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 657 FROM_HERE, index_write_task_.callback(), | 661 FROM_HERE, index_write_task_.callback(), |
| 658 base::TimeDelta::FromSeconds(kWriteIndexDelaySecs)); | 662 base::TimeDelta::FromSeconds(kWriteIndexDelaySecs)); |
| 659 } | 663 } |
| 660 | 664 |
| 661 void CacheStorage::WriteIndex(const base::Callback<void(bool)>& callback) { | 665 void CacheStorage::WriteIndex(base::OnceCallback<void(bool)> callback) { |
| 662 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 666 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 663 scheduler_->ScheduleOperation( | 667 scheduler_->ScheduleOperation( |
| 664 base::Bind(&CacheStorage::WriteIndexImpl, weak_factory_.GetWeakPtr(), | 668 base::BindOnce(&CacheStorage::WriteIndexImpl, weak_factory_.GetWeakPtr(), |
| 665 scheduler_->WrapCallbackToRunNext(callback))); | 669 scheduler_->WrapCallbackToRunNext(std::move(callback)))); |
| 666 } | 670 } |
| 667 | 671 |
| 668 void CacheStorage::WriteIndexImpl(const base::Callback<void(bool)>& callback) { | 672 void CacheStorage::WriteIndexImpl(base::OnceCallback<void(bool)> callback) { |
| 669 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 673 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 670 cache_loader_->WriteIndex(*cache_index_, callback); | 674 cache_loader_->WriteIndex(*cache_index_, std::move(callback)); |
| 671 } | 675 } |
| 672 | 676 |
| 673 bool CacheStorage::InitiateScheduledIndexWriteForTest( | 677 bool CacheStorage::InitiateScheduledIndexWriteForTest( |
| 674 const base::Callback<void(bool)>& callback) { | 678 base::OnceCallback<void(bool)> callback) { |
| 675 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 679 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 676 if (index_write_pending()) { | 680 if (index_write_pending()) { |
| 677 index_write_task_.Cancel(); | 681 index_write_task_.Cancel(); |
| 678 WriteIndex(callback); | 682 WriteIndex(std::move(callback)); |
| 679 return true; | 683 return true; |
| 680 } | 684 } |
| 681 callback.Run(true /* success */); | 685 std::move(callback).Run(true /* success */); |
| 682 return false; | 686 return false; |
| 683 } | 687 } |
| 684 | 688 |
| 685 void CacheStorage::CacheSizeUpdated(const CacheStorageCache* cache, | 689 void CacheStorage::CacheSizeUpdated(const CacheStorageCache* cache, |
| 686 int64_t size) { | 690 int64_t size) { |
| 687 // Should not be called for doomed caches. | 691 // Should not be called for doomed caches. |
| 688 DCHECK(!base::ContainsKey(doomed_caches_, | 692 DCHECK(!base::ContainsKey(doomed_caches_, |
| 689 const_cast<CacheStorageCache*>(cache))); | 693 const_cast<CacheStorageCache*>(cache))); |
| 690 cache_index_->SetCacheSize(cache->cache_name(), size); | 694 cache_index_->SetCacheSize(cache->cache_name(), size); |
| 691 ScheduleWriteIndex(); | 695 ScheduleWriteIndex(); |
| 692 } | 696 } |
| 693 | 697 |
| 694 void CacheStorage::StartAsyncOperationForTesting() { | 698 void CacheStorage::StartAsyncOperationForTesting() { |
| 695 scheduler_->ScheduleOperation(base::Bind(&base::DoNothing)); | 699 scheduler_->ScheduleOperation(base::BindOnce(&base::DoNothing)); |
| 696 } | 700 } |
| 697 | 701 |
| 698 void CacheStorage::CompleteAsyncOperationForTesting() { | 702 void CacheStorage::CompleteAsyncOperationForTesting() { |
| 699 scheduler_->CompleteOperationAndRunNext(); | 703 scheduler_->CompleteOperationAndRunNext(); |
| 700 } | 704 } |
| 701 | 705 |
| 702 // Init is run lazily so that it is called on the proper MessageLoop. | 706 // Init is run lazily so that it is called on the proper MessageLoop. |
| 703 void CacheStorage::LazyInit() { | 707 void CacheStorage::LazyInit() { |
| 704 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 708 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 705 DCHECK(!initialized_); | 709 DCHECK(!initialized_); |
| 706 | 710 |
| 707 if (initializing_) | 711 if (initializing_) |
| 708 return; | 712 return; |
| 709 | 713 |
| 710 DCHECK(!scheduler_->ScheduledOperations()); | 714 DCHECK(!scheduler_->ScheduledOperations()); |
| 711 | 715 |
| 712 initializing_ = true; | 716 initializing_ = true; |
| 713 scheduler_->ScheduleOperation( | 717 scheduler_->ScheduleOperation( |
| 714 base::Bind(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); | 718 base::BindOnce(&CacheStorage::LazyInitImpl, weak_factory_.GetWeakPtr())); |
| 715 } | 719 } |
| 716 | 720 |
| 717 void CacheStorage::LazyInitImpl() { | 721 void CacheStorage::LazyInitImpl() { |
| 718 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 722 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 719 DCHECK(!initialized_); | 723 DCHECK(!initialized_); |
| 720 DCHECK(initializing_); | 724 DCHECK(initializing_); |
| 721 | 725 |
| 722 // 1. Get the cache index (async call) | 726 // 1. Get the cache index (async call) |
| 723 // 2. For each cache name, load the cache (async call) | 727 // 2. For each cache name, load the cache (async call) |
| 724 // 3. Once each load is complete, update the map variables. | 728 // 3. Once each load is complete, update the map variables. |
| 725 // 4. Call the list of waiting callbacks. | 729 // 4. Call the list of waiting callbacks. |
| 726 | 730 |
| 727 cache_loader_->LoadIndex(base::Bind(&CacheStorage::LazyInitDidLoadIndex, | 731 cache_loader_->LoadIndex(base::BindOnce(&CacheStorage::LazyInitDidLoadIndex, |
| 728 weak_factory_.GetWeakPtr())); | 732 weak_factory_.GetWeakPtr())); |
| 729 } | 733 } |
| 730 | 734 |
| 731 void CacheStorage::LazyInitDidLoadIndex( | 735 void CacheStorage::LazyInitDidLoadIndex( |
| 732 std::unique_ptr<CacheStorageIndex> index) { | 736 std::unique_ptr<CacheStorageIndex> index) { |
| 733 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 737 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 734 DCHECK(cache_map_.empty()); | 738 DCHECK(cache_map_.empty()); |
| 735 | 739 |
| 736 for (const auto& cache_metadata : index->ordered_cache_metadata()) { | 740 for (const auto& cache_metadata : index->ordered_cache_metadata()) { |
| 737 cache_map_.insert(std::make_pair(cache_metadata.name, | 741 cache_map_.insert(std::make_pair(cache_metadata.name, |
| 738 std::unique_ptr<CacheStorageCache>())); | 742 std::unique_ptr<CacheStorageCache>())); |
| 739 } | 743 } |
| 740 | 744 |
| 741 DCHECK(!cache_index_); | 745 DCHECK(!cache_index_); |
| 742 cache_index_ = std::move(index); | 746 cache_index_ = std::move(index); |
| 743 | 747 |
| 744 initializing_ = false; | 748 initializing_ = false; |
| 745 initialized_ = true; | 749 initialized_ = true; |
| 746 | 750 |
| 747 scheduler_->CompleteOperationAndRunNext(); | 751 scheduler_->CompleteOperationAndRunNext(); |
| 748 } | 752 } |
| 749 | 753 |
| 750 void CacheStorage::OpenCacheImpl(const std::string& cache_name, | 754 void CacheStorage::OpenCacheImpl(const std::string& cache_name, |
| 751 const CacheAndErrorCallback& callback) { | 755 CacheAndErrorCallback callback) { |
| 752 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 756 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 753 GetLoadedCache(cache_name); | 757 GetLoadedCache(cache_name); |
| 754 if (cache_handle) { | 758 if (cache_handle) { |
| 755 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK); | 759 std::move(callback).Run(std::move(cache_handle), CACHE_STORAGE_OK); |
| 756 return; | 760 return; |
| 757 } | 761 } |
| 758 | 762 |
| 759 cache_loader_->PrepareNewCacheDestination( | 763 cache_loader_->PrepareNewCacheDestination( |
| 760 cache_name, base::Bind(&CacheStorage::CreateCacheDidCreateCache, | 764 cache_name, base::BindOnce(&CacheStorage::CreateCacheDidCreateCache, |
| 761 weak_factory_.GetWeakPtr(), cache_name, callback)); | 765 weak_factory_.GetWeakPtr(), cache_name, |
| 766 std::move(callback))); |
| 762 } | 767 } |
| 763 | 768 |
| 764 void CacheStorage::CreateCacheDidCreateCache( | 769 void CacheStorage::CreateCacheDidCreateCache( |
| 765 const std::string& cache_name, | 770 const std::string& cache_name, |
| 766 const CacheAndErrorCallback& callback, | 771 CacheAndErrorCallback callback, |
| 767 std::unique_ptr<CacheStorageCache> cache) { | 772 std::unique_ptr<CacheStorageCache> cache) { |
| 768 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 773 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 769 | 774 |
| 770 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult", | 775 UMA_HISTOGRAM_BOOLEAN("ServiceWorkerCache.CreateCacheStorageResult", |
| 771 static_cast<bool>(cache)); | 776 static_cast<bool>(cache)); |
| 772 | 777 |
| 773 if (!cache) { | 778 if (!cache) { |
| 774 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), | 779 std::move(callback).Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 775 CACHE_STORAGE_ERROR_STORAGE); | 780 CACHE_STORAGE_ERROR_STORAGE); |
| 776 return; | 781 return; |
| 777 } | 782 } |
| 778 | 783 |
| 779 CacheStorageCache* cache_ptr = cache.get(); | 784 CacheStorageCache* cache_ptr = cache.get(); |
| 780 | 785 |
| 781 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 786 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 782 cache_index_->Insert( | 787 cache_index_->Insert( |
| 783 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); | 788 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); |
| 784 | 789 |
| 785 cache_loader_->WriteIndex( | 790 cache_loader_->WriteIndex( |
| 786 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex, | 791 *cache_index_, |
| 787 weak_factory_.GetWeakPtr(), callback, | 792 base::BindOnce(&CacheStorage::CreateCacheDidWriteIndex, |
| 788 base::Passed(CreateCacheHandle(cache_ptr)))); | 793 weak_factory_.GetWeakPtr(), std::move(callback), |
| 794 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 789 | 795 |
| 790 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | 796 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); |
| 791 } | 797 } |
| 792 | 798 |
| 793 void CacheStorage::CreateCacheDidWriteIndex( | 799 void CacheStorage::CreateCacheDidWriteIndex( |
| 794 const CacheAndErrorCallback& callback, | 800 CacheAndErrorCallback callback, |
| 795 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 801 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 796 bool success) { | 802 bool success) { |
| 797 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 803 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 798 DCHECK(cache_handle); | 804 DCHECK(cache_handle); |
| 799 | 805 |
| 800 // TODO(jkarlin): Handle !success. | 806 // TODO(jkarlin): Handle !success. |
| 801 | 807 |
| 802 callback.Run(std::move(cache_handle), CACHE_STORAGE_OK); | 808 std::move(callback).Run(std::move(cache_handle), CACHE_STORAGE_OK); |
| 803 } | 809 } |
| 804 | 810 |
| 805 void CacheStorage::HasCacheImpl(const std::string& cache_name, | 811 void CacheStorage::HasCacheImpl(const std::string& cache_name, |
| 806 const BoolAndErrorCallback& callback) { | 812 BoolAndErrorCallback callback) { |
| 807 bool has_cache = base::ContainsKey(cache_map_, cache_name); | 813 bool has_cache = base::ContainsKey(cache_map_, cache_name); |
| 808 callback.Run(has_cache, CACHE_STORAGE_OK); | 814 std::move(callback).Run(has_cache, CACHE_STORAGE_OK); |
| 809 } | 815 } |
| 810 | 816 |
| 811 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, | 817 void CacheStorage::DeleteCacheImpl(const std::string& cache_name, |
| 812 const BoolAndErrorCallback& callback) { | 818 BoolAndErrorCallback callback) { |
| 813 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 819 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 814 GetLoadedCache(cache_name); | 820 GetLoadedCache(cache_name); |
| 815 if (!cache_handle) { | 821 if (!cache_handle) { |
| 816 base::ThreadTaskRunnerHandle::Get()->PostTask( | 822 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 817 FROM_HERE, base::Bind(callback, false, CACHE_STORAGE_ERROR_NOT_FOUND)); | 823 FROM_HERE, base::BindOnce(std::move(callback), false, |
| 824 CACHE_STORAGE_ERROR_NOT_FOUND)); |
| 818 return; | 825 return; |
| 819 } | 826 } |
| 820 | 827 |
| 821 cache_handle->value()->SetObserver(nullptr); | 828 cache_handle->value()->SetObserver(nullptr); |
| 822 cache_index_->DoomCache(cache_name); | 829 cache_index_->DoomCache(cache_name); |
| 823 cache_loader_->WriteIndex( | 830 cache_loader_->WriteIndex( |
| 824 *cache_index_, | 831 *cache_index_, base::BindOnce(&CacheStorage::DeleteCacheDidWriteIndex, |
| 825 base::Bind(&CacheStorage::DeleteCacheDidWriteIndex, | 832 weak_factory_.GetWeakPtr(), |
| 826 weak_factory_.GetWeakPtr(), | 833 base::Passed(std::move(cache_handle)), |
| 827 base::Passed(std::move(cache_handle)), callback)); | 834 std::move(callback))); |
| 828 } | 835 } |
| 829 | 836 |
| 830 void CacheStorage::DeleteCacheDidWriteIndex( | 837 void CacheStorage::DeleteCacheDidWriteIndex( |
| 831 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 838 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 832 const BoolAndErrorCallback& callback, | 839 BoolAndErrorCallback callback, |
| 833 bool success) { | 840 bool success) { |
| 834 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 841 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 835 | 842 |
| 836 if (!success) { | 843 if (!success) { |
| 837 // Undo any changes if the index couldn't be written to disk. | 844 // Undo any changes if the index couldn't be written to disk. |
| 838 cache_index_->RestoreDoomedCache(); | 845 cache_index_->RestoreDoomedCache(); |
| 839 cache_handle->value()->SetObserver(this); | 846 cache_handle->value()->SetObserver(this); |
| 840 callback.Run(false, CACHE_STORAGE_ERROR_STORAGE); | 847 std::move(callback).Run(false, CACHE_STORAGE_ERROR_STORAGE); |
| 841 return; | 848 return; |
| 842 } | 849 } |
| 843 | 850 |
| 844 cache_index_->FinalizeDoomedCache(); | 851 cache_index_->FinalizeDoomedCache(); |
| 845 | 852 |
| 846 CacheMap::iterator map_iter = | 853 CacheMap::iterator map_iter = |
| 847 cache_map_.find(cache_handle->value()->cache_name()); | 854 cache_map_.find(cache_handle->value()->cache_name()); |
| 848 DCHECK(map_iter != cache_map_.end()); | 855 DCHECK(map_iter != cache_map_.end()); |
| 849 | 856 |
| 850 doomed_caches_.insert( | 857 doomed_caches_.insert( |
| 851 std::make_pair(map_iter->second.get(), std::move(map_iter->second))); | 858 std::make_pair(map_iter->second.get(), std::move(map_iter->second))); |
| 852 cache_map_.erase(map_iter); | 859 cache_map_.erase(map_iter); |
| 853 | 860 |
| 854 cache_loader_->NotifyCacheDoomed(std::move(cache_handle)); | 861 cache_loader_->NotifyCacheDoomed(std::move(cache_handle)); |
| 855 | 862 |
| 856 callback.Run(true, CACHE_STORAGE_OK); | 863 std::move(callback).Run(true, CACHE_STORAGE_OK); |
| 857 } | 864 } |
| 858 | 865 |
| 859 // Call this once the last handle to a doomed cache is gone. It's okay if this | 866 // Call this once the last handle to a doomed cache is gone. It's okay if this |
| 860 // doesn't get to complete before shutdown, the cache will be removed from disk | 867 // doesn't get to complete before shutdown, the cache will be removed from disk |
| 861 // on next startup in that case. | 868 // on next startup in that case. |
| 862 void CacheStorage::DeleteCacheFinalize(CacheStorageCache* doomed_cache) { | 869 void CacheStorage::DeleteCacheFinalize(CacheStorageCache* doomed_cache) { |
| 863 doomed_cache->Size(base::Bind(&CacheStorage::DeleteCacheDidGetSize, | 870 doomed_cache->Size(base::BindOnce(&CacheStorage::DeleteCacheDidGetSize, |
| 864 weak_factory_.GetWeakPtr(), doomed_cache)); | 871 weak_factory_.GetWeakPtr(), doomed_cache)); |
| 865 } | 872 } |
| 866 | 873 |
| 867 void CacheStorage::DeleteCacheDidGetSize(CacheStorageCache* doomed_cache, | 874 void CacheStorage::DeleteCacheDidGetSize(CacheStorageCache* doomed_cache, |
| 868 int64_t cache_size) { | 875 int64_t cache_size) { |
| 869 quota_manager_proxy_->NotifyStorageModified( | 876 quota_manager_proxy_->NotifyStorageModified( |
| 870 storage::QuotaClient::kServiceWorkerCache, origin_, | 877 storage::QuotaClient::kServiceWorkerCache, origin_, |
| 871 storage::kStorageTypeTemporary, -1 * cache_size); | 878 storage::kStorageTypeTemporary, -1 * cache_size); |
| 872 | 879 |
| 873 cache_loader_->CleanUpDeletedCache(doomed_cache); | 880 cache_loader_->CleanUpDeletedCache(doomed_cache); |
| 874 auto doomed_caches_iter = doomed_caches_.find(doomed_cache); | 881 auto doomed_caches_iter = doomed_caches_.find(doomed_cache); |
| 875 DCHECK(doomed_caches_iter != doomed_caches_.end()); | 882 DCHECK(doomed_caches_iter != doomed_caches_.end()); |
| 876 doomed_caches_.erase(doomed_caches_iter); | 883 doomed_caches_.erase(doomed_caches_iter); |
| 877 } | 884 } |
| 878 | 885 |
| 879 void CacheStorage::EnumerateCachesImpl(const IndexCallback& callback) { | 886 void CacheStorage::EnumerateCachesImpl(IndexCallback callback) { |
| 880 callback.Run(*cache_index_); | 887 std::move(callback).Run(*cache_index_); |
| 881 } | 888 } |
| 882 | 889 |
| 883 void CacheStorage::MatchCacheImpl( | 890 void CacheStorage::MatchCacheImpl( |
| 884 const std::string& cache_name, | 891 const std::string& cache_name, |
| 885 std::unique_ptr<ServiceWorkerFetchRequest> request, | 892 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 886 const CacheStorageCacheQueryParams& match_params, | 893 const CacheStorageCacheQueryParams& match_params, |
| 887 const CacheStorageCache::ResponseCallback& callback) { | 894 CacheStorageCache::ResponseCallback callback) { |
| 888 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 895 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 889 GetLoadedCache(cache_name); | 896 GetLoadedCache(cache_name); |
| 890 | 897 |
| 891 if (!cache_handle) { | 898 if (!cache_handle) { |
| 892 callback.Run(CACHE_STORAGE_ERROR_CACHE_NAME_NOT_FOUND, | 899 std::move(callback).Run(CACHE_STORAGE_ERROR_CACHE_NAME_NOT_FOUND, |
| 893 std::unique_ptr<ServiceWorkerResponse>(), | 900 std::unique_ptr<ServiceWorkerResponse>(), |
| 894 std::unique_ptr<storage::BlobDataHandle>()); | 901 std::unique_ptr<storage::BlobDataHandle>()); |
| 895 return; | 902 return; |
| 896 } | 903 } |
| 897 | 904 |
| 898 // Pass the cache handle along to the callback to keep the cache open until | 905 // Pass the cache handle along to the callback to keep the cache open until |
| 899 // match is done. | 906 // match is done. |
| 900 CacheStorageCache* cache_ptr = cache_handle->value(); | 907 CacheStorageCache* cache_ptr = cache_handle->value(); |
| 901 cache_ptr->Match( | 908 cache_ptr->Match(std::move(request), match_params, |
| 902 std::move(request), match_params, | 909 base::BindOnce(&CacheStorage::MatchCacheDidMatch, |
| 903 base::Bind(&CacheStorage::MatchCacheDidMatch, weak_factory_.GetWeakPtr(), | 910 weak_factory_.GetWeakPtr(), |
| 904 base::Passed(std::move(cache_handle)), callback)); | 911 base::Passed(std::move(cache_handle)), |
| 912 std::move(callback))); |
| 905 } | 913 } |
| 906 | 914 |
| 907 void CacheStorage::MatchCacheDidMatch( | 915 void CacheStorage::MatchCacheDidMatch( |
| 908 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 916 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 909 const CacheStorageCache::ResponseCallback& callback, | 917 CacheStorageCache::ResponseCallback callback, |
| 910 CacheStorageError error, | 918 CacheStorageError error, |
| 911 std::unique_ptr<ServiceWorkerResponse> response, | 919 std::unique_ptr<ServiceWorkerResponse> response, |
| 912 std::unique_ptr<storage::BlobDataHandle> handle) { | 920 std::unique_ptr<storage::BlobDataHandle> handle) { |
| 913 callback.Run(error, std::move(response), std::move(handle)); | 921 std::move(callback).Run(error, std::move(response), std::move(handle)); |
| 914 } | 922 } |
| 915 | 923 |
| 916 void CacheStorage::MatchAllCachesImpl( | 924 void CacheStorage::MatchAllCachesImpl( |
| 917 std::unique_ptr<ServiceWorkerFetchRequest> request, | 925 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 918 const CacheStorageCacheQueryParams& match_params, | 926 const CacheStorageCacheQueryParams& match_params, |
| 919 const CacheStorageCache::ResponseCallback& callback) { | 927 CacheStorageCache::ResponseCallback callback) { |
| 920 std::vector<CacheMatchResponse>* match_responses = | 928 std::vector<CacheMatchResponse>* match_responses = |
| 921 new std::vector<CacheMatchResponse>(cache_index_->num_entries()); | 929 new std::vector<CacheMatchResponse>(cache_index_->num_entries()); |
| 922 | 930 |
| 923 base::Closure barrier_closure = base::BarrierClosure( | 931 base::RepeatingClosure barrier_closure = base::BarrierClosure( |
| 924 cache_index_->num_entries(), | 932 cache_index_->num_entries(), |
| 925 base::Bind(&CacheStorage::MatchAllCachesDidMatchAll, | 933 base::BindOnce(&CacheStorage::MatchAllCachesDidMatchAll, |
| 926 weak_factory_.GetWeakPtr(), | 934 weak_factory_.GetWeakPtr(), |
| 927 base::Passed(base::WrapUnique(match_responses)), callback)); | 935 base::Passed(base::WrapUnique(match_responses)), |
| 936 std::move(callback))); |
| 928 | 937 |
| 929 size_t idx = 0; | 938 size_t idx = 0; |
| 930 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 939 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 931 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 940 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 932 GetLoadedCache(cache_metadata.name); | 941 GetLoadedCache(cache_metadata.name); |
| 933 DCHECK(cache_handle); | 942 DCHECK(cache_handle); |
| 934 | 943 |
| 935 CacheStorageCache* cache_ptr = cache_handle->value(); | 944 CacheStorageCache* cache_ptr = cache_handle->value(); |
| 936 cache_ptr->Match(base::MakeUnique<ServiceWorkerFetchRequest>(*request), | 945 cache_ptr->Match( |
| 937 match_params, | 946 base::MakeUnique<ServiceWorkerFetchRequest>(*request), match_params, |
| 938 base::Bind(&CacheStorage::MatchAllCachesDidMatch, | 947 base::BindOnce(&CacheStorage::MatchAllCachesDidMatch, |
| 939 weak_factory_.GetWeakPtr(), | 948 weak_factory_.GetWeakPtr(), |
| 940 base::Passed(std::move(cache_handle)), | 949 base::Passed(std::move(cache_handle)), |
| 941 &match_responses->at(idx), barrier_closure)); | 950 &match_responses->at(idx), barrier_closure)); |
| 942 idx++; | 951 idx++; |
| 943 } | 952 } |
| 944 } | 953 } |
| 945 | 954 |
| 946 void CacheStorage::MatchAllCachesDidMatch( | 955 void CacheStorage::MatchAllCachesDidMatch( |
| 947 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 956 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 948 CacheMatchResponse* out_match_response, | 957 CacheMatchResponse* out_match_response, |
| 949 const base::Closure& barrier_closure, | 958 const base::RepeatingClosure& barrier_closure, |
| 950 CacheStorageError error, | 959 CacheStorageError error, |
| 951 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 960 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 952 std::unique_ptr<storage::BlobDataHandle> handle) { | 961 std::unique_ptr<storage::BlobDataHandle> handle) { |
| 953 out_match_response->error = error; | 962 out_match_response->error = error; |
| 954 out_match_response->service_worker_response = | 963 out_match_response->service_worker_response = |
| 955 std::move(service_worker_response); | 964 std::move(service_worker_response); |
| 956 out_match_response->blob_data_handle = std::move(handle); | 965 out_match_response->blob_data_handle = std::move(handle); |
| 957 barrier_closure.Run(); | 966 barrier_closure.Run(); |
| 958 } | 967 } |
| 959 | 968 |
| 960 void CacheStorage::MatchAllCachesDidMatchAll( | 969 void CacheStorage::MatchAllCachesDidMatchAll( |
| 961 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, | 970 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 962 const CacheStorageCache::ResponseCallback& callback) { | 971 CacheStorageCache::ResponseCallback callback) { |
| 963 for (CacheMatchResponse& match_response : *match_responses) { | 972 for (CacheMatchResponse& match_response : *match_responses) { |
| 964 if (match_response.error == CACHE_STORAGE_ERROR_NOT_FOUND) | 973 if (match_response.error == CACHE_STORAGE_ERROR_NOT_FOUND) |
| 965 continue; | 974 continue; |
| 966 callback.Run(match_response.error, | 975 std::move(callback).Run(match_response.error, |
| 967 std::move(match_response.service_worker_response), | 976 std::move(match_response.service_worker_response), |
| 968 std::move(match_response.blob_data_handle)); | 977 std::move(match_response.blob_data_handle)); |
| 969 return; | 978 return; |
| 970 } | 979 } |
| 971 callback.Run(CACHE_STORAGE_ERROR_NOT_FOUND, | 980 std::move(callback).Run(CACHE_STORAGE_ERROR_NOT_FOUND, |
| 972 std::unique_ptr<ServiceWorkerResponse>(), | 981 std::unique_ptr<ServiceWorkerResponse>(), |
| 973 std::unique_ptr<storage::BlobDataHandle>()); | 982 std::unique_ptr<storage::BlobDataHandle>()); |
| 974 } | 983 } |
| 975 | 984 |
| 976 void CacheStorage::AddCacheHandleRef(CacheStorageCache* cache) { | 985 void CacheStorage::AddCacheHandleRef(CacheStorageCache* cache) { |
| 977 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 986 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 978 auto iter = cache_handle_counts_.find(cache); | 987 auto iter = cache_handle_counts_.find(cache); |
| 979 if (iter == cache_handle_counts_.end()) { | 988 if (iter == cache_handle_counts_.end()) { |
| 980 cache_handle_counts_[cache] = 1; | 989 cache_handle_counts_[cache] = 1; |
| 981 return; | 990 return; |
| 982 } | 991 } |
| 983 | 992 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1032 map_iter->second = std::move(new_cache); | 1041 map_iter->second = std::move(new_cache); |
| 1033 | 1042 |
| 1034 return CreateCacheHandle(cache_ptr); | 1043 return CreateCacheHandle(cache_ptr); |
| 1035 } | 1044 } |
| 1036 | 1045 |
| 1037 return CreateCacheHandle(cache); | 1046 return CreateCacheHandle(cache); |
| 1038 } | 1047 } |
| 1039 | 1048 |
| 1040 void CacheStorage::SizeRetrievedFromCache( | 1049 void CacheStorage::SizeRetrievedFromCache( |
| 1041 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 1050 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 1042 const base::Closure& closure, | 1051 base::OnceClosure closure, |
| 1043 int64_t* accumulator, | 1052 int64_t* accumulator, |
| 1044 int64_t size) { | 1053 int64_t size) { |
| 1045 cache_index_->SetCacheSize(cache_handle->value()->cache_name(), size); | 1054 cache_index_->SetCacheSize(cache_handle->value()->cache_name(), size); |
| 1046 *accumulator += size; | 1055 *accumulator += size; |
| 1047 closure.Run(); | 1056 std::move(closure).Run(); |
| 1048 } | 1057 } |
| 1049 | 1058 |
| 1050 void CacheStorage::GetSizeThenCloseAllCachesImpl(const SizeCallback& callback) { | 1059 void CacheStorage::GetSizeThenCloseAllCachesImpl(SizeCallback callback) { |
| 1051 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1060 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1052 DCHECK(initialized_); | 1061 DCHECK(initialized_); |
| 1053 | 1062 |
| 1054 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1063 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1055 int64_t* accumulator_ptr = accumulator.get(); | 1064 int64_t* accumulator_ptr = accumulator.get(); |
| 1056 | 1065 |
| 1057 base::Closure barrier_closure = base::BarrierClosure( | 1066 base::RepeatingClosure barrier_closure = |
| 1058 cache_index_->num_entries(), | 1067 base::BarrierClosure(cache_index_->num_entries(), |
| 1059 base::Bind(&SizeRetrievedFromAllCaches, | 1068 base::BindOnce(&SizeRetrievedFromAllCaches, |
| 1060 base::Passed(std::move(accumulator)), callback)); | 1069 base::Passed(std::move(accumulator)), |
| 1070 std::move(callback))); |
| 1061 | 1071 |
| 1062 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 1072 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1063 auto cache_handle = GetLoadedCache(cache_metadata.name); | 1073 auto cache_handle = GetLoadedCache(cache_metadata.name); |
| 1064 CacheStorageCache* cache = cache_handle->value(); | 1074 CacheStorageCache* cache = cache_handle->value(); |
| 1065 cache->GetSizeThenClose(base::Bind(&CacheStorage::SizeRetrievedFromCache, | 1075 cache->GetSizeThenClose(base::BindOnce( |
| 1066 weak_factory_.GetWeakPtr(), | 1076 &CacheStorage::SizeRetrievedFromCache, weak_factory_.GetWeakPtr(), |
| 1067 base::Passed(std::move(cache_handle)), | 1077 base::Passed(std::move(cache_handle)), barrier_closure, |
| 1068 barrier_closure, accumulator_ptr)); | 1078 accumulator_ptr)); |
| 1069 } | 1079 } |
| 1070 } | 1080 } |
| 1071 | 1081 |
| 1072 void CacheStorage::SizeImpl(const SizeCallback& callback) { | 1082 void CacheStorage::SizeImpl(SizeCallback callback) { |
| 1073 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1083 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1074 DCHECK(initialized_); | 1084 DCHECK(initialized_); |
| 1075 | 1085 |
| 1076 if (cache_index_->GetStorageSize() != kSizeUnknown) { | 1086 if (cache_index_->GetStorageSize() != kSizeUnknown) { |
| 1077 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1087 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1078 FROM_HERE, base::Bind(callback, cache_index_->GetStorageSize())); | 1088 FROM_HERE, |
| 1089 base::BindOnce(std::move(callback), cache_index_->GetStorageSize())); |
| 1079 return; | 1090 return; |
| 1080 } | 1091 } |
| 1081 | 1092 |
| 1082 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1093 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1083 int64_t* accumulator_ptr = accumulator.get(); | 1094 int64_t* accumulator_ptr = accumulator.get(); |
| 1084 | 1095 |
| 1085 base::Closure barrier_closure = base::BarrierClosure( | 1096 base::RepeatingClosure barrier_closure = |
| 1086 cache_index_->num_entries(), | 1097 base::BarrierClosure(cache_index_->num_entries(), |
| 1087 base::Bind(&SizeRetrievedFromAllCaches, | 1098 base::BindOnce(&SizeRetrievedFromAllCaches, |
| 1088 base::Passed(std::move(accumulator)), callback)); | 1099 base::Passed(std::move(accumulator)), |
| 1100 std::move(callback))); |
| 1089 | 1101 |
| 1090 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 1102 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1091 if (cache_metadata.size != CacheStorage::kSizeUnknown) { | 1103 if (cache_metadata.size != CacheStorage::kSizeUnknown) { |
| 1092 *accumulator_ptr += cache_metadata.size; | 1104 *accumulator_ptr += cache_metadata.size; |
| 1093 barrier_closure.Run(); | 1105 barrier_closure.Run(); |
| 1094 continue; | 1106 continue; |
| 1095 } | 1107 } |
| 1096 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1108 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1097 GetLoadedCache(cache_metadata.name); | 1109 GetLoadedCache(cache_metadata.name); |
| 1098 CacheStorageCache* cache = cache_handle->value(); | 1110 CacheStorageCache* cache = cache_handle->value(); |
| 1099 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache, | 1111 cache->Size(base::BindOnce(&CacheStorage::SizeRetrievedFromCache, |
| 1100 weak_factory_.GetWeakPtr(), | 1112 weak_factory_.GetWeakPtr(), |
| 1101 base::Passed(std::move(cache_handle)), | 1113 base::Passed(std::move(cache_handle)), |
| 1102 barrier_closure, accumulator_ptr)); | 1114 barrier_closure, accumulator_ptr)); |
| 1103 } | 1115 } |
| 1104 } | 1116 } |
| 1105 | 1117 |
| 1106 } // namespace content | 1118 } // namespace content |
| OLD | NEW |