#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/barrier_closure.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "components/services/storage/service_worker/service_worker_storage_control_impl.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/loader/navigation_url_loader_impl.h"
#include "content/browser/loader/url_loader_factory_utils.h"
#include "content/browser/service_worker/service_worker_client.h"
#include "content/browser/service_worker/service_worker_container_host.h"
#include "content/browser/service_worker/service_worker_host.h"
#include "content/browser/service_worker/service_worker_object_host.h"
#include "content/browser/service_worker/service_worker_process_manager.h"
#include "content/browser/service_worker/service_worker_quota_client.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/browser/storage_partition_impl.h"
#include "content/browser/webui/web_ui_controller_factory_registry.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/service_worker_context_observer.h"
#include "content/public/browser/service_worker_running_info.h"
#include "content/public/browser/storage_usage_info.h"
#include "content/public/browser/web_ui_url_loader_factory.h"
#include "content/public/browser/webui_config.h"
#include "content/public/browser/webui_config_map.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/origin_util.h"
#include "content/public/common/url_constants.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/url_util.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/network/public/cpp/url_loader_factory_builder.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "storage/browser/quota/quota_client_type.h"
#include "storage/browser/quota/quota_manager_proxy.h"
#include "storage/browser/quota/special_storage_policy.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/privacy_budget/identifiability_study_settings.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/common/service_worker/service_worker_scope_match.h"
#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
namespace content {
namespace {
ServiceWorkerRunningInfo::ServiceWorkerVersionStatus
GetRunningInfoVersionStatusForStatus(
ServiceWorkerVersion::Status version_status) { … }
const base::FeatureParam<int> kUpdateDelayParam{ … };
void DidFindRegistrationForStartActiveWorker(
ServiceWorkerContextWrapper::StatusCallback callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void DidStartWorker(scoped_refptr<ServiceWorkerVersion> version,
ServiceWorkerContext::StartWorkerCallback info_callback,
ServiceWorkerContext::StatusCodeCallback failure_callback,
blink::ServiceWorkerStatusCode start_worker_status) { … }
void FoundRegistrationForStartWorker(
ServiceWorkerContext::StartWorkerCallback info_callback,
ServiceWorkerContext::StatusCodeCallback failure_callback,
blink::ServiceWorkerStatusCode service_worker_status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void RunOnceClosure(scoped_refptr<ServiceWorkerContextWrapper> ref_holder,
base::OnceClosure task) { … }
class WrapResultCallbackToTakeStatusCode { … };
void RunOrPostTaskOnUIThread(const base::Location& location,
base::OnceClosure task) { … }
}
ServiceWorkerContextSynchronousObserverList::
ServiceWorkerContextSynchronousObserverList() = default;
ServiceWorkerContextSynchronousObserverList::
~ServiceWorkerContextSynchronousObserverList() = default;
bool ServiceWorkerContext::ScopeMatches(const GURL& scope, const GURL& url) { … }
void ServiceWorkerContext::RunTask(
scoped_refptr<base::SequencedTaskRunner> task_runner,
const base::Location& from_here,
ServiceWorkerContext* service_worker_context,
base::OnceClosure task) { … }
base::TimeDelta ServiceWorkerContext::GetUpdateDelay() { … }
ServiceWorkerContextWrapper::ServiceWorkerContextWrapper(
BrowserContext* browser_context)
: … { … }
void ServiceWorkerContextWrapper::Init(
const base::FilePath& user_data_directory,
storage::QuotaManagerProxy* quota_manager_proxy,
storage::SpecialStoragePolicy* special_storage_policy,
ChromeBlobStorageContext* blob_context) { … }
void ServiceWorkerContextWrapper::InitInternal(
storage::QuotaManagerProxy* quota_manager_proxy,
storage::SpecialStoragePolicy* special_storage_policy,
ChromeBlobStorageContext* blob_context,
BrowserContext* browser_context) { … }
void ServiceWorkerContextWrapper::Shutdown() { … }
void ServiceWorkerContextWrapper::DeleteAndStartOver() { … }
StoragePartitionImpl* ServiceWorkerContextWrapper::storage_partition() const { … }
void ServiceWorkerContextWrapper::set_storage_partition(
StoragePartitionImpl* storage_partition) { … }
BrowserContext* ServiceWorkerContextWrapper::browser_context() { … }
void ServiceWorkerContextWrapper::OnRegistrationCompleted(
int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::OnRegistrationStored(
int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::OnAllRegistrationsDeletedForStorageKey(
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::OnErrorReported(
int64_t version_id,
const GURL& scope,
const blink::StorageKey& key,
const ServiceWorkerContextObserver::ErrorInfo& info) { … }
void ServiceWorkerContextWrapper::OnReportConsoleMessage(
int64_t version_id,
const GURL& scope,
const blink::StorageKey& key,
const ConsoleMessage& message) { … }
void ServiceWorkerContextWrapper::OnControlleeAdded(
int64_t version_id,
const std::string& client_uuid,
const ServiceWorkerClientInfo& client_info) { … }
void ServiceWorkerContextWrapper::OnControlleeRemoved(
int64_t version_id,
const std::string& client_uuid) { … }
void ServiceWorkerContextWrapper::OnNoControllees(
int64_t version_id,
const GURL& scope,
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::OnControlleeNavigationCommitted(
int64_t version_id,
const std::string& uuid,
GlobalRenderFrameHostId render_frame_host_id) { … }
void ServiceWorkerContextWrapper::OnWindowOpened(const GURL& script_url,
const GURL& url) { … }
void ServiceWorkerContextWrapper::OnClientNavigated(const GURL& script_url,
const GURL& url) { … }
void ServiceWorkerContextWrapper::OnStarting(int64_t version_id) { … }
void ServiceWorkerContextWrapper::OnStarted(
int64_t version_id,
const GURL& scope,
int process_id,
const GURL& script_url,
const blink::ServiceWorkerToken& token,
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::OnStopping(int64_t version_id) { … }
void ServiceWorkerContextWrapper::OnStopped(int64_t version_id) { … }
void ServiceWorkerContextWrapper::OnDeleteAndStartOver() { … }
void ServiceWorkerContextWrapper::OnVersionStateChanged(
int64_t version_id,
const GURL& scope,
const blink::StorageKey& key,
ServiceWorkerVersion::Status status) { … }
void ServiceWorkerContextWrapper::AddObserver(
ServiceWorkerContextObserver* observer) { … }
void ServiceWorkerContextWrapper::RemoveObserver(
ServiceWorkerContextObserver* observer) { … }
void ServiceWorkerContextWrapper::AddSyncObserver(
ServiceWorkerContextObserverSynchronous* observer) { … }
void ServiceWorkerContextWrapper::RemoveSyncObserver(
ServiceWorkerContextObserverSynchronous* observer) { … }
void ServiceWorkerContextWrapper::
NotifyRunningServiceWorkerStoppedToSynchronousObserver() { … }
void ServiceWorkerContextWrapper::RegisterServiceWorker(
const GURL& script_url,
const blink::StorageKey& key,
const blink::mojom::ServiceWorkerRegistrationOptions& options,
StatusCodeCallback callback) { … }
void ServiceWorkerContextWrapper::UnregisterServiceWorker(
const GURL& scope,
const blink::StorageKey& key,
StatusCodeCallback callback) { … }
void ServiceWorkerContextWrapper::UnregisterServiceWorkerImmediately(
const GURL& scope,
const blink::StorageKey& key,
StatusCodeCallback callback) { … }
void ServiceWorkerContextWrapper::UnregisterServiceWorkerImpl(
const GURL& scope,
const blink::StorageKey& key,
StatusCodeCallback callback) { … }
void ServiceWorkerContextWrapper::UnregisterServiceWorkerImmediatelyImpl(
const GURL& scope,
const blink::StorageKey& key,
StatusCodeCallback callback) { … }
ServiceWorkerExternalRequestResult
ServiceWorkerContextWrapper::StartingExternalRequest(
int64_t service_worker_version_id,
ServiceWorkerExternalRequestTimeoutType timeout_type,
const base::Uuid& request_uuid) { … }
bool ServiceWorkerContextWrapper::ExecuteScriptForTest(
const std::string& script,
int64_t service_worker_version_id,
ServiceWorkerScriptExecutionCallback callback) { … }
ServiceWorkerExternalRequestResult
ServiceWorkerContextWrapper::FinishedExternalRequest(
int64_t service_worker_version_id,
const base::Uuid& request_uuid) { … }
size_t ServiceWorkerContextWrapper::CountExternalRequestsForTest(
const blink::StorageKey& key) { … }
bool ServiceWorkerContextWrapper::MaybeHasRegistrationForStorageKey(
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::GetAllStorageKeysInfo(
GetUsageInfoCallback callback) { … }
void ServiceWorkerContextWrapper::DeleteForStorageKey(
const blink::StorageKey& key,
ResultCallback callback) { … }
void ServiceWorkerContextWrapper::CheckHasServiceWorker(
const GURL& url,
const blink::StorageKey& key,
CheckHasServiceWorkerCallback callback) { … }
void ServiceWorkerContextWrapper::CheckOfflineCapability(
const GURL& url,
const blink::StorageKey& key,
CheckOfflineCapabilityCallback callback) { … }
void ServiceWorkerContextWrapper::ClearAllServiceWorkersForTest(
base::OnceClosure callback) { … }
void ServiceWorkerContextWrapper::StartWorkerForScope(
const GURL& scope,
const blink::StorageKey& key,
StartWorkerCallback info_callback,
StatusCodeCallback failure_callback) { … }
void ServiceWorkerContextWrapper::StartServiceWorkerAndDispatchMessage(
const GURL& scope,
const blink::StorageKey& key,
blink::TransferableMessage message,
ResultCallback result_callback) { … }
void ServiceWorkerContextWrapper::
StartServiceWorkerAndDispatchMessageOnUIThread(
const GURL& scope,
const blink::StorageKey& key,
blink::TransferableMessage message,
ResultCallback result_callback) { … }
void ServiceWorkerContextWrapper::DidFindRegistrationForMessageDispatch(
blink::TransferableMessage message,
const GURL& source_origin,
ResultCallback result_callback,
blink::ServiceWorkerStatusCode service_worker_status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextWrapper::DidStartServiceWorkerForMessageDispatch(
blink::TransferableMessage message,
const GURL& source_origin,
scoped_refptr<ServiceWorkerRegistration> registration,
ResultCallback result_callback,
blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerContextWrapper::StartServiceWorkerForNavigationHint(
const GURL& document_url,
const blink::StorageKey& key,
StartServiceWorkerForNavigationHintCallback callback) { … }
void ServiceWorkerContextWrapper::WarmUpServiceWorker(
const GURL& document_url,
const blink::StorageKey& key,
WarmUpServiceWorkerCallback callback) { … }
void ServiceWorkerContextWrapper::StopAllServiceWorkersForStorageKey(
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::StopAllServiceWorkers(
base::OnceClosure callback) { … }
const base::flat_map<int64_t, ServiceWorkerRunningInfo>&
ServiceWorkerContextWrapper::GetRunningServiceWorkerInfos() { … }
bool ServiceWorkerContextWrapper::IsLiveStartingServiceWorker(
int64_t service_worker_version_id) { … }
bool ServiceWorkerContextWrapper::IsLiveRunningServiceWorker(
int64_t service_worker_version_id) { … }
service_manager::InterfaceProvider&
ServiceWorkerContextWrapper::GetRemoteInterfaces(
int64_t service_worker_version_id) { … }
blink::AssociatedInterfaceProvider&
ServiceWorkerContextWrapper::GetRemoteAssociatedInterfaces(
int64_t service_worker_version_id) { … }
std::optional<ServiceWorkerRunningInfo>
ServiceWorkerContextWrapper::GetRunningServiceWorkerInfo(int64_t version_id) { … }
scoped_refptr<ServiceWorkerRegistration>
ServiceWorkerContextWrapper::GetLiveRegistration(int64_t registration_id) { … }
ServiceWorkerVersion* ServiceWorkerContextWrapper::GetLiveVersion(
int64_t version_id) { … }
std::vector<ServiceWorkerRegistrationInfo>
ServiceWorkerContextWrapper::GetAllLiveRegistrationInfo() { … }
std::vector<ServiceWorkerVersionInfo>
ServiceWorkerContextWrapper::GetAllLiveVersionInfo() { … }
void ServiceWorkerContextWrapper::HasMainFrameWindowClient(
const blink::StorageKey& key,
BoolCallback callback) const { … }
std::unique_ptr<std::vector<GlobalRenderFrameHostId>>
ServiceWorkerContextWrapper::GetWindowClientFrameRoutingIds(
const blink::StorageKey& key) const { … }
void ServiceWorkerContextWrapper::FindReadyRegistrationForClientUrl(
const GURL& client_url,
const blink::StorageKey& key,
FindRegistrationCallback callback) { … }
void ServiceWorkerContextWrapper::FindReadyRegistrationForScope(
const GURL& scope,
const blink::StorageKey& key,
FindRegistrationCallback callback) { … }
void ServiceWorkerContextWrapper::FindRegistrationForScope(
const GURL& scope,
const blink::StorageKey& key,
FindRegistrationCallback callback) { … }
void ServiceWorkerContextWrapper::FindReadyRegistrationForId(
int64_t registration_id,
const blink::StorageKey& key,
FindRegistrationCallback callback) { … }
void ServiceWorkerContextWrapper::FindReadyRegistrationForIdOnly(
int64_t registration_id,
FindRegistrationCallback callback) { … }
void ServiceWorkerContextWrapper::GetAllRegistrations(
GetRegistrationsInfosCallback callback) { … }
void ServiceWorkerContextWrapper::GetRegistrationsForStorageKey(
const blink::StorageKey& key,
GetRegistrationsCallback callback) { … }
void ServiceWorkerContextWrapper::GetRegistrationUserData(
int64_t registration_id,
const std::vector<std::string>& keys,
GetUserDataCallback callback) { … }
void ServiceWorkerContextWrapper::GetRegistrationUserDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserDataCallback callback) { … }
void ServiceWorkerContextWrapper::GetRegistrationUserKeysAndDataByKeyPrefix(
int64_t registration_id,
const std::string& key_prefix,
GetUserKeysAndDataCallback callback) { … }
void ServiceWorkerContextWrapper::StoreRegistrationUserData(
int64_t registration_id,
const blink::StorageKey& key,
const std::vector<std::pair<std::string, std::string>>& key_value_pairs,
StatusCallback callback) { … }
void ServiceWorkerContextWrapper::ClearRegistrationUserData(
int64_t registration_id,
const std::vector<std::string>& keys,
StatusCallback callback) { … }
void ServiceWorkerContextWrapper::ClearRegistrationUserDataByKeyPrefixes(
int64_t registration_id,
const std::vector<std::string>& key_prefixes,
StatusCallback callback) { … }
void ServiceWorkerContextWrapper::GetUserDataForAllRegistrations(
const std::string& key,
GetUserDataForAllRegistrationsCallback callback) { … }
void ServiceWorkerContextWrapper::GetUserDataForAllRegistrationsByKeyPrefix(
const std::string& key_prefix,
GetUserDataForAllRegistrationsCallback callback) { … }
void ServiceWorkerContextWrapper::ClearUserDataForAllRegistrationsByKeyPrefix(
const std::string& key_prefix,
StatusCallback callback) { … }
void ServiceWorkerContextWrapper::StartActiveServiceWorker(
const GURL& scope,
const blink::StorageKey& key,
StatusCallback callback) { … }
void ServiceWorkerContextWrapper::SkipWaitingWorker(
const GURL& scope,
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::UpdateRegistration(
const GURL& scope,
const blink::StorageKey& key) { … }
void ServiceWorkerContextWrapper::SetForceUpdateOnPageLoad(
bool force_update_on_page_load) { … }
void ServiceWorkerContextWrapper::AddObserver(
ServiceWorkerContextCoreObserver* observer) { … }
void ServiceWorkerContextWrapper::RemoveObserver(
ServiceWorkerContextCoreObserver* observer) { … }
ServiceWorkerContextWrapper::~ServiceWorkerContextWrapper() { … }
void ServiceWorkerContextWrapper::FindRegistrationForScopeImpl(
const GURL& scope,
const blink::StorageKey& key,
bool include_installing_version,
FindRegistrationCallback callback) { … }
void ServiceWorkerContextWrapper::MaybeProcessPendingWarmUpRequest() { … }
void ServiceWorkerContextWrapper::DidFindRegistrationForFindImpl(
bool include_installing_version,
FindRegistrationCallback callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextWrapper::OnStatusChangedForFindReadyRegistration(
FindRegistrationCallback callback,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextWrapper::DidDeleteAndStartOver(
blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllStorageKeys(
GetUsageInfoCallback callback,
blink::ServiceWorkerStatusCode status,
const std::vector<ServiceWorkerRegistrationInfo>& registrations) { … }
void ServiceWorkerContextWrapper::DidFindRegistrationForUpdate(
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextWrapper::DidFindRegistrationForNavigationHint(
StartServiceWorkerForNavigationHintCallback callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextWrapper::DidStartServiceWorkerForNavigationHint(
const GURL& scope,
StartServiceWorkerForNavigationHintCallback callback,
blink::ServiceWorkerStatusCode code) { … }
void ServiceWorkerContextWrapper::DidFindRegistrationForWarmUp(
WarmUpServiceWorkerCallback callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextWrapper::DidWarmUpServiceWorker(
const GURL& scope,
WarmUpServiceWorkerCallback callback,
blink::ServiceWorkerStatusCode code) { … }
ServiceWorkerContextCore* ServiceWorkerContextWrapper::context() { … }
std::unique_ptr<blink::PendingURLLoaderFactoryBundle>
ServiceWorkerContextWrapper::
CreateNonNetworkPendingURLLoaderFactoryBundleForUpdateCheck(
BrowserContext* browser_context) { … }
void ServiceWorkerContextWrapper::BindStorageControl(
mojo::PendingReceiver<storage::mojom::ServiceWorkerStorageControl>
receiver) { … }
void ServiceWorkerContextWrapper::SetStorageControlBinderForTest(
StorageControlBinder binder) { … }
void ServiceWorkerContextWrapper::SetForceUpdateOnPageLoadForTesting(
bool force_update_on_page_load) { … }
void ServiceWorkerContextWrapper::SetLoaderFactoryForUpdateCheckForTest(
scoped_refptr<network::SharedURLLoaderFactory> loader_factory) { … }
scoped_refptr<network::SharedURLLoaderFactory>
ServiceWorkerContextWrapper::GetLoaderFactoryForUpdateCheck(
const GURL& scope,
network::mojom::ClientSecurityStatePtr client_security_state) { … }
scoped_refptr<network::SharedURLLoaderFactory>
ServiceWorkerContextWrapper::GetLoaderFactoryForMainScriptFetch(
const GURL& scope,
int64_t version_id,
network::mojom::ClientSecurityStatePtr client_security_state) { … }
scoped_refptr<network::SharedURLLoaderFactory>
ServiceWorkerContextWrapper::GetLoaderFactoryForBrowserInitiatedRequest(
const GURL& scope,
std::optional<int64_t> version_id,
network::mojom::ClientSecurityStatePtr client_security_state) { … }
void ServiceWorkerContextWrapper::ClearRunningServiceWorkers() { … }
ServiceWorkerVersion* ServiceWorkerContextWrapper::GetLiveServiceWorker(
int64_t service_worker_version_id) { … }
}