chromium/content/browser/service_worker/embedded_worker_instance.cc

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/service_worker/embedded_worker_instance.h"

#include <utility>

#include "base/check_is_test.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "content/browser/bad_message.h"
#include "content/browser/data_url_loader_factory.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/devtools/network_service_devtools_observer.h"
#include "content/browser/devtools/service_worker_devtools_agent_host.h"
#include "content/browser/devtools/service_worker_devtools_manager.h"
#include "content/browser/loader/url_loader_factory_utils.h"
#include "content/browser/network/cross_origin_embedder_policy_reporter.h"
#include "content/browser/process_lock.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/service_worker/service_worker_consts.h"
#include "content/browser/service_worker/service_worker_content_settings_proxy_impl.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_script_loader_factory.h"
#include "content/browser/storage_partition_impl.h"
#include "content/browser/url_loader_factory_params_helper.h"
#include "content/browser/usb/web_usb_service_impl.h"
#include "content/common/content_switches_internal.h"
#include "content/common/url_schemes.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/hid_delegate.h"
#include "content/public/browser/usb_delegate.h"
#include "content/public/browser/web_ui_url_loader_factory.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/url_constants.h"
#include "ipc/ipc_message.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/isolation_info.h"
#include "net/base/network_isolation_key.h"
#include "net/cookies/site_for_cookies.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/mojom/client_security_state.mojom.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/service_worker/embedded_worker_status.h"
#include "third_party/blink/public/mojom/loader/url_loader_factory_bundle.mojom.h"
#include "third_party/blink/public/mojom/renderer_preference_watcher.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_object.mojom.h"
#include "url/gurl.h"

#if !BUILDFLAG(IS_ANDROID)
#include "content/browser/hid/hid_service.h"
#endif

// TODO(crbug.com/40568315): Much of this file, which dealt with thread hops
// between UI and IO, can likely be simplified when the service worker core
// thread moves to the UI thread.

