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/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/browser/cache_storage/cache_storage_cache.h"
18 class SequencedTaskRunner
;
22 class URLRequestContextGetter
;
26 class BlobStorageContext
;
30 class CacheStorageScheduler
;
32 // TODO(jkarlin): Constrain the total bytes used per origin.
34 // CacheStorage holds the set of caches for a given origin. It is
35 // owned by the CacheStorageManager. This class expects to be run
36 // on the IO thread. The asynchronous methods are executed serially.
37 class CONTENT_EXPORT CacheStorage
{
39 typedef std::vector
<std::string
> StringVector
;
40 typedef base::Callback
<void(bool, CacheStorageError
)> BoolAndErrorCallback
;
41 typedef base::Callback
<void(const scoped_refptr
<CacheStorageCache
>&,
42 CacheStorageError
)> CacheAndErrorCallback
;
43 typedef base::Callback
<void(const StringVector
&, CacheStorageError
)>
44 StringsAndErrorCallback
;
46 static const char kIndexFileName
[];
49 const base::FilePath
& origin_path
,
51 base::SequencedTaskRunner
* cache_task_runner
,
52 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
53 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
54 base::WeakPtr
<storage::BlobStorageContext
> blob_context
,
57 // Any unfinished asynchronous operations may not complete or call their
59 virtual ~CacheStorage();
61 // Get the cache for the given key. If the cache is not found it is
63 void OpenCache(const std::string
& cache_name
,
64 const CacheAndErrorCallback
& callback
);
66 // Calls the callback with whether or not the cache exists.
67 void HasCache(const std::string
& cache_name
,
68 const BoolAndErrorCallback
& callback
);
70 // Deletes the cache if it exists. If it doesn't exist,
71 // CACHE_STORAGE_ERROR_NOT_FOUND is returned.
72 void DeleteCache(const std::string
& cache_name
,
73 const BoolAndErrorCallback
& callback
);
75 // Calls the callback with a vector of cache names (keys) available.
76 void EnumerateCaches(const StringsAndErrorCallback
& callback
);
78 // Calls match on the cache with the given |cache_name|.
79 void MatchCache(const std::string
& cache_name
,
80 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
81 const CacheStorageCache::ResponseCallback
& callback
);
83 // Calls match on all of the caches in parallel, calling |callback| with the
84 // first response found. Note that if multiple caches have the same
85 // request/response then it is not defined which cache's response will be
86 // returned. If no response is found then |callback| is called with
87 // CACHE_STORAGE_ERROR_NOT_FOUND.
88 void MatchAllCaches(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
89 const CacheStorageCache::ResponseCallback
& callback
);
91 // Calls close on each cache and runs the callback after all of them have
93 void CloseAllCaches(const base::Closure
& callback
);
95 // The size of all of the origin's contents in memory. Returns 0 if the cache
96 // backend is not a memory backend. Runs synchronously.
97 int64
MemoryBackedSize() const;
99 // The functions below are for tests to verify that the operations run
101 void StartAsyncOperationForTesting();
102 void CompleteAsyncOperationForTesting();
106 class SimpleCacheLoader
;
109 typedef std::map
<std::string
, base::WeakPtr
<CacheStorageCache
>> CacheMap
;
111 // Return a CacheStorageCache for the given name if the name is known. If the
112 // CacheStorageCache has been deleted, creates a new one.
113 scoped_refptr
<CacheStorageCache
> GetLoadedCache(
114 const std::string
& cache_name
);
116 // Initializer and its callback are below.
119 void LazyInitDidLoadIndex(
120 scoped_ptr
<std::vector
<std::string
>> indexed_cache_names
);
122 // The Open and CreateCache callbacks are below.
123 void OpenCacheImpl(const std::string
& cache_name
,
124 const CacheAndErrorCallback
& callback
);
125 void CreateCacheDidCreateCache(const std::string
& cache_name
,
126 const CacheAndErrorCallback
& callback
,
127 const scoped_refptr
<CacheStorageCache
>& cache
);
128 void CreateCacheDidWriteIndex(const CacheAndErrorCallback
& callback
,
129 const scoped_refptr
<CacheStorageCache
>& cache
,
132 // The HasCache callbacks are below.
133 void HasCacheImpl(const std::string
& cache_name
,
134 const BoolAndErrorCallback
& callback
);
136 // The DeleteCache callbacks are below.
137 void DeleteCacheImpl(const std::string
& cache_name
,
138 const BoolAndErrorCallback
& callback
);
140 void DeleteCacheDidClose(const std::string
& cache_name
,
141 const BoolAndErrorCallback
& callback
,
142 const StringVector
& ordered_cache_names
,
143 const scoped_refptr
<CacheStorageCache
>& cache
);
144 void DeleteCacheDidWriteIndex(const std::string
& cache_name
,
145 const BoolAndErrorCallback
& callback
,
147 void DeleteCacheDidCleanUp(const BoolAndErrorCallback
& callback
,
150 // The EnumerateCache callbacks are below.
151 void EnumerateCachesImpl(const StringsAndErrorCallback
& callback
);
153 // The MatchCache callbacks are below.
154 void MatchCacheImpl(const std::string
& cache_name
,
155 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
156 const CacheStorageCache::ResponseCallback
& callback
);
157 void MatchCacheDidMatch(const scoped_refptr
<CacheStorageCache
>& cache
,
158 const CacheStorageCache::ResponseCallback
& callback
,
159 CacheStorageError error
,
160 scoped_ptr
<ServiceWorkerResponse
> response
,
161 scoped_ptr
<storage::BlobDataHandle
> handle
);
163 // The MatchAllCaches callbacks are below.
164 void MatchAllCachesImpl(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
165 const CacheStorageCache::ResponseCallback
& callback
);
166 void MatchAllCachesDidMatch(scoped_refptr
<CacheStorageCache
> cache
,
167 const base::Closure
& barrier_closure
,
168 CacheStorageCache::ResponseCallback
* callback
,
169 CacheStorageError error
,
170 scoped_ptr
<ServiceWorkerResponse
> response
,
171 scoped_ptr
<storage::BlobDataHandle
> handle
);
172 void MatchAllCachesDidMatchAll(
173 scoped_ptr
<CacheStorageCache::ResponseCallback
> callback
);
175 // The CloseAllCaches callbacks are below.
176 void CloseAllCachesImpl(const base::Closure
& callback
);
178 void PendingClosure(const base::Closure
& callback
);
179 void PendingBoolAndErrorCallback(const BoolAndErrorCallback
& callback
,
181 CacheStorageError error
);
182 void PendingCacheAndErrorCallback(
183 const CacheAndErrorCallback
& callback
,
184 const scoped_refptr
<CacheStorageCache
>& cache
,
185 CacheStorageError error
);
186 void PendingStringsAndErrorCallback(const StringsAndErrorCallback
& callback
,
187 const StringVector
& strings
,
188 CacheStorageError error
);
189 void PendingResponseCallback(
190 const CacheStorageCache::ResponseCallback
& callback
,
191 CacheStorageError error
,
192 scoped_ptr
<ServiceWorkerResponse
> response
,
193 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
);
195 // Whether or not we've loaded the list of cache names into memory.
199 // The pending operation scheduler.
200 scoped_ptr
<CacheStorageScheduler
> scheduler_
;
202 // The map of cache names to CacheStorageCache objects.
205 // The names of caches in the order that they were created.
206 StringVector ordered_cache_names_
;
208 // The file path for this CacheStorage.
209 base::FilePath origin_path_
;
211 // The TaskRunner to run file IO on.
212 scoped_refptr
<base::SequencedTaskRunner
> cache_task_runner_
;
214 // Whether or not to store data in disk or memory.
217 // Performs backend specific operations (memory vs disk).
218 scoped_ptr
<CacheLoader
> cache_loader_
;
220 base::WeakPtrFactory
<CacheStorage
> weak_factory_
;
222 DISALLOW_COPY_AND_ASSIGN(CacheStorage
);
225 } // namespace content
227 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_