Skip to content

Truly independent web browser

LibWeb: Obtain storage keys in Rust given a settings object

Shannon Booth8372140070d4
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs b/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
index 6cac9264c20..1658f527ab1 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/broadcast_channel.rs
@@ -17,6 +17,7 @@ use libjs_rust::api::{EmbedderCell, EmbedderClass, EmbedderValue, Gc, GcWeak, Ma
use liburl_rust::FfiOrigin;
use liburl_rust::origin::Origin;
+use crate::bindings::environment_settings_object::relevant_settings_object;
use crate::bindings::event_target::{CppEvent, CppEventTarget, EventListenerListChanged, GetTheParent};
use crate::bindings::global_object::{
relevant_global_object, window_from_global_object, worker_global_scope_from_global_object,
@@ -172,7 +173,9 @@ impl BroadcastChannel {
// 2. Set this's closed flag to false.
closed_flag: Cell::new(false),
origin: relevant_settings_object_origin(realm),
- storage_key: obtain_a_storage_key_for_non_storage_purposes(realm),
+ storage_key: obtain_a_storage_key_for_non_storage_purposes(&relevant_settings_object(
+ realm.global_object(),
+ )),
});
channel.event_target.set_owner(channel);
channel.event_target.set_event_listener_list_changed_steps::<Self>();
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs b/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs
index a437d8f3968..85a3247d343 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/storage.rs
@@ -16,6 +16,7 @@ use libjs_rust::api::{EmbedderClass, EmbedderValue, Gc, MarkedVec, Realm, Throw,
use liburl_rust::url::ExcludeFragment;
use crate::bindings::conversions::UsvString;
+use crate::bindings::environment_settings_object::{CppEnvironmentSettingsObject, relevant_settings_object};
use crate::bindings::global_object::{CppWindow, active_windows, relevant_global_object, window_from_global_object};
use crate::bindings::interface::{Ref, Wrappable, allocate, cell_from_abi};
use crate::bindings::promise::{TaskSource, queue_a_global_task};
@@ -136,7 +137,7 @@ pub(crate) fn dispatch_storage_events(
// * relevant settings object's origin is same origin with storage's relevant settings object's origin
// NB: A storage key is an origin, and two keys are equal when their origins are same origin. Compared as the
// keys the bottles are kept under, so the file: documents that share one bottle hear each other.
- if obtain_a_storage_key(storage.realm).as_ref() != Some(storage_key) {
+ if obtain_a_storage_key(&storage.relevant_settings_object()).as_ref() != Some(storage_key) {
continue;
}
@@ -215,6 +216,11 @@ impl Storage {
&self.map
}
+ /// https://html.spec.whatwg.org/multipage/webappapis.html#relevant-settings-object
+ fn relevant_settings_object(&self) -> CppEnvironmentSettingsObject {
+ relevant_settings_object(relevant_global_object(self))
+ }
+
/// The Window that is the relevant global object.
fn window(&self) -> CppWindow {
window_from_global_object(relevant_global_object(self)).expect("a Storage belongs to a Window")
@@ -375,7 +381,7 @@ impl Storage {
// NB: The key the bottle is kept under, not the bare origin: Every file: document shares one bottle (see "obtain
// a storage key"), so each hears the others' changes. A window that has no key has no bottle to have written.
- let Some(storage_key) = obtain_a_storage_key(this.realm) else {
+ let Some(storage_key) = obtain_a_storage_key(&this.relevant_settings_object()) else {
return;
};
let session_page = (this.storage_type == StorageType::Session).then(|| this.window().page_id());
diff --git a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs
index 797a554af92..e624888d912 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/indexed_db/idb_factory.rs
@@ -12,6 +12,7 @@
use ak::Utf16String;
use libjs_rust::api::{Array, Gc, Must, Object, PrimitiveString, PropertyKey, Realm, Trace, Value, Vm};
+use crate::bindings::environment_settings_object::relevant_settings_object;
use crate::bindings::event_target::CppEvent;
use crate::bindings::interface::{Ref, Wrappable, allocate};
use crate::bindings::promise::{WebIdlPromise, create_promise, reject_promise, resolve_promise};
@@ -70,7 +71,7 @@ impl IDBFactory {
// 2. Let environment be this's relevant settings object.
// 3. Let storageKey be the result of running obtain a storage key given environment.
// If failure is returned, then throw a "SecurityError" DOMException and abort these steps.
- let Some(storage_key) = obtain_a_storage_key(realm) else {
+ let Some(storage_key) = obtain_a_storage_key(&relevant_settings_object(realm.global_object())) else {
return Err(Exception::Dom(
"SecurityError",
Utf16String::from_utf8("Failed to obtain a storage key"),
@@ -191,7 +192,7 @@ impl IDBFactory {
// 1. Let environment be this's relevant settings object.
// 2. Let storageKey be the result of running obtain a storage key given environment.
// If failure is returned, then throw a "SecurityError" DOMException and abort these steps.
- let Some(storage_key) = obtain_a_storage_key(realm) else {
+ let Some(storage_key) = obtain_a_storage_key(&relevant_settings_object(realm.global_object())) else {
return Err(Exception::Dom(
"SecurityError",
Utf16String::from_utf8("Failed to obtain a storage key"),
@@ -261,7 +262,7 @@ impl IDBFactory {
// 1. Let environment be this's relevant settings object.
// 2. Let storageKey be the result of running obtain a storage key given environment.
// If failure is returned, then return a promise rejected with a "SecurityError" DOMException
- let Some(storage_key) = obtain_a_storage_key(realm) else {
+ let Some(storage_key) = obtain_a_storage_key(&relevant_settings_object(realm.global_object())) else {
let cx = CallContext::new(vm, realm);
let error = Exception::Dom(
"SecurityError",
diff --git a/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs b/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs
index 9c021a21a60..39bc8d3fbc7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/storage_api/storage_key.rs
@@ -9,18 +9,19 @@
use core::ffi::c_void;
-use libjs_rust::api::{Gc, Realm};
use liburl_rust::FfiOrigin;
use liburl_rust::origin::Origin;
+use crate::bindings::environment_settings_object::CppEnvironmentSettingsObject;
+
unsafe extern "C" {
fn web_storage_api_obtain_a_storage_key(
- realm: *mut c_void,
+ environment: *mut c_void,
ctx: *mut c_void,
on_origin: unsafe extern "C" fn(*mut c_void, *const FfiOrigin),
) -> bool;
fn web_storage_api_obtain_a_storage_key_for_non_storage_purposes(
- realm: *mut c_void,
+ environment: *mut c_void,
ctx: *mut c_void,
on_origin: unsafe extern "C" fn(*mut c_void, *const FfiOrigin),
);
@@ -49,20 +50,20 @@ unsafe extern "C" fn on_origin(ctx: *mut c_void, ffi_origin: *const FfiOrigin) {
}
/// https://storage.spec.whatwg.org/#obtain-a-storage-key
-pub(crate) fn obtain_a_storage_key(realm: Gc<Realm>) -> Option<StorageKey> {
+pub(crate) fn obtain_a_storage_key(environment: &CppEnvironmentSettingsObject) -> 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) };
+ // SAFETY: The environment settings object is live, and the origin outlives the call.
+ unsafe { web_storage_api_obtain_a_storage_key(environment.as_ptr(), (&raw mut origin).cast(), on_origin) };
origin.map(|origin| StorageKey { origin })
}
/// https://storage.spec.whatwg.org/#obtain-a-storage-key-for-non-storage-purposes
-pub(crate) fn obtain_a_storage_key_for_non_storage_purposes(realm: Gc<Realm>) -> StorageKey {
+pub(crate) fn obtain_a_storage_key_for_non_storage_purposes(environment: &CppEnvironmentSettingsObject) -> StorageKey {
let mut origin: Option<Origin> = None;
- // SAFETY: The realm is live, and the origin outlives the call.
+ // SAFETY: The environment settings object is live, and the origin outlives the call.
unsafe {
web_storage_api_obtain_a_storage_key_for_non_storage_purposes(
- realm.as_ptr().cast(),
+ environment.as_ptr(),
(&raw mut origin).cast(),
on_origin,
);
diff --git a/Libraries/LibWeb/StorageAPI/StorageKey.cpp b/Libraries/LibWeb/StorageAPI/StorageKey.cpp
index caaede7fb33..d1900ee65ef 100644
--- a/Libraries/LibWeb/StorageAPI/StorageKey.cpp
+++ b/Libraries/LibWeb/StorageAPI/StorageKey.cpp
@@ -4,7 +4,6 @@
* 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>
@@ -39,14 +38,13 @@ StorageKey obtain_a_storage_key_for_non_storage_purposes(HTML::Environment const
// 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*));
-void web_storage_api_obtain_a_storage_key_for_non_storage_purposes(void* realm, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));
+bool web_storage_api_obtain_a_storage_key(HTML::EnvironmentSettingsObject* environment, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*));
+void web_storage_api_obtain_a_storage_key_for_non_storage_purposes(HTML::EnvironmentSettingsObject* environment, 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*))
+bool web_storage_api_obtain_a_storage_key(HTML::EnvironmentSettingsObject* environment, 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()));
+ auto storage_key = StorageAPI::obtain_a_storage_key(*environment);
if (!storage_key.has_value())
return false;
URL::RustIntegration::lend_origin_to_ffi(storage_key->origin, [&](URL::FFI::FfiOrigin const& origin) {
@@ -55,10 +53,9 @@ bool web_storage_api_obtain_a_storage_key(void* realm, void* ctx, void (*on_orig
return true;
}
-void web_storage_api_obtain_a_storage_key_for_non_storage_purposes(void* realm, void* ctx, void (*on_origin)(void*, URL::FFI::FfiOrigin const*))
+void web_storage_api_obtain_a_storage_key_for_non_storage_purposes(HTML::EnvironmentSettingsObject* environment, 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_for_non_storage_purposes(HTML::relevant_settings_object(js_realm.global_object()));
+ auto storage_key = StorageAPI::obtain_a_storage_key_for_non_storage_purposes(*environment);
URL::RustIntegration::lend_origin_to_ffi(storage_key.origin, [&](URL::FFI::FfiOrigin const& origin) {
on_origin(ctx, &origin);
});
diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.py
index 3b636b80aac..98acb70e7f4 100755
--- a/Meta/Linters/check_libweb_realm_mentions.py
+++ b/Meta/Linters/check_libweb_realm_mentions.py
@@ -79,12 +79,7 @@ ALLOWED_REALM_MENTIONS = {
41,
"service worker jobs, caches, and container promises still materialize results in selected realms",
),
- "StorageAPI": (
- 3,
- 6,
- "the C++ glue of the Rust storage keys reads the settings object of a realm's global, and the C++ glue of the "
- "Rust StorageManager creates one in a realm",
- ),
+ "StorageAPI": (2, 3, "the C++ glue of the Rust StorageManager creates one in a realm"),
"Streams": (7, 17, "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": (