LibWeb/HTML: Hook up a WorkerAgent for web workers

This commit is contained in:
Shannon Booth 2025-04-24 15:22:25 +12:00 committed by Andreas Kling
commit 041ff0c7ff
Notes: github-actions[bot] 2025-04-25 14:45:27 +00:00
10 changed files with 78 additions and 12 deletions

View file

@ -40,6 +40,7 @@
#include <LibWeb/HTML/Scripting/SimilarOriginWindowAgent.h>
#include <LibWeb/HTML/Scripting/SyntheticRealmSettings.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/HTML/Scripting/WorkerAgent.h>
#include <LibWeb/HTML/ShadowRealmGlobalScope.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WindowProxy.h>
@ -72,15 +73,28 @@ HTML::Script* active_script()
});
}
void initialize_main_thread_vm(HTML::EventLoop::Type type)
static NonnullOwnPtr<JS::Agent> create_agent(GC::Heap& heap, AgentType type)
{
switch (type) {
case AgentType::SimilarOriginWindow:
return HTML::SimilarOriginWindowAgent::create(heap);
case AgentType::DedicatedWorker:
case AgentType::SharedWorker:
return HTML::WorkerAgent::create(heap, JS::Agent::CanBlock::Yes);
case AgentType::ServiceWorker:
return HTML::WorkerAgent::create(heap, JS::Agent::CanBlock::No);
default:
break;
}
VERIFY_NOT_REACHED();
}
void initialize_main_thread_vm(AgentType type)
{
VERIFY(!s_main_thread_vm);
s_main_thread_vm = JS::VM::create();
s_main_thread_vm->set_agent(HTML::SimilarOriginWindowAgent::create());
auto& agent = as<HTML::Agent>(*s_main_thread_vm->agent());
agent.event_loop = s_main_thread_vm->heap().allocate<HTML::EventLoop>(type);
s_main_thread_vm->set_agent(create_agent(s_main_thread_vm->heap(), type));
s_main_thread_vm->on_unimplemented_property_access = [](auto const& object, auto const& property_key) {
dbgln("FIXME: Unimplemented IDL interface: '{}.{}'", object.class_name(), property_key.to_string());

View file

@ -32,7 +32,15 @@ struct WebEngineCustomJobCallbackData final : public JS::JobCallback::CustomData
HTML::Script* active_script();
void initialize_main_thread_vm(HTML::EventLoop::Type);
enum class AgentType : u8 {
SimilarOriginWindow,
DedicatedWorker,
SharedWorker,
ServiceWorker,
Worklet,
};
void initialize_main_thread_vm(AgentType);
JS::VM& main_thread_vm();
void queue_mutation_observer_microtask(DOM::Document const&);

View file

@ -508,6 +508,7 @@ set(SOURCES
HTML/Scripting/SyntheticRealmSettings.cpp
HTML/Scripting/TemporaryExecutionContext.cpp
HTML/Scripting/WindowEnvironmentSettingsObject.cpp
HTML/Scripting/WorkerAgent.cpp
HTML/Scripting/WorkerEnvironmentSettingsObject.cpp
HTML/Scripting/SerializedEnvironmentSettingsObject.cpp
HTML/SelectedFile.cpp

View file

@ -11,10 +11,12 @@
namespace Web::HTML {
NonnullOwnPtr<SimilarOriginWindowAgent> SimilarOriginWindowAgent::create()
NonnullOwnPtr<SimilarOriginWindowAgent> SimilarOriginWindowAgent::create(GC::Heap& heap)
{
// See 'creating an agent' step in: https://html.spec.whatwg.org/multipage/webappapis.html#obtain-similar-origin-window-agent
return adopt_own(*new SimilarOriginWindowAgent(CanBlock::No));
auto agent = adopt_own(*new SimilarOriginWindowAgent(CanBlock::No));
agent->event_loop = heap.allocate<HTML::EventLoop>(HTML::EventLoop::Type::Window);
return agent;
}
// https://html.spec.whatwg.org/multipage/webappapis.html#relevant-agent

View file

@ -20,7 +20,7 @@ namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webappapis.html#similar-origin-window-agent
struct SimilarOriginWindowAgent : public Agent {
static NonnullOwnPtr<SimilarOriginWindowAgent> create();
static NonnullOwnPtr<SimilarOriginWindowAgent> create(GC::Heap&);
// https://dom.spec.whatwg.org/#mutation-observer-compound-microtask-queued-flag
// Each similar-origin window agent has a mutation observer microtask queued (a boolean), which is initially false. [HTML]

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Scripting/WorkerAgent.h>
namespace Web::HTML {
NonnullOwnPtr<WorkerAgent> WorkerAgent::create(GC::Heap& heap, CanBlock can_block)
{
auto agent = adopt_own(*new WorkerAgent(can_block));
agent->event_loop = heap.allocate<HTML::EventLoop>(HTML::EventLoop::Type::Worker);
return agent;
}
}

View file

@ -0,0 +1,22 @@
/*
* Copyright (c) 2025, Shannon Booth <shannon@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/HTML/Scripting/Agent.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/webappapis.html#dedicated-worker-agent
// https://html.spec.whatwg.org/multipage/webappapis.html#shared-worker-agent
struct WorkerAgent : public Agent {
static NonnullOwnPtr<WorkerAgent> create(GC::Heap&, CanBlock);
private:
using Agent::Agent;
};
}

View file

@ -17,7 +17,7 @@ struct Globals {
Globals::Globals()
{
Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Window);
Web::Bindings::initialize_main_thread_vm(Web::Bindings::AgentType::SimilarOriginWindow);
}
}

View file

@ -191,7 +191,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Web::Platform::FontPlugin::install(*new WebView::FontPlugin(is_layout_test_mode, &font_provider));
Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Window);
Web::Bindings::initialize_main_thread_vm(Web::Bindings::AgentType::SimilarOriginWindow);
if (collect_garbage_on_every_allocation)
Web::Bindings::main_thread_vm().heap().set_should_collect_on_every_allocation(true);

View file

@ -68,7 +68,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Web::Platform::FontPlugin::install(*new WebView::FontPlugin(false));
Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Worker);
Web::Bindings::initialize_main_thread_vm(Web::Bindings::AgentType::DedicatedWorker);
TRY(initialize_resource_loader(Web::Bindings::main_thread_vm().heap(), request_server_socket));