| 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/service_worker/service_worker_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 "content/browser/service_worker/service_worker_cache.pb.h" | 17 #include "content/browser/cache_storage/cache_storage.h" |
| 18 #include "content/browser/service_worker/service_worker_cache_quota_client.h" | 18 #include "content/browser/cache_storage/cache_storage.pb.h" |
| 19 #include "content/browser/service_worker/service_worker_cache_storage.h" | 19 #include "content/browser/cache_storage/cache_storage_quota_client.h" |
| 20 #include "content/browser/service_worker/service_worker_context_core.h" | 20 #include "content/browser/service_worker/service_worker_context_core.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 23 #include "storage/browser/quota/quota_manager_proxy.h" | 23 #include "storage/browser/quota/quota_manager_proxy.h" |
| 24 #include "storage/common/database/database_identifier.h" | 24 #include "storage/common/database/database_identifier.h" |
| 25 #include "storage/common/quota/quota_status_code.h" | 25 #include "storage/common/quota/quota_status_code.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 bool DeleteDir(const base::FilePath& path) { | 32 bool DeleteDir(const base::FilePath& path) { |
| 33 return base::DeleteFile(path, true /* recursive */); | 33 return base::DeleteFile(path, true /* recursive */); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void DeleteOriginDidDeleteDir( | 36 void DeleteOriginDidDeleteDir( |
| 37 const storage::QuotaClient::DeletionCallback& callback, | 37 const storage::QuotaClient::DeletionCallback& callback, |
| 38 bool rv) { | 38 bool rv) { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 40 | 40 |
| 41 callback.Run(rv ? storage::kQuotaStatusOk : storage::kQuotaErrorAbort); | 41 callback.Run(rv ? storage::kQuotaStatusOk : storage::kQuotaErrorAbort); |
| 42 } | 42 } |
| 43 | 43 |
| 44 std::set<GURL> ListOriginsOnDisk(base::FilePath root_path_) { | 44 std::set<GURL> ListOriginsOnDisk(base::FilePath root_path_) { |
| 45 std::set<GURL> origins; | 45 std::set<GURL> origins; |
| 46 base::FileEnumerator file_enum( | 46 base::FileEnumerator file_enum(root_path_, false /* recursive */, |
| 47 root_path_, false /* recursive */, base::FileEnumerator::DIRECTORIES); | 47 base::FileEnumerator::DIRECTORIES); |
| 48 | 48 |
| 49 base::FilePath path; | 49 base::FilePath path; |
| 50 while (!(path = file_enum.Next()).empty()) { | 50 while (!(path = file_enum.Next()).empty()) { |
| 51 std::string protobuf; | 51 std::string protobuf; |
| 52 base::ReadFileToString( | 52 base::ReadFileToString(path.AppendASCII(CacheStorage::kIndexFileName), |
| 53 path.AppendASCII(ServiceWorkerCacheStorage::kIndexFileName), &protobuf); | 53 &protobuf); |
| 54 | 54 |
| 55 ServiceWorkerCacheStorageIndex index; | 55 CacheStorageIndex index; |
| 56 if (index.ParseFromString(protobuf)) { | 56 if (index.ParseFromString(protobuf)) { |
| 57 if (index.has_origin()) | 57 if (index.has_origin()) |
| 58 origins.insert(GURL(index.origin())); | 58 origins.insert(GURL(index.origin())); |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 return origins; | 62 return origins; |
| 63 } | 63 } |
| 64 | 64 |
| 65 void GetOriginsForHostDidListOrigins( | 65 void GetOriginsForHostDidListOrigins( |
| 66 const std::string& host, | 66 const std::string& host, |
| 67 const storage::QuotaClient::GetOriginsCallback& callback, | 67 const storage::QuotaClient::GetOriginsCallback& callback, |
| 68 const std::set<GURL>& origins) { | 68 const std::set<GURL>& origins) { |
| 69 std::set<GURL> out_origins; | 69 std::set<GURL> out_origins; |
| 70 for (const GURL& origin : origins) { | 70 for (const GURL& origin : origins) { |
| 71 if (host == net::GetHostOrSpecFromURL(origin)) | 71 if (host == net::GetHostOrSpecFromURL(origin)) |
| 72 out_origins.insert(origin); | 72 out_origins.insert(origin); |
| 73 } | 73 } |
| 74 callback.Run(out_origins); | 74 callback.Run(out_origins); |
| 75 } | 75 } |
| 76 | 76 |
| 77 } // namespace | 77 } // namespace |
| 78 | 78 |
| 79 // static | 79 // static |
| 80 scoped_ptr<ServiceWorkerCacheStorageManager> | 80 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( |
| 81 ServiceWorkerCacheStorageManager::Create( | |
| 82 const base::FilePath& path, | 81 const base::FilePath& path, |
| 83 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | 82 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, |
| 84 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { | 83 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) { |
| 85 base::FilePath root_path = path; | 84 base::FilePath root_path = path; |
| 86 if (!path.empty()) { | 85 if (!path.empty()) { |
| 87 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) | 86 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory) |
| 88 .AppendASCII("CacheStorage"); | 87 .AppendASCII("CacheStorage"); |
| 89 } | 88 } |
| 90 | 89 |
| 91 return make_scoped_ptr(new ServiceWorkerCacheStorageManager( | 90 return make_scoped_ptr(new CacheStorageManager(root_path, cache_task_runner, |
| 92 root_path, cache_task_runner, quota_manager_proxy)); | 91 quota_manager_proxy)); |
| 93 } | 92 } |
| 94 | 93 |
| 95 // static | 94 // static |
| 96 scoped_ptr<ServiceWorkerCacheStorageManager> | 95 scoped_ptr<CacheStorageManager> CacheStorageManager::Create( |
| 97 ServiceWorkerCacheStorageManager::Create( | 96 CacheStorageManager* old_manager) { |
| 98 ServiceWorkerCacheStorageManager* old_manager) { | 97 scoped_ptr<CacheStorageManager> manager(new CacheStorageManager( |
| 99 scoped_ptr<ServiceWorkerCacheStorageManager> manager( | 98 old_manager->root_path(), old_manager->cache_task_runner(), |
| 100 new ServiceWorkerCacheStorageManager( | 99 old_manager->quota_manager_proxy_.get())); |
| 101 old_manager->root_path(), | |
| 102 old_manager->cache_task_runner(), | |
| 103 old_manager->quota_manager_proxy_.get())); | |
| 104 // These values may be NULL, in which case this will be called again later by | 100 // These values may be NULL, in which case this will be called again later by |
| 105 // the dispatcher host per usual. | 101 // the dispatcher host per usual. |
| 106 manager->SetBlobParametersForCache(old_manager->url_request_context(), | 102 manager->SetBlobParametersForCache(old_manager->url_request_context(), |
| 107 old_manager->blob_storage_context()); | 103 old_manager->blob_storage_context()); |
| 108 return manager.Pass(); | 104 return manager.Pass(); |
| 109 } | 105 } |
| 110 | 106 |
| 111 ServiceWorkerCacheStorageManager::~ServiceWorkerCacheStorageManager() { | 107 CacheStorageManager::~CacheStorageManager() { |
| 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 113 for (ServiceWorkerCacheStorageMap::iterator it = cache_storage_map_.begin(); | 109 for (CacheStorageMap::iterator it = cache_storage_map_.begin(); |
| 114 it != cache_storage_map_.end(); | 110 it != cache_storage_map_.end(); ++it) { |
| 115 ++it) { | |
| 116 delete it->second; | 111 delete it->second; |
| 117 } | 112 } |
| 118 } | 113 } |
| 119 | 114 |
| 120 void ServiceWorkerCacheStorageManager::OpenCache( | 115 void CacheStorageManager::OpenCache( |
| 121 const GURL& origin, | 116 const GURL& origin, |
| 122 const std::string& cache_name, | 117 const std::string& cache_name, |
| 123 const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback) { | 118 const CacheStorage::CacheAndErrorCallback& callback) { |
| 124 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 125 | 120 |
| 126 ServiceWorkerCacheStorage* cache_storage = | 121 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 127 FindOrCreateServiceWorkerCacheManager(origin); | |
| 128 | 122 |
| 129 cache_storage->OpenCache(cache_name, callback); | 123 cache_storage->OpenCache(cache_name, callback); |
| 130 } | 124 } |
| 131 | 125 |
| 132 void ServiceWorkerCacheStorageManager::HasCache( | 126 void CacheStorageManager::HasCache( |
| 133 const GURL& origin, | 127 const GURL& origin, |
| 134 const std::string& cache_name, | 128 const std::string& cache_name, |
| 135 const ServiceWorkerCacheStorage::BoolAndErrorCallback& callback) { | 129 const CacheStorage::BoolAndErrorCallback& callback) { |
| 136 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 137 | 131 |
| 138 ServiceWorkerCacheStorage* cache_storage = | 132 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 139 FindOrCreateServiceWorkerCacheManager(origin); | |
| 140 cache_storage->HasCache(cache_name, callback); | 133 cache_storage->HasCache(cache_name, callback); |
| 141 } | 134 } |
| 142 | 135 |
| 143 void ServiceWorkerCacheStorageManager::DeleteCache( | 136 void CacheStorageManager::DeleteCache( |
| 144 const GURL& origin, | 137 const GURL& origin, |
| 145 const std::string& cache_name, | 138 const std::string& cache_name, |
| 146 const ServiceWorkerCacheStorage::BoolAndErrorCallback& callback) { | 139 const CacheStorage::BoolAndErrorCallback& callback) { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 | 141 |
| 149 ServiceWorkerCacheStorage* cache_storage = | 142 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 150 FindOrCreateServiceWorkerCacheManager(origin); | |
| 151 cache_storage->DeleteCache(cache_name, callback); | 143 cache_storage->DeleteCache(cache_name, callback); |
| 152 } | 144 } |
| 153 | 145 |
| 154 void ServiceWorkerCacheStorageManager::EnumerateCaches( | 146 void CacheStorageManager::EnumerateCaches( |
| 155 const GURL& origin, | 147 const GURL& origin, |
| 156 const ServiceWorkerCacheStorage::StringsAndErrorCallback& callback) { | 148 const CacheStorage::StringsAndErrorCallback& callback) { |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 149 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 158 | 150 |
| 159 ServiceWorkerCacheStorage* cache_storage = | 151 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 160 FindOrCreateServiceWorkerCacheManager(origin); | |
| 161 | 152 |
| 162 cache_storage->EnumerateCaches(callback); | 153 cache_storage->EnumerateCaches(callback); |
| 163 } | 154 } |
| 164 | 155 |
| 165 void ServiceWorkerCacheStorageManager::MatchCache( | 156 void CacheStorageManager::MatchCache( |
| 166 const GURL& origin, | 157 const GURL& origin, |
| 167 const std::string& cache_name, | 158 const std::string& cache_name, |
| 168 scoped_ptr<ServiceWorkerFetchRequest> request, | 159 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 169 const ServiceWorkerCache::ResponseCallback& callback) { | 160 const CacheStorageCache::ResponseCallback& callback) { |
| 170 ServiceWorkerCacheStorage* cache_storage = | 161 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 171 FindOrCreateServiceWorkerCacheManager(origin); | |
| 172 | 162 |
| 173 cache_storage->MatchCache(cache_name, request.Pass(), callback); | 163 cache_storage->MatchCache(cache_name, request.Pass(), callback); |
| 174 } | 164 } |
| 175 | 165 |
| 176 void ServiceWorkerCacheStorageManager::MatchAllCaches( | 166 void CacheStorageManager::MatchAllCaches( |
| 177 const GURL& origin, | 167 const GURL& origin, |
| 178 scoped_ptr<ServiceWorkerFetchRequest> request, | 168 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 179 const ServiceWorkerCache::ResponseCallback& callback) { | 169 const CacheStorageCache::ResponseCallback& callback) { |
| 180 ServiceWorkerCacheStorage* cache_storage = | 170 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 181 FindOrCreateServiceWorkerCacheManager(origin); | |
| 182 | 171 |
| 183 cache_storage->MatchAllCaches(request.Pass(), callback); | 172 cache_storage->MatchAllCaches(request.Pass(), callback); |
| 184 } | 173 } |
| 185 | 174 |
| 186 void ServiceWorkerCacheStorageManager::SetBlobParametersForCache( | 175 void CacheStorageManager::SetBlobParametersForCache( |
| 187 net::URLRequestContext* request_context, | 176 net::URLRequestContext* request_context, |
| 188 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) { | 177 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) { |
| 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 190 DCHECK(cache_storage_map_.empty()); | 179 DCHECK(cache_storage_map_.empty()); |
| 191 DCHECK(!request_context_ || request_context_ == request_context); | 180 DCHECK(!request_context_ || request_context_ == request_context); |
| 192 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); | 181 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get()); |
| 193 request_context_ = request_context; | 182 request_context_ = request_context; |
| 194 blob_context_ = blob_storage_context; | 183 blob_context_ = blob_storage_context; |
| 195 } | 184 } |
| 196 | 185 |
| 197 void ServiceWorkerCacheStorageManager::GetOriginUsage( | 186 void CacheStorageManager::GetOriginUsage( |
| 198 const GURL& origin_url, | 187 const GURL& origin_url, |
| 199 const storage::QuotaClient::GetUsageCallback& callback) { | 188 const storage::QuotaClient::GetUsageCallback& callback) { |
| 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 201 | 190 |
| 202 if (IsMemoryBacked()) { | 191 if (IsMemoryBacked()) { |
| 203 int64 sum = 0; | 192 int64 sum = 0; |
| 204 for (const auto& key_value : cache_storage_map_) | 193 for (const auto& key_value : cache_storage_map_) |
| 205 sum += key_value.second->MemoryBackedSize(); | 194 sum += key_value.second->MemoryBackedSize(); |
| 206 callback.Run(sum); | 195 callback.Run(sum); |
| 207 return; | 196 return; |
| 208 } | 197 } |
| 209 | 198 |
| 210 MigrateOrigin(origin_url); | 199 MigrateOrigin(origin_url); |
| 211 PostTaskAndReplyWithResult( | 200 PostTaskAndReplyWithResult( |
| 212 cache_task_runner_.get(), | 201 cache_task_runner_.get(), FROM_HERE, |
| 213 FROM_HERE, | |
| 214 base::Bind(base::ComputeDirectorySize, | 202 base::Bind(base::ComputeDirectorySize, |
| 215 ConstructOriginPath(root_path_, origin_url)), | 203 ConstructOriginPath(root_path_, origin_url)), |
| 216 base::Bind(callback)); | 204 base::Bind(callback)); |
| 217 } | 205 } |
| 218 | 206 |
| 219 void ServiceWorkerCacheStorageManager::GetOrigins( | 207 void CacheStorageManager::GetOrigins( |
| 220 const storage::QuotaClient::GetOriginsCallback& callback) { | 208 const storage::QuotaClient::GetOriginsCallback& callback) { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 209 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 222 | 210 |
| 223 if (IsMemoryBacked()) { | 211 if (IsMemoryBacked()) { |
| 224 std::set<GURL> origins; | 212 std::set<GURL> origins; |
| 225 for (const auto& key_value : cache_storage_map_) | 213 for (const auto& key_value : cache_storage_map_) |
| 226 origins.insert(key_value.first); | 214 origins.insert(key_value.first); |
| 227 | 215 |
| 228 callback.Run(origins); | 216 callback.Run(origins); |
| 229 return; | 217 return; |
| 230 } | 218 } |
| 231 | 219 |
| 232 PostTaskAndReplyWithResult(cache_task_runner_.get(), | 220 PostTaskAndReplyWithResult(cache_task_runner_.get(), FROM_HERE, |
| 233 FROM_HERE, | |
| 234 base::Bind(&ListOriginsOnDisk, root_path_), | 221 base::Bind(&ListOriginsOnDisk, root_path_), |
| 235 base::Bind(callback)); | 222 base::Bind(callback)); |
| 236 } | 223 } |
| 237 | 224 |
| 238 void ServiceWorkerCacheStorageManager::GetOriginsForHost( | 225 void CacheStorageManager::GetOriginsForHost( |
| 239 const std::string& host, | 226 const std::string& host, |
| 240 const storage::QuotaClient::GetOriginsCallback& callback) { | 227 const storage::QuotaClient::GetOriginsCallback& callback) { |
| 241 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 228 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 242 | 229 |
| 243 if (IsMemoryBacked()) { | 230 if (IsMemoryBacked()) { |
| 244 std::set<GURL> origins; | 231 std::set<GURL> origins; |
| 245 for (const auto& key_value : cache_storage_map_) { | 232 for (const auto& key_value : cache_storage_map_) { |
| 246 if (host == net::GetHostOrSpecFromURL(key_value.first)) | 233 if (host == net::GetHostOrSpecFromURL(key_value.first)) |
| 247 origins.insert(key_value.first); | 234 origins.insert(key_value.first); |
| 248 } | 235 } |
| 249 callback.Run(origins); | 236 callback.Run(origins); |
| 250 return; | 237 return; |
| 251 } | 238 } |
| 252 | 239 |
| 253 PostTaskAndReplyWithResult( | 240 PostTaskAndReplyWithResult( |
| 254 cache_task_runner_.get(), | 241 cache_task_runner_.get(), FROM_HERE, |
| 255 FROM_HERE, | |
| 256 base::Bind(&ListOriginsOnDisk, root_path_), | 242 base::Bind(&ListOriginsOnDisk, root_path_), |
| 257 base::Bind(&GetOriginsForHostDidListOrigins, host, callback)); | 243 base::Bind(&GetOriginsForHostDidListOrigins, host, callback)); |
| 258 } | 244 } |
| 259 | 245 |
| 260 void ServiceWorkerCacheStorageManager::DeleteOriginData( | 246 void CacheStorageManager::DeleteOriginData( |
| 261 const GURL& origin, | 247 const GURL& origin, |
| 262 const storage::QuotaClient::DeletionCallback& callback) { | 248 const storage::QuotaClient::DeletionCallback& callback) { |
| 263 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 249 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 264 | 250 |
| 265 ServiceWorkerCacheStorage* cache_storage = | 251 CacheStorage* cache_storage = FindOrCreateCacheStorage(origin); |
| 266 FindOrCreateServiceWorkerCacheManager(origin); | |
| 267 cache_storage_map_.erase(origin); | 252 cache_storage_map_.erase(origin); |
| 268 cache_storage->CloseAllCaches( | 253 cache_storage->CloseAllCaches( |
| 269 base::Bind(&ServiceWorkerCacheStorageManager::DeleteOriginDidClose, | 254 base::Bind(&CacheStorageManager::DeleteOriginDidClose, origin, callback, |
| 270 origin, callback, base::Passed(make_scoped_ptr(cache_storage)), | 255 base::Passed(make_scoped_ptr(cache_storage)), |
| 271 weak_ptr_factory_.GetWeakPtr())); | 256 weak_ptr_factory_.GetWeakPtr())); |
| 272 } | 257 } |
| 273 | 258 |
| 274 // static | 259 // static |
| 275 void ServiceWorkerCacheStorageManager::DeleteOriginDidClose( | 260 void CacheStorageManager::DeleteOriginDidClose( |
| 276 const GURL& origin, | 261 const GURL& origin, |
| 277 const storage::QuotaClient::DeletionCallback& callback, | 262 const storage::QuotaClient::DeletionCallback& callback, |
| 278 scoped_ptr<ServiceWorkerCacheStorage> cache_storage, | 263 scoped_ptr<CacheStorage> cache_storage, |
| 279 base::WeakPtr<ServiceWorkerCacheStorageManager> cache_manager) { | 264 base::WeakPtr<CacheStorageManager> cache_manager) { |
| 280 // TODO(jkarlin): Deleting the storage leaves any unfinished operations | 265 // TODO(jkarlin): Deleting the storage leaves any unfinished operations |
| 281 // hanging, resulting in unresolved promises. Fix this by guaranteeing that | 266 // hanging, resulting in unresolved promises. Fix this by guaranteeing that |
| 282 // callbacks are called in ServiceWorkerStorage. | 267 // callbacks are called in ServiceWorkerStorage. |
| 283 cache_storage.reset(); | 268 cache_storage.reset(); |
| 284 | 269 |
| 285 if (!cache_manager) { | 270 if (!cache_manager) { |
| 286 callback.Run(storage::kQuotaErrorAbort); | 271 callback.Run(storage::kQuotaErrorAbort); |
| 287 return; | 272 return; |
| 288 } | 273 } |
| 289 | 274 |
| 290 if (cache_manager->IsMemoryBacked()) { | 275 if (cache_manager->IsMemoryBacked()) { |
| 291 callback.Run(storage::kQuotaStatusOk); | 276 callback.Run(storage::kQuotaStatusOk); |
| 292 return; | 277 return; |
| 293 } | 278 } |
| 294 | 279 |
| 295 cache_manager->MigrateOrigin(origin); | 280 cache_manager->MigrateOrigin(origin); |
| 296 PostTaskAndReplyWithResult( | 281 PostTaskAndReplyWithResult( |
| 297 cache_manager->cache_task_runner_.get(), FROM_HERE, | 282 cache_manager->cache_task_runner_.get(), FROM_HERE, |
| 298 base::Bind(&DeleteDir, | 283 base::Bind(&DeleteDir, |
| 299 ConstructOriginPath(cache_manager->root_path_, origin)), | 284 ConstructOriginPath(cache_manager->root_path_, origin)), |
| 300 base::Bind(&DeleteOriginDidDeleteDir, callback)); | 285 base::Bind(&DeleteOriginDidDeleteDir, callback)); |
| 301 } | 286 } |
| 302 | 287 |
| 303 ServiceWorkerCacheStorageManager::ServiceWorkerCacheStorageManager( | 288 CacheStorageManager::CacheStorageManager( |
| 304 const base::FilePath& path, | 289 const base::FilePath& path, |
| 305 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, | 290 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner, |
| 306 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) | 291 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) |
| 307 : root_path_(path), | 292 : root_path_(path), |
| 308 cache_task_runner_(cache_task_runner), | 293 cache_task_runner_(cache_task_runner), |
| 309 quota_manager_proxy_(quota_manager_proxy), | 294 quota_manager_proxy_(quota_manager_proxy), |
| 310 request_context_(NULL), | 295 request_context_(NULL), |
| 311 weak_ptr_factory_(this) { | 296 weak_ptr_factory_(this) { |
| 312 if (quota_manager_proxy_.get()) { | 297 if (quota_manager_proxy_.get()) { |
| 313 quota_manager_proxy_->RegisterClient( | 298 quota_manager_proxy_->RegisterClient( |
| 314 new ServiceWorkerCacheQuotaClient(weak_ptr_factory_.GetWeakPtr())); | 299 new CacheStorageQuotaClient(weak_ptr_factory_.GetWeakPtr())); |
| 315 } | 300 } |
| 316 } | 301 } |
| 317 | 302 |
| 318 ServiceWorkerCacheStorage* | 303 CacheStorage* CacheStorageManager::FindOrCreateCacheStorage( |
| 319 ServiceWorkerCacheStorageManager::FindOrCreateServiceWorkerCacheManager( | |
| 320 const GURL& origin) { | 304 const GURL& origin) { |
| 321 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 305 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 322 DCHECK(request_context_); | 306 DCHECK(request_context_); |
| 323 ServiceWorkerCacheStorageMap::const_iterator it = | 307 CacheStorageMap::const_iterator it = cache_storage_map_.find(origin); |
| 324 cache_storage_map_.find(origin); | |
| 325 if (it == cache_storage_map_.end()) { | 308 if (it == cache_storage_map_.end()) { |
| 326 MigrateOrigin(origin); | 309 MigrateOrigin(origin); |
| 327 ServiceWorkerCacheStorage* cache_storage = | 310 CacheStorage* cache_storage = new CacheStorage( |
| 328 new ServiceWorkerCacheStorage(ConstructOriginPath(root_path_, origin), | 311 ConstructOriginPath(root_path_, origin), IsMemoryBacked(), |
| 329 IsMemoryBacked(), | 312 cache_task_runner_.get(), request_context_, quota_manager_proxy_, |
| 330 cache_task_runner_.get(), | 313 blob_context_, origin); |
| 331 request_context_, | |
| 332 quota_manager_proxy_, | |
| 333 blob_context_, | |
| 334 origin); | |
| 335 // The map owns fetch_stores. | 314 // The map owns fetch_stores. |
| 336 cache_storage_map_.insert(std::make_pair(origin, cache_storage)); | 315 cache_storage_map_.insert(std::make_pair(origin, cache_storage)); |
| 337 return cache_storage; | 316 return cache_storage; |
| 338 } | 317 } |
| 339 return it->second; | 318 return it->second; |
| 340 } | 319 } |
| 341 | 320 |
| 342 // static | 321 // static |
| 343 base::FilePath ServiceWorkerCacheStorageManager::ConstructLegacyOriginPath( | 322 base::FilePath CacheStorageManager::ConstructLegacyOriginPath( |
| 344 const base::FilePath& root_path, | 323 const base::FilePath& root_path, |
| 345 const GURL& origin) { | 324 const GURL& origin) { |
| 346 const std::string origin_hash = base::SHA1HashString(origin.spec()); | 325 const std::string origin_hash = base::SHA1HashString(origin.spec()); |
| 347 const std::string origin_hash_hex = base::StringToLowerASCII( | 326 const std::string origin_hash_hex = base::StringToLowerASCII( |
| 348 base::HexEncode(origin_hash.c_str(), origin_hash.length())); | 327 base::HexEncode(origin_hash.c_str(), origin_hash.length())); |
| 349 return root_path.AppendASCII(origin_hash_hex); | 328 return root_path.AppendASCII(origin_hash_hex); |
| 350 } | 329 } |
| 351 | 330 |
| 352 // static | 331 // static |
| 353 base::FilePath ServiceWorkerCacheStorageManager::ConstructOriginPath( | 332 base::FilePath CacheStorageManager::ConstructOriginPath( |
| 354 const base::FilePath& root_path, | 333 const base::FilePath& root_path, |
| 355 const GURL& origin) { | 334 const GURL& origin) { |
| 356 const std::string identifier = storage::GetIdentifierFromOrigin(origin); | 335 const std::string identifier = storage::GetIdentifierFromOrigin(origin); |
| 357 const std::string origin_hash = base::SHA1HashString(identifier); | 336 const std::string origin_hash = base::SHA1HashString(identifier); |
| 358 const std::string origin_hash_hex = base::StringToLowerASCII( | 337 const std::string origin_hash_hex = base::StringToLowerASCII( |
| 359 base::HexEncode(origin_hash.c_str(), origin_hash.length())); | 338 base::HexEncode(origin_hash.c_str(), origin_hash.length())); |
| 360 return root_path.AppendASCII(origin_hash_hex); | 339 return root_path.AppendASCII(origin_hash_hex); |
| 361 } | 340 } |
| 362 | 341 |
| 363 // Migrate from old origin-based path to storage identifier-based path. | 342 // Migrate from old origin-based path to storage identifier-based path. |
| 364 // TODO(jsbell); Remove after a few releases. | 343 // TODO(jsbell); Remove after a few releases. |
| 365 void ServiceWorkerCacheStorageManager::MigrateOrigin(const GURL& origin) { | 344 void CacheStorageManager::MigrateOrigin(const GURL& origin) { |
| 366 if (IsMemoryBacked()) | 345 if (IsMemoryBacked()) |
| 367 return; | 346 return; |
| 368 base::FilePath old_path = ConstructLegacyOriginPath(root_path_, origin); | 347 base::FilePath old_path = ConstructLegacyOriginPath(root_path_, origin); |
| 369 base::FilePath new_path = ConstructOriginPath(root_path_, origin); | 348 base::FilePath new_path = ConstructOriginPath(root_path_, origin); |
| 370 cache_task_runner_->PostTask( | 349 cache_task_runner_->PostTask( |
| 371 FROM_HERE, base::Bind(&MigrateOriginOnTaskRunner, old_path, new_path)); | 350 FROM_HERE, base::Bind(&MigrateOriginOnTaskRunner, old_path, new_path)); |
| 372 } | 351 } |
| 373 | 352 |
| 374 // static | 353 // static |
| 375 void ServiceWorkerCacheStorageManager::MigrateOriginOnTaskRunner( | 354 void CacheStorageManager::MigrateOriginOnTaskRunner( |
| 376 const base::FilePath& old_path, | 355 const base::FilePath& old_path, |
| 377 const base::FilePath& new_path) { | 356 const base::FilePath& new_path) { |
| 378 if (base::PathExists(old_path)) { | 357 if (base::PathExists(old_path)) { |
| 379 if (!base::PathExists(new_path)) | 358 if (!base::PathExists(new_path)) |
| 380 base::Move(old_path, new_path); | 359 base::Move(old_path, new_path); |
| 381 base::DeleteFile(old_path, /*recursive*/ true); | 360 base::DeleteFile(old_path, /*recursive*/ true); |
| 382 } | 361 } |
| 383 } | 362 } |
| 384 | 363 |
| 385 } // namespace content | 364 } // namespace content |
| OLD | NEW |