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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/service_worker_cache_storage_manager.h"
10 #include "base/bind.h"
11 #include "base/id_map.h"
12 #include "base/sha1.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "content/browser/service_worker/service_worker_cache_storage.h"
16 #include "content/browser/service_worker/service_worker_context_core.h"
17 #include "content/public/browser/browser_thread.h"
22 base::FilePath ConstructOriginPath(const base::FilePath& root_path,
24 std::string origin_hash = base::SHA1HashString(origin.spec());
25 std::string origin_hash_hex = base::StringToLowerASCII(
26 base::HexEncode(origin_hash.c_str(), origin_hash.length()));
27 return root_path.AppendASCII(origin_hash_hex);
35 scoped_ptr<ServiceWorkerCacheStorageManager>
36 ServiceWorkerCacheStorageManager::Create(
37 const base::FilePath& path,
38 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner) {
39 base::FilePath root_path = path;
41 root_path = path.Append(ServiceWorkerContextCore::kServiceWorkerDirectory)
42 .AppendASCII("CacheStorage");
45 return make_scoped_ptr(
46 new ServiceWorkerCacheStorageManager(root_path, cache_task_runner));
50 scoped_ptr<ServiceWorkerCacheStorageManager>
51 ServiceWorkerCacheStorageManager::Create(
52 ServiceWorkerCacheStorageManager* old_manager) {
53 scoped_ptr<ServiceWorkerCacheStorageManager> manager(
54 new ServiceWorkerCacheStorageManager(old_manager->root_path(),
55 old_manager->cache_task_runner()));
56 // These values may be NULL, in which case this will be called again later by
57 // the dispatcher host per usual.
58 manager->SetBlobParametersForCache(old_manager->url_request_context(),
59 old_manager->blob_storage_context());
60 return manager.Pass();
63 ServiceWorkerCacheStorageManager::~ServiceWorkerCacheStorageManager() {
64 for (ServiceWorkerCacheStorageMap::iterator it = cache_storage_map_.begin();
65 it != cache_storage_map_.end();
71 void ServiceWorkerCacheStorageManager::CreateCache(
73 const std::string& cache_name,
74 const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback) {
75 DCHECK_CURRENTLY_ON(BrowserThread::IO);
77 ServiceWorkerCacheStorage* cache_storage =
78 FindOrCreateServiceWorkerCacheManager(origin);
80 cache_storage->CreateCache(cache_name, callback);
83 void ServiceWorkerCacheStorageManager::GetCache(
85 const std::string& cache_name,
86 const ServiceWorkerCacheStorage::CacheAndErrorCallback& callback) {
87 DCHECK_CURRENTLY_ON(BrowserThread::IO);
89 ServiceWorkerCacheStorage* cache_storage =
90 FindOrCreateServiceWorkerCacheManager(origin);
92 cache_storage->GetCache(cache_name, callback);
95 void ServiceWorkerCacheStorageManager::HasCache(
97 const std::string& cache_name,
98 const ServiceWorkerCacheStorage::BoolAndErrorCallback& callback) {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO);
101 ServiceWorkerCacheStorage* cache_storage =
102 FindOrCreateServiceWorkerCacheManager(origin);
103 cache_storage->HasCache(cache_name, callback);
106 void ServiceWorkerCacheStorageManager::DeleteCache(
108 const std::string& cache_name,
109 const ServiceWorkerCacheStorage::BoolAndErrorCallback& callback) {
110 DCHECK_CURRENTLY_ON(BrowserThread::IO);
112 ServiceWorkerCacheStorage* cache_storage =
113 FindOrCreateServiceWorkerCacheManager(origin);
114 cache_storage->DeleteCache(cache_name, callback);
117 void ServiceWorkerCacheStorageManager::EnumerateCaches(
119 const ServiceWorkerCacheStorage::StringsAndErrorCallback& callback) {
120 DCHECK_CURRENTLY_ON(BrowserThread::IO);
122 ServiceWorkerCacheStorage* cache_storage =
123 FindOrCreateServiceWorkerCacheManager(origin);
125 cache_storage->EnumerateCaches(callback);
128 void ServiceWorkerCacheStorageManager::SetBlobParametersForCache(
129 net::URLRequestContext* request_context,
130 base::WeakPtr<storage::BlobStorageContext> blob_storage_context) {
131 DCHECK_CURRENTLY_ON(BrowserThread::IO);
132 DCHECK(cache_storage_map_.empty());
133 DCHECK(!request_context_ || request_context_ == request_context);
134 DCHECK(!blob_context_ || blob_context_.get() == blob_storage_context.get());
135 request_context_ = request_context;
136 blob_context_ = blob_storage_context;
139 ServiceWorkerCacheStorageManager::ServiceWorkerCacheStorageManager(
140 const base::FilePath& path,
141 const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner)
143 cache_task_runner_(cache_task_runner),
144 request_context_(NULL) {
147 ServiceWorkerCacheStorage*
148 ServiceWorkerCacheStorageManager::FindOrCreateServiceWorkerCacheManager(
149 const GURL& origin) {
150 DCHECK_CURRENTLY_ON(BrowserThread::IO);
151 DCHECK(request_context_);
153 ServiceWorkerCacheStorageMap::const_iterator it =
154 cache_storage_map_.find(origin);
155 if (it == cache_storage_map_.end()) {
156 bool memory_only = root_path_.empty();
157 ServiceWorkerCacheStorage* cache_storage =
158 new ServiceWorkerCacheStorage(ConstructOriginPath(root_path_, origin),
160 cache_task_runner_.get(),
163 // The map owns fetch_stores.
164 cache_storage_map_.insert(std::make_pair(origin, cache_storage));
165 return cache_storage;
170 } // namespace content