| 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> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 // Calculate the sum of all cache sizes in this store, but only if all sizes are | 54 // Calculate the sum of all cache sizes in this store, but only if all |
| 55 // known. If one or more sizes are not known then return kSizeUnknown. | 55 // sizes are known. If one or more sizes are not known then return |
| 56 // kCacheStorageSizeUnknown. |
| 56 int64_t GetCacheStorageSize(const proto::CacheStorageIndex& index) { | 57 int64_t GetCacheStorageSize(const proto::CacheStorageIndex& index) { |
| 57 int64_t storage_size = 0; | 58 int64_t storage_size = 0; |
| 58 for (int i = 0, max = index.cache_size(); i < max; ++i) { | 59 for (int i = 0, max = index.cache_size(); i < max; ++i) { |
| 59 const proto::CacheStorageIndex::Cache& cache = index.cache(i); | 60 const proto::CacheStorageIndex::Cache& cache = index.cache(i); |
| 60 if (!cache.has_size() || cache.size() == CacheStorage::kSizeUnknown) | 61 if (!cache.has_size() || cache.size() == kCacheStorageSizeUnknown) |
| 61 return CacheStorage::kSizeUnknown; | 62 return kCacheStorageSizeUnknown; |
| 62 storage_size += cache.size(); | 63 storage_size += cache.size(); |
| 63 } | 64 } |
| 64 return storage_size; | 65 return storage_size; |
| 65 } | 66 } |
| 66 | 67 |
| 67 // Open the various cache directories' index files and extract their origins, | 68 // Open the various cache directories' index files and extract their origins, |
| 68 // sizes (if current), and last modified times. | 69 // sizes (if current), and last modified times. |
| 69 void ListOriginsAndLastModifiedOnTaskRunner( | 70 void ListOriginsAndLastModifiedOnTaskRunner( |
| 70 std::vector<CacheStorageUsageInfo>* usages, | 71 std::vector<CacheStorageUsageInfo>* usages, |
| 71 base::FilePath root_path) { | 72 base::FilePath root_path) { |
| 72 base::FileEnumerator file_enum(root_path, false /* recursive */, | 73 base::FileEnumerator file_enum(root_path, false /* recursive */, |
| 73 base::FileEnumerator::DIRECTORIES); | 74 base::FileEnumerator::DIRECTORIES); |
| 74 | 75 |
| 75 base::FilePath path; | 76 base::FilePath path; |
| 76 while (!(path = file_enum.Next()).empty()) { | 77 while (!(path = file_enum.Next()).empty()) { |
| 77 base::FilePath index_path = path.AppendASCII(CacheStorage::kIndexFileName); | 78 base::FilePath index_path = path.AppendASCII(CacheStorage::kIndexFileName); |
| 78 base::File::Info file_info; | 79 base::File::Info file_info; |
| 79 base::Time index_last_modified; | 80 base::Time index_last_modified; |
| 80 if (GetFileInfo(index_path, &file_info)) | 81 if (GetFileInfo(index_path, &file_info)) |
| 81 index_last_modified = file_info.last_modified; | 82 index_last_modified = file_info.last_modified; |
| 82 std::string protobuf; | 83 std::string protobuf; |
| 83 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), | 84 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), |
| 84 &protobuf); | 85 &protobuf); |
| 85 proto::CacheStorageIndex index; | 86 proto::CacheStorageIndex index; |
| 86 if (index.ParseFromString(protobuf)) { | 87 if (index.ParseFromString(protobuf)) { |
| 87 if (index.has_origin()) { | 88 if (index.has_origin()) { |
| 88 if (base::GetFileInfo(path, &file_info)) { | 89 if (base::GetFileInfo(path, &file_info)) { |
| 89 int64_t storage_size = CacheStorage::kSizeUnknown; | 90 int64_t storage_size = kCacheStorageSizeUnknown; |
| 90 if (file_info.last_modified < index_last_modified) | 91 if (file_info.last_modified < index_last_modified) |
| 91 storage_size = GetCacheStorageSize(index); | 92 storage_size = GetCacheStorageSize(index); |
| 92 usages->push_back(CacheStorageUsageInfo( | 93 usages->push_back(CacheStorageUsageInfo( |
| 93 GURL(index.origin()), storage_size, file_info.last_modified)); | 94 GURL(index.origin()), storage_size, file_info.last_modified)); |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 } | 99 } |
| 99 | 100 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 130 | 131 |
| 131 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 132 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 132 base::Bind(callback, *usages)); | 133 base::Bind(callback, *usages)); |
| 133 } | 134 } |
| 134 | 135 |
| 135 void OneOriginSizeReported(const base::Closure& callback, | 136 void OneOriginSizeReported(const base::Closure& callback, |
| 136 CacheStorageUsageInfo* usage, | 137 CacheStorageUsageInfo* usage, |
| 137 int64_t size) { | 138 int64_t size) { |
| 138 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 139 | 140 |
| 140 DCHECK_NE(size, CacheStorage::kSizeUnknown); | 141 DCHECK_NE(size, kCacheStorageSizeUnknown); |
| 141 usage->total_size_bytes = size; | 142 usage->total_size_bytes = size; |
| 142 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 143 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 143 } | 144 } |
| 144 | 145 |
| 145 } // namespace | 146 } // namespace |
| 146 | 147 |
| 147 // static | 148 // static |
| 148 std::unique_ptr<CacheStorageManager> CacheStorageManager::Create( | 149 std::unique_ptr<CacheStorageManager> CacheStorageManager::Create( |
| 149 const base::FilePath& path, | 150 const base::FilePath& path, |
| 150 scoped_refptr<base::SequencedTaskRunner> cache_task_runner, | 151 scoped_refptr<base::SequencedTaskRunner> cache_task_runner, |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 FROM_HERE, base::Bind(callback, *usages)); | 291 FROM_HERE, base::Bind(callback, *usages)); |
| 291 return; | 292 return; |
| 292 } | 293 } |
| 293 | 294 |
| 294 base::Closure barrier_closure = base::BarrierClosure( | 295 base::Closure barrier_closure = base::BarrierClosure( |
| 295 usages_ptr->size(), | 296 usages_ptr->size(), |
| 296 base::Bind(&AllOriginSizesReported, base::Passed(std::move(usages)), | 297 base::Bind(&AllOriginSizesReported, base::Passed(std::move(usages)), |
| 297 callback)); | 298 callback)); |
| 298 | 299 |
| 299 for (CacheStorageUsageInfo& usage : *usages_ptr) { | 300 for (CacheStorageUsageInfo& usage : *usages_ptr) { |
| 300 if (usage.total_size_bytes != CacheStorage::kSizeUnknown) { | 301 if (usage.total_size_bytes != kCacheStorageSizeUnknown) { |
| 301 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, barrier_closure); | 302 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, barrier_closure); |
| 302 continue; | 303 continue; |
| 303 } | 304 } |
| 304 CacheStorage* cache_storage = FindOrCreateCacheStorage(usage.origin); | 305 CacheStorage* cache_storage = FindOrCreateCacheStorage(usage.origin); |
| 305 cache_storage->Size( | 306 cache_storage->Size( |
| 306 base::Bind(&OneOriginSizeReported, barrier_closure, &usage)); | 307 base::Bind(&OneOriginSizeReported, barrier_closure, &usage)); |
| 307 } | 308 } |
| 308 } | 309 } |
| 309 | 310 |
| 310 void CacheStorageManager::GetOriginUsage( | 311 void CacheStorageManager::GetOriginUsage( |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 const base::FilePath& root_path, | 445 const base::FilePath& root_path, |
| 445 const GURL& origin) { | 446 const GURL& origin) { |
| 446 const std::string identifier = storage::GetIdentifierFromOrigin(origin); | 447 const std::string identifier = storage::GetIdentifierFromOrigin(origin); |
| 447 const std::string origin_hash = base::SHA1HashString(identifier); | 448 const std::string origin_hash = base::SHA1HashString(identifier); |
| 448 const std::string origin_hash_hex = base::ToLowerASCII( | 449 const std::string origin_hash_hex = base::ToLowerASCII( |
| 449 base::HexEncode(origin_hash.c_str(), origin_hash.length())); | 450 base::HexEncode(origin_hash.c_str(), origin_hash.length())); |
| 450 return root_path.AppendASCII(origin_hash_hex); | 451 return root_path.AppendASCII(origin_hash_hex); |
| 451 } | 452 } |
| 452 | 453 |
| 453 } // namespace content | 454 } // namespace content |
| OLD | NEW |