Truly independent web browser
LibURL: Keep the blob URL entry of a URL in Rust
Shannon Booth18faf9e68363
diff --git a/Libraries/LibURL/Rust/src/ffi/url.rs b/Libraries/LibURL/Rust/src/ffi/url.rsindex ac0e0d5f5bb..4943f6b05e4 100644--- a/Libraries/LibURL/Rust/src/ffi/url.rs+++ b/Libraries/LibURL/Rust/src/ffi/url.rs@@ -8,6 +8,7 @@ use crate::rust_panic::abort_on_panic;use std::ffi::c_void;use crate::url::BasicParseOptions;+use crate::url::BlobUrlEntry;use crate::url::HostKind;use crate::url::State;use crate::url::Url;@@ -75,13 +76,15 @@ impl RustUrlByteSlice {pub struct RustUrl {pub serialization: RustUrlByteSlice,pub components: UrlComponents,+ pub blob_url_entry: *const c_void,}impl RustUrl {- fn from_url(url: &Url) -> Self {+ pub fn from_url(url: &Url) -> Self {Self {serialization: RustUrlByteSlice::from_str(url.as_str()),components: url.components(),+ blob_url_entry: url.blob_url_entry().map_or(std::ptr::null(), BlobUrlEntry::as_ptr),}}@@ -91,13 +94,18 @@ impl RustUrl {Url {serialization: unsafe { self.serialization.borrow() }.unwrap_or(""),components: self.components,+ blob_url_entry: (),}}/// # Safety- /// The serialization must be valid UTF-8 for the duration of this call, and the components must describe it.+ /// The serialization must be valid UTF-8 for the duration of this call, the components must describe it, and the+ /// blob URL entry must be null or live.pub unsafe fn to_url(&self) -> Url {- Url::from(unsafe { self.borrow() })+ Url {+ blob_url_entry: unsafe { BlobUrlEntry::from_borrowed(self.blob_url_entry) },+ ..Url::from(unsafe { self.borrow() })+ }}}@@ -151,7 +159,7 @@ unsafe fn modify_url(on_complete: FfiUrlResultFn,modify: impl FnOnce(&mut Url),) {- let mut url = Url::from(unsafe { (*url).borrow() });+ let mut url = unsafe { (*url).to_url() };modify(&mut url);let result = RustUrl::from_url(&url);// SAFETY: result borrows from url, which lives until the callback returns.diff --git a/Libraries/LibURL/Rust/src/lib.rs b/Libraries/LibURL/Rust/src/lib.rsindex 31dfbdaf250..cc308d20caf 100644--- a/Libraries/LibURL/Rust/src/lib.rs+++ b/Libraries/LibURL/Rust/src/lib.rs@@ -45,6 +45,14 @@ extern "C" fn url_rust_registrable_domain() {}+#[cfg(test)]+#[unsafe(no_mangle)]+extern "C" fn url_rust_blob_url_entry_ref(_entry: *const std::ffi::c_void) {}++#[cfg(test)]+#[unsafe(no_mangle)]+extern "C" fn url_rust_blob_url_entry_unref(_entry: *const std::ffi::c_void) {}+// 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.rsindex 94e28b427fb..2a4ae6575fa 100644--- a/Libraries/LibURL/Rust/src/origin.rs+++ b/Libraries/LibURL/Rust/src/origin.rs@@ -208,7 +208,7 @@ impl Origin {}}-impl<Serialization: AsRef<str>> Url<Serialization> {+impl<Serialization: AsRef<str>, BlobUrlEntrySlot> Url<Serialization, BlobUrlEntrySlot> {fn host(&self) -> Host {Host {kind: self.host_kind(),diff --git a/Libraries/LibURL/Rust/src/url/blob_url_entry.rs b/Libraries/LibURL/Rust/src/url/blob_url_entry.rsnew file mode 100644index 00000000000..f60f03650d1--- /dev/null+++ b/Libraries/LibURL/Rust/src/url/blob_url_entry.rs@@ -0,0 +1,53 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++use std::ffi::c_void;+use std::fmt;+use std::ptr::NonNull;++unsafe extern "C" {+ fn url_rust_blob_url_entry_ref(entry: *const c_void);+ fn url_rust_blob_url_entry_unref(entry: *const c_void);+}++// https://w3c.github.io/FileAPI/#blob-url-entry+pub struct BlobUrlEntry(NonNull<c_void>);++impl BlobUrlEntry {+ /// # Safety+ /// `entry` must be null or the blob URL entry of a URL of LibURL's C++ code, borrowed for the duration of the call.+ pub unsafe fn from_borrowed(entry: *const c_void) -> Option<Self> {+ let entry = NonNull::new(entry.cast_mut())?;+ // SAFETY: The caller passes a live entry, which this keeps a reference to.+ unsafe { url_rust_blob_url_entry_ref(entry.as_ptr()) };+ Some(Self(entry))+ }++ pub fn as_ptr(&self) -> *const c_void {+ self.0.as_ptr()+ }+}++impl Clone for BlobUrlEntry {+ fn clone(&self) -> Self {+ // SAFETY: This keeps a reference to the entry.+ unsafe { url_rust_blob_url_entry_ref(self.as_ptr()) };+ Self(self.0)+ }+}++impl Drop for BlobUrlEntry {+ fn drop(&mut self) {+ // SAFETY: This keeps a reference to the entry, which it lets go of.+ unsafe { url_rust_blob_url_entry_unref(self.as_ptr()) };+ }+}++impl fmt::Debug for BlobUrlEntry {+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {+ formatter.debug_tuple("BlobUrlEntry").field(&self.0).finish()+ }+}diff --git a/Libraries/LibURL/Rust/src/url/mod.rs b/Libraries/LibURL/Rust/src/url/mod.rsindex e9d8ea4bd2d..424de5d371e 100644--- a/Libraries/LibURL/Rust/src/url/mod.rs+++ b/Libraries/LibURL/Rust/src/url/mod.rs@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+mod blob_url_entry;pub mod form_urlencoded;mod host;mod input;@@ -13,6 +14,7 @@ mod scheme;mod serialize;mod types;+pub use self::blob_url_entry::BlobUrlEntry;pub(crate) use self::host::parse_host_into;pub use self::input::UrlInput;pub use self::percent_encoding::percent_decode_bytes;diff --git a/Libraries/LibURL/Rust/src/url/types.rs b/Libraries/LibURL/Rust/src/url/types.rsindex 0072fb4b78a..fc56100e7df 100644--- a/Libraries/LibURL/Rust/src/url/types.rs+++ b/Libraries/LibURL/Rust/src/url/types.rs@@ -4,8 +4,10 @@* SPDX-License-Identifier: BSD-2-Clause*/+use std::hash::{Hash, Hasher};use std::ops::Range;+use super::BlobUrlEntry;use super::UrlInput;use super::percent_encoding::PercentEncodeSet;use super::percent_encoding::percent_encode_input_into;@@ -130,15 +132,34 @@ enum Offset {// https://url.spec.whatwg.org/#url-representation// A URL is a struct that represents a universal identifier.// To disambiguate from a valid URL string it can also be referred to as a URL record.-#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]-pub struct Url<Serialization = String> {+#[derive(Clone, Copy, Debug, Default)]+pub struct Url<Serialization = String, BlobUrlEntrySlot = Option<BlobUrlEntry>> {pub(crate) serialization: Serialization,pub(crate) components: UrlComponents,++ // https://url.spec.whatwg.org/#concept-url-blob-entry+ // A URL also has an associated blob URL entry that is either null or a blob URL entry. It is initially null.+ pub(crate) blob_url_entry: BlobUrlEntrySlot,}-pub type UrlRef<'a> = Url<&'a str>;+pub type UrlRef<'a> = Url<&'a str, ()>;-impl<Serialization: AsRef<str>> Url<Serialization> {+impl<Serialization: PartialEq, BlobUrlEntrySlot> PartialEq for Url<Serialization, BlobUrlEntrySlot> {+ fn eq(&self, other: &Self) -> bool {+ self.serialization == other.serialization && self.components == other.components+ }+}++impl<Serialization: Eq, BlobUrlEntrySlot> Eq for Url<Serialization, BlobUrlEntrySlot> {}++impl<Serialization: Hash, BlobUrlEntrySlot> Hash for Url<Serialization, BlobUrlEntrySlot> {+ fn hash<H: Hasher>(&self, state: &mut H) {+ self.serialization.hash(state);+ self.components.hash(state);+ }+}++impl<Serialization: AsRef<str>, BlobUrlEntrySlot> Url<Serialization, BlobUrlEntrySlot> {fn slice(&self, start: u32, end: u32) -> &str {&self.as_str()[start as usize..end as usize]}@@ -155,6 +176,7 @@ impl<Serialization: AsRef<str>> Url<Serialization> {Url {serialization: self.as_str(),components: self.components,+ blob_url_entry: (),}}@@ -287,6 +309,7 @@ impl From<UrlRef<'_>> for Url {Url {serialization: url.serialization.to_owned(),components: url.components,+ blob_url_entry: None,}}}@@ -695,3 +718,10 @@ pub(crate) struct DetachedTail {query: Option<String>,fragment: Option<String>,}++impl Url {+ // https://url.spec.whatwg.org/#concept-url-blob-entry+ pub fn blob_url_entry(&self) -> Option<&BlobUrlEntry> {+ self.blob_url_entry.as_ref()+ }+}diff --git a/Libraries/LibURL/URL.cpp b/Libraries/LibURL/URL.cppindex 8d1d326bf90..ac3edf63894 100644--- a/Libraries/LibURL/URL.cpp+++ b/Libraries/LibURL/URL.cpp@@ -148,6 +148,7 @@ FFI::RustUrl URL::to_rust() constreturn {.serialization = { m_serialization.bytes().data(), m_serialization.bytes().size() },.components = m_components,+ .blob_url_entry = m_blob_url_entry.ptr(),};}@@ -155,6 +156,20 @@ void URL::set_from_rust(FFI::RustUrl const& rust_url){m_serialization = String::from_ascii_without_validation({ rust_url.serialization.data, rust_url.serialization.length });m_components = rust_url.components;+ m_blob_url_entry = static_cast<BlobURLEntryStorage const*>(rust_url.blob_url_entry);+}++extern "C" void url_rust_blob_url_entry_ref(void const* entry);+extern "C" void url_rust_blob_url_entry_unref(void const* entry);++extern "C" void url_rust_blob_url_entry_ref(void const* entry)+{+ static_cast<URL::BlobURLEntryStorage const*>(entry)->ref();+}++extern "C" void url_rust_blob_url_entry_unref(void const* entry)+{+ static_cast<URL::BlobURLEntryStorage const*>(entry)->unref();}void URL::set_from_rust_callback(void* url, FFI::RustUrl const* rust_url)diff --git a/Libraries/LibURL/URL.h b/Libraries/LibURL/URL.hindex e2f25e290ac..006dd6de285 100644--- a/Libraries/LibURL/URL.h+++ b/Libraries/LibURL/URL.h@@ -173,10 +173,6 @@ public:void set_from_rust(FFI::RustUrl const&);static void set_from_rust_callback(void* url, FFI::RustUrl const*);-private:- StringView view(u32 start, u32 end) const { return m_serialization.bytes_as_string_view().substring_view(start, end - start); }- u32 path_end() const;-struct BlobURLEntryStorage : public RefCounted<BlobURLEntryStorage> {explicit BlobURLEntryStorage(BlobURLEntry entry): entry(move(entry))@@ -186,6 +182,10 @@ private:BlobURLEntry entry;};+private:+ StringView view(u32 start, u32 end) const { return m_serialization.bytes_as_string_view().substring_view(start, end - start); }+ u32 path_end() const;+String m_serialization;FFI::UrlComponents m_components {.scheme_end = 0,diff --git a/Libraries/LibWeb/Rust/src/lib.rs b/Libraries/LibWeb/Rust/src/lib.rsindex 3da16449952..92c3a7f9253 100644--- a/Libraries/LibWeb/Rust/src/lib.rs+++ b/Libraries/LibWeb/Rust/src/lib.rs@@ -22,6 +22,8 @@ pub(crate) mod fork_vec;#[cfg(test)]mod gfx_test_stubs;pub(crate) mod heap_size;+#[cfg(test)]+mod url_test_stubs;pub(crate) use libcompositing_rust::fast_hash;pub(crate) mod css;diff --git a/Libraries/LibWeb/Rust/src/url_test_stubs.rs b/Libraries/LibWeb/Rust/src/url_test_stubs.rsnew file mode 100644index 00000000000..935803192ec--- /dev/null+++ b/Libraries/LibWeb/Rust/src/url_test_stubs.rs@@ -0,0 +1,14 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! LibURL's C++ is not linked into Rust unit tests.++use std::ffi::c_void;++#[unsafe(no_mangle)]+extern "C" fn url_rust_blob_url_entry_unref(_entry: *const c_void) {+ unreachable!("no unit test has a blob URL entry");+}