namespace content {

namespace {

// When a service worker version's failure count exceeds
// |kMaxSameProcessFailureCount|, the embedded worker is forced to start in a
// new process.
const int kMaxSameProcessFailureCount =;

const char kServiceWorkerTerminationCanceledMesage[] =;

bool HasSentStartWorker(EmbeddedWorkerInstance::StartingPhase phase) {}

void NotifyForegroundServiceWorker(bool added, int process_id) {}

}  // namespace

// Created when a renderer process is allocated for the worker. It is destroyed
// when the worker stops, and this proxies notifications to DevToolsManager.
// Owned by EmbeddedWorkerInstance.
//
// TODO(crbug.com/40725202): Remove this because we no longer need
// proxying the notifications because there's no thread hopping thanks to
// ServiceWorkerOnUI.
class EmbeddedWorkerInstance::DevToolsProxy {};

// A handle for a renderer process managed by ServiceWorkerProcessManager.
//
// TODO(crbug.com/40725202): Remove this as a clean up of
// ServiceWorkerOnUI.
class EmbeddedWorkerInstance::WorkerProcessHandle {};

// Info that is recorded as UMA on OnStarted().
struct EmbeddedWorkerInstance::StartInfo {};

EmbeddedWorkerInstance::~EmbeddedWorkerInstance() {}

void EmbeddedWorkerInstance::Start(
    blink::mojom::EmbeddedWorkerStartParamsPtr params,
    StatusCallback callback) {}

void EmbeddedWorkerInstance::Stop() {}

void EmbeddedWorkerInstance::StopIfNotAttachedToDevTools() {}

EmbeddedWorkerInstance::EmbeddedWorkerInstance(
    ServiceWorkerVersion* owner_version)
    :{}

void EmbeddedWorkerInstance::OnProcessAllocated(
    std::unique_ptr<WorkerProcessHandle> handle,
    ServiceWorkerMetrics::StartSituation start_situation) {}

void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager(
    std::unique_ptr<DevToolsProxy> devtools_proxy) {}

void EmbeddedWorkerInstance::SendStartWorker(
    blink::mojom::EmbeddedWorkerStartParamsPtr params) {}

void EmbeddedWorkerInstance::RequestTermination(
    RequestTerminationCallback callback) {}

void EmbeddedWorkerInstance::CountFeature(blink::mojom::WebFeature feature) {}

void EmbeddedWorkerInstance::OnReadyForInspection(
    mojo::PendingRemote<blink::mojom::DevToolsAgent> agent_remote,
    mojo::PendingReceiver<blink::mojom::DevToolsAgentHost> host_receiver) {}

void EmbeddedWorkerInstance::OnScriptLoaded() {}

void EmbeddedWorkerInstance::OnWorkerVersionInstalled() {}

void EmbeddedWorkerInstance::OnWorkerVersionDoomed() {}

void EmbeddedWorkerInstance::OnScriptEvaluationStart() {}

void EmbeddedWorkerInstance::OnStarted(
    blink::mojom::ServiceWorkerStartStatus start_status,
    blink::mojom::ServiceWorkerFetchHandlerType fetch_handler_type,
    bool has_hid_event_handlers,
    bool has_usb_event_handlers,
    int thread_id,
    blink::mojom::EmbeddedWorkerStartTimingPtr start_timing) {}

void EmbeddedWorkerInstance::OnStopped() {}

void EmbeddedWorkerInstance::Detach() {}

void EmbeddedWorkerInstance::UpdateForegroundPriority() {}

void EmbeddedWorkerInstance::UpdateLoaderFactories(
    std::unique_ptr<blink::PendingURLLoaderFactoryBundle> script_bundle,
    std::unique_ptr<blink::PendingURLLoaderFactoryBundle> subresource_bundle) {}

void EmbeddedWorkerInstance::BindCacheStorage(
    mojo::PendingReceiver<blink::mojom::CacheStorage> receiver,
    const storage::BucketLocator& bucket_locator) {}

#if !BUILDFLAG(IS_ANDROID)
void EmbeddedWorkerInstance::BindHidService(
    const url::Origin& origin,
    mojo::PendingReceiver<blink::mojom::HidService> receiver) {}
#endif  // !BUILDFLAG(IS_ANDROID)

void EmbeddedWorkerInstance::BindUsbService(
    const url::Origin& origin,
    mojo::PendingReceiver<blink::mojom::WebUsbService> receiver) {}

base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() {}

// Returns a factory bundle for doing loads on behalf of the specified |rph| and
// |origin|. The returned bundle has a default factory that goes to network and
// it may also include scheme-specific factories that don't go to network.
//
// The network factory does not support reconnection to the network service.
std::unique_ptr<blink::PendingURLLoaderFactoryBundle>
EmbeddedWorkerInstance::CreateFactoryBundle(
    RenderProcessHost* rph,
    int routing_id,
    const blink::StorageKey& storage_key,
    network::mojom::ClientSecurityStatePtr client_security_state,
    mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
        coep_reporter,
    ContentBrowserClient::URLLoaderFactoryType factory_type,
    const std::string& devtools_worker_token) {}

void EmbeddedWorkerInstance::SetPauseInitializingGlobalScope() {}

void EmbeddedWorkerInstance::ResumeInitializingGlobalScope() {}

void EmbeddedWorkerInstance::OnReportException(
    const std::u16string& error_message,
    int line_number,
    int column_number,
    const GURL& source_url) {}

void EmbeddedWorkerInstance::OnReportConsoleMessage(
    blink::mojom::ConsoleMessageSource source,
    blink::mojom::ConsoleMessageLevel message_level,
    const std::u16string& message,
    int line_number,
    const GURL& source_url) {}

int EmbeddedWorkerInstance::process_id() const {}

int EmbeddedWorkerInstance::worker_devtools_agent_route_id() const {}

base::UnguessableToken EmbeddedWorkerInstance::WorkerDevtoolsId() const {}

void EmbeddedWorkerInstance::AddObserver(Listener* listener) {}

void EmbeddedWorkerInstance::RemoveObserver(Listener* listener) {}

void EmbeddedWorkerInstance::SetDevToolsAttached(bool attached) {}

void EmbeddedWorkerInstance::OnNetworkAccessedForScriptLoad() {}

void EmbeddedWorkerInstance::ReleaseProcess() {}

void EmbeddedWorkerInstance::OnSetupFailed(
    StatusCallback callback,
    blink::ServiceWorkerStatusCode status) {}

// static
std::string EmbeddedWorkerInstance::StatusToString(
    blink::EmbeddedWorkerStatus status) {}

// static
std::string EmbeddedWorkerInstance::StartingPhaseToString(StartingPhase phase) {}

void EmbeddedWorkerInstance::NotifyForegroundServiceWorkerAdded() {}

void EmbeddedWorkerInstance::NotifyForegroundServiceWorkerRemoved() {}

mojo::PendingRemote<network::mojom::URLLoaderFactory>
EmbeddedWorkerInstance::MakeScriptLoaderFactoryRemote(
    std::unique_ptr<blink::PendingURLLoaderFactoryBundle> script_bundle) {}

void EmbeddedWorkerInstance::BindCacheStorageInternal() {}

mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
EmbeddedWorkerInstance::GetCoepReporter() {}

mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
EmbeddedWorkerInstance::GetCoepReporterInternal(
    StoragePartitionImpl* storage_partition) {}

EmbeddedWorkerInstance::CacheStorageRequest::CacheStorageRequest(
    mojo::PendingReceiver<blink::mojom::CacheStorage> receiver,
    storage::BucketLocator bucket)
    :{}

EmbeddedWorkerInstance::CacheStorageRequest::CacheStorageRequest(
    CacheStorageRequest&& other) = default;
EmbeddedWorkerInstance::CacheStorageRequest::~CacheStorageRequest() = default;

}  // namespace content