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_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
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/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "content/common/cache_storage/cache_storage_types.h" |
14 #include "content/common/service_worker/service_worker_types.h" | 15 #include "content/common/service_worker/service_worker_types.h" |
15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
16 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 class URLRequestContext; | 20 class URLRequestContext; |
20 class IOBufferWithSize; | 21 class IOBufferWithSize; |
21 } | 22 } |
22 | 23 |
23 namespace storage { | 24 namespace storage { |
24 class BlobDataBuilder; | 25 class BlobDataBuilder; |
25 class BlobDataHandle; | 26 class BlobDataHandle; |
26 class BlobStorageContext; | 27 class BlobStorageContext; |
27 class QuotaManagerProxy; | 28 class QuotaManagerProxy; |
28 } | 29 } |
29 | 30 |
30 namespace content { | 31 namespace content { |
31 class ChromeBlobStorageContext; | 32 class ChromeBlobStorageContext; |
32 class ServiceWorkerCacheMetadata; | 33 class CacheMetadata; |
33 class ServiceWorkerCacheScheduler; | 34 class CacheStorageScheduler; |
34 class TestServiceWorkerCache; | 35 class TestCacheStorageCache; |
35 | 36 |
36 // Represents a ServiceWorker Cache as seen in | 37 // Represents a ServiceWorker Cache as seen in |
37 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 38 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/ |
38 // The asynchronous methods are executed serially. Callbacks to the | 39 // The asynchronous methods are executed serially. Callbacks to the |
39 // 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. |
40 class CONTENT_EXPORT ServiceWorkerCache | 41 class CONTENT_EXPORT CacheStorageCache |
41 : public base::RefCounted<ServiceWorkerCache> { | 42 : public base::RefCounted<CacheStorageCache> { |
42 public: | 43 public: |
43 // This enum is used in histograms, so do not change the ordering and always | 44 // This enum is used in histograms, so do not change the ordering and always |
44 // append new types to the end. | 45 // append new types to the end. |
45 enum ErrorType { | 46 enum ErrorType { |
46 ERROR_TYPE_OK = 0, | 47 ERROR_TYPE_OK = 0, |
47 ERROR_TYPE_EXISTS, | 48 ERROR_TYPE_EXISTS, |
48 ERROR_TYPE_STORAGE, | 49 ERROR_TYPE_STORAGE, |
49 ERROR_TYPE_NOT_FOUND, | 50 ERROR_TYPE_NOT_FOUND, |
50 ERROR_TYPE_LAST = ERROR_TYPE_NOT_FOUND | 51 ERROR_TYPE_LAST = ERROR_TYPE_NOT_FOUND |
51 }; | 52 }; |
52 | 53 |
53 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 54 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
54 typedef base::Callback<void(ErrorType)> ErrorCallback; | 55 typedef base::Callback<void(ErrorType)> ErrorCallback; |
55 typedef base::Callback<void(ErrorType, | 56 typedef base::Callback<void(ErrorType, |
56 scoped_ptr<ServiceWorkerResponse>, | 57 scoped_ptr<ServiceWorkerResponse>, |
57 scoped_ptr<storage::BlobDataHandle>)> | 58 scoped_ptr<storage::BlobDataHandle>)> |
58 ResponseCallback; | 59 ResponseCallback; |
59 typedef std::vector<ServiceWorkerFetchRequest> Requests; | 60 typedef std::vector<ServiceWorkerFetchRequest> Requests; |
60 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)> | 61 typedef base::Callback<void(ErrorType, scoped_ptr<Requests>)> |
61 RequestsCallback; | 62 RequestsCallback; |
62 | 63 |
63 static scoped_refptr<ServiceWorkerCache> CreateMemoryCache( | 64 static scoped_refptr<CacheStorageCache> CreateMemoryCache( |
64 const GURL& origin, | 65 const GURL& origin, |
65 net::URLRequestContext* request_context, | 66 net::URLRequestContext* request_context, |
66 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 67 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
67 base::WeakPtr<storage::BlobStorageContext> blob_context); | 68 base::WeakPtr<storage::BlobStorageContext> blob_context); |
68 static scoped_refptr<ServiceWorkerCache> CreatePersistentCache( | 69 static scoped_refptr<CacheStorageCache> CreatePersistentCache( |
69 const GURL& origin, | 70 const GURL& origin, |
70 const base::FilePath& path, | 71 const base::FilePath& path, |
71 net::URLRequestContext* request_context, | 72 net::URLRequestContext* request_context, |
72 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 73 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
73 base::WeakPtr<storage::BlobStorageContext> blob_context); | 74 base::WeakPtr<storage::BlobStorageContext> blob_context); |
74 | 75 |
75 // Returns ERROR_TYPE_NOT_FOUND if not found. | 76 // Returns ERROR_TYPE_NOT_FOUND if not found. |
76 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, | 77 void Match(scoped_ptr<ServiceWorkerFetchRequest> request, |
77 const ResponseCallback& callback); | 78 const ResponseCallback& callback); |
78 | 79 |
79 // Puts the request and response object in the cache. The response body (if | 80 // Puts the request and response object in the cache. The response body (if |
80 // present) is stored in the cache, but not the request body. Returns | 81 // present) is stored in the cache, but not the request body. Returns |
81 // ERROR_TYPE_OK on success. | 82 // ERROR_TYPE_OK on success. |
82 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, | 83 void Put(scoped_ptr<ServiceWorkerFetchRequest> request, |
83 scoped_ptr<ServiceWorkerResponse> response, | 84 scoped_ptr<ServiceWorkerResponse> response, |
84 const ResponseCallback& callback); | 85 const ResponseCallback& callback); |
85 | 86 |
86 // Returns ErrorNotFound if not found. Otherwise deletes and returns | 87 // Returns ErrorNotFound if not found. Otherwise deletes and returns |
87 // ERROR_TYPE_OK. | 88 // ERROR_TYPE_OK. |
88 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, | 89 void Delete(scoped_ptr<ServiceWorkerFetchRequest> request, |
89 const ErrorCallback& callback); | 90 const ErrorCallback& callback); |
90 | 91 |
91 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. | 92 // TODO(jkarlin): Have keys take an optional ServiceWorkerFetchRequest. |
92 // Returns ErrorTypeOK and a vector of requests if there are no errors. | 93 // Returns ErrorTypeOK and a vector of requests if there are no errors. |
93 void Keys(const RequestsCallback& callback); | 94 void Keys(const RequestsCallback& callback); |
94 | 95 |
95 // Closes the backend. Future operations that require the backend | 96 // Closes the backend. Future operations that require the backend |
96 // will exit early. Close should only be called once per ServiceWorkerCache. | 97 // will exit early. Close should only be called once per CacheStorageCache. |
97 void Close(const base::Closure& callback); | 98 void Close(const base::Closure& callback); |
98 | 99 |
99 // The size of the cache contents in memory. Returns 0 if the cache backend is | 100 // The size of the cache contents in memory. Returns 0 if the cache backend is |
100 // not a memory cache backend. | 101 // not a memory cache backend. |
101 int64 MemoryBackedSize() const; | 102 int64 MemoryBackedSize() const; |
102 | 103 |
103 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 104 base::WeakPtr<CacheStorageCache> AsWeakPtr(); |
104 | 105 |
105 private: | 106 private: |
106 friend class base::RefCounted<ServiceWorkerCache>; | 107 friend class base::RefCounted<CacheStorageCache>; |
107 friend class TestServiceWorkerCache; | 108 friend class TestCacheStorageCache; |
108 | 109 |
109 class BlobReader; | 110 class BlobReader; |
110 struct KeysContext; | 111 struct KeysContext; |
111 struct MatchContext; | 112 struct MatchContext; |
112 struct PutContext; | 113 struct PutContext; |
113 | 114 |
114 // The backend progresses from uninitialized, to open, to closed, and cannot | 115 // The backend progresses from uninitialized, to open, to closed, and cannot |
115 // reverse direction. The open step may be skipped. | 116 // reverse direction. The open step may be skipped. |
116 enum BackendState { | 117 enum BackendState { |
117 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. | 118 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. |
118 BACKEND_OPEN, // Backend can be used. | 119 BACKEND_OPEN, // Backend can be used. |
119 BACKEND_CLOSED // Backend cannot be used. All ops should fail. | 120 BACKEND_CLOSED // Backend cannot be used. All ops should fail. |
120 }; | 121 }; |
121 | 122 |
122 typedef std::vector<disk_cache::Entry*> Entries; | 123 typedef std::vector<disk_cache::Entry*> Entries; |
123 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; | 124 typedef scoped_ptr<disk_cache::Backend> ScopedBackendPtr; |
124 | 125 |
125 ServiceWorkerCache( | 126 CacheStorageCache( |
126 const GURL& origin, | 127 const GURL& origin, |
127 const base::FilePath& path, | 128 const base::FilePath& path, |
128 net::URLRequestContext* request_context, | 129 net::URLRequestContext* request_context, |
129 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 130 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
130 base::WeakPtr<storage::BlobStorageContext> blob_context); | 131 base::WeakPtr<storage::BlobStorageContext> blob_context); |
131 | 132 |
132 // Async operations in progress will cancel and not run their callbacks. | 133 // Async operations in progress will cancel and not run their callbacks. |
133 virtual ~ServiceWorkerCache(); | 134 virtual ~CacheStorageCache(); |
134 | 135 |
135 // Match callbacks | 136 // Match callbacks |
136 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 137 void MatchImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
137 const ResponseCallback& callback); | 138 const ResponseCallback& callback); |
138 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); | 139 void MatchDidOpenEntry(scoped_ptr<MatchContext> match_context, int rv); |
139 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, | 140 void MatchDidReadMetadata(scoped_ptr<MatchContext> match_context, |
140 scoped_ptr<ServiceWorkerCacheMetadata> headers); | 141 scoped_ptr<CacheMetadata> headers); |
141 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, | 142 void MatchDidReadResponseBodyData(scoped_ptr<MatchContext> match_context, |
142 int rv); | 143 int rv); |
143 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); | 144 void MatchDoneWithBody(scoped_ptr<MatchContext> match_context); |
144 | 145 |
145 // Put callbacks. | 146 // Put callbacks. |
146 void PutImpl(scoped_ptr<PutContext> put_context); | 147 void PutImpl(scoped_ptr<PutContext> put_context); |
147 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error); | 148 void PutDidDelete(scoped_ptr<PutContext> put_context, ErrorType delete_error); |
148 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); | 149 void PutDidCreateEntry(scoped_ptr<PutContext> put_context, int rv); |
149 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, | 150 void PutDidWriteHeaders(scoped_ptr<PutContext> put_context, |
150 int expected_bytes, | 151 int expected_bytes, |
151 int rv); | 152 int rv); |
152 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, | 153 void PutDidWriteBlobToCache(scoped_ptr<PutContext> put_context, |
153 scoped_ptr<BlobReader> blob_reader, | 154 scoped_ptr<BlobReader> blob_reader, |
154 disk_cache::ScopedEntryPtr entry, | 155 disk_cache::ScopedEntryPtr entry, |
155 bool success); | 156 bool success); |
156 | 157 |
157 // Delete callbacks | 158 // Delete callbacks |
158 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 159 void DeleteImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
159 const ErrorCallback& callback); | 160 const ErrorCallback& callback); |
160 void DeleteDidOpenEntry( | 161 void DeleteDidOpenEntry( |
161 const GURL& origin, | 162 const GURL& origin, |
162 scoped_ptr<ServiceWorkerFetchRequest> request, | 163 scoped_ptr<ServiceWorkerFetchRequest> request, |
163 const ServiceWorkerCache::ErrorCallback& callback, | 164 const CacheStorageCache::ErrorCallback& callback, |
164 scoped_ptr<disk_cache::Entry*> entryptr, | 165 scoped_ptr<disk_cache::Entry*> entryptr, |
165 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 166 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
166 int rv); | 167 int rv); |
167 | 168 |
168 // Keys callbacks. | 169 // Keys callbacks. |
169 void KeysImpl(const RequestsCallback& callback); | 170 void KeysImpl(const RequestsCallback& callback); |
170 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv); | 171 void KeysDidOpenNextEntry(scoped_ptr<KeysContext> keys_context, int rv); |
171 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, | 172 void KeysProcessNextEntry(scoped_ptr<KeysContext> keys_context, |
172 const Entries::iterator& iter); | 173 const Entries::iterator& iter); |
173 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, | 174 void KeysDidReadMetadata(scoped_ptr<KeysContext> keys_context, |
174 const Entries::iterator& iter, | 175 const Entries::iterator& iter, |
175 scoped_ptr<ServiceWorkerCacheMetadata> metadata); | 176 scoped_ptr<CacheMetadata> metadata); |
176 | 177 |
177 void CloseImpl(const base::Closure& callback); | 178 void CloseImpl(const base::Closure& callback); |
178 | 179 |
179 // Loads the backend and calls the callback with the result (true for | 180 // Loads the backend and calls the callback with the result (true for |
180 // success). The callback will always be called. Virtual for tests. | 181 // success). The callback will always be called. Virtual for tests. |
181 virtual void CreateBackend(const ErrorCallback& callback); | 182 virtual void CreateBackend(const ErrorCallback& callback); |
182 void CreateBackendDidCreate(const ServiceWorkerCache::ErrorCallback& callback, | 183 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback, |
183 scoped_ptr<ScopedBackendPtr> backend_ptr, | 184 scoped_ptr<ScopedBackendPtr> backend_ptr, |
184 int rv); | 185 int rv); |
185 | 186 |
186 void InitBackend(); | 187 void InitBackend(); |
187 void InitDone(ErrorType error); | 188 void InitDone(ErrorType error); |
188 | 189 |
189 void PendingClosure(const base::Closure& callback); | 190 void PendingClosure(const base::Closure& callback); |
190 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); | 191 void PendingErrorCallback(const ErrorCallback& callback, ErrorType error); |
191 void PendingResponseCallback( | 192 void PendingResponseCallback( |
192 const ResponseCallback& callback, | 193 const ResponseCallback& callback, |
193 ErrorType error, | 194 ErrorType error, |
194 scoped_ptr<ServiceWorkerResponse> response, | 195 scoped_ptr<ServiceWorkerResponse> response, |
195 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 196 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
196 void PendingRequestsCallback(const RequestsCallback& callback, | 197 void PendingRequestsCallback(const RequestsCallback& callback, |
197 ErrorType error, | 198 ErrorType error, |
198 scoped_ptr<Requests> requests); | 199 scoped_ptr<Requests> requests); |
199 | 200 |
200 // Be sure to check |backend_state_| before use. | 201 // Be sure to check |backend_state_| before use. |
201 scoped_ptr<disk_cache::Backend> backend_; | 202 scoped_ptr<disk_cache::Backend> backend_; |
202 | 203 |
203 GURL origin_; | 204 GURL origin_; |
204 base::FilePath path_; | 205 base::FilePath path_; |
205 net::URLRequestContext* request_context_; | 206 net::URLRequestContext* request_context_; |
206 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 207 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
207 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; | 208 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; |
208 BackendState backend_state_; | 209 BackendState backend_state_; |
209 scoped_ptr<ServiceWorkerCacheScheduler> scheduler_; | 210 scoped_ptr<CacheStorageScheduler> scheduler_; |
210 bool initializing_; | 211 bool initializing_; |
211 | 212 |
212 // Whether or not to store data in disk or memory. | 213 // Whether or not to store data in disk or memory. |
213 bool memory_only_; | 214 bool memory_only_; |
214 | 215 |
215 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 216 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; |
216 | 217 |
217 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 218 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); |
218 }; | 219 }; |
219 | 220 |
220 } // namespace content | 221 } // namespace content |
221 | 222 |
222 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 223 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ |
OLD | NEW |