#include "content/browser/service_worker/service_worker_version.h"
#include <stddef.h>
#include <limits>
#include <map>
#include <optional>
#include <string>
#include "base/check.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/not_fatal_until.h"
#include "base/observer_list.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "base/trace_event/trace_event.h"
#include "base/uuid.h"
#include "components/services/storage/public/mojom/service_worker_database.mojom-forward.h"
#include "content/browser/bad_message.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/renderer_host/back_forward_cache_can_store_document_result.h"
#include "content/browser/renderer_host/private_network_access_util.h"
#include "content/browser/service_worker/payment_handler_support.h"
#include "content/browser/service_worker/service_worker_client.h"
#include "content/browser/service_worker/service_worker_consts.h"
#include "content/browser/service_worker/service_worker_container_host.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/browser/service_worker/service_worker_hid_delegate_observer.h"
#include "content/browser/service_worker/service_worker_host.h"
#include "content/browser/service_worker/service_worker_installed_scripts_sender.h"
#include "content/browser/service_worker/service_worker_security_utils.h"
#include "content/browser/service_worker/service_worker_usb_delegate_observer.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/service_worker_external_request_result.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/result_codes.h"
#include "crypto/secure_hash.h"
#include "crypto/sha2.h"
#include "ipc/ipc_message.h"
#include "mojo/public/c/system/types.h"
#include "net/base/net_errors.h"
#include "net/cookies/site_for_cookies.h"
#include "net/http/http_response_headers.h"
#include "services/network/public/mojom/cross_origin_embedder_policy.mojom.h"
#include "third_party/blink/public/common/origin_trials/trial_token_validator.h"
#include "third_party/blink/public/common/service_worker/service_worker_type_converters.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom.h"
namespace content {
namespace {
constexpr base::TimeDelta kStartInstalledWorkerTimeout = …;
const char kClaimClientsStateErrorMesage[] = …;
const char kClaimClientsShutdownErrorMesage[] = …;
const char kNotRespondingErrorMesage[] = …;
const char kForceUpdateInfoMessage[] = …;
void RunSoon(base::OnceClosure callback) { … }
void RunCallbackAfterStartWorker(base::WeakPtr<ServiceWorkerVersion> version,
ServiceWorkerVersion::StatusCallback callback,
blink::ServiceWorkerStatusCode status) { … }
void ClearTick(base::TimeTicks* time) { … }
const int kInvalidTraceId = …;
int NextTraceId() { … }
void OnConnectionError(base::WeakPtr<EmbeddedWorkerInstance> embedded_worker) { … }
void OnOpenWindowFinished(
blink::mojom::ServiceWorkerHost::OpenNewTabCallback callback,
blink::ServiceWorkerStatusCode status,
blink::mojom::ServiceWorkerClientInfoPtr client_info) { … }
void DidShowPaymentHandlerWindow(
const GURL& url,
const blink::StorageKey& key,
const base::WeakPtr<ServiceWorkerContextCore>& context,
blink::mojom::ServiceWorkerHost::OpenPaymentHandlerWindowCallback callback,
bool success,
int render_process_id,
int render_frame_id) { … }
void DidNavigateClient(
blink::mojom::ServiceWorkerHost::NavigateClientCallback callback,
const GURL& url,
blink::ServiceWorkerStatusCode status,
blink::mojom::ServiceWorkerClientInfoPtr client) { … }
const char* FetchHandlerTypeToSuffix(
ServiceWorkerVersion::FetchHandlerType type) { … }
std::optional<std::string> MergeResourceRecordSHA256ScriptChecksum(
const GURL& main_script_url,
const ServiceWorkerScriptCacheMap& script_cache_map,
std::optional<blink::mojom::ServiceWorkerFetchHandlerType>
fetch_handler_type) { … }
storage::mojom::CacheStorageControl* GetCacheStorageControl(
ServiceWorkerVersion& version) { … }
}
constexpr base::TimeDelta ServiceWorkerVersion::kTimeoutTimerDelay;
constexpr base::TimeDelta ServiceWorkerVersion::kStartNewWorkerTimeout;
constexpr base::TimeDelta ServiceWorkerVersion::kStopWorkerTimeout;
ServiceWorkerVersion::MainScriptResponse::MainScriptResponse(
const network::mojom::URLResponseHead& response_head) { … }
ServiceWorkerVersion::MainScriptResponse::~MainScriptResponse() = default;
void ServiceWorkerVersion::RestartTick(base::TimeTicks* time) const { … }
bool ServiceWorkerVersion::RequestExpired(
const base::TimeTicks& expiration_time) const { … }
base::TimeDelta ServiceWorkerVersion::GetTickDuration(
const base::TimeTicks& time) const { … }
ServiceWorkerVersion::ServiceWorkerVersion(
ServiceWorkerRegistration* registration,
const GURL& script_url,
blink::mojom::ScriptType script_type,
int64_t version_id,
mojo::PendingRemote<storage::mojom::ServiceWorkerLiveVersionRef>
remote_reference,
base::WeakPtr<ServiceWorkerContextCore> context)
: … { … }
ServiceWorkerVersion::~ServiceWorkerVersion() { … }
void ServiceWorkerVersion::SetNavigationPreloadState(
const blink::mojom::NavigationPreloadState& state) { … }
void ServiceWorkerVersion::SetRegistrationStatus(
ServiceWorkerRegistration::Status registration_status) { … }
void ServiceWorkerVersion::SetStatus(Status status) { … }
void ServiceWorkerVersion::RegisterStatusChangeCallback(
base::OnceClosure callback) { … }
ServiceWorkerVersionInfo ServiceWorkerVersion::GetInfo() { … }
ServiceWorkerVersion::FetchHandlerExistence
ServiceWorkerVersion::fetch_handler_existence() const { … }
ServiceWorkerVersion::FetchHandlerType
ServiceWorkerVersion::fetch_handler_type() const { … }
void ServiceWorkerVersion::set_fetch_handler_type(
FetchHandlerType fetch_handler_type) { … }
void ServiceWorkerVersion::set_has_hid_event_handlers(
bool has_hid_event_handlers) { … }
void ServiceWorkerVersion::set_has_usb_event_handlers(
bool has_usb_event_handlers) { … }
void ServiceWorkerVersion::StartWorker(ServiceWorkerMetrics::EventType purpose,
StatusCallback callback) { … }
void ServiceWorkerVersion::StopWorker(base::OnceClosure callback) { … }
void ServiceWorkerVersion::TriggerIdleTerminationAsap() { … }
bool ServiceWorkerVersion::OnRequestTermination() { … }
void ServiceWorkerVersion::ScheduleUpdate() { … }
void ServiceWorkerVersion::StartUpdate() { … }
int ServiceWorkerVersion::StartRequest(
ServiceWorkerMetrics::EventType event_type,
StatusCallback error_callback) { … }
int ServiceWorkerVersion::StartRequestWithCustomTimeout(
ServiceWorkerMetrics::EventType event_type,
StatusCallback error_callback,
const base::TimeDelta& timeout,
TimeoutBehavior timeout_behavior) { … }
ServiceWorkerExternalRequestResult ServiceWorkerVersion::StartExternalRequest(
const base::Uuid& request_uuid,
ServiceWorkerExternalRequestTimeoutType timeout_type) { … }
bool ServiceWorkerVersion::FinishRequest(int request_id, bool was_handled) { … }
bool ServiceWorkerVersion::FinishRequestWithFetchCount(int request_id,
bool was_handled,
uint32_t fetch_count) { … }
ServiceWorkerExternalRequestResult ServiceWorkerVersion::FinishExternalRequest(
const base::Uuid& request_uuid) { … }
ServiceWorkerVersion::SimpleEventCallback
ServiceWorkerVersion::CreateSimpleEventCallback(int request_id) { … }
void ServiceWorkerVersion::RunAfterStartWorker(
ServiceWorkerMetrics::EventType purpose,
StatusCallback callback) { … }
void ServiceWorkerVersion::AddControllee(
ServiceWorkerClient* service_worker_client) { … }
void ServiceWorkerVersion::RemoveControllee(const std::string& client_uuid) { … }
void ServiceWorkerVersion::OnControlleeNavigationCommitted(
const std::string& client_uuid,
const GlobalRenderFrameHostId& rfh_id) { … }
void ServiceWorkerVersion::MoveControlleeToBackForwardCacheMap(
const std::string& client_uuid) { … }
void ServiceWorkerVersion::RestoreControlleeFromBackForwardCacheMap(
const std::string& client_uuid) { … }
void ServiceWorkerVersion::RemoveControlleeFromBackForwardCacheMap(
const std::string& client_uuid) { … }
void ServiceWorkerVersion::Uncontrol(const std::string& client_uuid) { … }
void ServiceWorkerVersion::EvictBackForwardCachedControllees(
BackForwardCacheMetrics::NotRestoredReason reason) { … }
void ServiceWorkerVersion::EvictBackForwardCachedControllee(
ServiceWorkerClient* controllee,
BackForwardCacheMetrics::NotRestoredReason reason) { … }
void ServiceWorkerVersion::AddObserver(Observer* observer) { … }
void ServiceWorkerVersion::RemoveObserver(Observer* observer) { … }
void ServiceWorkerVersion::ReportError(blink::ServiceWorkerStatusCode status,
const std::string& status_message) { … }
void ServiceWorkerVersion::ReportForceUpdateToDevTools() { … }
void ServiceWorkerVersion::SetStartWorkerStatusCode(
blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerVersion::Doom() { … }
void ServiceWorkerVersion::InitializeGlobalScope() { … }
bool ServiceWorkerVersion::IsControlleeProcessID(int process_id) const { … }
void ServiceWorkerVersion::ExecuteScriptForTest(
const std::string& script,
ServiceWorkerScriptExecutionCallback callback) { … }
bool ServiceWorkerVersion::IsWarmingUp() const { … }
bool ServiceWorkerVersion::IsWarmedUp() const { … }
void ServiceWorkerVersion::SetValidOriginTrialTokens(
const blink::TrialTokenValidator::FeatureToTokensMap& tokens) { … }
void ServiceWorkerVersion::SetDevToolsAttached(bool attached) { … }
void ServiceWorkerVersion::SetMainScriptResponse(
std::unique_ptr<MainScriptResponse> response) { … }
void ServiceWorkerVersion::SimulatePingTimeoutForTesting() { … }
void ServiceWorkerVersion::SetTickClockForTesting(
const base::TickClock* tick_clock) { … }
void ServiceWorkerVersion::RunUserTasksForTesting() { … }
bool ServiceWorkerVersion::HasNoWork() const { … }
const ServiceWorkerVersion::MainScriptResponse*
ServiceWorkerVersion::GetMainScriptResponse() { … }
ServiceWorkerVersion::InflightRequestTimeoutInfo::InflightRequestTimeoutInfo(
int id,
ServiceWorkerMetrics::EventType event_type,
const base::TimeTicks& expiration_time,
TimeoutBehavior timeout_behavior)
: … { … }
ServiceWorkerVersion::InflightRequestTimeoutInfo::
~InflightRequestTimeoutInfo() { … }
bool ServiceWorkerVersion::InflightRequestTimeoutInfo::operator<(
const InflightRequestTimeoutInfo& other) const { … }
ServiceWorkerVersion::InflightRequest::InflightRequest(
StatusCallback callback,
base::Time time,
const base::TimeTicks& time_ticks,
ServiceWorkerMetrics::EventType event_type)
: … { … }
ServiceWorkerVersion::InflightRequest::~InflightRequest() { … }
void ServiceWorkerVersion::OnScriptEvaluationStart() { … }
void ServiceWorkerVersion::OnScriptLoaded() { … }
void ServiceWorkerVersion::OnProcessAllocated() { … }
void ServiceWorkerVersion::OnStarting() { … }
void ServiceWorkerVersion::OnStarted(
blink::mojom::ServiceWorkerStartStatus start_status,
FetchHandlerType new_fetch_handler_type,
bool new_has_hid_event_handlers,
bool new_has_usb_event_handlers) { … }
void ServiceWorkerVersion::OnStopping() { … }
void ServiceWorkerVersion::OnStopped(blink::EmbeddedWorkerStatus old_status) { … }
void ServiceWorkerVersion::OnDetached(blink::EmbeddedWorkerStatus old_status) { … }
void ServiceWorkerVersion::OnRegisteredToDevToolsManager() { … }
void ServiceWorkerVersion::OnReportException(
const std::u16string& error_message,
int line_number,
int column_number,
const GURL& source_url) { … }
void ServiceWorkerVersion::OnReportConsoleMessage(
blink::mojom::ConsoleMessageSource source,
blink::mojom::ConsoleMessageLevel message_level,
const std::u16string& message,
int line_number,
const GURL& source_url) { … }
void ServiceWorkerVersion::OnStartSent(blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerVersion::SetCachedMetadata(const GURL& url,
base::span<const uint8_t> data) { … }
void ServiceWorkerVersion::ClearCachedMetadata(const GURL& url) { … }
void ServiceWorkerVersion::ClaimClients(ClaimClientsCallback callback) { … }
void ServiceWorkerVersion::GetClients(
blink::mojom::ServiceWorkerClientQueryOptionsPtr options,
GetClientsCallback callback) { … }
void ServiceWorkerVersion::GetClient(const std::string& client_uuid,
GetClientCallback callback) { … }
void ServiceWorkerVersion::GetClientInternal(const std::string& client_uuid,
GetClientCallback callback) { … }
void ServiceWorkerVersion::OpenNewTab(const GURL& url,
OpenNewTabCallback callback) { … }
void ServiceWorkerVersion::OpenPaymentHandlerWindow(
const GURL& url,
OpenPaymentHandlerWindowCallback callback) { … }
void ServiceWorkerVersion::PostMessageToClient(
const std::string& client_uuid,
blink::TransferableMessage message) { … }
void ServiceWorkerVersion::FocusClient(const std::string& client_uuid,
FocusClientCallback callback) { … }
void ServiceWorkerVersion::NavigateClient(const std::string& client_uuid,
const GURL& url,
NavigateClientCallback callback) { … }
void ServiceWorkerVersion::SkipWaiting(SkipWaitingCallback callback) { … }
void ServiceWorkerVersion::AddRoutes(
const blink::ServiceWorkerRouterRules& rules,
AddRoutesCallback callback) { … }
void ServiceWorkerVersion::OnSetCachedMetadataFinished(int64_t callback_id,
size_t size,
int result) { … }
void ServiceWorkerVersion::OnClearCachedMetadataFinished(int64_t callback_id,
int result) { … }
void ServiceWorkerVersion::OpenWindow(
GURL url,
service_worker_client_utils::WindowType type,
OpenNewTabCallback callback) { … }
bool ServiceWorkerVersion::HasWorkInBrowser() const { … }
void ServiceWorkerVersion::OnSimpleEventFinished(
int request_id,
blink::mojom::ServiceWorkerEventStatus status) { … }
void ServiceWorkerVersion::CountFeature(blink::mojom::WebFeature feature) { … }
network::mojom::CrossOriginEmbedderPolicyValue
ServiceWorkerVersion::cross_origin_embedder_policy_value() const { … }
const network::CrossOriginEmbedderPolicy*
ServiceWorkerVersion::cross_origin_embedder_policy() const { … }
const network::DocumentIsolationPolicy*
ServiceWorkerVersion::document_isolation_policy() const { … }
const network::mojom::ClientSecurityStatePtr
ServiceWorkerVersion::BuildClientSecurityState() const { … }
bool ServiceWorkerVersion::IsInstalled(ServiceWorkerVersion::Status status) { … }
std::string ServiceWorkerVersion::VersionStatusToString(
ServiceWorkerVersion::Status status) { … }
void ServiceWorkerVersion::IncrementPendingUpdateHintCount() { … }
void ServiceWorkerVersion::DecrementPendingUpdateHintCount() { … }
void ServiceWorkerVersion::OnPongFromWorker() { … }
void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker(
ServiceWorkerMetrics::EventType purpose,
Status prestart_status,
bool is_browser_startup_complete,
StatusCallback callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerVersion::StartWorkerInternal() { … }
void ServiceWorkerVersion::StartTimeoutTimer() { … }
void ServiceWorkerVersion::StopTimeoutTimer() { … }
void ServiceWorkerVersion::SetTimeoutTimerInterval(base::TimeDelta interval) { … }
void ServiceWorkerVersion::OnTimeoutTimer() { … }
void ServiceWorkerVersion::PingWorker() { … }
void ServiceWorkerVersion::OnPingTimeout() { … }
void ServiceWorkerVersion::RecordStartWorkerResult(
ServiceWorkerMetrics::EventType purpose,
Status prestart_status,
int trace_id,
bool is_browser_startup_complete,
blink::ServiceWorkerStatusCode status) { … }
bool ServiceWorkerVersion::MaybeTimeoutRequest(
const InflightRequestTimeoutInfo& info) { … }
void ServiceWorkerVersion::SetAllRequestExpirations(
const base::TimeTicks& expiration_time) { … }
blink::ServiceWorkerStatusCode
ServiceWorkerVersion::DeduceStartWorkerFailureReason(
blink::ServiceWorkerStatusCode default_code) { … }
net::Error ServiceWorkerVersion::GetMainScriptNetError() { … }
void ServiceWorkerVersion::MarkIfStale() { … }
void ServiceWorkerVersion::FoundRegistrationForUpdate(
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerVersion::OnStoppedInternal(
blink::EmbeddedWorkerStatus old_status) { … }
void ServiceWorkerVersion::FinishStartWorker(
blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerVersion::CleanUpExternalRequest(
const base::Uuid& request_uuid,
blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerVersion::OnNoWorkInBrowser() { … }
bool ServiceWorkerVersion::IsStartWorkerAllowed() const { … }
void ServiceWorkerVersion::NotifyControlleeAdded(
const std::string& uuid,
const ServiceWorkerClientInfo& info) { … }
void ServiceWorkerVersion::NotifyControlleeRemoved(const std::string& uuid) { … }
void ServiceWorkerVersion::NotifyControlleeNavigationCommitted(
const std::string& uuid,
GlobalRenderFrameHostId render_frame_host_id) { … }
void ServiceWorkerVersion::NotifyWindowOpened(const GURL& script_url,
const GURL& url) { … }
void ServiceWorkerVersion::NotifyClientNavigated(const GURL& script_url,
const GURL& url) { … }
void ServiceWorkerVersion::PrepareForUpdate(
std::map<GURL, ServiceWorkerUpdateChecker::ComparedScriptInfo>
compared_script_info_map,
const GURL& updated_script_url,
scoped_refptr<PolicyContainerHost> policy_container_host) { … }
const std::map<GURL, ServiceWorkerUpdateChecker::ComparedScriptInfo>&
ServiceWorkerVersion::compared_script_info_map() const { … }
ServiceWorkerUpdateChecker::ComparedScriptInfo
ServiceWorkerVersion::TakeComparedScriptInfo(const GURL& script_url) { … }
bool ServiceWorkerVersion::ShouldRequireForegroundPriority(
int worker_process_id) const { … }
void ServiceWorkerVersion::UpdateForegroundPriority() { … }
void ServiceWorkerVersion::AddMessageToConsole(
blink::mojom::ConsoleMessageLevel message_level,
const std::string& message) { … }
void ServiceWorkerVersion::MaybeReportConsoleMessageToInternals(
blink::mojom::ConsoleMessageLevel message_level,
const std::string& message) { … }
storage::mojom::ServiceWorkerLiveVersionInfoPtr
ServiceWorkerVersion::RebindStorageReference() { … }
void ServiceWorkerVersion::SetResources(
const std::vector<storage::mojom::ServiceWorkerResourceRecordPtr>&
resources) { … }
ServiceWorkerRouterEvaluatorErrorEnums
ServiceWorkerVersion::SetupRouterEvaluator(
const blink::ServiceWorkerRouterRules& rules) { … }
bool ServiceWorkerVersion::NeedRouterEvaluate() const { … }
bool ServiceWorkerVersion::IsStaticRouterEnabled() { … }
void ServiceWorkerVersion::GetAssociatedInterface(
const std::string& name,
mojo::PendingAssociatedReceiver<blink::mojom::AssociatedInterface>
receiver) { … }
bool ServiceWorkerVersion::BFCacheContainsControllee(
const std::string& uuid) const { … }
base::WeakPtr<ServiceWorkerVersion> ServiceWorkerVersion::GetWeakPtr() { … }
mojo::PendingRemote<blink::mojom::CacheStorage>
ServiceWorkerVersion::GetRemoteCacheStorage() { … }
blink::mojom::ControllerServiceWorkerMode
ServiceWorkerVersion::GetControllerMode() const { … }
}