Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

A closed iframe's frame root goes with its JS, its document and its entity: a page that opened and closed artifacts kept each one's engine running, and a stale root swapped its document into the next iframe given the same id

shinyobjectz420e75358330
diff --git a/runtime/dom/src/host/frames.rs b/runtime/dom/src/host/frames.rs
index 658c77c5..5cbd0c80 100644
--- a/runtime/dom/src/host/frames.rs
+++ b/runtime/dom/src/host/frames.rs
@@ -14,6 +14,16 @@ pub struct Frame {
pub(crate) wid: u64,
/// sandboxed without allow-same-origin: an opaque origin, and no host calls or app asks at all
pub(crate) opaque: bool,
+ /// the document it was mounted for (`url_key`): its iframe's id can come back as another iframe's
+ pub(crate) url: u64,
+}
+
+/// A frame document's URL as a frame root keeps it.
+fn url_key(url: &str) -> u64 {
+ use std::hash::{Hash, Hasher};
+ let mut h = std::collections::hash_map::DefaultHasher::new();
+ url.hash(&mut h);
+ h.finish()
}
/// The JS of every root while iframes are on (raea_sys_page::iframes()): its iframes' windows (contentWindow) and the messages they post it.
@@ -158,6 +168,23 @@ pub(crate) fn mount_frames(world: &mut World, roots: &mut Roots) {
}
}
}
+ // a frame root whose iframe is gone, or whose iframe's id now holds another document, goes with its JS, its
+ // document and its entity: a page that opened and closed artifacts kept every one's engine running, and a stale
+ // root swapped its document into the next iframe given the same id
+ let alive: std::collections::HashSet<(Entity, NodeId, u64)> = roots.0.iter().filter(|r| r.frame.is_none()).flat_map(|r| r.frames_seen.iter().map(move |(n, u)| (r.page, *n, url_key(u)))).collect();
+ let stale: Vec<Entity> = roots.0.iter().filter(|r| r.frame.is_some_and(|f| !alive.contains(&(f.parent, f.node, f.url)))).map(|r| r.page).collect();
+ if !stale.is_empty() {
+ roots.0.retain(|r| !stale.contains(&r.page));
+ let mut docs = world.non_send_mut::<Docs>();
+ for page in &stale {
+ docs.0.remove(page);
+ }
+ for page in stale {
+ if let Ok(e) = world.get_entity_mut(page) {
+ e.despawn();
+ }
+ }
+ }
for (parent, node, wid, opaque, src, body, url, (w, h)) in found {
let path = url::Url::parse(&url).and_then(|u| u.join(&src)).ok().and_then(|u| u.to_file_path().ok());
let net = raea_sys_page::net::FrameNet { roots: raea_sys_page::net::page_roots() };
@@ -171,7 +198,7 @@ pub(crate) fn mount_frames(world: &mut World, roots: &mut Roots) {
let page = world.spawn_empty().id();
let held = BaseDocument::new(raea_sys_page::config(raea_sys_page::Page { size: UVec2::new(w, h), scale: 1.0 }));
world.non_send_mut::<Docs>().0.insert(page, held);
- let frame = Frame { parent, node, wid, opaque };
+ let frame = Frame { parent, node, wid, opaque, url: url_key(&url) };
if !swap_frame(world, page, &frame) {
continue;
}
diff --git a/runtime/dom/tests/frames.rs b/runtime/dom/tests/frames.rs
index 8921e1ab..a9f99a40 100644
--- a/runtime/dom/tests/frames.rs
+++ b/runtime/dom/tests/frames.rs
@@ -56,8 +56,9 @@ fn a_sandboxed_frame_posts_to_its_parent_and_reaches_nothing_else() {
let parent = format!(
r#"{WINDOW}
globalThis.dispatchEvent = (e) => __ui_emit("got", JSON.stringify({{ data: e.data, origin: e.origin, fromTheFrame: e.source === __raea_frames.window({{ __wid: 1 }}) }}));
- __ui_flush(JSON.stringify({{ v: 1, ops: [{{ op: "create", id: 1, kind: "iframe", props: {{ src: "/frame.html", sandbox: "allow-scripts", style: {{ width: "200px", height: "100px" }} }} }}, {{ op: "append", parent: 0, child: 1 }}, {{ op: "listen", id: 1, events: ["pointerdown"] }}] }}));
- globalThis.__ui_deliver = (json) => {{ const m = JSON.parse(json); if (m.t === "event") __ui_emit("parentHeard", JSON.stringify(m.event)); }};
+ __ui_flush(JSON.stringify({{ v: 1, ops: [{{ op: "create", id: 1, kind: "iframe", props: {{ src: "/frame.html", sandbox: "allow-scripts", style: {{ width: "200px", height: "100px" }} }} }}, {{ op: "append", parent: 0, child: 1 }}, {{ op: "listen", id: 1, events: ["pointerdown"] }}, {{ op: "create", id: 2, kind: "div", props: {{ style: {{ width: "400px", height: "100px" }} }} }}, {{ op: "append", parent: 0, child: 2 }}, {{ op: "listen", id: 2, events: ["pointerdown"] }}] }}));
+ // a press below the frame closes it (an artifact's review closed); any other event the parent hears is reported
+ globalThis.__ui_deliver = (json) => {{ const m = JSON.parse(json); if (m.t !== "event") return; if (m.payload && m.payload.clientY > 150) __ui_flush(JSON.stringify({{ v: 1, ops: [{{ op: "remove", id: 1 }}] }})); else __ui_emit("parentHeard", JSON.stringify(m.event)); }};
"#
);
let page = host::mount(app.world_mut(), 400, 300, &parent, &[], None).unwrap();
@@ -99,5 +100,29 @@ fn a_sandboxed_frame_posts_to_its_parent_and_reaches_nothing_else() {
let all = &app.world().resource::<PageAsks>().0;
assert!(all.iter().all(|(_, n, _)| n == "got"), "a frame's ask, or the press, reached the parent's app: {all:?}");
+
+ // the iframe removed (a press below it): the frame's root goes, its document and its entity with it, so a page
+ // that opens and closes artifacts doesn't keep each one's JS running
+ let frame_pages: Vec<Entity> = app.world().non_send::<Docs>().0.keys().copied().filter(|e| *e != page).collect();
+ assert_eq!(frame_pages.len(), 1, "the frame's own document, beside the parent's");
+ let below = blitz_traits::events::PointerCoords { page_x: 50.0, page_y: 160.0, screen_x: 50.0, screen_y: 160.0, client_x: 50.0, client_y: 160.0 };
+ host::input(app.world_mut(), page, blitz_traits::events::UiEvent::PointerDown(blitz_traits::events::BlitzPointerEvent {
+ id: blitz_traits::events::BlitzPointerId::Mouse,
+ is_primary: true,
+ coords: below,
+ button: blitz_traits::events::MouseEventButton::Main,
+ buttons: blitz_traits::events::MouseEventButtons::Primary,
+ mods: Default::default(),
+ details: Default::default(),
+ element: Default::default(),
+ active_pointers: Default::default(),
+ }));
+ for _ in 0..4 {
+ app.update();
+ }
+ let docs: Vec<Entity> = app.world().non_send::<Docs>().0.keys().copied().collect();
+ assert_eq!(docs, vec![page], "the frame's document outlived its iframe");
+ assert!(app.world().get_entity(frame_pages[0]).is_err(), "the frame's entity outlived its iframe");
+
std::fs::remove_dir_all(&root).unwrap();
}