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> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "content/browser/cache_storage/cache_storage_cache.h" | 20 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 21 #include "content/browser/cache_storage/cache_storage_cache_observer.h" | |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 class SequencedTaskRunner; | 24 class SequencedTaskRunner; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace net { | 27 namespace net { |
| 27 class URLRequestContextGetter; | 28 class URLRequestContextGetter; |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace storage { | 31 namespace storage { |
| 31 class BlobStorageContext; | 32 class BlobStorageContext; |
| 32 } | 33 } |
| 33 | 34 |
| 34 namespace content { | 35 namespace content { |
| 35 class CacheStorageCacheHandle; | 36 class CacheStorageCacheHandle; |
| 37 class CacheStorageIndex; | |
| 36 class CacheStorageScheduler; | 38 class CacheStorageScheduler; |
| 37 | 39 |
| 38 // TODO(jkarlin): Constrain the total bytes used per origin. | 40 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 39 | 41 |
| 40 // CacheStorage holds the set of caches for a given origin. It is | 42 // CacheStorage holds the set of caches for a given origin. It is |
| 41 // owned by the CacheStorageManager. This class expects to be run | 43 // owned by the CacheStorageManager. This class expects to be run |
| 42 // on the IO thread. The asynchronous methods are executed serially. | 44 // on the IO thread. The asynchronous methods are executed serially. |
| 43 class CONTENT_EXPORT CacheStorage { | 45 class CONTENT_EXPORT CacheStorage : public CacheStorageCacheObserver { |
| 44 public: | 46 public: |
| 45 typedef std::vector<std::string> StringVector; | 47 constexpr static int64_t kSizeUnknown = -1; |
| 48 | |
| 46 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 49 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 47 typedef base::Callback<void(std::unique_ptr<CacheStorageCacheHandle>, | 50 typedef base::Callback<void(std::unique_ptr<CacheStorageCacheHandle>, |
| 48 CacheStorageError)> | 51 CacheStorageError)> |
| 49 CacheAndErrorCallback; | 52 CacheAndErrorCallback; |
| 50 using StringsCallback = base::Callback<void(const StringVector&)>; | 53 using IndexCallback = base::Callback<void(const CacheStorageIndex&)>; |
| 51 using SizeCallback = base::Callback<void(int64_t)>; | 54 using SizeCallback = base::Callback<void(int64_t)>; |
| 52 | 55 |
| 53 static const char kIndexFileName[]; | 56 static const char kIndexFileName[]; |
| 54 | 57 |
| 55 CacheStorage( | 58 CacheStorage( |
| 56 const base::FilePath& origin_path, | 59 const base::FilePath& origin_path, |
| 57 bool memory_only, | 60 bool memory_only, |
| 58 base::SequencedTaskRunner* cache_task_runner, | 61 base::SequencedTaskRunner* cache_task_runner, |
| 59 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 62 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 60 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 63 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 79 | 82 |
| 80 // Deletes the cache if it exists. If it doesn't exist, | 83 // Deletes the cache if it exists. If it doesn't exist, |
| 81 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. Any existing | 84 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. Any existing |
| 82 // CacheStorageCacheHandle(s) to the cache will remain valid but future | 85 // CacheStorageCacheHandle(s) to the cache will remain valid but future |
| 83 // CacheStorage operations won't be able to access the cache. The cache | 86 // CacheStorage operations won't be able to access the cache. The cache |
| 84 // isn't actually erased from disk until the last handle is dropped. | 87 // isn't actually erased from disk until the last handle is dropped. |
| 85 // TODO(jkarlin): Rename to DoomCache. | 88 // TODO(jkarlin): Rename to DoomCache. |
| 86 void DeleteCache(const std::string& cache_name, | 89 void DeleteCache(const std::string& cache_name, |
| 87 const BoolAndErrorCallback& callback); | 90 const BoolAndErrorCallback& callback); |
| 88 | 91 |
| 89 // Calls the callback with a vector of cache names (keys) available. | 92 // Calls the callback with the cache index. |
| 90 void EnumerateCaches(const StringsCallback& callback); | 93 void EnumerateCaches(const IndexCallback& callback); |
| 91 | 94 |
| 92 // Calls match on the cache with the given |cache_name|. | 95 // Calls match on the cache with the given |cache_name|. |
| 93 void MatchCache(const std::string& cache_name, | 96 void MatchCache(const std::string& cache_name, |
| 94 std::unique_ptr<ServiceWorkerFetchRequest> request, | 97 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 95 const CacheStorageCacheQueryParams& match_params, | 98 const CacheStorageCacheQueryParams& match_params, |
| 96 const CacheStorageCache::ResponseCallback& callback); | 99 const CacheStorageCache::ResponseCallback& callback); |
| 97 | 100 |
| 98 // Calls match on all of the caches in parallel, calling |callback| with the | 101 // Calls match on all of the caches in parallel, calling |callback| with the |
| 99 // response from the first cache (in order of cache creation) to have the | 102 // response from the first cache (in order of cache creation) to have the |
| 100 // entry. If no response is found then |callback| is called with | 103 // entry. If no response is found then |callback| is called with |
| 101 // CACHE_STORAGE_ERROR_NOT_FOUND. | 104 // CACHE_STORAGE_ERROR_NOT_FOUND. |
| 102 void MatchAllCaches(std::unique_ptr<ServiceWorkerFetchRequest> request, | 105 void MatchAllCaches(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 103 const CacheStorageCacheQueryParams& match_params, | 106 const CacheStorageCacheQueryParams& match_params, |
| 104 const CacheStorageCache::ResponseCallback& callback); | 107 const CacheStorageCache::ResponseCallback& callback); |
| 105 | 108 |
| 106 // Sums the sizes of each cache and closes them. Runs |callback| with the | 109 // Sums the sizes of each cache and closes them. Runs |callback| with the |
| 107 // size. | 110 // size. |
| 108 void GetSizeThenCloseAllCaches(const SizeCallback& callback); | 111 void GetSizeThenCloseAllCaches(const SizeCallback& callback); |
| 109 | 112 |
| 110 // The size of all of the origin's contents. This value should be used as an | 113 // The size of all of the origin's contents. This value should be used as an |
| 111 // estimate only since the cache may be modified at any time. | 114 // estimate only since the cache may be modified at any time. |
| 112 void Size(const SizeCallback& callback); | 115 void Size(const SizeCallback& callback); |
| 113 | 116 |
| 114 // The functions below are for tests to verify that the operations run | 117 // The functions below are for tests to verify that the operations run |
| 115 // serially. | 118 // serially. |
| 116 void StartAsyncOperationForTesting(); | 119 void StartAsyncOperationForTesting(); |
| 117 void CompleteAsyncOperationForTesting(); | 120 void CompleteAsyncOperationForTesting(); |
| 118 | 121 |
| 122 // CacheStorageCacheObserver: | |
| 123 void CacheSizeUpdated(const CacheStorageCache* cache, int64_t size) override; | |
| 124 | |
| 119 private: | 125 private: |
| 120 friend class CacheStorageCacheHandle; | 126 friend class CacheStorageCacheHandle; |
| 121 friend class CacheStorageCache; | 127 friend class CacheStorageCache; |
| 128 friend class CacheStorageManagerTest; | |
| 122 class CacheLoader; | 129 class CacheLoader; |
| 123 class MemoryLoader; | 130 class MemoryLoader; |
| 124 class SimpleCacheLoader; | 131 class SimpleCacheLoader; |
| 125 struct CacheMatchResponse; | 132 struct CacheMatchResponse; |
| 126 | 133 |
| 127 typedef std::map<std::string, std::unique_ptr<CacheStorageCache>> CacheMap; | 134 typedef std::map<std::string, std::unique_ptr<CacheStorageCache>> CacheMap; |
| 128 | 135 |
| 129 // Functions for exposing handles to CacheStorageCache to clients. | 136 // Functions for exposing handles to CacheStorageCache to clients. |
| 130 std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle( | 137 std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle( |
| 131 CacheStorageCache* cache); | 138 CacheStorageCache* cache); |
| 132 void AddCacheHandleRef(CacheStorageCache* cache); | 139 void AddCacheHandleRef(CacheStorageCache* cache); |
| 133 void DropCacheHandleRef(CacheStorageCache* cache); | 140 void DropCacheHandleRef(CacheStorageCache* cache); |
| 134 | 141 |
| 135 // Returns a CacheStorageCacheHandle for the given name if the name is known. | 142 // Returns a CacheStorageCacheHandle for the given name if the name is known. |
| 136 // If the CacheStorageCache has been deleted, creates a new one. | 143 // If the CacheStorageCache has been deleted, creates a new one. |
| 137 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( | 144 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( |
| 138 const std::string& cache_name); | 145 const std::string& cache_name); |
| 139 | 146 |
| 140 // Initializer and its callback are below. | 147 // Initializer and its callback are below. |
| 141 void LazyInit(); | 148 void LazyInit(); |
| 142 void LazyInitImpl(); | 149 void LazyInitImpl(); |
| 143 void LazyInitDidLoadIndex( | 150 void LazyInitDidLoadIndex(std::unique_ptr<CacheStorageIndex> index); |
| 144 std::unique_ptr<std::vector<std::string>> indexed_cache_names); | |
| 145 | 151 |
| 146 // The Open and CreateCache callbacks are below. | 152 // The Open and CreateCache callbacks are below. |
| 147 void OpenCacheImpl(const std::string& cache_name, | 153 void OpenCacheImpl(const std::string& cache_name, |
| 148 const CacheAndErrorCallback& callback); | 154 const CacheAndErrorCallback& callback); |
| 149 void CreateCacheDidCreateCache(const std::string& cache_name, | 155 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 150 const CacheAndErrorCallback& callback, | 156 const CacheAndErrorCallback& callback, |
| 151 std::unique_ptr<CacheStorageCache> cache); | 157 std::unique_ptr<CacheStorageCache> cache); |
| 152 void CreateCacheDidWriteIndex( | 158 void CreateCacheDidWriteIndex( |
| 153 const CacheAndErrorCallback& callback, | 159 const CacheAndErrorCallback& callback, |
| 154 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 160 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 155 bool success); | 161 bool success); |
| 156 | 162 |
| 157 // The HasCache callbacks are below. | 163 // The HasCache callbacks are below. |
| 158 void HasCacheImpl(const std::string& cache_name, | 164 void HasCacheImpl(const std::string& cache_name, |
| 159 const BoolAndErrorCallback& callback); | 165 const BoolAndErrorCallback& callback); |
| 160 | 166 |
| 161 // The DeleteCache callbacks are below. | 167 // The DeleteCache callbacks are below. |
| 162 void DeleteCacheImpl(const std::string& cache_name, | 168 void DeleteCacheImpl(const std::string& cache_name, |
| 163 const BoolAndErrorCallback& callback); | 169 const BoolAndErrorCallback& callback); |
| 164 void DeleteCacheDidWriteIndex( | 170 void DeleteCacheDidWriteIndex( |
| 165 const std::string& cache_name, | 171 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 166 const StringVector& original_ordered_cache_names, | |
| 167 const BoolAndErrorCallback& callback, | 172 const BoolAndErrorCallback& callback, |
| 168 bool success); | 173 bool success); |
| 169 void DeleteCacheFinalize(std::unique_ptr<CacheStorageCache> doomed_cache); | 174 void DeleteCacheFinalize(std::unique_ptr<CacheStorageCache> doomed_cache); |
| 170 void DeleteCacheDidGetSize(std::unique_ptr<CacheStorageCache> cache, | 175 void DeleteCacheDidGetSize(std::unique_ptr<CacheStorageCache> cache, |
| 171 int64_t cache_size); | 176 int64_t cache_size); |
| 172 void DeleteCacheDidCleanUp(bool success); | 177 void DeleteCacheDidCleanUp(bool success); |
| 173 | 178 |
| 174 // The EnumerateCache callbacks are below. | 179 // The EnumerateCache callbacks are below. |
| 175 void EnumerateCachesImpl(const StringsCallback& callback); | 180 void EnumerateCachesImpl(const IndexCallback& callback); |
| 176 | 181 |
| 177 // The MatchCache callbacks are below. | 182 // The MatchCache callbacks are below. |
| 178 void MatchCacheImpl(const std::string& cache_name, | 183 void MatchCacheImpl(const std::string& cache_name, |
| 179 std::unique_ptr<ServiceWorkerFetchRequest> request, | 184 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 180 const CacheStorageCacheQueryParams& match_params, | 185 const CacheStorageCacheQueryParams& match_params, |
| 181 const CacheStorageCache::ResponseCallback& callback); | 186 const CacheStorageCache::ResponseCallback& callback); |
| 182 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 187 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 183 const CacheStorageCache::ResponseCallback& callback, | 188 const CacheStorageCache::ResponseCallback& callback, |
| 184 CacheStorageError error, | 189 CacheStorageError error, |
| 185 std::unique_ptr<ServiceWorkerResponse> response, | 190 std::unique_ptr<ServiceWorkerResponse> response, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 196 CacheStorageError error, | 201 CacheStorageError error, |
| 197 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 202 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 198 std::unique_ptr<storage::BlobDataHandle> handle); | 203 std::unique_ptr<storage::BlobDataHandle> handle); |
| 199 void MatchAllCachesDidMatchAll( | 204 void MatchAllCachesDidMatchAll( |
| 200 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, | 205 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 201 const CacheStorageCache::ResponseCallback& callback); | 206 const CacheStorageCache::ResponseCallback& callback); |
| 202 | 207 |
| 203 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); | 208 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); |
| 204 | 209 |
| 205 void SizeImpl(const SizeCallback& callback); | 210 void SizeImpl(const SizeCallback& callback); |
| 211 void SizeRetrievedFromCache( | |
| 212 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 213 const base::Closure& closure, | |
| 214 int64_t* accumulator, | |
| 215 int64_t size); | |
| 216 | |
| 217 void DelayedScheduleWriteIndex(); | |
| 218 void ScheduleWriteIndex(); | |
| 219 void WriteIndex(const base::Callback<void(bool)>& callback); | |
|
jkarlin
2016/12/19 17:48:15
The naming convention for this directory has been
cmumford
2016/12/19 19:25:57
Done.
| |
| 220 bool index_write_pending() const { return !index_write_task_.IsCancelled(); } | |
| 221 // Start a scheduled index write immediately. Returns true if a write was | |
| 222 // scheduled, or false if not. | |
| 223 bool InitiateScheduledIndexWriteForTest(); | |
| 206 | 224 |
| 207 // Whether or not we've loaded the list of cache names into memory. | 225 // Whether or not we've loaded the list of cache names into memory. |
| 208 bool initialized_; | 226 bool initialized_; |
| 209 bool initializing_; | 227 bool initializing_; |
| 210 | 228 |
| 211 // True if the backend is supposed to reside in memory only. | 229 // True if the backend is supposed to reside in memory only. |
| 212 bool memory_only_; | 230 bool memory_only_; |
| 213 | 231 |
| 214 // The pending operation scheduler. | 232 // The pending operation scheduler. |
| 215 std::unique_ptr<CacheStorageScheduler> scheduler_; | 233 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| 216 | 234 |
| 217 // The map of cache names to CacheStorageCache objects. | 235 // The map of cache names to CacheStorageCache objects. |
| 218 CacheMap cache_map_; | 236 CacheMap cache_map_; |
| 219 | 237 |
| 220 // Caches that have been deleted but must still be held onto until all handles | 238 // Caches that have been deleted but must still be held onto until all handles |
| 221 // have been released. | 239 // have been released. |
| 222 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> | 240 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> |
| 223 doomed_caches_; | 241 doomed_caches_; |
| 224 | 242 |
| 225 // CacheStorageCacheHandle reference counts | 243 // CacheStorageCacheHandle reference counts |
| 226 std::map<CacheStorageCache*, size_t> cache_handle_counts_; | 244 std::map<CacheStorageCache*, size_t> cache_handle_counts_; |
| 227 | 245 |
| 228 // The names of caches in the order that they were created. | 246 // The cache index data. |
| 229 StringVector ordered_cache_names_; | 247 std::unique_ptr<CacheStorageIndex> cache_index_; |
| 230 | 248 |
| 231 // The file path for this CacheStorage. | 249 // The file path for this CacheStorage. |
| 232 base::FilePath origin_path_; | 250 base::FilePath origin_path_; |
| 233 | 251 |
| 234 // The TaskRunner to run file IO on. | 252 // The TaskRunner to run file IO on. |
| 235 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 253 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 236 | 254 |
| 237 // Performs backend specific operations (memory vs disk). | 255 // Performs backend specific operations (memory vs disk). |
| 238 std::unique_ptr<CacheLoader> cache_loader_; | 256 std::unique_ptr<CacheLoader> cache_loader_; |
| 239 | 257 |
| 240 // The quota manager. | 258 // The quota manager. |
| 241 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 259 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 242 | 260 |
| 243 // The origin that this CacheStorage is associated with. | 261 // The origin that this CacheStorage is associated with. |
| 244 GURL origin_; | 262 GURL origin_; |
| 245 | 263 |
| 264 base::CancelableClosure index_write_task_; | |
| 265 | |
| 246 base::WeakPtrFactory<CacheStorage> weak_factory_; | 266 base::WeakPtrFactory<CacheStorage> weak_factory_; |
| 247 | 267 |
| 248 DISALLOW_COPY_AND_ASSIGN(CacheStorage); | 268 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
| 249 }; | 269 }; |
| 250 | 270 |
| 251 } // namespace content | 271 } // namespace content |
| 252 | 272 |
| 253 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 273 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| OLD | NEW |