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_manager.h" | 5 #include "content/browser/cache_storage/cache_storage_manager.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | |
| 12 #include <utility> | 11 #include <utility> |
| 13 | 12 |
| 14 #include "base/barrier_closure.h" | 13 #include "base/barrier_closure.h" |
| 15 #include "base/bind.h" | 14 #include "base/bind.h" |
| 16 #include "base/files/file_enumerator.h" | 15 #include "base/files/file_enumerator.h" |
| 17 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 18 #include "base/id_map.h" | 17 #include "base/id_map.h" |
| 19 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 20 #include "base/sha1.h" | 19 #include "base/sha1.h" |
| 21 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 44 void DeleteOriginDidDeleteDir( | 43 void DeleteOriginDidDeleteDir( |
| 45 const storage::QuotaClient::DeletionCallback& callback, | 44 const storage::QuotaClient::DeletionCallback& callback, |
| 46 bool rv) { | 45 bool rv) { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 48 | 47 |
| 49 base::ThreadTaskRunnerHandle::Get()->PostTask( | 48 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 50 FROM_HERE, base::Bind(callback, rv ? storage::kQuotaStatusOk | 49 FROM_HERE, base::Bind(callback, rv ? storage::kQuotaStatusOk |
| 51 : storage::kQuotaErrorAbort)); | 50 : storage::kQuotaErrorAbort)); |
| 52 } | 51 } |
| 53 | 52 |
| 54 // Open the various cache directories' index files and extract their origins and | 53 // Calculate the sum of all cache sizes in this store, but only if all sizes are |
| 55 // last modified times. | 54 // known. If one or more sizes are not known then return kSizeUnknown. |
| 55 int64_t GetCacheStorageSize(const proto::CacheStorageIndex& index) { | |
| 56 int64_t storage_size = 0; | |
| 57 for (int i = 0, max = index.cache_size(); i < max; ++i) { | |
| 58 const proto::CacheStorageIndex::Cache& cache = index.cache(i); | |
| 59 if (!cache.has_size() || cache.size() == CacheStorage::kSizeUnknown) | |
| 60 return CacheStorage::kSizeUnknown; | |
| 61 storage_size += cache.size(); | |
| 62 } | |
| 63 return storage_size; | |
| 64 } | |
| 65 | |
| 66 // Open the various cache directories' index files and extract their origins, | |
| 67 // sizes (if current), and last modified times. | |
| 56 void ListOriginsAndLastModifiedOnTaskRunner( | 68 void ListOriginsAndLastModifiedOnTaskRunner( |
| 57 std::vector<CacheStorageUsageInfo>* usages, | 69 std::vector<CacheStorageUsageInfo>* usages, |
| 58 base::FilePath root_path) { | 70 base::FilePath root_path) { |
| 59 base::FileEnumerator file_enum(root_path, false /* recursive */, | 71 base::FileEnumerator file_enum(root_path, false /* recursive */, |
| 60 base::FileEnumerator::DIRECTORIES); | 72 base::FileEnumerator::DIRECTORIES); |
| 61 | 73 |
| 62 base::FilePath path; | 74 base::FilePath path; |
| 63 while (!(path = file_enum.Next()).empty()) { | 75 while (!(path = file_enum.Next()).empty()) { |
| 76 base::FilePath index_path = path.AppendASCII(CacheStorage::kIndexFileName); | |
| 77 base::File::Info file_info; | |
| 78 base::Time index_last_modified; | |
| 79 if (GetFileInfo(index_path, &file_info)) | |
| 80 index_last_modified = file_info.last_modified; | |
| 64 std::string protobuf; | 81 std::string protobuf; |
| 65 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), | 82 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), |
| 66 &protobuf); | 83 &protobuf); |
| 67 proto::CacheStorageIndex index; | 84 proto::CacheStorageIndex index; |
| 68 if (index.ParseFromString(protobuf)) { | 85 if (index.ParseFromString(protobuf)) { |
| 69 if (index.has_origin()) { | 86 if (index.has_origin()) { |
| 70 base::File::Info file_info; | |
| 71 if (base::GetFileInfo(path, &file_info)) { | 87 if (base::GetFileInfo(path, &file_info)) { |
| 88 int64_t storage_size = CacheStorage::kSizeUnknown; | |
| 89 if (file_info.last_modified < index_last_modified) | |
| 90 storage_size = GetCacheStorageSize(index); | |
| 72 usages->push_back(CacheStorageUsageInfo( | 91 usages->push_back(CacheStorageUsageInfo( |
| 73 GURL(index.origin()), 0 /* size */, file_info.last_modified)); | 92 GURL(index.origin()), storage_size, file_info.last_modified)); |
| 74 } | 93 } |
| 75 } | 94 } |
| 76 } | 95 } |
| 77 } | 96 } |
| 78 } | 97 } |
| 79 | 98 |
| 80 std::set<GURL> ListOriginsOnTaskRunner(base::FilePath root_path) { | 99 std::set<GURL> ListOriginsOnTaskRunner(base::FilePath root_path) { |
| 81 std::vector<CacheStorageUsageInfo> usages; | 100 std::vector<CacheStorageUsageInfo> usages; |
| 82 ListOriginsAndLastModifiedOnTaskRunner(&usages, root_path); | 101 ListOriginsAndLastModifiedOnTaskRunner(&usages, root_path); |
| 83 | 102 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 110 | 129 |
| 111 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 130 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 112 base::Bind(callback, *usages)); | 131 base::Bind(callback, *usages)); |
| 113 } | 132 } |
| 114 | 133 |
| 115 void OneOriginSizeReported(const base::Closure& callback, | 134 void OneOriginSizeReported(const base::Closure& callback, |
| 116 CacheStorageUsageInfo* usage, | 135 CacheStorageUsageInfo* usage, |
| 117 int64_t size) { | 136 int64_t size) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 137 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 | 138 |
| 139 DCHECK_NE(size, CacheStorage::kSizeUnknown); | |
| 120 usage->total_size_bytes = size; | 140 usage->total_size_bytes = size; |
| 121 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 141 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 122 } | 142 } |
| 123 | 143 |
| 124 } // namespace | 144 } // namespace |
| 125 | 145 |
| 126 // static | 146 // static |
| 127 std::unique_ptr<CacheStorageManager> CacheStorageManager::Create( | 147 std::unique_ptr<CacheStorageManager> CacheStorageManager::Create( |
| 128 const base::FilePath& path, | 148 const base::FilePath& path, |
| 129 scoped_refptr<base::SequencedTaskRunner> cache_task_runner, | 149 scoped_refptr<base::SequencedTaskRunner> cache_task_runner, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 const std::string& cache_name, | 199 const std::string& cache_name, |
| 180 const CacheStorage::BoolAndErrorCallback& callback) { | 200 const CacheStorage::BoolAndErrorCallback& callback) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 201 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 | 202 |
| 183 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); | 203 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 184 cache_storage->DeleteCache(cache_name, callback); | 204 cache_storage->DeleteCache(cache_name, callback); |
| 185 } | 205 } |
| 186 | 206 |
| 187 void CacheStorageManager::EnumerateCaches( | 207 void CacheStorageManager::EnumerateCaches( |
| 188 const GURL& origin, | 208 const GURL& origin, |
| 189 const CacheStorage::StringsCallback& callback) { | 209 const CacheStorage::CacheMetadataCallback& callback) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 | 211 |
| 192 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); | 212 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 193 | 213 |
| 194 cache_storage->EnumerateCaches(callback); | 214 cache_storage->EnumerateCaches(callback); |
| 195 } | 215 } |
| 196 | 216 |
| 197 void CacheStorageManager::MatchCache( | 217 void CacheStorageManager::MatchCache( |
| 198 const GURL& origin, | 218 const GURL& origin, |
| 199 const std::string& cache_name, | 219 const std::string& cache_name, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 FROM_HERE, base::Bind(callback, *usages)); | 289 FROM_HERE, base::Bind(callback, *usages)); |
| 270 return; | 290 return; |
| 271 } | 291 } |
| 272 | 292 |
| 273 base::Closure barrier_closure = base::BarrierClosure( | 293 base::Closure barrier_closure = base::BarrierClosure( |
| 274 usages_ptr->size(), | 294 usages_ptr->size(), |
| 275 base::Bind(&AllOriginSizesReported, base::Passed(std::move(usages)), | 295 base::Bind(&AllOriginSizesReported, base::Passed(std::move(usages)), |
| 276 callback)); | 296 callback)); |
| 277 | 297 |
| 278 for (CacheStorageUsageInfo& usage : *usages_ptr) { | 298 for (CacheStorageUsageInfo& usage : *usages_ptr) { |
| 299 if (usage.total_size_bytes != CacheStorage::kSizeUnknown) { | |
| 300 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, barrier_closure); | |
| 301 continue; | |
|
jkarlin
2016/11/23 16:02:18
Woot!
cmumford
2016/11/29 18:10:21
Acknowledged.
| |
| 302 } | |
| 279 CacheStorage* cache_storage = FindOrCreateCacheStorage(usage.origin); | 303 CacheStorage* cache_storage = FindOrCreateCacheStorage(usage.origin); |
| 280 cache_storage->Size( | 304 cache_storage->Size( |
| 281 base::Bind(&OneOriginSizeReported, barrier_closure, &usage)); | 305 base::Bind(&OneOriginSizeReported, barrier_closure, &usage)); |
| 282 } | 306 } |
| 283 } | 307 } |
| 284 | 308 |
| 285 void CacheStorageManager::GetOriginUsage( | 309 void CacheStorageManager::GetOriginUsage( |
| 286 const GURL& origin_url, | 310 const GURL& origin_url, |
| 287 const storage::QuotaClient::GetUsageCallback& callback) { | 311 const storage::QuotaClient::GetUsageCallback& callback) { |
| 288 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 312 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 const base::FilePath& root_path, | 443 const base::FilePath& root_path, |
| 420 const GURL& origin) { | 444 const GURL& origin) { |
| 421 const std::string identifier = storage::GetIdentifierFromOrigin(origin); | 445 const std::string identifier = storage::GetIdentifierFromOrigin(origin); |
| 422 const std::string origin_hash = base::SHA1HashString(identifier); | 446 const std::string origin_hash = base::SHA1HashString(identifier); |
| 423 const std::string origin_hash_hex = base::ToLowerASCII( | 447 const std::string origin_hash_hex = base::ToLowerASCII( |
| 424 base::HexEncode(origin_hash.c_str(), origin_hash.length())); | 448 base::HexEncode(origin_hash.c_str(), origin_hash.length())); |
| 425 return root_path.AppendASCII(origin_hash_hex); | 449 return root_path.AppendASCII(origin_hash_hex); |
| 426 } | 450 } |
| 427 | 451 |
| 428 } // namespace content | 452 } // namespace content |
| OLD | NEW |