Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1746)

Side by Side Diff: content/browser/cache_storage/cache_storage.h

Issue 2416713002: Write out CacheStorageCache size to index file. (Closed)
Patch Set: BrowserThread::PostDelayedTask(IO, ...) --> base::ThreadTaskRunnerHandle::Get()->Post* Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(CacheStorageCache* doomed_cache); 174 void DeleteCacheFinalize(CacheStorageCache* doomed_cache);
170 void DeleteCacheDidGetSize(CacheStorageCache* doomed_cache, 175 void DeleteCacheDidGetSize(CacheStorageCache* doomed_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
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 ScheduleWriteIndex();
218 void WriteIndex(const base::Callback<void(bool)>& callback);
219 void WriteIndexImpl(const base::Callback<void(bool)>& callback);
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(
224 const base::Callback<void(bool)>& callback);
206 225
207 // Whether or not we've loaded the list of cache names into memory. 226 // Whether or not we've loaded the list of cache names into memory.
208 bool initialized_; 227 bool initialized_;
209 bool initializing_; 228 bool initializing_;
210 229
211 // True if the backend is supposed to reside in memory only. 230 // True if the backend is supposed to reside in memory only.
212 bool memory_only_; 231 bool memory_only_;
213 232
214 // The pending operation scheduler. 233 // The pending operation scheduler.
215 std::unique_ptr<CacheStorageScheduler> scheduler_; 234 std::unique_ptr<CacheStorageScheduler> scheduler_;
216 235
217 // The map of cache names to CacheStorageCache objects. 236 // The map of cache names to CacheStorageCache objects.
218 CacheMap cache_map_; 237 CacheMap cache_map_;
219 238
220 // Caches that have been deleted but must still be held onto until all handles 239 // Caches that have been deleted but must still be held onto until all handles
221 // have been released. 240 // have been released.
222 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>> 241 std::map<CacheStorageCache*, std::unique_ptr<CacheStorageCache>>
223 doomed_caches_; 242 doomed_caches_;
224 243
225 // CacheStorageCacheHandle reference counts 244 // CacheStorageCacheHandle reference counts
226 std::map<CacheStorageCache*, size_t> cache_handle_counts_; 245 std::map<CacheStorageCache*, size_t> cache_handle_counts_;
227 246
228 // The names of caches in the order that they were created. 247 // The cache index data.
229 StringVector ordered_cache_names_; 248 std::unique_ptr<CacheStorageIndex> cache_index_;
230 249
231 // The file path for this CacheStorage. 250 // The file path for this CacheStorage.
232 base::FilePath origin_path_; 251 base::FilePath origin_path_;
233 252
234 // The TaskRunner to run file IO on. 253 // The TaskRunner to run file IO on.
235 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; 254 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
236 255
237 // Performs backend specific operations (memory vs disk). 256 // Performs backend specific operations (memory vs disk).
238 std::unique_ptr<CacheLoader> cache_loader_; 257 std::unique_ptr<CacheLoader> cache_loader_;
239 258
240 // The quota manager. 259 // The quota manager.
241 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; 260 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
242 261
243 // The origin that this CacheStorage is associated with. 262 // The origin that this CacheStorage is associated with.
244 GURL origin_; 263 GURL origin_;
245 264
265 base::CancelableClosure index_write_task_;
266
246 base::WeakPtrFactory<CacheStorage> weak_factory_; 267 base::WeakPtrFactory<CacheStorage> weak_factory_;
247 268
248 DISALLOW_COPY_AND_ASSIGN(CacheStorage); 269 DISALLOW_COPY_AND_ASSIGN(CacheStorage);
249 }; 270 };
250 271
251 } // namespace content 272 } // namespace content
252 273
253 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_ 274 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698