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_
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"
20 class URLRequestContextGetter
;
21 class IOBufferWithSize
;
26 class BlobStorageContext
;
27 class QuotaManagerProxy
;
32 class CacheStorageBlobToDiskCache
;
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
> {
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 Requests
= std::vector
<ServiceWorkerFetchRequest
>;
50 using RequestsCallback
=
51 base::Callback
<void(CacheStorageError
, scoped_ptr
<Requests
>)>;
53 static scoped_refptr
<CacheStorageCache
> CreateMemoryCache(
55 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
56 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
57 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
58 static scoped_refptr
<CacheStorageCache
> CreatePersistentCache(
60 const base::FilePath
& path
,
61 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
62 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
63 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
65 // Returns ERROR_TYPE_NOT_FOUND if not found.
66 void Match(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
67 const ResponseCallback
& callback
);
69 // Runs given batch operations. This corresponds to the Batch Cache Operations
70 // algorithm in the spec.
72 // |operations| cannot mix PUT and DELETE operations and cannot contain
73 // multiple DELETE operations.
75 // In the case of the PUT operation, puts request and response objects in the
76 // cache and returns OK when all operations are successfully completed.
77 // In the case of the DELETE operation, returns ERROR_NOT_FOUND if a specified
78 // entry is not found. Otherwise deletes it and returns OK.
80 // TODO(nhiroki): This function should run all operations atomically.
81 // http://crbug.com/486637
82 void BatchOperation(const std::vector
<CacheStorageBatchOperation
>& operations
,
83 const ErrorCallback
& callback
);
84 void BatchDidOneOperation(const base::Closure
& barrier_closure
,
85 ErrorCallback
* callback
,
86 CacheStorageError error
);
87 void BatchDidAllOperations(scoped_ptr
<ErrorCallback
> callback
);
89 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest.
90 // Returns CACHE_STORAGE_OK and a vector of requests if there are no errors.
91 void Keys(const RequestsCallback
& callback
);
93 // Closes the backend. Future operations that require the backend
94 // will exit early. Close should only be called once per CacheStorageCache.
95 void Close(const base::Closure
& callback
);
97 // The size of the cache contents in memory. Returns 0 if the cache backend is
98 // not a memory cache backend.
99 int64
MemoryBackedSize() const;
101 base::WeakPtr
<CacheStorageCache
> AsWeakPtr();
104 friend class base::RefCounted
<CacheStorageCache
>;
105 friend class TestCacheStorageCache
;
110 // The backend progresses from uninitialized, to open, to closed, and cannot
111 // reverse direction. The open step may be skipped.
113 BACKEND_UNINITIALIZED
, // No backend, create backend on first operation.
114 BACKEND_OPEN
, // Backend can be used.
115 BACKEND_CLOSED
// Backend cannot be used. All ops should fail.
118 using Entries
= std::vector
<disk_cache::Entry
*>;
119 using ScopedBackendPtr
= scoped_ptr
<disk_cache::Backend
>;
120 using BlobToDiskCacheIDMap
=
121 IDMap
<CacheStorageBlobToDiskCache
, IDMapOwnPointer
>;
125 const base::FilePath
& path
,
126 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
127 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
128 base::WeakPtr
<storage::BlobStorageContext
> blob_context
);
130 // Async operations in progress will cancel and not run their callbacks.
131 virtual ~CacheStorageCache();
133 // Returns true if the backend is ready to operate.
134 bool LazyInitialize();
137 void MatchImpl(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
138 const ResponseCallback
& callback
);
139 void MatchDidOpenEntry(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
140 const ResponseCallback
& callback
,
141 scoped_ptr
<disk_cache::Entry
*> entry_ptr
,
143 void MatchDidReadMetadata(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
144 const ResponseCallback
& callback
,
145 disk_cache::ScopedEntryPtr entry
,
146 scoped_ptr
<CacheMetadata
> headers
);
148 // Puts the request and response object in the cache. The response body (if
149 // present) is stored in the cache, but not the request body. Returns OK on
151 void Put(const CacheStorageBatchOperation
& operation
,
152 const ErrorCallback
& callback
);
153 void PutImpl(scoped_ptr
<PutContext
> put_context
);
154 void PutDidDelete(scoped_ptr
<PutContext
> put_context
,
155 CacheStorageError delete_error
);
156 void PutDidCreateEntry(scoped_ptr
<disk_cache::Entry
*> entry_ptr
,
157 scoped_ptr
<PutContext
> put_context
,
159 void PutDidWriteHeaders(scoped_ptr
<PutContext
> put_context
,
162 void PutDidWriteBlobToCache(scoped_ptr
<PutContext
> put_context
,
163 BlobToDiskCacheIDMap::KeyType blob_to_cache_key
,
164 disk_cache::ScopedEntryPtr entry
,
167 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK.
168 void Delete(const CacheStorageBatchOperation
& operation
,
169 const ErrorCallback
& callback
);
170 void DeleteImpl(scoped_ptr
<ServiceWorkerFetchRequest
> request
,
171 const ErrorCallback
& callback
);
172 void DeleteDidOpenEntry(
174 scoped_ptr
<ServiceWorkerFetchRequest
> request
,
175 const CacheStorageCache::ErrorCallback
& callback
,
176 scoped_ptr
<disk_cache::Entry
*> entryptr
,
177 const scoped_refptr
<storage::QuotaManagerProxy
>& quota_manager_proxy
,
181 void KeysImpl(const RequestsCallback
& callback
);
182 void KeysDidOpenNextEntry(scoped_ptr
<KeysContext
> keys_context
, int rv
);
183 void KeysProcessNextEntry(scoped_ptr
<KeysContext
> keys_context
,
184 const Entries::iterator
& iter
);
185 void KeysDidReadMetadata(scoped_ptr
<KeysContext
> keys_context
,
186 const Entries::iterator
& iter
,
187 scoped_ptr
<CacheMetadata
> metadata
);
189 void CloseImpl(const base::Closure
& callback
);
191 // Loads the backend and calls the callback with the result (true for
192 // success). The callback will always be called. Virtual for tests.
193 virtual void CreateBackend(const ErrorCallback
& callback
);
194 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback
& callback
,
195 scoped_ptr
<ScopedBackendPtr
> backend_ptr
,
199 void InitDone(CacheStorageError error
);
201 void PendingClosure(const base::Closure
& callback
);
202 void PendingErrorCallback(const ErrorCallback
& callback
,
203 CacheStorageError error
);
204 void PendingResponseCallback(
205 const ResponseCallback
& callback
,
206 CacheStorageError error
,
207 scoped_ptr
<ServiceWorkerResponse
> response
,
208 scoped_ptr
<storage::BlobDataHandle
> blob_data_handle
);
209 void PendingRequestsCallback(const RequestsCallback
& callback
,
210 CacheStorageError error
,
211 scoped_ptr
<Requests
> requests
);
213 // Be sure to check |backend_state_| before use.
214 scoped_ptr
<disk_cache::Backend
> backend_
;
217 base::FilePath path_
;
218 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
219 scoped_refptr
<storage::QuotaManagerProxy
> quota_manager_proxy_
;
220 base::WeakPtr
<storage::BlobStorageContext
> blob_storage_context_
;
221 BackendState backend_state_
;
222 scoped_ptr
<CacheStorageScheduler
> scheduler_
;
225 // Owns the elements of the list
226 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_
;
228 // Whether or not to store data in disk or memory.
231 base::WeakPtrFactory
<CacheStorageCache
> weak_ptr_factory_
;
233 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache
);
236 } // namespace content
238 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_