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> | |
| 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; |
| 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 static const int64_t kSizeUnknown = -1; |
| 48 | |
| 49 struct CacheInfo { | |
|
jkarlin
2016/10/21 18:04:19
Suggest CacheMetadata? Seems ever so slightly more
cmumford
2016/11/10 17:28:16
Done.
| |
| 50 CacheInfo(const std::string& name, int64_t size) : name(name), size(size) {} | |
| 51 std::string name; | |
| 52 // The size (in bytes) of the cache. Set to kSizeUnknown if size not known. | |
| 53 int64_t size; | |
| 54 }; | |
| 55 | |
| 46 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; | 56 typedef base::Callback<void(bool, CacheStorageError)> BoolAndErrorCallback; |
| 47 typedef base::Callback<void(std::unique_ptr<CacheStorageCacheHandle>, | 57 typedef base::Callback<void(std::unique_ptr<CacheStorageCacheHandle>, |
| 48 CacheStorageError)> | 58 CacheStorageError)> |
| 49 CacheAndErrorCallback; | 59 CacheAndErrorCallback; |
| 50 using StringsCallback = base::Callback<void(const StringVector&)>; | 60 // TODO(cmumford): Why not pass back the entire index instead of it's list of |
| 61 // CachInfo objects? | |
| 62 using CacheInfoCallback = base::Callback<void(const std::list<CacheInfo>&)>; | |
| 51 using SizeCallback = base::Callback<void(int64_t)>; | 63 using SizeCallback = base::Callback<void(int64_t)>; |
| 52 | 64 |
| 53 static const char kIndexFileName[]; | 65 static const char kIndexFileName[]; |
| 54 | 66 |
| 55 CacheStorage( | 67 CacheStorage( |
| 56 const base::FilePath& origin_path, | 68 const base::FilePath& origin_path, |
| 57 bool memory_only, | 69 bool memory_only, |
| 58 base::SequencedTaskRunner* cache_task_runner, | 70 base::SequencedTaskRunner* cache_task_runner, |
| 59 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 71 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 60 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, | 72 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 79 | 91 |
| 80 // Deletes the cache if it exists. If it doesn't exist, | 92 // Deletes the cache if it exists. If it doesn't exist, |
| 81 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. Any existing | 93 // CACHE_STORAGE_ERROR_NOT_FOUND is returned. Any existing |
| 82 // CacheStorageCacheHandle(s) to the cache will remain valid but future | 94 // CacheStorageCacheHandle(s) to the cache will remain valid but future |
| 83 // CacheStorage operations won't be able to access the cache. The cache | 95 // 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. | 96 // isn't actually erased from disk until the last handle is dropped. |
| 85 // TODO(jkarlin): Rename to DoomCache. | 97 // TODO(jkarlin): Rename to DoomCache. |
| 86 void DeleteCache(const std::string& cache_name, | 98 void DeleteCache(const std::string& cache_name, |
| 87 const BoolAndErrorCallback& callback); | 99 const BoolAndErrorCallback& callback); |
| 88 | 100 |
| 89 // Calls the callback with a vector of cache names (keys) available. | 101 // Calls the callback with a collection of cache metadata available. |
| 90 void EnumerateCaches(const StringsCallback& callback); | 102 void EnumerateCaches(const CacheInfoCallback& callback); |
| 91 | 103 |
| 92 // Calls match on the cache with the given |cache_name|. | 104 // Calls match on the cache with the given |cache_name|. |
| 93 void MatchCache(const std::string& cache_name, | 105 void MatchCache(const std::string& cache_name, |
| 94 std::unique_ptr<ServiceWorkerFetchRequest> request, | 106 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 95 const CacheStorageCacheQueryParams& match_params, | 107 const CacheStorageCacheQueryParams& match_params, |
| 96 const CacheStorageCache::ResponseCallback& callback); | 108 const CacheStorageCache::ResponseCallback& callback); |
| 97 | 109 |
| 98 // Calls match on all of the caches in parallel, calling |callback| with the | 110 // 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 | 111 // 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 | 112 // entry. If no response is found then |callback| is called with |
| 101 // CACHE_STORAGE_ERROR_NOT_FOUND. | 113 // CACHE_STORAGE_ERROR_NOT_FOUND. |
| 102 void MatchAllCaches(std::unique_ptr<ServiceWorkerFetchRequest> request, | 114 void MatchAllCaches(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 103 const CacheStorageCacheQueryParams& match_params, | 115 const CacheStorageCacheQueryParams& match_params, |
| 104 const CacheStorageCache::ResponseCallback& callback); | 116 const CacheStorageCache::ResponseCallback& callback); |
| 105 | 117 |
| 106 // Sums the sizes of each cache and closes them. Runs |callback| with the | 118 // Sums the sizes of each cache and closes them. Runs |callback| with the |
| 107 // size. | 119 // size. |
| 108 void GetSizeThenCloseAllCaches(const SizeCallback& callback); | 120 void GetSizeThenCloseAllCaches(const SizeCallback& callback); |
| 109 | 121 |
| 110 // The size of all of the origin's contents. This value should be used as an | 122 // 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. | 123 // estimate only since the cache may be modified at any time. |
| 112 void Size(const SizeCallback& callback); | 124 void Size(const SizeCallback& callback); |
| 113 | 125 |
| 114 // The functions below are for tests to verify that the operations run | 126 // The functions below are for tests to verify that the operations run |
| 115 // serially. | 127 // serially. |
| 116 void StartAsyncOperationForTesting(); | 128 void StartAsyncOperationForTesting(); |
| 117 void CompleteAsyncOperationForTesting(); | 129 void CompleteAsyncOperationForTesting(); |
| 118 | 130 |
| 131 // CacheStorageCacheObserver: | |
| 132 void CacheModified(const CacheStorageCache* cache, | |
| 133 int64_t size_delta) override; | |
| 134 void SetCacheSize(const CacheStorageCache* cache, | |
| 135 int64_t cache_size) override; | |
| 136 | |
| 119 private: | 137 private: |
| 120 friend class CacheStorageCacheHandle; | 138 friend class CacheStorageCacheHandle; |
| 121 friend class CacheStorageCache; | 139 friend class CacheStorageCache; |
| 122 class CacheLoader; | 140 class CacheLoader; |
| 141 // Note: Not to be confused with class by same name generated by protobuf. | |
| 142 class CacheStorageIndex; | |
| 123 class MemoryLoader; | 143 class MemoryLoader; |
| 124 class SimpleCacheLoader; | 144 class SimpleCacheLoader; |
| 125 struct CacheMatchResponse; | 145 struct CacheMatchResponse; |
| 126 | 146 |
| 127 typedef std::map<std::string, std::unique_ptr<CacheStorageCache>> CacheMap; | 147 typedef std::map<std::string, std::unique_ptr<CacheStorageCache>> CacheMap; |
| 128 | 148 |
| 129 // Functions for exposing handles to CacheStorageCache to clients. | 149 // Functions for exposing handles to CacheStorageCache to clients. |
| 130 std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle( | 150 std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle( |
| 131 CacheStorageCache* cache); | 151 CacheStorageCache* cache); |
| 132 void AddCacheHandleRef(CacheStorageCache* cache); | 152 void AddCacheHandleRef(CacheStorageCache* cache); |
| 133 void DropCacheHandleRef(CacheStorageCache* cache); | 153 void DropCacheHandleRef(CacheStorageCache* cache); |
| 134 | 154 |
| 135 // Returns a CacheStorageCacheHandle for the given name if the name is known. | 155 // Returns a CacheStorageCacheHandle for the given name if the name is known. |
| 136 // If the CacheStorageCache has been deleted, creates a new one. | 156 // If the CacheStorageCache has been deleted, creates a new one. |
| 137 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( | 157 std::unique_ptr<CacheStorageCacheHandle> GetLoadedCache( |
| 138 const std::string& cache_name); | 158 const std::string& cache_name); |
| 139 | 159 |
| 140 // Initializer and its callback are below. | 160 // Initializer and its callback are below. |
| 141 void LazyInit(); | 161 void LazyInit(); |
| 142 void LazyInitImpl(); | 162 void LazyInitImpl(); |
| 143 void LazyInitDidLoadIndex( | 163 void LazyInitDidLoadIndex(std::unique_ptr<CacheStorageIndex> index); |
| 144 std::unique_ptr<std::vector<std::string>> indexed_cache_names); | |
| 145 | 164 |
| 146 // The Open and CreateCache callbacks are below. | 165 // The Open and CreateCache callbacks are below. |
| 147 void OpenCacheImpl(const std::string& cache_name, | 166 void OpenCacheImpl(const std::string& cache_name, |
| 148 const CacheAndErrorCallback& callback); | 167 const CacheAndErrorCallback& callback); |
| 149 void CreateCacheDidCreateCache(const std::string& cache_name, | 168 void CreateCacheDidCreateCache(const std::string& cache_name, |
| 150 const CacheAndErrorCallback& callback, | 169 const CacheAndErrorCallback& callback, |
| 151 std::unique_ptr<CacheStorageCache> cache); | 170 std::unique_ptr<CacheStorageCache> cache); |
| 152 void CreateCacheDidWriteIndex( | 171 void CreateCacheDidWriteIndex( |
| 153 const CacheAndErrorCallback& callback, | 172 const CacheAndErrorCallback& callback, |
| 154 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 173 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 155 bool success); | 174 bool success); |
| 156 | 175 |
| 157 // The HasCache callbacks are below. | 176 // The HasCache callbacks are below. |
| 158 void HasCacheImpl(const std::string& cache_name, | 177 void HasCacheImpl(const std::string& cache_name, |
| 159 const BoolAndErrorCallback& callback); | 178 const BoolAndErrorCallback& callback); |
| 160 | 179 |
| 161 // The DeleteCache callbacks are below. | 180 // The DeleteCache callbacks are below. |
| 162 void DeleteCacheImpl(const std::string& cache_name, | 181 void DeleteCacheImpl(const std::string& cache_name, |
| 163 const BoolAndErrorCallback& callback); | 182 const BoolAndErrorCallback& callback); |
| 164 void DeleteCacheDidWriteIndex( | 183 void DeleteCacheDidWriteIndex( |
| 165 const std::string& cache_name, | 184 const std::string& cache_name, |
| 166 const StringVector& original_ordered_cache_names, | 185 std::unique_ptr<CacheStorageIndex> index_before_delete, |
| 167 const BoolAndErrorCallback& callback, | 186 const BoolAndErrorCallback& callback, |
| 168 bool success); | 187 bool success); |
| 169 void DeleteCacheFinalize(std::unique_ptr<CacheStorageCache> doomed_cache); | 188 void DeleteCacheFinalize(std::unique_ptr<CacheStorageCache> doomed_cache); |
| 170 void DeleteCacheDidGetSize(std::unique_ptr<CacheStorageCache> cache, | 189 void DeleteCacheDidGetSize(std::unique_ptr<CacheStorageCache> cache, |
| 171 int64_t cache_size); | 190 int64_t cache_size); |
| 172 void DeleteCacheDidCleanUp(bool success); | 191 void DeleteCacheDidCleanUp(bool success); |
| 173 | 192 |
| 174 // The EnumerateCache callbacks are below. | 193 // The EnumerateCache callbacks are below. |
| 175 void EnumerateCachesImpl(const StringsCallback& callback); | 194 void EnumerateCachesImpl(const CacheInfoCallback& callback); |
| 176 | 195 |
| 177 // The MatchCache callbacks are below. | 196 // The MatchCache callbacks are below. |
| 178 void MatchCacheImpl(const std::string& cache_name, | 197 void MatchCacheImpl(const std::string& cache_name, |
| 179 std::unique_ptr<ServiceWorkerFetchRequest> request, | 198 std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 180 const CacheStorageCacheQueryParams& match_params, | 199 const CacheStorageCacheQueryParams& match_params, |
| 181 const CacheStorageCache::ResponseCallback& callback); | 200 const CacheStorageCache::ResponseCallback& callback); |
| 182 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 201 void MatchCacheDidMatch(std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 183 const CacheStorageCache::ResponseCallback& callback, | 202 const CacheStorageCache::ResponseCallback& callback, |
| 184 CacheStorageError error, | 203 CacheStorageError error, |
| 185 std::unique_ptr<ServiceWorkerResponse> response, | 204 std::unique_ptr<ServiceWorkerResponse> response, |
| 186 std::unique_ptr<storage::BlobDataHandle> handle); | 205 std::unique_ptr<storage::BlobDataHandle> handle); |
| 187 | 206 |
| 188 // The MatchAllCaches callbacks are below. | 207 // The MatchAllCaches callbacks are below. |
| 189 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, | 208 void MatchAllCachesImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, |
| 190 const CacheStorageCacheQueryParams& match_params, | 209 const CacheStorageCacheQueryParams& match_params, |
| 191 const CacheStorageCache::ResponseCallback& callback); | 210 const CacheStorageCache::ResponseCallback& callback); |
| 192 void MatchAllCachesDidMatch( | 211 void MatchAllCachesDidMatch( |
| 193 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 212 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 194 CacheMatchResponse* out_match_response, | 213 CacheMatchResponse* out_match_response, |
| 195 const base::Closure& barrier_closure, | 214 const base::Closure& barrier_closure, |
| 196 CacheStorageError error, | 215 CacheStorageError error, |
| 197 std::unique_ptr<ServiceWorkerResponse> service_worker_response, | 216 std::unique_ptr<ServiceWorkerResponse> service_worker_response, |
| 198 std::unique_ptr<storage::BlobDataHandle> handle); | 217 std::unique_ptr<storage::BlobDataHandle> handle); |
| 199 void MatchAllCachesDidMatchAll( | 218 void MatchAllCachesDidMatchAll( |
| 200 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, | 219 std::unique_ptr<std::vector<CacheMatchResponse>> match_responses, |
| 201 const CacheStorageCache::ResponseCallback& callback); | 220 const CacheStorageCache::ResponseCallback& callback); |
| 202 | 221 |
| 222 void CloseAllCaches(); | |
| 203 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); | 223 void GetSizeThenCloseAllCachesImpl(const SizeCallback& callback); |
| 204 | 224 |
| 205 void SizeImpl(const SizeCallback& callback); | 225 void SizeImpl(const SizeCallback& callback); |
| 226 void SizeRetrievedFromCache( | |
| 227 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | |
| 228 const base::Closure& closure, | |
| 229 int64_t* accumulator, | |
| 230 int64_t size); | |
| 231 | |
| 232 void ScheduleWriteIndex(); | |
| 206 | 233 |
| 207 // Whether or not we've loaded the list of cache names into memory. | 234 // Whether or not we've loaded the list of cache names into memory. |
| 208 bool initialized_; | 235 bool initialized_; |
| 209 bool initializing_; | 236 bool initializing_; |
| 210 | 237 |
| 211 // True if the backend is supposed to reside in memory only. | 238 // True if the backend is supposed to reside in memory only. |
| 212 bool memory_only_; | 239 bool memory_only_; |
| 213 | 240 |
| 214 // The pending operation scheduler. | 241 // The pending operation scheduler. |
| 215 std::unique_ptr<CacheStorageScheduler> scheduler_; | 242 std::unique_ptr<CacheStorageScheduler> scheduler_; |
| 216 | 243 |
| 217 // The map of cache names to CacheStorageCache objects. | 244 // The map of cache names to CacheStorageCache objects. |
| 218 CacheMap cache_map_; | 245 CacheMap cache_map_; |
| 219 | 246 |
| 220 // Caches that have been deleted but must still be held onto until all handles | 247 // Caches that have been deleted but must still be held onto until all handles |
| 221 // have been released. | 248 // have been released. |
| 222 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> | 249 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> |
| 223 doomed_caches_; | 250 doomed_caches_; |
| 224 | 251 |
| 225 // CacheStorageCacheHandle reference counts | 252 // CacheStorageCacheHandle reference counts |
| 226 std::map<CacheStorageCache*, size_t> cache_handle_counts_; | 253 std::map<CacheStorageCache*, size_t> cache_handle_counts_; |
| 227 | 254 |
| 228 // The names of caches in the order that they were created. | 255 // The cache index data. |
| 229 StringVector ordered_cache_names_; | 256 std::unique_ptr<CacheStorageIndex> index_; |
| 230 | 257 |
| 231 // The file path for this CacheStorage. | 258 // The file path for this CacheStorage. |
| 232 base::FilePath origin_path_; | 259 base::FilePath origin_path_; |
| 233 | 260 |
| 234 // The TaskRunner to run file IO on. | 261 // The TaskRunner to run file IO on. |
| 235 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 262 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 236 | 263 |
| 237 // Performs backend specific operations (memory vs disk). | 264 // Performs backend specific operations (memory vs disk). |
| 238 std::unique_ptr<CacheLoader> cache_loader_; | 265 std::unique_ptr<CacheLoader> cache_loader_; |
| 239 | 266 |
| 240 // The quota manager. | 267 // The quota manager. |
| 241 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; | 268 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
| 242 | 269 |
| 243 // The origin that this CacheStorage is associated with. | 270 // The origin that this CacheStorage is associated with. |
| 244 GURL origin_; | 271 GURL origin_; |
| 245 | 272 |
| 246 base::WeakPtrFactory<CacheStorage> weak_factory_; | 273 base::WeakPtrFactory<CacheStorage> weak_factory_; |
| 247 | 274 |
| 248 DISALLOW_COPY_AND_ASSIGN(CacheStorage); | 275 DISALLOW_COPY_AND_ASSIGN(CacheStorage); |
| 249 }; | 276 }; |
| 250 | 277 |
| 251 } // namespace content | 278 } // namespace content |
| 252 | 279 |
| 253 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ | 280 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ |
| OLD | NEW |