| 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_CACHE_STORAGE_CACHE_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 namespace net { | 24 namespace net { |
| 25 class URLRequestContextGetter; | 25 class URLRequestContextGetter; |
| 26 } | 26 } |
| 27 | 27 |
| 28 namespace storage { | 28 namespace storage { |
| 29 class BlobStorageContext; | 29 class BlobStorageContext; |
| 30 } | 30 } |
| 31 | 31 |
| 32 namespace content { | 32 namespace content { |
| 33 class CacheStorageScheduler; | 33 class CacheStorageScheduler; |
| 34 class CacheStorageCacheHandle; |
| 34 | 35 |
| 35 // TODO(jkarlin): Constrain the total bytes used per origin. | 36 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 36 | 37 |
| 37 // CacheStorage holds the set of caches for a given origin. It is | 38 // CacheStorage holds the set of caches for a given origin. It is |
| 38 // owned by the CacheStorageManager. This class expects to be run | 39 // owned by the CacheStorageManager. This class expects to be run |
| 39 // on the IO thread. The asynchronous methods are executed serially. | 40 // on the IO thread. The asynchronous methods are executed serially. |
| 40 class CONTENT_EXPORT CacheStorage { | 41 class CONTENT_EXPORT CacheStorage { |
| 41 public: | 42 public: |
| 42 typedef std::vector<std::string> StringVector; | 43 typedef std::vector<std::string> StringVector; |
| 43 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 44 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 44 typedef base::Callback<void(scoped_refptr<CacheStorageCache>, | 45 typedef base::Callback<void(std::unique_ptr<CacheStorageCacheHandle>, |
| 45 CacheStorageError)> | 46 CacheStorageError)> |
| 46 CacheAndErrorCallback; | 47 CacheAndErrorCallback; |
| 47 typedef base::Callback<void(const StringVector&, CacheStorageError)> | 48 typedef base::Callback<void(const StringVector&, CacheStorageError)> |
| 48 StringsAndErrorCallback; | 49 StringsAndErrorCallback; |
| 49 using SizeCallback = base::Callback<void(int64_t)>; | 50 using SizeCallback = base::Callback<void(int64_t)>; |
| 50 | 51 |
| 51 static const char kIndexFileName[]; | 52 static const char kIndexFileName[]; |
| 52 | 53 |
| 53 CacheStorage( | 54 CacheStorage( |
| 54 const base::FilePath& origin_path, | 55 const base::FilePath& origin_path, |
| 55 bool memory_only, | 56 bool memory_only, |
| 56 base::SequencedTaskRunner* cache_task_runner, | 57 base::SequencedTaskRunner* cache_task_runner, |
| 57 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 58 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 58 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 59 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 59 base::WeakPtr<storage::BlobStorageContext> blob_context, | 60 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 60 const GURL& origin); | 61 const GURL& origin); |
| 61 | 62 |
| 62 // Any unfinished asynchronous operations may not complete or call their | 63 // Any unfinished asynchronous operations may not complete or call their |
| 63 // callbacks. | 64 // callbacks. |
| 64 virtual ~CacheStorage(); | 65 virtual ~CacheStorage(); |
| 65 | 66 |
| 66 // Get the cache for the given key. If the cache is not found it is | 67 // Get the cache for the given key. If the cache is not found it is |
| 67 // created. | 68 // created. The CacheStorgeCacheHandle in the callback prolongs the lifetime |
| 69 // of the cache. Once all handles to a cache are deleted the cache is deleted. |
| 70 // The cache will also be deleted in the CacheStorage's destructor so be sure |
| 71 // to check the handle's value before using it. |
| 68 void OpenCache(const std::string& cache_name, | 72 void OpenCache(const std::string& cache_name, |
| 69 const CacheAndErrorCallback& callback); | 73 const CacheAndErrorCallback& callback); |
| 70 | 74 |
| 71 // Calls the callback with whether or not the cache exists. | 75 // Calls the callback with whether or not the cache exists. |
| 72 void HasCache(const std::string& cache_name, | 76 void HasCache(const std::string& cache_name, |
| 73 const BoolAndErrorCallback& callback); | 77 const BoolAndErrorCallback& callback); |
| 74 | 78 |
| 75 // Deletes the cache if it exists. If it doesn't exist, | 79 // Deletes the cache if it exists. If it doesn't exist, |
| 76 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. | 80 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. |
| 77 void DeleteCache(const std::string& cache_name, | 81 void DeleteCache(const std::string& cache_name, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 99 // The size of all of the origin's contents. This value should be used as an | 103 // The size of all of the origin's contents. This value should be used as an |
| 100 // estimate only since the cache may be modified at any time. | 104 // estimate only since the cache may be modified at any time. |
| 101 void Size(const SizeCallback& callback); | 105 void Size(const SizeCallback& callback); |
| 102 | 106 |
| 103 // The functions below are for tests to verify that the operations run | 107 // The functions below are for tests to verify that the operations run |
| 104 // serially. | 108 // serially. |
| 105 void StartAsyncOperationForTesting(); | 109 void StartAsyncOperationForTesting(); |
| 106 void CompleteAsyncOperationForTesting(); | 110 void CompleteAsyncOperationForTesting(); |
| 107 | 111 |
| 108 private: | 112 private: |
| 113 friend class CacheStorageCacheHandle; |
| 114 friend class CacheStorageCache; |
| 109 friend class TestCacheStorage; | 115 friend class TestCacheStorage; |
| 110 class CacheLoader; | 116 class CacheLoader; |
| 111 class MemoryLoader; | 117 class MemoryLoader; |
| 112 class SimpleCacheLoader; | 118 class SimpleCacheLoader; |
| 113 struct CacheMatchResponse; | 119 struct CacheMatchResponse; |
| 114 | 120 |
| 115 typedef std::map<std::string, base::WeakPtr<CacheStorageCache>> CacheMap; | 121 typedef std::map<std::string, std::unique_ptr<CacheStorageCache>> CacheMap; |
| 116 | 122 |
| 117 // Return a CacheStorageCache for the given name if the name is known. If the | 123 // Functions for exposing handles to CacheStorageCache to clients. |
| 118 // CacheStorageCache has been deleted, creates a new one. | 124 std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle( |
| 119 scoped_refptr<CacheStorageCache> GetLoadedCache( | 125 CacheStorageCache* cache); |
| 126 void AddCacheHandleRef(CacheStorageCache* cache); |
| 127 void DropCacheHandleRef(CacheStorageCache* cache); |
| 128 |
| 129 // Returns a CacheStorageCacheHandle for the given name if the name is known. |
| 130 // If the CacheStorageCache has been deleted, creates a new one. |
| 131 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( |
| 120 const std::string& cache_name); | 132 const std::string& cache_name); |
| 121 | 133 |
| 122 // Holds a reference to a cache for thirty seconds. | 134 // Holds a reference to a cache for a short period in case they're used again |
| 123 void TemporarilyPreserveCache(scoped_refptr<CacheStorageCache> cache); | 135 // soon. |
| 136 void TemporarilyPreserveCache( |
| 137 std::unique_ptr<CacheStorageCacheHandle> cache_handle); |
| 124 virtual void SchedulePreservedCacheRemoval( | 138 virtual void SchedulePreservedCacheRemoval( |
| 125 const base::Closure& callback); // Virtual for testing. | 139 const base::Closure& callback); // Virtual for testing. |
| 126 void RemovePreservedCache(const CacheStorageCache* cache); | 140 void RemovePreservedCache(const CacheStorageCache* cache); |
| 127 | 141 |
| 128 // Initializer and its callback are below. | 142 // Initializer and its callback are below. |
| 129 void LazyInit(); | 143 void LazyInit(); |
| 130 void LazyInitImpl(); | 144 void LazyInitImpl(); |
| 131 void LazyInitDidLoadIndex( | 145 void LazyInitDidLoadIndex( |
| 132 std::unique_ptr<std::vector<std::string>> indexed_cache_names); | 146 std::unique_ptr<std::vector<std::string>> indexed_cache_names); |
| 133 | 147 |
| 134 // The Open and CreateCache callbacks are below. | 148 // The Open and CreateCache callbacks are below. |
| 135 void OpenCacheImpl(const std::string& cache_name, | 149 void OpenCacheImpl(const std::string& cache_name, |
| 136 const CacheAndErrorCallback& callback); | 150 const CacheAndErrorCallback& callback); |
| 137 void CreateCacheDidCreateCache(const std::string& cache_name, | 151 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 138 const CacheAndErrorCallback& callback, | 152 const CacheAndErrorCallback& callback, |
| 139 scoped_refptr<CacheStorageCache> cache); | 153 std::unique_ptr<CacheStorageCache> cache); |
| 140 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 154 void CreateCacheDidWriteIndex( |
| 141 scoped_refptr<CacheStorageCache> cache, | 155 const CacheAndErrorCallback& callback, |
| 142 bool success); | 156 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 157 bool success); |
| 143 | 158 |
| 144 // The HasCache callbacks are below. | 159 // The HasCache callbacks are below. |
| 145 void HasCacheImpl(const std::string& cache_name, | 160 void HasCacheImpl(const std::string& cache_name, |
| 146 const BoolAndErrorCallback& callback); | 161 const BoolAndErrorCallback& callback); |
| 147 | 162 |
| 148 // The DeleteCache callbacks are below. | 163 // The DeleteCache callbacks are below. |
| 149 void DeleteCacheImpl(const std::string& cache_name, | 164 void DeleteCacheImpl(const std::string& cache_name, |
| 150 const BoolAndErrorCallback& callback); | 165 const BoolAndErrorCallback& callback); |
| 151 | 166 |
| 152 void DeleteCacheDidClose(const std::string& cache_name, | 167 void DeleteCacheDidClose( |
| 153 const BoolAndErrorCallback& callback, | 168 const std::string& cache_name, |
| 154 const StringVector& ordered_cache_names, | 169 const BoolAndErrorCallback& callback, |
| 155 scoped_refptr<CacheStorageCache> cache, | 170 const StringVector& ordered_cache_names, |
| 156 int64_t cache_size); | 171 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 172 int64_t cache_size); |
| 157 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 173 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 158 const BoolAndErrorCallback& callback, | 174 const BoolAndErrorCallback& callback, |
| 159 int cache_size, | 175 int cache_size, |
| 160 bool success); | 176 bool success); |
| 161 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 177 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 162 bool success); | 178 bool success); |
| 163 | 179 |
| 164 // The EnumerateCache callbacks are below. | 180 // The EnumerateCache callbacks are below. |
| 165 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); | 181 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); |
| 166 | 182 |
| 167 // The MatchCache callbacks are below. | 183 // The MatchCache callbacks are below. |
| 168 void MatchCacheImpl(const std::string& cache_name, | 184 void MatchCacheImpl(const std::string& cache_name, |
| 169 std::unique_ptr<ServiceWorkerFetchRequest> request, | 185 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 170 const CacheStorageCache::ResponseCallback& callback); | 186 const CacheStorageCache::ResponseCallback& callback); |
| 171 void MatchCacheDidMatch(scoped_refptr<CacheStorageCache> cache, | 187 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 172 const CacheStorageCache::ResponseCallback& callback, | 188 const CacheStorageCache::ResponseCallback& callback, |
| 173 CacheStorageError error, | 189 CacheStorageError error, |
| 174 std::unique_ptr<ServiceWorkerResponse> response, | 190 std::unique_ptr<ServiceWorkerResponse> response, |
| 175 std::unique_ptr<storage::BlobDataHandle> handle); | 191 std::unique_ptr<storage::BlobDataHandle> handle); |
| 176 | 192 |
| 177 // The MatchAllCaches callbacks are below. | 193 // The MatchAllCaches callbacks are below. |
| 178 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, | 194 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 179 const CacheStorageCache::ResponseCallback& callback); | 195 const CacheStorageCache::ResponseCallback& callback); |
| 180 void MatchAllCachesDidMatch( | 196 void MatchAllCachesDidMatch( |
| 181 scoped_refptr<CacheStorageCache> cache, | 197 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 182 CacheMatchResponse* out_match_response, | 198 CacheMatchResponse* out_match_response, |
| 183 const base::Closure& barrier_closure, | 199 const base::Closure& barrier_closure, |
| 184 CacheStorageError error, | 200 CacheStorageError error, |
| 185 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 201 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 186 std::unique_ptr<storage::BlobDataHandle> handle); | 202 std::unique_ptr<storage::BlobDataHandle> handle); |
| 187 void MatchAllCachesDidMatchAll( | 203 void MatchAllCachesDidMatchAll( |
| 188 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, | 204 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 189 const CacheStorageCache::ResponseCallback& callback); | 205 const CacheStorageCache::ResponseCallback& callback); |
| 190 | 206 |
| 191 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); | 207 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); |
| 192 | 208 |
| 193 void SizeImpl(const SizeCallback& callback); | 209 void SizeImpl(const SizeCallback& callback); |
| 194 | 210 |
| 195 void PendingClosure(const base::Closure& callback); | 211 void PendingClosure(const base::Closure& callback); |
| 196 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, | 212 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, |
| 197 bool found, | 213 bool found, |
| 198 CacheStorageError error); | 214 CacheStorageError error); |
| 199 void PendingCacheAndErrorCallback(const CacheAndErrorCallback& callback, | 215 void PendingCacheAndErrorCallback( |
| 200 scoped_refptr<CacheStorageCache> cache, | 216 const CacheAndErrorCallback& callback, |
| 201 CacheStorageError error); | 217 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 218 CacheStorageError error); |
| 202 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, | 219 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, |
| 203 const StringVector& strings, | 220 const StringVector& strings, |
| 204 CacheStorageError error); | 221 CacheStorageError error); |
| 205 void PendingResponseCallback( | 222 void PendingResponseCallback( |
| 206 const CacheStorageCache::ResponseCallback& callback, | 223 const CacheStorageCache::ResponseCallback& callback, |
| 207 CacheStorageError error, | 224 CacheStorageError error, |
| 208 std::unique_ptr<ServiceWorkerResponse> response, | 225 std::unique_ptr<ServiceWorkerResponse> response, |
| 209 std::unique_ptr<storage::BlobDataHandle> blob_data_handle); | 226 std::unique_ptr<storage::BlobDataHandle> blob_data_handle); |
| 210 | 227 |
| 211 void PendingSizeCallback(const SizeCallback& callback, int64_t size); | 228 void PendingSizeCallback(const SizeCallback& callback, int64_t size); |
| 212 | 229 |
| 213 // Whether or not we've loaded the list of cache names into memory. | 230 // Whether or not we've loaded the list of cache names into memory. |
| 214 bool initialized_; | 231 bool initialized_; |
| 215 bool initializing_; | 232 bool initializing_; |
| 216 | 233 |
| 234 // True if the backend is supposed to reside in memory only. |
| 235 bool memory_only_; |
| 236 |
| 217 // The pending operation scheduler. | 237 // The pending operation scheduler. |
| 218 std::unique_ptr<CacheStorageScheduler> scheduler_; | 238 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| 219 | 239 |
| 220 // The map of cache names to CacheStorageCache objects. | 240 // The map of cache names to CacheStorageCache objects. |
| 221 CacheMap cache_map_; | 241 CacheMap cache_map_; |
| 222 | 242 |
| 243 // Caches that have been deleted but must still be held onto until all handles |
| 244 // have been released. |
| 245 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> |
| 246 deleted_caches_; |
| 247 |
| 248 // CacheStorageCacheHandle reference counts |
| 249 std::map<CacheStorageCache*, size_t> cache_handle_counts_; |
| 250 |
| 223 // The names of caches in the order that they were created. | 251 // The names of caches in the order that they were created. |
| 224 StringVector ordered_cache_names_; | 252 StringVector ordered_cache_names_; |
| 225 | 253 |
| 226 // The file path for this CacheStorage. | 254 // The file path for this CacheStorage. |
| 227 base::FilePath origin_path_; | 255 base::FilePath origin_path_; |
| 228 | 256 |
| 229 // The TaskRunner to run file IO on. | 257 // The TaskRunner to run file IO on. |
| 230 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 258 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 231 | 259 |
| 232 // Performs backend specific operations (memory vs disk). | 260 // Performs backend specific operations (memory vs disk). |
| 233 std::unique_ptr<CacheLoader> cache_loader_; | 261 std::unique_ptr<CacheLoader> cache_loader_; |
| 234 | 262 |
| 235 // Holds ref pointers to recently opened caches so that they can be reused | 263 // Holds handles to recently opened caches so that they can be reused |
| 236 // without having the open the cache again. | 264 // without having to open the cache again. |
| 237 std::map<const CacheStorageCache*, scoped_refptr<CacheStorageCache>> | 265 std::map<const CacheStorageCache*, std::unique_ptr<CacheStorageCacheHandle>> |
| 238 preserved_caches_; | 266 preserved_caches_; |
| 239 | 267 |
| 240 // The quota manager. | 268 // The quota manager. |
| 241 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 269 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 242 | 270 |
| 243 // The origin that this CacheStorage is associated with. | 271 // The origin that this CacheStorage is associated with. |
| 244 GURL origin_; | 272 GURL origin_; |
| 245 | 273 |
| 246 base::WeakPtrFactory<CacheStorage> weak_factory_; | 274 base::WeakPtrFactory<CacheStorage> weak_factory_; |
| 247 | 275 |
| 248 DISALLOW_COPY_AND_ASSIGN(CacheStorage); | 276 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
| 249 }; | 277 }; |
| 250 | 278 |
| 251 } // namespace content | 279 } // namespace content |
| 252 | 280 |
| 253 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 281 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| OLD | NEW |