| 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/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/time/time.h" |
| 17 #include "content/browser/cache_storage/cache_storage.h" | 19 #include "content/browser/cache_storage/cache_storage.h" |
| 18 #include "content/browser/cache_storage/cache_storage.pb.h" | 20 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 19 #include "content/browser/cache_storage/cache_storage_quota_client.h" | 21 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
| 20 #include "content/browser/service_worker/service_worker_context_core.h" | 22 #include "content/browser/service_worker/service_worker_context_core.h" |
| 21 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 22 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 23 #include "storage/browser/quota/quota_manager_proxy.h" | 25 #include "storage/browser/quota/quota_manager_proxy.h" |
| 24 #include "storage/common/database/database_identifier.h" | 26 #include "storage/common/database/database_identifier.h" |
| 25 #include "storage/common/quota/quota_status_code.h" | 27 #include "storage/common/quota/quota_status_code.h" |
| 26 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 27 | 29 |
| 28 namespace content { | 30 namespace content { |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 bool DeleteDir(const base::FilePath& path) { | 34 bool DeleteDir(const base::FilePath& path) { |
| 33 return base::DeleteFile(path, true /* recursive */); | 35 return base::DeleteFile(path, true /* recursive */); |
| 34 } | 36 } |
| 35 | 37 |
| 36 void DeleteOriginDidDeleteDir( | 38 void DeleteOriginDidDeleteDir( |
| 37 const storage::QuotaClient::DeletionCallback& callback, | 39 const storage::QuotaClient::DeletionCallback& callback, |
| 38 bool rv) { | 40 bool rv) { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 41 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 40 | 42 |
| 41 callback.Run(rv ? storage::kQuotaStatusOk : storage::kQuotaErrorAbort); | 43 callback.Run(rv ? storage::kQuotaStatusOk : storage::kQuotaErrorAbort); |
| 42 } | 44 } |
| 43 | 45 |
| 44 std::set<GURL> ListOriginsOnDisk(base::FilePath root_path_) { | 46 std::set<GURL> ListOriginsOnDisk(base::FilePath root_path) { |
| 45 std::set<GURL> origins; | 47 std::set<GURL> origins; |
| 46 base::FileEnumerator file_enum(root_path_, false /* recursive */, | 48 base::FileEnumerator file_enum(root_path, false /* recursive */, |
| 47 base::FileEnumerator::DIRECTORIES); | 49 base::FileEnumerator::DIRECTORIES); |
| 48 | 50 |
| 49 base::FilePath path; | 51 base::FilePath path; |
| 50 while (!(path = file_enum.Next()).empty()) { | 52 while (!(path = file_enum.Next()).empty()) { |
| 51 std::string protobuf; | 53 std::string protobuf; |
| 52 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), | 54 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), |
| 53 &protobuf); | 55 &protobuf); |
| 54 | 56 |
| 55 CacheStorageIndex index; | 57 CacheStorageIndex index; |
| 56 if (index.ParseFromString(protobuf)) { | 58 if (index.ParseFromString(protobuf)) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 67 const storage::QuotaClient::GetOriginsCallback& callback, | 69 const storage::QuotaClient::GetOriginsCallback& callback, |
| 68 const std::set<GURL>& origins) { | 70 const std::set<GURL>& origins) { |
| 69 std::set<GURL> out_origins; | 71 std::set<GURL> out_origins; |
| 70 for (const GURL& origin : origins) { | 72 for (const GURL& origin : origins) { |
| 71 if (host == net::GetHostOrSpecFromURL(origin)) | 73 if (host == net::GetHostOrSpecFromURL(origin)) |
| 72 out_origins.insert(origin); | 74 out_origins.insert(origin); |
| 73 } | 75 } |
| 74 callback.Run(out_origins); | 76 callback.Run(out_origins); |
| 75 } | 77 } |
| 76 | 78 |
| 79 void EmptyQuotaStatusCallback(storage::QuotaStatusCode code) {} |
| 80 |
| 77 } // namespace | 81 } // namespace |
| 78 | 82 |
| 79 // static | 83 // static |
| 80 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( | 84 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( |
| 81 const base::FilePath& path, | 85 const base::FilePath& path, |
| 82 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | 86 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, |
| 83 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { | 87 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { |
| 84 base::FilePath root_path = path; | 88 base::FilePath root_path = path; |
| 85 if (!path.empty()) { | 89 if (!path.empty()) { |
| 86 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | 90 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) { | 181 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 DCHECK(cache_storage_map_.empty()); | 183 DCHECK(cache_storage_map_.empty()); |
| 180 DCHECK(!request_context_getter_ || | 184 DCHECK(!request_context_getter_ || |
| 181 request_context_getter_.get() == request_context_getter.get()); | 185 request_context_getter_.get() == request_context_getter.get()); |
| 182 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); | 186 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); |
| 183 request_context_getter_ = request_context_getter; | 187 request_context_getter_ = request_context_getter; |
| 184 blob_context_ = blob_storage_context; | 188 blob_context_ = blob_storage_context; |
| 185 } | 189 } |
| 186 | 190 |
| 191 void CacheStorageManager::GetAllOriginsUsage( |
| 192 const CacheStorageContext::GetUsageInfoCallback& callback) { |
| 193 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 194 |
| 195 if (IsMemoryBacked()) { |
| 196 std::vector<CacheStorageUsageInfo> entries; |
| 197 entries.reserve(cache_storage_map_.size()); |
| 198 for (const auto& origin_details : cache_storage_map_) { |
| 199 entries.push_back(CacheStorageUsageInfo( |
| 200 origin_details.first, origin_details.second->MemoryBackedSize(), |
| 201 base::Time())); |
| 202 } |
| 203 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 204 base::Bind(callback, entries)); |
| 205 return; |
| 206 } |
| 207 |
| 208 PostTaskAndReplyWithResult( |
| 209 cache_task_runner_.get(), FROM_HERE, |
| 210 base::Bind(&CacheStorageManager::GetAllOriginsUsageOnTaskRunner, |
| 211 root_path_), |
| 212 base::Bind(callback)); |
| 213 } |
| 214 |
| 215 // static |
| 216 std::vector<CacheStorageUsageInfo> |
| 217 CacheStorageManager::GetAllOriginsUsageOnTaskRunner( |
| 218 const base::FilePath root_path) { |
| 219 std::vector<CacheStorageUsageInfo> entries; |
| 220 const std::set<GURL> origins = ListOriginsOnDisk(root_path); |
| 221 entries.reserve(origins.size()); |
| 222 for (const GURL& origin : origins) { |
| 223 base::FilePath path = |
| 224 CacheStorageManager::ConstructOriginPath(root_path, origin); |
| 225 int64 size = base::ComputeDirectorySize(path); |
| 226 base::File::Info file_info; |
| 227 base::Time last_modified; |
| 228 if (base::GetFileInfo(path, &file_info)) |
| 229 last_modified = file_info.last_modified; |
| 230 entries.push_back(CacheStorageUsageInfo(origin, size, last_modified)); |
| 231 } |
| 232 return entries; |
| 233 } |
| 234 |
| 187 void CacheStorageManager::GetOriginUsage( | 235 void CacheStorageManager::GetOriginUsage( |
| 188 const GURL& origin_url, | 236 const GURL& origin_url, |
| 189 const storage::QuotaClient::GetUsageCallback& callback) { | 237 const storage::QuotaClient::GetUsageCallback& callback) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 238 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 | 239 |
| 192 if (IsMemoryBacked()) { | 240 if (IsMemoryBacked()) { |
| 193 int64 sum = 0; | 241 int64 usage = 0; |
| 194 for (const auto& key_value : cache_storage_map_) | 242 if (ContainsKey(cache_storage_map_, origin_url)) |
| 195 sum += key_value.second->MemoryBackedSize(); | 243 usage = cache_storage_map_[origin_url]->MemoryBackedSize(); |
| 196 callback.Run(sum); | 244 callback.Run(usage); |
| 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, |
| 204 ConstructOriginPath(root_path_, origin_url)), | 252 ConstructOriginPath(root_path_, origin_url)), |
| 205 base::Bind(callback)); | 253 base::Bind(callback)); |
| 206 } | 254 } |
| (...skipping 43 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 |