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 <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/id_map.h" | 13 #include "base/id_map.h" |
| 14 #include "base/sha1.h" | 14 #include "base/sha1.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/time/time.h" | |
| 17 #include "content/browser/cache_storage/cache_storage.h" | 18 #include "content/browser/cache_storage/cache_storage.h" |
| 18 #include "content/browser/cache_storage/cache_storage.pb.h" | 19 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 19 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 20 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
| 20 #include "content/browser/service_worker/service_worker_context_core.h" | 21 #include "content/browser/service_worker/service_worker_context_core.h" |
| 21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 22 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
| 23 #include "storage/browser/quota/quota_manager_proxy.h" | 24 #include "storage/browser/quota/quota_manager_proxy.h" |
| 24 #include "storage/common/database/database_identifier.h" | 25 #include "storage/common/database/database_identifier.h" |
| 25 #include "storage/common/quota/quota_status_code.h" | 26 #include "storage/common/quota/quota_status_code.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 bool DeleteDir(const base::FilePath& path) { | 33 bool DeleteDir(const base::FilePath& path) { |
| 33 return base::DeleteFile(path, true /* recursive */); | 34 return base::DeleteFile(path, true /* recursive */); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void DeleteOriginDidDeleteDir( | 37 void DeleteOriginDidDeleteDir( |
| 37 const storage::QuotaClient::DeletionCallback& callback, | 38 const storage::QuotaClient::DeletionCallback& callback, |
| 38 bool rv) { | 39 bool rv) { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 40 | 41 |
| 41 callback.Run(rv ? storage::kQuotaStatusOk : storage::kQuotaErrorAbort); | 42 callback.Run(rv ? storage::kQuotaStatusOk : storage::kQuotaErrorAbort); |
| 42 } | 43 } |
| 43 | 44 |
| 44 std::set<GURL> ListOriginsOnDisk(base::FilePath root_path_) { | 45 std::set<GURL> ListOriginsOnDisk(base::FilePath root_path) { |
| 45 std::set<GURL> origins; | 46 std::set<GURL> origins; |
| 46 base::FileEnumerator file_enum(root_path_, false /* recursive */, | 47 base::FileEnumerator file_enum(root_path, false /* recursive */, |
| 47 base::FileEnumerator::DIRECTORIES); | 48 base::FileEnumerator::DIRECTORIES); |
| 48 | 49 |
| 49 base::FilePath path; | 50 base::FilePath path; |
| 50 while (!(path = file_enum.Next()).empty()) { | 51 while (!(path = file_enum.Next()).empty()) { |
| 51 std::string protobuf; | 52 std::string protobuf; |
| 52 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), | 53 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), |
| 53 &protobuf); | 54 &protobuf); |
| 54 | 55 |
| 55 CacheStorageIndex index; | 56 CacheStorageIndex index; |
| 56 if (index.ParseFromString(protobuf)) { | 57 if (index.ParseFromString(protobuf)) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 67 const storage::QuotaClient::GetOriginsCallback& callback, | 68 const storage::QuotaClient::GetOriginsCallback& callback, |
| 68 const std::set<GURL>& origins) { | 69 const std::set<GURL>& origins) { |
| 69 std::set<GURL> out_origins; | 70 std::set<GURL> out_origins; |
| 70 for (const GURL& origin : origins) { | 71 for (const GURL& origin : origins) { |
| 71 if (host == net::GetHostOrSpecFromURL(origin)) | 72 if (host == net::GetHostOrSpecFromURL(origin)) |
| 72 out_origins.insert(origin); | 73 out_origins.insert(origin); |
| 73 } | 74 } |
| 74 callback.Run(out_origins); | 75 callback.Run(out_origins); |
| 75 } | 76 } |
| 76 | 77 |
| 78 void EmptyQuotaStatusCallback(storage::QuotaStatusCode code) {} | |
| 79 | |
| 77 } // namespace | 80 } // namespace |
| 78 | 81 |
| 79 // static | 82 // static |
| 80 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( | 83 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( |
| 81 const base::FilePath& path, | 84 const base::FilePath& path, |
| 82 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | 85 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, |
| 83 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { | 86 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { |
| 84 base::FilePath root_path = path; | 87 base::FilePath root_path = path; |
| 85 if (!path.empty()) { | 88 if (!path.empty()) { |
| 86 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | 89 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) { | 180 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 DCHECK(cache_storage_map_.empty()); | 182 DCHECK(cache_storage_map_.empty()); |
| 180 DCHECK(!request_context_getter_ || | 183 DCHECK(!request_context_getter_ || |
| 181 request_context_getter_.get() == request_context_getter.get()); | 184 request_context_getter_.get() == request_context_getter.get()); |
| 182 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); | 185 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); |
| 183 request_context_getter_ = request_context_getter; | 186 request_context_getter_ = request_context_getter; |
| 184 blob_context_ = blob_storage_context; | 187 blob_context_ = blob_storage_context; |
| 185 } | 188 } |
| 186 | 189 |
| 190 void CacheStorageManager::GetAllOriginsUsage( | |
| 191 const CacheStorageContext::GetUsageInfoCallback& callback) { | |
| 192 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 193 | |
| 194 if (IsMemoryBacked()) { | |
| 195 std::vector<CacheStorageUsageInfo> entries; | |
| 196 entries.reserve(cache_storage_map_.size()); | |
| 197 for (const auto& origin_details : cache_storage_map_) { | |
| 198 entries.push_back(CacheStorageUsageInfo( | |
| 199 origin_details.first, origin_details.second->MemoryBackedSize(), | |
| 200 base::Time())); | |
| 201 } | |
| 202 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 203 base::Bind(callback, entries)); | |
| 204 return; | |
| 205 } | |
| 206 | |
| 207 PostTaskAndReplyWithResult( | |
| 208 cache_task_runner_.get(), FROM_HERE, | |
| 209 base::Bind(&CacheStorageManager::GetAllOriginsUsageOnTaskRunner, | |
| 210 root_path_), | |
| 211 base::Bind(callback)); | |
| 212 } | |
| 213 | |
| 214 // static | |
| 215 std::vector<CacheStorageUsageInfo> | |
| 216 CacheStorageManager::GetAllOriginsUsageOnTaskRunner( | |
| 217 const base::FilePath root_path) { | |
| 218 std::vector<CacheStorageUsageInfo> entries; | |
| 219 const std::set<GURL> origins = ListOriginsOnDisk(root_path); | |
| 220 entries.reserve(origins.size()); | |
| 221 for (const GURL& origin : origins) { | |
| 222 base::FilePath path = | |
| 223 CacheStorageManager::ConstructOriginPath(root_path, origin); | |
| 224 int64 size = base::ComputeDirectorySize(path); | |
| 225 base::File::Info file_info; | |
| 226 base::Time last_modified; | |
| 227 if (base::GetFileInfo(path, &file_info)) | |
| 228 last_modified = file_info.last_modified; | |
| 229 entries.push_back(CacheStorageUsageInfo(origin, size, last_modified)); | |
| 230 } | |
| 231 return entries; | |
| 232 } | |
| 233 | |
| 187 void CacheStorageManager::GetOriginUsage( | 234 void CacheStorageManager::GetOriginUsage( |
| 188 const GURL& origin_url, | 235 const GURL& origin_url, |
| 189 const storage::QuotaClient::GetUsageCallback& callback) { | 236 const storage::QuotaClient::GetUsageCallback& callback) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 237 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 | 238 |
| 192 if (IsMemoryBacked()) { | 239 if (IsMemoryBacked()) { |
| 193 int64 sum = 0; | 240 int64 sum = 0; |
| 241 // TODO(jsbell): Should this be filtering by origin? | |
|
jkarlin
2015/08/18 13:22:35
Eek, yes!
jsbell
2015/08/18 18:42:43
Done, and added unit tests.
| |
| 194 for (const auto& key_value : cache_storage_map_) | 242 for (const auto& key_value : cache_storage_map_) |
| 195 sum += key_value.second->MemoryBackedSize(); | 243 sum += key_value.second->MemoryBackedSize(); |
| 196 callback.Run(sum); | 244 callback.Run(sum); |
| 197 return; | 245 return; |
| 198 } | 246 } |
| 199 | 247 |
| 200 MigrateOrigin(origin_url); | 248 MigrateOrigin(origin_url); |
| 201 PostTaskAndReplyWithResult( | 249 PostTaskAndReplyWithResult( |
| 202 cache_task_runner_.get(), FROM_HERE, | 250 cache_task_runner_.get(), FROM_HERE, |
| 203 base::Bind(base::ComputeDirectorySize, | 251 base::Bind(base::ComputeDirectorySize, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 298 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 251 | 299 |
| 252 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); | 300 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 253 cache_storage_map_.erase(origin); | 301 cache_storage_map_.erase(origin); |
| 254 cache_storage->CloseAllCaches( | 302 cache_storage->CloseAllCaches( |
| 255 base::Bind(&CacheStorageManager::DeleteOriginDidClose, origin, callback, | 303 base::Bind(&CacheStorageManager::DeleteOriginDidClose, origin, callback, |
| 256 base::Passed(make_scoped_ptr(cache_storage)), | 304 base::Passed(make_scoped_ptr(cache_storage)), |
| 257 weak_ptr_factory_.GetWeakPtr())); | 305 weak_ptr_factory_.GetWeakPtr())); |
| 258 } | 306 } |
| 259 | 307 |
| 308 void CacheStorageManager::DeleteOriginData(const GURL& origin) { | |
| 309 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 310 DeleteOriginData(origin, base::Bind(&EmptyQuotaStatusCallback)); | |
| 311 } | |
| 312 | |
| 260 // static | 313 // static |
| 261 void CacheStorageManager::DeleteOriginDidClose( | 314 void CacheStorageManager::DeleteOriginDidClose( |
| 262 const GURL& origin, | 315 const GURL& origin, |
| 263 const storage::QuotaClient::DeletionCallback& callback, | 316 const storage::QuotaClient::DeletionCallback& callback, |
| 264 scoped_ptr<CacheStorage> cache_storage, | 317 scoped_ptr<CacheStorage> cache_storage, |
| 265 base::WeakPtr<CacheStorageManager> cache_manager) { | 318 base::WeakPtr<CacheStorageManager> cache_manager) { |
| 266 // TODO(jkarlin): Deleting the storage leaves any unfinished operations | 319 // TODO(jkarlin): Deleting the storage leaves any unfinished operations |
| 267 // hanging, resulting in unresolved promises. Fix this by guaranteeing that | 320 // hanging, resulting in unresolved promises. Fix this by guaranteeing that |
| 268 // callbacks are called in ServiceWorkerStorage. | 321 // callbacks are called in ServiceWorkerStorage. |
| 269 cache_storage.reset(); | 322 cache_storage.reset(); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 const base::FilePath& old_path, | 408 const base::FilePath& old_path, |
| 356 const base::FilePath& new_path) { | 409 const base::FilePath& new_path) { |
| 357 if (base::PathExists(old_path)) { | 410 if (base::PathExists(old_path)) { |
| 358 if (!base::PathExists(new_path)) | 411 if (!base::PathExists(new_path)) |
| 359 base::Move(old_path, new_path); | 412 base::Move(old_path, new_path); |
| 360 base::DeleteFile(old_path, /*recursive*/ true); | 413 base::DeleteFile(old_path, /*recursive*/ true); |
| 361 } | 414 } |
| 362 } | 415 } |
| 363 | 416 |
| 364 } // namespace content | 417 } // namespace content |
| OLD | NEW |