[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / cache_storage / cache_storage_cache.h
blobadac254a232a7c1c3f668836ebcd9180f84db9d3
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_CACHE_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
8 #include <vector>
10 #include "base/callback.h"
11 #include "base/files/file_path.h"
12 #include "base/id_map.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/common/cache_storage/cache_storage_types.h"
16 #include "content/common/service_worker/service_worker_types.h"
17 #include "net/disk_cache/disk_cache.h"
19 namespace net {
20 class URLRequestContextGetter;
21 class IOBufferWithSize;
24 namespace storage {
25 class BlobDataHandle;
26 class BlobStorageContext;
27 class QuotaManagerProxy;
30 namespace content {
32 class CacheStorageBlobToDiskCache;
33 class CacheMetadata;
34 class CacheStorageScheduler;
35 class TestCacheStorageCache;
37 // Represents a ServiceWorker Cache as seen in
38 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/
39 // The asynchronous methods are executed serially. Callbacks to the
40 // public functions will be called so long as the cache object lives.
41 class CONTENT_EXPORT CacheStorageCache
42 : public base::RefCounted<CacheStorageCache> {
43 public:
44 using ErrorCallback = base::Callback<void(CacheStorageError)>;
45 using ResponseCallback =
46 base::Callback<void(CacheStorageError,
47 scoped_ptr<ServiceWorkerResponse>,
48 scoped_ptr<storage::BlobDataHandle>)>;
49 using Responses = std::vector<ServiceWorkerResponse>;
50 using BlobDataHandles = std::vector<storage::BlobDataHandle>;
51 using ResponsesCallback = base::Callback<void(CacheStorageError,
52 scoped_ptr<Responses>,
53 scoped_ptr<BlobDataHandles>)>;
54 using Requests = std::vector<ServiceWorkerFetchRequest>;
55 using RequestsCallback =
56 base::Callback<void(CacheStorageError, scoped_ptr<Requests>)>;
58 static scoped_refptr<CacheStorageCache> CreateMemoryCache(
59 const GURL& origin,
60 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
61 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
62 base::WeakPtr<storage::BlobStorageContext> blob_context);
63 static scoped_refptr<CacheStorageCache> CreatePersistentCache(
64 const GURL& origin,
65 const base::FilePath& path,
66 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
67 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
68 base::WeakPtr<storage::BlobStorageContext> blob_context);
70 // Returns ERROR_TYPE_NOT_FOUND if not found.
71 void Match(scoped_ptr<ServiceWorkerFetchRequest> request,
72 const ResponseCallback& callback);
74 // Returns CACHE_STORAGE_OK and all responses in this cache. If there are no
75 // responses, returns CACHE_STORAGE_OK and an empty vector.
76 void MatchAll(const ResponsesCallback& callback);
78 // Runs given batch operations. This corresponds to the Batch Cache Operations
79 // algorithm in the spec.
81 // |operations| cannot mix PUT and DELETE operations and cannot contain
82 // multiple DELETE operations.
84 // In the case of the PUT operation, puts request and response objects in the
85 // cache and returns OK when all operations are successfully completed.
86 // In the case of the DELETE operation, returns ERROR_NOT_FOUND if a specified
87 // entry is not found. Otherwise deletes it and returns OK.
89 // TODO(nhiroki): This function should run all operations atomically.
90 // http://crbug.com/486637
91 void BatchOperation(const std::vector<CacheStorageBatchOperation>& operations,
92 const ErrorCallback& callback);
93 void BatchDidOneOperation(const base::Closure& barrier_closure,
94 ErrorCallback* callback,
95 CacheStorageError error);
96 void BatchDidAllOperations(scoped_ptr<ErrorCallback> callback);
98 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
99 // Returns CACHE_STORAGE_OK and a vector of requests if there are no errors.
100 void Keys(const RequestsCallback& callback);
102 // Closes the backend. Future operations that require the backend
103 // will exit early. Close should only be called once per CacheStorageCache.
104 void Close(const base::Closure& callback);
106 // The size of the cache contents in memory. Returns 0 if the cache backend is
107 // not a memory cache backend.
108 int64 MemoryBackedSize() const;
110 base::WeakPtr<CacheStorageCache> AsWeakPtr();
112 private:
113 friend class base::RefCounted<CacheStorageCache>;
114 friend class TestCacheStorageCache;
116 struct OpenAllEntriesContext;
117 struct MatchAllContext;
118 struct KeysContext;
119 struct PutContext;
121 // The backend progresses from uninitialized, to open, to closed, and cannot
122 // reverse direction. The open step may be skipped.
123 enum BackendState {
124 BACKEND_UNINITIALIZED, // No backend, create backend on first operation.
125 BACKEND_OPEN, // Backend can be used.
126 BACKEND_CLOSED // Backend cannot be used. All ops should fail.
129 using Entries = std::vector<disk_cache::Entry*>;
130 using ScopedBackendPtr = scoped_ptr<disk_cache::Backend>;
131 using BlobToDiskCacheIDMap =
132 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>;
133 using OpenAllEntriesCallback =
134 base::Callback<void(scoped_ptr<OpenAllEntriesContext>,
135 CacheStorageError)>;
137 CacheStorageCache(
138 const GURL& origin,
139 const base::FilePath& path,
140 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
141 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
142 base::WeakPtr<storage::BlobStorageContext> blob_context);
144 // Async operations in progress will cancel and not run their callbacks.
145 virtual ~CacheStorageCache();
147 // Returns true if the backend is ready to operate.
148 bool LazyInitialize();
150 // Returns all entries in this cache.
151 void OpenAllEntries(const OpenAllEntriesCallback& callback);
152 void DidOpenNextEntry(scoped_ptr<OpenAllEntriesContext> entries_context,
153 const OpenAllEntriesCallback& callback,
154 int rv);
156 // Match callbacks
157 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
158 const ResponseCallback& callback);
159 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request,
160 const ResponseCallback& callback,
161 scoped_ptr<disk_cache::Entry*> entry_ptr,
162 int rv);
163 void MatchDidReadMetadata(scoped_ptr<ServiceWorkerFetchRequest> request,
164 const ResponseCallback& callback,
165 disk_cache::ScopedEntryPtr entry,
166 scoped_ptr<CacheMetadata> headers);
168 // MatchAll callbacks
169 void MatchAllImpl(const ResponsesCallback& callback);
170 void MatchAllDidOpenAllEntries(
171 const ResponsesCallback& callback,
172 scoped_ptr<OpenAllEntriesContext> entries_context,
173 CacheStorageError error);
174 void MatchAllProcessNextEntry(scoped_ptr<MatchAllContext> context,
175 const Entries::iterator& iter);
176 void MatchAllDidReadMetadata(scoped_ptr<MatchAllContext> context,
177 const Entries::iterator& iter,
178 scoped_ptr<CacheMetadata> metadata);
180 // Puts the request and response object in the cache. The response body (if
181 // present) is stored in the cache, but not the request body. Returns OK on
182 // success.
183 void Put(const CacheStorageBatchOperation& operation,
184 const ErrorCallback& callback);
185 void PutImpl(scoped_ptr<PutContext> put_context);
186 void PutDidDelete(scoped_ptr<PutContext> put_context,
187 CacheStorageError delete_error);
188 void PutDidCreateEntry(scoped_ptr<disk_cache::Entry*> entry_ptr,
189 scoped_ptr<PutContext> put_context,
190 int rv);
191 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context,
192 int expected_bytes,
193 int rv);
194 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context,
195 BlobToDiskCacheIDMap::KeyType blob_to_cache_key,
196 disk_cache::ScopedEntryPtr entry,
197 bool success);
199 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK.
200 void Delete(const CacheStorageBatchOperation& operation,
201 const ErrorCallback& callback);
202 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request,
203 const ErrorCallback& callback);
204 void DeleteDidOpenEntry(
205 const GURL& origin,
206 scoped_ptr<ServiceWorkerFetchRequest> request,
207 const CacheStorageCache::ErrorCallback& callback,
208 scoped_ptr<disk_cache::Entry*> entryptr,
209 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
210 int rv);
212 // Keys callbacks.
213 void KeysImpl(const RequestsCallback& callback);
214 void KeysDidOpenAllEntries(const RequestsCallback& callback,
215 scoped_ptr<OpenAllEntriesContext> entries_context,
216 CacheStorageError error);
217 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context,
218 const Entries::iterator& iter);
219 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context,
220 const Entries::iterator& iter,
221 scoped_ptr<CacheMetadata> metadata);
223 void CloseImpl(const base::Closure& callback);
225 // Loads the backend and calls the callback with the result (true for
226 // success). The callback will always be called. Virtual for tests.
227 virtual void CreateBackend(const ErrorCallback& callback);
228 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback,
229 scoped_ptr<ScopedBackendPtr> backend_ptr,
230 int rv);
232 void InitBackend();
233 void InitDone(CacheStorageError error);
235 void PendingClosure(const base::Closure& callback);
236 void PendingErrorCallback(const ErrorCallback& callback,
237 CacheStorageError error);
238 void PendingResponseCallback(
239 const ResponseCallback& callback,
240 CacheStorageError error,
241 scoped_ptr<ServiceWorkerResponse> response,
242 scoped_ptr<storage::BlobDataHandle> blob_data_handle);
243 void PendingResponsesCallback(const ResponsesCallback& callback,
244 CacheStorageError error,
245 scoped_ptr<Responses> responses,
246 scoped_ptr<BlobDataHandles> blob_data_handles);
247 void PendingRequestsCallback(const RequestsCallback& callback,
248 CacheStorageError error,
249 scoped_ptr<Requests> requests);
251 void PopulateResponseMetadata(const CacheMetadata& metadata,
252 ServiceWorkerResponse* response);
253 scoped_ptr<storage::BlobDataHandle> PopulateResponseBody(
254 disk_cache::ScopedEntryPtr entry,
255 ServiceWorkerResponse* response);
257 // Be sure to check |backend_state_| before use.
258 scoped_ptr<disk_cache::Backend> backend_;
260 GURL origin_;
261 base::FilePath path_;
262 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
263 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
264 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
265 BackendState backend_state_;
266 scoped_ptr<CacheStorageScheduler> scheduler_;
267 bool initializing_;
269 // Owns the elements of the list
270 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_;
272 // Whether or not to store data in disk or memory.
273 bool memory_only_;
275 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_;
277 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache);
280 } // namespace content
282 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_