Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(window): encapsulate document wrapper binding

ldm08fba73c58731
diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rs
index 463856acd..eda6ac902 100644
--- a/moli-renderer-v8/src/context_bootstrap.rs
+++ b/moli-renderer-v8/src/context_bootstrap.rs
@@ -1004,7 +1004,9 @@ pub(crate) fn live_ranges_detached_text_split<'s>(
update_live_ranges_for_detached_text_split(scope, original, new_text, offset);
}
-pub(crate) use self::window_accessors::current_window_style_viewport;
+pub(crate) use self::window_accessors::{
+ bind_current_child_window_document, current_window_style_viewport,
+};
pub(crate) use session_history::{
initialize_main_session_history, install_session_history_position,
diff --git a/moli-renderer-v8/src/context_bootstrap/window_accessors.rs b/moli-renderer-v8/src/context_bootstrap/window_accessors.rs
index b9de5fe84..3779034e4 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_accessors.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_accessors.rs
@@ -8,6 +8,7 @@ use crate::{
};
mod child_context;
+mod document;
mod helpers;
mod interceptors;
mod surface;
@@ -17,9 +18,11 @@ use super::CHILD_BROWSING_CONTEXT_HANDLE_SLOT as WINDOW_CHILD_CONTEXT_HANDLE_SLO
const WINDOW_FRAME_ELEMENT_SLOT: &str = "__moliWindowFrameElement";
pub(super) use child_context::{
- window_credentialless_getter, window_cross_origin_isolated_getter, window_document_getter,
- window_frame_element_getter, window_length_getter,
+ window_credentialless_getter, window_cross_origin_isolated_getter, window_frame_element_getter,
+ window_length_getter,
};
+pub(crate) use document::bind_current_child_window_document;
+pub(super) use document::window_document_getter;
pub(super) use helpers::window_child_context_handle;
pub(crate) use helpers::{current_window_style_viewport, window_host_ptr};
pub(super) use interceptors::{
diff --git a/moli-renderer-v8/src/context_bootstrap/window_accessors/child_context.rs b/moli-renderer-v8/src/context_bootstrap/window_accessors/child_context.rs
index a0548e424..873632f0b 100644
--- a/moli-renderer-v8/src/context_bootstrap/window_accessors/child_context.rs
+++ b/moli-renderer-v8/src/context_bootstrap/window_accessors/child_context.rs
@@ -2,7 +2,6 @@ use super::helpers::{
window_child_context_handle, window_hidden_value, window_host_ptr, window_receiver,
};
use super::*;
-use crate::native_bridge::WindowEnvironmentSettings;
pub(in crate::context_bootstrap) fn window_length_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
@@ -92,61 +91,3 @@ pub(in crate::context_bootstrap) fn window_cross_origin_isolated_getter<'s>(
.unwrap_or_else(|| host.cross_origin_isolated());
rv.set_bool(isolated);
}
-
-pub(in crate::context_bootstrap) fn window_document_getter<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'_, v8::Value>,
-) {
- let Some(receiver) = window_receiver(scope, &args) else {
- return;
- };
- let Some(host_ptr) = window_host_ptr(scope, receiver) else {
- rv.set_null();
- return;
- };
- let Some(context) = receiver.get_creation_context(scope) else {
- rv.set_null();
- return;
- };
- // A borrowed getter runs in its function's realm. Resolve both settings
- // and wrappers in the receiver's realm, including a retained old Window.
- let scope = &mut v8::ContextScope::new(scope, context);
- let handle = if let Some(settings) = WindowEnvironmentSettings::for_current_realm(scope) {
- let window = context.global(scope);
- if let Some(document) = window_hidden_value(
- scope,
- window,
- WindowEnvironmentSettings::DOCUMENT_WRAPPER_SLOT,
- ) {
- rv.set(document);
- return;
- }
- settings.document_handle()
- } else if let Some(handle) = window_child_context_handle(scope, receiver) {
- // During bootstrap the Document wrapper can precede settings binding.
- // Only this exact live Window may follow the child's current route.
- let host = unsafe { &*host_ptr };
- if host
- .window_execution_context_identity_for_access_check(context)
- .is_none_or(|identity| !host.window_execution_context_identity_is_current(identity))
- {
- rv.set_null();
- return;
- }
- match unsafe { &mut *host_ptr }.child_browsing_context_document_wrapper(scope, handle) {
- Some(document) => rv.set(document.into()),
- None => rv.set_null(),
- }
- return;
- } else {
- unsafe { &*host_ptr }.document_handle()
- };
- match unsafe { &mut *host_ptr }
- .native_bridge_mut()
- .wrap_handle(scope, host_ptr, handle)
- {
- Some(document) => rv.set(document.into()),
- None => rv.set_null(),
- }
-}
diff --git a/moli-renderer-v8/src/context_bootstrap/window_accessors/document.rs b/moli-renderer-v8/src/context_bootstrap/window_accessors/document.rs
new file mode 100644
index 000000000..77cc60425
--- /dev/null
+++ b/moli-renderer-v8/src/context_bootstrap/window_accessors/document.rs
@@ -0,0 +1,77 @@
+use super::helpers::{window_child_context_handle, window_host_ptr, window_receiver};
+use crate::{
+ document_runtime::DomHandle,
+ native_bridge::{JsContextHost, WindowEnvironmentSettings},
+ util::{get_private_object, set_private_value},
+};
+
+const WINDOW_DOCUMENT_SLOT: &str = "__moliWindowAssociatedDocument";
+
+pub(crate) fn bind_current_child_window_document<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ host: &JsContextHost,
+ child_handle: DomHandle,
+ document: v8::Local<'s, v8::Object>,
+) -> Option<()> {
+ WindowEnvironmentSettings::bind_current_child_document(scope, host, child_handle)?;
+ let window = scope.get_current_context().global(scope);
+ // Realm retirement clears the shared wrapper cache. Keep the original
+ // Document as a private V8 edge so Window.document retains object identity
+ // without a Rust Global keeping this Context alive.
+ set_private_value(scope, window, WINDOW_DOCUMENT_SLOT, document.into());
+ Some(())
+}
+
+pub(in crate::context_bootstrap) fn window_document_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ let Some(receiver) = window_receiver(scope, &args) else {
+ return;
+ };
+ let Some(host_ptr) = window_host_ptr(scope, receiver) else {
+ rv.set_null();
+ return;
+ };
+ let Some(context) = receiver.get_creation_context(scope) else {
+ rv.set_null();
+ return;
+ };
+ // A borrowed getter runs in its function's realm. Resolve both settings
+ // and wrappers in the receiver's realm, including a retained old Window.
+ let scope = &mut v8::ContextScope::new(scope, context);
+ let handle = if let Some(settings) = WindowEnvironmentSettings::for_current_realm(scope) {
+ let window = context.global(scope);
+ if let Some(document) = get_private_object(scope, window, WINDOW_DOCUMENT_SLOT) {
+ rv.set(document.into());
+ return;
+ }
+ settings.document_handle()
+ } else if let Some(handle) = window_child_context_handle(scope, receiver) {
+ // During bootstrap the Document wrapper can precede settings binding.
+ // Only this exact live Window may follow the child's current route.
+ let host = unsafe { &*host_ptr };
+ if host
+ .window_execution_context_identity_for_access_check(context)
+ .is_none_or(|identity| !host.window_execution_context_identity_is_current(identity))
+ {
+ rv.set_null();
+ return;
+ }
+ match unsafe { &mut *host_ptr }.child_browsing_context_document_wrapper(scope, handle) {
+ Some(document) => rv.set(document.into()),
+ None => rv.set_null(),
+ }
+ return;
+ } else {
+ unsafe { &*host_ptr }.document_handle()
+ };
+ match unsafe { &mut *host_ptr }
+ .native_bridge_mut()
+ .wrap_handle(scope, host_ptr, handle)
+ {
+ Some(document) => rv.set(document.into()),
+ None => rv.set_null(),
+ }
+}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
index 728592a6d..650b61f78 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/document.rs
@@ -1,11 +1,12 @@
use super::super::{JsContextHost, WindowExecutionContextOwner};
use super::document_slots::sync_child_document_window_slots;
+use crate::context_bootstrap::bind_current_child_window_document;
use crate::document_runtime::DomHandle;
use crate::document_script_scheduler::FrameDocumentClassicScriptSchedulerWork;
use crate::dom::native::Node;
use crate::dom_parser::DOM_PARSER_FOREIGN_NODE_SLOT;
use crate::native_bridge::{
- OwnerDispatchScope, WindowEnvironmentSettings,
+ OwnerDispatchScope,
document::{detached_native_handle_for_runtime, is_html_document},
node::remove_child_to_current_reaction_queue,
throw_dom_exception,
@@ -112,9 +113,7 @@ impl JsContextHost {
self.clear_child_browsing_context_live_foreign_pairings(scope, document_handle);
install_child_document_stream_methods(scope, document, handle);
if let Some(window) = window {
- let _ = WindowEnvironmentSettings::bind_current_child_document(
- scope, self, handle, document,
- );
+ let _ = bind_current_child_window_document(scope, self, handle, document);
sync_child_document_window_slots(
scope,
document,
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/realm_state/initialize.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/realm_state/initialize.rs
index 142116a59..1da548f05 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/realm_state/initialize.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frame_runtime/realm_state/initialize.rs
@@ -6,13 +6,13 @@ use super::{
};
use crate::{
context_bootstrap::{
- WINDOW_NAME_SLOT, bind_window_navigator_identity_seed, bind_window_performance_seed,
- install_navigation_bootstrap_entry_for_holder,
+ WINDOW_NAME_SLOT, bind_current_child_window_document, bind_window_navigator_identity_seed,
+ bind_window_performance_seed, install_navigation_bootstrap_entry_for_holder,
reset_window_location_history_navigation_runtime_state, set_window_origin_runtime_state,
sync_window_location_history_navigation_runtime_surface,
},
native_bridge::{
- JsContextHost, OwnerDispatchScope, WindowEnvironmentSettings, WindowExecutionContextOwner,
+ JsContextHost, OwnerDispatchScope, WindowExecutionContextOwner,
child_window_surface::{
bind_materialized_child_window_indexed_db_factory,
initialize_child_window_realm_environment, rebind_child_window_document_environment,
@@ -65,7 +65,7 @@ pub(in crate::native_bridge::context_host::child_frame_runtime) fn initialize_ch
sync_child_document_window_slots(scope, document, global, true);
set_object_slot(scope, global, "document", document.into());
validate_child_window_realm_snapshot(host, &snapshot)?;
- bind_document_settings(host, scope, &snapshot, document)?;
+ bind_document(host, scope, &snapshot, document)?;
Ok(ChildWindowRealmProjection {
parent,
@@ -127,17 +127,17 @@ pub(in crate::native_bridge::context_host::child_frame_runtime) fn rebind_child_
sync_child_document_window_slots(scope, document, global, true);
set_object_slot(scope, global, "document", document.into());
validate_child_window_realm_snapshot(host, &snapshot)?;
- bind_document_settings(host, scope, &snapshot, document)
+ bind_document(host, scope, &snapshot, document)
}
-fn bind_document_settings<'s>(
+fn bind_document<'s>(
host: &JsContextHost,
scope: &mut v8::PinScope<'s, '_>,
snapshot: &super::model::ChildWindowRealmSnapshot,
document: v8::Local<'s, v8::Object>,
) -> Result<()> {
- WindowEnvironmentSettings::bind_current_child_document(scope, host, snapshot.handle, document)
- .ok_or_else(|| anyhow::anyhow!("missing current child settings Document"))
+ bind_current_child_window_document(scope, host, snapshot.handle, document)
+ .ok_or_else(|| anyhow::anyhow!("missing current child Window Document binding"))
}
fn validate_registered_realm(
diff --git a/moli-renderer-v8/src/native_bridge/context_host/window_execution_context/settings.rs b/moli-renderer-v8/src/native_bridge/context_host/window_execution_context/settings.rs
index f87081d33..ba58abba8 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/window_execution_context/settings.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/window_execution_context/settings.rs
@@ -2,7 +2,6 @@ use super::super::JsContextHost;
use super::super::OwnerDispatchScope;
use super::WindowExecutionContextIdentity;
use crate::document_runtime::DomHandle;
-use crate::util::set_private_value;
use moli_url::WebOrigin;
use std::rc::Rc;
use url::Url;
@@ -22,13 +21,10 @@ pub(crate) struct WindowEnvironmentSettings {
}
impl WindowEnvironmentSettings {
- pub(crate) const DOCUMENT_WRAPPER_SLOT: &'static str = "__moliWindowAssociatedDocument";
-
- pub(in crate::native_bridge::context_host) fn bind_current_child_document<'s>(
- scope: &mut v8::PinScope<'s, '_>,
+ pub(crate) fn bind_current_child_document(
+ scope: &mut v8::PinScope<'_, '_>,
host: &JsContextHost,
child_handle: DomHandle,
- document_wrapper: v8::Local<'s, v8::Object>,
) -> Option<()> {
// A reused element can point to a new Window. Only a currently
// registered realm may change its associated Document; a retained old
@@ -42,16 +38,6 @@ impl WindowEnvironmentSettings {
let owner = host.frame_owner_current_child_snapshot(child_handle)?;
let origin = WebOrigin::from_serialized(&owner.settings.origin);
let context = scope.get_current_context();
- let window = context.global(scope);
- // Realm retirement clears the shared wrapper cache. Keep the original
- // Document as a private V8 edge so Window.document retains object
- // identity without a Rust Global keeping this Context alive.
- set_private_value(
- scope,
- window,
- Self::DOCUMENT_WRAPPER_SLOT,
- document_wrapper.into(),
- );
let _previous = context.set_slot(Rc::new(Self {
document,
origin,