Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(renderer): construct runtimes with document owners

ldm069037ba0ae27
diff --git a/moli-renderer-v8/src/document_runtime/lifecycle.rs b/moli-renderer-v8/src/document_runtime/lifecycle.rs
index 4203c67e3..ca04f7358 100644
--- a/moli-renderer-v8/src/document_runtime/lifecycle.rs
+++ b/moli-renderer-v8/src/document_runtime/lifecycle.rs
@@ -201,10 +201,11 @@ impl DocumentRuntime {
}
pub(crate) fn open_document(&mut self) {
- // Production runtimes replace this temporary standalone identity with
- // the exact new Frame owner in the surrounding owner transaction.
- // Rotating first invalidates parser work from the retired Document
- // even while that transaction is still being committed.
+ // Production main-frame runtimes finish this transition with
+ // `commit_main_document_open`. Rotating first invalidates parser work
+ // from the retired Document even while the surrounding owner
+ // transaction is still being committed. Standalone runtimes retain
+ // this exact fresh token as their new incarnation.
self.document_incarnation = DocumentRuntimeIncarnationIdentity::standalone();
// Blink's Document::ImplicitOpen removes every current child and leaves
// the Document empty. The parser creates html/head/body only when input
@@ -278,7 +279,15 @@ impl DocumentRuntime {
self.late_preload_stylesheet_handles.clear();
}
- pub(crate) fn bind_main_document_task_owner(&mut self, owner: FrameDocumentTaskOwner) {
+ /// Completes the exact owner transaction started by [`Self::open_document`].
+ pub(crate) fn commit_main_document_open(&mut self, owner: FrameDocumentTaskOwner) {
+ assert!(
+ matches!(
+ self.document_incarnation,
+ DocumentRuntimeIncarnationIdentity::Standalone(_)
+ ),
+ "main Document open must invalidate its retired incarnation before commit"
+ );
self.document_incarnation = DocumentRuntimeIncarnationIdentity::MainFrame(owner);
}
diff --git a/moli-renderer-v8/src/document_runtime/runtime_core.rs b/moli-renderer-v8/src/document_runtime/runtime_core.rs
index 8602d66f9..a7c951724 100644
--- a/moli-renderer-v8/src/document_runtime/runtime_core.rs
+++ b/moli-renderer-v8/src/document_runtime/runtime_core.rs
@@ -10,8 +10,40 @@ use super::*;
// Splitting them out is mostly about finishing the file-level separation so `document_runtime.rs`
// stops carrying even a small "misc core methods" tail while the actual behavior remains unchanged.
impl DocumentRuntime {
- pub(crate) fn from_dom_host(
+ /// Constructs the runtime for an already-admitted main Document.
+ ///
+ /// The owner is part of the runtime's initial identity; production callers
+ /// must not construct an ownerless runtime and attach it afterward.
+ pub(crate) fn from_main_frame_dom_host(
dom_host: DomHost,
+ main_document_owner: crate::frame_owner_model::FrameDocumentTaskOwner,
+ page_task_tx: Option<crate::page_task_queue::PageTaskSender>,
+ page_task_parser_boundary_injection_tx: tokio::sync::mpsc::UnboundedSender<PageTask>,
+ stylesheet_task_sender: crate::page_task_queue::RendererPageStylesheetTaskSender,
+ main_parser_continuation_sender: crate::page_task_queue::RendererPageMainParserContinuationSender,
+ ) -> Self {
+ Self::from_dom_host_with_incarnation(
+ dom_host,
+ DocumentRuntimeIncarnationIdentity::MainFrame(main_document_owner),
+ page_task_tx,
+ page_task_parser_boundary_injection_tx,
+ stylesheet_task_sender,
+ main_parser_continuation_sender,
+ )
+ }
+
+ pub(crate) fn main_frame_document_task_owner(
+ &self,
+ ) -> Option<crate::frame_owner_model::FrameDocumentTaskOwner> {
+ match &self.document_incarnation {
+ DocumentRuntimeIncarnationIdentity::MainFrame(owner) => Some(*owner),
+ DocumentRuntimeIncarnationIdentity::Standalone(_) => None,
+ }
+ }
+
+ fn from_dom_host_with_incarnation(
+ dom_host: DomHost,
+ document_incarnation: DocumentRuntimeIncarnationIdentity,
page_task_tx: Option<crate::page_task_queue::PageTaskSender>,
page_task_parser_boundary_injection_tx: tokio::sync::mpsc::UnboundedSender<PageTask>,
stylesheet_task_sender: crate::page_task_queue::RendererPageStylesheetTaskSender,
@@ -82,7 +114,7 @@ impl DocumentRuntime {
custom_element_reaction_depth: 0,
structural_mutation_depth: 0,
dom_content_loaded_dispatched: false,
- document_incarnation: DocumentRuntimeIncarnationIdentity::standalone(),
+ document_incarnation,
document_input_stream_opened: false,
next_document_write_external_script_load_id: 0,
document_write_script_preload_scanner: None,
@@ -115,8 +147,9 @@ impl DocumentRuntime {
let parser_boundary_lifecycle_tx =
parser_boundary_lifecycle_source.parser_boundary_sender();
let owner = test_stylesheet_document_owner();
- let mut runtime = Self::from_dom_host(
+ let mut runtime = Self::from_dom_host_with_incarnation(
DomHost::from_dom(document),
+ DocumentRuntimeIncarnationIdentity::standalone(),
None,
parser_boundary_lifecycle_tx,
sender,
diff --git a/moli-renderer-v8/src/frame_owner_model/store.rs b/moli-renderer-v8/src/frame_owner_model/store.rs
index 6a8fd8ca7..d3e13ae0f 100644
--- a/moli-renderer-v8/src/frame_owner_model/store.rs
+++ b/moli-renderer-v8/src/frame_owner_model/store.rs
@@ -1347,6 +1347,15 @@ impl FrameOwnerStore {
.filter(|snapshot| snapshot.kind == FrameKind::Main)
}
+ pub(crate) fn current_main_document_task_owner(&self) -> Option<FrameDocumentTaskOwner> {
+ let snapshot = self.current_main_owner_snapshot()?;
+ Some(FrameDocumentTaskOwner::new(
+ snapshot.scheduler_lane_id,
+ snapshot.local_window_id,
+ snapshot.document_id,
+ ))
+ }
+
pub(crate) fn main_document_task_owner_is_current(
&self,
owner: FrameDocumentTaskOwner,
diff --git a/moli-renderer-v8/src/frame_owner_model/store_tests.rs b/moli-renderer-v8/src/frame_owner_model/store_tests.rs
index ac00afb19..c1b20642c 100644
--- a/moli-renderer-v8/src/frame_owner_model/store_tests.rs
+++ b/moli-renderer-v8/src/frame_owner_model/store_tests.rs
@@ -152,6 +152,14 @@ fn main_frame_identity_uses_reserved_owner_records() {
);
assert_eq!(realm_id, FrameRealmId(0));
+ assert_eq!(
+ store.current_main_document_task_owner(),
+ Some(FrameDocumentTaskOwner::new(
+ FrameSchedulerLaneId(0),
+ LocalWindowId(0),
+ DocumentId(0),
+ ))
+ );
let frame_id = FrameId("main".to_owned());
let frame = store.frames.get(&frame_id).expect("main frame record");
assert_eq!(frame.kind, FrameKind::Main);
diff --git a/moli-renderer-v8/src/native_bridge/context_host/core.rs b/moli-renderer-v8/src/native_bridge/context_host/core.rs
index 73d4520cc..bc06ed0b6 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/core.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/core.rs
@@ -133,6 +133,7 @@ impl JsContextHost {
impl JsContextHost {
pub(crate) fn new(
runtime: &mut DocumentRuntime,
+ mut frame_owner_store: FrameOwnerStore,
bindings: NativeBridgeBindings,
backend_node_registry: SharedRendererBackendNodeRegistry,
dom_debugger_pause_scheduler: crate::script_vm::RendererDomDebuggerPauseScheduler,
@@ -151,35 +152,15 @@ impl JsContextHost {
browser_context_runtime.next_shared_worker_client_owner_id();
let javascript_dialog_handler_enabled =
browser_context_runtime.javascript_dialog_handler_enabled();
- let document_handle = runtime.document_handle();
let document_url = runtime.document_url().clone();
- let document_base_url = runtime
- .dom_host()
- .document_base_url_for_handle(document_handle)
- .unwrap_or_else(|| document_url.clone());
- let mut frame_owner_store = FrameOwnerStore::default();
- frame_owner_store.ensure_main_frame(
- document_handle,
- document_url.clone(),
- document_base_url,
- moli_url::origin_ascii_serialization(&document_url),
- runtime.document_policy_container().clone(),
- crate::types::SubresourcePolicyContext {
- cross_origin_embedder_policy: runtime.cross_origin_embedder_policy(),
- document_isolation_policy: runtime.document_isolation_policy(),
- cross_origin_isolated: runtime.cross_origin_isolated(),
- },
- None,
- );
- let main_owner_snapshot = frame_owner_store
- .current_main_owner_snapshot()
+ let main_document_owner = frame_owner_store
+ .current_main_document_task_owner()
.expect("main frame owner must exist before client projection");
- let main_document_owner = FrameDocumentTaskOwner::new(
- main_owner_snapshot.scheduler_lane_id,
- main_owner_snapshot.local_window_id,
- main_owner_snapshot.document_id,
+ assert_eq!(
+ runtime.main_frame_document_task_owner(),
+ Some(main_document_owner),
+ "DocumentRuntime and FrameOwnerStore must admit the same main Document"
);
- runtime.bind_main_document_task_owner(main_document_owner);
let service_worker_storage_key = top_level_storage_key
.as_ref()
.map(moli_storage_key::MoliStorageKey::serialized_storage_key)
@@ -534,7 +515,7 @@ impl JsContextHost {
.expect("main document owner must exist before document.open() replacement");
{
let runtime: &mut DocumentRuntime = self;
- runtime.bind_main_document_task_owner(transition.current_owner());
+ runtime.commit_main_document_open(transition.current_owner());
runtime.start_root_document_parser_stream();
}
self.dom_agent_state
diff --git a/moli-renderer-v8/src/native_bridge/context_host/module_owner_tasks.rs b/moli-renderer-v8/src/native_bridge/context_host/module_owner_tasks.rs
index 104cde514..e0207fa8e 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/module_owner_tasks.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/module_owner_tasks.rs
@@ -9,12 +9,7 @@ use crate::page_task_queue::RendererPageChildModuleDependencyFetchStartEnqueue;
impl JsContextHost {
pub(crate) fn current_main_document_task_owner(&self) -> Option<FrameDocumentTaskOwner> {
- let snapshot = self.frame_owner_store.current_main_owner_snapshot()?;
- Some(FrameDocumentTaskOwner::new(
- snapshot.scheduler_lane_id,
- snapshot.local_window_id,
- snapshot.document_id,
- ))
+ self.frame_owner_store.current_main_document_task_owner()
}
pub(crate) fn current_child_document_task_owner(
diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rs
index 2a2ff0a54..ba3f8c337 100644
--- a/moli-renderer-v8/src/script_vm.rs
+++ b/moli-renderer-v8/src/script_vm.rs
@@ -14,8 +14,8 @@ use crate::{
native::{Attribute, DomHost, DomMutationEffects, NativeDom, NativeNodeId, NodeData},
},
frame_owner_model::{
- ChildDocumentModulatorStore, DocumentId, FrameDocumentTaskOwner, FrameRealmId,
- FrameScriptJob, FrameScriptJobKind,
+ ChildDocumentModulatorStore, DocumentId, FrameDocumentTaskOwner, FrameOwnerStore,
+ FrameRealmId, FrameScriptJob, FrameScriptJobKind,
},
inspector_microtasks::with_scoped_inspector_microtasks,
network::{ResourceRequestClient, context::DocumentResourceLoader},
@@ -1835,6 +1835,28 @@ impl ScriptVmPageRealmBootstrap {
crate::service_worker_runtime::ServiceWorkerClientId,
>,
) -> std::result::Result<Self, ScriptVmBootstrapError> {
+ let document_handle = dom_host.document_handle();
+ let document_url = dom_host
+ .dom()
+ .final_url()
+ .expect("parsed native dom must retain a document url")
+ .clone();
+ let document_base_url = dom_host
+ .document_base_url_for_handle(document_handle)
+ .unwrap_or_else(|| document_url.clone());
+ let mut frame_owner_store = FrameOwnerStore::default();
+ frame_owner_store.ensure_main_frame(
+ document_handle,
+ document_url.clone(),
+ document_base_url,
+ moli_url::origin_ascii_serialization(&document_url),
+ crate::document_runtime::DocumentPolicyContainer::default(),
+ crate::types::SubresourcePolicyContext::default(),
+ None,
+ );
+ let main_document_owner = frame_owner_store
+ .current_main_document_task_owner()
+ .expect("main frame admission must produce a Document owner");
let post_domcontentloaded_page_task_tx = page_task_tx.page_task_sender();
let page_runtime_wake_tx = page_task_tx.page_runtime_wake_sender();
let top_level_navigation_handoff_tx = page_task_tx.top_level_navigation_handoff_sender();
@@ -1842,8 +1864,9 @@ impl ScriptVmPageRealmBootstrap {
let stylesheet_task_sender = page_task_tx.stylesheet_task_sender();
let main_parser_continuation_sender = page_task_tx.main_parser_continuation_sender();
let resource_owner_id = crate::resource_owner::ResourceOwnerId::new();
- let mut document_runtime = Box::new(DocumentRuntime::from_dom_host(
+ let mut document_runtime = Box::new(DocumentRuntime::from_main_frame_dom_host(
dom_host,
+ main_document_owner,
Some(post_domcontentloaded_page_task_tx.clone()),
page_task_parser_boundary_injection_tx,
stylesheet_task_sender,
@@ -1884,6 +1907,7 @@ impl ScriptVmPageRealmBootstrap {
let context_host = Rc::new(RefCell::new(JsContextHost::new(
document_runtime.as_mut(),
+ frame_owner_store,
bridge_bindings,
backend_node_registry,
dom_debugger_pause_scheduler,