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

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

Issue 2901083002: [CacheStorage] Pad and bin opaque resource sizes. (Closed)
Patch Set: Rebased and resolved conflicts. Created 3 years, 7 months 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_CACHE_H_ 5 #ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ 6 #define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 base::WeakPtr<storage::BlobStorageContext> blob_context); 77 base::WeakPtr<storage::BlobStorageContext> blob_context);
78 static std::unique_ptr<CacheStorageCache> CreatePersistentCache( 78 static std::unique_ptr<CacheStorageCache> CreatePersistentCache(
79 const GURL& origin, 79 const GURL& origin,
80 const std::string& cache_name, 80 const std::string& cache_name,
81 CacheStorage* cache_storage, 81 CacheStorage* cache_storage,
82 const base::FilePath& path, 82 const base::FilePath& path,
83 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 83 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
84 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 84 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
85 base::WeakPtr<storage::BlobStorageContext> blob_context, 85 base::WeakPtr<storage::BlobStorageContext> blob_context,
86 int64_t cache_size); 86 int64_t cache_size);
87 static int64_t CalculateResponsePadding(
88 const ServiceWorkerResponse& response);
87 89
88 // Returns ERROR_TYPE_NOT_FOUND if not found. 90 // Returns ERROR_TYPE_NOT_FOUND if not found.
89 void Match(std::unique_ptr<ServiceWorkerFetchRequest> request, 91 void Match(std::unique_ptr<ServiceWorkerFetchRequest> request,
90 const CacheStorageCacheQueryParams& match_params, 92 const CacheStorageCacheQueryParams& match_params,
91 const ResponseCallback& callback); 93 const ResponseCallback& callback);
92 94
93 // Returns CACHE_STORAGE_OK and matched responses in this cache. If there are 95 // Returns CACHE_STORAGE_OK and matched responses in this cache. If there are
94 // no responses, returns CACHE_STORAGE_OK and an empty vector. 96 // no responses, returns CACHE_STORAGE_OK and an empty vector.
95 void MatchAll(std::unique_ptr<ServiceWorkerFetchRequest> request, 97 void MatchAll(std::unique_ptr<ServiceWorkerFetchRequest> request,
96 const CacheStorageCacheQueryParams& match_params, 98 const CacheStorageCacheQueryParams& match_params,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 // the cache's size. 153 // the cache's size.
152 void GetSizeThenClose(const SizeCallback& callback); 154 void GetSizeThenClose(const SizeCallback& callback);
153 155
154 // Async operations in progress will cancel and not run their callbacks. 156 // Async operations in progress will cancel and not run their callbacks.
155 virtual ~CacheStorageCache(); 157 virtual ~CacheStorageCache();
156 158
157 base::FilePath path() const { return path_; } 159 base::FilePath path() const { return path_; }
158 160
159 std::string cache_name() const { return cache_name_; } 161 std::string cache_name() const { return cache_name_; }
160 162
161 int64_t cache_size() const { return cache_size_; } 163 // Return the total cache size (actual space used + padding).
164 int64_t CacheSize() const;
162 165
163 // Set the one observer that will be notified of changes to this cache. 166 // Set the one observer that will be notified of changes to this cache.
164 // Note: Either the observer must have a lifetime longer than this instance 167 // Note: Either the observer must have a lifetime longer than this instance
165 // or call SetObserver(nullptr) to stop receiving notification of changes. 168 // or call SetObserver(nullptr) to stop receiving notification of changes.
166 void SetObserver(CacheStorageCacheObserver* observer); 169 void SetObserver(CacheStorageCacheObserver* observer);
167 170
168 base::WeakPtr<CacheStorageCache> AsWeakPtr(); 171 base::WeakPtr<CacheStorageCache> AsWeakPtr();
169 172
170 private: 173 private:
171 enum class QueryCacheType { REQUESTS, REQUESTS_AND_RESPONSES, CACHE_ENTRIES }; 174 // QueryCache types:
175 const uint32_t QUERY_CACHE_REQUESTS = 0x1;
176 const uint32_t QUERY_CACHE_RESPONSES_WITH_BODIES = 0x2;
177 const uint32_t QUERY_CACHE_RESPONSES_NO_BODIES = 0x4;
178 const uint32_t QUERY_CACHE_ENTRIES = 0x8;
172 179
173 // The backend progresses from uninitialized, to open, to closed, and cannot 180 // The backend progresses from uninitialized, to open, to closed, and cannot
174 // reverse direction. The open step may be skipped. 181 // reverse direction. The open step may be skipped.
175 enum BackendState { 182 enum BackendState {
176 BACKEND_UNINITIALIZED, // No backend, create backend on first operation. 183 BACKEND_UNINITIALIZED, // No backend, create backend on first operation.
177 BACKEND_OPEN, // Backend can be used. 184 BACKEND_OPEN, // Backend can be used.
178 BACKEND_CLOSED // Backend cannot be used. All ops should fail. 185 BACKEND_CLOSED // Backend cannot be used. All ops should fail.
179 }; 186 };
180 187
181 friend class base::RefCounted<CacheStorageCache>; 188 friend class base::RefCounted<CacheStorageCache>;
182 friend class TestCacheStorageCache; 189 friend class TestCacheStorageCache;
183 friend class CacheStorageCacheTest; 190 friend class CacheStorageCacheTest;
184 191
185 struct OpenAllEntriesContext; 192 struct OpenAllEntriesContext;
186 struct PutContext; 193 struct PutContext;
187 struct QueryCacheContext; 194 struct QueryCacheContext;
188 struct QueryCacheResult; 195 struct QueryCacheResult;
189 196
190 using QueryCacheResults = std::vector<QueryCacheResult>; 197 using QueryCacheResults = std::vector<QueryCacheResult>;
191 using QueryCacheCallback = 198 using QueryCacheCallback =
192 base::Callback<void(CacheStorageError, 199 base::Callback<void(CacheStorageError,
193 std::unique_ptr<QueryCacheResults>)>; 200 std::unique_ptr<QueryCacheResults>)>;
194 using Entries = std::vector<disk_cache::Entry*>; 201 using Entries = std::vector<disk_cache::Entry*>;
195 using ScopedBackendPtr = std::unique_ptr<disk_cache::Backend>; 202 using ScopedBackendPtr = std::unique_ptr<disk_cache::Backend>;
196 using BlobToDiskCacheIDMap = 203 using BlobToDiskCacheIDMap =
197 IDMap<std::unique_ptr<CacheStorageBlobToDiskCache>>; 204 IDMap<std::unique_ptr<CacheStorageBlobToDiskCache>>;
198 using OpenAllEntriesCallback = 205 using OpenAllEntriesCallback =
199 base::Callback<void(std::unique_ptr<OpenAllEntriesContext>, 206 base::Callback<void(std::unique_ptr<OpenAllEntriesContext>,
200 CacheStorageError)>; 207 CacheStorageError)>;
208 using SizePaddingCallback =
209 base::Callback<void(int64_t cache_size, int64_t cache_padding)>;
201 210
202 CacheStorageCache( 211 CacheStorageCache(
203 const GURL& origin, 212 const GURL& origin,
204 const std::string& cache_name, 213 const std::string& cache_name,
205 const base::FilePath& path, 214 const base::FilePath& path,
206 CacheStorage* cache_storage, 215 CacheStorage* cache_storage,
207 scoped_refptr<net::URLRequestContextGetter> request_context_getter, 216 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
208 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy, 217 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
209 base::WeakPtr<storage::BlobStorageContext> blob_context, 218 base::WeakPtr<storage::BlobStorageContext> blob_context,
210 int64_t cache_size); 219 int64_t cache_size);
211 220
212 // Returns all entries in this cache. 221 // Returns all entries in this cache.
213 void OpenAllEntries(const OpenAllEntriesCallback& callback); 222 void OpenAllEntries(const OpenAllEntriesCallback& callback);
214 void DidOpenNextEntry(std::unique_ptr<OpenAllEntriesContext> entries_context, 223 void DidOpenNextEntry(std::unique_ptr<OpenAllEntriesContext> entries_context,
215 const OpenAllEntriesCallback& callback, 224 const OpenAllEntriesCallback& callback,
216 int rv); 225 int rv);
217 226
218 // Runs |callback| with matching requests/response data. The data provided 227 // Runs |callback| with matching requests/response data. The data provided
219 // in the QueryCacheResults depends on the |query_type|. If |query_type| is 228 // in the QueryCacheResults depends on the |query_type|. If |query_type| is
220 // CACHE_ENTRIES then only out_entries is valid. If |query_type| is REQUESTS 229 // CACHE_ENTRIES then only out_entries is valid. If |query_type| is REQUESTS
221 // then only out_requests is valid. If |query_type| is 230 // then only out_requests is valid. If |query_type| is
222 // REQUESTS_AND_RESPONSES then only out_requests, out_responses, and 231 // REQUESTS_AND_RESPONSES then only out_requests, out_responses, and
223 // out_blob_data_handles are valid. 232 // out_blob_data_handles are valid.
224 void QueryCache(std::unique_ptr<ServiceWorkerFetchRequest> request, 233 void QueryCache(std::unique_ptr<ServiceWorkerFetchRequest> request,
225 const CacheStorageCacheQueryParams& options, 234 const CacheStorageCacheQueryParams& options,
226 QueryCacheType query_type, 235 uint32_t query_types,
227 const QueryCacheCallback& callback); 236 const QueryCacheCallback& callback);
228 void QueryCacheDidOpenFastPath( 237 void QueryCacheDidOpenFastPath(
229 std::unique_ptr<QueryCacheContext> query_cache_context, 238 std::unique_ptr<QueryCacheContext> query_cache_context,
230 int rv); 239 int rv);
231 void QueryCacheOpenNextEntry( 240 void QueryCacheOpenNextEntry(
232 std::unique_ptr<QueryCacheContext> query_cache_context); 241 std::unique_ptr<QueryCacheContext> query_cache_context);
233 void QueryCacheFilterEntry( 242 void QueryCacheFilterEntry(
234 std::unique_ptr<QueryCacheContext> query_cache_context, 243 std::unique_ptr<QueryCacheContext> query_cache_context,
235 int rv); 244 int rv);
236 void QueryCacheDidReadMetadata( 245 void QueryCacheDidReadMetadata(
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 disk_cache::ScopedEntryPtr entry, 307 disk_cache::ScopedEntryPtr entry,
299 int expected_bytes, 308 int expected_bytes,
300 int rv); 309 int rv);
301 310
302 // Puts the request and response object in the cache. The response body (if 311 // Puts the request and response object in the cache. The response body (if
303 // present) is stored in the cache, but not the request body. Returns OK on 312 // present) is stored in the cache, but not the request body. Returns OK on
304 // success. 313 // success.
305 void Put(const CacheStorageBatchOperation& operation, 314 void Put(const CacheStorageBatchOperation& operation,
306 const ErrorCallback& callback); 315 const ErrorCallback& callback);
307 void PutImpl(std::unique_ptr<PutContext> put_context); 316 void PutImpl(std::unique_ptr<PutContext> put_context);
308 void PutDidDoomEntry(std::unique_ptr<PutContext> put_context, int rv); 317 void PutDidDeleteEntry(std::unique_ptr<PutContext> put_context,
318 CacheStorageError error,
319 std::unique_ptr<QueryCacheResults> query_results);
309 void PutDidGetUsageAndQuota(std::unique_ptr<PutContext> put_context, 320 void PutDidGetUsageAndQuota(std::unique_ptr<PutContext> put_context,
310 storage::QuotaStatusCode status_code, 321 storage::QuotaStatusCode status_code,
311 int64_t usage, 322 int64_t usage,
312 int64_t quota); 323 int64_t quota);
313 void PutDidCreateEntry(std::unique_ptr<disk_cache::Entry*> entry_ptr, 324 void PutDidCreateEntry(std::unique_ptr<disk_cache::Entry*> entry_ptr,
314 std::unique_ptr<PutContext> put_context, 325 std::unique_ptr<PutContext> put_context,
315 int rv); 326 int rv);
316 void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context, 327 void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context,
317 int expected_bytes, 328 int expected_bytes,
318 int rv); 329 int rv);
319 void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context, 330 void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context,
320 BlobToDiskCacheIDMap::KeyType blob_to_cache_key, 331 BlobToDiskCacheIDMap::KeyType blob_to_cache_key,
321 disk_cache::ScopedEntryPtr entry, 332 disk_cache::ScopedEntryPtr entry,
322 bool success); 333 bool success);
323 334
324 // Asynchronously calculates the current cache size, notifies the quota 335 // Asynchronously calculates the current cache size, notifies the quota
325 // manager of any change from the last report, and sets cache_size_ to the new 336 // manager of any change from the last report, and sets cache_size_ to the new
326 // size. 337 // size.
327 void UpdateCacheSize(const base::Closure& callback); 338 void UpdateCacheSize(const base::Closure& callback);
328 void UpdateCacheSizeGotSize(std::unique_ptr<CacheStorageCacheHandle>, 339 void UpdateCacheSizeGotSize(std::unique_ptr<CacheStorageCacheHandle>,
329 const base::Closure& callback, 340 const base::Closure& callback,
330 int current_cache_size); 341 int current_cache_size);
331 342
332 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK. 343 // Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK.
333 void Delete(const CacheStorageBatchOperation& operation, 344 void Delete(const CacheStorageBatchOperation& operation,
334 const ErrorCallback& callback); 345 const ErrorCallback& callback);
335 void DeleteImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, 346 void DeleteImpl(std::unique_ptr<ServiceWorkerFetchRequest> request,
336 const CacheStorageCacheQueryParams& match_params, 347 const CacheStorageCacheQueryParams& match_params,
337 const ErrorCallback& callback); 348 const QueryCacheCallback& callback);
338 void DeleteDidQueryCache( 349 void DeleteDidQueryCache(
339 const ErrorCallback& callback, 350 const QueryCacheCallback& callback,
340 CacheStorageError error, 351 CacheStorageError error,
341 std::unique_ptr<QueryCacheResults> query_cache_results); 352 std::unique_ptr<QueryCacheResults> query_cache_results);
353 void DeleteDidDelete(const ErrorCallback& callback,
354 CacheStorageError error,
355 std::unique_ptr<QueryCacheResults> query_results);
342 356
343 // Keys callbacks. 357 // Keys callbacks.
344 void KeysImpl(std::unique_ptr<ServiceWorkerFetchRequest> request, 358 void KeysImpl(std::unique_ptr<ServiceWorkerFetchRequest> request,
345 const CacheStorageCacheQueryParams& options, 359 const CacheStorageCacheQueryParams& options,
346 const RequestsCallback& callback); 360 const RequestsCallback& callback);
347 void KeysDidQueryCache( 361 void KeysDidQueryCache(
348 const RequestsCallback& callback, 362 const RequestsCallback& callback,
349 CacheStorageError error, 363 CacheStorageError error,
350 std::unique_ptr<QueryCacheResults> query_cache_results); 364 std::unique_ptr<QueryCacheResults> query_cache_results);
351 365
352 void CloseImpl(const base::Closure& callback); 366 void CloseImpl(const base::Closure& callback);
353 367
354 void SizeImpl(const SizeCallback& callback); 368 void SizeImpl(const SizeCallback& callback);
355 369
356 void GetSizeThenCloseDidGetSize(const SizeCallback& callback, 370 void GetSizeThenCloseDidGetSize(const SizeCallback& callback,
357 int64_t cache_size); 371 int64_t cache_size);
358 372
359 // Loads the backend and calls the callback with the result (true for 373 // Loads the backend and calls the callback with the result (true for
360 // success). The callback will always be called. Virtual for tests. 374 // success). The callback will always be called. Virtual for tests.
361 virtual void CreateBackend(const ErrorCallback& callback); 375 virtual void CreateBackend(const ErrorCallback& callback);
362 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback, 376 void CreateBackendDidCreate(const CacheStorageCache::ErrorCallback& callback,
363 std::unique_ptr<ScopedBackendPtr> backend_ptr, 377 std::unique_ptr<ScopedBackendPtr> backend_ptr,
364 int rv); 378 int rv);
365 379
380 // Calculate the size and padding of the cache.
381 void CalculateCacheSizePadding(const SizePaddingCallback& callback);
382 void CalculateCacheSizePaddingGotSize(const SizePaddingCallback& callback,
383 int cache_size);
384 void PaddingDidQueryCache(
385 const SizePaddingCallback& callback,
386 int cache_size,
387 CacheStorageError error,
388 std::unique_ptr<QueryCacheResults> query_cache_results);
389
390 // Calculate the size (but not padding) of the cache.
391 void CalculateCacheSize(const net::CompletionCallback& callback);
392
366 void InitBackend(); 393 void InitBackend();
367 void InitDidCreateBackend(const base::Closure& callback, 394 void InitDidCreateBackend(const base::Closure& callback,
368 CacheStorageError cache_create_error); 395 CacheStorageError cache_create_error);
369 void InitGotCacheSize(const base::Closure& callback, 396 void InitGotCacheSize(const base::Closure& callback,
370 CacheStorageError cache_create_error, 397 CacheStorageError cache_create_error,
371 int cache_size); 398 int64_t cache_size,
399 int64_t cache_padding);
372 400
373 std::unique_ptr<storage::BlobDataHandle> PopulateResponseBody( 401 std::unique_ptr<storage::BlobDataHandle> PopulateResponseBody(
374 disk_cache::ScopedEntryPtr entry, 402 disk_cache::ScopedEntryPtr entry,
375 ServiceWorkerResponse* response); 403 ServiceWorkerResponse* response);
376 404
377 // Virtual for testing. 405 // Virtual for testing.
378 virtual std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle(); 406 virtual std::unique_ptr<CacheStorageCacheHandle> CreateCacheHandle();
379 407
380 // Be sure to check |backend_state_| before use. 408 // Be sure to check |backend_state_| before use.
381 std::unique_ptr<disk_cache::Backend> backend_; 409 std::unique_ptr<disk_cache::Backend> backend_;
382 410
383 GURL origin_; 411 GURL origin_;
384 const std::string cache_name_; 412 const std::string cache_name_;
385 base::FilePath path_; 413 base::FilePath path_;
386 414
387 // Raw pointer is safe because CacheStorage owns this object. 415 // Raw pointer is safe because CacheStorage owns this object.
388 CacheStorage* cache_storage_; 416 CacheStorage* cache_storage_;
389 417
390 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 418 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
391 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; 419 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
392 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_; 420 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
393 BackendState backend_state_ = BACKEND_UNINITIALIZED; 421 BackendState backend_state_ = BACKEND_UNINITIALIZED;
394 std::unique_ptr<CacheStorageScheduler> scheduler_; 422 std::unique_ptr<CacheStorageScheduler> scheduler_;
395 bool initializing_ = false; 423 bool initializing_ = false;
396 int64_t cache_size_; 424 int64_t cache_size_;
425 int64_t cache_padding_ = 0;
426 int64_t last_reported_size_ = 0;
397 size_t max_query_size_bytes_; 427 size_t max_query_size_bytes_;
398 CacheStorageCacheObserver* cache_observer_; 428 CacheStorageCacheObserver* cache_observer_;
399 429
400 // Owns the elements of the list 430 // Owns the elements of the list
401 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_; 431 BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_;
402 432
403 // Whether or not to store data in disk or memory. 433 // Whether or not to store data in disk or memory.
404 bool memory_only_; 434 bool memory_only_;
405 435
406 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_; 436 base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_;
407 437
408 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache); 438 DISALLOW_COPY_AND_ASSIGN(CacheStorageCache);
409 }; 439 };
410 440
411 } // namespace content 441 } // namespace content
412 442
413 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_ 443 #endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698