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 "net/url_request/url_request_context_getter.h"
18 #include "storage/browser/quota/quota_client.h"
22 class SequencedTaskRunner
;
26 class BlobStorageContext
;
27 class QuotaManagerProxy
;
32 class CacheStorageQuotaClient
;
34 // Keeps track of a CacheStorage per origin. There is one
35 // CacheStorageManager per ServiceWorkerContextCore.
36 // TODO(jkarlin): Remove CacheStorage from memory once they're no
37 // longer in active use.
38 class CONTENT_EXPORT CacheStorageManager
{
40 static scoped_ptr
<CacheStorageManager
> Create(
41 const base::FilePath
& path
,
42 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
43 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
);
45 static scoped_ptr
<CacheStorageManager
> Create(
46 CacheStorageManager
* old_manager
);
48 virtual ~CacheStorageManager();
50 // Methods to support the CacheStorage spec. These methods call the
51 // corresponding CacheStorage method on the appropriate thread.
52 void OpenCache(const GURL
& origin
,
53 const std::string
& cache_name
,
54 const CacheStorage::CacheAndErrorCallback
& callback
);
55 void HasCache(const GURL
& origin
,
56 const std::string
& cache_name
,
57 const CacheStorage::BoolAndErrorCallback
& callback
);
58 void DeleteCache(const GURL
& origin
,
59 const std::string
& cache_name
,
60 const CacheStorage::BoolAndErrorCallback
& callback
);
61 void EnumerateCaches(const GURL
& origin
,
62 const CacheStorage::StringsAndErrorCallback
& callback
);
63 void MatchCache(const GURL
& origin
,
64 const std::string
& cache_name
,
65 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
66 const CacheStorageCache::ResponseCallback
& callback
);
67 void MatchAllCaches(const GURL
& origin
,
68 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
69 const CacheStorageCache::ResponseCallback
& callback
);
71 // This must be called before creating any of the public *Cache functions
73 void SetBlobParametersForCache(
74 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
75 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context
);
77 base::WeakPtr
<CacheStorageManager
> AsWeakPtr() {
78 return weak_ptr_factory_
.GetWeakPtr();
82 friend class CacheStorageQuotaClient
;
83 friend class CacheStorageManagerTest
;
84 friend class CacheStorageMigrationTest
;
86 typedef std::map
<GURL
, CacheStorage
*> CacheStorageMap
;
89 const base::FilePath
& path
,
90 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner
,
91 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
);
93 // The returned CacheStorage* is owned by this manager.
94 CacheStorage
* FindOrCreateCacheStorage(const GURL
& origin
);
96 // QuotaClient support
97 void GetOriginUsage(const GURL
& origin_url
,
98 const storage::QuotaClient::GetUsageCallback
& callback
);
99 void GetOrigins(const storage::QuotaClient::GetOriginsCallback
& callback
);
100 void GetOriginsForHost(
101 const std::string
& host
,
102 const storage::QuotaClient::GetOriginsCallback
& callback
);
103 void DeleteOriginData(const GURL
& origin
,
104 const storage::QuotaClient::DeletionCallback
& callback
);
105 static void DeleteOriginDidClose(
107 const storage::QuotaClient::DeletionCallback
& callback
,
108 scoped_ptr
<CacheStorage
> cache_storage
,
109 base::WeakPtr
<CacheStorageManager
> cache_manager
);
111 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter()
113 return request_context_getter_
;
115 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context() const {
116 return blob_context_
;
118 base::FilePath
root_path() const { return root_path_
; }
119 const scoped_refptr
<base::SequencedTaskRunner
>& cache_task_runner() const {
120 return cache_task_runner_
;
123 bool IsMemoryBacked() const { return root_path_
.empty(); }
125 // Map a origin to the path. Exposed for testing.
126 static base::FilePath
ConstructLegacyOriginPath(
127 const base::FilePath
& root_path
,
129 // Map a database identifier (computed from an origin) to the path. Exposed
131 static base::FilePath
ConstructOriginPath(const base::FilePath
& root_path
,
134 // Migrate from old origin-based path to storage identifier-based path.
135 // TODO(jsbell); Remove method and all calls after a few releases.
136 void MigrateOrigin(const GURL
& origin
);
137 static void MigrateOriginOnTaskRunner(const base::FilePath
& old_path
,
138 const base::FilePath
& new_path
);
140 base::FilePath root_path_
;
141 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner_
;
143 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
145 // The map owns the CacheStorages and the CacheStorages are only accessed on
146 // |cache_task_runner_|.
147 CacheStorageMap cache_storage_map_
;
149 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
150 base::WeakPtr
<storage::BlobStorageContext
> blob_context_
;
152 base::WeakPtrFactory
<CacheStorageManager
> weak_ptr_factory_
;
153 DISALLOW_COPY_AND_ASSIGN(CacheStorageManager
);
156 } // namespace content
158 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_