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 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "content/browser/cache_storage/cache_storage.h"
15 #include "content/common/content_export.h"
16 #include "storage/browser/quota/quota_client.h"
20 class SequencedTaskRunner
;
24 class URLRequestContext
;
28 class BlobStorageContext
;
29 class QuotaManagerProxy
;
34 class CacheStorageQuotaClient
;
36 // Keeps track of a CacheStorage per origin. There is one
37 // CacheStorageManager per ServiceWorkerContextCore.
38 // TODO(jkarlin): Remove CacheStorage from memory once they're no
39 // longer in active use.
40 class CONTENT_EXPORT CacheStorageManager
{
42 static scoped_ptr
<CacheStorageManager
> Create(
43 const base::FilePath
& path
,
44 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
45 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
);
47 static scoped_ptr
<CacheStorageManager
> Create(
48 CacheStorageManager
* old_manager
);
50 virtual ~CacheStorageManager();
52 // Methods to support the CacheStorage spec. These methods call the
53 // corresponding CacheStorage method on the appropriate thread.
54 void OpenCache(const GURL
& origin
,
55 const std::string
& cache_name
,
56 const CacheStorage::CacheAndErrorCallback
& callback
);
57 void HasCache(const GURL
& origin
,
58 const std::string
& cache_name
,
59 const CacheStorage::BoolAndErrorCallback
& callback
);
60 void DeleteCache(const GURL
& origin
,
61 const std::string
& cache_name
,
62 const CacheStorage::BoolAndErrorCallback
& callback
);
63 void EnumerateCaches(const GURL
& origin
,
64 const CacheStorage::StringsAndErrorCallback
& callback
);
65 void MatchCache(const GURL
& origin
,
66 const std::string
& cache_name
,
67 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
68 const CacheStorageCache::ResponseCallback
& callback
);
69 void MatchAllCaches(const GURL
& origin
,
70 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
71 const CacheStorageCache::ResponseCallback
& callback
);
73 // This must be called before creating any of the public *Cache functions
75 void SetBlobParametersForCache(
76 net::URLRequestContext
* request_context
,
77 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
);
79 base::WeakPtr
<CacheStorageManager
> AsWeakPtr() {
80 return weak_ptr_factory_
.GetWeakPtr();
84 friend class CacheStorageQuotaClient
;
85 friend class CacheStorageManagerTest
;
86 friend class CacheStorageMigrationTest
;
88 typedef std::map
<GURL
, CacheStorage
*> CacheStorageMap
;
91 const base::FilePath
& path
,
92 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
93 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
);
95 // The returned CacheStorage* is owned by this manager.
96 CacheStorage
* FindOrCreateCacheStorage(const GURL
& origin
);
98 // QuotaClient support
99 void GetOriginUsage(const GURL
& origin_url
,
100 const storage::QuotaClient::GetUsageCallback
& callback
);
101 void GetOrigins(const storage::QuotaClient::GetOriginsCallback
& callback
);
102 void GetOriginsForHost(
103 const std::string
& host
,
104 const storage::QuotaClient::GetOriginsCallback
& callback
);
105 void DeleteOriginData(const GURL
& origin
,
106 const storage::QuotaClient::DeletionCallback
& callback
);
107 static void DeleteOriginDidClose(
109 const storage::QuotaClient::DeletionCallback
& callback
,
110 scoped_ptr
<CacheStorage
> cache_storage
,
111 base::WeakPtr
<CacheStorageManager
> cache_manager
);
113 net::URLRequestContext
* url_request_context() const {
114 return request_context_
;
116 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context() const {
117 return blob_context_
;
119 base::FilePath
root_path() const { return root_path_
; }
120 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner() const {
121 return cache_task_runner_
;
124 bool IsMemoryBacked() const { return root_path_
.empty(); }
126 // Map a origin to the path. Exposed for testing.
127 static base::FilePath
ConstructLegacyOriginPath(
128 const base::FilePath
& root_path
,
130 // Map a database identifier (computed from an origin) to the path. Exposed
132 static base::FilePath
ConstructOriginPath(const base::FilePath
& root_path
,
135 // Migrate from old origin-based path to storage identifier-based path.
136 // TODO(jsbell); Remove method and all calls after a few releases.
137 void MigrateOrigin(const GURL
& origin
);
138 static void MigrateOriginOnTaskRunner(const base::FilePath
& old_path
,
139 const base::FilePath
& new_path
);
141 base::FilePath root_path_
;
142 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner_
;
144 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
146 // The map owns the CacheStorages and the CacheStorages are only accessed on
147 // |cache_task_runner_|.
148 CacheStorageMap cache_storage_map_
;
150 net::URLRequestContext
* request_context_
;
151 base::WeakPtr
<storage::BlobStorageContext
> blob_context_
;
153 base::WeakPtrFactory
<CacheStorageManager
> weak_ptr_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(CacheStorageManager
);
157 } // namespace content
159 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_