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