| 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 |
| 45 std::string HexedHash(const std::string& value) { | 52 std::string HexedHash(const std::string& value) { |
| 46 std::string value_hash = base::SHA1HashString(value); | 53 std::string value_hash = base::SHA1HashString(value); |
| 47 std::string valued_hexed_hash = base::ToLowerASCII( | 54 std::string valued_hexed_hash = base::ToLowerASCII( |
| 48 base::HexEncode(value_hash.c_str(), value_hash.length())); | 55 base::HexEncode(value_hash.c_str(), value_hash.length())); |
| 49 return valued_hexed_hash; | 56 return valued_hexed_hash; |
| 50 } | 57 } |
| 51 | 58 |
| 52 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, | 59 void SizeRetrievedFromAllCaches(std::unique_ptr<int64_t> accumulator, |
| 53 CacheStorage::SizeCallback callback) { | 60 CacheStorage::SizeCallback callback) { |
| 54 base::ThreadTaskRunnerHandle::Get()->PostTask( | 61 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 55 FROM_HERE, base::BindOnce(std::move(callback), *accumulator)); | 62 FROM_HERE, base::BindOnce(std::move(callback), *accumulator)); |
| 56 } | 63 } |
| 57 | 64 |
| 58 void DoNothingWithBool(bool success) {} | 65 void DoNothingWithBool(bool success) {} |
| 59 | 66 |
| 67 std::unique_ptr<SymmetricKey> SessionPaddingKey() { |
| 68 class KeyOwner { |
| 69 public: |
| 70 std::unique_ptr<SymmetricKey> CreateDuplicate() const { |
| 71 return SymmetricKey::Import(kPaddingKeyAlgorithm, key->key()); |
| 72 } |
| 73 |
| 74 private: |
| 75 std::unique_ptr<SymmetricKey> key = |
| 76 SymmetricKey::GenerateRandomKey(kPaddingKeyAlgorithm, 128); |
| 77 }; |
| 78 static LazyInstance<KeyOwner>::Leaky s_key_owner = LAZY_INSTANCE_INITIALIZER; |
| 79 return s_key_owner.Get().CreateDuplicate(); |
| 80 } |
| 81 |
| 82 std::unique_ptr<SymmetricKey> ImportPaddingKey(const std::string& raw_key) { |
| 83 return SymmetricKey::Import(kPaddingKeyAlgorithm, raw_key); |
| 84 } |
| 85 |
| 60 } // namespace | 86 } // namespace |
| 61 | 87 |
| 62 const char CacheStorage::kIndexFileName[] = "index.txt"; | 88 const char CacheStorage::kIndexFileName[] = "index.txt"; |
| 63 constexpr int64_t CacheStorage::kSizeUnknown; | 89 constexpr int64_t CacheStorage::kSizeUnknown; |
| 64 | 90 |
| 65 struct CacheStorage::CacheMatchResponse { | 91 struct CacheStorage::CacheMatchResponse { |
| 66 CacheMatchResponse() = default; | 92 CacheMatchResponse() = default; |
| 67 ~CacheMatchResponse() = default; | 93 ~CacheMatchResponse() = default; |
| 68 | 94 |
| 69 CacheStorageError error; | 95 CacheStorageError error; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 95 origin_(origin) { | 121 origin_(origin) { |
| 96 DCHECK(!origin_.is_empty()); | 122 DCHECK(!origin_.is_empty()); |
| 97 } | 123 } |
| 98 | 124 |
| 99 virtual ~CacheLoader() {} | 125 virtual ~CacheLoader() {} |
| 100 | 126 |
| 101 // Creates a CacheStorageCache with the given name. It does not attempt to | 127 // Creates a CacheStorageCache with the given name. It does not attempt to |
| 102 // load the backend, that happens lazily when the cache is used. | 128 // load the backend, that happens lazily when the cache is used. |
| 103 virtual std::unique_ptr<CacheStorageCache> CreateCache( | 129 virtual std::unique_ptr<CacheStorageCache> CreateCache( |
| 104 const std::string& cache_name, | 130 const std::string& cache_name, |
| 105 int64_t cache_size) = 0; | 131 int64_t cache_size, |
| 132 int64_t cache_padding, |
| 133 std::unique_ptr<SymmetricKey> cache_padding_key) = 0; |
| 106 | 134 |
| 107 // Deletes any pre-existing cache of the same name and then loads it. | 135 // Deletes any pre-existing cache of the same name and then loads it. |
| 108 virtual void PrepareNewCacheDestination(const std::string& cache_name, | 136 virtual void PrepareNewCacheDestination(const std::string& cache_name, |
| 109 CacheCallback callback) = 0; | 137 CacheCallback callback) = 0; |
| 110 | 138 |
| 111 // After the backend has been deleted, do any extra house keeping such as | 139 // After the backend has been deleted, do any extra house keeping such as |
| 112 // removing the cache's directory. | 140 // removing the cache's directory. |
| 113 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; | 141 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; |
| 114 | 142 |
| 115 // Writes the cache index to disk if applicable. | 143 // 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, | 185 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 158 CacheStorage* cache_storage, | 186 CacheStorage* cache_storage, |
| 159 const GURL& origin) | 187 const GURL& origin) |
| 160 : CacheLoader(cache_task_runner, | 188 : CacheLoader(cache_task_runner, |
| 161 request_context, | 189 request_context, |
| 162 quota_manager_proxy, | 190 quota_manager_proxy, |
| 163 blob_context, | 191 blob_context, |
| 164 cache_storage, | 192 cache_storage, |
| 165 origin) {} | 193 origin) {} |
| 166 | 194 |
| 167 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, | 195 std::unique_ptr<CacheStorageCache> CreateCache( |
| 168 int64_t cache_size) override { | 196 const std::string& cache_name, |
| 197 int64_t cache_size, |
| 198 int64_t cache_padding, |
| 199 std::unique_ptr<SymmetricKey> cache_padding_key) override { |
| 169 return CacheStorageCache::CreateMemoryCache( | 200 return CacheStorageCache::CreateMemoryCache( |
| 170 origin_, cache_name, cache_storage_, request_context_getter_, | 201 origin_, cache_name, cache_storage_, request_context_getter_, |
| 171 quota_manager_proxy_, blob_context_); | 202 quota_manager_proxy_, blob_context_, SessionPaddingKey()); |
| 172 } | 203 } |
| 173 | 204 |
| 174 void PrepareNewCacheDestination(const std::string& cache_name, | 205 void PrepareNewCacheDestination(const std::string& cache_name, |
| 175 CacheCallback callback) override { | 206 CacheCallback callback) override { |
| 176 std::unique_ptr<CacheStorageCache> cache = | 207 std::unique_ptr<CacheStorageCache> cache = |
| 177 CreateCache(cache_name, 0 /*cache_size*/); | 208 CreateCache(cache_name, 0 /*cache_size*/, 0 /* cache_padding */, |
| 209 SessionPaddingKey()); |
| 178 std::move(callback).Run(std::move(cache)); | 210 std::move(callback).Run(std::move(cache)); |
| 179 } | 211 } |
| 180 | 212 |
| 181 void CleanUpDeletedCache(CacheStorageCache* cache) override {} | 213 void CleanUpDeletedCache(CacheStorageCache* cache) override {} |
| 182 | 214 |
| 183 void WriteIndex(const CacheStorageIndex& index, | 215 void WriteIndex(const CacheStorageIndex& index, |
| 184 BoolCallback callback) override { | 216 BoolCallback callback) override { |
| 185 std::move(callback).Run(true); | 217 std::move(callback).Run(true); |
| 186 } | 218 } |
| 187 | 219 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 const GURL& origin) | 257 const GURL& origin) |
| 226 : CacheLoader(cache_task_runner, | 258 : CacheLoader(cache_task_runner, |
| 227 request_context, | 259 request_context, |
| 228 quota_manager_proxy, | 260 quota_manager_proxy, |
| 229 blob_context, | 261 blob_context, |
| 230 cache_storage, | 262 cache_storage, |
| 231 origin), | 263 origin), |
| 232 origin_path_(origin_path), | 264 origin_path_(origin_path), |
| 233 weak_ptr_factory_(this) {} | 265 weak_ptr_factory_(this) {} |
| 234 | 266 |
| 235 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, | 267 std::unique_ptr<CacheStorageCache> CreateCache( |
| 236 int64_t cache_size) override { | 268 const std::string& cache_name, |
| 269 int64_t cache_size, |
| 270 int64_t cache_padding, |
| 271 std::unique_ptr<SymmetricKey> cache_padding_key) override { |
| 237 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 272 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 238 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); | 273 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 239 | 274 |
| 240 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; | 275 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 241 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); | 276 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 242 return CacheStorageCache::CreatePersistentCache( | 277 return CacheStorageCache::CreatePersistentCache( |
| 243 origin_, cache_name, cache_storage_, cache_path, | 278 origin_, cache_name, cache_storage_, cache_path, |
| 244 request_context_getter_, quota_manager_proxy_, blob_context_, | 279 request_context_getter_, quota_manager_proxy_, blob_context_, |
| 245 cache_size); | 280 cache_size, cache_padding, std::move(cache_padding_key)); |
| 246 } | 281 } |
| 247 | 282 |
| 248 void PrepareNewCacheDestination(const std::string& cache_name, | 283 void PrepareNewCacheDestination(const std::string& cache_name, |
| 249 CacheCallback callback) override { | 284 CacheCallback callback) override { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 285 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 251 | 286 |
| 252 PostTaskAndReplyWithResult( | 287 PostTaskAndReplyWithResult( |
| 253 cache_task_runner_.get(), FROM_HERE, | 288 cache_task_runner_.get(), FROM_HERE, |
| 254 base::BindOnce(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, | 289 base::BindOnce(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 255 origin_path_), | 290 origin_path_), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 273 | 308 |
| 274 void PrepareNewCacheCreateCache(const std::string& cache_name, | 309 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 275 CacheCallback callback, | 310 CacheCallback callback, |
| 276 const std::string& cache_dir) { | 311 const std::string& cache_dir) { |
| 277 if (cache_dir.empty()) { | 312 if (cache_dir.empty()) { |
| 278 std::move(callback).Run(std::unique_ptr<CacheStorageCache>()); | 313 std::move(callback).Run(std::unique_ptr<CacheStorageCache>()); |
| 279 return; | 314 return; |
| 280 } | 315 } |
| 281 | 316 |
| 282 cache_name_to_cache_dir_[cache_name] = cache_dir; | 317 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 283 std::move(callback).Run( | 318 std::move(callback).Run(CreateCache(cache_name, CacheStorage::kSizeUnknown, |
| 284 CreateCache(cache_name, CacheStorage::kSizeUnknown)); | 319 CacheStorage::kSizeUnknown, |
| 320 SessionPaddingKey())); |
| 285 } | 321 } |
| 286 | 322 |
| 287 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 323 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 288 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 324 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 289 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); | 325 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 290 | 326 |
| 291 base::FilePath cache_path = | 327 base::FilePath cache_path = |
| 292 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 328 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 293 doomed_cache_to_path_.erase(cache); | 329 doomed_cache_to_path_.erase(cache); |
| 294 | 330 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 315 for (const auto& cache_metadata : index.ordered_cache_metadata()) { | 351 for (const auto& cache_metadata : index.ordered_cache_metadata()) { |
| 316 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); | 352 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_metadata.name)); |
| 317 | 353 |
| 318 proto::CacheStorageIndex::Cache* index_cache = protobuf_index.add_cache(); | 354 proto::CacheStorageIndex::Cache* index_cache = protobuf_index.add_cache(); |
| 319 index_cache->set_name(cache_metadata.name); | 355 index_cache->set_name(cache_metadata.name); |
| 320 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_metadata.name]); | 356 index_cache->set_cache_dir(cache_name_to_cache_dir_[cache_metadata.name]); |
| 321 if (cache_metadata.size == CacheStorage::kSizeUnknown) | 357 if (cache_metadata.size == CacheStorage::kSizeUnknown) |
| 322 index_cache->clear_size(); | 358 index_cache->clear_size(); |
| 323 else | 359 else |
| 324 index_cache->set_size(cache_metadata.size); | 360 index_cache->set_size(cache_metadata.size); |
| 361 index_cache->set_padding_key(cache_metadata.padding_key); |
| 362 index_cache->set_padding(cache_metadata.padding); |
| 363 index_cache->set_padding_version( |
| 364 CacheStorageCache::GetResponsePaddingVersion()); |
| 325 } | 365 } |
| 326 | 366 |
| 327 std::string serialized; | 367 std::string serialized; |
| 328 bool success = protobuf_index.SerializeToString(&serialized); | 368 bool success = protobuf_index.SerializeToString(&serialized); |
| 329 DCHECK(success); | 369 DCHECK(success); |
| 330 | 370 |
| 331 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 371 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
| 332 base::FilePath index_path = | 372 base::FilePath index_path = |
| 333 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 373 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
| 334 | 374 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 409 |
| 370 std::unique_ptr<std::set<std::string>> cache_dirs( | 410 std::unique_ptr<std::set<std::string>> cache_dirs( |
| 371 new std::set<std::string>); | 411 new std::set<std::string>); |
| 372 | 412 |
| 373 auto index = base::MakeUnique<CacheStorageIndex>(); | 413 auto index = base::MakeUnique<CacheStorageIndex>(); |
| 374 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { | 414 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { |
| 375 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); | 415 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); |
| 376 DCHECK(cache.has_cache_dir()); | 416 DCHECK(cache.has_cache_dir()); |
| 377 int64_t cache_size = | 417 int64_t cache_size = |
| 378 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; | 418 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; |
| 379 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); | 419 int64_t cache_padding; |
| 420 if (cache.has_padding()) { |
| 421 if (cache.has_padding_version() && |
| 422 cache.padding_version() == |
| 423 CacheStorageCache::GetResponsePaddingVersion()) { |
| 424 cache_padding = cache.padding(); |
| 425 } else { |
| 426 // The padding algorithm version changed so set to unknown to force |
| 427 // recalculation. |
| 428 cache_padding = CacheStorage::kSizeUnknown; |
| 429 } |
| 430 } else { |
| 431 cache_padding = CacheStorage::kSizeUnknown; |
| 432 } |
| 433 |
| 434 std::string cache_padding_key = cache.has_padding_key() |
| 435 ? cache.padding_key() |
| 436 : SessionPaddingKey()->key(); |
| 437 |
| 438 index->Insert(CacheStorageIndex::CacheMetadata( |
| 439 cache.name(), cache_size, cache_padding, |
| 440 std::move(cache_padding_key))); |
| 380 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 441 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 381 cache_dirs->insert(cache.cache_dir()); | 442 cache_dirs->insert(cache.cache_dir()); |
| 382 } | 443 } |
| 383 | 444 |
| 384 cache_task_runner_->PostTask( | 445 cache_task_runner_->PostTask( |
| 385 FROM_HERE, base::BindOnce(&DeleteUnreferencedCachesInPool, origin_path_, | 446 FROM_HERE, base::BindOnce(&DeleteUnreferencedCachesInPool, origin_path_, |
| 386 base::Passed(&cache_dirs))); | 447 base::Passed(&cache_dirs))); |
| 387 std::move(callback).Run(std::move(index)); | 448 std::move(callback).Run(std::move(index)); |
| 388 } | 449 } |
| 389 | 450 |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 777 | 838 |
| 778 if (!cache) { | 839 if (!cache) { |
| 779 std::move(callback).Run(std::unique_ptr<CacheStorageCacheHandle>(), | 840 std::move(callback).Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 780 CACHE_STORAGE_ERROR_STORAGE); | 841 CACHE_STORAGE_ERROR_STORAGE); |
| 781 return; | 842 return; |
| 782 } | 843 } |
| 783 | 844 |
| 784 CacheStorageCache* cache_ptr = cache.get(); | 845 CacheStorageCache* cache_ptr = cache.get(); |
| 785 | 846 |
| 786 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 847 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 787 cache_index_->Insert( | 848 cache_index_->Insert(CacheStorageIndex::CacheMetadata( |
| 788 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); | 849 cache_name, cache_ptr->cache_size(), cache_ptr->cache_padding(), |
| 850 cache_ptr->cache_padding_key()->key())); |
| 789 | 851 |
| 790 cache_loader_->WriteIndex( | 852 cache_loader_->WriteIndex( |
| 791 *cache_index_, | 853 *cache_index_, |
| 792 base::BindOnce(&CacheStorage::CreateCacheDidWriteIndex, | 854 base::BindOnce(&CacheStorage::CreateCacheDidWriteIndex, |
| 793 weak_factory_.GetWeakPtr(), std::move(callback), | 855 weak_factory_.GetWeakPtr(), std::move(callback), |
| 794 base::Passed(CreateCacheHandle(cache_ptr)))); | 856 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 795 | 857 |
| 796 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | 858 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); |
| 797 } | 859 } |
| 798 | 860 |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1090 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1029 DCHECK(initialized_); | 1091 DCHECK(initialized_); |
| 1030 | 1092 |
| 1031 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 1093 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 1032 if (map_iter == cache_map_.end()) | 1094 if (map_iter == cache_map_.end()) |
| 1033 return std::unique_ptr<CacheStorageCacheHandle>(); | 1095 return std::unique_ptr<CacheStorageCacheHandle>(); |
| 1034 | 1096 |
| 1035 CacheStorageCache* cache = map_iter->second.get(); | 1097 CacheStorageCache* cache = map_iter->second.get(); |
| 1036 | 1098 |
| 1037 if (!cache) { | 1099 if (!cache) { |
| 1100 const CacheStorageIndex::CacheMetadata* metadata = |
| 1101 cache_index_->FindMetadata(cache_name); |
| 1102 DCHECK(metadata); |
| 1038 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( | 1103 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( |
| 1039 cache_name, cache_index_->GetCacheSize(cache_name)); | 1104 cache_name, metadata->size, metadata->padding, |
| 1105 ImportPaddingKey(metadata->padding_key)); |
| 1040 CacheStorageCache* cache_ptr = new_cache.get(); | 1106 CacheStorageCache* cache_ptr = new_cache.get(); |
| 1041 map_iter->second = std::move(new_cache); | 1107 map_iter->second = std::move(new_cache); |
| 1042 | 1108 |
| 1043 return CreateCacheHandle(cache_ptr); | 1109 return CreateCacheHandle(cache_ptr); |
| 1044 } | 1110 } |
| 1045 | 1111 |
| 1046 return CreateCacheHandle(cache); | 1112 return CreateCacheHandle(cache); |
| 1047 } | 1113 } |
| 1048 | 1114 |
| 1049 void CacheStorage::SizeRetrievedFromCache( | 1115 void CacheStorage::SizeRetrievedFromCache( |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1076 &CacheStorage::SizeRetrievedFromCache, weak_factory_.GetWeakPtr(), | 1142 &CacheStorage::SizeRetrievedFromCache, weak_factory_.GetWeakPtr(), |
| 1077 base::Passed(std::move(cache_handle)), barrier_closure, | 1143 base::Passed(std::move(cache_handle)), barrier_closure, |
| 1078 accumulator_ptr)); | 1144 accumulator_ptr)); |
| 1079 } | 1145 } |
| 1080 } | 1146 } |
| 1081 | 1147 |
| 1082 void CacheStorage::SizeImpl(SizeCallback callback) { | 1148 void CacheStorage::SizeImpl(SizeCallback callback) { |
| 1083 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1149 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1084 DCHECK(initialized_); | 1150 DCHECK(initialized_); |
| 1085 | 1151 |
| 1086 if (cache_index_->GetStorageSize() != kSizeUnknown) { | 1152 if (cache_index_->GetPaddedStorageSize() != kSizeUnknown) { |
| 1087 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1153 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1088 FROM_HERE, | 1154 FROM_HERE, base::BindOnce(std::move(callback), |
| 1089 base::BindOnce(std::move(callback), cache_index_->GetStorageSize())); | 1155 cache_index_->GetPaddedStorageSize())); |
| 1090 return; | 1156 return; |
| 1091 } | 1157 } |
| 1092 | 1158 |
| 1093 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1159 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1094 int64_t* accumulator_ptr = accumulator.get(); | 1160 int64_t* accumulator_ptr = accumulator.get(); |
| 1095 | 1161 |
| 1096 base::RepeatingClosure barrier_closure = | 1162 base::RepeatingClosure barrier_closure = |
| 1097 base::BarrierClosure(cache_index_->num_entries(), | 1163 base::BarrierClosure(cache_index_->num_entries(), |
| 1098 base::BindOnce(&SizeRetrievedFromAllCaches, | 1164 base::BindOnce(&SizeRetrievedFromAllCaches, |
| 1099 base::Passed(std::move(accumulator)), | 1165 base::Passed(std::move(accumulator)), |
| 1100 std::move(callback))); | 1166 std::move(callback))); |
| 1101 | 1167 |
| 1102 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 1168 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1103 if (cache_metadata.size != CacheStorage::kSizeUnknown) { | 1169 if (cache_metadata.size != CacheStorage::kSizeUnknown) { |
| 1104 *accumulator_ptr += cache_metadata.size; | 1170 *accumulator_ptr += cache_metadata.size; |
| 1105 barrier_closure.Run(); | 1171 barrier_closure.Run(); |
| 1106 continue; | 1172 continue; |
| 1107 } | 1173 } |
| 1108 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1174 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1109 GetLoadedCache(cache_metadata.name); | 1175 GetLoadedCache(cache_metadata.name); |
| 1110 CacheStorageCache* cache = cache_handle->value(); | 1176 CacheStorageCache* cache = cache_handle->value(); |
| 1111 cache->Size(base::BindOnce(&CacheStorage::SizeRetrievedFromCache, | 1177 cache->Size(base::BindOnce(&CacheStorage::SizeRetrievedFromCache, |
| 1112 weak_factory_.GetWeakPtr(), | 1178 weak_factory_.GetWeakPtr(), |
| 1113 base::Passed(std::move(cache_handle)), | 1179 base::Passed(std::move(cache_handle)), |
| 1114 barrier_closure, accumulator_ptr)); | 1180 barrier_closure, accumulator_ptr)); |
| 1115 } | 1181 } |
| 1116 } | 1182 } |
| 1117 | 1183 |
| 1118 } // namespace content | 1184 } // namespace content |
| OLD | NEW |