#include "third_party/blink/renderer/core/workers/worker_thread.h"
#include <limits>
#include <memory>
#include <utility>
#include "base/metrics/histogram_functions.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/single_thread_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "third_party/blink/public/common/loader/worker_main_script_load_parameters.h"
#include "third_party/blink/public/mojom/frame/lifecycle.mojom-shared.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/frame/policy_container.h"
#include "third_party/blink/renderer/core/inspector/console_message_storage.h"
#include "third_party/blink/renderer/core/inspector/inspector_issue_storage.h"
#include "third_party/blink/renderer/core/inspector/inspector_task_runner.h"
#include "third_party/blink/renderer/core/inspector/worker_devtools_params.h"
#include "third_party/blink/renderer/core/inspector/worker_inspector_controller.h"
#include "third_party/blink/renderer/core/inspector/worker_thread_debugger.h"
#include "third_party/blink/renderer/core/loader/worker_resource_timing_notifier_impl.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/workers/cross_thread_global_scope_creation_params_copier.h"
#include "third_party/blink/renderer/core/workers/global_scope_creation_params.h"
#include "third_party/blink/renderer/core/workers/worker_backing_thread.h"
#include "third_party/blink/renderer/core/workers/worker_clients.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_client_settings_object_snapshot.h"
#include "third_party/blink/renderer/platform/loader/fetch/worker_resource_timing_notifier.h"
#include "third_party/blink/renderer/platform/scheduler/public/event_loop.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/scheduler/public/worker_scheduler.h"
#include "third_party/blink/renderer/platform/scheduler/worker/non_main_thread_impl.h"
#include "third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_std.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"
namespace blink {
ExitCode;
namespace {
constexpr base::TimeDelta kForcibleTerminationDelay = …;
}
base::Lock& WorkerThread::ThreadSetLock() { … }
static std::atomic_int g_unique_worker_thread_id(1);
static int GetNextWorkerThreadId() { … }
class WorkerThread::RefCountedWaitableEvent
: public WTF::ThreadSafeRefCounted<RefCountedWaitableEvent> { … };
class WorkerThread::InterruptData { … };
WorkerThread::~WorkerThread() { … }
void WorkerThread::Start(
std::unique_ptr<GlobalScopeCreationParams> global_scope_creation_params,
const std::optional<WorkerBackingThreadStartupData>& thread_startup_data,
std::unique_ptr<WorkerDevToolsParams> devtools_params) { … }
void WorkerThread::EvaluateClassicScript(
const KURL& script_url,
const String& source_code,
std::unique_ptr<Vector<uint8_t>> cached_meta_data,
const v8_inspector::V8StackTraceId& stack_id) { … }
void WorkerThread::FetchAndRunClassicScript(
const KURL& script_url,
std::unique_ptr<WorkerMainScriptLoadParameters>
worker_main_script_load_params,
std::unique_ptr<WebPolicyContainer> policy_container,
std::unique_ptr<CrossThreadFetchClientSettingsObjectData>
outside_settings_object_data,
WorkerResourceTimingNotifier* outside_resource_timing_notifier,
const v8_inspector::V8StackTraceId& stack_id) { … }
void WorkerThread::FetchAndRunModuleScript(
const KURL& script_url,
std::unique_ptr<WorkerMainScriptLoadParameters>
worker_main_script_load_params,
std::unique_ptr<WebPolicyContainer> policy_container,
std::unique_ptr<CrossThreadFetchClientSettingsObjectData>
outside_settings_object_data,
WorkerResourceTimingNotifier* outside_resource_timing_notifier,
network::mojom::CredentialsMode credentials_mode,
RejectCoepUnsafeNone reject_coep_unsafe_none) { … }
void WorkerThread::Pause() { … }
void WorkerThread::Freeze(bool is_in_back_forward_cache) { … }
void WorkerThread::Resume() { … }
void WorkerThread::Terminate() { … }
void WorkerThread::TerminateForTesting() { … }
void WorkerThread::WillProcessTask(const base::PendingTask& pending_task,
bool was_blocked_or_low_priority) { … }
void WorkerThread::DidProcessTask(const base::PendingTask& pending_task) { … }
v8::Isolate* WorkerThread::GetIsolate() { … }
bool WorkerThread::IsCurrentThread() { … }
void WorkerThread::DebuggerTaskStarted() { … }
void WorkerThread::DebuggerTaskFinished() { … }
WorkerOrWorkletGlobalScope* WorkerThread::GlobalScope() { … }
WorkerInspectorController* WorkerThread::GetWorkerInspectorController() { … }
unsigned WorkerThread::WorkerThreadCount() { … }
HashSet<WorkerThread*>& WorkerThread::InitializingWorkerThreads() { … }
HashSet<WorkerThread*>& WorkerThread::WorkerThreads() { … }
bool WorkerThread::IsForciblyTerminated() { … }
void WorkerThread::WaitForShutdownForTesting() { … }
ExitCode WorkerThread::GetExitCodeForTesting() { … }
scheduler::WorkerScheduler* WorkerThread::GetScheduler() { … }
scoped_refptr<base::SingleThreadTaskRunner> WorkerThread::GetTaskRunner(
TaskType type) { … }
void WorkerThread::ChildThreadStartedOnWorkerThread(WorkerThread* child) { … }
void WorkerThread::ChildThreadTerminatedOnWorkerThread(WorkerThread* child) { … }
WorkerThread::WorkerThread(WorkerReportingProxy& worker_reporting_proxy)
: … { … }
WorkerThread::WorkerThread(WorkerReportingProxy& worker_reporting_proxy,
scoped_refptr<base::SingleThreadTaskRunner>
parent_thread_default_task_runner)
: … { … }
void WorkerThread::ScheduleToTerminateScriptExecution() { … }
WorkerThread::TerminationState WorkerThread::ShouldTerminateScriptExecution() { … }
void WorkerThread::EnsureScriptExecutionTerminates(ExitCode exit_code) { … }
void WorkerThread::InitializeSchedulerOnWorkerThread(
base::WaitableEvent* waitable_event) { … }
void WorkerThread::InitializeOnWorkerThread(
std::unique_ptr<GlobalScopeCreationParams> global_scope_creation_params,
const std::optional<WorkerBackingThreadStartupData>& thread_startup_data,
std::unique_ptr<WorkerDevToolsParams> devtools_params) { … }
void WorkerThread::EvaluateClassicScriptOnWorkerThread(
const KURL& script_url,
String source_code,
std::unique_ptr<Vector<uint8_t>> cached_meta_data,
const v8_inspector::V8StackTraceId& stack_id) { … }
void WorkerThread::FetchAndRunClassicScriptOnWorkerThread(
const KURL& script_url,
std::unique_ptr<WorkerMainScriptLoadParameters>
worker_main_script_load_params,
std::unique_ptr<WebPolicyContainer> policy_container,
std::unique_ptr<CrossThreadFetchClientSettingsObjectData>
outside_settings_object,
WorkerResourceTimingNotifier* outside_resource_timing_notifier,
const v8_inspector::V8StackTraceId& stack_id) { … }
void WorkerThread::FetchAndRunModuleScriptOnWorkerThread(
const KURL& script_url,
std::unique_ptr<WorkerMainScriptLoadParameters>
worker_main_script_load_params,
std::unique_ptr<WebPolicyContainer> policy_container,
std::unique_ptr<CrossThreadFetchClientSettingsObjectData>
outside_settings_object,
WorkerResourceTimingNotifier* outside_resource_timing_notifier,
network::mojom::CredentialsMode credentials_mode,
bool reject_coep_unsafe_none) { … }
void WorkerThread::PrepareForShutdownOnWorkerThread() { … }
void WorkerThread::PerformShutdownOnWorkerThread() { … }
void WorkerThread::SetThreadState(ThreadState next_thread_state) { … }
void WorkerThread::SetExitCode(ExitCode exit_code) { … }
bool WorkerThread::CheckRequestedToTerminate() { … }
void WorkerThread::PauseOrFreeze(mojom::blink::FrameLifecycleState state,
bool is_in_back_forward_cache) { … }
void WorkerThread::PauseOrFreezeOnWorkerThread(
mojom::blink::FrameLifecycleState state,
bool is_in_back_forward_cache) { … }
void WorkerThread::ResumeOnWorkerThread() { … }
void WorkerThread::PauseOrFreezeWithInterruptDataOnWorkerThread(
InterruptData* interrupt_data) { … }
void WorkerThread::PauseOrFreezeInsideV8InterruptOnWorkerThread(v8::Isolate*,
void* data) { … }
void WorkerThread::PauseOrFreezeInsidePostTaskOnWorkerThread(
InterruptData* interrupt_data) { … }
}