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_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "content/browser/service_worker/service_worker_cache.h" | 14 #include "content/browser/cache_storage/cache_storage_cache.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class SequencedTaskRunner; | 17 class SequencedTaskRunner; |
18 } | 18 } |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 class URLRequestContext; | 21 class URLRequestContext; |
22 } | 22 } |
23 | 23 |
24 namespace storage { | 24 namespace storage { |
25 class BlobStorageContext; | 25 class BlobStorageContext; |
26 } | 26 } |
27 | 27 |
28 namespace content { | 28 namespace content { |
29 class ServiceWorkerCacheScheduler; | 29 class CacheStorageScheduler; |
30 | 30 |
31 // TODO(jkarlin): Constrain the total bytes used per origin. | 31 // TODO(jkarlin): Constrain the total bytes used per origin. |
32 | 32 |
33 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is | 33 // CacheStorage holds the set of caches for a given origin. It is |
34 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run | 34 // owned by the CacheStorageManager. This class expects to be run |
35 // on the IO thread. The asynchronous methods are executed serially. | 35 // on the IO thread. The asynchronous methods are executed serially. |
36 class CONTENT_EXPORT ServiceWorkerCacheStorage { | 36 class CONTENT_EXPORT CacheStorage { |
37 public: | 37 public: |
38 enum CacheStorageError { | 38 enum CacheStorageError { |
39 CACHE_STORAGE_ERROR_NO_ERROR, | 39 CACHE_STORAGE_ERROR_NO_ERROR, |
40 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, | 40 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
41 CACHE_STORAGE_ERROR_NOT_FOUND, | 41 CACHE_STORAGE_ERROR_NOT_FOUND, |
42 CACHE_STORAGE_ERROR_EXISTS, | 42 CACHE_STORAGE_ERROR_EXISTS, |
43 CACHE_STORAGE_ERROR_STORAGE, | 43 CACHE_STORAGE_ERROR_STORAGE, |
44 CACHE_STORAGE_ERROR_CLOSING | 44 CACHE_STORAGE_ERROR_CLOSING |
45 }; | 45 }; |
46 typedef std::vector<std::string> StringVector; | 46 typedef std::vector<std::string> StringVector; |
47 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 47 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
48 typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&, | 48 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&, |
49 CacheStorageError)> CacheAndErrorCallback; | 49 CacheStorageError)> CacheAndErrorCallback; |
50 typedef base::Callback<void(const StringVector&, CacheStorageError)> | 50 typedef base::Callback<void(const StringVector&, CacheStorageError)> |
51 StringsAndErrorCallback; | 51 StringsAndErrorCallback; |
52 | 52 |
53 static const char kIndexFileName[]; | 53 static const char kIndexFileName[]; |
54 | 54 |
55 ServiceWorkerCacheStorage( | 55 CacheStorage( |
56 const base::FilePath& origin_path, | 56 const base::FilePath& origin_path, |
57 bool memory_only, | 57 bool memory_only, |
58 base::SequencedTaskRunner* cache_task_runner, | 58 base::SequencedTaskRunner* cache_task_runner, |
59 net::URLRequestContext* request_context, | 59 net::URLRequestContext* request_context, |
60 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 60 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
61 base::WeakPtr<storage::BlobStorageContext> blob_context, | 61 base::WeakPtr<storage::BlobStorageContext> blob_context, |
62 const GURL& origin); | 62 const GURL& origin); |
63 | 63 |
64 // Any unfinished asynchronous operations may not complete or call their | 64 // Any unfinished asynchronous operations may not complete or call their |
65 // callbacks. | 65 // callbacks. |
66 virtual ~ServiceWorkerCacheStorage(); | 66 virtual ~CacheStorage(); |
67 | 67 |
68 // Get the cache for the given key. If the cache is not found it is | 68 // Get the cache for the given key. If the cache is not found it is |
69 // created. | 69 // created. |
70 void OpenCache(const std::string& cache_name, | 70 void OpenCache(const std::string& cache_name, |
71 const CacheAndErrorCallback& callback); | 71 const CacheAndErrorCallback& callback); |
72 | 72 |
73 // Calls the callback with whether or not the cache exists. | 73 // Calls the callback with whether or not the cache exists. |
74 void HasCache(const std::string& cache_name, | 74 void HasCache(const std::string& cache_name, |
75 const BoolAndErrorCallback& callback); | 75 const BoolAndErrorCallback& callback); |
76 | 76 |
77 // Deletes the cache if it exists. If it doesn't exist, | 77 // Deletes the cache if it exists. If it doesn't exist, |
78 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. | 78 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. |
79 void DeleteCache(const std::string& cache_name, | 79 void DeleteCache(const std::string& cache_name, |
80 const BoolAndErrorCallback& callback); | 80 const BoolAndErrorCallback& callback); |
81 | 81 |
82 // Calls the callback with a vector of cache names (keys) available. | 82 // Calls the callback with a vector of cache names (keys) available. |
83 void EnumerateCaches(const StringsAndErrorCallback& callback); | 83 void EnumerateCaches(const StringsAndErrorCallback& callback); |
84 | 84 |
85 // Calls match on the cache with the given |cache_name|. | 85 // Calls match on the cache with the given |cache_name|. |
86 void MatchCache(const std::string& cache_name, | 86 void MatchCache(const std::string& cache_name, |
87 scoped_ptr<ServiceWorkerFetchRequest> request, | 87 scoped_ptr<ServiceWorkerFetchRequest> request, |
88 const ServiceWorkerCache::ResponseCallback& callback); | 88 const CacheStorageCache::ResponseCallback& callback); |
89 | 89 |
90 // Calls match on all of the caches in parallel, calling |callback| with the | 90 // Calls match on all of the caches in parallel, calling |callback| with the |
91 // first response found. Note that if multiple caches have the same | 91 // first response found. Note that if multiple caches have the same |
92 // request/response then it is not defined which cache's response will be | 92 // request/response then it is not defined which cache's response will be |
93 // returned. If no response is found then |callback| is called with | 93 // returned. If no response is found then |callback| is called with |
94 // ServiceWorkerCache::ErrorTypeNotFound. | 94 // CacheStorageCache::ErrorTypeNotFound. |
95 void MatchAllCaches(scoped_ptr<ServiceWorkerFetchRequest> request, | 95 void MatchAllCaches(scoped_ptr<ServiceWorkerFetchRequest> request, |
96 const ServiceWorkerCache::ResponseCallback& callback); | 96 const CacheStorageCache::ResponseCallback& callback); |
97 | 97 |
98 // Calls close on each cache and runs the callback after all of them have | 98 // Calls close on each cache and runs the callback after all of them have |
99 // closed. | 99 // closed. |
100 void CloseAllCaches(const base::Closure& callback); | 100 void CloseAllCaches(const base::Closure& callback); |
101 | 101 |
102 // The size of all of the origin's contents in memory. Returns 0 if the cache | 102 // The size of all of the origin's contents in memory. Returns 0 if the cache |
103 // backend is not a memory backend. Runs synchronously. | 103 // backend is not a memory backend. Runs synchronously. |
104 int64 MemoryBackedSize() const; | 104 int64 MemoryBackedSize() const; |
105 | 105 |
106 // The functions below are for tests to verify that the operations run | 106 // The functions below are for tests to verify that the operations run |
107 // serially. | 107 // serially. |
108 void StartAsyncOperationForTesting(); | 108 void StartAsyncOperationForTesting(); |
109 void CompleteAsyncOperationForTesting(); | 109 void CompleteAsyncOperationForTesting(); |
110 | 110 |
111 private: | 111 private: |
112 class MemoryLoader; | 112 class MemoryLoader; |
113 class SimpleCacheLoader; | 113 class SimpleCacheLoader; |
114 class CacheLoader; | 114 class CacheLoader; |
115 | 115 |
116 typedef std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap; | 116 typedef std::map<std::string, base::WeakPtr<CacheStorageCache>> CacheMap; |
117 | 117 |
118 // Return a ServiceWorkerCache for the given name if the name is known. If the | 118 // Return a CacheStorageCache for the given name if the name is known. If the |
119 // ServiceWorkerCache has been deleted, creates a new one. | 119 // CacheStorageCache has been deleted, creates a new one. |
120 scoped_refptr<ServiceWorkerCache> GetLoadedCache( | 120 scoped_refptr<CacheStorageCache> GetLoadedCache( |
121 const std::string& cache_name); | 121 const std::string& cache_name); |
122 | 122 |
123 // Initializer and its callback are below. | 123 // Initializer and its callback are below. |
124 void LazyInit(); | 124 void LazyInit(); |
125 void LazyInitImpl(); | 125 void LazyInitImpl(); |
126 void LazyInitDidLoadIndex( | 126 void LazyInitDidLoadIndex( |
127 scoped_ptr<std::vector<std::string> > indexed_cache_names); | 127 scoped_ptr<std::vector<std::string>> indexed_cache_names); |
128 | 128 |
129 // The Open and CreateCache callbacks are below. | 129 // The Open and CreateCache callbacks are below. |
130 void OpenCacheImpl(const std::string& cache_name, | 130 void OpenCacheImpl(const std::string& cache_name, |
131 const CacheAndErrorCallback& callback); | 131 const CacheAndErrorCallback& callback); |
132 void CreateCacheDidCreateCache( | 132 void CreateCacheDidCreateCache(const std::string& cache_name, |
133 const std::string& cache_name, | 133 const CacheAndErrorCallback& callback, |
134 const CacheAndErrorCallback& callback, | 134 const scoped_refptr<CacheStorageCache>& cache); |
135 const scoped_refptr<ServiceWorkerCache>& cache); | |
136 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 135 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
137 const scoped_refptr<ServiceWorkerCache>& cache, | 136 const scoped_refptr<CacheStorageCache>& cache, |
138 bool success); | 137 bool success); |
139 | 138 |
140 // The HasCache callbacks are below. | 139 // The HasCache callbacks are below. |
141 void HasCacheImpl(const std::string& cache_name, | 140 void HasCacheImpl(const std::string& cache_name, |
142 const BoolAndErrorCallback& callback); | 141 const BoolAndErrorCallback& callback); |
143 | 142 |
144 // The DeleteCache callbacks are below. | 143 // The DeleteCache callbacks are below. |
145 void DeleteCacheImpl(const std::string& cache_name, | 144 void DeleteCacheImpl(const std::string& cache_name, |
146 const BoolAndErrorCallback& callback); | 145 const BoolAndErrorCallback& callback); |
147 | 146 |
148 void DeleteCacheDidClose(const std::string& cache_name, | 147 void DeleteCacheDidClose(const std::string& cache_name, |
149 const BoolAndErrorCallback& callback, | 148 const BoolAndErrorCallback& callback, |
150 const StringVector& ordered_cache_names, | 149 const StringVector& ordered_cache_names, |
151 const scoped_refptr<ServiceWorkerCache>& cache); | 150 const scoped_refptr<CacheStorageCache>& cache); |
152 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 151 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
153 const BoolAndErrorCallback& callback, | 152 const BoolAndErrorCallback& callback, |
154 bool success); | 153 bool success); |
155 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 154 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
156 bool success); | 155 bool success); |
157 | 156 |
158 // The EnumerateCache callbacks are below. | 157 // The EnumerateCache callbacks are below. |
159 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); | 158 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); |
160 | 159 |
161 // The MatchCache callbacks are below. | 160 // The MatchCache callbacks are below. |
162 void MatchCacheImpl(const std::string& cache_name, | 161 void MatchCacheImpl(const std::string& cache_name, |
163 scoped_ptr<ServiceWorkerFetchRequest> request, | 162 scoped_ptr<ServiceWorkerFetchRequest> request, |
164 const ServiceWorkerCache::ResponseCallback& callback); | 163 const CacheStorageCache::ResponseCallback& callback); |
165 void MatchCacheDidMatch(const scoped_refptr<ServiceWorkerCache>& cache, | 164 void MatchCacheDidMatch(const scoped_refptr<CacheStorageCache>& cache, |
166 const ServiceWorkerCache::ResponseCallback& callback, | 165 const CacheStorageCache::ResponseCallback& callback, |
167 ServiceWorkerCache::ErrorType error, | 166 CacheStorageCache::ErrorType error, |
168 scoped_ptr<ServiceWorkerResponse> response, | 167 scoped_ptr<ServiceWorkerResponse> response, |
169 scoped_ptr<storage::BlobDataHandle> handle); | 168 scoped_ptr<storage::BlobDataHandle> handle); |
170 | 169 |
171 // The MatchAllCaches callbacks are below. | 170 // The MatchAllCaches callbacks are below. |
172 void MatchAllCachesImpl(scoped_ptr<ServiceWorkerFetchRequest> request, | 171 void MatchAllCachesImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
173 const ServiceWorkerCache::ResponseCallback& callback); | 172 const CacheStorageCache::ResponseCallback& callback); |
174 void MatchAllCachesDidMatch(scoped_refptr<ServiceWorkerCache> cache, | 173 void MatchAllCachesDidMatch(scoped_refptr<CacheStorageCache> cache, |
175 const base::Closure& barrier_closure, | 174 const base::Closure& barrier_closure, |
176 ServiceWorkerCache::ResponseCallback* callback, | 175 CacheStorageCache::ResponseCallback* callback, |
177 ServiceWorkerCache::ErrorType error, | 176 CacheStorageCache::ErrorType error, |
178 scoped_ptr<ServiceWorkerResponse> response, | 177 scoped_ptr<ServiceWorkerResponse> response, |
179 scoped_ptr<storage::BlobDataHandle> handle); | 178 scoped_ptr<storage::BlobDataHandle> handle); |
180 void MatchAllCachesDidMatchAll( | 179 void MatchAllCachesDidMatchAll( |
181 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback); | 180 scoped_ptr<CacheStorageCache::ResponseCallback> callback); |
182 | 181 |
183 // The CloseAllCaches callbacks are below. | 182 // The CloseAllCaches callbacks are below. |
184 void CloseAllCachesImpl(const base::Closure& callback); | 183 void CloseAllCachesImpl(const base::Closure& callback); |
185 | 184 |
186 void PendingClosure(const base::Closure& callback); | 185 void PendingClosure(const base::Closure& callback); |
187 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, | 186 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, |
188 bool found, | 187 bool found, |
189 CacheStorageError error); | 188 CacheStorageError error); |
190 void PendingCacheAndErrorCallback( | 189 void PendingCacheAndErrorCallback( |
191 const CacheAndErrorCallback& callback, | 190 const CacheAndErrorCallback& callback, |
192 const scoped_refptr<ServiceWorkerCache>& cache, | 191 const scoped_refptr<CacheStorageCache>& cache, |
193 CacheStorageError error); | 192 CacheStorageError error); |
194 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, | 193 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, |
195 const StringVector& strings, | 194 const StringVector& strings, |
196 CacheStorageError error); | 195 CacheStorageError error); |
197 void PendingResponseCallback( | 196 void PendingResponseCallback( |
198 const ServiceWorkerCache::ResponseCallback& callback, | 197 const CacheStorageCache::ResponseCallback& callback, |
199 ServiceWorkerCache::ErrorType error, | 198 CacheStorageCache::ErrorType error, |
200 scoped_ptr<ServiceWorkerResponse> response, | 199 scoped_ptr<ServiceWorkerResponse> response, |
201 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 200 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
202 | 201 |
203 // Whether or not we've loaded the list of cache names into memory. | 202 // Whether or not we've loaded the list of cache names into memory. |
204 bool initialized_; | 203 bool initialized_; |
205 bool initializing_; | 204 bool initializing_; |
206 | 205 |
207 // The pending operation scheduler. | 206 // The pending operation scheduler. |
208 scoped_ptr<ServiceWorkerCacheScheduler> scheduler_; | 207 scoped_ptr<CacheStorageScheduler> scheduler_; |
209 | 208 |
210 // The map of cache names to ServiceWorkerCache objects. | 209 // The map of cache names to CacheStorageCache objects. |
211 CacheMap cache_map_; | 210 CacheMap cache_map_; |
212 | 211 |
213 // The names of caches in the order that they were created. | 212 // The names of caches in the order that they were created. |
214 StringVector ordered_cache_names_; | 213 StringVector ordered_cache_names_; |
215 | 214 |
216 // The file path for this CacheStorage. | 215 // The file path for this CacheStorage. |
217 base::FilePath origin_path_; | 216 base::FilePath origin_path_; |
218 | 217 |
219 // The TaskRunner to run file IO on. | 218 // The TaskRunner to run file IO on. |
220 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 219 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
221 | 220 |
222 // Whether or not to store data in disk or memory. | 221 // Whether or not to store data in disk or memory. |
223 bool memory_only_; | 222 bool memory_only_; |
224 | 223 |
225 // Performs backend specific operations (memory vs disk). | 224 // Performs backend specific operations (memory vs disk). |
226 scoped_ptr<CacheLoader> cache_loader_; | 225 scoped_ptr<CacheLoader> cache_loader_; |
227 | 226 |
228 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; | 227 base::WeakPtrFactory<CacheStorage> weak_factory_; |
229 | 228 |
230 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 229 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
231 }; | 230 }; |
232 | 231 |
233 } // namespace content | 232 } // namespace content |
234 | 233 |
235 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 234 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
OLD | NEW |