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_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/weak_ptr.h"
14 #include "content/browser/cache_storage/cache_storage_cache.h"
17 class SequencedTaskRunner
;
21 class URLRequestContext
;
25 class BlobStorageContext
;
29 class CacheStorageScheduler
;
31 // TODO(jkarlin): Constrain the total bytes used per origin.
33 // CacheStorage holds the set of caches for a given origin. It is
34 // owned by the CacheStorageManager. This class expects to be run
35 // on the IO thread. The asynchronous methods are executed serially.
36 class CONTENT_EXPORT CacheStorage
{
38 enum CacheStorageError
{
39 CACHE_STORAGE_ERROR_NO_ERROR
,
40 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED
,
41 CACHE_STORAGE_ERROR_NOT_FOUND
,
42 CACHE_STORAGE_ERROR_EXISTS
,
43 CACHE_STORAGE_ERROR_STORAGE
,
44 CACHE_STORAGE_ERROR_CLOSING
46 typedef std::vector
<std::string
> StringVector
;
47 typedef base::Callback
<void(bool, CacheStorageError
)> BoolAndErrorCallback
;
48 typedef base::Callback
<void(const scoped_refptr
<CacheStorageCache
>&,
49 CacheStorageError
)> CacheAndErrorCallback
;
50 typedef base::Callback
<void(const StringVector
&, CacheStorageError
)>
51 StringsAndErrorCallback
;
53 static const char kIndexFileName
[];
56 const base::FilePath
& origin_path
,
58 base::SequencedTaskRunner
* cache_task_runner
,
59 net::URLRequestContext
* request_context
,
60 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
61 base::WeakPtr
<storage::BlobStorageContext
> blob_context
,
64 // Any unfinished asynchronous operations may not complete or call their
66 virtual ~CacheStorage();
68 // Get the cache for the given key. If the cache is not found it is
70 void OpenCache(const std::string
& cache_name
,
71 const CacheAndErrorCallback
& callback
);
73 // Calls the callback with whether or not the cache exists.
74 void HasCache(const std::string
& cache_name
,
75 const BoolAndErrorCallback
& callback
);
77 // Deletes the cache if it exists. If it doesn't exist,
78 // CACHE_STORAGE_ERROR_NOT_FOUND is returned.
79 void DeleteCache(const std::string
& cache_name
,
80 const BoolAndErrorCallback
& callback
);
82 // Calls the callback with a vector of cache names (keys) available.
83 void EnumerateCaches(const StringsAndErrorCallback
& callback
);
85 // Calls match on the cache with the given |cache_name|.
86 void MatchCache(const std::string
& cache_name
,
87 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
88 const CacheStorageCache::ResponseCallback
& callback
);
90 // Calls match on all of the caches in parallel, calling |callback| with the
91 // first response found. Note that if multiple caches have the same
92 // request/response then it is not defined which cache's response will be
93 // returned. If no response is found then |callback| is called with
94 // CacheStorageCache::ErrorTypeNotFound.
95 void MatchAllCaches(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
96 const CacheStorageCache::ResponseCallback
& callback
);
98 // Calls close on each cache and runs the callback after all of them have
100 void CloseAllCaches(const base::Closure
& callback
);
102 // The size of all of the origin's contents in memory. Returns 0 if the cache
103 // backend is not a memory backend. Runs synchronously.
104 int64
MemoryBackedSize() const;
106 // The functions below are for tests to verify that the operations run
108 void StartAsyncOperationForTesting();
109 void CompleteAsyncOperationForTesting();
113 class SimpleCacheLoader
;
116 typedef std::map
<std::string
, base::WeakPtr
<CacheStorageCache
>> CacheMap
;
118 // Return a CacheStorageCache for the given name if the name is known. If the
119 // CacheStorageCache has been deleted, creates a new one.
120 scoped_refptr
<CacheStorageCache
> GetLoadedCache(
121 const std::string
& cache_name
);
123 // Initializer and its callback are below.
126 void LazyInitDidLoadIndex(
127 scoped_ptr
<std::vector
<std::string
>> indexed_cache_names
);
129 // The Open and CreateCache callbacks are below.
130 void OpenCacheImpl(const std::string
& cache_name
,
131 const CacheAndErrorCallback
& callback
);
132 void CreateCacheDidCreateCache(const std::string
& cache_name
,
133 const CacheAndErrorCallback
& callback
,
134 const scoped_refptr
<CacheStorageCache
>& cache
);
135 void CreateCacheDidWriteIndex(const CacheAndErrorCallback
& callback
,
136 const scoped_refptr
<CacheStorageCache
>& cache
,
139 // The HasCache callbacks are below.
140 void HasCacheImpl(const std::string
& cache_name
,
141 const BoolAndErrorCallback
& callback
);
143 // The DeleteCache callbacks are below.
144 void DeleteCacheImpl(const std::string
& cache_name
,
145 const BoolAndErrorCallback
& callback
);
147 void DeleteCacheDidClose(const std::string
& cache_name
,
148 const BoolAndErrorCallback
& callback
,
149 const StringVector
& ordered_cache_names
,
150 const scoped_refptr
<CacheStorageCache
>& cache
);
151 void DeleteCacheDidWriteIndex(const std::string
& cache_name
,
152 const BoolAndErrorCallback
& callback
,
154 void DeleteCacheDidCleanUp(const BoolAndErrorCallback
& callback
,
157 // The EnumerateCache callbacks are below.
158 void EnumerateCachesImpl(const StringsAndErrorCallback
& callback
);
160 // The MatchCache callbacks are below.
161 void MatchCacheImpl(const std::string
& cache_name
,
162 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
163 const CacheStorageCache::ResponseCallback
& callback
);
164 void MatchCacheDidMatch(const scoped_refptr
<CacheStorageCache
>& cache
,
165 const CacheStorageCache::ResponseCallback
& callback
,
166 CacheStorageCache::ErrorType error
,
167 scoped_ptr
<ServiceWorkerResponse
> response
,
168 scoped_ptr
<storage::BlobDataHandle
> handle
);
170 // The MatchAllCaches callbacks are below.
171 void MatchAllCachesImpl(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
172 const CacheStorageCache::ResponseCallback
& callback
);
173 void MatchAllCachesDidMatch(scoped_refptr
<CacheStorageCache
> cache
,
174 const base::Closure
& barrier_closure
,
175 CacheStorageCache::ResponseCallback
* callback
,
176 CacheStorageCache::ErrorType error
,
177 scoped_ptr
<ServiceWorkerResponse
> response
,
178 scoped_ptr
<storage::BlobDataHandle
> handle
);
179 void MatchAllCachesDidMatchAll(
180 scoped_ptr
<CacheStorageCache::ResponseCallback
> callback
);
182 // The CloseAllCaches callbacks are below.
183 void CloseAllCachesImpl(const base::Closure
& callback
);
185 void PendingClosure(const base::Closure
& callback
);
186 void PendingBoolAndErrorCallback(const BoolAndErrorCallback
& callback
,
188 CacheStorageError error
);
189 void PendingCacheAndErrorCallback(
190 const CacheAndErrorCallback
& callback
,
191 const scoped_refptr
<CacheStorageCache
>& cache
,
192 CacheStorageError error
);
193 void PendingStringsAndErrorCallback(const StringsAndErrorCallback
& callback
,
194 const StringVector
& strings
,
195 CacheStorageError error
);
196 void PendingResponseCallback(
197 const CacheStorageCache::ResponseCallback
& callback
,
198 CacheStorageCache::ErrorType error
,
199 scoped_ptr
<ServiceWorkerResponse
> response
,
200 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
);
202 // Whether or not we've loaded the list of cache names into memory.
206 // The pending operation scheduler.
207 scoped_ptr
<CacheStorageScheduler
> scheduler_
;
209 // The map of cache names to CacheStorageCache objects.
212 // The names of caches in the order that they were created.
213 StringVector ordered_cache_names_
;
215 // The file path for this CacheStorage.
216 base::FilePath origin_path_
;
218 // The TaskRunner to run file IO on.
219 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner_
;
221 // Whether or not to store data in disk or memory.
224 // Performs backend specific operations (memory vs disk).
225 scoped_ptr
<CacheLoader
> cache_loader_
;
227 base::WeakPtrFactory
<CacheStorage
> weak_factory_
;
229 DISALLOW_COPY_AND_ASSIGN(CacheStorage
);
232 } // namespace content
234 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_