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

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

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

Powered by Google App Engine
This is Rietveld 408576698