#include "content/browser/service_worker/service_worker_container_host.h"
#include <set>
#include <utility>
#include "base/containers/contains.h"
#include "base/debug/crash_logging.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.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_context_core.h"
#include "content/browser/service_worker/service_worker_context_wrapper.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_registration_object_host.h"
#include "content/browser/service_worker/service_worker_security_utils.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/common/content_client.h"
#include "content/public/common/origin_util.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
namespace content {
namespace {
ServiceWorkerMetrics::EventType PurposeToEventType(
blink::mojom::ControllerServiceWorkerPurpose purpose) { … }
}
ServiceWorkerContainerHost::ServiceWorkerContainerHost() = default;
ServiceWorkerContainerHostForServiceWorker::
ServiceWorkerContainerHostForServiceWorker(
base::WeakPtr<ServiceWorkerContextCore> context,
ServiceWorkerHost* service_worker_host,
const GURL& url,
const blink::StorageKey& storage_key)
: … { … }
ServiceWorkerContainerHost::~ServiceWorkerContainerHost() { … }
ServiceWorkerContainerHostForClient::~ServiceWorkerContainerHostForClient() =
default;
ServiceWorkerContainerHostForServiceWorker::
~ServiceWorkerContainerHostForServiceWorker() = default;
ServiceWorkerContainerHostForClient::ServiceWorkerContainerHostForClient(
base::PassKey<ServiceWorkerClient>,
base::WeakPtr<ServiceWorkerClient> service_worker_client,
blink::mojom::ServiceWorkerContainerInfoForClientPtr& container_info,
const PolicyContainerPolicies& policy_container_policies,
mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
coep_reporter,
ukm::SourceId ukm_source_id)
: … { … }
bool ServiceWorkerContainerHostForClient::IsContainerRemoteConnected() const { … }
void ServiceWorkerContainerHostForClient::Register(
const GURL& script_url,
blink::mojom::ServiceWorkerRegistrationOptionsPtr options,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
RegisterCallback callback) { … }
void ServiceWorkerContainerHostForClient::GetRegistration(
const GURL& client_url,
GetRegistrationCallback callback) { … }
void ServiceWorkerContainerHostForClient::GetRegistrations(
GetRegistrationsCallback callback) { … }
void ServiceWorkerContainerHostForClient::GetRegistrationForReady(
GetRegistrationForReadyCallback callback) { … }
void ServiceWorkerContainerHostForClient::EnsureControllerServiceWorker(
mojo::PendingReceiver<blink::mojom::ControllerServiceWorker> receiver,
blink::mojom::ControllerServiceWorkerPurpose purpose) { … }
void ServiceWorkerContainerHost::CloneContainerHost(
mojo::PendingReceiver<blink::mojom::ServiceWorkerContainerHost> receiver) { … }
void ServiceWorkerContainerHostForClient::HintToUpdateServiceWorker() { … }
void ServiceWorkerContainerHostForClient::EnsureFileAccess(
const std::vector<base::FilePath>& file_paths,
EnsureFileAccessCallback callback) { … }
void ServiceWorkerContainerHostForClient::OnExecutionReady() { … }
void ServiceWorkerContainerHostForServiceWorker::Register(
const GURL& script_url,
blink::mojom::ServiceWorkerRegistrationOptionsPtr options,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
RegisterCallback callback) { … }
void ServiceWorkerContainerHostForServiceWorker::GetRegistration(
const GURL& client_url,
GetRegistrationCallback callback) { … }
void ServiceWorkerContainerHostForServiceWorker::GetRegistrations(
GetRegistrationsCallback callback) { … }
void ServiceWorkerContainerHostForServiceWorker::GetRegistrationForReady(
GetRegistrationForReadyCallback callback) { … }
void ServiceWorkerContainerHostForServiceWorker::EnsureControllerServiceWorker(
mojo::PendingReceiver<blink::mojom::ControllerServiceWorker> receiver,
blink::mojom::ControllerServiceWorkerPurpose purpose) { … }
void ServiceWorkerContainerHostForServiceWorker::HintToUpdateServiceWorker() { … }
void ServiceWorkerContainerHostForServiceWorker::EnsureFileAccess(
const std::vector<base::FilePath>& file_paths,
EnsureFileAccessCallback callback) { … }
void ServiceWorkerContainerHostForServiceWorker::OnExecutionReady() { … }
void ServiceWorkerContainerHostForClient::OnVersionAttributesChanged(
ServiceWorkerRegistration* registration,
blink::mojom::ChangedServiceWorkerObjectsMaskPtr changed_mask) { … }
void ServiceWorkerContainerHostForClient::PostMessageToClient(
ServiceWorkerVersion& version,
blink::TransferableMessage message) { … }
void ServiceWorkerContainerHostForClient::CountFeature(
blink::mojom::WebFeature feature) { … }
blink::mojom::ControllerServiceWorkerInfoPtr
ServiceWorkerContainerHostForClient::CreateControllerServiceWorkerInfo() { … }
void ServiceWorkerContainerHostForClient::SendSetController(
bool notify_controllerchange) { … }
ServiceWorkerRegistrationObjectManager::ServiceWorkerRegistrationObjectManager(
ServiceWorkerContainerHost* container_host)
: … { … }
ServiceWorkerRegistrationObjectManager::
~ServiceWorkerRegistrationObjectManager() = default;
blink::mojom::ServiceWorkerRegistrationObjectInfoPtr
ServiceWorkerRegistrationObjectManager::CreateInfo(
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerRegistrationObjectManager::RemoveHost(
int64_t registration_id) { … }
ServiceWorkerObjectManager::ServiceWorkerObjectManager(
ServiceWorkerContainerHost* container_host)
: … { … }
ServiceWorkerObjectManager::~ServiceWorkerObjectManager() = default;
blink::mojom::ServiceWorkerObjectInfoPtr
ServiceWorkerObjectManager::CreateInfoToSend(
scoped_refptr<ServiceWorkerVersion> version) { … }
base::WeakPtr<ServiceWorkerObjectHost>
ServiceWorkerObjectManager::GetOrCreateHost(
scoped_refptr<ServiceWorkerVersion> version) { … }
void ServiceWorkerObjectManager::RemoveHost(int64_t version_id) { … }
mojo::PendingRemote<blink::mojom::ControllerServiceWorker>
ServiceWorkerContainerHostForClient::GetRemoteControllerServiceWorker() { … }
void ServiceWorkerContainerHostForClient::CloneControllerServiceWorker(
mojo::PendingReceiver<blink::mojom::ControllerServiceWorker> receiver) { … }
bool ServiceWorkerContainerHostForClient::AllowServiceWorker(
const GURL& scope,
const GURL& script_url) { … }
bool ServiceWorkerContainerHostForServiceWorker::AllowServiceWorker(
const GURL& scope,
const GURL& script_url) { … }
const base::WeakPtr<ServiceWorkerContextCore>&
ServiceWorkerContainerHostForClient::context() const { … }
base::WeakPtr<ServiceWorkerContainerHost>
ServiceWorkerContainerHostForClient::AsWeakPtr() { … }
const GURL& ServiceWorkerContainerHostForClient::url() const { … }
const base::WeakPtr<ServiceWorkerContextCore>&
ServiceWorkerContainerHostForServiceWorker::context() const { … }
base::WeakPtr<ServiceWorkerContainerHost>
ServiceWorkerContainerHostForServiceWorker::AsWeakPtr() { … }
const GURL& ServiceWorkerContainerHostForServiceWorker::url() const { … }
ServiceWorkerHost*
ServiceWorkerContainerHostForServiceWorker::service_worker_host() { … }
const blink::StorageKey& ServiceWorkerContainerHostForServiceWorker::key()
const { … }
const url::Origin&
ServiceWorkerContainerHostForServiceWorker::top_frame_origin() const { … }
void ServiceWorkerContainerHostForClient::ReturnRegistrationForReadyIfNeeded() { … }
void ServiceWorkerContainerHostForClient::StartControllerComplete(
mojo::PendingReceiver<blink::mojom::ControllerServiceWorker> receiver,
blink::ServiceWorkerStatusCode status) { … }
void ServiceWorkerContainerHostForClient::RegistrationComplete(
const GURL& script_url,
const GURL& scope,
RegisterCallback callback,
int64_t trace_id,
mojo::ReportBadMessageCallback bad_message_callback,
blink::ServiceWorkerStatusCode status,
const std::string& status_message,
int64_t registration_id) { … }
void ServiceWorkerContainerHostForClient::GetRegistrationComplete(
GetRegistrationCallback callback,
int64_t trace_id,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContainerHostForClient::GetRegistrationsComplete(
GetRegistrationsCallback callback,
int64_t trace_id,
blink::ServiceWorkerStatusCode status,
const std::vector<scoped_refptr<ServiceWorkerRegistration>>&
registrations) { … }
bool ServiceWorkerContainerHostForClient::IsValidGetRegistrationMessage(
const GURL& client_url,
std::string* out_error) const { … }
bool ServiceWorkerContainerHostForClient::IsValidGetRegistrationsMessage(
std::string* out_error) const { … }
bool ServiceWorkerContainerHostForClient::IsValidGetRegistrationForReadyMessage(
std::string* out_error) const { … }
template <typename CallbackType, typename... Args>
bool ServiceWorkerContainerHostForClient::CanServeContainerHostMethods(
CallbackType* callback,
const GURL& scope,
const GURL& script_url,
const char* error_prefix,
Args... args) { … }
namespace {
StatusCallback;
PrepareExtendableMessageEventCallback;
void DispatchExtendableMessageEventAfterStartWorker(
scoped_refptr<ServiceWorkerVersion> worker,
blink::TransferableMessage message,
const url::Origin& source_origin,
const std::optional<base::TimeDelta>& timeout,
StatusCallback callback,
PrepareExtendableMessageEventCallback prepare_callback,
blink::ServiceWorkerStatusCode start_worker_status) { … }
void StartWorkerToDispatchExtendableMessageEvent(
scoped_refptr<ServiceWorkerVersion> worker,
blink::TransferableMessage message,
const url::Origin& source_origin,
const std::optional<base::TimeDelta>& timeout,
StatusCallback callback,
PrepareExtendableMessageEventCallback prepare_callback) { … }
bool PrepareExtendableMessageEventFromClient(
base::WeakPtr<ServiceWorkerContextCore> context,
int64_t registration_id,
blink::mojom::ServiceWorkerClientInfoPtr source_client_info,
blink::mojom::ExtendableMessageEventPtr* event) { … }
bool PrepareExtendableMessageEventFromServiceWorker(
scoped_refptr<ServiceWorkerVersion> worker,
base::WeakPtr<ServiceWorkerContainerHostForServiceWorker>
source_container_host,
blink::mojom::ExtendableMessageEventPtr* event) { … }
void DispatchExtendableMessageEventFromClient(
base::WeakPtr<ServiceWorkerContextCore> context,
scoped_refptr<ServiceWorkerVersion> worker,
blink::TransferableMessage message,
const url::Origin& source_origin,
StatusCallback callback,
blink::mojom::ServiceWorkerClientInfoPtr source_client_info) { … }
void DispatchExtendableMessageEventFromServiceWorker(
scoped_refptr<ServiceWorkerVersion> worker,
blink::TransferableMessage message,
const url::Origin& source_origin,
const std::optional<base::TimeDelta>& timeout,
StatusCallback callback,
base::WeakPtr<ServiceWorkerContainerHostForServiceWorker>
source_container_host) { … }
}
void ServiceWorkerContainerHostForServiceWorker::DispatchExtendableMessageEvent(
scoped_refptr<ServiceWorkerVersion> version,
::blink::TransferableMessage message,
StatusCallback callback) { … }
void ServiceWorkerContainerHostForClient::DispatchExtendableMessageEvent(
scoped_refptr<ServiceWorkerVersion> version,
::blink::TransferableMessage message,
StatusCallback callback) { … }
void ServiceWorkerContainerHostForClient::Update(
scoped_refptr<ServiceWorkerRegistration> registration,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
blink::mojom::ServiceWorkerRegistrationObjectHost::UpdateCallback
callback) { … }
void ServiceWorkerContainerHostForServiceWorker::Update(
scoped_refptr<ServiceWorkerRegistration> registration,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
blink::mojom::ServiceWorkerRegistrationObjectHost::UpdateCallback
callback) { … }
ServiceWorkerVersion* ServiceWorkerContainerHostForClient::controller() const { … }
}