Truly independent web browser
LibWeb: Move the Rust storage key out of IndexedDB
Shannon Boothb24d06b9e54b
diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/cpp.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/cpp.rsindex 8f5b87f5687..3ab8cace0b3 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/cpp.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/cpp.rs@@ -11,8 +11,6 @@ use core::ptr::NonNull;use ak::Utf16String;use libjs_rust::api::{EmbedderCell, Gc, Object, Realm, Trace, Vm};-use liburl_rust::FfiOrigin;-use liburl_rust::origin::Origin;use crate::bindings::conversions::CppInterface;use crate::bindings::cpp_cell::CppCell;@@ -21,17 +19,11 @@ use crate::indexed_db::internal::database::Database;use crate::indexed_db::internal::function::{Function, FunctionCell, call_function};use crate::indexed_db::internal::key::{Key, KeyValue};use crate::indexed_db::internal::mutation_log::{TransactionChange, TransactionChanges};-use crate::indexed_db::internal::storage::StorageKey;unsafe extern "C" {fn web_indexed_db_create_dom_exception(name: ak::Span<'_, u8>, message: usize) -> *mut c_void;fn web_indexed_db_dom_exception_from_value(value: u64) -> *mut c_void;fn web_indexed_db_wrap_dom_exception(realm: *mut c_void, exception: *mut c_void) -> *mut c_void;- fn web_indexed_db_obtain_a_storage_key(- realm: *mut c_void,- ctx: *mut c_void,- on_origin: unsafe extern "C" fn(*mut c_void, *const FfiOrigin),- ) -> bool;fn web_indexed_db_create_dom_string_list(realm: *mut c_void, strings: *const usize, count: usize) -> *mut c_void;fn web_indexed_db_queue_a_database_task(cell: *mut c_void, steps: unsafe extern "C" fn(*mut c_void, *mut c_void));fn web_indexed_db_run_in_parallel(cell: *mut c_void, steps: unsafe extern "C" fn(*mut c_void, *mut c_void));@@ -101,20 +93,6 @@ impl DomException {}}-/// https://storage.spec.whatwg.org/#obtain-a-storage-key, given the relevant settings object of the realm's global-/// object.-pub(crate) fn obtain_a_storage_key(realm: Gc<Realm>) -> Option<StorageKey> {- unsafe extern "C" fn on_origin(ctx: *mut c_void, ffi_origin: *const FfiOrigin) {- // SAFETY: ctx is the origin below, and LibWeb lends the origin for the call.- unsafe { *ctx.cast::<Option<Origin>>() = Some((*ffi_origin).to_origin()) };- }-- let mut origin: Option<Origin> = None;- // SAFETY: The realm is live, and the origin outlives the call.- unsafe { web_indexed_db_obtain_a_storage_key(realm.as_ptr().cast(), (&raw mut origin).cast(), on_origin) };- origin.map(|origin| StorageKey { origin })-}-pub(crate) fn create_dom_string_list(cx: &CallContext, strings: &[Utf16String]) -> CppInterface {let raw_strings: Vec<usize> = strings.iter().map(Utf16String::raw_identity).collect();// SAFETY: The realm is live, the strings are lent for the call, and LibWeb returns a live wrapper.diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/exports.rsindex e9713d3efc9..8736a9617b1 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/exports.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/exports.rs@@ -22,7 +22,7 @@ use crate::indexed_db::internal::algorithms::{delete_records_from_an_object_store,};use crate::indexed_db::internal::database::Database;-use crate::indexed_db::internal::storage::StorageKey;+use crate::storage_api::storage_key::StorageKey;/// # Safety///diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rsindex c048eb85b3c..172b41031df 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs@@ -16,7 +16,7 @@ use crate::bindings::event_target::CppEvent;use crate::bindings::interface::{Ref, Wrappable, allocate};use crate::bindings::promise::{WebIdlPromise, create_promise, reject_promise, resolve_promise};use crate::bindings::{CallContext, Exception, WebIdlResult};-use crate::indexed_db::cpp::{obtain_a_storage_key, run_in_parallel};+use crate::indexed_db::cpp::run_in_parallel;use crate::indexed_db::idb_open_db_request::IDBOpenDBRequest;use crate::indexed_db::idb_request::AnyIDBRequest;use crate::indexed_db::internal::algorithms::{@@ -26,6 +26,7 @@ use crate::indexed_db::internal::algorithms::{use crate::indexed_db::internal::database::{Database, DatabaseRef};use crate::indexed_db::internal::function::{NoCells, create_function, create_function_with};use crate::indexed_db::internal::key::Key;+use crate::storage_api::storage_key::obtain_a_storage_key;/// The dictionary IDBDatabaseInfo of IndexedDB/IDBFactory.idl.pub(crate) struct IDBDatabaseInfo {diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rsindex 281829249c8..79de103c34d 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/algorithms.rs@@ -47,7 +47,8 @@ use crate::indexed_db::internal::function::{use crate::indexed_db::internal::index::{Index, IndexRef};use crate::indexed_db::internal::key::{Key, KeyType, code_unit_order};use crate::indexed_db::internal::object_store::{ObjectStore, ObjectStoreRef};-use crate::indexed_db::internal::storage::{ConnectionQueueRef, StorageKey, storage};+use crate::indexed_db::internal::storage::{ConnectionQueueRef, storage};+use crate::storage_api::storage_key::StorageKey;/// A key path, https://w3c.github.io/IndexedDB/#key-path-construct: a string or a list of strings.#[derive(Clone)]diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/database.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/database.rsindex 5df403b6636..f29ffff8d16 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/database.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/database.rs@@ -18,7 +18,8 @@ use crate::indexed_db::idb_transaction::IDBTransaction;use crate::indexed_db::internal::algorithms::queue_a_database_task;use crate::indexed_db::internal::function::Function;use crate::indexed_db::internal::object_store::ObjectStoreRef;-use crate::indexed_db::internal::storage::{StorageKey, storage};+use crate::indexed_db::internal::storage::storage;+use crate::storage_api::storage_key::StorageKey;type WeakConnection = GcWeak<EmbedderCell<crate::bindings::interface::Wrapped<IDBDatabase>>>;diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rsindex b75849b416d..27d3956f82a 100644--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/internal/storage.rs@@ -14,32 +14,15 @@ use core::ptr::NonNull;use ak::Utf16String;use libjs_rust::api::{EmbedderCell, EmbedderClass, EmbedderValue, Gc, GcRefCell, Trace, Vm};-use liburl_rust::origin::Origin;use crate::indexed_db::internal::database::DatabaseRef;use crate::indexed_db::internal::request_list::RequestList;+use crate::storage_api::storage_key::StorageKey;unsafe extern "C" {fn gc_root_create(cell: *mut c_void) -> *mut c_void;}-// https://storage.spec.whatwg.org/#storage-keys-#[derive(Clone)]-pub(crate) struct StorageKey {- // A storage key is a tuple consisting of an origin (an origin). [HTML]- // NOTE: This is expected to change; see Client-Side Storage Partitioning https://privacycg.github.io/storage-partitioning/.- pub(crate) origin: Origin,-}--impl PartialEq for StorageKey {- fn eq(&self, other: &Self) -> bool {- // To determine whether a storage key A equals storage key B, run these steps:- // 1. If A’s origin is not same origin with B’s origin, then return false.- // 2. Return true.- self.origin.is_same_origin(&other.origin)- }-}-// https://w3c.github.io/IndexedDB/#connection-queues#[derive(Trace)]#[gc(crate = libjs_rust)]diff --git a/Libraries/LibWeb/Bindings/Rust/src/lib.rs b/Libraries/LibWeb/Bindings/Rust/src/lib.rsindex 34b63ea3682..59d14b4e2ba 100644--- a/Libraries/LibWeb/Bindings/Rust/src/lib.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/lib.rs@@ -116,6 +116,8 @@ pub(crate) mod indexed_db;allow(dead_code, reason = "only the generated bindings use the interfaces"))]pub(crate) mod speech;+#[cfg(not(test))]+pub(crate) mod storage_api;#[cfg_attr(not(web_idl_bindings),allow(dead_code, reason = "only the generated bindings use the interfaces")diff --git a/Libraries/LibWeb/Bindings/Rust/src/storage_api/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/storage_api/mod.rsnew file mode 100644index 00000000000..56cdf15aa72--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/storage_api/mod.rs@@ -0,0 +1,9 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! The Storage Standard, https://storage.spec.whatwg.org/++pub(crate) mod storage_key;diff --git a/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs b/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rsnew file mode 100644index 00000000000..13582ef9dc5--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs@@ -0,0 +1,52 @@+/*+ * Copyright (c) 2024, Andrew Kaster <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! Storage keys, https://storage.spec.whatwg.org/#storage-keys++use core::ffi::c_void;++use libjs_rust::api::{Gc, Realm};+use liburl_rust::FfiOrigin;+use liburl_rust::origin::Origin;++unsafe extern "C" {+ fn web_storage_api_obtain_a_storage_key(+ realm: *mut c_void,+ ctx: *mut c_void,+ on_origin: unsafe extern "C" fn(*mut c_void, *const FfiOrigin),+ ) -> bool;+}++// https://storage.spec.whatwg.org/#storage-keys+#[derive(Clone)]+pub(crate) struct StorageKey {+ // A storage key is a tuple consisting of an origin (an origin). [HTML]+ // NOTE: This is expected to change; see Client-Side Storage Partitioning https://privacycg.github.io/storage-partitioning/.+ pub(crate) origin: Origin,+}++impl PartialEq for StorageKey {+ fn eq(&self, other: &Self) -> bool {+ // To determine whether a storage key A equals storage key B, run these steps:+ // 1. If A’s origin is not same origin with B’s origin, then return false.+ // 2. Return true.+ self.origin.is_same_origin(&other.origin)+ }+}++unsafe extern "C" fn on_origin(ctx: *mut c_void, ffi_origin: *const FfiOrigin) {+ // SAFETY: ctx is the origin of the caller, and LibWeb lends the origin for the call.+ unsafe { *ctx.cast::<Option<Origin>>() = Some((*ffi_origin).to_origin()) };+}++/// https://storage.spec.whatwg.org/#obtain-a-storage-key+pub(crate) fn obtain_a_storage_key(realm: Gc<Realm>) -> Option<StorageKey> {+ let mut origin: Option<Origin> = None;+ // SAFETY: The realm is live, and the origin outlives the call.+ unsafe { web_storage_api_obtain_a_storage_key(realm.as_ptr().cast(), (&raw mut origin).cast(), on_origin) };+ origin.map(|origin| StorageKey { origin })+}diff --git a/Libraries/LibWeb/IndexedDB/RustIntegration.cpp b/Libraries/LibWeb/IndexedDB/RustIntegration.cppindex 0987c731f8f..14ad6b195dc 100644--- a/Libraries/LibWeb/IndexedDB/RustIntegration.cpp+++ b/Libraries/LibWeb/IndexedDB/RustIntegration.cpp@@ -11,7 +11,6 @@#include <LibGC/Weak.h>#include <LibJS/HostClassBuilder.h>#include <LibJS/Runtime/Realm.h>-#include <LibURL/RustIntegration.h>#include <LibWeb/Bindings/MainThreadVM.h>#include <LibWeb/Bindings/RustInterface.h>#include <LibWeb/Bindings/Wrappable.h>@@ -22,7 +21,6 @@#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/Page/Page.h>#include <LibWeb/Platform/EventLoopPlugin.h>-#include <LibWeb/StorageAPI/StorageKey.h>#include <LibWeb/WebIDL/DOMException.h>#include <LibWebCommon/IndexedDB/TransactionChanges.h>@@ -38,7 +36,6 @@ void web_rust_indexed_db_check_pending_connection_wait(JSVM*, void* database);void* web_indexed_db_create_dom_exception(ReadonlyBytes name, FlatPtr message);void* web_indexed_db_dom_exception_from_value(JSValue);JSObject* web_indexed_db_wrap_dom_exception(void* realm, void* exception);-bool web_indexed_db_obtain_a_storage_key(void* realm, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));JSObject* web_indexed_db_create_dom_string_list(void* realm, FlatPtr const* strings, size_t count);void web_indexed_db_queue_a_database_task(void* cell, void (*steps)(JSVM*, void*));void web_indexed_db_run_in_parallel(void* cell, void (*steps)(JSVM*, void*));@@ -76,18 +73,6 @@ JSObject* web_indexed_db_wrap_dom_exception(void* realm, void* exception)return JS::HostABI::object_to_abi(Bindings::wrap(Bindings::host_defined_wrapper_world(js_realm), js_realm, GC::Ref { dom_exception }).ptr());}-bool web_indexed_db_obtain_a_storage_key(void* realm, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*))-{- auto& js_realm = *static_cast<JS::Realm*>(realm);- auto storage_key = StorageAPI::obtain_a_storage_key(HTML::relevant_settings_object(js_realm.global_object()));- if (!storage_key.has_value())- return false;- URL::RustIntegration::lend_origin_to_ffi(storage_key->origin, [&](URL::FFI::FfiOrigin const& origin) {- on_origin(ctx, &origin);- });- return true;-}-JSObject* web_indexed_db_create_dom_string_list(void* realm, FlatPtr const* strings, size_t count){auto& js_realm = *static_cast<JS::Realm*>(realm);diff --git a/Libraries/LibWeb/StorageAPI/StorageKey.cpp b/Libraries/LibWeb/StorageAPI/StorageKey.cppindex 4a35eca04f3..9d1273e497d 100644--- a/Libraries/LibWeb/StorageAPI/StorageKey.cpp+++ b/Libraries/LibWeb/StorageAPI/StorageKey.cpp@@ -4,6 +4,8 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <LibJS/Runtime/Realm.h>+#include <LibURL/RustIntegration.h>#include <LibWeb/DOMURL/DOMURL.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/StorageAPI/StorageKey.h>@@ -34,4 +36,22 @@ StorageKey obtain_a_storage_key_for_non_storage_purposes(HTML::Environment constreturn { move(origin) };}+// Declared on the Rust side in Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs.+extern "C" {++bool web_storage_api_obtain_a_storage_key(void* realm, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));+}++bool web_storage_api_obtain_a_storage_key(void* realm, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*))+{+ auto& js_realm = *static_cast<JS::Realm*>(realm);+ auto storage_key = StorageAPI::obtain_a_storage_key(HTML::relevant_settings_object(js_realm.global_object()));+ if (!storage_key.has_value())+ return false;+ URL::RustIntegration::lend_origin_to_ffi(storage_key->origin, [&](URL::FFI::FfiOrigin const& origin) {+ on_origin(ctx, &origin);+ });+ return true;+}+}diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex 78a40312ed3..9f044d7d605 100755--- a/Meta/Linters/check_libweb_realm_mentions.py+++ b/Meta/Linters/check_libweb_realm_mentions.py@@ -63,7 +63,7 @@ ALLOWED_REALM_MENTIONS = {),"IndexedDB": (4,- 12,+ 11,"the C++ glue of the Rust IndexedDB creates the factory in a global's realm and wraps values in a given realm",),"Infra": (2, 13, "JSON parse/serialization helpers still expose JS values in selected realms"),@@ -74,6 +74,7 @@ ALLOWED_REALM_MENTIONS = {41,"service worker jobs, caches, and container promises still materialize results in selected realms",),+ "StorageAPI": (1, 2, "the C++ glue of the Rust storage keys reads the settings object of a realm's global"),"Streams": (5, 13, "the C++ facade of the Rust streams creates streams and promises in caller-selected realms"),"TrustedTypes": (2, 9, "Trusted Types policy factory operations still use selected realms"),"WebAssembly": (