| 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> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/barrier_closure.h" | 13 #include "base/barrier_closure.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/memory_mapped_file.h" | 15 #include "base/files/memory_mapped_file.h" |
| 16 #include "base/guid.h" | 16 #include "base/guid.h" |
| 17 #include "base/lazy_instance.h" | |
| 18 #include "base/location.h" | 17 #include "base/location.h" |
| 19 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 20 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 21 #include "base/metrics/histogram_macros.h" | 20 #include "base/metrics/histogram_macros.h" |
| 22 #include "base/numerics/safe_conversions.h" | 21 #include "base/numerics/safe_conversions.h" |
| 23 #include "base/sequenced_task_runner.h" | 22 #include "base/sequenced_task_runner.h" |
| 24 #include "base/sha1.h" | 23 #include "base/sha1.h" |
| 25 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
| 26 #include "base/stl_util.h" | 25 #include "base/stl_util.h" |
| 27 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
| 28 #include "base/strings/string_util.h" | 27 #include "base/strings/string_util.h" |
| 29 #include "base/threading/thread_task_runner_handle.h" | 28 #include "base/threading/thread_task_runner_handle.h" |
| 30 #include "content/browser/cache_storage/cache_storage.pb.h" | 29 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 31 #include "content/browser/cache_storage/cache_storage_cache.h" | 30 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 32 #include "content/browser/cache_storage/cache_storage_cache_handle.h" | 31 #include "content/browser/cache_storage/cache_storage_cache_handle.h" |
| 33 #include "content/browser/cache_storage/cache_storage_index.h" | 32 #include "content/browser/cache_storage/cache_storage_index.h" |
| 34 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 33 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
| 35 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 36 #include "crypto/symmetric_key.h" | |
| 37 #include "net/base/directory_lister.h" | 35 #include "net/base/directory_lister.h" |
| 38 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
| 39 #include "net/url_request/url_request_context_getter.h" | 37 #include "net/url_request/url_request_context_getter.h" |
| 40 #include "storage/browser/blob/blob_storage_context.h" | 38 #include "storage/browser/blob/blob_storage_context.h" |
| 41 #include "storage/browser/quota/quota_manager_proxy.h" | 39 #include "storage/browser/quota/quota_manager_proxy.h" |
| 42 | 40 |
| 43 using base::LazyInstance; | |
| 44 using crypto::SymmetricKey; | |
| 45 | |
| 46 namespace content { | 41 namespace content { |
| 47 | 42 |
| 48 namespace { | 43 namespace { |
| 49 | 44 |
| 50 const SymmetricKey::Algorithm kPaddingKeyAlgorithm = SymmetricKey::AES; | |
| 51 | |
| 52 // LazyInstance needs a new-able type so this class exists solely to "own" | |
| 53 // a SymmetricKey. | |
| 54 class SymmetricKeyOwner { | |
| 55 public: | |
| 56 std::unique_ptr<SymmetricKey> CreateDuplicate() const { | |
| 57 return SymmetricKey::Import(kPaddingKeyAlgorithm, key()); | |
| 58 } | |
| 59 | |
| 60 // Only for test purposes. | |
| 61 void GenerateNew() { | |
| 62 key_ = SymmetricKey::GenerateRandomKey(kPaddingKeyAlgorithm, 128); | |
| 63 } | |
| 64 | |
| 65 const std::string& key() const { return key_->key(); } | |
| 66 | |
| 67 private: | |
| 68 std::unique_ptr<SymmetricKey> key_ = | |
| 69 SymmetricKey::GenerateRandomKey(kPaddingKeyAlgorithm, 128); | |
| 70 }; | |
| 71 | |
| 72 static LazyInstance<SymmetricKeyOwner>::Leaky s_padding_key = | |
| 73 LAZY_INSTANCE_INITIALIZER; | |
| 74 | |
| 75 std::string HexedHash(const std::string& value) { | 45 std::string HexedHash(const std::string& value) { |
| 76 std::string value_hash = base::SHA1HashString(value); | 46 std::string value_hash = base::SHA1HashString(value); |
| 77 std::string valued_hexed_hash = base::ToLowerASCII( | 47 std::string valued_hexed_hash = base::ToLowerASCII( |
| 78 base::HexEncode(value_hash.c_str(), value_hash.length())); | 48 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 79 return valued_hexed_hash; | 49 return valued_hexed_hash; |
| 80 } | 50 } |
| 81 | 51 |
| 82 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, | 52 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, |
| 83 CacheStorage::SizeCallback callback) { | 53 CacheStorage::SizeCallback callback) { |
| 84 base::ThreadTaskRunnerHandle::Get()->PostTask( | 54 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 85 FROM_HERE, base::BindOnce(std::move(callback), *accumulator)); | 55 FROM_HERE, base::BindOnce(std::move(callback), *accumulator)); |
| 86 } | 56 } |
| 87 | 57 |
| 88 void DoNothingWithBool(bool success) {} | 58 void DoNothingWithBool(bool success) {} |
| 89 | 59 |
| 90 std::unique_ptr<SymmetricKey> ImportPaddingKey(const std::string& raw_key) { | |
| 91 return SymmetricKey::Import(kPaddingKeyAlgorithm, raw_key); | |
| 92 } | |
| 93 | |
| 94 } // namespace | 60 } // namespace |
| 95 | 61 |
| 96 const char CacheStorage::kIndexFileName[] = "index.txt"; | 62 const char CacheStorage::kIndexFileName[] = "index.txt"; |
| 97 constexpr int64_t CacheStorage::kSizeUnknown; | 63 constexpr int64_t CacheStorage::kSizeUnknown; |
| 98 | 64 |
| 99 struct CacheStorage::CacheMatchResponse { | 65 struct CacheStorage::CacheMatchResponse { |
| 100 CacheMatchResponse() = default; | 66 CacheMatchResponse() = default; |
| 101 ~CacheMatchResponse() = default; | 67 ~CacheMatchResponse() = default; |
| 102 | 68 |
| 103 CacheStorageError error; | 69 CacheStorageError error; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 129 origin_(origin) { | 95 origin_(origin) { |
| 130 DCHECK(!origin_.is_empty()); | 96 DCHECK(!origin_.is_empty()); |
| 131 } | 97 } |
| 132 | 98 |
| 133 virtual ~CacheLoader() {} | 99 virtual ~CacheLoader() {} |
| 134 | 100 |
| 135 // 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 |
| 136 // load the backend, that happens lazily when the cache is used. | 102 // load the backend, that happens lazily when the cache is used. |
| 137 virtual std::unique_ptr<CacheStorageCache> CreateCache( | 103 virtual std::unique_ptr<CacheStorageCache> CreateCache( |
| 138 const std::string& cache_name, | 104 const std::string& cache_name, |
| 139 int64_t cache_size, | 105 int64_t cache_size) = 0; |
| 140 int64_t cache_padding, | |
| 141 std::unique_ptr<SymmetricKey> cache_padding_key) = 0; | |
| 142 | 106 |
| 143 // 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. |
| 144 virtual void PrepareNewCacheDestination(const std::string& cache_name, | 108 virtual void PrepareNewCacheDestination(const std::string& cache_name, |
| 145 CacheCallback callback) = 0; | 109 CacheCallback callback) = 0; |
| 146 | 110 |
| 147 // 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 |
| 148 // removing the cache's directory. | 112 // removing the cache's directory. |
| 149 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; | 113 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; |
| 150 | 114 |
| 151 // Writes the cache index to disk if applicable. | 115 // Writes the cache index to disk if applicable. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 base::WeakPtr<storage::BlobStorageContext> blob_context, | 157 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 194 CacheStorage* cache_storage, | 158 CacheStorage* cache_storage, |
| 195 const GURL& origin) | 159 const GURL& origin) |
| 196 : CacheLoader(cache_task_runner, | 160 : CacheLoader(cache_task_runner, |
| 197 request_context, | 161 request_context, |
| 198 quota_manager_proxy, | 162 quota_manager_proxy, |
| 199 blob_context, | 163 blob_context, |
| 200 cache_storage, | 164 cache_storage, |
| 201 origin) {} | 165 origin) {} |
| 202 | 166 |
| 203 std::unique_ptr<CacheStorageCache> CreateCache( | 167 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, |
| 204 const std::string& cache_name, | 168 int64_t cache_size) override { |
| 205 int64_t cache_size, | |
| 206 int64_t cache_padding, | |
| 207 std::unique_ptr<SymmetricKey> cache_padding_key) override { | |
| 208 return CacheStorageCache::CreateMemoryCache( | 169 return CacheStorageCache::CreateMemoryCache( |
| 209 origin_, cache_name, cache_storage_, request_context_getter_, | 170 origin_, cache_name, cache_storage_, request_context_getter_, |
| 210 quota_manager_proxy_, blob_context_, | 171 quota_manager_proxy_, blob_context_); |
| 211 s_padding_key.Get().CreateDuplicate()); | |
| 212 } | 172 } |
| 213 | 173 |
| 214 void PrepareNewCacheDestination(const std::string& cache_name, | 174 void PrepareNewCacheDestination(const std::string& cache_name, |
| 215 CacheCallback callback) override { | 175 CacheCallback callback) override { |
| 216 std::unique_ptr<CacheStorageCache> cache = | 176 std::unique_ptr<CacheStorageCache> cache = |
| 217 CreateCache(cache_name, 0 /*cache_size*/, 0 /* cache_padding */, | 177 CreateCache(cache_name, 0 /*cache_size*/); |
| 218 s_padding_key.Get().CreateDuplicate()); | |
| 219 std::move(callback).Run(std::move(cache)); | 178 std::move(callback).Run(std::move(cache)); |
| 220 } | 179 } |
| 221 | 180 |
| 222 void CleanUpDeletedCache(CacheStorageCache* cache) override {} | 181 void CleanUpDeletedCache(CacheStorageCache* cache) override {} |
| 223 | 182 |
| 224 void WriteIndex(const CacheStorageIndex& index, | 183 void WriteIndex(const CacheStorageIndex& index, |
| 225 BoolCallback callback) override { | 184 BoolCallback callback) override { |
| 226 std::move(callback).Run(true); | 185 std::move(callback).Run(true); |
| 227 } | 186 } |
| 228 | 187 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 const GURL& origin) | 225 const GURL& origin) |
| 267 : CacheLoader(cache_task_runner, | 226 : CacheLoader(cache_task_runner, |
| 268 request_context, | 227 request_context, |
| 269 quota_manager_proxy, | 228 quota_manager_proxy, |
| 270 blob_context, | 229 blob_context, |
| 271 cache_storage, | 230 cache_storage, |
| 272 origin), | 231 origin), |
| 273 origin_path_(origin_path), | 232 origin_path_(origin_path), |
| 274 weak_ptr_factory_(this) {} | 233 weak_ptr_factory_(this) {} |
| 275 | 234 |
| 276 std::unique_ptr<CacheStorageCache> CreateCache( | 235 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, |
| 277 const std::string& cache_name, | 236 int64_t cache_size) override { |
| 278 int64_t cache_size, | |
| 279 int64_t cache_padding, | |
| 280 std::unique_ptr<SymmetricKey> cache_padding_key) override { | |
| 281 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 237 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 282 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); | 238 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 283 | 239 |
| 284 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; | 240 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 285 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); | 241 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 286 return CacheStorageCache::CreatePersistentCache( | 242 return CacheStorageCache::CreatePersistentCache( |
| 287 origin_, cache_name, cache_storage_, cache_path, | 243 origin_, cache_name, cache_storage_, cache_path, |
| 288 request_context_getter_, quota_manager_proxy_, blob_context_, | 244 request_context_getter_, quota_manager_proxy_, blob_context_, |
| 289 cache_size, cache_padding, std::move(cache_padding_key)); | 245 cache_size); |
| 290 } | 246 } |
| 291 | 247 |
| 292 void PrepareNewCacheDestination(const std::string& cache_name, | 248 void PrepareNewCacheDestination(const std::string& cache_name, |
| 293 CacheCallback callback) override { | 249 CacheCallback callback) override { |
| 294 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 295 | 251 |
| 296 PostTaskAndReplyWithResult( | 252 PostTaskAndReplyWithResult( |
| 297 cache_task_runner_.get(), FROM_HERE, | 253 cache_task_runner_.get(), FROM_HERE, |
| 298 base::BindOnce(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, | 254 base::BindOnce(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 299 origin_path_), | 255 origin_path_), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 317 | 273 |
| 318 void PrepareNewCacheCreateCache(const std::string& cache_name, | 274 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 319 CacheCallback callback, | 275 CacheCallback callback, |
| 320 const std::string& cache_dir) { | 276 const std::string& cache_dir) { |
| 321 if (cache_dir.empty()) { | 277 if (cache_dir.empty()) { |
| 322 std::move(callback).Run(std::unique_ptr<CacheStorageCache>()); | 278 std::move(callback).Run(std::unique_ptr<CacheStorageCache>()); |
| 323 return; | 279 return; |
| 324 } | 280 } |
| 325 | 281 |
| 326 cache_name_to_cache_dir_[cache_name] = cache_dir; | 282 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 327 std::move(callback).Run(CreateCache(cache_name, CacheStorage::kSizeUnknown, | 283 std::move(callback).Run( |
| 328 CacheStorage::kSizeUnknown, | 284 CreateCache(cache_name, CacheStorage::kSizeUnknown)); |
| 329 s_padding_key.Get().CreateDuplicate())); | |
| 330 } | 285 } |
| 331 | 286 |
| 332 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 287 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 333 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 288 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 334 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); | 289 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 335 | 290 |
| 336 base::FilePath cache_path = | 291 base::FilePath cache_path = |
| 337 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 292 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 338 doomed_cache_to_path_.erase(cache); | 293 doomed_cache_to_path_.erase(cache); |
| 339 | 294 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 360 for (const auto& cache_metadata : index.ordered_cache_metadata()) { | 315 for (const auto& cache_metadata : index.ordered_cache_metadata()) { |
| 361 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); | 316 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); |
| 362 | 317 |
| 363 proto::CacheStorageIndex::Cache* index_cache = protobuf_index.add_cache(); | 318 proto::CacheStorageIndex::Cache* index_cache = protobuf_index.add_cache(); |
| 364 index_cache->set_name(cache_metadata.name); | 319 index_cache->set_name(cache_metadata.name); |
| 365 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_metadata.name]); | 320 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_metadata.name]); |
| 366 if (cache_metadata.size == CacheStorage::kSizeUnknown) | 321 if (cache_metadata.size == CacheStorage::kSizeUnknown) |
| 367 index_cache->clear_size(); | 322 index_cache->clear_size(); |
| 368 else | 323 else |
| 369 index_cache->set_size(cache_metadata.size); | 324 index_cache->set_size(cache_metadata.size); |
| 370 index_cache->set_padding_key(cache_metadata.padding_key); | |
| 371 index_cache->set_padding(cache_metadata.padding); | |
| 372 index_cache->set_padding_version( | |
| 373 CacheStorageCache::GetResponsePaddingVersion()); | |
| 374 } | 325 } |
| 375 | 326 |
| 376 std::string serialized; | 327 std::string serialized; |
| 377 bool success = protobuf_index.SerializeToString(&serialized); | 328 bool success = protobuf_index.SerializeToString(&serialized); |
| 378 DCHECK(success); | 329 DCHECK(success); |
| 379 | 330 |
| 380 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 331 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
| 381 base::FilePath index_path = | 332 base::FilePath index_path = |
| 382 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 333 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
| 383 | 334 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 | 369 |
| 419 std::unique_ptr<std::set<std::string>> cache_dirs( | 370 std::unique_ptr<std::set<std::string>> cache_dirs( |
| 420 new std::set<std::string>); | 371 new std::set<std::string>); |
| 421 | 372 |
| 422 auto index = base::MakeUnique<CacheStorageIndex>(); | 373 auto index = base::MakeUnique<CacheStorageIndex>(); |
| 423 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) { |
| 424 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); | 375 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); |
| 425 DCHECK(cache.has_cache_dir()); | 376 DCHECK(cache.has_cache_dir()); |
| 426 int64_t cache_size = | 377 int64_t cache_size = |
| 427 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; | 378 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; |
| 428 int64_t cache_padding; | 379 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); |
| 429 if (cache.has_padding()) { | |
| 430 if (cache.has_padding_version() && | |
| 431 cache.padding_version() == | |
| 432 CacheStorageCache::GetResponsePaddingVersion()) { | |
| 433 cache_padding = cache.padding(); | |
| 434 } else { | |
| 435 // The padding algorithm version changed so set to unknown to force | |
| 436 // recalculation. | |
| 437 cache_padding = CacheStorage::kSizeUnknown; | |
| 438 } | |
| 439 } else { | |
| 440 cache_padding = CacheStorage::kSizeUnknown; | |
| 441 } | |
| 442 | |
| 443 std::string cache_padding_key = cache.has_padding_key() | |
| 444 ? cache.padding_key() | |
| 445 : s_padding_key.Get().key(); | |
| 446 | |
| 447 index->Insert(CacheStorageIndex::CacheMetadata( | |
| 448 cache.name(), cache_size, cache_padding, | |
| 449 std::move(cache_padding_key))); | |
| 450 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 380 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 451 cache_dirs->insert(cache.cache_dir()); | 381 cache_dirs->insert(cache.cache_dir()); |
| 452 } | 382 } |
| 453 | 383 |
| 454 cache_task_runner_->PostTask( | 384 cache_task_runner_->PostTask( |
| 455 FROM_HERE, base::BindOnce(&DeleteUnreferencedCachesInPool, origin_path_, | 385 FROM_HERE, base::BindOnce(&DeleteUnreferencedCachesInPool, origin_path_, |
| 456 base::Passed(&cache_dirs))); | 386 base::Passed(&cache_dirs))); |
| 457 std::move(callback).Run(std::move(index)); | 387 std::move(callback).Run(std::move(index)); |
| 458 } | 388 } |
| 459 | 389 |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 679 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 750 if (index_write_pending()) { | 680 if (index_write_pending()) { |
| 751 index_write_task_.Cancel(); | 681 index_write_task_.Cancel(); |
| 752 WriteIndex(std::move(callback)); | 682 WriteIndex(std::move(callback)); |
| 753 return true; | 683 return true; |
| 754 } | 684 } |
| 755 std::move(callback).Run(true /* success */); | 685 std::move(callback).Run(true /* success */); |
| 756 return false; | 686 return false; |
| 757 } | 687 } |
| 758 | 688 |
| 759 void CacheStorage::CacheSizeUpdated(const CacheStorageCache* cache) { | 689 void CacheStorage::CacheSizeUpdated(const CacheStorageCache* cache, |
| 690 int64_t size) { |
| 760 // Should not be called for doomed caches. | 691 // Should not be called for doomed caches. |
| 761 DCHECK(!base::ContainsKey(doomed_caches_, | 692 DCHECK(!base::ContainsKey(doomed_caches_, |
| 762 const_cast<CacheStorageCache*>(cache))); | 693 const_cast<CacheStorageCache*>(cache))); |
| 763 DCHECK_NE(cache->cache_padding(), kSizeUnknown); | 694 cache_index_->SetCacheSize(cache->cache_name(), size); |
| 764 cache_index_->SetCacheSize(cache->cache_name(), cache->cache_size()); | |
| 765 cache_index_->SetCachePadding(cache->cache_name(), cache->cache_padding()); | |
| 766 ScheduleWriteIndex(); | 695 ScheduleWriteIndex(); |
| 767 } | 696 } |
| 768 | 697 |
| 769 void CacheStorage::StartAsyncOperationForTesting() { | 698 void CacheStorage::StartAsyncOperationForTesting() { |
| 770 scheduler_->ScheduleOperation(base::BindOnce(&base::DoNothing)); | 699 scheduler_->ScheduleOperation(base::BindOnce(&base::DoNothing)); |
| 771 } | 700 } |
| 772 | 701 |
| 773 void CacheStorage::CompleteAsyncOperationForTesting() { | 702 void CacheStorage::CompleteAsyncOperationForTesting() { |
| 774 scheduler_->CompleteOperationAndRunNext(); | 703 scheduler_->CompleteOperationAndRunNext(); |
| 775 } | 704 } |
| 776 | 705 |
| 777 // static | |
| 778 void CacheStorage::GenerateNewKeyForTesting() { | |
| 779 s_padding_key.Get().GenerateNew(); | |
| 780 } | |
| 781 | |
| 782 // 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. |
| 783 void CacheStorage::LazyInit() { | 707 void CacheStorage::LazyInit() { |
| 784 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 708 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 785 DCHECK(!initialized_); | 709 DCHECK(!initialized_); |
| 786 | 710 |
| 787 if (initializing_) | 711 if (initializing_) |
| 788 return; | 712 return; |
| 789 | 713 |
| 790 DCHECK(!scheduler_->ScheduledOperations()); | 714 DCHECK(!scheduler_->ScheduledOperations()); |
| 791 | 715 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 | 777 |
| 854 if (!cache) { | 778 if (!cache) { |
| 855 std::move(callback).Run(std::unique_ptr<CacheStorageCacheHandle>(), | 779 std::move(callback).Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 856 CACHE_STORAGE_ERROR_STORAGE); | 780 CACHE_STORAGE_ERROR_STORAGE); |
| 857 return; | 781 return; |
| 858 } | 782 } |
| 859 | 783 |
| 860 CacheStorageCache* cache_ptr = cache.get(); | 784 CacheStorageCache* cache_ptr = cache.get(); |
| 861 | 785 |
| 862 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 786 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 863 cache_index_->Insert(CacheStorageIndex::CacheMetadata( | 787 cache_index_->Insert( |
| 864 cache_name, cache_ptr->cache_size(), cache_ptr->cache_padding(), | 788 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); |
| 865 cache_ptr->cache_padding_key()->key())); | |
| 866 | 789 |
| 867 cache_loader_->WriteIndex( | 790 cache_loader_->WriteIndex( |
| 868 *cache_index_, | 791 *cache_index_, |
| 869 base::BindOnce(&CacheStorage::CreateCacheDidWriteIndex, | 792 base::BindOnce(&CacheStorage::CreateCacheDidWriteIndex, |
| 870 weak_factory_.GetWeakPtr(), std::move(callback), | 793 weak_factory_.GetWeakPtr(), std::move(callback), |
| 871 base::Passed(CreateCacheHandle(cache_ptr)))); | 794 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 872 | 795 |
| 873 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | 796 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); |
| 874 } | 797 } |
| 875 | 798 |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1028 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1106 DCHECK(initialized_); | 1029 DCHECK(initialized_); |
| 1107 | 1030 |
| 1108 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 1031 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 1109 if (map_iter == cache_map_.end()) | 1032 if (map_iter == cache_map_.end()) |
| 1110 return std::unique_ptr<CacheStorageCacheHandle>(); | 1033 return std::unique_ptr<CacheStorageCacheHandle>(); |
| 1111 | 1034 |
| 1112 CacheStorageCache* cache = map_iter->second.get(); | 1035 CacheStorageCache* cache = map_iter->second.get(); |
| 1113 | 1036 |
| 1114 if (!cache) { | 1037 if (!cache) { |
| 1115 const CacheStorageIndex::CacheMetadata* metadata = | |
| 1116 cache_index_->GetMetadata(cache_name); | |
| 1117 DCHECK(metadata); | |
| 1118 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( | 1038 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( |
| 1119 cache_name, metadata->size, metadata->padding, | 1039 cache_name, cache_index_->GetCacheSize(cache_name)); |
| 1120 ImportPaddingKey(metadata->padding_key)); | |
| 1121 CacheStorageCache* cache_ptr = new_cache.get(); | 1040 CacheStorageCache* cache_ptr = new_cache.get(); |
| 1122 map_iter->second = std::move(new_cache); | 1041 map_iter->second = std::move(new_cache); |
| 1123 | 1042 |
| 1124 return CreateCacheHandle(cache_ptr); | 1043 return CreateCacheHandle(cache_ptr); |
| 1125 } | 1044 } |
| 1126 | 1045 |
| 1127 return CreateCacheHandle(cache); | 1046 return CreateCacheHandle(cache); |
| 1128 } | 1047 } |
| 1129 | 1048 |
| 1130 void CacheStorage::SizeRetrievedFromCache( | 1049 void CacheStorage::SizeRetrievedFromCache( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1157 &CacheStorage::SizeRetrievedFromCache, weak_factory_.GetWeakPtr(), | 1076 &CacheStorage::SizeRetrievedFromCache, weak_factory_.GetWeakPtr(), |
| 1158 base::Passed(std::move(cache_handle)), barrier_closure, | 1077 base::Passed(std::move(cache_handle)), barrier_closure, |
| 1159 accumulator_ptr)); | 1078 accumulator_ptr)); |
| 1160 } | 1079 } |
| 1161 } | 1080 } |
| 1162 | 1081 |
| 1163 void CacheStorage::SizeImpl(SizeCallback callback) { | 1082 void CacheStorage::SizeImpl(SizeCallback callback) { |
| 1164 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1083 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1165 DCHECK(initialized_); | 1084 DCHECK(initialized_); |
| 1166 | 1085 |
| 1167 if (cache_index_->GetPaddedStorageSize() != kSizeUnknown) { | 1086 if (cache_index_->GetStorageSize() != kSizeUnknown) { |
| 1168 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1087 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1169 FROM_HERE, base::BindOnce(std::move(callback), | 1088 FROM_HERE, |
| 1170 cache_index_->GetPaddedStorageSize())); | 1089 base::BindOnce(std::move(callback), cache_index_->GetStorageSize())); |
| 1171 return; | 1090 return; |
| 1172 } | 1091 } |
| 1173 | 1092 |
| 1174 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1093 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1175 int64_t* accumulator_ptr = accumulator.get(); | 1094 int64_t* accumulator_ptr = accumulator.get(); |
| 1176 | 1095 |
| 1177 base::RepeatingClosure barrier_closure = | 1096 base::RepeatingClosure barrier_closure = |
| 1178 base::BarrierClosure(cache_index_->num_entries(), | 1097 base::BarrierClosure(cache_index_->num_entries(), |
| 1179 base::BindOnce(&SizeRetrievedFromAllCaches, | 1098 base::BindOnce(&SizeRetrievedFromAllCaches, |
| 1180 base::Passed(std::move(accumulator)), | 1099 base::Passed(std::move(accumulator)), |
| 1181 std::move(callback))); | 1100 std::move(callback))); |
| 1182 | 1101 |
| 1183 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 1102 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1184 if (cache_metadata.size != CacheStorage::kSizeUnknown) { | 1103 if (cache_metadata.size != CacheStorage::kSizeUnknown) { |
| 1185 *accumulator_ptr += cache_metadata.size; | 1104 *accumulator_ptr += cache_metadata.size; |
| 1186 barrier_closure.Run(); | 1105 barrier_closure.Run(); |
| 1187 continue; | 1106 continue; |
| 1188 } | 1107 } |
| 1189 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1108 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1190 GetLoadedCache(cache_metadata.name); | 1109 GetLoadedCache(cache_metadata.name); |
| 1191 CacheStorageCache* cache = cache_handle->value(); | 1110 CacheStorageCache* cache = cache_handle->value(); |
| 1192 cache->Size(base::BindOnce(&CacheStorage::SizeRetrievedFromCache, | 1111 cache->Size(base::BindOnce(&CacheStorage::SizeRetrievedFromCache, |
| 1193 weak_factory_.GetWeakPtr(), | 1112 weak_factory_.GetWeakPtr(), |
| 1194 base::Passed(std::move(cache_handle)), | 1113 base::Passed(std::move(cache_handle)), |
| 1195 barrier_closure, accumulator_ptr)); | 1114 barrier_closure, accumulator_ptr)); |
| 1196 } | 1115 } |
| 1197 } | 1116 } |
| 1198 | 1117 |
| 1199 } // namespace content | 1118 } // namespace content |
| OLD | NEW |