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)) { |
57 if (index.has_origin()) | 59 if (index.has_origin()) |
58 origins.insert(GURL(index.origin())); | 60 origins.insert(GURL(index.origin())); |
59 } | 61 } |
60 } | 62 } |
61 | 63 |
62 return origins; | 64 return origins; |
63 } | 65 } |
64 | 66 |
| 67 std::vector<CacheStorageUsageInfo> GetAllOriginsUsageOnTaskRunner( |
| 68 const base::FilePath root_path) { |
| 69 std::vector<CacheStorageUsageInfo> entries; |
| 70 const std::set<GURL> origins = ListOriginsOnDisk(root_path); |
| 71 entries.reserve(origins.size()); |
| 72 for (const GURL& origin : origins) { |
| 73 base::FilePath path = |
| 74 CacheStorageManager::ConstructOriginPath(root_path, origin); |
| 75 int64 size = base::ComputeDirectorySize(path); |
| 76 base::File::Info file_info; |
| 77 base::Time last_modified; |
| 78 if (base::GetFileInfo(path, &file_info)) |
| 79 last_modified = file_info.last_modified; |
| 80 entries.push_back(CacheStorageUsageInfo(origin, size, last_modified)); |
| 81 } |
| 82 return entries; |
| 83 } |
| 84 |
65 void GetOriginsForHostDidListOrigins( | 85 void GetOriginsForHostDidListOrigins( |
66 const std::string& host, | 86 const std::string& host, |
67 const storage::QuotaClient::GetOriginsCallback& callback, | 87 const storage::QuotaClient::GetOriginsCallback& callback, |
68 const std::set<GURL>& origins) { | 88 const std::set<GURL>& origins) { |
69 std::set<GURL> out_origins; | 89 std::set<GURL> out_origins; |
70 for (const GURL& origin : origins) { | 90 for (const GURL& origin : origins) { |
71 if (host == net::GetHostOrSpecFromURL(origin)) | 91 if (host == net::GetHostOrSpecFromURL(origin)) |
72 out_origins.insert(origin); | 92 out_origins.insert(origin); |
73 } | 93 } |
74 callback.Run(out_origins); | 94 callback.Run(out_origins); |
75 } | 95 } |
76 | 96 |
| 97 void EmptyQuotaStatusCallback(storage::QuotaStatusCode code) {} |
| 98 |
77 } // namespace | 99 } // namespace |
78 | 100 |
79 // static | 101 // static |
80 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( | 102 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( |
81 const base::FilePath& path, | 103 const base::FilePath& path, |
82 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | 104 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, |
83 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { | 105 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { |
84 base::FilePath root_path = path; | 106 base::FilePath root_path = path; |
85 if (!path.empty()) { | 107 if (!path.empty()) { |
86 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | 108 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) { | 199 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) { |
178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
179 DCHECK(cache_storage_map_.empty()); | 201 DCHECK(cache_storage_map_.empty()); |
180 DCHECK(!request_context_getter_ || | 202 DCHECK(!request_context_getter_ || |
181 request_context_getter_.get() == request_context_getter.get()); | 203 request_context_getter_.get() == request_context_getter.get()); |
182 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); | 204 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); |
183 request_context_getter_ = request_context_getter; | 205 request_context_getter_ = request_context_getter; |
184 blob_context_ = blob_storage_context; | 206 blob_context_ = blob_storage_context; |
185 } | 207 } |
186 | 208 |
| 209 void CacheStorageManager::GetAllOriginsUsage( |
| 210 const CacheStorageContext::GetUsageInfoCallback& callback) { |
| 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 212 |
| 213 if (IsMemoryBacked()) { |
| 214 std::vector<CacheStorageUsageInfo> entries; |
| 215 entries.reserve(cache_storage_map_.size()); |
| 216 for (const auto& origin_details : cache_storage_map_) { |
| 217 entries.push_back(CacheStorageUsageInfo( |
| 218 origin_details.first, origin_details.second->MemoryBackedSize(), |
| 219 base::Time())); |
| 220 } |
| 221 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 222 base::Bind(callback, entries)); |
| 223 return; |
| 224 } |
| 225 |
| 226 PostTaskAndReplyWithResult( |
| 227 cache_task_runner_.get(), FROM_HERE, |
| 228 base::Bind(&GetAllOriginsUsageOnTaskRunner, root_path_), |
| 229 base::Bind(callback)); |
| 230 } |
| 231 |
187 void CacheStorageManager::GetOriginUsage( | 232 void CacheStorageManager::GetOriginUsage( |
188 const GURL& origin_url, | 233 const GURL& origin_url, |
189 const storage::QuotaClient::GetUsageCallback& callback) { | 234 const storage::QuotaClient::GetUsageCallback& callback) { |
190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 235 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
191 | 236 |
192 if (IsMemoryBacked()) { | 237 if (IsMemoryBacked()) { |
193 int64 sum = 0; | 238 int64 usage = 0; |
194 for (const auto& key_value : cache_storage_map_) | 239 if (ContainsKey(cache_storage_map_, origin_url)) |
195 sum += key_value.second->MemoryBackedSize(); | 240 usage = cache_storage_map_[origin_url]->MemoryBackedSize(); |
196 callback.Run(sum); | 241 callback.Run(usage); |
197 return; | 242 return; |
198 } | 243 } |
199 | 244 |
200 MigrateOrigin(origin_url); | 245 MigrateOrigin(origin_url); |
201 PostTaskAndReplyWithResult( | 246 PostTaskAndReplyWithResult( |
202 cache_task_runner_.get(), FROM_HERE, | 247 cache_task_runner_.get(), FROM_HERE, |
203 base::Bind(base::ComputeDirectorySize, | 248 base::Bind(base::ComputeDirectorySize, |
204 ConstructOriginPath(root_path_, origin_url)), | 249 ConstructOriginPath(root_path_, origin_url)), |
205 base::Bind(callback)); | 250 base::Bind(callback)); |
206 } | 251 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 295 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
251 | 296 |
252 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); | 297 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
253 cache_storage_map_.erase(origin); | 298 cache_storage_map_.erase(origin); |
254 cache_storage->CloseAllCaches( | 299 cache_storage->CloseAllCaches( |
255 base::Bind(&CacheStorageManager::DeleteOriginDidClose, origin, callback, | 300 base::Bind(&CacheStorageManager::DeleteOriginDidClose, origin, callback, |
256 base::Passed(make_scoped_ptr(cache_storage)), | 301 base::Passed(make_scoped_ptr(cache_storage)), |
257 weak_ptr_factory_.GetWeakPtr())); | 302 weak_ptr_factory_.GetWeakPtr())); |
258 } | 303 } |
259 | 304 |
| 305 void CacheStorageManager::DeleteOriginData(const GURL& origin) { |
| 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 307 DeleteOriginData(origin, base::Bind(&EmptyQuotaStatusCallback)); |
| 308 } |
| 309 |
260 // static | 310 // static |
261 void CacheStorageManager::DeleteOriginDidClose( | 311 void CacheStorageManager::DeleteOriginDidClose( |
262 const GURL& origin, | 312 const GURL& origin, |
263 const storage::QuotaClient::DeletionCallback& callback, | 313 const storage::QuotaClient::DeletionCallback& callback, |
264 scoped_ptr<CacheStorage> cache_storage, | 314 scoped_ptr<CacheStorage> cache_storage, |
265 base::WeakPtr<CacheStorageManager> cache_manager) { | 315 base::WeakPtr<CacheStorageManager> cache_manager) { |
266 // TODO(jkarlin): Deleting the storage leaves any unfinished operations | 316 // TODO(jkarlin): Deleting the storage leaves any unfinished operations |
267 // hanging, resulting in unresolved promises. Fix this by guaranteeing that | 317 // hanging, resulting in unresolved promises. Fix this by guaranteeing that |
268 // callbacks are called in ServiceWorkerStorage. | 318 // callbacks are called in ServiceWorkerStorage. |
269 cache_storage.reset(); | 319 cache_storage.reset(); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 const base::FilePath& old_path, | 405 const base::FilePath& old_path, |
356 const base::FilePath& new_path) { | 406 const base::FilePath& new_path) { |
357 if (base::PathExists(old_path)) { | 407 if (base::PathExists(old_path)) { |
358 if (!base::PathExists(new_path)) | 408 if (!base::PathExists(new_path)) |
359 base::Move(old_path, new_path); | 409 base::Move(old_path, new_path); |
360 base::DeleteFile(old_path, /*recursive*/ true); | 410 base::DeleteFile(old_path, /*recursive*/ true); |
361 } | 411 } |
362 } | 412 } |
363 | 413 |
364 } // namespace content | 414 } // namespace content |
OLD | NEW |