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> |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 origin_(origin) { | 95 origin_(origin) { |
| 96 DCHECK(!origin_.is_empty()); | 96 DCHECK(!origin_.is_empty()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 virtual ~CacheLoader() {} | 99 virtual ~CacheLoader() {} |
| 100 | 100 |
| 101 // Creates a CacheStorageCache with the given name. It does not attempt to | 101 // Creates a CacheStorageCache with the given name. It does not attempt to |
| 102 // load the backend, that happens lazily when the cache is used. | 102 // load the backend, that happens lazily when the cache is used. |
| 103 virtual std::unique_ptr<CacheStorageCache> CreateCache( | 103 virtual std::unique_ptr<CacheStorageCache> CreateCache( |
| 104 const std::string& cache_name, | 104 const std::string& cache_name, |
| 105 int64_t cache_size) = 0; | 105 int64_t cache_size, |
| 106 int64_t cache_paddinge, | |
|
jkarlin
2017/06/08 18:58:48
cache_padding
cmumford
2017/06/12 18:09:30
Done.
| |
| 107 const std::string& cache_padding_key) = 0; | |
| 106 | 108 |
| 107 // Deletes any pre-existing cache of the same name and then loads it. | 109 // Deletes any pre-existing cache of the same name and then loads it. |
| 108 virtual void PrepareNewCacheDestination(const std::string& cache_name, | 110 virtual void PrepareNewCacheDestination(const std::string& cache_name, |
| 109 const CacheCallback& callback) = 0; | 111 const CacheCallback& callback) = 0; |
| 110 | 112 |
| 111 // After the backend has been deleted, do any extra house keeping such as | 113 // After the backend has been deleted, do any extra house keeping such as |
| 112 // removing the cache's directory. | 114 // removing the cache's directory. |
| 113 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; | 115 virtual void CleanUpDeletedCache(CacheStorageCache* cache) = 0; |
| 114 | 116 |
| 115 // Writes the cache index to disk if applicable. | 117 // 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, | 159 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 158 CacheStorage* cache_storage, | 160 CacheStorage* cache_storage, |
| 159 const GURL& origin) | 161 const GURL& origin) |
| 160 : CacheLoader(cache_task_runner, | 162 : CacheLoader(cache_task_runner, |
| 161 request_context, | 163 request_context, |
| 162 quota_manager_proxy, | 164 quota_manager_proxy, |
| 163 blob_context, | 165 blob_context, |
| 164 cache_storage, | 166 cache_storage, |
| 165 origin) {} | 167 origin) {} |
| 166 | 168 |
| 167 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, | 169 std::unique_ptr<CacheStorageCache> CreateCache( |
| 168 int64_t cache_size) override { | 170 const std::string& cache_name, |
| 171 int64_t cache_size, | |
| 172 int64_t cache_padding, | |
| 173 const std::string& cache_padding_key) override { | |
| 169 return CacheStorageCache::CreateMemoryCache( | 174 return CacheStorageCache::CreateMemoryCache( |
| 170 origin_, cache_name, cache_storage_, request_context_getter_, | 175 origin_, cache_name, cache_storage_, request_context_getter_, |
| 171 quota_manager_proxy_, blob_context_); | 176 quota_manager_proxy_, blob_context_); |
| 172 } | 177 } |
| 173 | 178 |
| 174 void PrepareNewCacheDestination(const std::string& cache_name, | 179 void PrepareNewCacheDestination(const std::string& cache_name, |
| 175 const CacheCallback& callback) override { | 180 const CacheCallback& callback) override { |
| 176 std::unique_ptr<CacheStorageCache> cache = | 181 std::unique_ptr<CacheStorageCache> cache = |
| 177 CreateCache(cache_name, 0 /*cache_size*/); | 182 CreateCache(cache_name, 0 /*cache_size*/, 0 /* cache_padding */, |
| 183 CacheStorageCache::SessionPaddingHMACKey()); | |
| 178 callback.Run(std::move(cache)); | 184 callback.Run(std::move(cache)); |
| 179 } | 185 } |
| 180 | 186 |
| 181 void CleanUpDeletedCache(CacheStorageCache* cache) override {} | 187 void CleanUpDeletedCache(CacheStorageCache* cache) override {} |
| 182 | 188 |
| 183 void WriteIndex(const CacheStorageIndex& index, | 189 void WriteIndex(const CacheStorageIndex& index, |
| 184 const BoolCallback& callback) override { | 190 const BoolCallback& callback) override { |
| 185 callback.Run(true); | 191 callback.Run(true); |
| 186 } | 192 } |
| 187 | 193 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 const GURL& origin) | 231 const GURL& origin) |
| 226 : CacheLoader(cache_task_runner, | 232 : CacheLoader(cache_task_runner, |
| 227 request_context, | 233 request_context, |
| 228 quota_manager_proxy, | 234 quota_manager_proxy, |
| 229 blob_context, | 235 blob_context, |
| 230 cache_storage, | 236 cache_storage, |
| 231 origin), | 237 origin), |
| 232 origin_path_(origin_path), | 238 origin_path_(origin_path), |
| 233 weak_ptr_factory_(this) {} | 239 weak_ptr_factory_(this) {} |
| 234 | 240 |
| 235 std::unique_ptr<CacheStorageCache> CreateCache(const std::string& cache_name, | 241 std::unique_ptr<CacheStorageCache> CreateCache( |
| 236 int64_t cache_size) override { | 242 const std::string& cache_name, |
| 243 int64_t cache_size, | |
| 244 int64_t cache_padding, | |
| 245 const std::string& cache_padding_key) override { | |
| 237 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 246 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 238 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); | 247 DCHECK(base::ContainsKey(cache_name_to_cache_dir_, cache_name)); |
| 239 | 248 |
| 240 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; | 249 std::string cache_dir = cache_name_to_cache_dir_[cache_name]; |
| 241 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); | 250 base::FilePath cache_path = origin_path_.AppendASCII(cache_dir); |
| 242 return CacheStorageCache::CreatePersistentCache( | 251 return CacheStorageCache::CreatePersistentCache( |
| 243 origin_, cache_name, cache_storage_, cache_path, | 252 origin_, cache_name, cache_storage_, cache_path, |
| 244 request_context_getter_, quota_manager_proxy_, blob_context_, | 253 request_context_getter_, quota_manager_proxy_, blob_context_, |
| 245 cache_size); | 254 cache_size, cache_padding, cache_padding_key); |
| 246 } | 255 } |
| 247 | 256 |
| 248 void PrepareNewCacheDestination(const std::string& cache_name, | 257 void PrepareNewCacheDestination(const std::string& cache_name, |
| 249 const CacheCallback& callback) override { | 258 const CacheCallback& callback) override { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 259 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 251 | 260 |
| 252 PostTaskAndReplyWithResult( | 261 PostTaskAndReplyWithResult( |
| 253 cache_task_runner_.get(), FROM_HERE, | 262 cache_task_runner_.get(), FROM_HERE, |
| 254 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, | 263 base::Bind(&SimpleCacheLoader::PrepareNewCacheDirectoryInPool, |
| 255 origin_path_), | 264 origin_path_), |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 272 | 281 |
| 273 void PrepareNewCacheCreateCache(const std::string& cache_name, | 282 void PrepareNewCacheCreateCache(const std::string& cache_name, |
| 274 const CacheCallback& callback, | 283 const CacheCallback& callback, |
| 275 const std::string& cache_dir) { | 284 const std::string& cache_dir) { |
| 276 if (cache_dir.empty()) { | 285 if (cache_dir.empty()) { |
| 277 callback.Run(std::unique_ptr<CacheStorageCache>()); | 286 callback.Run(std::unique_ptr<CacheStorageCache>()); |
| 278 return; | 287 return; |
| 279 } | 288 } |
| 280 | 289 |
| 281 cache_name_to_cache_dir_[cache_name] = cache_dir; | 290 cache_name_to_cache_dir_[cache_name] = cache_dir; |
| 282 callback.Run(CreateCache(cache_name, CacheStorage::kSizeUnknown)); | 291 callback.Run(CreateCache(cache_name, CacheStorage::kSizeUnknown, |
| 292 CacheStorage::kSizeUnknown, | |
|
jkarlin
2017/06/08 18:58:48
Can't we use 0 for initial padding?
cmumford
2017/06/12 18:09:31
In CacheStorageCache::InitGotCacheSize we will onl
| |
| 293 CacheStorageCache::SessionPaddingHMACKey())); | |
| 283 } | 294 } |
| 284 | 295 |
| 285 void CleanUpDeletedCache(CacheStorageCache* cache) override { | 296 void CleanUpDeletedCache(CacheStorageCache* cache) override { |
| 286 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 297 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 287 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); | 298 DCHECK(base::ContainsKey(doomed_cache_to_path_, cache)); |
| 288 | 299 |
| 289 base::FilePath cache_path = | 300 base::FilePath cache_path = |
| 290 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); | 301 origin_path_.AppendASCII(doomed_cache_to_path_[cache]); |
| 291 doomed_cache_to_path_.erase(cache); | 302 doomed_cache_to_path_.erase(cache); |
| 292 | 303 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 | 376 |
| 366 std::unique_ptr<std::set<std::string>> cache_dirs( | 377 std::unique_ptr<std::set<std::string>> cache_dirs( |
| 367 new std::set<std::string>); | 378 new std::set<std::string>); |
| 368 | 379 |
| 369 auto index = base::MakeUnique<CacheStorageIndex>(); | 380 auto index = base::MakeUnique<CacheStorageIndex>(); |
| 370 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { | 381 for (int i = 0, max = protobuf_index.cache_size(); i < max; ++i) { |
| 371 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); | 382 const proto::CacheStorageIndex::Cache& cache = protobuf_index.cache(i); |
| 372 DCHECK(cache.has_cache_dir()); | 383 DCHECK(cache.has_cache_dir()); |
| 373 int64_t cache_size = | 384 int64_t cache_size = |
| 374 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; | 385 cache.has_size() ? cache.size() : CacheStorage::kSizeUnknown; |
| 375 index->Insert(CacheStorageIndex::CacheMetadata(cache.name(), cache_size)); | 386 int64_t cache_padding = |
| 387 cache.has_padding() ? cache.padding() : CacheStorage::kSizeUnknown; | |
| 388 std::string cache_padding_key = | |
| 389 cache.has_padding_key() ? cache.padding_key() | |
| 390 : CacheStorageCache::SessionPaddingHMACKey(); | |
| 391 | |
| 392 index->Insert(CacheStorageIndex::CacheMetadata( | |
| 393 cache.name(), cache_size, cache_padding, cache_padding_key)); | |
| 376 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); | 394 cache_name_to_cache_dir_[cache.name()] = cache.cache_dir(); |
| 377 cache_dirs->insert(cache.cache_dir()); | 395 cache_dirs->insert(cache.cache_dir()); |
| 378 } | 396 } |
| 379 | 397 |
| 380 cache_task_runner_->PostTask( | 398 cache_task_runner_->PostTask( |
| 381 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, | 399 FROM_HERE, base::Bind(&DeleteUnreferencedCachesInPool, origin_path_, |
| 382 base::Passed(&cache_dirs))); | 400 base::Passed(&cache_dirs))); |
| 383 callback.Run(std::move(index)); | 401 callback.Run(std::move(index)); |
| 384 } | 402 } |
| 385 | 403 |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 772 | 790 |
| 773 if (!cache) { | 791 if (!cache) { |
| 774 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), | 792 callback.Run(std::unique_ptr<CacheStorageCacheHandle>(), |
| 775 CACHE_STORAGE_ERROR_STORAGE); | 793 CACHE_STORAGE_ERROR_STORAGE); |
| 776 return; | 794 return; |
| 777 } | 795 } |
| 778 | 796 |
| 779 CacheStorageCache* cache_ptr = cache.get(); | 797 CacheStorageCache* cache_ptr = cache.get(); |
| 780 | 798 |
| 781 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); | 799 cache_map_.insert(std::make_pair(cache_name, std::move(cache))); |
| 782 cache_index_->Insert( | 800 cache_index_->Insert(CacheStorageIndex::CacheMetadata( |
| 783 CacheStorageIndex::CacheMetadata(cache_name, cache_ptr->cache_size())); | 801 cache_name, cache_ptr->cache_size(), cache_ptr->cache_padding(), |
| 802 cache_ptr->cache_padding_key())); | |
| 784 | 803 |
| 785 cache_loader_->WriteIndex( | 804 cache_loader_->WriteIndex( |
| 786 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex, | 805 *cache_index_, base::Bind(&CacheStorage::CreateCacheDidWriteIndex, |
| 787 weak_factory_.GetWeakPtr(), callback, | 806 weak_factory_.GetWeakPtr(), callback, |
| 788 base::Passed(CreateCacheHandle(cache_ptr)))); | 807 base::Passed(CreateCacheHandle(cache_ptr)))); |
| 789 | 808 |
| 790 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); | 809 cache_loader_->NotifyCacheCreated(cache_name, CreateCacheHandle(cache_ptr)); |
| 791 } | 810 } |
| 792 | 811 |
| 793 void CacheStorage::CreateCacheDidWriteIndex( | 812 void CacheStorage::CreateCacheDidWriteIndex( |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1019 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1038 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1020 DCHECK(initialized_); | 1039 DCHECK(initialized_); |
| 1021 | 1040 |
| 1022 CacheMap::iterator map_iter = cache_map_.find(cache_name); | 1041 CacheMap::iterator map_iter = cache_map_.find(cache_name); |
| 1023 if (map_iter == cache_map_.end()) | 1042 if (map_iter == cache_map_.end()) |
| 1024 return std::unique_ptr<CacheStorageCacheHandle>(); | 1043 return std::unique_ptr<CacheStorageCacheHandle>(); |
| 1025 | 1044 |
| 1026 CacheStorageCache* cache = map_iter->second.get(); | 1045 CacheStorageCache* cache = map_iter->second.get(); |
| 1027 | 1046 |
| 1028 if (!cache) { | 1047 if (!cache) { |
| 1048 const CacheStorageIndex::CacheMetadata* metadata = | |
| 1049 cache_index_->FindMetadata(cache_name); | |
| 1050 DCHECK_NE(metadata, nullptr); | |
|
jkarlin
2017/06/08 18:58:48
DCHECK(metadata);
cmumford
2017/06/12 18:09:31
Done.
| |
| 1029 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( | 1051 std::unique_ptr<CacheStorageCache> new_cache = cache_loader_->CreateCache( |
| 1030 cache_name, cache_index_->GetCacheSize(cache_name)); | 1052 cache_name, metadata->size, metadata->padding, metadata->padding_key); |
| 1031 CacheStorageCache* cache_ptr = new_cache.get(); | 1053 CacheStorageCache* cache_ptr = new_cache.get(); |
| 1032 map_iter->second = std::move(new_cache); | 1054 map_iter->second = std::move(new_cache); |
| 1033 | 1055 |
| 1034 return CreateCacheHandle(cache_ptr); | 1056 return CreateCacheHandle(cache_ptr); |
| 1035 } | 1057 } |
| 1036 | 1058 |
| 1037 return CreateCacheHandle(cache); | 1059 return CreateCacheHandle(cache); |
| 1038 } | 1060 } |
| 1039 | 1061 |
| 1040 void CacheStorage::SizeRetrievedFromCache( | 1062 void CacheStorage::SizeRetrievedFromCache( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 1066 weak_factory_.GetWeakPtr(), | 1088 weak_factory_.GetWeakPtr(), |
| 1067 base::Passed(std::move(cache_handle)), | 1089 base::Passed(std::move(cache_handle)), |
| 1068 barrier_closure, accumulator_ptr)); | 1090 barrier_closure, accumulator_ptr)); |
| 1069 } | 1091 } |
| 1070 } | 1092 } |
| 1071 | 1093 |
| 1072 void CacheStorage::SizeImpl(const SizeCallback& callback) { | 1094 void CacheStorage::SizeImpl(const SizeCallback& callback) { |
| 1073 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1095 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1074 DCHECK(initialized_); | 1096 DCHECK(initialized_); |
| 1075 | 1097 |
| 1076 if (cache_index_->GetStorageSize() != kSizeUnknown) { | 1098 if (cache_index_->GetPaddedStorageSize() != kSizeUnknown) { |
| 1077 base::ThreadTaskRunnerHandle::Get()->PostTask( | 1099 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 1078 FROM_HERE, base::Bind(callback, cache_index_->GetStorageSize())); | 1100 FROM_HERE, base::Bind(callback, cache_index_->GetPaddedStorageSize())); |
| 1079 return; | 1101 return; |
| 1080 } | 1102 } |
| 1081 | 1103 |
| 1082 std::unique_ptr<int64_t> accumulator(new int64_t(0)); | 1104 std::unique_ptr<int64_t> accumulator(new int64_t(0)); |
| 1083 int64_t* accumulator_ptr = accumulator.get(); | 1105 int64_t* accumulator_ptr = accumulator.get(); |
| 1084 | 1106 |
| 1085 base::Closure barrier_closure = base::BarrierClosure( | 1107 base::Closure barrier_closure = base::BarrierClosure( |
| 1086 cache_index_->num_entries(), | 1108 cache_index_->num_entries(), |
| 1087 base::Bind(&SizeRetrievedFromAllCaches, | 1109 base::Bind(&SizeRetrievedFromAllCaches, |
| 1088 base::Passed(std::move(accumulator)), callback)); | 1110 base::Passed(std::move(accumulator)), callback)); |
| 1089 | 1111 |
| 1090 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { | 1112 for (const auto& cache_metadata : cache_index_->ordered_cache_metadata()) { |
| 1091 if (cache_metadata.size != CacheStorage::kSizeUnknown) { | 1113 if (cache_metadata.size != CacheStorage::kSizeUnknown) { |
| 1092 *accumulator_ptr += cache_metadata.size; | 1114 *accumulator_ptr += cache_metadata.size; |
| 1093 barrier_closure.Run(); | 1115 barrier_closure.Run(); |
| 1094 continue; | 1116 continue; |
| 1095 } | 1117 } |
| 1096 std::unique_ptr<CacheStorageCacheHandle> cache_handle = | 1118 std::unique_ptr<CacheStorageCacheHandle> cache_handle = |
| 1097 GetLoadedCache(cache_metadata.name); | 1119 GetLoadedCache(cache_metadata.name); |
| 1098 CacheStorageCache* cache = cache_handle->value(); | 1120 CacheStorageCache* cache = cache_handle->value(); |
| 1099 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache, | 1121 cache->Size(base::Bind(&CacheStorage::SizeRetrievedFromCache, |
| 1100 weak_factory_.GetWeakPtr(), | 1122 weak_factory_.GetWeakPtr(), |
| 1101 base::Passed(std::move(cache_handle)), | 1123 base::Passed(std::move(cache_handle)), |
| 1102 barrier_closure, accumulator_ptr)); | 1124 barrier_closure, accumulator_ptr)); |
| 1103 } | 1125 } |
| 1104 } | 1126 } |
| 1105 | 1127 |
| 1106 } // namespace content | 1128 } // namespace content |
| OLD | NEW |