1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
#define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/containers/id_map.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "components/services/storage/public/cpp/quota_error_or.h"
#include "components/services/storage/public/mojom/cache_storage_control.mojom.h"
#include "content/browser/cache_storage/blob_storage_context_wrapper.h"
#include "content/browser/cache_storage/cache_storage_cache_handle.h"
#include "content/browser/cache_storage/cache_storage_handle.h"
#include "content/browser/cache_storage/cache_storage_scheduler_types.h"
#include "content/browser/cache_storage/scoped_writable_entry.h"
#include "content/common/content_export.h"
#include "net/base/completion_once_callback.h"
#include "net/base/io_buffer.h"
#include "net/disk_cache/disk_cache.h"
#include "third_party/blink/public/mojom/cache_storage/cache_storage.mojom.h"
#include "third_party/blink/public/mojom/quota/quota_types.mojom.h"
namespace storage {
class QuotaManagerProxy;
} // namespace storage
namespace content {
class CacheStorageBlobToDiskCache;
class CacheStorageCacheEntryHandler;
class CacheStorageCacheObserver;
class CacheStorageScheduler;
class CacheStorage;
struct PutContext;
namespace proto {
class CacheMetadata;
} // namespace proto
namespace cache_storage_cache_unittest {
class TestCacheStorageCache;
class CacheStorageCacheTest;
} // namespace cache_storage_cache_unittest
// Represents a ServiceWorker Cache as seen in:
//
// https://w3c.github.io/ServiceWorker/#cache-interface
//
// The asynchronous methods are executed serially. Callbacks to the public
// functions will be called so long as the cache object lives. Client code must
// hold a `CacheStorageCacheHandle` for the duration of operations, otherwise
// the operation may be cancelled in some circumstances.
class CONTENT_EXPORT CacheStorageCache {
public:
using CacheEntriesCallback =
base::OnceCallback<void(blink::mojom::CacheStorageError,
std::vector<blink::mojom::CacheEntryPtr>)>;
using ErrorCallback =
base::OnceCallback<void(blink::mojom::CacheStorageError)>;
using VerboseErrorCallback =
base::OnceCallback<void(blink::mojom::CacheStorageVerboseErrorPtr)>;
using BadMessageCallback = base::OnceCallback<void()>;
using ResponseCallback =
base::OnceCallback<void(blink::mojom::CacheStorageError,
blink::mojom::FetchAPIResponsePtr)>;
using ResponsesCallback =
base::OnceCallback<void(blink::mojom::CacheStorageError,
std::vector<blink::mojom::FetchAPIResponsePtr>)>;
using Requests = std::vector<blink::mojom::FetchAPIRequestPtr>;
using RequestsCallback =
base::OnceCallback<void(blink::mojom::CacheStorageError,
std::unique_ptr<Requests>)>;
using SizeCallback = base::OnceCallback<void(int64_t)>;
using SizePaddingCallback = base::OnceCallback<void(int64_t, int64_t)>;
// The stream index for a cache Entry. This cannot be extended without changes
// in the Entry implementation. INDEX_SIDE_DATA is used for storing any
// additional data, such as response side blobs or request bodies.
enum EntryIndex {
INDEX_INVALID = -1,
INDEX_HEADERS = 0,
INDEX_RESPONSE_BODY,
INDEX_SIDE_DATA
};
static std::unique_ptr<CacheStorageCache> CreateMemoryCache(
const storage::BucketLocator& bucket_locator,
storage::mojom::CacheStorageOwner owner,
const std::u16string& cache_name,
CacheStorage* cache_storage,
scoped_refptr<base::SequencedTaskRunner> scheduler_task_runner,
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
scoped_refptr<BlobStorageContextWrapper> blob_storage_context);
static std::unique_ptr<CacheStorageCache> CreatePersistentCache(
const storage::BucketLocator& bucket_locator,
storage::mojom::CacheStorageOwner owner,
const std::u16string& cache_name,
CacheStorage* cache_storage,
const base::FilePath& path,
scoped_refptr<base::SequencedTaskRunner> scheduler_task_runner,
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
scoped_refptr<BlobStorageContextWrapper> blob_storage_context,
int64_t cache_size,
int64_t cache_padding);
static int32_t GetResponsePaddingVersion();
// Returns ERROR_TYPE_NOT_FOUND if not found.
void Match(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr match_options,
CacheStorageSchedulerPriority priority,
int64_t trace_id,
ResponseCallback callback);
// Returns blink::mojom::CacheStorageError::kSuccess and matched
// responses in this cache. If there are no responses, returns
// blink::mojom::CacheStorageError::kSuccess and an empty vector.
void MatchAll(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr match_options,
int64_t trace_id,
ResponsesCallback callback);
// Writes the side data (ex: V8 code cache) for the specified cache entry.
// If it doesn't exist, or the |expected_response_time| differs from the
// entry's, blink::mojom::CacheStorageError::kErrorNotFound is returned.
// Note: This "side data" is same meaning as "metadata" in HTTPCache. We use
// "metadata" in cache_storage.proto for the pair of headers of a request and
// a response. To avoid the confusion we use "side data" here.
void WriteSideData(ErrorCallback callback,
const GURL& url,
base::Time expected_response_time,
int64_t trace_id,
scoped_refptr<net::IOBuffer> buffer,
int buf_len);
// Runs given batch operations. This corresponds to the Batch Cache Operations
// algorithm in the spec.
//
// |operations| cannot mix PUT and DELETE operations and cannot contain
// multiple DELETE operations.
//
// In the case of the PUT operation, puts request and response objects in the
// cache and returns OK when all operations are successfully completed.
// In the case of the DELETE operation, returns ERROR_NOT_FOUND if a specified
// entry is not found. Otherwise deletes it and returns OK.
//
// TODO(nhiroki): This function should run all operations atomically.
// http://crbug.com/486637
void BatchOperation(std::vector<blink::mojom::BatchOperationPtr> operations,
int64_t trace_id,
VerboseErrorCallback callback,
BadMessageCallback bad_message_callback);
void BatchDidGetBucketSpaceRemaining(
std::vector<blink::mojom::BatchOperationPtr> operations,
int64_t trace_id,
VerboseErrorCallback callback,
BadMessageCallback bad_message_callback,
std::optional<std::string> message,
uint64_t space_required,
uint64_t side_data_size,
storage::QuotaErrorOr<int64_t> space_remaining);
// Returns blink::mojom::CacheStorageError::kSuccess and a vector of
// requests if there are no errors.
void Keys(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr options,
int64_t trace_id,
RequestsCallback callback);
// Closes the backend. Future operations that require the backend
// will exit early. Close should only be called once per CacheStorageCache.
void Close(base::OnceClosure callback);
// The size of the cache's contents. The callback reports the padded
// size. If you want the unpadded size you may call the cache_size()
// getter method on the cache object when the callback is invoked; the
// getter will have an up-to-date value at that point.
void Size(SizeCallback callback);
// Gets the cache's size, closes the backend, and then runs |callback| with
// the cache's size. As per the comment for Size(), this also returns the
// padded size.
void GetSizeThenClose(SizeCallback callback);
// Puts the request/response pair in the cache. This is a public member to
// directly bypass the batch operations and write into the cache. This is used
// by non-CacheAPI owners. The Cache Storage API uses batch operations defined
// in the dispatcher.
void Put(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::FetchAPIResponsePtr response,
int64_t trace_id,
ErrorCallback callback);
// Similar to MatchAll, but returns the associated requests as well.
void GetAllMatchedEntries(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr match_options,
int64_t trace_id,
CacheEntriesCallback callback);
// Try to determine the initialization state of the cache. Unknown may be
// returned for cross-sequence clients using the cross-sequence wrappers.
enum class InitState { Unknown, Initializing, Initialized };
InitState GetInitState() const;
CacheStorageCache(const CacheStorageCache&) = delete;
CacheStorageCache& operator=(const CacheStorageCache&) = delete;
// Async operations in progress will cancel and not run their callbacks.
virtual ~CacheStorageCache();
base::FilePath path() const { return path_; }
std::u16string cache_name() const { return cache_name_; }
int64_t cache_size() const { return cache_size_; }
int64_t cache_padding() const { return cache_padding_; }
// Return the total cache size (actual size + padding). If either is unknown
// then CacheStorage::kSizeUnknown is returned.
int64_t PaddedCacheSize() const;
// Set the one observer that will be notified of changes to this cache.
// Note: Either the observer must have a lifetime longer than this instance
// or call SetObserver(nullptr) to stop receiving notification of changes.
void SetObserver(CacheStorageCacheObserver* observer);
static size_t EstimatedStructSize(
const blink::mojom::FetchAPIRequestPtr& request);
base::WeakPtr<CacheStorageCache> AsWeakPtr();
// Create a handle that will hold the CacheStorageCache alive. Client code
// should hold one of these handles while waiting for operation callbacks to
// be invoked.
//
// Note, its still possible for the CacheStorageCache to be deleted even if
// there are outstanding handle references. This can occur when the user
// triggers a storage wipe, for example. The handle value should be treated
// as a weak pointer.
CacheStorageCacheHandle CreateHandle();
void AddHandleRef();
void DropHandleRef();
bool IsUnreferenced() const;
// the default scheduler with a customized scheduler for testing.
// The current scheduler must be idle.
void SetSchedulerForTesting(std::unique_ptr<CacheStorageScheduler> scheduler);
static CacheStorageCache* From(const CacheStorageCacheHandle& handle) {
return static_cast<CacheStorageCache*>(handle.value());
}
private:
// QueryCache types:
enum QueryCacheFlags {
QUERY_CACHE_REQUESTS = 0x1,
QUERY_CACHE_RESPONSES_WITH_BODIES = 0x2,
QUERY_CACHE_RESPONSES_NO_BODIES = 0x4,
QUERY_CACHE_ENTRIES = 0x8,
};
// The backend progresses from uninitialized, to open, to closed, and cannot
// reverse direction. The open step may be skipped.
enum BackendState {
BACKEND_UNINITIALIZED, // No backend, create backend on first operation.
BACKEND_OPEN, // Backend can be used.
BACKEND_CLOSED // Backend cannot be used. All ops should fail.
};
friend class base::RefCounted<CacheStorageCache>;
friend class cache_storage_cache_unittest::TestCacheStorageCache;
friend class cache_storage_cache_unittest::CacheStorageCacheTest;
struct QueryCacheContext;
struct QueryCacheResult;
struct BatchInfo;
using QueryTypes = int32_t;
using QueryCacheResults = std::vector<QueryCacheResult>;
using QueryCacheCallback =
base::OnceCallback<void(blink::mojom::CacheStorageError,
std::unique_ptr<QueryCacheResults>)>;
using Entries = std::vector<disk_cache::Entry*>;
using BlobToDiskCacheIDMap =
base::IDMap<std::unique_ptr<CacheStorageBlobToDiskCache>>;
CacheStorageCache(
const storage::BucketLocator& bucket_locator,
storage::mojom::CacheStorageOwner owner,
const std::u16string& cache_name,
const base::FilePath& path,
CacheStorage* cache_storage,
scoped_refptr<base::SequencedTaskRunner> scheduler_task_runner,
scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy,
scoped_refptr<BlobStorageContextWrapper> blob_storage_context,
int64_t cache_size,
int64_t cache_padding);
// Callback passed to operations. If |error| is a real error, invokes
// |error_callback|. Always invokes |completion_closure| to signal
// completion.
void BatchDidOneOperation(BatchInfo& batch_status,
blink::mojom::CacheStorageError error);
// Runs |callback| with matching requests/response data. The data provided
// in the QueryCacheResults depends on the |query_type|. If |query_type| is
// CACHE_ENTRIES then only out_entries is valid. If |query_type| is REQUESTS
// then only out_requests is valid. If |query_type| is
// REQUESTS_AND_RESPONSES then only out_requests, out_responses, and
// out_blob_data_handles are valid.
void QueryCache(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr options,
QueryTypes query_types,
CacheStorageSchedulerPriority priority,
QueryCacheCallback callback);
void QueryCacheDidOpenFastPath(
std::unique_ptr<QueryCacheContext> query_cache_context,
disk_cache::EntryResult result);
void QueryCacheOpenNextEntry(
std::unique_ptr<QueryCacheContext> query_cache_context);
void QueryCacheFilterEntry(
std::unique_ptr<QueryCacheContext> query_cache_context,
disk_cache::EntryResult result);
void QueryCacheDidReadMetadata(
std::unique_ptr<QueryCacheContext> query_cache_context,
disk_cache::ScopedEntryPtr entry,
std::unique_ptr<proto::CacheMetadata> metadata);
void QueryCacheUpgradePadding(
std::unique_ptr<QueryCacheContext> query_cache_context,
disk_cache::ScopedEntryPtr entry,
std::unique_ptr<proto::CacheMetadata> metadata);
static bool QueryCacheResultCompare(const QueryCacheResult& lhs,
const QueryCacheResult& rhs);
static size_t EstimatedResponseSizeWithoutBlob(
const blink::mojom::FetchAPIResponse& response);
// Match callbacks
void MatchImpl(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr match_options,
int64_t trace_id,
CacheStorageSchedulerPriority priority,
ResponseCallback callback);
void MatchDidMatchAll(
ResponseCallback callback,
blink::mojom::CacheStorageError match_all_error,
std::vector<blink::mojom::FetchAPIResponsePtr> match_all_responses);
// MatchAll callbacks
void MatchAllImpl(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr options,
int64_t trace_id,
CacheStorageSchedulerPriority priority,
ResponsesCallback callback);
void MatchAllDidQueryCache(
ResponsesCallback callback,
int64_t trace_id,
blink::mojom::CacheStorageError error,
std::unique_ptr<QueryCacheResults> query_cache_results);
// Utility method to write metadata headers to an entry.
using WriteMetadataCallback =
base::OnceCallback<void(int exepected_bytes, int rv)>;
void WriteMetadata(disk_cache::Entry* entry,
const proto::CacheMetadata& metadata,
WriteMetadataCallback callback);
// WriteSideData callbacks
void WriteSideDataDidGetBucketSpaceRemaining(
ErrorCallback callback,
const GURL& url,
base::Time expected_response_time,
int64_t trace_id,
scoped_refptr<net::IOBuffer> buffer,
int buf_len,
storage::QuotaErrorOr<int64_t> space_remaining);
void WriteSideDataImpl(ErrorCallback callback,
const GURL& url,
base::Time expected_response_time,
int64_t trace_id,
scoped_refptr<net::IOBuffer> buffer,
int buf_len);
void WriteSideDataDidGetUsageAndQuota(
ErrorCallback callback,
const GURL& url,
base::Time expected_response_time,
int64_t trace_id,
scoped_refptr<net::IOBuffer> buffer,
int buf_len,
blink::mojom::QuotaStatusCode status_code,
int64_t usage,
int64_t quota);
void WriteSideDataDidOpenEntry(ErrorCallback callback,
base::Time expected_response_time,
int64_t trace_id,
scoped_refptr<net::IOBuffer> buffer,
int buf_len,
disk_cache::EntryResult result);
void WriteSideDataDidReadMetaData(
ErrorCallback callback,
base::Time expected_response_time,
int64_t trace_id,
scoped_refptr<net::IOBuffer> buffer,
int buf_len,
ScopedWritableEntry entry,
std::unique_ptr<proto::CacheMetadata> headers);
void WriteSideDataDidWrite(
ErrorCallback callback,
ScopedWritableEntry entry,
int expected_bytes,
std::unique_ptr<content::proto::CacheMetadata> metadata,
int64_t trace_id,
int rv);
void WriteSideDataDidWriteMetadata(ErrorCallback callback,
ScopedWritableEntry entry,
int64_t padding,
int64_t side_data_padding,
int expected_bytes,
int rv);
void WriteSideDataComplete(ErrorCallback callback,
ScopedWritableEntry entry,
int64_t padding,
int64_t side_data_padding,
blink::mojom::CacheStorageError error);
// Puts the request and response object in the cache. The response body (if
// present) is stored in the cache, but not the request body. Returns OK on
// success.
void Put(blink::mojom::BatchOperationPtr operation,
int64_t trace_id,
ErrorCallback callback);
void PutImpl(std::unique_ptr<PutContext> put_context);
void PutDidDeleteEntry(std::unique_ptr<PutContext> put_context,
blink::mojom::CacheStorageError error);
void PutDidGetUsageAndQuota(std::unique_ptr<PutContext> put_context,
blink::mojom::QuotaStatusCode status_code,
int64_t usage,
int64_t quota);
void PutDidCreateEntry(std::unique_ptr<PutContext> put_context,
disk_cache::EntryResult result);
void PutDidWriteHeaders(std::unique_ptr<PutContext> put_context,
int64_t padding,
int64_t side_data_padding,
int expected_bytes,
int rv);
void PutWriteBlobToCache(std::unique_ptr<PutContext> put_context,
int disk_cache_body_index);
void PutDidWriteBlobToCache(std::unique_ptr<PutContext> put_context,
BlobToDiskCacheIDMap::KeyType blob_to_cache_key,
int disk_cache_body_index,
ScopedWritableEntry entry,
bool success);
void PutWriteBlobToCacheComplete(std::unique_ptr<PutContext> put_context,
int disk_cache_body_index,
ScopedWritableEntry entry,
int rv);
void PutComplete(std::unique_ptr<PutContext> put_context,
blink::mojom::CacheStorageError error);
// Asynchronously calculates the current cache size, notifies the quota
// manager of any change from the last report, and sets cache_size_ to the new
// size.
void UpdateCacheSize(base::OnceClosure callback);
void UpdateCacheSizeGotSize(CacheStorageCacheHandle,
base::OnceClosure callback,
int64_t current_cache_size);
void UpdateCacheSizeNotifiedStorageModified(base::OnceClosure callback);
// GetAllMatchedEntries callbacks.
void GetAllMatchedEntriesImpl(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr options,
int64_t trace_id,
CacheEntriesCallback callback);
void GetAllMatchedEntriesDidQueryCache(
int64_t trace_id,
CacheEntriesCallback callback,
blink::mojom::CacheStorageError error,
std::unique_ptr<QueryCacheResults> query_cache_results);
// Returns ERROR_NOT_FOUND if not found. Otherwise deletes and returns OK.
void Delete(blink::mojom::BatchOperationPtr operation,
ErrorCallback callback);
void DeleteImpl(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr match_options,
ErrorCallback callback);
void DeleteDidQueryCache(
ErrorCallback callback,
blink::mojom::CacheStorageError error,
std::unique_ptr<QueryCacheResults> query_cache_results);
// Keys callbacks.
void KeysImpl(blink::mojom::FetchAPIRequestPtr request,
blink::mojom::CacheQueryOptionsPtr options,
int64_t trace_id,
RequestsCallback callback);
void KeysDidQueryCache(
RequestsCallback callback,
int64_t trace_id,
blink::mojom::CacheStorageError error,
std::unique_ptr<QueryCacheResults> query_cache_results);
void CloseImpl(base::OnceClosure callback);
void SizeImpl(SizeCallback callback);
void GetSizeThenCloseDidGetSize(SizeCallback callback, int64_t cache_size);
// Loads the backend and calls the callback with the result (true for
// success). The callback will always be called. Virtual for tests.
virtual void CreateBackend(ErrorCallback callback);
void CreateBackendDidCreate(ErrorCallback callback,
disk_cache::BackendResult result);
// Calculate the size and padding of the cache.
void CalculateCacheSizePadding(SizePaddingCallback callback);
void CalculateCacheSizePaddingGotSize(SizePaddingCallback callback,
int64_t cache_size);
void PaddingDidQueryCache(
SizePaddingCallback callback,
int64_t cache_size,
blink::mojom::CacheStorageError error,
std::unique_ptr<QueryCacheResults> query_cache_results);
// Calculate the size (but not padding) of the cache.
void CalculateCacheSize(net::Int64CompletionOnceCallback callback);
void InitBackend();
void InitDidCreateBackend(base::OnceClosure callback,
blink::mojom::CacheStorageError cache_create_error);
void InitGotCacheSize(base::OnceClosure callback,
blink::mojom::CacheStorageError cache_create_error,
int64_t cache_size);
void InitGotCacheSizeAndPadding(
base::OnceClosure callback,
blink::mojom::CacheStorageError cache_create_error,
int64_t cache_size,
int64_t cache_padding);
void DeleteBackendCompletedIO();
// Calculate the required safe space to put the entry in the cache.
base::CheckedNumeric<uint64_t> CalculateRequiredSafeSpaceForPut(
const blink::mojom::BatchOperationPtr& operation);
base::CheckedNumeric<uint64_t> CalculateRequiredSafeSpaceForRequest(
const blink::mojom::FetchAPIRequestPtr& request);
base::CheckedNumeric<uint64_t> CalculateRequiredSafeSpaceForResponse(
const blink::mojom::FetchAPIResponsePtr& response);
// Wrap |callback| in order to reference a CacheStorageCacheHandle
// for the duration of an asynchronous operation. We must keep this
// self reference for a couple reasons. First, we must allow any writes
// to cleanly complete in order to avoid truncated entries. In addition,
// we must keep the cache and its disk_cache backend alive until all
// open Entry objects are destroyed to avoid having a second backend
// opened by another CacheStorageCache clobbering the entries.
template <typename... Args>
base::OnceCallback<void(Args...)> WrapCallbackWithHandle(
base::OnceCallback<void(Args...)> callback) {
return base::BindOnce(&CacheStorageCache::RunWithHandle<Args...>,
weak_ptr_factory_.GetWeakPtr(), CreateHandle(),
std::move(callback));
}
// Invoked by wrapped callbacks with the CacheStorageCacheHandle passed
// as a parameter. The handle is kept alive here simply to maintain
// a self-reference during the operation.
template <typename... Args>
void RunWithHandle(CacheStorageCacheHandle handle,
base::OnceCallback<void(Args...)> callback,
Args... args) {
std::move(callback).Run(std::forward<Args>(args)...);
// |handle| is destroyed after running the inner wrapped callback.
}
// Be sure to check |backend_state_| before use.
std::unique_ptr<disk_cache::Backend> backend_;
const storage::BucketLocator bucket_locator_;
const storage::mojom::CacheStorageOwner owner_;
const std::u16string cache_name_;
const base::FilePath path_;
// Raw pointer is safe because the CacheStorage instance owns this
// CacheStorageCache object.
const raw_ptr<CacheStorage> cache_storage_;
// A handle that is used to keep the owning CacheStorage instance referenced
// as long this cache object is also referenced.
CacheStorageHandle cache_storage_handle_;
const scoped_refptr<base::SequencedTaskRunner> scheduler_task_runner_;
const scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
BackendState backend_state_ = BACKEND_UNINITIALIZED;
std::unique_ptr<CacheStorageScheduler> scheduler_;
bool initializing_ = false;
// The actual cache size (not including padding).
int64_t cache_size_;
int64_t cache_padding_ = 0;
int64_t last_reported_size_ = 0;
size_t max_query_size_bytes_;
size_t handle_ref_count_ = 0;
int query_cache_recursive_depth_ = 0;
raw_ptr<CacheStorageCacheObserver> cache_observer_;
std::unique_ptr<CacheStorageCacheEntryHandler> cache_entry_handler_;
// Owns the elements of the list
BlobToDiskCacheIDMap active_blob_to_disk_cache_writers_;
// Whether or not to store data in disk or memory.
const bool memory_only_;
// Active while waiting for the backend to finish its closing up, and contains
// the callback passed to CloseImpl.
base::OnceClosure post_backend_closed_callback_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<CacheStorageCache> weak_ptr_factory_{this};
};
} // namespace content
#endif // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_CACHE_H_
|