#include "content/browser/service_worker/service_worker_context_core.h"
#include <limits>
#include <memory>
#include <set>
#include <tuple>
#include <utility>
#include "base/barrier_closure.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/not_fatal_until.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "components/services/storage/public/cpp/quota_client_callback_wrapper.h"
#include "content/browser/log_console_message.h"
#include "content/browser/renderer_host/render_frame_host_impl.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_observer.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_info.h"
#include "content/browser/service_worker/service_worker_job_coordinator.h"
#include "content/browser/service_worker/service_worker_offline_capability_checker.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_register_job.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/browser/service_worker/service_worker_security_utils.h"
#include "content/browser/service_worker/service_worker_usb_delegate_observer.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/browser/storage_partition_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_host.h"
#include "content/public/browser/console_message.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/service_worker_running_info.h"
#include "content/public/common/url_utils.h"
#include "ipc/ipc_message.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_response_info.h"
#include "storage/browser/quota/quota_manager_proxy.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/mojom/service_worker/service_worker_container_type.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration_options.mojom.h"
#include "url/gurl.h"
namespace content {
namespace {
void CheckFetchHandlerOfInstalledServiceWorker(
ServiceWorkerContext::CheckHasServiceWorkerCallback callback,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
class RegistrationDeletionListener
: public ServiceWorkerRegistration::Listener { … };
void SuccessReportingCallback(
int* expected_calls,
std::vector<std::unique_ptr<RegistrationDeletionListener>>* listeners,
base::OnceCallback<void(blink::ServiceWorkerStatusCode)>& callback,
blink::ServiceWorkerStatusCode status) { … }
bool IsSameOriginServiceWorkerClient(
const blink::StorageKey& key,
bool allow_reserved_client,
bool allow_back_forward_cached_client,
ServiceWorkerClient& service_worker_client) { … }
bool IsSameOriginWindowServiceWorkerClient(
const blink::StorageKey& key,
bool allow_reserved_client,
ServiceWorkerClient& service_worker_client) { … }
class ClearAllServiceWorkersHelper
: public base::RefCounted<ClearAllServiceWorkersHelper> { … };
int GetWarmedUpServiceWorkerCount(
const std::map<int64_t, ServiceWorkerVersion*>& live_versions) { … }
}
ServiceWorkerClientOwner::ServiceWorkerClientIterator::
~ServiceWorkerClientIterator() = default;
ServiceWorkerClient&
ServiceWorkerClientOwner::ServiceWorkerClientIterator::operator*() const { … }
ServiceWorkerClient*
ServiceWorkerClientOwner::ServiceWorkerClientIterator::operator->() const { … }
ServiceWorkerClientOwner::ServiceWorkerClientIterator&
ServiceWorkerClientOwner::ServiceWorkerClientIterator::operator++() { … }
bool ServiceWorkerClientOwner::ServiceWorkerClientIterator::IsAtEnd() const { … }
ServiceWorkerClientOwner::ServiceWorkerClientIterator::
ServiceWorkerClientIterator(ServiceWorkerClientByClientUUIDMap* map,
ServiceWorkerClientPredicate predicate)
: … { … }
void ServiceWorkerClientOwner::ServiceWorkerClientIterator::
ForwardUntilMatchingServiceWorkerClient() { … }
ServiceWorkerClientOwner::ServiceWorkerClientOwner(
ServiceWorkerContextCore& context)
: … { … }
ServiceWorkerClientOwner::~ServiceWorkerClientOwner() = default;
void ServiceWorkerClientOwner::ResetContext(
ServiceWorkerContextCore& new_context) { … }
ServiceWorkerContextCore::ServiceWorkerContextCore(
storage::QuotaManagerProxy* quota_manager_proxy,
storage::SpecialStoragePolicy* special_storage_policy,
std::unique_ptr<blink::PendingURLLoaderFactoryBundle>
non_network_pending_loader_factory_bundle_for_update_check,
base::ObserverListThreadSafe<ServiceWorkerContextCoreObserver>*
observer_list,
ServiceWorkerContextSynchronousObserverList* synchronous_observer_list,
ServiceWorkerContextWrapper* wrapper)
: … { … }
ServiceWorkerContextCore::ServiceWorkerContextCore(
std::unique_ptr<ServiceWorkerContextCore> old_context,
ServiceWorkerContextWrapper* wrapper)
: … { … }
ServiceWorkerContextCore::~ServiceWorkerContextCore() { … }
ServiceWorkerClientOwner::ServiceWorkerClientIterator
ServiceWorkerClientOwner::GetServiceWorkerClients(
const blink::StorageKey& key,
bool include_reserved_clients,
bool include_back_forward_cached_clients) { … }
ServiceWorkerClientOwner::ServiceWorkerClientIterator
ServiceWorkerClientOwner::GetWindowServiceWorkerClients(
const blink::StorageKey& key,
bool include_reserved_clients) { … }
void ServiceWorkerClientOwner::HasMainFrameWindowClient(
const blink::StorageKey& key,
BoolCallback callback) { … }
ScopedServiceWorkerClient
ServiceWorkerClientOwner::CreateServiceWorkerClientForWindow(
bool are_ancestors_secure,
int frame_tree_node_id) { … }
ScopedServiceWorkerClient
ServiceWorkerClientOwner::CreateServiceWorkerClientForWorker(
int process_id,
ServiceWorkerClientInfo client_info) { … }
void ServiceWorkerClientOwner::BindHost(
ServiceWorkerContainerHostForClient& container_host,
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver) { … }
void ServiceWorkerClientOwner::UpdateServiceWorkerClientClientID(
const std::string& current_client_uuid,
const std::string& new_client_uuid) { … }
ServiceWorkerClient* ServiceWorkerClientOwner::GetServiceWorkerClientByClientID(
const std::string& client_uuid) { … }
ServiceWorkerClient* ServiceWorkerClientOwner::GetServiceWorkerClientByWindowId(
const base::UnguessableToken& window_id) { … }
void ServiceWorkerClientOwner::OnContainerHostReceiverDisconnected() { … }
void ServiceWorkerContextCore::OnClientDestroyed(
ServiceWorkerClient& service_worker_client) { … }
void ServiceWorkerClientOwner::DestroyServiceWorkerClient(
base::WeakPtr<ServiceWorkerClient> service_worker_client) { … }
void ServiceWorkerContextCore::RegisterServiceWorker(
const GURL& script_url,
const blink::StorageKey& key,
const blink::mojom::ServiceWorkerRegistrationOptions& options,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
RegistrationCallback callback,
const GlobalRenderFrameHostId& requesting_frame_id,
const PolicyContainerPolicies& policy_container_policies) { … }
void ServiceWorkerContextCore::UpdateServiceWorkerWithoutExecutionContext(
ServiceWorkerRegistration* registration,
bool force_bypass_cache) { … }
void ServiceWorkerContextCore::UpdateServiceWorker(
ServiceWorkerRegistration* registration,
bool force_bypass_cache,
bool skip_script_comparison,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
UpdateCallback callback) { … }
void ServiceWorkerContextCore::UnregisterServiceWorker(
const GURL& scope,
const blink::StorageKey& key,
bool is_immediate,
UnregistrationCallback callback) { … }
void ServiceWorkerContextCore::DeleteForStorageKey(const blink::StorageKey& key,
StatusCallback callback) { … }
void ServiceWorkerContextCore::PerformStorageCleanup(
base::OnceClosure callback) { … }
void ServiceWorkerContextCore::DidGetRegistrationsForDeleteForStorageKey(
const blink::StorageKey& key,
base::OnceCallback<void(blink::ServiceWorkerStatusCode)> callback,
blink::ServiceWorkerStatusCode status,
const std::vector<scoped_refptr<ServiceWorkerRegistration>>&
registrations) { … }
int ServiceWorkerContextCore::GetNextEmbeddedWorkerId() { … }
void ServiceWorkerContextCore::NotifyClientIsExecutionReady(
const ServiceWorkerClient& service_worker_client) { … }
bool ServiceWorkerContextCore::MaybeHasRegistrationForStorageKey(
const blink::StorageKey& key) { … }
void ServiceWorkerContextCore::WaitForRegistrationsInitializedForTest() { … }
void ServiceWorkerContextCore::AddWarmUpRequest(
const GURL& document_url,
const blink::StorageKey& key,
ServiceWorkerContext::WarmUpServiceWorkerCallback callback) { … }
std::optional<ServiceWorkerContextCore::WarmUpRequest>
ServiceWorkerContextCore::PopNextWarmUpRequest() { … }
bool ServiceWorkerContextCore::IsWaitingForWarmUp(
const blink::StorageKey& key) const { … }
void ServiceWorkerContextCore::RegistrationComplete(
const GURL& scope,
const blink::StorageKey& key,
ServiceWorkerContextCore::RegistrationCallback callback,
blink::ServiceWorkerStatusCode status,
const std::string& status_message,
ServiceWorkerRegistration* registration) { … }
void ServiceWorkerContextCore::UpdateServiceWorkerImpl(
ServiceWorkerRegistration* registration,
bool force_bypass_cache,
bool skip_script_comparison,
blink::mojom::FetchClientSettingsObjectPtr
outside_fetch_client_settings_object,
UpdateCallback callback) { … }
void ServiceWorkerContextCore::UpdateComplete(
ServiceWorkerContextCore::UpdateCallback callback,
blink::ServiceWorkerStatusCode status,
const std::string& status_message,
ServiceWorkerRegistration* registration) { … }
void ServiceWorkerContextCore::UnregistrationComplete(
const GURL& scope,
const blink::StorageKey& key,
ServiceWorkerContextCore::UnregistrationCallback callback,
int64_t registration_id,
blink::ServiceWorkerStatusCode status) { … }
bool ServiceWorkerContextCore::IsValidRegisterRequest(
const GURL& script_url,
const GURL& scope_url,
const blink::StorageKey& key,
std::string* out_error) const { … }
scoped_refptr<ServiceWorkerRegistration>
ServiceWorkerContextCore::GetLiveRegistration(int64_t id) { … }
void ServiceWorkerContextCore::AddLiveRegistration(
ServiceWorkerRegistration* registration) { … }
void ServiceWorkerContextCore::RemoveLiveRegistration(int64_t id) { … }
ServiceWorkerVersion* ServiceWorkerContextCore::GetLiveVersion(int64_t id) { … }
void ServiceWorkerContextCore::AddLiveVersion(ServiceWorkerVersion* version) { … }
void ServiceWorkerContextCore::RemoveLiveVersion(int64_t id) { … }
std::vector<ServiceWorkerRegistrationInfo>
ServiceWorkerContextCore::GetAllLiveRegistrationInfo() { … }
std::vector<ServiceWorkerVersionInfo>
ServiceWorkerContextCore::GetAllLiveVersionInfo() { … }
void ServiceWorkerContextCore::ProtectVersion(
const scoped_refptr<ServiceWorkerVersion>& version) { … }
void ServiceWorkerContextCore::UnprotectVersion(int64_t version_id) { … }
void ServiceWorkerContextCore::ScheduleDeleteAndStartOver() const { … }
void ServiceWorkerContextCore::DeleteAndStartOver(StatusCallback callback) { … }
void ServiceWorkerContextCore::ClearAllServiceWorkersForTest(
base::OnceClosure callback) { … }
void ServiceWorkerContextCore::CheckHasServiceWorker(
const GURL& url,
const blink::StorageKey& key,
ServiceWorkerContext::CheckHasServiceWorkerCallback callback) { … }
void ServiceWorkerContextCore::CheckOfflineCapability(
const GURL& url,
const blink::StorageKey& key,
ServiceWorkerContext::CheckOfflineCapabilityCallback callback) { … }
void ServiceWorkerContextCore::UpdateVersionFailureCount(
int64_t version_id,
blink::ServiceWorkerStatusCode status) { … }
int ServiceWorkerContextCore::GetVersionFailureCount(int64_t version_id) { … }
void ServiceWorkerContextCore::NotifyRegistrationStored(
int64_t registration_id,
const GURL& scope,
const blink::StorageKey& key) { … }
void ServiceWorkerContextCore::NotifyAllRegistrationsDeletedForStorageKey(
const blink::StorageKey& key) { … }
void ServiceWorkerContextCore::OnStorageWiped() { … }
void ServiceWorkerContextCore::OnMainScriptResponseSet(
int64_t version_id,
const ServiceWorkerVersion::MainScriptResponse& response) { … }
void ServiceWorkerContextCore::OnWindowOpened(const GURL& script_url,
const GURL& url) { … }
void ServiceWorkerContextCore::OnClientNavigated(const GURL& script_url,
const GURL& url) { … }
void ServiceWorkerContextCore::OnControlleeAdded(
ServiceWorkerVersion* version,
const std::string& client_uuid,
const ServiceWorkerClientInfo& client_info) { … }
void ServiceWorkerContextCore::OnControlleeRemoved(
ServiceWorkerVersion* version,
const std::string& client_uuid) { … }
void ServiceWorkerContextCore::OnNoControllees(ServiceWorkerVersion* version) { … }
void ServiceWorkerContextCore::OnControlleeNavigationCommitted(
ServiceWorkerVersion* version,
const std::string& client_uuid,
GlobalRenderFrameHostId render_frame_host_id) { … }
void ServiceWorkerContextCore::OnRunningStateChanged(
ServiceWorkerVersion* version) { … }
void ServiceWorkerContextCore::OnVersionStateChanged(
ServiceWorkerVersion* version) { … }
void ServiceWorkerContextCore::OnDevToolsRoutingIdChanged(
ServiceWorkerVersion* version) { … }
void ServiceWorkerContextCore::OnErrorReported(
ServiceWorkerVersion* version,
const std::u16string& error_message,
int line_number,
int column_number,
const GURL& source_url) { … }
void ServiceWorkerContextCore::OnReportConsoleMessage(
ServiceWorkerVersion* version,
blink::mojom::ConsoleMessageSource source,
blink::mojom::ConsoleMessageLevel message_level,
const std::u16string& message,
int line_number,
const GURL& source_url) { … }
mojo::Remote<storage::mojom::ServiceWorkerStorageControl>&
ServiceWorkerContextCore::GetStorageControl() { … }
ServiceWorkerProcessManager* ServiceWorkerContextCore::process_manager() { … }
void ServiceWorkerContextCore::DidFindRegistrationForCheckHasServiceWorker(
ServiceWorkerContext::CheckHasServiceWorkerCallback callback,
blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextCore::OnRegistrationFinishedForCheckHasServiceWorker(
ServiceWorkerContext::CheckHasServiceWorkerCallback callback,
scoped_refptr<ServiceWorkerRegistration> registration) { … }
void ServiceWorkerContextCore::DidGetRegisteredStorageKeys(
base::TimeTicks start_time,
const std::vector<blink::StorageKey>& storage_keys) { … }
ScopedServiceWorkerClient::ScopedServiceWorkerClient(
base::WeakPtr<ServiceWorkerClient> service_worker_client)
: … { … }
ScopedServiceWorkerClient::~ScopedServiceWorkerClient() { … }
ScopedServiceWorkerClient::ScopedServiceWorkerClient(
ScopedServiceWorkerClient&& other) = default;
std::tuple<blink::mojom::ServiceWorkerContainerInfoForClientPtr,
blink::mojom::ControllerServiceWorkerInfoPtr>
ScopedServiceWorkerClient::CommitResponseAndRelease(
std::optional<GlobalRenderFrameHostId> rfh_id,
const PolicyContainerPolicies& policy_container_policies,
mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
coep_reporter,
ukm::SourceId ukm_source_id) { … }
#if !BUILDFLAG(IS_ANDROID)
ServiceWorkerHidDelegateObserver*
ServiceWorkerContextCore::hid_delegate_observer() { … }
void ServiceWorkerContextCore::SetServiceWorkerHidDelegateObserverForTesting(
std::unique_ptr<ServiceWorkerHidDelegateObserver> hid_delegate_observer) { … }
ServiceWorkerUsbDelegateObserver*
ServiceWorkerContextCore::usb_delegate_observer() { … }
void ServiceWorkerContextCore::SetServiceWorkerUsbDelegateObserverForTesting(
std::unique_ptr<ServiceWorkerUsbDelegateObserver> usb_delegate_observer) { … }
#endif
}