Skip to content

Truly independent web browser

LibWeb+Meta: Define isTrusted on events implemented in Rust

Andreas Kling792f5b24e73e
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
index 29a2835e5e2..6c87276e5fe 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/interface.rs
@@ -31,9 +31,9 @@ use libjs_rust::api::{
use crate::bindings::CallContext;
use crate::bindings::realm_data::wrapper_world_of;
use crate::bindings::services::{
- web_bindings_ensure_web_constructor, web_bindings_ensure_web_prototype, web_bindings_realm_is_secure_context,
- web_bindings_realm_is_window, web_bindings_rejected_promise, web_bindings_rust_wrapper_for,
- web_bindings_set_rust_wrapper, web_bindings_with_temporary_execution_context,
+ web_bindings_define_unforgeable_attributes, web_bindings_ensure_web_constructor, web_bindings_ensure_web_prototype,
+ web_bindings_realm_is_secure_context, web_bindings_realm_is_window, web_bindings_rejected_promise,
+ web_bindings_rust_wrapper_for, web_bindings_set_rust_wrapper, web_bindings_with_temporary_execution_context,
};
/// What an implementation can tell the bindings about its objects, as a C++ implementation object does by overriding
@@ -114,6 +114,9 @@ pub(crate) trait Interface: Wrappable + Trace + Sized + 'static {
const INTERFACE_NAME: u16;
/// The name that errors call the interface.
const NAME: &'static str;
+ /// The interface implemented in C++ that the interface inherits from, if it has unforgeable attributes, which are
+ /// properties of each wrapper.
+ const UNFORGEABLE_ATTRIBUTES_INTERFACE: Option<u16> = None;
/// The class of the wrappers of the objects of the interface.
fn wrapper_class() -> &'static JSHostClass;
@@ -334,7 +337,7 @@ fn register_wrapper_outside_main_world<T: Interface>(realm: Gc<Realm>, object: R
fn create_wrapper<T: Interface>(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>, object: Ref<T>) -> Gc<HostObject> {
// SAFETY: The wrapper class's hooks expect a wrappable exactly for an interface that inherits from an interface
// implemented in C++, whose objects own one, and the wrappable and the host data are live cells.
- unsafe {
+ let wrapper = unsafe {
HostObject::create(
vm,
realm,
@@ -343,7 +346,14 @@ fn create_wrapper<T: Interface>(vm: &Vm, realm: Gc<Realm>, prototype: Gc<Object>
object.cpp_parent(),
Some(object.as_non_null().cast()),
)
+ };
+ if let Some(interface_name) = T::UNFORGEABLE_ATTRIBUTES_INTERFACE {
+ // SAFETY: The realm and the wrapper are live, and the interface is one the wrapper's interface inherits from.
+ unsafe {
+ web_bindings_define_unforgeable_attributes(realm.as_ptr().cast(), wrapper.as_ptr().cast(), interface_name);
+ }
}
+ wrapper
}
/// The cell of an object of the interface, as LibWeb's C++ code holds it: a pointer to a class that derives from
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
index 5b240158952..49ecc22dfb7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
@@ -29,6 +29,13 @@ unsafe extern "C" {
/// interface implemented in Rust, or null.
pub(crate) fn web_bindings_rust_wrapper_for(realm: *mut c_void, cell: *mut c_void) -> *mut c_void;
+ /// Defines the unforgeable attributes of the interface implemented in C++ on the wrapper.
+ pub(crate) fn web_bindings_define_unforgeable_attributes(
+ realm: *mut c_void,
+ wrapper: *mut c_void,
+ interface_name: u16,
+ );
+
/// Makes `wrapper` the wrapper that the world of its realm, which is not a main world, holds for the cell.
pub(crate) fn web_bindings_set_rust_wrapper(realm: *mut c_void, cell: *mut c_void, wrapper: *mut c_void);
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 8800a59f76b..feed9fd047f 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -456,10 +456,18 @@ def write_ensure_web_prototype_declaration(out: TextIO) -> None:
extern "C" JSObject* web_bindings_ensure_web_prototype(void* realm, u16 interface_name);
extern "C" JSObject* web_bindings_ensure_web_constructor(void* realm, u16 interface_name);
extern "C" JSObject* web_bindings_ensure_web_iterator_prototype(void* realm, u16 interface_name);
+
+// Defines the unforgeable attributes of an interface implemented in C++ on the wrapper of an object of an interface
+// implemented in Rust that inherits from it.
+extern "C" void web_bindings_define_unforgeable_attributes(void* realm, JSObject* wrapper, u16 interface_name);
"""
)
+def has_unforgeable_attributes(interface: Interface) -> bool:
+ return any("LegacyUnforgeable" in attribute.extended_attributes for attribute in interface.regular_attributes)
+
+
def write_ensure_web_prototype_implementation(out: TextIO, intrinsics: List[Interface]) -> None:
rust_interfaces = [
interface for interface in intrinsics if is_implemented_in_rust(interface) and not interface.is_namespace
@@ -525,6 +533,31 @@ JSObject* {function}(void* realm, u16 interface_name)
),
)
+ out.write(
+ """
+void web_bindings_define_unforgeable_attributes(void* realm, JSObject* wrapper, u16 interface_name)
+{
+ [[maybe_unused]] auto& js_realm = *static_cast<JS::Realm*>(realm);
+ [[maybe_unused]] auto& object = *JS::HostABI::object_from_abi(wrapper);
+ switch (static_cast<InterfaceName>(interface_name)) {
+"""
+ )
+ out.writelines(
+ f""" case InterfaceName::{interface.name}:
+ {interface.prototype_class}::define_unforgeable_attributes(js_realm, object);
+ return;
+"""
+ for interface in intrinsics
+ if interface.name in cpp_parent_names and has_unforgeable_attributes(interface)
+ )
+ out.write(
+ """ default:
+ VERIFY_NOT_REACHED();
+ }
+}
+"""
+ )
+
# Rust side
@@ -1037,6 +1070,14 @@ def write_rust_interface(
inherited_interface_name = f"Some(super::{rust_module_name(parent)}::INTERFACE_NAME)"
else:
inherited_interface_name = f"Some({cpp_interface_name_constant(parent)})"
+ cpp_ancestor = parent
+ while cpp_ancestor is not None and is_implemented_in_rust(cpp_ancestor):
+ cpp_ancestor = context.interfaces.get(cpp_ancestor.parent_name)
+ unforgeable_attributes_interface = (
+ f"Some({cpp_interface_name_constant(cpp_ancestor)})"
+ if cpp_ancestor is not None and has_unforgeable_attributes(cpp_ancestor)
+ else "None"
+ )
method_documentation = "".join(
f"/// - `{signature}`\n" for signature in implementation_method_signatures(context, interface, members)
)
@@ -1061,6 +1102,7 @@ def write_rust_interface(
const CELL_CLASS: &'static EmbedderClass<Wrapped<Self>> = &CELL_CLASS;
const INTERFACE_NAME: u16 = INTERFACE_NAME;
const NAME: &'static str = "{interface.namespaced_name}";
+ const UNFORGEABLE_ATTRIBUTES_INTERFACE: Option<u16> = {unforgeable_attributes_interface};
fn wrapper_class() -> &'static JSHostClass {{
// SAFETY: LibWeb defines the class of the wrappers as constant data.
diff --git a/Tests/LibWeb/Text/expected/DOM/Event-isTrusted-is-own-property.txt b/Tests/LibWeb/Text/expected/DOM/Event-isTrusted-is-own-property.txt
new file mode 100644
index 00000000000..4233ad1a79b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/DOM/Event-isTrusted-is-own-property.txt
@@ -0,0 +1,6 @@
+Event: false, own: true
+AnimationEvent: false, own: true
+FontFaceSetLoadEvent: false, own: true
+IDBVersionChangeEvent: false, own: true
+MediaQueryListEvent: false, own: true
+TransitionEvent: false, own: true
diff --git a/Tests/LibWeb/Text/input/DOM/Event-isTrusted-is-own-property.html b/Tests/LibWeb/Text/input/DOM/Event-isTrusted-is-own-property.html
new file mode 100644
index 00000000000..c871fefa9ef
--- /dev/null
+++ b/Tests/LibWeb/Text/input/DOM/Event-isTrusted-is-own-property.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const events = [
+ new Event("x"),
+ new AnimationEvent("x"),
+ new FontFaceSetLoadEvent("x"),
+ new IDBVersionChangeEvent("x"),
+ new MediaQueryListEvent("x"),
+ new TransitionEvent("x"),
+ ];
+ for (const event of events)
+ println(`${event.constructor.name}: ${event.isTrusted}, own: ${Object.hasOwn(event, "isTrusted")}`);
+ });
+</script>