mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-05 16:41:52 +00:00
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / 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
This change implements following behavior defined in the spec: https://html.spec.whatwg.org/multipage/web-messaging.html#examples-5 > The start() method, whether called explicitly or implicitly (by setting onmessage), starts the flow of messages: messages posted on message ports are initially paused, so that they don't get dropped on the floor before the script has had a chance to set up its handlers. Now we don't read bytes from the underlying transport socket until the message port transitions to the enabled state. This required the following places to explicitly enable the message port, because now, when it actually matters, we must do so, or otherwise sent messages will get stuck: - `onmessage` attribute setter in DedicatedWorkerGlobalScope, because it implicitly sets the onmessage handler for the worker's underlying port. - Stream API operations where the message port enabling steps were previously marked as FIXMEs.
64 lines
2.3 KiB
C++
64 lines
2.3 KiB
C++
/*
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibWeb/Bindings/PrincipalHostDefined.h>
|
|
#include <LibWeb/HTML/MessagePort.h>
|
|
#include <LibWeb/HTML/WorkerAgentParent.h>
|
|
#include <LibWeb/Page/Page.h>
|
|
#include <LibWeb/Worker/WebWorkerClient.h>
|
|
|
|
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, 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)
|
|
{
|
|
}
|
|
|
|
void WorkerAgentParent::initialize(JS::Realm& realm)
|
|
{
|
|
Base::initialize(realm);
|
|
|
|
m_message_port = MessagePort::create(realm);
|
|
m_message_port->entangle_with(*m_outside_port);
|
|
|
|
TransferDataHolder data_holder;
|
|
MUST(m_message_port->transfer_steps(data_holder));
|
|
|
|
// FIXME: Specification says this supposed to happen in step 11 of onComplete handler defined in https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
|
|
// but that would require introducing a new IPC message type to communicate this from WebWorker to WebContent process,
|
|
// so let's do it here for now.
|
|
m_outside_port->start();
|
|
|
|
// 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(m_agent_type);
|
|
|
|
auto worker_socket = MUST(Core::LocalSocket::adopt_fd(worker_socket_file.take_fd()));
|
|
MUST(worker_socket->set_blocking(true));
|
|
|
|
// TODO: Mach IPC
|
|
auto transport = make<IPC::Transport>(move(worker_socket));
|
|
|
|
m_worker_ipc = make_ref_counted<WebWorkerClient>(move(transport));
|
|
|
|
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)
|
|
{
|
|
Base::visit_edges(visitor);
|
|
visitor.visit(m_message_port);
|
|
visitor.visit(m_outside_port);
|
|
visitor.visit(m_outside_settings);
|
|
}
|
|
|
|
}
|