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