ladybird/Libraries/LibWeb/HTML/Worker.h
Timothy Flynn ea77092100
Some checks are pending
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
LibWeb: Begin implementing SharedWorker
Shared workers are essentially just workers that may be accessed from
scripts within the same origin. There are plenty of FIXMEs here (mostly
building on existing worker FIXMEs that are already in place), but this
lets us run the shared worker variants of WPT tests.
2025-05-02 17:48:02 -04:00

75 lines
2.5 KiB
C++

/*
* Copyright (c) 2022, Ben Abraham <ben.d.abraham@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/AbstractWorker.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/HTML/WorkerAgentParent.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
#define ENUMERATE_WORKER_EVENT_HANDLERS(E) \
E(onmessage, HTML::EventNames::message) \
E(onmessageerror, HTML::EventNames::messageerror)
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
class Worker
: public DOM::EventTarget
, public HTML::AbstractWorker {
WEB_PLATFORM_OBJECT(Worker, DOM::EventTarget);
GC_DECLARE_ALLOCATOR(Worker);
public:
static WebIDL::ExceptionOr<GC::Ref<Worker>> create(String const& script_url, WorkerOptions const& options, DOM::Document& document);
static WebIDL::ExceptionOr<GC::Ref<Worker>> construct_impl(JS::Realm& realm, String const& script_url, WorkerOptions const& options)
{
auto& window = as<HTML::Window>(realm.global_object());
return Worker::create(script_url, options, window.associated_document());
}
WebIDL::ExceptionOr<void> terminate();
WebIDL::ExceptionOr<void> post_message(JS::Value message, StructuredSerializeOptions const&);
WebIDL::ExceptionOr<void> post_message(JS::Value message, Vector<GC::Root<JS::Object>> const& transfer);
virtual ~Worker() = default;
GC::Ptr<MessagePort> outside_message_port() { return m_outside_port; }
void set_agent(WorkerAgentParent& agent) { m_agent = agent; }
#undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name) \
void set_##attribute_name(WebIDL::CallbackType*); \
WebIDL::CallbackType* attribute_name();
ENUMERATE_WORKER_EVENT_HANDLERS(__ENUMERATE)
#undef __ENUMERATE
protected:
Worker(String const&, WorkerOptions const&, DOM::Document&);
// ^AbstractWorker
virtual DOM::EventTarget& this_event_target() override { return *this; }
private:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
String m_script_url;
WorkerOptions m_options;
GC::Ptr<DOM::Document> m_document;
GC::Ptr<MessagePort> m_outside_port;
GC::Ptr<WorkerAgentParent> m_agent;
};
void run_a_worker(Variant<GC::Ref<Worker>, GC::Ref<SharedWorker>> worker, URL::URL& url, EnvironmentSettingsObject& outside_settings, GC::Ptr<MessagePort> outside_port, WorkerOptions const& options);
}