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 "base/memory/ref_counted.h"
15 #include "content/browser/cache_storage/cache_storage.h"
16 #include "content/common/content_export.h"
17 #include "content/public/browser/cache_storage_context.h"
18 #include "content/public/browser/cache_storage_usage_info.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "storage/browser/quota/quota_client.h"
24 class SequencedTaskRunner
;
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 // Map a database identifier (computed from an origin) to the path.
51 static base::FilePath
ConstructOriginPath(const base::FilePath
& root_path
,
54 virtual ~CacheStorageManager();
56 // Methods to support the CacheStorage spec. These methods call the
57 // corresponding CacheStorage method on the appropriate thread.
58 void OpenCache(const GURL
& origin
,
59 const std::string
& cache_name
,
60 const CacheStorage::CacheAndErrorCallback
& callback
);
61 void HasCache(const GURL
& origin
,
62 const std::string
& cache_name
,
63 const CacheStorage::BoolAndErrorCallback
& callback
);
64 void DeleteCache(const GURL
& origin
,
65 const std::string
& cache_name
,
66 const CacheStorage::BoolAndErrorCallback
& callback
);
67 void EnumerateCaches(const GURL
& origin
,
68 const CacheStorage::StringsAndErrorCallback
& callback
);
69 void MatchCache(const GURL
& origin
,
70 const std::string
& cache_name
,
71 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
72 const CacheStorageCache::ResponseCallback
& callback
);
73 void MatchAllCaches(const GURL
& origin
,
74 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
75 const CacheStorageCache::ResponseCallback
& callback
);
77 // This must be called before creating any of the public *Cache functions
79 void SetBlobParametersForCache(
80 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
81 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
);
83 base::WeakPtr
<CacheStorageManager
> AsWeakPtr() {
84 return weak_ptr_factory_
.GetWeakPtr();
88 friend class CacheStorageContextImpl
;
89 friend class CacheStorageManagerTest
;
90 friend class CacheStorageMigrationTest
;
91 friend class CacheStorageQuotaClient
;
93 typedef std::map
<GURL
, CacheStorage
*> CacheStorageMap
;
96 const base::FilePath
& path
,
97 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
98 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
);
100 // The returned CacheStorage* is owned by this manager.
101 CacheStorage
* FindOrCreateCacheStorage(const GURL
& origin
);
103 // QuotaClient and Browsing Data Deletion support
104 void GetAllOriginsUsage(
105 const CacheStorageContext::GetUsageInfoCallback
& callback
);
106 void GetOriginUsage(const GURL
& origin_url
,
107 const storage::QuotaClient::GetUsageCallback
& callback
);
108 void GetOrigins(const storage::QuotaClient::GetOriginsCallback
& callback
);
109 void GetOriginsForHost(
110 const std::string
& host
,
111 const storage::QuotaClient::GetOriginsCallback
& callback
);
112 void DeleteOriginData(const GURL
& origin
,
113 const storage::QuotaClient::DeletionCallback
& callback
);
114 void DeleteOriginData(const GURL
& origin
);
115 static void DeleteOriginDidClose(
117 const storage::QuotaClient::DeletionCallback
& callback
,
118 scoped_ptr
<CacheStorage
> cache_storage
,
119 base::WeakPtr
<CacheStorageManager
> cache_manager
);
121 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter()
123 return request_context_getter_
;
125 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context() const {
126 return blob_context_
;
128 base::FilePath
root_path() const { return root_path_
; }
129 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner() const {
130 return cache_task_runner_
;
133 bool IsMemoryBacked() const { return root_path_
.empty(); }
135 // Map a origin to the path. Exposed for testing.
136 static base::FilePath
ConstructLegacyOriginPath(
137 const base::FilePath
& root_path
,
140 // Migrate from old origin-based path to storage identifier-based path.
141 // TODO(jsbell); Remove method and all calls after a few releases.
142 void MigrateOrigin(const GURL
& origin
);
143 static void MigrateOriginOnTaskRunner(const base::FilePath
& old_path
,
144 const base::FilePath
& new_path
);
146 base::FilePath root_path_
;
147 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner_
;
149 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
151 // The map owns the CacheStorages and the CacheStorages are only accessed on
152 // |cache_task_runner_|.
153 CacheStorageMap cache_storage_map_
;
155 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
156 base::WeakPtr
<storage::BlobStorageContext
> blob_context_
;
158 base::WeakPtrFactory
<CacheStorageManager
> weak_ptr_factory_
;
159 DISALLOW_COPY_AND_ASSIGN(CacheStorageManager
);
162 } // namespace content
164 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_