| 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 const CacheStorage::SizeCallback& callback) { | 60 const CacheStorage::SizeCallback& callback) { |
| 54 base::ThreadTaskRunnerHandle::Get()->PostTask( | 61 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 55 FROM_HERE, base::Bind(callback, *accumulator)); | 62 FROM_HERE, base::Bind(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 const CacheCallback& callback) = 0; | 137 const 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 const CacheCallback& callback) override { | 206 const 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 callback.Run(std::move(cache)); | 210 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 const BoolCallback& callback) override { | 216 const BoolCallback& callback) override { |
| 185 callback.Run(true); | 217 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 const CacheCallback& callback) override { | 284 const 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::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, | 289 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 255 origin_path_), | 290 origin_path_), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 272 | 307 |
| 273 void PrepareNewCacheCreateCache(const std::string& cache_name, | 308 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 274 const CacheCallback& callback, | 309 const CacheCallback& callback, |
| 275 const std::string& cache_dir) { | 310 const std::string& cache_dir) { |
| 276 if (cache_dir.empty()) { | 311 if (cache_dir.empty()) { |
| 277 callback.Run(std::unique_ptr<CacheStorageCache>()); | 312 callback.Run(std::unique_ptr<CacheStorageCache>()); |
| 278 return; | 313 return; |
| 279 } | 314 } |
| 280 | 315 |
| 281 cache_name_to_cache_dir_[cache_name] = cache_dir; | 316 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 282 callback.Run(CreateCache(cache_name, CacheStorage::kSizeUnknown)); | 317 callback.Run(CreateCache(cache_name, CacheStorage::kSizeUnknown, |
| 318 CacheStorage::kSizeUnknown, SessionPaddingKey())); |
| 283 } | 319 } |
| 284 | 320 |
| 285 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 321 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 322 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 287 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); | 323 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 288 | 324 |
| 289 base::FilePath cache_path = | 325 base::FilePath cache_path = |
| 290 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 326 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 291 doomed_cache_to_path_.erase(cache); | 327 doomed_cache_to_path_.erase(cache); |
| 292 | 328 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 401 |
| 366 std::unique_ptr<std::set<std::string>> cache_dirs( | 402 std::unique_ptr<std::set<std::string>> cache_dirs( |
| 367 new std::set<std::string>); | 403 new std::set<std::string>); |
| 368 | 404 |
| 369 auto index = base::MakeUnique<CacheStorageIndex>(); | 405 auto index = base::MakeUnique<CacheStorageIndex>(); |
| 370 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { | 406 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { |
| 371 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); | 407 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); |
| 372 DCHECK(cache.has_cache_dir()); | 408 DCHECK(cache.has_cache_dir()); |
| 373 int64_t cache_size = | 409 int64_t cache_size = |
| 374 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; | 410 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; |
| 375 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); | 411 int64_t cache_padding = |
| 412 cache.has_padding() ? cache.padding() : CacheStorage::kSizeUnknown; |
| 413 std::string cache_padding_key = cache.has_padding_key() |
| 414 ? cache.padding_key() |
| 415 : SessionPaddingKey()->key(); |
| 416 |
| 417 index->Insert(CacheStorageIndex::CacheMetadata( |
| 418 cache.name(), cache_size, cache_padding, |
| 419 std::move(cache_padding_key))); |
| 376 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 420 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 377 cache_dirs->insert(cache.cache_dir()); | 421 cache_dirs->insert(cache.cache_dir()); |
| 378 } | 422 } |
| 379 | 423 |
| 380 cache_task_runner_->PostTask( | 424 cache_task_runner_->PostTask( |
| 381 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | 425 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, |
| 382 base::Passed(&cache_dirs))); | 426 base::Passed(&cache_dirs))); |
| 383 callback.Run(std::move(index)); | 427 callback.Run(std::move(index)); |
| 384 } | 428 } |
| 385 | 429 |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 | 816 |
| 773 if (!cache) { | 817 if (!cache) { |
| 774 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), | 818 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 775 CACHE_STORAGE_ERROR_STORAGE); | 819 CACHE_STORAGE_ERROR_STORAGE); |
| 776 return; | 820 return; |
| 777 } | 821 } |
| 778 | 822 |
| 779 CacheStorageCache* cache_ptr = cache.get(); | 823 CacheStorageCache* cache_ptr = cache.get(); |
| 780 | 824 |
| 781 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 825 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 782 cache_index_->Insert( | 826 cache_index_->Insert(CacheStorageIndex::CacheMetadata( |
| 783 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); | 827 cache_name, cache_ptr->cache_size(), cache_ptr->cache_padding(), |
| 828 cache_ptr->cache_padding_key()->key())); |
| 784 | 829 |
| 785 cache_loader_->WriteIndex( | 830 cache_loader_->WriteIndex( |
| 786 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex, | 831 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex, |
| 787 weak_factory_.GetWeakPtr(), callback, | 832 weak_factory_.GetWeakPtr(), callback, |
| 788 base::Passed(CreateCacheHandle(cache_ptr)))); | 833 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 789 | 834 |
| 790 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | 835 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); |
| 791 } | 836 } |
| 792 | 837 |
| 793 void CacheStorage::CreateCacheDidWriteIndex( | 838 void CacheStorage::CreateCacheDidWriteIndex( |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1064 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1020 DCHECK(initialized_); | 1065 DCHECK(initialized_); |
| 1021 | 1066 |
| 1022 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 1067 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 1023 if (map_iter == cache_map_.end()) | 1068 if (map_iter == cache_map_.end()) |
| 1024 return std::unique_ptr<CacheStorageCacheHandle>(); | 1069 return std::unique_ptr<CacheStorageCacheHandle>(); |
| 1025 | 1070 |
| 1026 CacheStorageCache* cache = map_iter->second.get(); | 1071 CacheStorageCache* cache = map_iter->second.get(); |
| 1027 | 1072 |
| 1028 if (!cache) { | 1073 if (!cache) { |
| 1074 const CacheStorageIndex::CacheMetadata* metadata = |
| 1075 cache_index_->FindMetadata(cache_name); |
| 1076 DCHECK(metadata); |
| 1029 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( | 1077 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( |
| 1030 cache_name, cache_index_->GetCacheSize(cache_name)); | 1078 cache_name, metadata->size, metadata->padding, |
| 1079 ImportPaddingKey(metadata->padding_key)); |
| 1031 CacheStorageCache* cache_ptr = new_cache.get(); | 1080 CacheStorageCache* cache_ptr = new_cache.get(); |
| 1032 map_iter->second = std::move(new_cache); | 1081 map_iter->second = std::move(new_cache); |
| 1033 | 1082 |
| 1034 return CreateCacheHandle(cache_ptr); | 1083 return CreateCacheHandle(cache_ptr); |
| 1035 } | 1084 } |
| 1036 | 1085 |
| 1037 return CreateCacheHandle(cache); | 1086 return CreateCacheHandle(cache); |
| 1038 } | 1087 } |
| 1039 | 1088 |
| 1040 void CacheStorage::SizeRetrievedFromCache( | 1089 void CacheStorage::SizeRetrievedFromCache( |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1066 weak_factory_.GetWeakPtr(), | 1115 weak_factory_.GetWeakPtr(), |
| 1067 base::Passed(std::move(cache_handle)), | 1116 base::Passed(std::move(cache_handle)), |
| 1068 barrier_closure, accumulator_ptr)); | 1117 barrier_closure, accumulator_ptr)); |
| 1069 } | 1118 } |
| 1070 } | 1119 } |
| 1071 | 1120 |
| 1072 void CacheStorage::SizeImpl(const SizeCallback& callback) { | 1121 void CacheStorage::SizeImpl(const SizeCallback& callback) { |
| 1073 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1122 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1074 DCHECK(initialized_); | 1123 DCHECK(initialized_); |
| 1075 | 1124 |
| 1076 if (cache_index_->GetStorageSize() != kSizeUnknown) { | 1125 if (cache_index_->GetPaddedStorageSize() != kSizeUnknown) { |
| 1077 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1126 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1078 FROM_HERE, base::Bind(callback, cache_index_->GetStorageSize())); | 1127 FROM_HERE, base::Bind(callback, cache_index_->GetPaddedStorageSize())); |
| 1079 return; | 1128 return; |
| 1080 } | 1129 } |
| 1081 | 1130 |
| 1082 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1131 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1083 int64_t* accumulator_ptr = accumulator.get(); | 1132 int64_t* accumulator_ptr = accumulator.get(); |
| 1084 | 1133 |
| 1085 base::Closure barrier_closure = base::BarrierClosure( | 1134 base::Closure barrier_closure = base::BarrierClosure( |
| 1086 cache_index_->num_entries(), | 1135 cache_index_->num_entries(), |
| 1087 base::Bind(&SizeRetrievedFromAllCaches, | 1136 base::Bind(&SizeRetrievedFromAllCaches, |
| 1088 base::Passed(std::move(accumulator)), callback)); | 1137 base::Passed(std::move(accumulator)), callback)); |
| 1089 | 1138 |
| 1090 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 1139 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1091 if (cache_metadata.size != CacheStorage::kSizeUnknown) { | 1140 if (cache_metadata.size != CacheStorage::kSizeUnknown) { |
| 1092 *accumulator_ptr += cache_metadata.size; | 1141 *accumulator_ptr += cache_metadata.size; |
| 1093 barrier_closure.Run(); | 1142 barrier_closure.Run(); |
| 1094 continue; | 1143 continue; |
| 1095 } | 1144 } |
| 1096 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1145 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1097 GetLoadedCache(cache_metadata.name); | 1146 GetLoadedCache(cache_metadata.name); |
| 1098 CacheStorageCache* cache = cache_handle->value(); | 1147 CacheStorageCache* cache = cache_handle->value(); |
| 1099 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache, | 1148 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache, |
| 1100 weak_factory_.GetWeakPtr(), | 1149 weak_factory_.GetWeakPtr(), |
| 1101 base::Passed(std::move(cache_handle)), | 1150 base::Passed(std::move(cache_handle)), |
| 1102 barrier_closure, accumulator_ptr)); | 1151 barrier_closure, accumulator_ptr)); |
| 1103 } | 1152 } |
| 1104 } | 1153 } |
| 1105 | 1154 |
| 1106 } // namespace content | 1155 } // namespace content |
| OLD | NEW |