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