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