Chromium Code Reviews| 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. |
| 124 std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle( | |
| 125 CacheStorageCache* cache); | |
| 126 void AddCacheHandleRef(CacheStorageCache* cache); | |
| 127 void DropCacheHandleRef(CacheStorageCache* cache); | |
| 128 | |
| 129 // Return a CacheStorageCacheHandle for the given name if the name is known. | |
|
nhiroki
2016/06/14 05:30:24
nit: s/Return/Returns/
jkarlin
2016/06/14 06:41:08
Done.
| |
| 130 // If the | |
| 118 // CacheStorageCache has been deleted, creates a new one. | 131 // CacheStorageCache has been deleted, creates a new one. |
|
nhiroki
2016/06/14 05:30:24
Can you squash this line into the previous line?
jkarlin
2016/06/14 06:41:08
Done.
| |
| 119 scoped_refptr<CacheStorageCache> GetLoadedCache( | 132 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( |
| 120 const std::string& cache_name); | 133 const std::string& cache_name); |
| 121 | 134 |
| 122 // Holds a reference to a cache for thirty seconds. | 135 // Holds a reference to a cache for a short period in case they're used again |
| 123 void TemporarilyPreserveCache(scoped_refptr<CacheStorageCache> cache); | 136 // soon. |
| 137 void TemporarilyPreserveCache( | |
| 138 std::unique_ptr<CacheStorageCacheHandle> cache_handle); | |
| 124 virtual void SchedulePreservedCacheRemoval( | 139 virtual void SchedulePreservedCacheRemoval( |
| 125 const base::Closure& callback); // Virtual for testing. | 140 const base::Closure& callback); // Virtual for testing. |
| 126 void RemovePreservedCache(const CacheStorageCache* cache); | 141 void RemovePreservedCache(const CacheStorageCache* cache); |
| 127 | 142 |
| 128 // Initializer and its callback are below. | 143 // Initializer and its callback are below. |
| 129 void LazyInit(); | 144 void LazyInit(); |
| 130 void LazyInitImpl(); | 145 void LazyInitImpl(); |
| 131 void LazyInitDidLoadIndex( | 146 void LazyInitDidLoadIndex( |
| 132 std::unique_ptr<std::vector<std::string>> indexed_cache_names); | 147 std::unique_ptr<std::vector<std::string>> indexed_cache_names); |
| 133 | 148 |
| 134 // The Open and CreateCache callbacks are below. | 149 // The Open and CreateCache callbacks are below. |
| 135 void OpenCacheImpl(const std::string& cache_name, | 150 void OpenCacheImpl(const std::string& cache_name, |
| 136 const CacheAndErrorCallback& callback); | 151 const CacheAndErrorCallback& callback); |
| 137 void CreateCacheDidCreateCache(const std::string& cache_name, | 152 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 138 const CacheAndErrorCallback& callback, | 153 const CacheAndErrorCallback& callback, |
| 139 scoped_refptr<CacheStorageCache> cache); | 154 std::unique_ptr<CacheStorageCache> cache); |
| 140 void CreateCacheDidWriteIndex(const CacheAndErrorCallback& callback, | 155 void CreateCacheDidWriteIndex( |
| 141 scoped_refptr<CacheStorageCache> cache, | 156 const CacheAndErrorCallback& callback, |
| 142 bool success); | 157 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 158 bool success); | |
| 143 | 159 |
| 144 // The HasCache callbacks are below. | 160 // The HasCache callbacks are below. |
| 145 void HasCacheImpl(const std::string& cache_name, | 161 void HasCacheImpl(const std::string& cache_name, |
| 146 const BoolAndErrorCallback& callback); | 162 const BoolAndErrorCallback& callback); |
| 147 | 163 |
| 148 // The DeleteCache callbacks are below. | 164 // The DeleteCache callbacks are below. |
| 149 void DeleteCacheImpl(const std::string& cache_name, | 165 void DeleteCacheImpl(const std::string& cache_name, |
| 150 const BoolAndErrorCallback& callback); | 166 const BoolAndErrorCallback& callback); |
| 151 | 167 |
| 152 void DeleteCacheDidClose(const std::string& cache_name, | 168 void DeleteCacheDidClose( |
| 153 const BoolAndErrorCallback& callback, | 169 const std::string& cache_name, |
| 154 const StringVector& ordered_cache_names, | 170 const BoolAndErrorCallback& callback, |
| 155 scoped_refptr<CacheStorageCache> cache, | 171 const StringVector& ordered_cache_names, |
| 156 int64_t cache_size); | 172 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 173 int64_t cache_size); | |
| 157 void DeleteCacheDidWriteIndex(const std::string& cache_name, | 174 void DeleteCacheDidWriteIndex(const std::string& cache_name, |
| 158 const BoolAndErrorCallback& callback, | 175 const BoolAndErrorCallback& callback, |
| 159 int cache_size, | 176 int cache_size, |
| 160 bool success); | 177 bool success); |
| 161 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, | 178 void DeleteCacheDidCleanUp(const BoolAndErrorCallback& callback, |
| 162 bool success); | 179 bool success); |
| 163 | 180 |
| 164 // The EnumerateCache callbacks are below. | 181 // The EnumerateCache callbacks are below. |
| 165 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); | 182 void EnumerateCachesImpl(const StringsAndErrorCallback& callback); |
| 166 | 183 |
| 167 // The MatchCache callbacks are below. | 184 // The MatchCache callbacks are below. |
| 168 void MatchCacheImpl(const std::string& cache_name, | 185 void MatchCacheImpl(const std::string& cache_name, |
| 169 std::unique_ptr<ServiceWorkerFetchRequest> request, | 186 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 170 const CacheStorageCache::ResponseCallback& callback); | 187 const CacheStorageCache::ResponseCallback& callback); |
| 171 void MatchCacheDidMatch(scoped_refptr<CacheStorageCache> cache, | 188 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 172 const CacheStorageCache::ResponseCallback& callback, | 189 const CacheStorageCache::ResponseCallback& callback, |
| 173 CacheStorageError error, | 190 CacheStorageError error, |
| 174 std::unique_ptr<ServiceWorkerResponse> response, | 191 std::unique_ptr<ServiceWorkerResponse> response, |
| 175 std::unique_ptr<storage::BlobDataHandle> handle); | 192 std::unique_ptr<storage::BlobDataHandle> handle); |
| 176 | 193 |
| 177 // The MatchAllCaches callbacks are below. | 194 // The MatchAllCaches callbacks are below. |
| 178 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, | 195 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 179 const CacheStorageCache::ResponseCallback& callback); | 196 const CacheStorageCache::ResponseCallback& callback); |
| 180 void MatchAllCachesDidMatch( | 197 void MatchAllCachesDidMatch( |
| 181 scoped_refptr<CacheStorageCache> cache, | 198 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 182 CacheMatchResponse* out_match_response, | 199 CacheMatchResponse* out_match_response, |
| 183 const base::Closure& barrier_closure, | 200 const base::Closure& barrier_closure, |
| 184 CacheStorageError error, | 201 CacheStorageError error, |
| 185 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 202 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 186 std::unique_ptr<storage::BlobDataHandle> handle); | 203 std::unique_ptr<storage::BlobDataHandle> handle); |
| 187 void MatchAllCachesDidMatchAll( | 204 void MatchAllCachesDidMatchAll( |
| 188 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, | 205 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 189 const CacheStorageCache::ResponseCallback& callback); | 206 const CacheStorageCache::ResponseCallback& callback); |
| 190 | 207 |
| 191 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); | 208 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); |
| 192 | 209 |
| 193 void SizeImpl(const SizeCallback& callback); | 210 void SizeImpl(const SizeCallback& callback); |
| 194 | 211 |
| 195 void PendingClosure(const base::Closure& callback); | 212 void PendingClosure(const base::Closure& callback); |
| 196 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, | 213 void PendingBoolAndErrorCallback(const BoolAndErrorCallback& callback, |
| 197 bool found, | 214 bool found, |
| 198 CacheStorageError error); | 215 CacheStorageError error); |
| 199 void PendingCacheAndErrorCallback(const CacheAndErrorCallback& callback, | 216 void PendingCacheAndErrorCallback( |
| 200 scoped_refptr<CacheStorageCache> cache, | 217 const CacheAndErrorCallback& callback, |
| 201 CacheStorageError error); | 218 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 219 CacheStorageError error); | |
| 202 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, | 220 void PendingStringsAndErrorCallback(const StringsAndErrorCallback& callback, |
| 203 const StringVector& strings, | 221 const StringVector& strings, |
| 204 CacheStorageError error); | 222 CacheStorageError error); |
| 205 void PendingResponseCallback( | 223 void PendingResponseCallback( |
| 206 const CacheStorageCache::ResponseCallback& callback, | 224 const CacheStorageCache::ResponseCallback& callback, |
| 207 CacheStorageError error, | 225 CacheStorageError error, |
| 208 std::unique_ptr<ServiceWorkerResponse> response, | 226 std::unique_ptr<ServiceWorkerResponse> response, |
| 209 std::unique_ptr<storage::BlobDataHandle> blob_data_handle); | 227 std::unique_ptr<storage::BlobDataHandle> blob_data_handle); |
| 210 | 228 |
| 211 void PendingSizeCallback(const SizeCallback& callback, int64_t size); | 229 void PendingSizeCallback(const SizeCallback& callback, int64_t size); |
| 212 | 230 |
| 213 // Whether or not we've loaded the list of cache names into memory. | 231 // Whether or not we've loaded the list of cache names into memory. |
| 214 bool initialized_; | 232 bool initialized_; |
| 215 bool initializing_; | 233 bool initializing_; |
| 216 | 234 |
| 235 // True if the backend is supposed to reside in memory only. | |
| 236 bool memory_only_; | |
| 237 | |
| 217 // The pending operation scheduler. | 238 // The pending operation scheduler. |
| 218 std::unique_ptr<CacheStorageScheduler> scheduler_; | 239 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| 219 | 240 |
| 220 // The map of cache names to CacheStorageCache objects. | 241 // The map of cache names to CacheStorageCache objects. |
| 221 CacheMap cache_map_; | 242 CacheMap cache_map_; |
| 222 | 243 |
| 244 // Caches that have been deleted but must still be held onto until all handles | |
| 245 // have been released. | |
| 246 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> | |
| 247 deleted_caches_; | |
| 248 | |
| 249 // CacheStorageCacheHandle reference counts | |
| 250 std::map<CacheStorageCache*, size_t> cache_handle_counts_; | |
| 251 | |
| 223 // The names of caches in the order that they were created. | 252 // The names of caches in the order that they were created. |
| 224 StringVector ordered_cache_names_; | 253 StringVector ordered_cache_names_; |
| 225 | 254 |
| 226 // The file path for this CacheStorage. | 255 // The file path for this CacheStorage. |
| 227 base::FilePath origin_path_; | 256 base::FilePath origin_path_; |
| 228 | 257 |
| 229 // The TaskRunner to run file IO on. | 258 // The TaskRunner to run file IO on. |
| 230 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 259 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 231 | 260 |
| 232 // Performs backend specific operations (memory vs disk). | 261 // Performs backend specific operations (memory vs disk). |
| 233 std::unique_ptr<CacheLoader> cache_loader_; | 262 std::unique_ptr<CacheLoader> cache_loader_; |
| 234 | 263 |
| 235 // Holds ref pointers to recently opened caches so that they can be reused | 264 // Holds handles to recently opened caches so that they can be reused |
| 236 // without having the open the cache again. | 265 // without having to open the cache again. |
| 237 std::map<const CacheStorageCache*, scoped_refptr<CacheStorageCache>> | 266 std::map<const CacheStorageCache*, std::unique_ptr<CacheStorageCacheHandle>> |
| 238 preserved_caches_; | 267 preserved_caches_; |
| 239 | 268 |
| 240 // The quota manager. | 269 // The quota manager. |
| 241 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 270 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 242 | 271 |
| 243 // The origin that this CacheStorage is associated with. | 272 // The origin that this CacheStorage is associated with. |
| 244 GURL origin_; | 273 GURL origin_; |
| 245 | 274 |
| 246 base::WeakPtrFactory<CacheStorage> weak_factory_; | 275 base::WeakPtrFactory<CacheStorage> weak_factory_; |
| 247 | 276 |
| 248 DISALLOW_COPY_AND_ASSIGN(CacheStorage); | 277 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
| 249 }; | 278 }; |
| 250 | 279 |
| 251 } // namespace content | 280 } // namespace content |
| 252 | 281 |
| 253 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 282 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| OLD | NEW |