Skip to content

Truly independent web browser

LibWeb: Implement the Geolocation position interfaces in Rust

Shannon Boothdbd31d8ff32b
diff --git a/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_coordinates.rs b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_coordinates.rs
new file mode 100644
index 00000000000..c784699a6b5
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_coordinates.rs
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2025, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! GeolocationCoordinates, https://w3c.github.io/geolocation/#coordinates_interface
+
+use core::ffi::c_void;
+
+use libjs_rust::api::{Trace, Vm};
+
+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_into_abi};
+use crate::bindings::{CallContext, WebIdlResult};
+
+/// Web::Geolocation::CoordinatesData.
+#[repr(C)]
+pub(crate) struct CoordinatesData {
+ accuracy: f64,
+ latitude: f64,
+ longitude: f64,
+ has_altitude: bool,
+ altitude: f64,
+ has_altitude_accuracy: bool,
+ altitude_accuracy: f64,
+ has_heading: bool,
+ heading: f64,
+ has_speed: bool,
+ speed: f64,
+}
+
+// https://w3c.github.io/geolocation/#coordinates_interface
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct GeolocationCoordinates {
+ #[gc(untraced)]
+ accuracy: f64,
+ #[gc(untraced)]
+ latitude: f64,
+ #[gc(untraced)]
+ longitude: f64,
+ #[gc(untraced)]
+ altitude: Option<f64>,
+ #[gc(untraced)]
+ altitude_accuracy: Option<f64>,
+ #[gc(untraced)]
+ heading: Option<f64>,
+ #[gc(untraced)]
+ speed: Option<f64>,
+}
+
+impl Wrappable for GeolocationCoordinates {}
+
+impl GeolocationCoordinates {
+ pub(crate) fn create(vm: &Vm, data: &CoordinatesData) -> Ref<Self> {
+ let optional = |has_value: bool, value: f64| has_value.then_some(value);
+ allocate(vm, |_| Self {
+ accuracy: data.accuracy,
+ latitude: data.latitude,
+ longitude: data.longitude,
+ altitude: optional(data.has_altitude, data.altitude),
+ altitude_accuracy: optional(data.has_altitude_accuracy, data.altitude_accuracy),
+ heading: optional(data.has_heading, data.heading),
+ speed: optional(data.has_speed, data.speed),
+ })
+ }
+
+ pub(crate) fn accuracy(&self, _: &mut CallContext) -> WebIdlResult<f64> {
+ Ok(self.accuracy)
+ }
+
+ pub(crate) fn latitude(&self, _: &mut CallContext) -> WebIdlResult<f64> {
+ Ok(self.latitude)
+ }
+
+ pub(crate) fn longitude(&self, _: &mut CallContext) -> WebIdlResult<f64> {
+ Ok(self.longitude)
+ }
+
+ pub(crate) fn altitude(&self, _: &mut CallContext) -> WebIdlResult<Option<f64>> {
+ Ok(self.altitude)
+ }
+
+ pub(crate) fn altitude_accuracy(&self, _: &mut CallContext) -> WebIdlResult<Option<f64>> {
+ Ok(self.altitude_accuracy)
+ }
+
+ pub(crate) fn heading(&self, _: &mut CallContext) -> WebIdlResult<Option<f64>> {
+ Ok(self.heading)
+ }
+
+ pub(crate) fn speed(&self, _: &mut CallContext) -> WebIdlResult<Option<f64>> {
+ Ok(self.speed)
+ }
+}
+
+/// A new GeolocationCoordinates with the data.
+///
+/// # Safety
+///
+/// `vm` must be the live VM, and `data` the data that the call lends.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_geolocation_create_geolocation_coordinates(
+ vm: *mut c_void,
+ data: &CoordinatesData,
+) -> *mut c_void {
+ // SAFETY: The caller passes the live VM.
+ let vm = unsafe { &*vm.cast::<Vm>() };
+ cell_into_abi(GeolocationCoordinates::create(vm, data))
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position.rs b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position.rs
new file mode 100644
index 00000000000..68fffc79440
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position.rs
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2025, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! GeolocationPosition, https://w3c.github.io/geolocation/#position_interface
+
+use core::ffi::c_void;
+
+use libjs_rust::api::{Trace, Vm};
+
+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_from_abi, cell_into_abi};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::geolocation::geolocation_coordinates::GeolocationCoordinates;
+
+// https://w3c.github.io/geolocation/#dom-geolocationposition
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct GeolocationPosition {
+ coords: Ref<GeolocationCoordinates>,
+ #[gc(untraced)]
+ timestamp: u64,
+
+ // https://w3c.github.io/geolocation/#dfn-ishighaccuracy
+ #[gc(untraced)]
+ is_high_accuracy: bool,
+}
+
+impl Wrappable for GeolocationPosition {}
+
+impl GeolocationPosition {
+ pub(crate) fn create(
+ vm: &Vm,
+ coords: Ref<GeolocationCoordinates>,
+ timestamp: u64,
+ is_high_accuracy: bool,
+ ) -> Ref<Self> {
+ allocate(vm, |_| Self {
+ coords,
+ timestamp,
+ is_high_accuracy,
+ })
+ }
+
+ pub(crate) fn coords(&self, _: &mut CallContext) -> WebIdlResult<Ref<GeolocationCoordinates>> {
+ Ok(self.coords)
+ }
+
+ pub(crate) fn timestamp(&self, _: &mut CallContext) -> WebIdlResult<u64> {
+ Ok(self.timestamp)
+ }
+}
+
+/// A new GeolocationPosition.
+///
+/// # Safety
+///
+/// `vm` must be the live VM, and `coords` the live cell of a GeolocationCoordinates.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_geolocation_create_geolocation_position(
+ vm: *mut c_void,
+ coords: *mut c_void,
+ timestamp: u64,
+ is_high_accuracy: bool,
+) -> *mut c_void {
+ // SAFETY: The caller passes the live VM and a live cell.
+ let (vm, coords) = unsafe { (&*vm.cast::<Vm>(), cell_from_abi::<GeolocationCoordinates>(coords)) };
+ cell_into_abi(GeolocationPosition::create(vm, coords, timestamp, is_high_accuracy))
+}
+
+/// # Safety
+///
+/// `cell` must be the live cell of a GeolocationPosition.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_geolocation_geolocation_position_timestamp(cell: *mut c_void) -> u64 {
+ // SAFETY: The caller passes a live cell.
+ unsafe { cell_from_abi::<GeolocationPosition>(cell) }.timestamp
+}
+
+/// # Safety
+///
+/// `cell` must be the live cell of a GeolocationPosition.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_geolocation_geolocation_position_is_high_accuracy(cell: *mut c_void) -> bool {
+ // SAFETY: The caller passes a live cell.
+ unsafe { cell_from_abi::<GeolocationPosition>(cell) }.is_high_accuracy
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position_error.rs b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position_error.rs
new file mode 100644
index 00000000000..38bdc3ce448
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position_error.rs
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2025, Jelle Raaijmakers <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! GeolocationPositionError, https://w3c.github.io/geolocation/#dom-geolocationpositionerror
+
+use core::ffi::c_void;
+
+use ak::Utf16String;
+use libjs_rust::api::{Trace, Vm};
+
+use crate::bindings::interface::{Ref, Wrappable, allocate, cell_into_abi};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::geolocation::geolocation_position_error_code::GeolocationPositionErrorCode;
+
+// https://w3c.github.io/geolocation/#dom-geolocationpositionerror
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct GeolocationPositionError {
+ #[gc(untraced)]
+ code: GeolocationPositionErrorCode,
+}
+
+impl Wrappable for GeolocationPositionError {}
+
+impl GeolocationPositionError {
+ pub(crate) fn create(vm: &Vm, code: GeolocationPositionErrorCode) -> Ref<Self> {
+ allocate(vm, |_| Self { code })
+ }
+
+ pub(crate) fn code(&self, _: &mut CallContext) -> WebIdlResult<u16> {
+ Ok(self.code as u16)
+ }
+
+ // https://w3c.github.io/geolocation/#message-attribute
+ pub(crate) fn message(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {
+ // The message attribute is a developer-friendly textual description of the code attribute.
+ let message: &[u8] = match self.code {
+ GeolocationPositionErrorCode::PositionUnavailable => b"Position unavailable",
+ GeolocationPositionErrorCode::PermissionDenied => b"Permission denied",
+ GeolocationPositionErrorCode::Timeout => b"Timeout",
+ };
+ Ok(Utf16String::from_ascii(message))
+ }
+}
+
+/// A new GeolocationPositionError with the code.
+///
+/// # Safety
+///
+/// `vm` must be the live VM.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_rust_geolocation_create_geolocation_position_error(
+ vm: *mut c_void,
+ code: u16,
+) -> *mut c_void {
+ // SAFETY: The caller passes the live VM.
+ let vm = unsafe { &*vm.cast::<Vm>() };
+ cell_into_abi(GeolocationPositionError::create(
+ vm,
+ GeolocationPositionErrorCode::from_u16(code),
+ ))
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position_error_code.rs b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position_error_code.rs
new file mode 100644
index 00000000000..fd7bcc9639f
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/geolocation/geolocation_position_error_code.rs
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/// https://w3c.github.io/geolocation/#dom-geolocationpositionerror-code
+#[derive(Clone, Copy, PartialEq, Eq)]
+#[repr(u16)]
+pub(crate) enum GeolocationPositionErrorCode {
+ PermissionDenied = 1,
+ PositionUnavailable = 2,
+ Timeout = 3,
+}
+
+impl GeolocationPositionErrorCode {
+ pub(crate) fn from_u16(code: u16) -> Self {
+ match code {
+ 1 => Self::PermissionDenied,
+ 2 => Self::PositionUnavailable,
+ 3 => Self::Timeout,
+ _ => unreachable!("LibWeb passes a GeolocationPositionErrorCode"),
+ }
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/geolocation/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/geolocation/mod.rs
new file mode 100644
index 00000000000..7f2646388e0
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/geolocation/mod.rs
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+pub(crate) mod geolocation_coordinates;
+pub(crate) mod geolocation_position;
+pub(crate) mod geolocation_position_error;
+pub(crate) mod geolocation_position_error_code;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
index a26b04aea05..4e217c18fe5 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs
@@ -131,6 +131,16 @@ pub(crate) mod fetch;
reason = "the bindings call every attribute and operation as one that can throw"
)]
pub(crate) mod file_api;
+#[cfg(not(test))]
+#[cfg_attr(
+ 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 geolocation;
#[cfg_attr(
not(web_idl_bindings),
allow(dead_code, reason = "only the generated bindings use the interfaces")
diff --git a/Libraries/LibWeb/Geolocation/Geolocation.cpp b/Libraries/LibWeb/Geolocation/Geolocation.cpp
index cbe461664bf..9f7b46a6784 100644
--- a/Libraries/LibWeb/Geolocation/Geolocation.cpp
+++ b/Libraries/LibWeb/Geolocation/Geolocation.cpp
@@ -339,7 +339,7 @@ void Geolocation::acquire_a_position(GC::Ref<WebIDL::CallbackType> success_callb
// 2. Let position be a new GeolocationPosition passing emulatedPositionData, acquisitionTime and
// options.enableHighAccuracy.
- auto position = GC::Heap::the().allocate<GeolocationPosition>(
+ auto position = GeolocationPosition::create(
emulated_position_data.get<GC::Ref<GeolocationCoordinates>>(),
acquisition_time,
options.enable_high_accuracy);
@@ -490,11 +490,11 @@ void Geolocation::acquire_position_from_page(GC::Ref<WebIDL::CallbackType> succe
// * heading:
// A double? that represents the heading in degrees, or null if not available or the device is
// stationary. Heading measures the direction in which the device is moving relative to true north.
- auto position_data = GC::Heap::the().allocate<GeolocationCoordinates>(result.get<CoordinatesData>());
+ auto position_data = GeolocationCoordinates::create(result.get<CoordinatesData>());
// 6.2. Set position to a new GeolocationPosition passing positionData, acquisitionTime and
// options.enableHighAccuracy.
- auto position = GC::Heap::the().allocate<GeolocationPosition>(position_data, acquisition_time, options.enable_high_accuracy);
+ auto position = GeolocationPosition::create(position_data, acquisition_time, options.enable_high_accuracy);
// 6.3. Set this's [[cachedPosition]] to position.
self.m_cached_position = *position;
@@ -536,7 +536,7 @@ void Geolocation::call_back_with_error(GC::Ptr<WebIDL::CallbackType> callback, G
return;
// 2. Let error be a newly created GeolocationPositionError instance whose code attribute is initialized to code.
- auto error = GC::Heap::the().allocate<GeolocationPositionError>(code);
+ auto error = GeolocationPositionError::create(code);
// 3. Queue a task on the geolocation task source with a step that invokes callback with « error » and "report".
queue_error_callback(*callback, error);
diff --git a/Libraries/LibWeb/Geolocation/GeolocationCoordinates.cpp b/Libraries/LibWeb/Geolocation/GeolocationCoordinates.cpp
index a5b1657e598..713e0e6f5b0 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationCoordinates.cpp
+++ b/Libraries/LibWeb/Geolocation/GeolocationCoordinates.cpp
@@ -4,19 +4,51 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
#include <LibWeb/Geolocation/GeolocationCoordinates.h>
namespace Web::Geolocation {
-GC_DEFINE_ALLOCATOR(GeolocationCoordinates);
+namespace {
-GeolocationCoordinates::GeolocationCoordinates()
-{
+struct CoordinatesDataAbi {
+ double accuracy;
+ double latitude;
+ double longitude;
+ bool has_altitude;
+ double altitude;
+ bool has_altitude_accuracy;
+ double altitude_accuracy;
+ bool has_heading;
+ double heading;
+ bool has_speed;
+ double speed;
+};
+
+}
+
+extern "C" {
+// Defined in Bindings/Rust/src/geolocation/geolocation_coordinates.rs.
+GeolocationCoordinates* web_rust_geolocation_create_geolocation_coordinates(JSVM*, CoordinatesDataAbi const&);
}
-GeolocationCoordinates::GeolocationCoordinates(CoordinatesData data)
- : m_coordinates_data(move(data))
+GC::Ref<GeolocationCoordinates> GeolocationCoordinates::create(CoordinatesData const& data)
{
+ CoordinatesDataAbi abi {
+ .accuracy = data.accuracy,
+ .latitude = data.latitude,
+ .longitude = data.longitude,
+ .has_altitude = data.altitude.has_value(),
+ .altitude = data.altitude.value_or(0),
+ .has_altitude_accuracy = data.altitude_accuracy.has_value(),
+ .altitude_accuracy = data.altitude_accuracy.value_or(0),
+ .has_heading = data.heading.has_value(),
+ .heading = data.heading.value_or(0),
+ .has_speed = data.speed.has_value(),
+ .speed = data.speed.value_or(0),
+ };
+ return *web_rust_geolocation_create_geolocation_coordinates(JS::HostABI::vm_to_abi(JS::VM::the()), abi);
}
}
diff --git a/Libraries/LibWeb/Geolocation/GeolocationCoordinates.h b/Libraries/LibWeb/Geolocation/GeolocationCoordinates.h
index 72184c6612b..a91aa8e270d 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationCoordinates.h
+++ b/Libraries/LibWeb/Geolocation/GeolocationCoordinates.h
@@ -7,7 +7,8 @@
#pragma once
#include <AK/Optional.h>
-#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Bindings/GeolocationCoordinates.h>
+#include <LibWeb/Bindings/RustInterface.h>
namespace Web::Geolocation {
@@ -22,25 +23,11 @@ struct CoordinatesData {
};
// https://w3c.github.io/geolocation/#coordinates_interface
-class GeolocationCoordinates : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(GeolocationCoordinates, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(GeolocationCoordinates);
+class GeolocationCoordinates : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(GeolocationCoordinates, geolocation_coordinates)
public:
- double accuracy() const { return m_coordinates_data.accuracy; }
- double latitude() const { return m_coordinates_data.latitude; }
- double longitude() const { return m_coordinates_data.longitude; }
- Optional<double> altitude() const { return m_coordinates_data.altitude; }
- Optional<double> altitude_accuracy() const { return m_coordinates_data.altitude_accuracy; }
- Optional<double> heading() const { return m_coordinates_data.heading; }
- Optional<double> speed() const { return m_coordinates_data.speed; }
-
-private:
- friend class Geolocation;
- GeolocationCoordinates();
- explicit GeolocationCoordinates(CoordinatesData);
-
- CoordinatesData m_coordinates_data;
+ [[nodiscard]] static GC::Ref<GeolocationCoordinates> create(CoordinatesData const&);
};
}
diff --git a/Libraries/LibWeb/Geolocation/GeolocationCoordinates.idl b/Libraries/LibWeb/Geolocation/GeolocationCoordinates.idl
index 9d273d59c46..c3c3037bc17 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationCoordinates.idl
+++ b/Libraries/LibWeb/Geolocation/GeolocationCoordinates.idl
@@ -1,5 +1,5 @@
// https://w3c.github.io/geolocation/#coordinates_interface
-[Exposed=Window, SecureContext]
+[Exposed=Window, SecureContext, ImplementedIn=Rust]
interface GeolocationCoordinates {
readonly attribute double accuracy;
readonly attribute double latitude;
diff --git a/Libraries/LibWeb/Geolocation/GeolocationPosition.cpp b/Libraries/LibWeb/Geolocation/GeolocationPosition.cpp
index 37d6fd23dcb..3464dfb74eb 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationPosition.cpp
+++ b/Libraries/LibWeb/Geolocation/GeolocationPosition.cpp
@@ -4,24 +4,32 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
#include <LibWeb/Geolocation/GeolocationPosition.h>
namespace Web::Geolocation {
-GC_DEFINE_ALLOCATOR(GeolocationPosition);
+extern "C" {
+// Defined in Bindings/Rust/src/geolocation/geolocation_position.rs.
+GeolocationPosition* web_rust_geolocation_create_geolocation_position(JSVM*, GeolocationCoordinates*, u64 timestamp, bool is_high_accuracy);
+u64 web_rust_geolocation_geolocation_position_timestamp(GeolocationPosition*);
+bool web_rust_geolocation_geolocation_position_is_high_accuracy(GeolocationPosition*);
+}
+
+GC::Ref<GeolocationPosition> GeolocationPosition::create(GC::Ref<GeolocationCoordinates> coords, HighResolutionTime::EpochTimeStamp timestamp, bool is_high_accuracy)
+{
+ return *web_rust_geolocation_create_geolocation_position(JS::HostABI::vm_to_abi(JS::VM::the()), coords.ptr(), timestamp, is_high_accuracy);
+}
-GeolocationPosition::GeolocationPosition(GC::Ref<GeolocationCoordinates> coords,
- HighResolutionTime::EpochTimeStamp timestamp, bool is_high_accuracy)
- : m_coords(coords)
- , m_timestamp(timestamp)
- , m_is_high_accuracy(is_high_accuracy)
+HighResolutionTime::EpochTimeStamp GeolocationPosition::timestamp() const
{
+ return web_rust_geolocation_geolocation_position_timestamp(const_cast<GeolocationPosition*>(this));
}
-void GeolocationPosition::visit_edges(GC::Cell::Visitor& visitor)
+bool GeolocationPosition::is_high_accuracy() const
{
- Base::visit_edges(visitor);
- visitor.visit(m_coords);
+ return web_rust_geolocation_geolocation_position_is_high_accuracy(const_cast<GeolocationPosition*>(this));
}
}
diff --git a/Libraries/LibWeb/Geolocation/GeolocationPosition.h b/Libraries/LibWeb/Geolocation/GeolocationPosition.h
index 24ae2c2e1aa..421f3c56e1c 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationPosition.h
+++ b/Libraries/LibWeb/Geolocation/GeolocationPosition.h
@@ -6,34 +6,22 @@
#pragma once
-#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Bindings/GeolocationPosition.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/Geolocation/GeolocationCoordinates.h>
#include <LibWeb/HighResolutionTime/EpochTimeStamp.h>
namespace Web::Geolocation {
// https://w3c.github.io/geolocation/#dom-geolocationposition
-class GeolocationPosition : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(GeolocationPosition, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(GeolocationPosition);
+class GeolocationPosition : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(GeolocationPosition, geolocation_position)
public:
- static constexpr size_t coords_offset() { return offsetof(GeolocationPosition, m_coords); }
- GC::Ref<GeolocationCoordinates const> coords() const { return m_coords; }
- HighResolutionTime::EpochTimeStamp timestamp() const { return m_timestamp; }
- bool is_high_accuracy() const { return m_is_high_accuracy; }
+ [[nodiscard]] static GC::Ref<GeolocationPosition> create(GC::Ref<GeolocationCoordinates>, HighResolutionTime::EpochTimeStamp, bool is_high_accuracy);
-private:
- friend class Geolocation;
- GeolocationPosition(GC::Ref<GeolocationCoordinates>, HighResolutionTime::EpochTimeStamp, bool is_high_accuracy);
-
- virtual void visit_edges(GC::Cell::Visitor&) override;
-
- GC::Ref<GeolocationCoordinates const> m_coords;
- HighResolutionTime::EpochTimeStamp m_timestamp;
-
- // https://w3c.github.io/geolocation/#dfn-ishighaccuracy
- bool m_is_high_accuracy;
+ HighResolutionTime::EpochTimeStamp timestamp() const;
+ bool is_high_accuracy() const;
};
}
diff --git a/Libraries/LibWeb/Geolocation/GeolocationPosition.idl b/Libraries/LibWeb/Geolocation/GeolocationPosition.idl
index 477b6417476..ee69bc4dbba 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationPosition.idl
+++ b/Libraries/LibWeb/Geolocation/GeolocationPosition.idl
@@ -1,7 +1,7 @@
// https://w3c.github.io/geolocation/#position_interface
-[Exposed=Window, SecureContext]
+[Exposed=Window, SecureContext, ImplementedIn=Rust]
interface GeolocationPosition {
- [DirectGetter] readonly attribute GeolocationCoordinates coords;
+ readonly attribute GeolocationCoordinates coords;
readonly attribute EpochTimeStamp timestamp;
[Default] object toJSON();
};
diff --git a/Libraries/LibWeb/Geolocation/GeolocationPositionError.cpp b/Libraries/LibWeb/Geolocation/GeolocationPositionError.cpp
index bfdc57187d3..aeceaf18d18 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationPositionError.cpp
+++ b/Libraries/LibWeb/Geolocation/GeolocationPositionError.cpp
@@ -4,30 +4,20 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <LibJS/HostClassBuilder.h>
+#include <LibJS/Runtime/VM.h>
#include <LibWeb/Geolocation/GeolocationPositionError.h>
namespace Web::Geolocation {
-GC_DEFINE_ALLOCATOR(GeolocationPositionError);
-
-GeolocationPositionError::GeolocationPositionError(ErrorCode code)
- : m_code(code)
-{
+extern "C" {
+// Defined in Bindings/Rust/src/geolocation/geolocation_position_error.rs.
+GeolocationPositionError* web_rust_geolocation_create_geolocation_position_error(JSVM*, u16 code);
}
-// https://w3c.github.io/geolocation/#message-attribute
-Utf16String GeolocationPositionError::message() const
+GC::Ref<GeolocationPositionError> GeolocationPositionError::create(ErrorCode code)
{
- // The message attribute is a developer-friendly textual description of the code attribute.
- switch (m_code) {
- case ErrorCode::PositionUnavailable:
- return "Position unavailable"_utf16;
- case ErrorCode::PermissionDenied:
- return "Permission denied"_utf16;
- case ErrorCode::Timeout:
- return "Timeout"_utf16;
- }
- VERIFY_NOT_REACHED();
+ return *web_rust_geolocation_create_geolocation_position_error(JS::HostABI::vm_to_abi(JS::VM::the()), to_underlying(code));
}
}
diff --git a/Libraries/LibWeb/Geolocation/GeolocationPositionError.h b/Libraries/LibWeb/Geolocation/GeolocationPositionError.h
index 75c59902e9f..c6448d32410 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationPositionError.h
+++ b/Libraries/LibWeb/Geolocation/GeolocationPositionError.h
@@ -6,28 +6,20 @@
#pragma once
-#include <AK/String.h>
-#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Bindings/GeolocationPositionError.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWebCommon/Geolocation/GeolocationPositionErrorCode.h>
-#include <LibWebCommon/WebIDL/Types.h>
namespace Web::Geolocation {
// https://w3c.github.io/geolocation/#dom-geolocationpositionerror
-class GeolocationPositionError : public Bindings::GCAllocatedWrappable {
- WEB_WRAPPABLE(GeolocationPositionError, Bindings::GCAllocatedWrappable);
- GC_DECLARE_ALLOCATOR(GeolocationPositionError);
+class GeolocationPositionError : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(GeolocationPositionError, geolocation_position_error)
public:
using ErrorCode = GeolocationPositionErrorCode;
- ErrorCode code() const { return m_code; }
- Utf16String message() const;
-
-private:
- explicit GeolocationPositionError(ErrorCode);
-
- ErrorCode m_code { 0 };
+ [[nodiscard]] static GC::Ref<GeolocationPositionError> create(ErrorCode);
};
}
diff --git a/Libraries/LibWeb/Geolocation/GeolocationPositionError.idl b/Libraries/LibWeb/Geolocation/GeolocationPositionError.idl
index 588cda96b28..ebdd256c564 100644
--- a/Libraries/LibWeb/Geolocation/GeolocationPositionError.idl
+++ b/Libraries/LibWeb/Geolocation/GeolocationPositionError.idl
@@ -1,5 +1,5 @@
// https://w3c.github.io/geolocation/#dom-geolocationpositionerror
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface GeolocationPositionError {
const unsigned short PERMISSION_DENIED = 1;
const unsigned short POSITION_UNAVAILABLE = 2;
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 2412cdabbec..024a01e07be 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -1719,7 +1719,7 @@ void Page::set_emulated_position_data(Geolocation::EmulatedPositionData data)
void Page::set_emulated_position_data(Geolocation::CoordinatesData coordinates_data)
{
- set_emulated_position_data(heap().allocate<Geolocation::GeolocationCoordinates>(move(coordinates_data)));
+ set_emulated_position_data(Geolocation::GeolocationCoordinates::create(coordinates_data));
}
u64 Page::register_emulated_position_data_observer(GC::Ref<GC::Function<void()>> observer)