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