mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:17:46 +00:00
LibWeb+WebWorker: Implement a first cut of post_message for Workers
This implementation completely ignores MessagePorts, and manually plumbs data through LocalSockets.
This commit is contained in:
parent
05ec93e276
commit
1602663b9e
15 changed files with 225 additions and 29 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <LibJS/Runtime/Realm.h>
|
||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||
#include <LibWeb/HTML/Scripting/Environments.h>
|
||||
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
|
||||
#include <LibWeb/HTML/Worker.h>
|
||||
#include <LibWeb/HTML/WorkerDebugConsoleClient.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
@ -109,6 +110,54 @@ void Worker::run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_setti
|
|||
|
||||
// Note: This spawns a new process to act as the 'agent' for the worker.
|
||||
m_agent = heap().allocate_without_realm<WorkerAgent>(url, options);
|
||||
|
||||
auto& socket = m_agent->socket();
|
||||
// FIXME: Hide this logic in MessagePort
|
||||
socket.set_notifications_enabled(true);
|
||||
socket.on_ready_to_read = [this] {
|
||||
auto& socket = this->m_agent->socket();
|
||||
auto& vm = this->vm();
|
||||
auto& realm = this->realm();
|
||||
|
||||
auto num_bytes_ready = MUST(socket.pending_bytes());
|
||||
switch (m_outside_port_state) {
|
||||
case PortState::Header: {
|
||||
if (num_bytes_ready < 8)
|
||||
break;
|
||||
auto const magic = MUST(socket.read_value<u32>());
|
||||
if (magic != 0xDEADBEEF) {
|
||||
m_outside_port_state = PortState::Error;
|
||||
break;
|
||||
}
|
||||
m_outside_port_incoming_message_size = MUST(socket.read_value<u32>());
|
||||
num_bytes_ready -= 8;
|
||||
m_outside_port_state = PortState::Data;
|
||||
}
|
||||
[[fallthrough]];
|
||||
case PortState::Data: {
|
||||
if (num_bytes_ready < m_outside_port_incoming_message_size)
|
||||
break;
|
||||
SerializationRecord rec; // FIXME: Keep in class scope
|
||||
rec.resize(m_outside_port_incoming_message_size / sizeof(u32));
|
||||
|
||||
MUST(socket.read_until_filled(to_bytes(rec.span())));
|
||||
|
||||
TemporaryExecutionContext cxt(relevant_settings_object(*this));
|
||||
VERIFY(&realm == vm.current_realm());
|
||||
MessageEventInit event_init {};
|
||||
event_init.data = MUST(structured_deserialize(vm, rec, realm, {}));
|
||||
// FIXME: Fill in the rest of the info from MessagePort
|
||||
|
||||
this->dispatch_event(MessageEvent::create(realm, EventNames::message, event_init));
|
||||
|
||||
m_outside_port_state = PortState::Header;
|
||||
break;
|
||||
}
|
||||
case PortState::Error:
|
||||
VERIFY_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate
|
||||
|
@ -120,16 +169,29 @@ WebIDL::ExceptionOr<void> Worker::terminate()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
|
||||
void Worker::post_message(JS::Value message, JS::Value)
|
||||
WebIDL::ExceptionOr<void> Worker::post_message(JS::Value message, JS::Value)
|
||||
{
|
||||
dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Post Message: {}", message.to_string_without_side_effects());
|
||||
|
||||
// 1. Let targetPort be the port with which this is entangled, if any; otherwise let it be null.
|
||||
auto& target_port = m_outside_port;
|
||||
// FIXME: 1. Let targetPort be the port with which this is entangled, if any; otherwise let it be null.
|
||||
// FIXME: 2. Let options be «[ "transfer" → transfer ]».
|
||||
// FIXME: 3. Run the message port post message steps providing this, targetPort, message and options.
|
||||
|
||||
// 2. Let options be «[ "transfer" → transfer ]».
|
||||
// 3. Run the message port post message steps providing this, targetPort, message and options.
|
||||
target_port->post_message(message);
|
||||
auto& realm = this->realm();
|
||||
auto& vm = this->vm();
|
||||
|
||||
// FIXME: Use the with-transfer variant, which should(?) prepend the magic + size at the front
|
||||
auto data = TRY(structured_serialize(vm, message));
|
||||
|
||||
Array<u32, 2> header = { 0xDEADBEEF, static_cast<u32>(data.size() * sizeof(u32)) };
|
||||
|
||||
if (auto const err = m_agent->socket().write_until_depleted(to_readonly_bytes(header.span())); err.is_error())
|
||||
return WebIDL::DataCloneError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted("{}", err.error())));
|
||||
|
||||
if (auto const err = m_agent->socket().write_until_depleted(to_readonly_bytes(data.span())); err.is_error())
|
||||
return WebIDL::DataCloneError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted("{}", err.error())));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
#undef __ENUMERATE
|
||||
|
|
|
@ -41,7 +41,7 @@ public:
|
|||
|
||||
WebIDL::ExceptionOr<void> terminate();
|
||||
|
||||
void post_message(JS::Value message, JS::Value transfer);
|
||||
WebIDL::ExceptionOr<void> post_message(JS::Value message, JS::Value transfer);
|
||||
|
||||
virtual ~Worker() = default;
|
||||
|
||||
|
@ -66,6 +66,13 @@ private:
|
|||
|
||||
JS::GCPtr<DOM::Document> m_document;
|
||||
JS::GCPtr<MessagePort> m_outside_port;
|
||||
// FIXME: Move tihs state into the message port (and actually use it :) )
|
||||
enum class PortState : u8 {
|
||||
Header,
|
||||
Data,
|
||||
Error,
|
||||
} m_outside_port_state { PortState::Header };
|
||||
size_t m_outside_port_incoming_message_size { 0 };
|
||||
|
||||
JS::GCPtr<WorkerAgent> m_agent;
|
||||
|
||||
|
|
|
@ -109,7 +109,8 @@ WorkerAgent::WorkerAgent(AK::URL url, WorkerOptions const& options)
|
|||
|
||||
int fds[2] = {};
|
||||
MUST(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fds));
|
||||
m_message_port_fd = fds[0];
|
||||
|
||||
m_socket = MUST(Core::BufferedLocalSocket::create(MUST(Core::LocalSocket::adopt_fd(fds[0]))));
|
||||
|
||||
m_worker_ipc->async_start_dedicated_worker(m_url, options.type, options.credentials, options.name, fds[1]);
|
||||
}
|
||||
|
|
|
@ -6,11 +6,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefCounted.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/HTML/MessageEvent.h>
|
||||
#include <LibWeb/HTML/MessagePort.h>
|
||||
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
||||
#include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/Worker/WebWorkerClient.h>
|
||||
|
@ -31,12 +30,14 @@ struct WorkerAgent : JS::Cell {
|
|||
|
||||
RefPtr<Web::HTML::WebWorkerClient> m_worker_ipc;
|
||||
|
||||
Core::BufferedLocalSocket& socket() const { return *m_socket; }
|
||||
|
||||
private:
|
||||
WorkerOptions m_worker_options;
|
||||
AK::URL m_url;
|
||||
|
||||
// TODO: associate with MessagePorts?
|
||||
int m_message_port_fd;
|
||||
// FIXME: associate with MessagePorts
|
||||
OwnPtr<Core::BufferedLocalSocket> m_socket;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -4,14 +4,17 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Array.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibWeb/Bindings/DedicatedWorkerExposedInterfaces.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/Intrinsics.h>
|
||||
#include <LibWeb/Bindings/WorkerGlobalScopePrototype.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/HTML/EventHandler.h>
|
||||
#include <LibWeb/HTML/EventNames.h>
|
||||
#include <LibWeb/HTML/MessageEvent.h>
|
||||
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
|
||||
#include <LibWeb/HTML/StructuredSerialize.h>
|
||||
#include <LibWeb/HTML/WorkerGlobalScope.h>
|
||||
#include <LibWeb/HTML/WorkerLocation.h>
|
||||
#include <LibWeb/HTML/WorkerNavigator.h>
|
||||
|
@ -52,6 +55,55 @@ void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
|
|||
visitor.visit(m_navigator);
|
||||
}
|
||||
|
||||
void WorkerGlobalScope::set_outside_port(NonnullOwnPtr<Core::BufferedLocalSocket> port)
|
||||
{
|
||||
m_outside_port = move(port);
|
||||
|
||||
// FIXME: Hide this logic in MessagePort
|
||||
m_outside_port->set_notifications_enabled(true);
|
||||
m_outside_port->on_ready_to_read = [this] {
|
||||
auto& vm = this->vm();
|
||||
auto& realm = this->realm();
|
||||
|
||||
auto num_bytes_ready = MUST(m_outside_port->pending_bytes());
|
||||
switch (m_outside_port_state) {
|
||||
case PortState::Header: {
|
||||
if (num_bytes_ready < 8)
|
||||
break;
|
||||
auto const magic = MUST(m_outside_port->read_value<u32>());
|
||||
if (magic != 0xDEADBEEF) {
|
||||
m_outside_port_state = PortState::Error;
|
||||
break;
|
||||
}
|
||||
m_outside_port_incoming_message_size = MUST(m_outside_port->read_value<u32>());
|
||||
num_bytes_ready -= 8;
|
||||
m_outside_port_state = PortState::Data;
|
||||
}
|
||||
[[fallthrough]];
|
||||
case PortState::Data: {
|
||||
if (num_bytes_ready < m_outside_port_incoming_message_size)
|
||||
break;
|
||||
SerializationRecord rec; // FIXME: Keep in class scope
|
||||
rec.resize(m_outside_port_incoming_message_size / sizeof(u32));
|
||||
MUST(m_outside_port->read_until_filled(to_bytes(rec.span())));
|
||||
|
||||
TemporaryExecutionContext cxt(relevant_settings_object(*this));
|
||||
MessageEventInit event_init {};
|
||||
event_init.data = MUST(structured_deserialize(vm, rec, realm, {}));
|
||||
// FIXME: Fill in the rest of the info from MessagePort
|
||||
|
||||
this->dispatch_event(MessageEvent::create(realm, EventNames::message, event_init));
|
||||
|
||||
m_outside_port_state = PortState::Header;
|
||||
break;
|
||||
}
|
||||
case PortState::Error:
|
||||
VERIFY_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
|
||||
WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> urls)
|
||||
{
|
||||
|
@ -94,6 +146,25 @@ JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const
|
|||
return *m_navigator;
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<void> WorkerGlobalScope::post_message(JS::Value message, JS::Value)
|
||||
{
|
||||
auto& realm = this->realm();
|
||||
auto& vm = this->vm();
|
||||
|
||||
// FIXME: Use the with-transfer variant, which should(?) prepend the magic + size at the front
|
||||
auto data = TRY(structured_serialize(vm, message));
|
||||
|
||||
Array<u32, 2> header = { 0xDEADBEEF, static_cast<u32>(data.size() * sizeof(u32)) };
|
||||
|
||||
if (auto const err = m_outside_port->write_until_depleted(to_readonly_bytes(header.span())); err.is_error())
|
||||
return WebIDL::DataCloneError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted("{}", err.error())));
|
||||
|
||||
if (auto const err = m_outside_port->write_until_depleted(to_readonly_bytes(data.span())); err.is_error())
|
||||
return WebIDL::DataCloneError::create(realm, TRY_OR_THROW_OOM(vm, String::formatted("{}", err.error())));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
#undef __ENUMERATE
|
||||
#define __ENUMERATE(attribute_name, event_name) \
|
||||
void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/Optional.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/Socket.h>
|
||||
#include <LibWeb/DOM/EventTarget.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
|
||||
|
@ -16,13 +17,16 @@
|
|||
#include <LibWeb/HTML/WorkerNavigator.h>
|
||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||
|
||||
#define ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(E) \
|
||||
E(onerror, HTML::EventNames::error) \
|
||||
E(onlanguagechange, HTML::EventNames::languagechange) \
|
||||
E(ononline, HTML::EventNames::online) \
|
||||
E(onoffline, HTML::EventNames::offline) \
|
||||
E(onrejectionhandled, HTML::EventNames::rejectionhandled) \
|
||||
E(onunhandledrejection, HTML::EventNames::unhandledrejection)
|
||||
// FIXME: message/messageerror belong on subclasses only
|
||||
#define ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(E) \
|
||||
E(onerror, HTML::EventNames::error) \
|
||||
E(onlanguagechange, HTML::EventNames::languagechange) \
|
||||
E(ononline, HTML::EventNames::online) \
|
||||
E(onoffline, HTML::EventNames::offline) \
|
||||
E(onrejectionhandled, HTML::EventNames::rejectionhandled) \
|
||||
E(onunhandledrejection, HTML::EventNames::unhandledrejection) \
|
||||
E(onmessage, HTML::EventNames::message) \
|
||||
E(onmessageerror, HTML::EventNames::messageerror)
|
||||
|
||||
namespace Web::HTML {
|
||||
|
||||
|
@ -69,6 +73,8 @@ public:
|
|||
ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
|
||||
#undef __ENUMERATE
|
||||
|
||||
WebIDL::ExceptionOr<void> post_message(JS::Value message, JS::Value transfer);
|
||||
|
||||
// Non-IDL public methods
|
||||
|
||||
AK::URL const& url() const { return m_url.value(); }
|
||||
|
@ -78,6 +84,8 @@ public:
|
|||
// this is not problematic as it cannot be observed from script.
|
||||
void set_location(JS::NonnullGCPtr<WorkerLocation> loc) { m_location = move(loc); }
|
||||
|
||||
void set_outside_port(NonnullOwnPtr<Core::BufferedLocalSocket> port);
|
||||
|
||||
void initialize_web_interfaces(Badge<WorkerEnvironmentSettingsObject>);
|
||||
|
||||
Web::Page* page() { return &m_page; }
|
||||
|
@ -91,6 +99,14 @@ private:
|
|||
JS::GCPtr<WorkerLocation> m_location;
|
||||
JS::GCPtr<WorkerNavigator> m_navigator;
|
||||
|
||||
OwnPtr<Core::BufferedLocalSocket> m_outside_port;
|
||||
enum class PortState : u8 {
|
||||
Header,
|
||||
Data,
|
||||
Error,
|
||||
} m_outside_port_state { PortState::Header };
|
||||
size_t m_outside_port_incoming_message_size { 0 };
|
||||
|
||||
// FIXME: Add all these internal slots
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/workers.html#concept-WorkerGlobalScope-owner-set
|
||||
|
|
|
@ -18,6 +18,11 @@ interface WorkerGlobalScope : EventTarget {
|
|||
attribute EventHandler ononline;
|
||||
attribute EventHandler onrejectionhandled;
|
||||
attribute EventHandler onunhandledrejection;
|
||||
|
||||
// FIXME: This belongs on the subclasses of WorkerGlobalScope
|
||||
undefined postMessage(any message, any transfer);
|
||||
attribute EventHandler onmessage;
|
||||
attribute EventHandler onmessageerror;
|
||||
};
|
||||
|
||||
WorkerGlobalScope includes WindowOrWorkerGlobalScope;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue