| 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_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_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/service_worker/service_worker_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 | 30 |
| 30 // TODO(jkarlin): Constrain the total bytes used per origin. | 31 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 31 | 32 |
| 32 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is | 33 // ServiceWorkerCacheStorage holds the set of caches for a given origin. It is |
| 33 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run | 34 // owned by the ServiceWorkerCacheStorageManager. This class expects to be run |
| 34 // on the IO thread. | 35 // on the IO thread. The asynchronous methods are executed serially. |
| 35 class CONTENT_EXPORT ServiceWorkerCacheStorage { | 36 class CONTENT_EXPORT ServiceWorkerCacheStorage { |
| 36 public: | 37 public: |
| 37 enum CacheStorageError { | 38 enum CacheStorageError { |
| 38 CACHE_STORAGE_ERROR_NO_ERROR, | 39 CACHE_STORAGE_ERROR_NO_ERROR, |
| 39 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, | 40 CACHE_STORAGE_ERROR_NOT_IMPLEMENTED, |
| 40 CACHE_STORAGE_ERROR_NOT_FOUND, | 41 CACHE_STORAGE_ERROR_NOT_FOUND, |
| 41 CACHE_STORAGE_ERROR_EXISTS, | 42 CACHE_STORAGE_ERROR_EXISTS, |
| 42 CACHE_STORAGE_ERROR_STORAGE, | 43 CACHE_STORAGE_ERROR_STORAGE, |
| 43 CACHE_STORAGE_ERROR_CLOSING | 44 CACHE_STORAGE_ERROR_CLOSING |
| 44 }; | 45 }; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 const ServiceWorkerCache::ResponseCallback& callback); | 88 const ServiceWorkerCache::ResponseCallback& callback); |
| 88 | 89 |
| 89 // 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 |
| 90 // first response found. Note that if multiple caches have the same | 91 // first response found. Note that if multiple caches have the same |
| 91 // 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 |
| 92 // returned. If no response is found then |callback| is called with | 93 // returned. If no response is found then |callback| is called with |
| 93 // ServiceWorkerCache::ErrorTypeNotFound. | 94 // ServiceWorkerCache::ErrorTypeNotFound. |
| 94 void MatchAllCaches(scoped_ptr<ServiceWorkerFetchRequest> request, | 95 void MatchAllCaches(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 95 const ServiceWorkerCache::ResponseCallback& callback); | 96 const ServiceWorkerCache::ResponseCallback& callback); |
| 96 | 97 |
| 98 // Calls close on each cache and runs the callback after all of them have |
| 99 // closed. |
| 97 void CloseAllCaches(const base::Closure& callback); | 100 void CloseAllCaches(const base::Closure& callback); |
| 98 | 101 |
| 99 // 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 |
| 100 // backend is not a memory backend. | 103 // backend is not a memory backend. Runs synchronously. |
| 101 int64 MemoryBackedSize() const; | 104 int64 MemoryBackedSize() const; |
| 102 | 105 |
| 106 // The functions below are for tests to verify that the operations run |
| 107 // serially. |
| 108 void StartAsyncOperationForTesting(); |
| 109 void CompleteAsyncOperationForTesting(); |
| 110 |
| 103 private: | 111 private: |
| 104 class MemoryLoader; | 112 class MemoryLoader; |
| 105 class SimpleCacheLoader; | 113 class SimpleCacheLoader; |
| 106 class CacheLoader; | 114 class CacheLoader; |
| 107 | 115 |
| 108 typedef std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap; | 116 typedef std::map<std::string, base::WeakPtr<ServiceWorkerCache> > CacheMap; |
| 109 | 117 |
| 110 // Return a ServiceWorkerCache for the given name if the name is known. If the | 118 // Return a ServiceWorkerCache for the given name if the name is known. If the |
| 111 // ServiceWorkerCache has been deleted, creates a new one. | 119 // ServiceWorkerCache has been deleted, creates a new one. |
| 112 scoped_refptr<ServiceWorkerCache> GetLoadedCache( | 120 scoped_refptr<ServiceWorkerCache> GetLoadedCache( |
| 113 const std::string& cache_name); | 121 const std::string& cache_name); |
| 114 | 122 |
| 115 // Initializer and its callback are below. While LazyInit is running any new | 123 // Initializer and its callback are below. |
| 116 // operations will be queued and started in order after initialization. | 124 void LazyInit(); |
| 117 void LazyInit(const base::Closure& closure); | 125 void LazyInitImpl(); |
| 118 void LazyInitDidLoadIndex( | 126 void LazyInitDidLoadIndex( |
| 119 const base::Closure& callback, | |
| 120 scoped_ptr<std::vector<std::string> > indexed_cache_names); | 127 scoped_ptr<std::vector<std::string> > indexed_cache_names); |
| 121 | 128 |
| 122 void AddCacheToMap(const std::string& cache_name, | 129 void AddCacheToMap(const std::string& cache_name, |
| 123 base::WeakPtr<ServiceWorkerCache> cache); | 130 base::WeakPtr<ServiceWorkerCache> cache); |
| 124 | 131 |
| 125 // The CreateCache callbacks are below. | 132 // The Open and CreateCache callbacks are below. |
| 133 void OpenCacheImpl(const std::string& cache_name, |
| 134 const CacheAndErrorCallback& callback); |
| 126 void CreateCacheDidCreateCache( | 135 void CreateCacheDidCreateCache( |
| 127 const std::string& cache_name, | 136 const std::string& cache_name, |
| 128 const CacheAndErrorCallback& callback, | 137 const CacheAndErrorCallback& callback, |
| 129 const scoped_refptr<ServiceWorkerCache>& cache); | 138 const scoped_refptr<ServiceWorkerCache>& cache); |
| 130 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 139 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
| 131 const scoped_refptr<ServiceWorkerCache>& cache, | 140 const scoped_refptr<ServiceWorkerCache>& cache, |
| 132 bool success); | 141 bool success); |
| 133 | 142 |
| 143 // The HasCache callbacks are below. |
| 144 void HasCacheImpl(const std::string& cache_name, |
| 145 const BoolAndErrorCallback& callback); |
| 146 |
| 134 // The DeleteCache callbacks are below. | 147 // The DeleteCache callbacks are below. |
| 148 void DeleteCacheImpl(const std::string& cache_name, |
| 149 const BoolAndErrorCallback& callback); |
| 150 |
| 135 void DeleteCacheDidClose(const std::string& cache_name, | 151 void DeleteCacheDidClose(const std::string& cache_name, |
| 136 const BoolAndErrorCallback& callback, | 152 const BoolAndErrorCallback& callback, |
| 137 const StringVector& ordered_cache_names, | 153 const StringVector& ordered_cache_names, |
| 138 const scoped_refptr<ServiceWorkerCache>& cache); | 154 const scoped_refptr<ServiceWorkerCache>& cache); |
| 139 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 155 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 140 const BoolAndErrorCallback& callback, | 156 const BoolAndErrorCallback& callback, |
| 141 bool success); | 157 bool success); |
| 142 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 158 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 143 bool success); | 159 bool success); |
| 144 | 160 |
| 161 // The EnumerateCache callbacks are below. |
| 162 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); |
| 163 |
| 145 // The MatchCache callbacks are below. | 164 // The MatchCache callbacks are below. |
| 165 void MatchCacheImpl(const std::string& cache_name, |
| 166 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 167 const ServiceWorkerCache::ResponseCallback& callback); |
| 146 void MatchCacheDidMatch(const scoped_refptr<ServiceWorkerCache>& cache, | 168 void MatchCacheDidMatch(const scoped_refptr<ServiceWorkerCache>& cache, |
| 147 const ServiceWorkerCache::ResponseCallback& callback, | 169 const ServiceWorkerCache::ResponseCallback& callback, |
| 148 ServiceWorkerCache::ErrorType error, | 170 ServiceWorkerCache::ErrorType error, |
| 149 scoped_ptr<ServiceWorkerResponse> response, | 171 scoped_ptr<ServiceWorkerResponse> response, |
| 150 scoped_ptr<storage::BlobDataHandle> handle); | 172 scoped_ptr<storage::BlobDataHandle> handle); |
| 151 | 173 |
| 152 // The MatchAllCaches callbacks are below. | 174 // The MatchAllCaches callbacks are below. |
| 175 void MatchAllCachesImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 176 const ServiceWorkerCache::ResponseCallback& callback); |
| 153 void MatchAllCachesDidMatch(scoped_refptr<ServiceWorkerCache> cache, | 177 void MatchAllCachesDidMatch(scoped_refptr<ServiceWorkerCache> cache, |
| 154 const base::Closure& barrier_closure, | 178 const base::Closure& barrier_closure, |
| 155 ServiceWorkerCache::ResponseCallback* callback, | 179 ServiceWorkerCache::ResponseCallback* callback, |
| 156 ServiceWorkerCache::ErrorType error, | 180 ServiceWorkerCache::ErrorType error, |
| 157 scoped_ptr<ServiceWorkerResponse> response, | 181 scoped_ptr<ServiceWorkerResponse> response, |
| 158 scoped_ptr<storage::BlobDataHandle> handle); | 182 scoped_ptr<storage::BlobDataHandle> handle); |
| 159 void MatchAllCachesDidMatchAll( | 183 void MatchAllCachesDidMatchAll( |
| 160 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback); | 184 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback); |
| 161 | 185 |
| 186 // The CloseAllCaches callbacks are below. |
| 187 void CloseAllCachesImpl(const base::Closure& callback); |
| 188 |
| 189 void PendingClosure(const base::Closure& callback); |
| 190 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, |
| 191 bool found, |
| 192 CacheStorageError error); |
| 193 void PendingCacheAndErrorCallback( |
| 194 const CacheAndErrorCallback& callback, |
| 195 const scoped_refptr<ServiceWorkerCache>& cache, |
| 196 CacheStorageError error); |
| 197 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, |
| 198 const StringVector& strings, |
| 199 CacheStorageError error); |
| 200 void PendingResponseCallback( |
| 201 const ServiceWorkerCache::ResponseCallback& callback, |
| 202 ServiceWorkerCache::ErrorType error, |
| 203 scoped_ptr<ServiceWorkerResponse> response, |
| 204 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
| 205 |
| 162 // Whether or not we've loaded the list of cache names into memory. | 206 // Whether or not we've loaded the list of cache names into memory. |
| 163 bool initialized_; | 207 bool initialized_; |
| 208 bool initializing_; |
| 164 | 209 |
| 165 // The list of operations waiting on initialization. | 210 // The pending operation scheduler. |
| 166 std::vector<base::Closure> init_callbacks_; | 211 scoped_ptr<ServiceWorkerCacheScheduler> scheduler_; |
| 167 | 212 |
| 168 // The map of cache names to ServiceWorkerCache objects. | 213 // The map of cache names to ServiceWorkerCache objects. |
| 169 CacheMap cache_map_; | 214 CacheMap cache_map_; |
| 170 | 215 |
| 171 // The names of caches in the order that they were created. | 216 // The names of caches in the order that they were created. |
| 172 StringVector ordered_cache_names_; | 217 StringVector ordered_cache_names_; |
| 173 | 218 |
| 174 // The file path for this CacheStorage. | 219 // The file path for this CacheStorage. |
| 175 base::FilePath origin_path_; | 220 base::FilePath origin_path_; |
| 176 | 221 |
| 177 // The TaskRunner to run file IO on. | 222 // The TaskRunner to run file IO on. |
| 178 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 223 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 179 | 224 |
| 180 // Whether or not to store data in disk or memory. | 225 // Whether or not to store data in disk or memory. |
| 181 bool memory_only_; | 226 bool memory_only_; |
| 182 | 227 |
| 183 // Performs backend specific operations (memory vs disk). | 228 // Performs backend specific operations (memory vs disk). |
| 184 scoped_ptr<CacheLoader> cache_loader_; | 229 scoped_ptr<CacheLoader> cache_loader_; |
| 185 | 230 |
| 186 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; | 231 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; |
| 187 | 232 |
| 188 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 233 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
| 189 }; | 234 }; |
| 190 | 235 |
| 191 } // namespace content | 236 } // namespace content |
| 192 | 237 |
| 193 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 238 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| OLD | NEW |