OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // The asynchronous methods are executed serially. Callbacks to the | 39 // The asynchronous methods are executed serially. Callbacks to the |
40 // public functions will be called so long as the cache object lives. | 40 // public functions will be called so long as the cache object lives. |
41 class CONTENT_EXPORT CacheStorageCache | 41 class CONTENT_EXPORT CacheStorageCache |
42 : public base::RefCounted<CacheStorageCache> { | 42 : public base::RefCounted<CacheStorageCache> { |
43 public: | 43 public: |
44 using ErrorCallback = base::Callback<void(CacheStorageError)>; | 44 using ErrorCallback = base::Callback<void(CacheStorageError)>; |
45 using ResponseCallback = | 45 using ResponseCallback = |
46 base::Callback<void(CacheStorageError, | 46 base::Callback<void(CacheStorageError, |
47 scoped_ptr<ServiceWorkerResponse>, | 47 scoped_ptr<ServiceWorkerResponse>, |
48 scoped_ptr<storage::BlobDataHandle>)>; | 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>)>; |
49 using Requests = std::vector<ServiceWorkerFetchRequest>; | 54 using Requests = std::vector<ServiceWorkerFetchRequest>; |
50 using RequestsCallback = | 55 using RequestsCallback = |
51 base::Callback<void(CacheStorageError, scoped_ptr<Requests>)>; | 56 base::Callback<void(CacheStorageError, scoped_ptr<Requests>)>; |
52 | 57 |
53 static scoped_refptr<CacheStorageCache> CreateMemoryCache( | 58 static scoped_refptr<CacheStorageCache> CreateMemoryCache( |
54 const GURL& origin, | 59 const GURL& origin, |
55 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 60 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
56 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 61 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
57 base::WeakPtr<storage::BlobStorageContext> blob_context); | 62 base::WeakPtr<storage::BlobStorageContext> blob_context); |
58 static scoped_refptr<CacheStorageCache> CreatePersistentCache( | 63 static scoped_refptr<CacheStorageCache> CreatePersistentCache( |
59 const GURL& origin, | 64 const GURL& origin, |
60 const base::FilePath& path, | 65 const base::FilePath& path, |
61 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 66 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
62 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 67 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
63 base::WeakPtr<storage::BlobStorageContext> blob_context); | 68 base::WeakPtr<storage::BlobStorageContext> blob_context); |
64 | 69 |
65 // Returns ERROR_TYPE_NOT_FOUND if not found. | 70 // Returns ERROR_TYPE_NOT_FOUND if not found. |
66 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 71 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
67 const ResponseCallback& callback); | 72 const ResponseCallback& callback); |
68 | 73 |
| 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); |
| 77 |
69 // Runs given batch operations. This corresponds to the Batch Cache Operations | 78 // Runs given batch operations. This corresponds to the Batch Cache Operations |
70 // algorithm in the spec. | 79 // algorithm in the spec. |
71 // | 80 // |
72 // |operations| cannot mix PUT and DELETE operations and cannot contain | 81 // |operations| cannot mix PUT and DELETE operations and cannot contain |
73 // multiple DELETE operations. | 82 // multiple DELETE operations. |
74 // | 83 // |
75 // In the case of the PUT operation, puts request and response objects in the | 84 // 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. | 85 // 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 | 86 // 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. | 87 // entry is not found. Otherwise deletes it and returns OK. |
(...skipping 18 matching lines...) Expand all Loading... |
97 // The size of the cache contents in memory. Returns 0 if the cache backend is | 106 // The size of the cache contents in memory. Returns 0 if the cache backend is |
98 // not a memory cache backend. | 107 // not a memory cache backend. |
99 int64 MemoryBackedSize() const; | 108 int64 MemoryBackedSize() const; |
100 | 109 |
101 base::WeakPtr<CacheStorageCache> AsWeakPtr(); | 110 base::WeakPtr<CacheStorageCache> AsWeakPtr(); |
102 | 111 |
103 private: | 112 private: |
104 friend class base::RefCounted<CacheStorageCache>; | 113 friend class base::RefCounted<CacheStorageCache>; |
105 friend class TestCacheStorageCache; | 114 friend class TestCacheStorageCache; |
106 | 115 |
| 116 struct OpenAllEntriesContext; |
| 117 struct MatchAllContext; |
107 struct KeysContext; | 118 struct KeysContext; |
108 struct PutContext; | 119 struct PutContext; |
109 | 120 |
110 // The backend progresses from uninitialized, to open, to closed, and cannot | 121 // The backend progresses from uninitialized, to open, to closed, and cannot |
111 // reverse direction. The open step may be skipped. | 122 // reverse direction. The open step may be skipped. |
112 enum BackendState { | 123 enum BackendState { |
113 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. | 124 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. |
114 BACKEND_OPEN, // Backend can be used. | 125 BACKEND_OPEN, // Backend can be used. |
115 BACKEND_CLOSED // Backend cannot be used. All ops should fail. | 126 BACKEND_CLOSED // Backend cannot be used. All ops should fail. |
116 }; | 127 }; |
117 | 128 |
118 using Entries = std::vector<disk_cache::Entry*>; | 129 using Entries = std::vector<disk_cache::Entry*>; |
119 using ScopedBackendPtr = scoped_ptr<disk_cache::Backend>; | 130 using ScopedBackendPtr = scoped_ptr<disk_cache::Backend>; |
120 using BlobToDiskCacheIDMap = | 131 using BlobToDiskCacheIDMap = |
121 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>; | 132 IDMap<CacheStorageBlobToDiskCache, IDMapOwnPointer>; |
| 133 using OpenAllEntriesCallback = |
| 134 base::Callback<void(scoped_ptr<OpenAllEntriesContext>, |
| 135 CacheStorageError)>; |
122 | 136 |
123 CacheStorageCache( | 137 CacheStorageCache( |
124 const GURL& origin, | 138 const GURL& origin, |
125 const base::FilePath& path, | 139 const base::FilePath& path, |
126 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 140 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
127 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 141 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
128 base::WeakPtr<storage::BlobStorageContext> blob_context); | 142 base::WeakPtr<storage::BlobStorageContext> blob_context); |
129 | 143 |
130 // Async operations in progress will cancel and not run their callbacks. | 144 // Async operations in progress will cancel and not run their callbacks. |
131 virtual ~CacheStorageCache(); | 145 virtual ~CacheStorageCache(); |
132 | 146 |
133 // Returns true if the backend is ready to operate. | 147 // Returns true if the backend is ready to operate. |
134 bool LazyInitialize(); | 148 bool LazyInitialize(); |
135 | 149 |
| 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); |
| 155 |
136 // Match callbacks | 156 // Match callbacks |
137 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 157 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
138 const ResponseCallback& callback); | 158 const ResponseCallback& callback); |
139 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, | 159 void MatchDidOpenEntry(scoped_ptr<ServiceWorkerFetchRequest> request, |
140 const ResponseCallback& callback, | 160 const ResponseCallback& callback, |
141 scoped_ptr<disk_cache::Entry*> entry_ptr, | 161 scoped_ptr<disk_cache::Entry*> entry_ptr, |
142 int rv); | 162 int rv); |
143 void MatchDidReadMetadata(scoped_ptr<ServiceWorkerFetchRequest> request, | 163 void MatchDidReadMetadata(scoped_ptr<ServiceWorkerFetchRequest> request, |
144 const ResponseCallback& callback, | 164 const ResponseCallback& callback, |
145 disk_cache::ScopedEntryPtr entry, | 165 disk_cache::ScopedEntryPtr entry, |
146 scoped_ptr<CacheMetadata> headers); | 166 scoped_ptr<CacheMetadata> headers); |
147 | 167 |
| 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); |
| 179 |
148 // Puts the request and response object in the cache. The response body (if | 180 // 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 | 181 // present) is stored in the cache, but not the request body. Returns OK on |
150 // success. | 182 // success. |
151 void Put(const CacheStorageBatchOperation& operation, | 183 void Put(const CacheStorageBatchOperation& operation, |
152 const ErrorCallback& callback); | 184 const ErrorCallback& callback); |
153 void PutImpl(scoped_ptr<PutContext> put_context); | 185 void PutImpl(scoped_ptr<PutContext> put_context); |
154 void PutDidDelete(scoped_ptr<PutContext> put_context, | 186 void PutDidDelete(scoped_ptr<PutContext> put_context, |
155 CacheStorageError delete_error); | 187 CacheStorageError delete_error); |
156 void PutDidCreateEntry(scoped_ptr<disk_cache::Entry*> entry_ptr, | 188 void PutDidCreateEntry(scoped_ptr<disk_cache::Entry*> entry_ptr, |
157 scoped_ptr<PutContext> put_context, | 189 scoped_ptr<PutContext> put_context, |
(...skipping 14 matching lines...) Expand all Loading... |
172 void DeleteDidOpenEntry( | 204 void DeleteDidOpenEntry( |
173 const GURL& origin, | 205 const GURL& origin, |
174 scoped_ptr<ServiceWorkerFetchRequest> request, | 206 scoped_ptr<ServiceWorkerFetchRequest> request, |
175 const CacheStorageCache::ErrorCallback& callback, | 207 const CacheStorageCache::ErrorCallback& callback, |
176 scoped_ptr<disk_cache::Entry*> entryptr, | 208 scoped_ptr<disk_cache::Entry*> entryptr, |
177 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 209 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
178 int rv); | 210 int rv); |
179 | 211 |
180 // Keys callbacks. | 212 // Keys callbacks. |
181 void KeysImpl(const RequestsCallback& callback); | 213 void KeysImpl(const RequestsCallback& callback); |
182 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv); | 214 void KeysDidOpenAllEntries(const RequestsCallback& callback, |
| 215 scoped_ptr<OpenAllEntriesContext> entries_context, |
| 216 CacheStorageError error); |
183 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 217 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
184 const Entries::iterator& iter); | 218 const Entries::iterator& iter); |
185 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, | 219 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, |
186 const Entries::iterator& iter, | 220 const Entries::iterator& iter, |
187 scoped_ptr<CacheMetadata> metadata); | 221 scoped_ptr<CacheMetadata> metadata); |
188 | 222 |
189 void CloseImpl(const base::Closure& callback); | 223 void CloseImpl(const base::Closure& callback); |
190 | 224 |
191 // Loads the backend and calls the callback with the result (true for | 225 // Loads the backend and calls the callback with the result (true for |
192 // success). The callback will always be called. Virtual for tests. | 226 // success). The callback will always be called. Virtual for tests. |
193 virtual void CreateBackend(const ErrorCallback& callback); | 227 virtual void CreateBackend(const ErrorCallback& callback); |
194 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback, | 228 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback, |
195 scoped_ptr<ScopedBackendPtr> backend_ptr, | 229 scoped_ptr<ScopedBackendPtr> backend_ptr, |
196 int rv); | 230 int rv); |
197 | 231 |
198 void InitBackend(); | 232 void InitBackend(); |
199 void InitDone(CacheStorageError error); | 233 void InitDone(CacheStorageError error); |
200 | 234 |
201 void PendingClosure(const base::Closure& callback); | 235 void PendingClosure(const base::Closure& callback); |
202 void PendingErrorCallback(const ErrorCallback& callback, | 236 void PendingErrorCallback(const ErrorCallback& callback, |
203 CacheStorageError error); | 237 CacheStorageError error); |
204 void PendingResponseCallback( | 238 void PendingResponseCallback( |
205 const ResponseCallback& callback, | 239 const ResponseCallback& callback, |
206 CacheStorageError error, | 240 CacheStorageError error, |
207 scoped_ptr<ServiceWorkerResponse> response, | 241 scoped_ptr<ServiceWorkerResponse> response, |
208 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 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); |
209 void PendingRequestsCallback(const RequestsCallback& callback, | 247 void PendingRequestsCallback(const RequestsCallback& callback, |
210 CacheStorageError error, | 248 CacheStorageError error, |
211 scoped_ptr<Requests> requests); | 249 scoped_ptr<Requests> requests); |
212 | 250 |
| 251 void PopulateResponseMetadata(const CacheMetadata& metadata, |
| 252 ServiceWorkerResponse* response); |
| 253 scoped_ptr<storage::BlobDataHandle> PopulateResponseBody( |
| 254 disk_cache::ScopedEntryPtr entry, |
| 255 ServiceWorkerResponse* response); |
| 256 |
213 // Be sure to check |backend_state_| before use. | 257 // Be sure to check |backend_state_| before use. |
214 scoped_ptr<disk_cache::Backend> backend_; | 258 scoped_ptr<disk_cache::Backend> backend_; |
215 | 259 |
216 GURL origin_; | 260 GURL origin_; |
217 base::FilePath path_; | 261 base::FilePath path_; |
218 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 262 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
219 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 263 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
220 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 264 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
221 BackendState backend_state_; | 265 BackendState backend_state_; |
222 scoped_ptr<CacheStorageScheduler> scheduler_; | 266 scoped_ptr<CacheStorageScheduler> scheduler_; |
223 bool initializing_; | 267 bool initializing_; |
224 | 268 |
225 // Owns the elements of the list | 269 // Owns the elements of the list |
226 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; | 270 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; |
227 | 271 |
228 // Whether or not to store data in disk or memory. | 272 // Whether or not to store data in disk or memory. |
229 bool memory_only_; | 273 bool memory_only_; |
230 | 274 |
231 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; | 275 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; |
232 | 276 |
233 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); | 277 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); |
234 }; | 278 }; |
235 | 279 |
236 } // namespace content | 280 } // namespace content |
237 | 281 |
238 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ | 282 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
OLD | NEW |