Skip to content

Truly independent web browser

LibURL+LibWeb: Expose the URL of a Document to Rust

Shannon Booth0d93dcbd4598
diff --git a/Libraries/LibURL/Rust/src/ffi/url.rs b/Libraries/LibURL/Rust/src/ffi/url.rs
index 5b12e593177..ac0e0d5f5bb 100644
--- a/Libraries/LibURL/Rust/src/ffi/url.rs
+++ b/Libraries/LibURL/Rust/src/ffi/url.rs
@@ -93,6 +93,12 @@ impl RustUrl {
components: self.components,
}
}
+
+ /// # Safety
+ /// The serialization must be valid UTF-8 for the duration of this call, and the components must describe it.
+ pub unsafe fn to_url(&self) -> Url {
+ Url::from(unsafe { self.borrow() })
+ }
}
#[repr(C)]
diff --git a/Libraries/LibURL/Rust/src/lib.rs b/Libraries/LibURL/Rust/src/lib.rs
index 8df9b2ce8ef..31dfbdaf250 100644
--- a/Libraries/LibURL/Rust/src/lib.rs
+++ b/Libraries/LibURL/Rust/src/lib.rs
@@ -20,6 +20,7 @@ mod textcodec;
pub mod url;
pub use ffi::origin::FfiOrigin;
+pub use ffi::url::RustUrl;
pub use url::BasicParseOptions;
pub use url::State;
pub use url::Url;
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
index d4e4aeeefcf..0eef7cda409 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/global_object.rs
@@ -10,6 +10,7 @@ use core::ffi::c_void;
use core::ptr::NonNull;
use libjs_rust::api::{Gc, Object, Trace};
+use liburl_rust::Url;
use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::Wrappable;
@@ -18,6 +19,7 @@ unsafe extern "C" {
fn web_bindings_window_from_global_object(global_object: *mut c_void) -> *mut c_void;
fn web_bindings_window_associated_document(window: *mut c_void) -> *mut c_void;
fn web_bindings_document_is_fully_active(document: *mut c_void) -> bool;
+ fn web_bindings_document_url(document: *mut c_void) -> liburl_rust::RustUrl;
fn web_bindings_worker_global_scope_from_global_object(global_object: *mut c_void) -> *mut c_void;
fn web_bindings_worker_global_scope_is_closing(worker_global_scope: *mut c_void) -> bool;
}
@@ -86,6 +88,12 @@ impl CppDocument {
// SAFETY: The Document is live.
unsafe { web_bindings_document_is_fully_active(self.0.as_ptr()) }
}
+
+ /// https://dom.spec.whatwg.org/#concept-document-url
+ pub(crate) fn url(&self) -> Url {
+ // SAFETY: The Document is live, and LibWeb lends its URL, which this copies before the Document can change it.
+ unsafe { web_bindings_document_url(self.0.as_ptr()).to_url() }
+ }
}
impl CppWorkerGlobalScope {
diff --git a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
index 42fb78b83d3..29ad194c045 100644
--- a/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
+++ b/Libraries/LibWeb/Bindings/RustBindingsServices.cpp
@@ -116,6 +116,7 @@ bool web_bindings_realm_is_secure_context(void* realm);
HTML::Window* web_bindings_window_from_global_object(JSObject* global_object);
DOM::Document* web_bindings_window_associated_document(HTML::Window*);
bool web_bindings_document_is_fully_active(DOM::Document*);
+URL::FFI::RustUrl web_bindings_document_url(DOM::Document*);
HTML::WorkerGlobalScope* web_bindings_worker_global_scope_from_global_object(JSObject* global_object);
bool web_bindings_worker_global_scope_is_closing(HTML::WorkerGlobalScope*);
void web_bindings_with_temporary_execution_context(void* realm, bool callbacks_enabled, void* context, void (*steps)(void*));
@@ -214,6 +215,11 @@ bool web_bindings_document_is_fully_active(DOM::Document* document)
return document->is_fully_active();
}
+URL::FFI::RustUrl web_bindings_document_url(DOM::Document* document)
+{
+ return document->url().to_rust();
+}
+
HTML::WorkerGlobalScope* web_bindings_worker_global_scope_from_global_object(JSObject* global_object)
{
return Bindings::worker_global_scope_from_global_object(*JS::HostABI::object_from_abi(global_object));
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 6a4c5d8b220..6c879b9751e 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -407,7 +407,7 @@ public:
void set_referrer(Utf16String);
void set_url(URL::URL const& url);
- URL::URL url() const { return m_url; }
+ URL::URL const& url() const { return m_url; }
URL::URL fallback_base_url() const;
URL::URL base_url() const;
Optional<URL::URL> get_the_base_url() const;