| 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 // The Open and CreateCache callbacks are below. |
| 123 base::WeakPtr<ServiceWorkerCache> cache); | 130 void OpenCacheImpl(const std::string& cache_name, |
| 124 | 131 const CacheAndErrorCallback& callback); |
| 125 // The CreateCache callbacks are below. | |
| 126 void CreateCacheDidCreateCache( | 132 void CreateCacheDidCreateCache( |
| 127 const std::string& cache_name, | 133 const std::string& cache_name, |
| 128 const CacheAndErrorCallback& callback, | 134 const CacheAndErrorCallback& callback, |
| 129 const scoped_refptr<ServiceWorkerCache>& cache); | 135 const scoped_refptr<ServiceWorkerCache>& cache); |
| 130 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 136 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, |
| 131 const scoped_refptr<ServiceWorkerCache>& cache, | 137 const scoped_refptr<ServiceWorkerCache>& cache, |
| 132 bool success); | 138 bool success); |
| 133 | 139 |
| 140 // The HasCache callbacks are below. |
| 141 void HasCacheImpl(const std::string& cache_name, |
| 142 const BoolAndErrorCallback& callback); |
| 143 |
| 134 // The DeleteCache callbacks are below. | 144 // The DeleteCache callbacks are below. |
| 145 void DeleteCacheImpl(const std::string& cache_name, |
| 146 const BoolAndErrorCallback& callback); |
| 147 |
| 135 void DeleteCacheDidClose(const std::string& cache_name, | 148 void DeleteCacheDidClose(const std::string& cache_name, |
| 136 const BoolAndErrorCallback& callback, | 149 const BoolAndErrorCallback& callback, |
| 137 const StringVector& ordered_cache_names, | 150 const StringVector& ordered_cache_names, |
| 138 const scoped_refptr<ServiceWorkerCache>& cache); | 151 const scoped_refptr<ServiceWorkerCache>& cache); |
| 139 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 152 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 140 const BoolAndErrorCallback& callback, | 153 const BoolAndErrorCallback& callback, |
| 141 bool success); | 154 bool success); |
| 142 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 155 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 143 bool success); | 156 bool success); |
| 144 | 157 |
| 158 // The EnumerateCache callbacks are below. |
| 159 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); |
| 160 |
| 145 // The MatchCache callbacks are below. | 161 // The MatchCache callbacks are below. |
| 162 void MatchCacheImpl(const std::string& cache_name, |
| 163 scoped_ptr<ServiceWorkerFetchRequest> request, |
| 164 const ServiceWorkerCache::ResponseCallback& callback); |
| 146 void MatchCacheDidMatch(const scoped_refptr<ServiceWorkerCache>& cache, | 165 void MatchCacheDidMatch(const scoped_refptr<ServiceWorkerCache>& cache, |
| 147 const ServiceWorkerCache::ResponseCallback& callback, | 166 const ServiceWorkerCache::ResponseCallback& callback, |
| 148 ServiceWorkerCache::ErrorType error, | 167 ServiceWorkerCache::ErrorType error, |
| 149 scoped_ptr<ServiceWorkerResponse> response, | 168 scoped_ptr<ServiceWorkerResponse> response, |
| 150 scoped_ptr<storage::BlobDataHandle> handle); | 169 scoped_ptr<storage::BlobDataHandle> handle); |
| 151 | 170 |
| 152 // The MatchAllCaches callbacks are below. | 171 // The MatchAllCaches callbacks are below. |
| 172 void MatchAllCachesImpl(scoped_ptr<ServiceWorkerFetchRequest> request, |
| 173 const ServiceWorkerCache::ResponseCallback& callback); |
| 153 void MatchAllCachesDidMatch(scoped_refptr<ServiceWorkerCache> cache, | 174 void MatchAllCachesDidMatch(scoped_refptr<ServiceWorkerCache> cache, |
| 154 const base::Closure& barrier_closure, | 175 const base::Closure& barrier_closure, |
| 155 ServiceWorkerCache::ResponseCallback* callback, | 176 ServiceWorkerCache::ResponseCallback* callback, |
| 156 ServiceWorkerCache::ErrorType error, | 177 ServiceWorkerCache::ErrorType error, |
| 157 scoped_ptr<ServiceWorkerResponse> response, | 178 scoped_ptr<ServiceWorkerResponse> response, |
| 158 scoped_ptr<storage::BlobDataHandle> handle); | 179 scoped_ptr<storage::BlobDataHandle> handle); |
| 159 void MatchAllCachesDidMatchAll( | 180 void MatchAllCachesDidMatchAll( |
| 160 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback); | 181 scoped_ptr<ServiceWorkerCache::ResponseCallback> callback); |
| 161 | 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 |
| 162 // Whether or not we've loaded the list of cache names into memory. | 203 // Whether or not we've loaded the list of cache names into memory. |
| 163 bool initialized_; | 204 bool initialized_; |
| 205 bool initializing_; |
| 164 | 206 |
| 165 // The list of operations waiting on initialization. | 207 // The pending operation scheduler. |
| 166 std::vector<base::Closure> init_callbacks_; | 208 scoped_ptr<ServiceWorkerCacheScheduler> scheduler_; |
| 167 | 209 |
| 168 // The map of cache names to ServiceWorkerCache objects. | 210 // The map of cache names to ServiceWorkerCache objects. |
| 169 CacheMap cache_map_; | 211 CacheMap cache_map_; |
| 170 | 212 |
| 171 // The names of caches in the order that they were created. | 213 // The names of caches in the order that they were created. |
| 172 StringVector ordered_cache_names_; | 214 StringVector ordered_cache_names_; |
| 173 | 215 |
| 174 // The file path for this CacheStorage. | 216 // The file path for this CacheStorage. |
| 175 base::FilePath origin_path_; | 217 base::FilePath origin_path_; |
| 176 | 218 |
| 177 // The TaskRunner to run file IO on. | 219 // The TaskRunner to run file IO on. |
| 178 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 220 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 179 | 221 |
| 180 // Whether or not to store data in disk or memory. | 222 // Whether or not to store data in disk or memory. |
| 181 bool memory_only_; | 223 bool memory_only_; |
| 182 | 224 |
| 183 // Performs backend specific operations (memory vs disk). | 225 // Performs backend specific operations (memory vs disk). |
| 184 scoped_ptr<CacheLoader> cache_loader_; | 226 scoped_ptr<CacheLoader> cache_loader_; |
| 185 | 227 |
| 186 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; | 228 base::WeakPtrFactory<ServiceWorkerCacheStorage> weak_factory_; |
| 187 | 229 |
| 188 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); | 230 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheStorage); |
| 189 }; | 231 }; |
| 190 | 232 |
| 191 } // namespace content | 233 } // namespace content |
| 192 | 234 |
| 193 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ | 235 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_STORAGE_H_ |
| OLD | NEW |