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