LibWeb+WebWorker: Create SharedWorkerGlobalScope for Shared Workers

Also push the onconnect event for the initial connection.

This still doesn't properly handle sending an onconnect event to a
pre-existing SharedWorkerGlobalScope with the same name for the same
origin, but it does give us a lot of WPT passes in the SharedWorker
category.
This commit is contained in:
Andrew Kaster 2025-05-18 14:10:01 -06:00 committed by Andrew Kaster
commit 4d039fc3d4
Notes: github-actions[bot] 2025-05-18 23:51:04 +00:00
11 changed files with 88 additions and 39 deletions

View file

@ -25,6 +25,15 @@ class SharedWorkerGlobalScope
public:
virtual ~SharedWorkerGlobalScope() override;
void set_constructor_origin(URL::Origin origin) { m_constructor_origin = move(origin); }
URL::Origin const& constructor_origin() const { return m_constructor_origin; }
void set_constructor_url(URL::URL url) { m_constructor_url = move(url); }
URL::URL const& constructor_url() const { return m_constructor_url; }
Fetch::Infrastructure::Request::CredentialsMode credentials() const { return m_credentials; }
void set_credentials(Fetch::Infrastructure::Request::CredentialsMode credentials) { m_credentials = credentials; }
void close();
#define __ENUMERATE(attribute_name, event_name) \
@ -38,6 +47,10 @@ private:
virtual void initialize_web_interfaces_impl() override;
virtual void finalize() override;
URL::Origin m_constructor_origin;
URL::URL m_constructor_url;
Fetch::Infrastructure::Request::CredentialsMode m_credentials;
};
HashTable<GC::RawRef<SharedWorkerGlobalScope>>& all_shared_worker_global_scopes();

View file

@ -93,7 +93,7 @@ WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(String const& script_url, Wo
void run_a_worker(Variant<GC::Ref<Worker>, GC::Ref<SharedWorker>> worker, URL::URL& url, EnvironmentSettingsObject& outside_settings, GC::Ptr<MessagePort> port, WorkerOptions const& options)
{
// 1. Let is shared be true if worker is a SharedWorker object, and false otherwise.
// FIXME: SharedWorker support
Bindings::AgentType agent_type = worker.has<GC::Ref<SharedWorker>>() ? Bindings::AgentType::SharedWorker : Bindings::AgentType::DedicatedWorker;
// 2. Let owner be the relevant owner to add given outside settings.
// FIXME: Support WorkerGlobalScope options
@ -111,7 +111,7 @@ void run_a_worker(Variant<GC::Ref<Worker>, GC::Ref<SharedWorker>> worker, URL::U
// and is shared. Run the rest of these steps in that agent.
// Note: This spawns a new process to act as the 'agent' for the worker.
auto agent = outside_settings.realm().create<WorkerAgentParent>(url, options, port, outside_settings);
auto agent = outside_settings.realm().create<WorkerAgentParent>(url, options, port, outside_settings, agent_type);
worker.visit([&](auto worker) { worker->set_agent(agent); });
}

View file

@ -14,8 +14,9 @@ namespace Web::HTML {
GC_DEFINE_ALLOCATOR(WorkerAgentParent);
WorkerAgentParent::WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings)
WorkerAgentParent::WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings, Bindings::AgentType agent_type)
: m_worker_options(options)
, m_agent_type(agent_type)
, m_url(move(url))
, m_outside_port(outside_port)
, m_outside_settings(outside_settings)
@ -34,7 +35,7 @@ void WorkerAgentParent::initialize(JS::Realm& realm)
// NOTE: This blocking IPC call may launch another process.
// If spinning the event loop for this can cause other javascript to execute, we're in trouble.
auto worker_socket_file = Bindings::principal_host_defined_page(realm).client().request_worker_agent(Bindings::AgentType::DedicatedWorker);
auto worker_socket_file = Bindings::principal_host_defined_page(realm).client().request_worker_agent(m_agent_type);
auto worker_socket = MUST(Core::LocalSocket::adopt_fd(worker_socket_file.take_fd()));
MUST(worker_socket->set_blocking(true));
@ -44,7 +45,7 @@ void WorkerAgentParent::initialize(JS::Realm& realm)
m_worker_ipc = make_ref_counted<WebWorkerClient>(move(transport));
m_worker_ipc->async_start_dedicated_worker(m_url, m_worker_options.type, m_worker_options.credentials, m_worker_options.name, move(data_holder), m_outside_settings->serialize());
m_worker_ipc->async_start_worker(m_url, m_worker_options.type, m_worker_options.credentials, m_worker_options.name, move(data_holder), m_outside_settings->serialize(), m_agent_type);
}
void WorkerAgentParent::visit_edges(Cell::Visitor& visitor)

View file

@ -6,6 +6,7 @@
#pragma once
#include <LibWeb/Bindings/AgentType.h>
#include <LibWeb/Bindings/RequestPrototype.h>
#include <LibWeb/Bindings/WorkerPrototype.h>
#include <LibWeb/Forward.h>
@ -25,12 +26,13 @@ class WorkerAgentParent : public JS::Cell {
GC_DECLARE_ALLOCATOR(WorkerAgentParent);
protected:
WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings);
WorkerAgentParent(URL::URL url, WorkerOptions const& options, GC::Ptr<MessagePort> outside_port, GC::Ref<EnvironmentSettingsObject> outside_settings, Bindings::AgentType);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
private:
WorkerOptions m_worker_options;
Bindings::AgentType m_agent_type { Bindings::AgentType::DedicatedWorker };
URL::URL m_url;
GC::Ptr<MessagePort> m_message_port;