Skip to content

Truly independent web browser

LibWeb: Create MessageEvents and add event listeners from Rust

Shannon Booth3ec94850eeeb
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
index 0a02f2bc437..ea9b00f0578 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
@@ -13,8 +13,9 @@ use core::ffi::c_void;
use core::ptr::NonNull;
use ak::Utf16String;
-use libjs_rust::api::{Gc, Realm, Trace, Value};
+use libjs_rust::api::{FunctionObject, Gc, Object, Realm, Trace, Value};
+use crate::bindings::CallContext;
use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::{Interface, Ref, Wrappable, cell_from_abi, cell_into_abi};
@@ -28,6 +29,12 @@ unsafe extern "C" {
);
fn web_bindings_event_handler_attribute(event_target: *mut c_void, name: ak::Span<'_, u8>) -> u64;
fn web_bindings_set_event_handler_attribute(event_target: *mut c_void, name: ak::Span<'_, u8>, value: u64);
+ fn web_bindings_add_event_listener(
+ realm: *mut c_void,
+ event_target: *mut c_void,
+ r#type: ak::Span<'_, u8>,
+ callback: *mut c_void,
+ );
fn web_bindings_create_event(
realm: *mut c_void,
r#type: ak::Span<'_, u8>,
@@ -42,7 +49,16 @@ unsafe extern "C" {
cancelable: bool,
composed: bool,
) -> *mut c_void;
+ fn web_bindings_create_message_event(
+ realm: *mut c_void,
+ r#type: ak::Span<'_, u8>,
+ data: u64,
+ ports: *const *mut c_void,
+ count: usize,
+ ) -> *mut c_void;
+ fn web_bindings_message_event_data(event: u64) -> u64;
fn web_bindings_set_event_owner(event: *mut c_void, interface_name: u16, owner: *mut c_void);
+ fn web_bindings_event_set_is_trusted(event: *mut c_void, is_trusted: bool);
fn web_bindings_dispatch_event(target: *mut c_void, event: *mut c_void) -> bool;
fn web_bindings_dispatch_event_with_legacy_output_did_listeners_throw_flag(
target: *mut c_void,
@@ -90,6 +106,20 @@ impl CppEventTarget {
self.0.as_ptr()
}
+ /// Adds an event listener for the event type whose callback is the function, with no options,
+ /// https://dom.spec.whatwg.org/#add-an-event-listener
+ pub(crate) fn add_event_listener(&self, cx: &CallContext, event_type: &str, callback: Gc<FunctionObject>) {
+ // SAFETY: The realm, the EventTarget and the callback are live, and the type is lent for the call.
+ unsafe {
+ web_bindings_add_event_listener(
+ cx.realm().as_ptr().cast(),
+ self.as_ptr(),
+ ak::Span::new(event_type.as_bytes()),
+ callback.as_ptr().cast(),
+ );
+ }
+ }
+
/// https://dom.spec.whatwg.org/#concept-event-dispatch
pub(crate) fn dispatch_event(&self, event: &CppEvent) -> bool {
// SAFETY: The EventTarget and the event are live.
@@ -143,6 +173,23 @@ impl CppEvent {
})
}
+ /// A MessageEvent in the realm, https://html.spec.whatwg.org/multipage/comms.html#messageevent, with its data
+ /// attribute initialized to the data and its ports attribute initialized to the MessagePorts that the objects are.
+ pub(crate) fn create_message_event(realm: Gc<Realm>, r#type: &str, data: Value, ports: &[Gc<Object>]) -> Self {
+ let ports: Vec<*mut c_void> = ports.iter().map(|port| port.as_ptr().cast()).collect();
+ // SAFETY: The realm, the data and the ports are live, the type and the ports are lent for the call, and LibWeb
+ // returns a live event.
+ Self(unsafe {
+ CppCell::from_raw(web_bindings_create_message_event(
+ realm.as_ptr().cast(),
+ ak::Span::new(r#type.as_bytes()),
+ data.to_raw(),
+ ports.as_ptr(),
+ ports.len(),
+ ))
+ })
+ }
+
pub(crate) fn set_owner<T: Interface>(&self, owner: Ref<T>) {
// SAFETY: The event and the owner are live, and the owner keeps the event alive.
unsafe { web_bindings_set_event_owner(self.as_ptr(), T::INTERFACE_NAME, cell_into_abi(owner)) };
@@ -152,6 +199,12 @@ impl CppEvent {
self.0.as_ptr()
}
+ /// https://dom.spec.whatwg.org/#dom-event-istrusted
+ pub(crate) fn set_is_trusted(&self, is_trusted: bool) {
+ // SAFETY: The event is live.
+ unsafe { web_bindings_event_set_is_trusted(self.as_ptr(), is_trusted) };
+ }
+
/// https://dom.spec.whatwg.org/#canceled-flag
pub(crate) fn canceled(&self) -> bool {
// SAFETY: The event is live.
@@ -159,6 +212,12 @@ impl CppEvent {
}
}
+/// The data attribute of a MessageEvent, which the value is a wrapper of.
+pub(crate) fn message_event_data(event: Value) -> Value {
+ // SAFETY: The event is live, and LibWeb returns its data, which is live.
+ unsafe { Value::from_raw(web_bindings_message_event_data(event.to_raw())) }
+}
+
fn event_target_of(object: &impl Wrappable) -> *mut c_void {
object
.cpp_parent()
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
index e2a41c5fd92..7ea7bca6f5a 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/promise.rs
@@ -68,6 +68,7 @@ unsafe extern "C" {
pub(crate) enum TaskSource {
Unspecified = 0,
Networking = 3,
+ PostedMessage = 6,
FileReading = 13,
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs
index 635404184f3..a9829cb9dd2 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/cross_realm_transform.rs
@@ -17,6 +17,7 @@ use libjs_rust::api::{
};
use crate::bindings::cpp_cell::CppCell;
+use crate::bindings::event_target::message_event_data;
use crate::bindings::interface::{Ref, allocate};
use crate::bindings::promise::{
NativeSteps, WebIdlPromise, create_promise, create_rejected_promise_from_exception, create_resolved_promise,
@@ -52,9 +53,6 @@ unsafe extern "C" {
on_message_error: *mut c_void,
);
- /// The data of a MessageEvent, whose wrapper the handlers of a port are called with.
- fn web_streams_message_event_data(event: u64) -> u64;
-
/// Runs the message port post message steps providing the port with which the port is entangled, if any, the
/// message and «[ "transfer" → « » ]».
fn web_streams_message_port_post_message(
@@ -121,8 +119,8 @@ impl CrossRealmTransformPort {
},
|vm: &Vm, function: &NativeSteps<(C, Gc<Realm>), MessageSteps<C>>| {
let (state, realm) = function.captures;
- // SAFETY: The port's message event handler is called with a MessageEvent, whose data is live.
- let data = unsafe { Value::from_raw(web_streams_message_event_data(vm.argument(0).to_raw())) };
+ // NB: The port's message event handler is called with a MessageEvent.
+ let data = message_event_data(vm.argument(0));
(function.steps)(&CallContext::new(vm, realm), state, data);
Ok(Value::UNDEFINED)
},
diff --git a/Libraries/LibWeb/Bindings/RustEventTarget.cpp b/Libraries/LibWeb/Bindings/RustEventTarget.cpp
index a5a2797ba66..0a66d54e3b9 100644
--- a/Libraries/LibWeb/Bindings/RustEventTarget.cpp
+++ b/Libraries/LibWeb/Bindings/RustEventTarget.cpp
@@ -10,6 +10,9 @@
#include <LibWeb/Bindings/RustEventTarget.h>
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/DOM/EventDispatcher.h>
+#include <LibWeb/DOM/IDLEventListener.h>
+#include <LibWeb/HTML/MessageEvent.h>
+#include <LibWeb/HTML/MessagePort.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/WebIDL/CallbackType.h>
@@ -100,9 +103,13 @@ RustEventTarget* web_bindings_create_event_target();
void web_bindings_set_event_target_owner(RustEventTarget*, u16 interface_name, RustInterfaceCell* owner, RustEventTarget::GetTheParent);
JSValue web_bindings_event_handler_attribute(RustEventTarget*, ReadonlyBytes name);
void web_bindings_set_event_handler_attribute(RustEventTarget*, ReadonlyBytes name, JSValue);
+void web_bindings_add_event_listener(void* realm, DOM::EventTarget*, ReadonlyBytes type, JSObject* callback);
DOM::Event* web_bindings_create_event(void* realm, ReadonlyBytes type, bool bubbles, bool cancelable, bool composed);
RustEvent* web_bindings_create_rust_event(void* realm, FlatPtr type, bool bubbles, bool cancelable, bool composed);
+DOM::Event* web_bindings_create_message_event(void* realm, ReadonlyBytes type, JSValue data, JSObject* const* ports, size_t count);
+JSValue web_bindings_message_event_data(JSValue event);
void web_bindings_set_event_owner(RustEvent*, u16 interface_name, RustInterfaceCell* owner);
+void web_bindings_event_set_is_trusted(DOM::Event*, bool is_trusted);
bool web_bindings_dispatch_event(DOM::EventTarget*, DOM::Event*);
bool web_bindings_dispatch_event_with_legacy_output_did_listeners_throw_flag(DOM::EventTarget*, DOM::Event*);
bool web_bindings_event_canceled(DOM::Event*);
@@ -138,6 +145,14 @@ void web_bindings_set_event_handler_attribute(RustEventTarget* event_target, Rea
event_target->set_event_handler_attribute(Utf16FlyString::from_utf8_without_validation(name), callback);
}
+// https://dom.spec.whatwg.org/#add-an-event-listener, for a listener whose callback is the function, with no options.
+void web_bindings_add_event_listener(void* realm, DOM::EventTarget* target, ReadonlyBytes type, JSObject* callback)
+{
+ auto& js_realm = *static_cast<JS::Realm*>(realm);
+ auto callback_type = GC::Heap::the().allocate<WebIDL::CallbackType>(*JS::HostABI::object_from_abi(callback), js_realm);
+ target->add_event_listener_without_options(Utf16FlyString::from_utf8_without_validation(type), DOM::IDLEventListener::create(callback_type));
+}
+
DOM::Event* web_bindings_create_event(void* realm, ReadonlyBytes type, bool bubbles, bool cancelable, bool composed)
{
auto& js_realm = *static_cast<JS::Realm*>(realm);
@@ -153,11 +168,36 @@ RustEvent* web_bindings_create_rust_event(void* realm, FlatPtr type, bool bubble
return RustEvent::create(Utf16FlyString::from_utf16(type_string.utf16_view()), event_init, HighResolutionTime::current_high_resolution_time(js_realm.global_object())).ptr();
}
+DOM::Event* web_bindings_create_message_event(void* realm, ReadonlyBytes type, JSValue data, JSObject* const* ports, size_t count)
+{
+ auto& js_realm = *static_cast<JS::Realm*>(realm);
+ HTML::MessageEventInit event_init;
+ event_init.data = JS::HostABI::value_from_abi(data);
+ for (size_t i = 0; i < count; ++i) {
+ auto* port = impl_from<HTML::MessagePort>(JS::HostABI::object_from_abi(ports[i]));
+ VERIFY(port);
+ event_init.ports.append(*port);
+ }
+ return HTML::MessageEvent::create(js_realm.global_object(), Utf16FlyString::from_utf8_without_validation(type), event_init).ptr();
+}
+
+JSValue web_bindings_message_event_data(JSValue event)
+{
+ auto* message_event = impl_from<HTML::MessageEvent>(&JS::HostABI::value_from_abi(event).as_object());
+ VERIFY(message_event);
+ return JS::HostABI::value_to_abi(message_event->data());
+}
+
void web_bindings_set_event_owner(RustEvent* event, u16 interface_name, RustInterfaceCell* owner)
{
event->set_owner(static_cast<InterfaceName>(interface_name), *owner);
}
+void web_bindings_event_set_is_trusted(DOM::Event* event, bool is_trusted)
+{
+ event->set_is_trusted(is_trusted);
+}
+
bool web_bindings_dispatch_event(DOM::EventTarget* target, DOM::Event* event)
{
return target->dispatch_event(*event);
diff --git a/Libraries/LibWeb/Streams/ReadableStream.cpp b/Libraries/LibWeb/Streams/ReadableStream.cpp
index db1fb6634c0..170eda7d07f 100644
--- a/Libraries/LibWeb/Streams/ReadableStream.cpp
+++ b/Libraries/LibWeb/Streams/ReadableStream.cpp
@@ -15,7 +15,6 @@
#include <LibWeb/DOM/AbortSignal.h>
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/HTML/EventNames.h>
-#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/MessagePort.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
@@ -73,7 +72,6 @@ void web_streams_abort_signal_remove_algorithm(DOM::AbortSignal*, u64 id);
void web_streams_abort_signal_signal_abort(DOM::AbortSignal*, JSValue reason, void* realm);
void web_streams_create_entangled_ports(void* realm, void** port1, void** port2);
void web_streams_message_port_add_handlers(void* realm, HTML::MessagePort*, JSObject* on_message, JSObject* on_message_error);
-JSValue web_streams_message_event_data(JSValue event);
JSCompletion web_streams_message_port_post_message(void* realm, HTML::MessagePort*, JSValue message);
void web_streams_message_port_disentangle(HTML::MessagePort*);
void web_streams_message_port_enable(HTML::MessagePort*);
@@ -379,13 +377,6 @@ void web_streams_message_port_add_handlers(void* realm, HTML::MessagePort* port,
add_message_event_listener(realm_of(realm), *port, HTML::EventNames::messageerror, on_message_error);
}
-JSValue web_streams_message_event_data(JSValue event)
-{
- auto* message_event = Bindings::impl_from<HTML::MessageEvent>(&JS::HostABI::value_from_abi(event).as_object());
- VERIFY(message_event);
- return JS::HostABI::value_to_abi(message_event->data());
-}
-
JSCompletion web_streams_message_port_post_message(void* realm, HTML::MessagePort* port, JSValue message)
{
auto& js_realm = realm_of(realm);