#include "components/services/storage/service_worker/service_worker_storage.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "components/services/storage/public/cpp/constants.h"
#include "components/services/storage/service_worker/service_worker_disk_cache.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_errors.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/service_worker/service_worker_scope_match.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
namespace storage {
namespace {
void RunSoon(const base::Location& from_here, base::OnceClosure closure) { … }
const base::FilePath::CharType kDatabaseName[] = …);
const base::FilePath::CharType kDiskCacheName[] = …);
enum class DeleteAndStartOverResult { … };
void RecordDeleteAndStartOverResult(DeleteAndStartOverResult result) { … }
bool IsServiceWorkerStorageSuppressPostTaskEnabled() { … }
void MaybePostTask(scoped_refptr<base::SequencedTaskRunner> task_runner,
const base::Location& from_here,
base::OnceClosure task) { … }
template <class Task, class Reply>
void MaybePostTaskAndReplyWithResult(
scoped_refptr<base::SequencedTaskRunner> task_runner,
const base::Location& from_here,
Task task,
Reply reply) { … }
void MaybePostTaskAndReply(scoped_refptr<base::SequencedTaskRunner> task_runner,
const base::Location& from_here,
base::OnceClosure task,
base::OnceClosure reply) { … }
}
BASE_FEATURE(…);
const base::FeatureParam<int> kServiceWorkerScopeCacheLimitPerKey{ … };
ServiceWorkerStorage::InitialData::InitialData()
: … { … }
ServiceWorkerStorage::InitialData::~InitialData() = default;
ServiceWorkerStorage::DidDeleteRegistrationParams::DidDeleteRegistrationParams(
int64_t registration_id,
const blink::StorageKey& key,
DeleteRegistrationCallback callback)
: … { … }
ServiceWorkerStorage::DidDeleteRegistrationParams::
~DidDeleteRegistrationParams() { … }
ServiceWorkerStorage::~ServiceWorkerStorage() { … }
std::unique_ptr<ServiceWorkerStorage> ServiceWorkerStorage::Create(
const base::FilePath& user_data_directory,
scoped_refptr<base::SequencedTaskRunner> database_task_runner) { … }
void ServiceWorkerStorage::GetRegisteredStorageKeys(
GetRegisteredStorageKeysCallback callback) { … }
void ServiceWorkerStorage::FindRegistrationForClientUrl(
const GURL& client_url,
const blink::StorageKey& key,
FindRegistrationForClientUrlDataCallback callback) { … }
void ServiceWorkerStorage::FindRegistrationForScope(
const GURL& scope,
const blink::StorageKey& key,
FindRegistrationDataCallback callback) { … }
void ServiceWorkerStorage::FindRegistrationForId(
int64_t registration_id,
const blink::StorageKey& key,
FindRegistrationDataCallback callback) { … }
void ServiceWorkerStorage::FindRegistrationForIdOnly(
int64_t registration_id,
FindRegistrationDataCallback callback) { … }
void ServiceWorkerStorage::GetRegistrationsForStorageKey(
const blink::StorageKey& key,
GetRegistrationsDataCallback callback) { … }
void ServiceWorkerStorage::GetUsageForStorageKey(
const blink::StorageKey& key,
GetUsageForStorageKeyCallback callback) { … }
void ServiceWorkerStorage::GetAllRegistrations(
GetAllRegistrationsCallback callback) { … }
void ServiceWorkerStorage::StoreRegistrationData(
mojom::ServiceWorkerRegistrationDataPtr registration_data,
ResourceList resources,
StoreRegistrationDataCallback callback) { … }
void ServiceWorkerStorage::UpdateToActiveState(
int64_t registration_id,
const blink::StorageKey& key,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::UpdateLastUpdateCheckTime(
int64_t registration_id,
const blink::StorageKey& key,
base::Time last_update_check_time,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::UpdateNavigationPreloadEnabled(
int64_t registration_id,
const blink::StorageKey& key,
bool enable,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::UpdateNavigationPreloadHeader(
int64_t registration_id,
const blink::StorageKey& key,
const std::string& value,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::UpdateFetchHandlerType(
int64_t registration_id,
const blink::StorageKey& key,
blink::mojom::ServiceWorkerFetchHandlerType type,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::UpdateResourceSha256Checksums(
int64_t registration_id,
const blink::StorageKey& key,
const base::flat_map<int64_t, std::string>& updated_sha256_checksums,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::DeleteRegistration(
int64_t registration_id,
const blink::StorageKey& key,
DeleteRegistrationCallback callback) { … }
void ServiceWorkerStorage::PerformStorageCleanup(base::OnceClosure callback) { … }
void ServiceWorkerStorage::CreateResourceReader(
int64_t resource_id,
mojo::PendingReceiver<mojom::ServiceWorkerResourceReader> receiver) { … }
void ServiceWorkerStorage::CreateResourceWriter(
int64_t resource_id,
mojo::PendingReceiver<mojom::ServiceWorkerResourceWriter> receiver) { … }
void ServiceWorkerStorage::CreateResourceMetadataWriter(
int64_t resource_id,
mojo::PendingReceiver<mojom::ServiceWorkerResourceMetadataWriter>
receiver) { … }
void ServiceWorkerStorage::StoreUncommittedResourceId(
int64_t resource_id,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::DoomUncommittedResources(
const std::vector<int64_t>& resource_ids,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::StoreUserData(
int64_t registration_id,
const blink::StorageKey& key,
std::vector<mojom::ServiceWorkerUserDataPtr> user_data,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::GetUserData(int64_t registration_id,
const std::vector<std::string>& keys,
GetUserDataInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserDataInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserKeysAndDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserKeysAndDataInDBCallback callback) { … }
void ServiceWorkerStorage::ClearUserData(int64_t registration_id,
const std::vector<std::string>& keys,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::ClearUserDataByKeyPrefixes(
int64_t registration_id,
const std::vector<std::string>& key_prefixes,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::GetUserDataForAllRegistrations(
const std::string& key,
GetUserDataForAllRegistrationsInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserDataForAllRegistrationsByKeyPrefix(
const std::string& key_prefix,
GetUserDataForAllRegistrationsInDBCallback callback) { … }
void ServiceWorkerStorage::ClearUserDataForAllRegistrationsByKeyPrefix(
const std::string& key_prefix,
DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::DeleteAndStartOver(DatabaseStatusCallback callback) { … }
void ServiceWorkerStorage::DiskCacheImplDoneWithDisk() { … }
void ServiceWorkerStorage::GetNewRegistrationId(
base::OnceCallback<void(int64_t registration_id)> callback) { … }
void ServiceWorkerStorage::GetNewVersionId(
base::OnceCallback<void(int64_t version_id)> callback) { … }
void ServiceWorkerStorage::GetNewResourceId(
base::OnceCallback<void(int64_t resource_id)> callback) { … }
void ServiceWorkerStorage::Disable() { … }
void ServiceWorkerStorage::PurgeResources(
const std::vector<int64_t>& resource_ids) { … }
void ServiceWorkerStorage::ApplyPolicyUpdates(
const std::vector<mojom::StoragePolicyUpdatePtr>& policy_updates,
DatabaseStatusCallback callback) { … }
ServiceWorkerStorage::ServiceWorkerStorage(
const base::FilePath& user_data_directory,
scoped_refptr<base::SequencedTaskRunner> database_task_runner)
: … { … }
base::FilePath ServiceWorkerStorage::GetDatabasePath() { … }
base::FilePath ServiceWorkerStorage::GetDiskCachePath() { … }
void ServiceWorkerStorage::LazyInitializeForTest() { … }
void ServiceWorkerStorage::SetPurgingCompleteCallbackForTest(
base::OnceClosure callback) { … }
void ServiceWorkerStorage::GetPurgingResourceIdsForTest(
ResourceIdsCallback callback) { … }
void ServiceWorkerStorage::GetPurgeableResourceIdsForTest(
ResourceIdsCallback callback) { … }
void ServiceWorkerStorage::GetUncommittedResourceIdsForTest(
ResourceIdsCallback callback) { … }
void ServiceWorkerStorage::LazyInitialize(base::OnceClosure callback) { … }
void ServiceWorkerStorage::DidReadInitialData(
std::unique_ptr<InitialData> data,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::DidGetRegistrationsForStorageKey(
GetRegistrationsDataCallback callback,
std::unique_ptr<RegistrationList> registration_data_list,
std::unique_ptr<std::vector<ResourceList>> resource_lists,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::DidGetAllRegistrations(
GetAllRegistrationsCallback callback,
std::unique_ptr<RegistrationList> registration_data_list,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::DidStoreRegistrationData(
StoreRegistrationDataCallback callback,
uint64_t new_resources_total_size_bytes,
const blink::StorageKey& key,
const ServiceWorkerDatabase::DeletedVersion& deleted_version,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::DidDeleteRegistration(
std::unique_ptr<DidDeleteRegistrationParams> params,
StorageKeyState storage_key_state,
const ServiceWorkerDatabase::DeletedVersion& deleted_version,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::DidDoomUncommittedResourceIds(
const std::vector<int64_t>& resource_ids,
DatabaseStatusCallback callback,
ServiceWorkerDatabase::Status status) { … }
ServiceWorkerDiskCache* ServiceWorkerStorage::disk_cache() { … }
void ServiceWorkerStorage::InitializeDiskCache() { … }
void ServiceWorkerStorage::OnDiskCacheInitialized(int rv) { … }
void ServiceWorkerStorage::StartPurgingResources(
const std::vector<int64_t>& resource_ids) { … }
void ServiceWorkerStorage::StartPurgingResources(
const ResourceList& resources) { … }
void ServiceWorkerStorage::ContinuePurgingResources() { … }
void ServiceWorkerStorage::PurgeResource(int64_t id) { … }
void ServiceWorkerStorage::OnResourcePurged(int64_t id, int rv) { … }
void ServiceWorkerStorage::DeleteStaleResources() { … }
void ServiceWorkerStorage::DidCollectStaleResources(
const std::vector<int64_t>& stale_resource_ids,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::ClearSessionOnlyOrigins() { … }
void ServiceWorkerStorage::OnResourceReaderDisconnected(
uint64_t resource_operation_id) { … }
void ServiceWorkerStorage::OnResourceWriterDisconnected(
uint64_t resource_operation_id) { … }
void ServiceWorkerStorage::OnResourceMetadataWriterDisconnected(
uint64_t resource_operation_id) { … }
void ServiceWorkerStorage::CollectStaleResourcesFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
GetResourcesCallback callback) { … }
void ServiceWorkerStorage::ReadInitialDataFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
InitializeCallback callback) { … }
void ServiceWorkerStorage::DeleteRegistrationFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64_t registration_id,
const blink::StorageKey& key,
DeleteRegistrationInDBCallback callback) { … }
void ServiceWorkerStorage::WriteRegistrationInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
mojom::ServiceWorkerRegistrationDataPtr registration,
ResourceList resources,
WriteRegistrationCallback callback) { … }
void ServiceWorkerStorage::FindForClientUrlInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
const GURL& client_url,
const blink::StorageKey& key,
FindForClientUrlInDBCallback callback) { … }
void ServiceWorkerStorage::FindForScopeInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
const GURL& scope,
const blink::StorageKey& key,
FindInDBCallback callback) { … }
void ServiceWorkerStorage::FindForIdInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64_t registration_id,
const blink::StorageKey& key,
FindInDBCallback callback) { … }
void ServiceWorkerStorage::FindForIdOnlyInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64_t registration_id,
FindInDBCallback callback) { … }
void ServiceWorkerStorage::GetUsageForStorageKeyInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
const blink::StorageKey& key,
GetUsageForStorageKeyCallback callback) { … }
void ServiceWorkerStorage::GetUserDataInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64_t registration_id,
const std::vector<std::string>& keys,
GetUserDataInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserDataByKeyPrefixInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64_t registration_id,
const std::string& key_prefix,
GetUserDataInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserKeysAndDataByKeyPrefixInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
int64_t registration_id,
const std::string& key_prefix,
GetUserKeysAndDataInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserDataForAllRegistrationsInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
const std::string& key,
GetUserDataForAllRegistrationsInDBCallback callback) { … }
void ServiceWorkerStorage::GetUserDataForAllRegistrationsByKeyPrefixInDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
const std::string& key_prefix,
GetUserDataForAllRegistrationsInDBCallback callback) { … }
void ServiceWorkerStorage::DeleteAllDataForOriginsFromDB(
ServiceWorkerDatabase* database,
const std::set<url::Origin>& origins) { … }
void ServiceWorkerStorage::PerformStorageCleanupInDB(
ServiceWorkerDatabase* database) { … }
void ServiceWorkerStorage::GetPurgeableResourceIdsFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
ServiceWorkerStorage::ResourceIdsCallback callback) { … }
void ServiceWorkerStorage::GetUncommittedResourceIdsFromDB(
ServiceWorkerDatabase* database,
scoped_refptr<base::SequencedTaskRunner> original_task_runner,
ServiceWorkerStorage::ResourceIdsCallback callback) { … }
void ServiceWorkerStorage::DidDeleteDatabase(
DatabaseStatusCallback callback,
ServiceWorkerDatabase::Status status) { … }
void ServiceWorkerStorage::DidDeleteDiskCache(DatabaseStatusCallback callback,
bool result) { … }
}