perf(renderer): borrow validated frame documents during export

This commit is contained in:
ldm0
2026-11-13 19:26:46 +08:00
committed by Donough Liu
parent 1f7e36e0b8
commit aff75c6b9d
3 changed files with 135 additions and 38 deletions
+71 -33
View File
@@ -45,6 +45,36 @@ pub(crate) struct FrameOwnerStore {
pending_parent_document_descendant_completions: VecDeque<FrameDocumentDescendantLoadCompletion>,
}
// A view into the authoritative owner indexes. Validation is shared with
// snapshots, but synchronous readers need not clone settings or URLs.
struct CurrentFrameOwner<'a> {
frame_id: &'a FrameId,
frame: &'a FrameRecord,
local_window: &'a LocalWindowRecord,
document: &'a DocumentRecord,
}
impl CurrentFrameOwner<'_> {
fn snapshot(&self) -> FrameOwnerSnapshot {
FrameOwnerSnapshot {
frame_id: self.frame_id.clone(),
kind: self.frame.kind,
parent_frame_id: self.frame.parent_frame_id.clone(),
owner_element_handle: self.frame.owner_element_handle,
#[cfg(test)]
window_proxy_id: self.frame.window_proxy_id,
scheduler_lane_id: self.frame.scheduler_lane_id,
local_window_id: self.local_window.id,
document_id: self.document.id,
document_handle: self.document.document_handle,
document_url: self.document.url.clone(),
document_base_url: self.document.base_url.clone(),
realm_id: self.local_window.realm_id,
settings: self.local_window.settings.clone(),
}
}
}
impl FrameOwnerStore {
fn new_loading_document_lifecycle(
&mut self,
@@ -1648,6 +1678,11 @@ impl FrameOwnerStore {
&self,
frame_id: &FrameId,
) -> Option<FrameOwnerSnapshot> {
self.current_frame_owner(frame_id)
.map(|owner| owner.snapshot())
}
fn current_frame_owner<'a>(&'a self, frame_id: &'a FrameId) -> Option<CurrentFrameOwner<'a>> {
let frame = self.frames.get(frame_id)?;
if frame.lifecycle != FrameLifecycleState::Attached {
return None;
@@ -1700,21 +1735,11 @@ impl FrameOwnerStore {
}
}
Some(FrameOwnerSnapshot {
frame_id: frame_id.clone(),
kind: frame.kind,
parent_frame_id: frame.parent_frame_id.clone(),
owner_element_handle: frame.owner_element_handle,
#[cfg(test)]
window_proxy_id: frame.window_proxy_id,
scheduler_lane_id: frame.scheduler_lane_id,
local_window_id,
document_id,
document_handle: document.document_handle,
document_url: document.url.clone(),
document_base_url: document.base_url.clone(),
realm_id: local_window.realm_id,
settings: local_window.settings.clone(),
Some(CurrentFrameOwner {
frame_id,
frame,
local_window,
document,
})
}
@@ -1722,24 +1747,7 @@ impl FrameOwnerStore {
&self,
child_handle: DomHandle,
) -> Option<ChildFrameOwnerSnapshot> {
let frame_id = self.frame_ids_by_child_handle.get(&child_handle)?.clone();
let owner_element = self.frame_owner_elements.get(&child_handle)?;
if owner_element.owner_handle != child_handle
|| owner_element.content_frame_id.as_ref() != Some(&frame_id)
|| owner_element.lifecycle != FrameOwnerElementLifecycleState::Attached
{
return None;
}
let owner = self.current_frame_owner_snapshot(&frame_id)?;
if owner.kind != FrameKind::ChildIframe
|| owner.owner_element_handle != Some(child_handle)
|| owner.frame_id != frame_id
|| owner_element.parent_frame_id != owner.parent_frame_id
{
return None;
}
let owner = self.current_child_owner(child_handle)?.snapshot();
Some(ChildFrameOwnerSnapshot {
owner_handle: child_handle,
frame_id: owner.frame_id,
@@ -1755,6 +1763,36 @@ impl FrameOwnerStore {
})
}
fn current_child_owner(&self, child_handle: DomHandle) -> Option<CurrentFrameOwner<'_>> {
let frame_id = self.frame_ids_by_child_handle.get(&child_handle)?;
let owner_element = self.frame_owner_elements.get(&child_handle)?;
if owner_element.owner_handle != child_handle
|| owner_element.content_frame_id.as_ref() != Some(frame_id)
|| owner_element.lifecycle != FrameOwnerElementLifecycleState::Attached
{
return None;
}
let owner = self.current_frame_owner(frame_id)?;
if owner.frame.kind != FrameKind::ChildIframe
|| owner.frame.owner_element_handle != Some(child_handle)
|| owner.frame_id != frame_id
|| owner_element.parent_frame_id != owner.frame.parent_frame_id
{
return None;
}
Some(owner)
}
pub(crate) fn current_child_document(
&self,
child_handle: DomHandle,
) -> Option<(DomHandle, &Url)> {
let owner = self.current_child_owner(child_handle)?;
Some((owner.document.document_handle, &owner.document.url))
}
pub(crate) fn current_child_document_owner(
&self,
child_handle: DomHandle,
@@ -1878,6 +1878,65 @@ fn stale_realm_clear_does_not_clear_replacement_child_realm() {
);
}
#[test]
fn borrowed_child_document_tracks_current_owner_without_retaining_a_snapshot() {
let mut store = FrameOwnerStore::default();
let child = handle(403);
let owner = commit_test_child_document(
&mut store,
child,
handle(404),
"borrowed-document",
Some("main"),
);
let (document, document_url) = store.current_child_document(child).unwrap();
let record = store.documents.get(&owner.document_id).unwrap();
assert_eq!(document, record.document_handle);
assert!(std::ptr::eq(document_url, &record.url));
assert!(store.update_current_child_document_urls(
child,
url("https://borrowed-document.test/updated"),
url("https://borrowed-document.test/base/"),
));
assert_eq!(
store.current_child_document(child).unwrap().1.as_str(),
"https://borrowed-document.test/updated",
);
store
.scheduler_lanes
.get_mut(&owner.scheduler_lane_id)
.unwrap()
.lifecycle = FrameSchedulerLaneLifecycleState::Detached;
assert!(store.current_child_document(child).is_none());
store
.scheduler_lanes
.get_mut(&owner.scheduler_lane_id)
.unwrap()
.lifecycle = FrameSchedulerLaneLifecycleState::Active;
assert!(store.current_child_document(child).is_some());
let realm = materialize_test_child_realm(&mut store, child, 403);
let valid_document = store.realms.get(&realm).unwrap().document_id;
store.realms.get_mut(&realm).unwrap().document_id = main_document_id();
assert!(store.current_child_document(child).is_none());
store.realms.get_mut(&realm).unwrap().document_id = valid_document;
assert!(store.current_child_document(child).is_some());
store.detach_current_child_document(child);
assert!(store.current_child_document(child).is_none());
commit_test_child_document(
&mut store,
child,
handle(405),
"borrowed-replacement",
Some("main"),
);
assert_eq!(
store.current_child_document(child),
Some((handle(405), &url("https://borrowed-replacement.test/"))),
);
}
#[test]
fn frame_scheduler_lane_is_required_for_current_owner_snapshot() {
let mut store = FrameOwnerStore::default();
@@ -27,18 +27,18 @@ impl HtmlEmbeddedDocumentProvider for PageScriptHost {
if !self.child_browsing_context_host_is_active(owner) {
return None;
}
let current = self.frame_owner_current_child_snapshot(owner)?;
if document != current.document_handle
let (current_document, url) = self.frame_owner_store.current_child_document(owner)?;
if document != current_document
|| !self
.dom_host()
.node(current.document_handle)
.node(current_document)
.is_some_and(crate::dom::native::Node::is_document)
{
return None;
}
Some(HtmlEmbeddedDocument {
document: current.document_handle,
url: Cow::Owned(current.document_url.into()),
document: current_document,
url: Cow::Borrowed(url.as_str()),
})
}
}