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 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 // TODO(jkarlin): Constrain the total bytes used per origin. | 40 // TODO(jkarlin): Constrain the total bytes used per origin. |
| 41 | 41 |
| 42 // 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 |
| 43 // owned by the CacheStorageManager. This class expects to be run | 43 // owned by the CacheStorageManager. This class expects to be run |
| 44 // on the IO thread. The asynchronous methods are executed serially. | 44 // on the IO thread. The asynchronous methods are executed serially. |
| 45 class CONTENT_EXPORT CacheStorage : public CacheStorageCacheObserver { | 45 class CONTENT_EXPORT CacheStorage : public CacheStorageCacheObserver { |
| 46 public: | 46 public: |
| 47 constexpr static int64_t kSizeUnknown = -1; | 47 constexpr static int64_t kSizeUnknown = -1; |
| 48 | 48 |
| 49 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 49 using BoolAndErrorCallback = |
| 50 typedef base::Callback<void(std::unique_ptr<CacheStorageCacheHandle>, | 50 base::OnceCallback<void(bool, CacheStorageError)>; |
| 51 CacheStorageError)> | 51 using CacheAndErrorCallback = |
| 52 CacheAndErrorCallback; | 52 base::OnceCallback<void(std::unique_ptr<CacheStorageCacheHandle>, |
| 53 using IndexCallback = base::Callback<void(const CacheStorageIndex&)>; | 53 CacheStorageError)>; |
| 54 using SizeCallback = base::Callback<void(int64_t)>; | 54 using IndexCallback = base::OnceCallback<void(const CacheStorageIndex&)>; |
| 55 using SizeCallback = base::OnceCallback<void(int64_t)>; | |
| 55 | 56 |
| 56 static const char kIndexFileName[]; | 57 static const char kIndexFileName[]; |
| 57 | 58 |
| 58 CacheStorage( | 59 CacheStorage( |
| 59 const base::FilePath& origin_path, | 60 const base::FilePath& origin_path, |
| 60 bool memory_only, | 61 bool memory_only, |
| 61 base::SequencedTaskRunner* cache_task_runner, | 62 base::SequencedTaskRunner* cache_task_runner, |
| 62 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 63 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 63 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 64 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| 64 base::WeakPtr<storage::BlobStorageContext> blob_context, | 65 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 65 const GURL& origin); | 66 const GURL& origin); |
| 66 | 67 |
| 67 // Any unfinished asynchronous operations may not complete or call their | 68 // Any unfinished asynchronous operations may not complete or call their |
| 68 // callbacks. | 69 // callbacks. |
| 69 virtual ~CacheStorage(); | 70 virtual ~CacheStorage(); |
| 70 | 71 |
| 71 // Get the cache for the given key. If the cache is not found it is | 72 // Get the cache for the given key. If the cache is not found it is |
| 72 // created. The CacheStorgeCacheHandle in the callback prolongs the lifetime | 73 // created. The CacheStorgeCacheHandle in the callback prolongs the lifetime |
| 73 // of the cache. Once all handles to a cache are deleted the cache is deleted. | 74 // of the cache. Once all handles to a cache are deleted the cache is deleted. |
| 74 // The cache will also be deleted in the CacheStorage's destructor so be sure | 75 // The cache will also be deleted in the CacheStorage's destructor so be sure |
| 75 // to check the handle's value before using it. | 76 // to check the handle's value before using it. |
| 76 void OpenCache(const std::string& cache_name, | 77 void OpenCache(const std::string& cache_name, CacheAndErrorCallback callback); |
| 77 const CacheAndErrorCallback& callback); | |
| 78 | 78 |
| 79 // Calls the callback with whether or not the cache exists. | 79 // Calls the callback with whether or not the cache exists. |
| 80 void HasCache(const std::string& cache_name, | 80 void HasCache(const std::string& cache_name, BoolAndErrorCallback callback); |
| 81 const BoolAndErrorCallback& callback); | |
| 82 | 81 |
| 83 // Deletes the cache if it exists. If it doesn't exist, | 82 // Deletes the cache if it exists. If it doesn't exist, |
| 84 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. Any existing | 83 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. Any existing |
| 85 // CacheStorageCacheHandle(s) to the cache will remain valid but future | 84 // CacheStorageCacheHandle(s) to the cache will remain valid but future |
| 86 // CacheStorage operations won't be able to access the cache. The cache | 85 // CacheStorage operations won't be able to access the cache. The cache |
| 87 // isn't actually erased from disk until the last handle is dropped. | 86 // isn't actually erased from disk until the last handle is dropped. |
| 88 // TODO(jkarlin): Rename to DoomCache. | 87 // TODO(jkarlin): Rename to DoomCache. |
| 89 void DeleteCache(const std::string& cache_name, | 88 void DeleteCache(const std::string& cache_name, |
| 90 const BoolAndErrorCallback& callback); | 89 BoolAndErrorCallback callback); |
| 91 | 90 |
| 92 // Calls the callback with the cache index. | 91 // Calls the callback with the cache index. |
| 93 void EnumerateCaches(const IndexCallback& callback); | 92 void EnumerateCaches(IndexCallback callback); |
| 94 | 93 |
| 95 // Calls match on the cache with the given |cache_name|. | 94 // Calls match on the cache with the given |cache_name|. |
| 96 void MatchCache(const std::string& cache_name, | 95 void MatchCache(const std::string& cache_name, |
| 97 std::unique_ptr<ServiceWorkerFetchRequest> request, | 96 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 98 const CacheStorageCacheQueryParams& match_params, | 97 const CacheStorageCacheQueryParams& match_params, |
| 99 const CacheStorageCache::ResponseCallback& callback); | 98 CacheStorageCache::ResponseCallback callback); |
| 100 | 99 |
| 101 // Calls match on all of the caches in parallel, calling |callback| with the | 100 // Calls match on all of the caches in parallel, calling |callback| with the |
| 102 // response from the first cache (in order of cache creation) to have the | 101 // response from the first cache (in order of cache creation) to have the |
| 103 // entry. If no response is found then |callback| is called with | 102 // entry. If no response is found then |callback| is called with |
| 104 // CACHE_STORAGE_ERROR_NOT_FOUND. | 103 // CACHE_STORAGE_ERROR_NOT_FOUND. |
| 105 void MatchAllCaches(std::unique_ptr<ServiceWorkerFetchRequest> request, | 104 void MatchAllCaches(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 106 const CacheStorageCacheQueryParams& match_params, | 105 const CacheStorageCacheQueryParams& match_params, |
| 107 const CacheStorageCache::ResponseCallback& callback); | 106 CacheStorageCache::ResponseCallback callback); |
| 108 | 107 |
| 109 // Sums the sizes of each cache and closes them. Runs |callback| with the | 108 // Sums the sizes of each cache and closes them. Runs |callback| with the |
| 110 // size. | 109 // size. |
| 111 void GetSizeThenCloseAllCaches(const SizeCallback& callback); | 110 void GetSizeThenCloseAllCaches(SizeCallback callback); |
| 112 | 111 |
| 113 // The size of all of the origin's contents. This value should be used as an | 112 // The size of all of the origin's contents. This value should be used as an |
| 114 // estimate only since the cache may be modified at any time. | 113 // estimate only since the cache may be modified at any time. |
| 115 void Size(const SizeCallback& callback); | 114 void Size(SizeCallback callback); |
| 116 | 115 |
| 117 // The functions below are for tests to verify that the operations run | 116 // The functions below are for tests to verify that the operations run |
| 118 // serially. | 117 // serially. |
| 119 void StartAsyncOperationForTesting(); | 118 void StartAsyncOperationForTesting(); |
| 120 void CompleteAsyncOperationForTesting(); | 119 void CompleteAsyncOperationForTesting(); |
| 121 | 120 |
| 122 // CacheStorageCacheObserver: | 121 // CacheStorageCacheObserver: |
| 123 void CacheSizeUpdated(const CacheStorageCache* cache, int64_t size) override; | 122 void CacheSizeUpdated(const CacheStorageCache* cache, int64_t size) override; |
| 124 | 123 |
| 125 private: | 124 private: |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 144 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( | 143 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( |
| 145 const std::string& cache_name); | 144 const std::string& cache_name); |
| 146 | 145 |
| 147 // Initializer and its callback are below. | 146 // Initializer and its callback are below. |
| 148 void LazyInit(); | 147 void LazyInit(); |
| 149 void LazyInitImpl(); | 148 void LazyInitImpl(); |
| 150 void LazyInitDidLoadIndex(std::unique_ptr<CacheStorageIndex> index); | 149 void LazyInitDidLoadIndex(std::unique_ptr<CacheStorageIndex> index); |
| 151 | 150 |
| 152 // The Open and CreateCache callbacks are below. | 151 // The Open and CreateCache callbacks are below. |
| 153 void OpenCacheImpl(const std::string& cache_name, | 152 void OpenCacheImpl(const std::string& cache_name, |
| 154 const CacheAndErrorCallback& callback); | 153 CacheAndErrorCallback callback); |
| 155 void CreateCacheDidCreateCache(const std::string& cache_name, | 154 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 156 const CacheAndErrorCallback& callback, | 155 CacheAndErrorCallback callback, |
| 157 std::unique_ptr<CacheStorageCache> cache); | 156 std::unique_ptr<CacheStorageCache> cache); |
| 158 void CreateCacheDidWriteIndex( | 157 void CreateCacheDidWriteIndex( |
| 159 const CacheAndErrorCallback& callback, | 158 CacheAndErrorCallback callback, |
| 160 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 159 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 161 bool success); | 160 bool success); |
| 162 | 161 |
| 163 // The HasCache callbacks are below. | 162 // The HasCache callbacks are below. |
| 164 void HasCacheImpl(const std::string& cache_name, | 163 void HasCacheImpl(const std::string& cache_name, |
| 165 const BoolAndErrorCallback& callback); | 164 BoolAndErrorCallback callback); |
| 166 | 165 |
| 167 // The DeleteCache callbacks are below. | 166 // The DeleteCache callbacks are below. |
| 168 void DeleteCacheImpl(const std::string& cache_name, | 167 void DeleteCacheImpl(const std::string& cache_name, |
| 169 const BoolAndErrorCallback& callback); | 168 BoolAndErrorCallback callback); |
| 170 void DeleteCacheDidWriteIndex( | 169 void DeleteCacheDidWriteIndex( |
| 171 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 170 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 172 const BoolAndErrorCallback& callback, | 171 BoolAndErrorCallback callback, |
| 173 bool success); | 172 bool success); |
| 174 void DeleteCacheFinalize(CacheStorageCache* doomed_cache); | 173 void DeleteCacheFinalize(CacheStorageCache* doomed_cache); |
| 175 void DeleteCacheDidGetSize(CacheStorageCache* doomed_cache, | 174 void DeleteCacheDidGetSize(CacheStorageCache* doomed_cache, |
| 176 int64_t cache_size); | 175 int64_t cache_size); |
| 177 void DeleteCacheDidCleanUp(bool success); | 176 void DeleteCacheDidCleanUp(bool success); |
| 178 | 177 |
| 179 // The EnumerateCache callbacks are below. | 178 // The EnumerateCache callbacks are below. |
| 180 void EnumerateCachesImpl(const IndexCallback& callback); | 179 void EnumerateCachesImpl(IndexCallback callback); |
| 181 | 180 |
| 182 // The MatchCache callbacks are below. | 181 // The MatchCache callbacks are below. |
| 183 void MatchCacheImpl(const std::string& cache_name, | 182 void MatchCacheImpl(const std::string& cache_name, |
| 184 std::unique_ptr<ServiceWorkerFetchRequest> request, | 183 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 185 const CacheStorageCacheQueryParams& match_params, | 184 const CacheStorageCacheQueryParams& match_params, |
| 186 const CacheStorageCache::ResponseCallback& callback); | 185 CacheStorageCache::ResponseCallback callback); |
| 187 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 186 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 188 const CacheStorageCache::ResponseCallback& callback, | 187 CacheStorageCache::ResponseCallback callback, |
| 189 CacheStorageError error, | 188 CacheStorageError error, |
| 190 std::unique_ptr<ServiceWorkerResponse> response, | 189 std::unique_ptr<ServiceWorkerResponse> response, |
| 191 std::unique_ptr<storage::BlobDataHandle> handle); | 190 std::unique_ptr<storage::BlobDataHandle> handle); |
| 192 | 191 |
| 193 // The MatchAllCaches callbacks are below. | 192 // The MatchAllCaches callbacks are below. |
| 194 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, | 193 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 195 const CacheStorageCacheQueryParams& match_params, | 194 const CacheStorageCacheQueryParams& match_params, |
| 196 const CacheStorageCache::ResponseCallback& callback); | 195 CacheStorageCache::ResponseCallback callback); |
| 197 void MatchAllCachesDidMatch( | 196 void MatchAllCachesDidMatch( |
| 198 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 197 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 199 CacheMatchResponse* out_match_response, | 198 CacheMatchResponse* out_match_response, |
| 200 const base::Closure& barrier_closure, | 199 const base::RepeatingClosure& barrier_closure, |
| 201 CacheStorageError error, | 200 CacheStorageError error, |
| 202 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 201 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 203 std::unique_ptr<storage::BlobDataHandle> handle); | 202 std::unique_ptr<storage::BlobDataHandle> handle); |
| 204 void MatchAllCachesDidMatchAll( | 203 void MatchAllCachesDidMatchAll( |
| 205 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, | 204 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 206 const CacheStorageCache::ResponseCallback& callback); | 205 CacheStorageCache::ResponseCallback callback); |
| 207 | 206 |
| 208 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); | 207 void GetSizeThenCloseAllCachesImpl(SizeCallback callback); |
| 209 | 208 |
| 210 void SizeImpl(const SizeCallback& callback); | 209 void SizeImpl(SizeCallback callback); |
| 211 void SizeRetrievedFromCache( | 210 void SizeRetrievedFromCache( |
| 212 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 211 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 213 const base::Closure& closure, | 212 base::OnceClosure closure, |
|
cmumford
2017/06/21 19:07:56
Should this be a RepeatingClosure?
jsbell
2017/06/21 20:38:16
This method only calls it once, so it can take a O
| |
| 214 int64_t* accumulator, | 213 int64_t* accumulator, |
| 215 int64_t size); | 214 int64_t size); |
| 216 | 215 |
| 217 void ScheduleWriteIndex(); | 216 void ScheduleWriteIndex(); |
| 218 void WriteIndex(const base::Callback<void(bool)>& callback); | 217 void WriteIndex(base::OnceCallback<void(bool)> callback); |
| 219 void WriteIndexImpl(const base::Callback<void(bool)>& callback); | 218 void WriteIndexImpl(base::OnceCallback<void(bool)> callback); |
| 220 bool index_write_pending() const { return !index_write_task_.IsCancelled(); } | 219 bool index_write_pending() const { return !index_write_task_.IsCancelled(); } |
| 221 // Start a scheduled index write immediately. Returns true if a write was | 220 // Start a scheduled index write immediately. Returns true if a write was |
| 222 // scheduled, or false if not. | 221 // scheduled, or false if not. |
| 223 bool InitiateScheduledIndexWriteForTest( | 222 bool InitiateScheduledIndexWriteForTest( |
| 224 const base::Callback<void(bool)>& callback); | 223 base::OnceCallback<void(bool)> callback); |
| 225 | 224 |
| 226 // 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. |
| 227 bool initialized_; | 226 bool initialized_; |
| 228 bool initializing_; | 227 bool initializing_; |
| 229 | 228 |
| 230 // True if the backend is supposed to reside in memory only. | 229 // True if the backend is supposed to reside in memory only. |
| 231 bool memory_only_; | 230 bool memory_only_; |
| 232 | 231 |
| 233 // The pending operation scheduler. | 232 // The pending operation scheduler. |
| 234 std::unique_ptr<CacheStorageScheduler> scheduler_; | 233 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 265 base::CancelableClosure index_write_task_; | 264 base::CancelableClosure index_write_task_; |
| 266 | 265 |
| 267 base::WeakPtrFactory<CacheStorage> weak_factory_; | 266 base::WeakPtrFactory<CacheStorage> weak_factory_; |
| 268 | 267 |
| 269 DISALLOW_COPY_AND_ASSIGN(CacheStorage); | 268 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
| 270 }; | 269 }; |
| 271 | 270 |
| 272 } // namespace content | 271 } // namespace content |
| 273 | 272 |
| 274 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 273 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| OLD | NEW |