Skip to content

Truly independent web browser

LibURL+LibWeb: Implement Origin in Rust

Shannon Booth038e1d1883b0
diff --git a/Libraries/LibURL/Host.cpp b/Libraries/LibURL/Host.cpp
index 0d5c10d2eed..7467ab85f5d 100644
--- a/Libraries/LibURL/Host.cpp
+++ b/Libraries/LibURL/Host.cpp
@@ -251,4 +251,14 @@ Optional<String> Host::registrable_domain() const
return registrable_domain;
}
+extern "C" void url_rust_registrable_domain(u8 const* domain, size_t domain_length, void* ctx, void (*on_success)(void*, u8 const*, size_t));
+
+extern "C" void url_rust_registrable_domain(u8 const* domain, size_t domain_length, void* ctx, void (*on_success)(void*, u8 const*, size_t))
+{
+ auto registrable_domain = Host { String::from_ascii_without_validation({ domain, domain_length }) }.registrable_domain();
+ if (!registrable_domain.has_value())
+ return;
+ on_success(ctx, registrable_domain->bytes().data(), registrable_domain->bytes_as_string_view().length());
+}
+
}
diff --git a/Libraries/LibURL/Rust/src/ffi/origin.rs b/Libraries/LibURL/Rust/src/ffi/origin.rs
index 4aaddb2767d..5507732e10c 100644
--- a/Libraries/LibURL/Rust/src/ffi/origin.rs
+++ b/Libraries/LibURL/Rust/src/ffi/origin.rs
@@ -7,7 +7,7 @@
use std::ffi::c_void;
use crate::ffi::url::{FfiUrlHost, RustUrl, RustUrlByteSlice};
-use crate::origin::{Nonce, OpaqueOriginKind, Origin};
+use crate::origin::{Host, Nonce, OpaqueOriginKind, Origin};
use crate::rust_panic::abort_on_panic;
use crate::url::HostKind;
@@ -63,6 +63,36 @@ impl FfiOrigin {
pub fn with_origin<R>(origin: &Origin, f: impl FnOnce(&FfiOrigin) -> R) -> R {
f(&Self::from_origin(origin))
}
+
+ /// # Safety
+ /// The strings of the origin must be valid UTF-8 for the duration of this call. The string of a host is its
+ /// serialization, whatever its kind.
+ pub unsafe fn to_origin(&self) -> Origin {
+ if self.is_opaque {
+ return Origin::Opaque {
+ nonce: self.nonce,
+ kind: self.opaque_kind,
+ };
+ }
+ let host_from_ffi = |host: &FfiUrlHost| Host {
+ kind: host.kind,
+ serialization: unsafe {
+ RustUrlByteSlice {
+ data: host.string_data,
+ length: host.string_length,
+ }
+ .borrow()
+ }
+ .unwrap_or("")
+ .to_owned(),
+ };
+ Origin::Tuple {
+ scheme: unsafe { self.scheme.borrow() }.unwrap_or("").to_owned(),
+ host: host_from_ffi(&self.host),
+ port: self.has_port.then_some(self.port),
+ domain: (self.domain.kind != HostKind::Null).then(|| host_from_ffi(&self.domain)),
+ }
+ }
}
pub type FfiOriginResultFn = unsafe extern "C" fn(*mut c_void, *const FfiOrigin);
diff --git a/Libraries/LibURL/Rust/src/lib.rs b/Libraries/LibURL/Rust/src/lib.rs
index 0c9fb612c86..47dd61c5920 100644
--- a/Libraries/LibURL/Rust/src/lib.rs
+++ b/Libraries/LibURL/Rust/src/lib.rs
@@ -19,6 +19,7 @@ pub mod pattern;
mod textcodec;
pub mod url;
+pub use ffi::origin::FfiOrigin;
pub use url::BasicParseOptions;
pub use url::State;
pub use url::Url;
@@ -32,6 +33,16 @@ extern "C" fn url_rust_file_scheme_urls_have_tuple_origins() -> bool {
false
}
+#[cfg(test)]
+#[unsafe(no_mangle)]
+extern "C" fn url_rust_registrable_domain(
+ _domain: *const u8,
+ _domain_length: usize,
+ _ctx: *mut std::ffi::c_void,
+ _on_success: unsafe extern "C" fn(*mut std::ffi::c_void, *const u8, usize),
+) {
+}
+
// LibUnicode's C++ IDNA implementation is not linked into Rust unit tests.
#[cfg(test)]
#[unsafe(no_mangle)]
diff --git a/Libraries/LibURL/Rust/src/origin.rs b/Libraries/LibURL/Rust/src/origin.rs
index ebc5f5e96a3..e5cbec3806d 100644
--- a/Libraries/LibURL/Rust/src/origin.rs
+++ b/Libraries/LibURL/Rust/src/origin.rs
@@ -4,11 +4,20 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use std::ffi::c_void;
+
use crate::url::{BasicParseOptions, HostKind, Url, basic_parse};
unsafe extern "C" {
// NB: LibWeb's library links its own copy of this crate, so process-wide state lives in LibURL's C++.
fn url_rust_file_scheme_urls_have_tuple_origins() -> bool;
+
+ fn url_rust_registrable_domain(
+ domain: *const u8,
+ domain_length: usize,
+ ctx: *mut c_void,
+ on_success: unsafe extern "C" fn(*mut c_void, *const u8, usize),
+ );
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
@@ -17,6 +26,34 @@ pub struct Host {
pub serialization: String,
}
+impl Host {
+ // https://url.spec.whatwg.org/#host-registrable-domain
+ pub fn registrable_domain(&self) -> Option<String> {
+ if self.kind != HostKind::Domain || self.serialization.is_empty() {
+ return None;
+ }
+
+ unsafe extern "C" fn on_success(ctx: *mut c_void, data: *const u8, length: usize) {
+ // SAFETY: ctx is the Option<String> below, and LibURL passes an ASCII string.
+ let result = unsafe { &mut *ctx.cast::<Option<String>>() };
+ let bytes = unsafe { ak::slice_from_raw_parts(data, length) };
+ *result = Some(String::from_utf8(bytes.to_vec()).expect("a registrable domain is ASCII"));
+ }
+
+ let mut result: Option<String> = None;
+ // SAFETY: The domain and the result outlive the call.
+ unsafe {
+ url_rust_registrable_domain(
+ self.serialization.as_ptr(),
+ self.serialization.len(),
+ (&raw mut result).cast(),
+ on_success,
+ );
+ }
+ result
+ }
+}
+
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum OpaqueOriginKind {
@@ -45,6 +82,12 @@ pub enum Origin {
},
}
+// https://html.spec.whatwg.org/multipage/browsers.html#site
+enum Site<'a> {
+ Opaque(&'a Nonce),
+ SchemeAndHost(&'a str, Host),
+}
+
impl Origin {
pub fn new_opaque(kind: OpaqueOriginKind) -> Self {
let mut nonce = Nonce::default();
@@ -60,6 +103,81 @@ impl Origin {
domain: None,
}
}
+
+ pub fn is_opaque(&self) -> bool {
+ matches!(self, Self::Opaque { .. })
+ }
+
+ // https://html.spec.whatwg.org/multipage/origin.html#same-origin
+ pub fn is_same_origin(&self, other: &Self) -> bool {
+ match (self, other) {
+ // 1. If A and B are the same opaque origin, then return true.
+ (Self::Opaque { nonce: a, .. }, Self::Opaque { nonce: b, .. }) => a == b,
+ // 2. If A and B are both tuple origins, their schemes and ports are identical, and their hosts are equal, then
+ // return true.
+ (
+ Self::Tuple {
+ scheme: scheme_a,
+ host: host_a,
+ port: port_a,
+ ..
+ },
+ Self::Tuple {
+ scheme: scheme_b,
+ host: host_b,
+ port: port_b,
+ ..
+ },
+ ) => scheme_a == scheme_b && port_a == port_b && host_a == host_b,
+ // 3. Return false.
+ _ => false,
+ }
+ }
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#obtain-a-site
+ fn site(&self) -> Site<'_> {
+ match self {
+ // 1. If origin is an opaque origin, then return origin.
+ Self::Opaque { nonce, .. } => Site::Opaque(nonce),
+ Self::Tuple { scheme, host, .. } => match host.registrable_domain() {
+ // 2. If origin's host's registrable domain is null, then return (origin's scheme, origin's host).
+ None => Site::SchemeAndHost(scheme, host.clone()),
+ // 3. Return (origin's scheme, origin's host's registrable domain).
+ Some(registrable_domain) => Site::SchemeAndHost(
+ scheme,
+ Host {
+ kind: HostKind::Domain,
+ serialization: registrable_domain,
+ },
+ ),
+ },
+ }
+ }
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#same-site
+ pub fn is_same_site(&self, other: &Self) -> bool {
+ // 1. Let siteA be the result of obtaining a site given A.
+ let site_a = self.site();
+
+ // 2. Let siteB be the result of obtaining a site given B.
+ let site_b = other.site();
+
+ // 3. If siteA is same site with siteB, then return true.
+ // 4. Return false.
+ // https://html.spec.whatwg.org/multipage/browsers.html#concept-site-same-site
+ match (site_a, site_b) {
+ // 1. If A and B are the same opaque origin, then return true.
+ (Site::Opaque(a), Site::Opaque(b)) => a == b,
+ // 2. If A or B is an opaque origin, then return false.
+ (Site::Opaque(_), _) | (_, Site::Opaque(_)) => false,
+ // 3. If A's and B's scheme values are different, then return false.
+ // 4. If A's and B's host values are not equal, then return false.
+ // 5. Return true.
+ (Site::SchemeAndHost(scheme_a, host_a), Site::SchemeAndHost(scheme_b, host_b)) => {
+ scheme_a == scheme_b && host_a == host_b
+ }
+ }
+ }
}
impl<Serialization: AsRef<str>> Url<Serialization> {
diff --git a/Libraries/LibURL/RustIntegration.cpp b/Libraries/LibURL/RustIntegration.cpp
index b9f19167aae..1195563b3be 100644
--- a/Libraries/LibURL/RustIntegration.cpp
+++ b/Libraries/LibURL/RustIntegration.cpp
@@ -11,6 +11,7 @@
#include <AK/StringUtils.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
+#include <LibURL/Origin.h>
#include <LibURL/Parser.h>
#include <LibURL/RustFFI.h>
#include <LibURL/RustIntegration.h>
@@ -182,6 +183,48 @@ Origin origin_from_ffi(FFI::FfiOrigin const& ffi)
return Origin { string_from_ffi(ffi.scheme), host_from_ffi(ffi.host).release_value(), port, host_from_ffi(ffi.domain) };
}
+static FFI::FfiUrlHost host_to_ffi(Optional<Host> const& host, String const& serialization)
+{
+ auto kind = FFI::HostKind::Null;
+ if (host.has_value()) {
+ kind = host->value().visit(
+ [](IPv4Address const&) { return FFI::HostKind::Ipv4; },
+ [](IPv6Address const&) { return FFI::HostKind::Ipv6; },
+ [](String const&) { return FFI::HostKind::Domain; },
+ [](OpaqueHost const&) { return FFI::HostKind::Opaque; });
+ }
+ return {
+ .kind = kind,
+ .ipv4 = {},
+ .ipv6 = {},
+ .string_data = serialization.bytes().data(),
+ .string_length = serialization.bytes().size(),
+ };
+}
+
+void lend_origin_to_ffi(Origin const& origin, Function<void(FFI::FfiOrigin const&)> const& callback)
+{
+ if (origin.is_opaque()) {
+ FFI::FfiOrigin ffi_origin {};
+ ffi_origin.is_opaque = true;
+ ffi_origin.opaque_kind = static_cast<FFI::OpaqueOriginKind>(origin.opaque_data().type);
+ __builtin_memcpy(ffi_origin.nonce, origin.opaque_data().nonce.data(), sizeof(ffi_origin.nonce));
+ callback(ffi_origin);
+ return;
+ }
+
+ auto scheme = origin.scheme().value_or(String {});
+ auto host = origin.host().serialize();
+ auto domain = origin.domain().map([](Host const& domain) { return domain.serialize(); }).value_or(String {});
+ FFI::FfiOrigin ffi_origin {};
+ ffi_origin.scheme = string_to_ffi(scheme);
+ ffi_origin.host = host_to_ffi(origin.host(), host);
+ ffi_origin.has_port = origin.port().has_value();
+ ffi_origin.port = origin.port().value_or(0);
+ ffi_origin.domain = host_to_ffi(origin.domain(), domain);
+ callback(ffi_origin);
+}
+
URLPattern::Impl::~Impl()
{
if (rust_url_pattern)
diff --git a/Libraries/LibURL/RustIntegration.h b/Libraries/LibURL/RustIntegration.h
index dccad173205..ede235e73b4 100644
--- a/Libraries/LibURL/RustIntegration.h
+++ b/Libraries/LibURL/RustIntegration.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
@@ -22,6 +23,7 @@ Optional<Host> parse_host(StringView input, bool is_opaque = false);
Optional<Host> parse_host(Utf16View input, bool is_opaque = false);
Optional<Host> host_from_ffi(FFI::FfiUrlHost const&);
Origin origin_from_ffi(FFI::FfiOrigin const&);
+void lend_origin_to_ffi(Origin const&, Function<void(FFI::FfiOrigin const&)> const&);
FFI::RustUrlInput rust_url_input(Utf16View);
class URLPattern {
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
index b7d5f5c0a93..febf24d3ad6 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/services.rs
@@ -39,6 +39,15 @@ unsafe extern "C" {
/// an interface that LibWeb implements in C++.
pub(crate) fn web_bindings_object_implements_interface(object: *mut c_void, interface_name: u16) -> bool;
+ /// Runs the extract an origin operation of the object, https://html.spec.whatwg.org/multipage/browsers.html#extract-an-origin,
+ /// if it is a platform object of an interface implemented in C++ or a WindowProxy, and calls `on_origin` with the
+ /// origin, which it lends for the call, if that is not null. Returns whether it called `on_origin`.
+ pub(crate) fn web_bindings_extract_an_origin(
+ object: *mut c_void,
+ ctx: *mut c_void,
+ on_origin: unsafe extern "C" fn(*mut c_void, *const liburl_rust::FfiOrigin),
+ ) -> bool;
+
/// The wrapper in the realm of a new DOMException with the name, which is ASCII, and the message, a borrowed
/// AK::Utf16String.
pub(crate) fn web_bindings_create_dom_exception(
diff --git a/Libraries/LibWeb/Bindings/Rust/src/domurl/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/domurl/mod.rs
new file mode 100644
index 00000000000..5fe4a9656c6
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/domurl/mod.rs
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The URL Standard, https://url.spec.whatwg.org/, and the Origin interface of the HTML Standard,
+//! https://html.spec.whatwg.org/multipage/browsers.html#the-origin-interface
+
+use ak::Utf16StringUnits;
+use liburl_rust::url::UrlInput;
+
+#[cfg(not(test))]
+pub(crate) mod origin;
+
+pub(crate) fn url_input(units: Utf16StringUnits<'_>) -> UrlInput<'_> {
+ match units {
+ Utf16StringUnits::Ascii(bytes) => UrlInput::Utf8(core::str::from_utf8(bytes).expect("ASCII is UTF-8")),
+ Utf16StringUnits::Utf16(code_units) => UrlInput::Utf16(code_units),
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/domurl/origin.rs b/Libraries/LibWeb/Bindings/Rust/src/domurl/origin.rs
new file mode 100644
index 00000000000..d905a6e796a
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/domurl/origin.rs
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Origin, https://html.spec.whatwg.org/multipage/browsers.html#the-origin-interface
+
+use ak::Utf16String;
+use libjs_rust::api::{Trace, Value, is_host_instance_of};
+use liburl_rust::origin::{OpaqueOriginKind, Origin as OriginValue};
+use liburl_rust::{BasicParseOptions, FfiOrigin, basic_parse};
+
+use super::url_input;
+use crate::bindings::conversions::to_dom_string;
+use crate::bindings::interface::{Interface, Ref, Wrappable, allocate, implementation_of};
+use crate::bindings::services::web_bindings_extract_an_origin;
+use crate::bindings::{CallContext, Exception, WebIdlResult};
+
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct Origin {
+ // https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-origin
+ // Origin objects have an associated origin, which holds an origin.
+ #[gc(untraced)]
+ origin: OriginValue,
+}
+
+impl Wrappable for Origin {}
+
+impl Origin {
+ // https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-constructor
+ pub(crate) fn construct(_: &mut CallContext) -> WebIdlResult<Self> {
+ // The new Origin() constructor steps are to set this's origin to a unique opaque origin.
+ Ok(Self {
+ origin: OriginValue::new_opaque(OpaqueOriginKind::Standard),
+ })
+ }
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-from
+ pub(crate) fn from(cx: &mut CallContext, value: Value) -> WebIdlResult<Ref<Self>> {
+ // 1. If value is a platform object:
+ // 1. Let origin be the result of executing value's extract an origin operation.
+ // 2. If origin is not null, then return a new Origin object whose origin is origin.
+ if let Some(origin) = extract_an_origin(value) {
+ return Ok(allocate(cx.vm(), Self { origin }));
+ }
+
+ // 2. If value is a string:
+ if value.is_string() {
+ // 1. Let parsedURL be the result of basic URL parsing value.
+ let string = to_dom_string(cx.vm(), value)?;
+ let parsed_url = basic_parse(url_input(string.as_units()), BasicParseOptions::new());
+
+ // 2. If parsedURL is not failure, then return a new Origin object whose origin is set to parsedURL's
+ // origin.
+ if let Some(parsed_url) = parsed_url {
+ let origin = parsed_url.origin();
+ return Ok(allocate(cx.vm(), Self { origin }));
+ }
+ }
+
+ // 3. Throw a TypeError.
+ Err(Exception::TypeError(Utf16String::from_utf8(
+ "Value is not a valid Origin",
+ )))
+ }
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-opaque
+ pub(crate) fn opaque(&self, _: &mut CallContext) -> WebIdlResult<bool> {
+ // The opaque getter steps are to return true if this's origin is an opaque origin; otherwise false.
+ Ok(self.origin.is_opaque())
+ }
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-issameorigin
+ pub(crate) fn is_same_origin(&self, _: &mut CallContext, other: Ref<Self>) -> WebIdlResult<bool> {
+ // The isSameOrigin(other) method steps are to return true if this's origin is same origin with other's
+ // origin; otherwise false.
+ Ok(self.origin.is_same_origin(&other.origin))
+ }
+
+ // https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-issamesite
+ pub(crate) fn is_same_site(&self, _: &mut CallContext, other: Ref<Self>) -> WebIdlResult<bool> {
+ // The isSameSite(other) method steps are to return true if this's origin is same site with other's origin;
+ // otherwise false.
+ Ok(self.origin.is_same_site(&other.origin))
+ }
+}
+
+// https://html.spec.whatwg.org/multipage/browsers.html#extract-an-origin
+fn extract_an_origin(value: Value) -> Option<OriginValue> {
+ if !value.is_object() {
+ return None;
+ }
+ let object = value.as_object();
+
+ // Objects implementing the Origin interface's extract an origin steps are to return this's origin.
+ if is_host_instance_of(&object, Origin::wrapper_class()) {
+ return Some(implementation_of::<Origin>(object).origin.clone());
+ }
+
+ unsafe extern "C" fn on_origin(ctx: *mut core::ffi::c_void, ffi_origin: *const FfiOrigin) {
+ // SAFETY: ctx is the result below, and LibWeb lends the origin for the call.
+ unsafe { *ctx.cast::<Option<OriginValue>>() = Some((*ffi_origin).to_origin()) };
+ }
+
+ let mut origin = None;
+ // SAFETY: The object is live, and the result outlives the call.
+ unsafe { web_bindings_extract_an_origin(object.as_ptr().cast(), (&raw mut origin).cast(), on_origin) };
+ origin
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index 24f0413b914..67d078f29b5 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -43,6 +43,15 @@ pub(crate) mod canvas_2d;
not(web_idl_bindings),
allow(dead_code, reason = "only the generated bindings use the interfaces")
)]
+#[allow(
+ clippy::unnecessary_wraps,
+ reason = "the bindings call every attribute and operation as one that can throw"
+)]
+pub(crate) mod domurl;
+#[cfg_attr(
+ not(web_idl_bindings),
+ allow(dead_code, reason = "only the generated bindings use the interfaces")
+)]
pub(crate) mod encoding;
#[cfg_attr(
not(web_idl_bindings),
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index af37f580cc1..3594709ed85 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -7,6 +7,7 @@
#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/HostObject.h>
#include <LibJS/Runtime/Realm.h>
+#include <LibURL/RustIntegration.h>
#include <LibWeb/Bindings/DOMException.h>
#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/Bindings/RustSerialization.h>
@@ -14,6 +15,8 @@
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/StructuredSerialize.h>
+#include <LibWeb/HTML/Window.h>
+#include <LibWeb/HTML/WindowProxy.h>
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/ExceptionOrUtils.h>
#include <LibWeb/WebIDL/Promise.h>
@@ -86,6 +89,7 @@ JSObject* web_bindings_rejected_promise(void* current_realm, JSValue this_value,
bool web_bindings_realm_is_window(void* realm);
bool web_bindings_object_implements_interface(JSObject* object, u16 interface_name);
JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, FlatPtr message);
+bool web_bindings_extract_an_origin(JSObject* object, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));
JSCompletion web_bindings_serialize_value(RustSerializationWriter*, JSValue);
void web_bindings_serialize_u32(RustSerializationWriter*, u32);
void web_bindings_serialize_f64(RustSerializationWriter*, double);
@@ -135,6 +139,28 @@ JSObject* web_bindings_create_dom_exception(void* realm, ReadonlyBytes name, Fla
return JS::HostABI::object_to_abi(wrap(host_defined_wrapper_world(js_realm), js_realm, exception).ptr());
}
+// https://html.spec.whatwg.org/multipage/browsers.html#extract-an-origin
+bool web_bindings_extract_an_origin(JSObject* object, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*))
+{
+ auto* js_object = JS::HostABI::object_from_abi(object);
+ Optional<URL::Origin> origin;
+
+ // NB: IDL only ever sees HTML::WindowProxy but we want to use HTML::Window.
+ if (auto* window_proxy = HTML::WindowProxy::from_object(*js_object)) {
+ if (auto window = window_proxy->window())
+ origin = window->extract_an_origin();
+ } else if (auto const* wrappable = wrappable_impl_from(js_object)) {
+ origin = wrappable->extract_an_origin();
+ }
+
+ if (!origin.has_value())
+ return false;
+ URL::RustIntegration::lend_origin_to_ffi(*origin, [&](URL::FFI::FfiOrigin const& ffi_origin) {
+ on_origin(ctx, &ffi_origin);
+ });
+ return true;
+}
+
// StructuredSerializeInternal of a value that the serialized form of an object of an interface implemented in Rust
// holds, https://html.spec.whatwg.org/multipage/structured-data.html#sub-serialization
JSCompletion web_bindings_serialize_value(RustSerializationWriter* writer, JSValue value)
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 6c5b5c10336..f366e07a0fd 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -327,7 +327,6 @@ set(SOURCES
DOM/Utils.cpp
DOM/XMLDocument.cpp
DOMURL/DOMURL.cpp
- DOMURL/Origin.cpp
DOMURL/URLSearchParams.cpp
DOMURL/URLSearchParamsIterator.cpp
Dump.cpp
diff --git a/Libraries/LibWeb/DOMURL/Origin.cpp b/Libraries/LibWeb/DOMURL/Origin.cpp
deleted file mode 100644
index ad024867672..00000000000
--- a/Libraries/LibWeb/DOMURL/Origin.cpp
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * Copyright (c) 2025, Shannon Booth <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGC/Heap.h>
-#include <LibJS/Runtime/Value.h>
-#include <LibURL/Parser.h>
-#include <LibWeb/Bindings/PlatformObject.h>
-#include <LibWeb/DOMURL/Origin.h>
-#include <LibWeb/HTML/Window.h>
-#include <LibWeb/HTML/WindowProxy.h>
-
-namespace Web::DOMURL {
-
-GC_DEFINE_ALLOCATOR(Origin);
-
-Origin::Origin(URL::Origin origin)
- : m_origin(move(origin))
-{
-}
-
-Origin::~Origin() = default;
-
-GC::Ref<Origin> Origin::create(URL::Origin origin)
-{
- return GC::Heap::the().allocate<Origin>(move(origin));
-}
-
-// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-constructor
-GC::Ref<Origin> Origin::create_opaque()
-{
- // The new Origin() constructor steps are to set this's origin to a unique opaque origin.
- return create(URL::Origin::create_opaque());
-}
-
-GC::Ref<Origin> Origin::construct_impl()
-{
- return create_opaque();
-}
-
-static Optional<URL::Origin> extract_origin_from_platform_object(JS::Value value)
-{
- if (!value.is_object())
- return {};
- auto const* wrappable = Bindings::wrappable_impl_from(&value.as_object());
- if (!wrappable)
- return {};
- return wrappable->extract_an_origin();
-}
-
-// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-from
-WebIDL::ExceptionOr<GC::Ref<Origin>> Origin::from(JS::Value value)
-{
- // NB: IDL only ever sees HTML::WindowProxy but we want to use HTML::Window.
- if (auto* window_proxy = value.is_object() ? HTML::WindowProxy::from_object(value.as_object()) : nullptr) {
- if (auto window = window_proxy->window()) {
- auto origin = window->extract_an_origin();
- if (origin.has_value())
- return create(origin.release_value());
- }
- }
-
- // 1. If value is a platform object:
- if (auto origin = extract_origin_from_platform_object(value); origin.has_value()) {
- // 1. Let origin be the result of executing value's extract an origin operation.
- // 2. If origin is not null, then return a new Origin object whose origin is origin.
- return create(origin.release_value());
- }
-
- // 2. If value is a string:
- if (value.is_string()) {
- auto string = value.as_string().utf16_string_view();
-
- // 1. Let parsedURL be the result of basic URL parsing value.
- auto parsed_url = URL::Parser::basic_parse(string);
-
- // 2. If parsedURL is not failure, then return a new Origin object whose origin is set to parsedURL's origin.
- if (parsed_url.has_value())
- return create(parsed_url->origin());
- }
-
- // 3. Throw a TypeError.
- return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Value is not a valid Origin"_utf16 };
-}
-
-// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-opaque
-bool Origin::opaque() const
-{
- // The opaque getter steps are to return true if this's origin is an opaque origin; otherwise false.
- return m_origin.is_opaque();
-}
-
-// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-issameorigin
-bool Origin::is_same_origin(Origin const& other) const
-{
- // The isSameOrigin(other) method steps are to return true if this's origin is same origin with other's origin;
- // otherwise false.
- return m_origin.is_same_origin(other.m_origin);
-}
-
-// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-issamesite
-bool Origin::is_same_site(Origin const& other) const
-{
- // The isSameSite(other) method steps are to return true if this's origin is same site with other's origin;
- // otherwise false.
- return m_origin.is_same_site(other.m_origin);
-}
-
-// https://html.spec.whatwg.org/multipage/browsers.html#extract-an-origin
-Optional<URL::Origin> Origin::extract_an_origin() const
-{
- // Objects implementing the Origin interface's extract an origin steps are to return this's origin.
- return m_origin;
-}
-
-}
diff --git a/Libraries/LibWeb/DOMURL/Origin.h b/Libraries/LibWeb/DOMURL/Origin.h
deleted file mode 100644
index e6067631604..00000000000
--- a/Libraries/LibWeb/DOMURL/Origin.h
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2025, Shannon Booth <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/Optional.h>
-#include <LibJS/Forward.h>
-#include <LibURL/URL.h>
-#include <LibWeb/Bindings/Wrappable.h>
-#include <LibWeb/WebIDL/ExceptionOr.h>
-
-namespace Web::DOMURL {
-
-// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-interface
-class Origin : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(Origin, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(Origin);
-
-public:
- static GC::Ref<Origin> create(URL::Origin);
- static GC::Ref<Origin> create_opaque();
- static GC::Ref<Origin> construct_impl();
- static WebIDL::ExceptionOr<GC::Ref<Origin>> from(JS::Value);
-
- bool opaque() const;
- bool is_same_origin(Origin const&) const;
- bool is_same_site(Origin const&) const;
- virtual Optional<URL::Origin> extract_an_origin() const override;
-
- virtual ~Origin() override;
-
-private:
- explicit Origin(URL::Origin);
-
- // https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-origin
- // Origin objects have an associated origin, which holds an origin.
- URL::Origin m_origin;
-};
-
-}
diff --git a/Libraries/LibWeb/DOMURL/Origin.idl b/Libraries/LibWeb/DOMURL/Origin.idl
index c2c66d51807..ccc8db016b9 100644
--- a/Libraries/LibWeb/DOMURL/Origin.idl
+++ b/Libraries/LibWeb/DOMURL/Origin.idl
@@ -1,5 +1,5 @@
// https://html.spec.whatwg.org/multipage/browsers.html#dom-origin-interface
-[Exposed=*]
+[Exposed=*, ImplementedIn=Rust]
interface Origin {
constructor();