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