Lightpanda: the headless browser designed for AI and automation
webapi: iframe removal cleanup
Karl Seguinb992cbe89fa9
diff --git a/src/Notification.zig b/src/Notification.zigindex 2734b9a0e..e4be273e4 100644--- a/src/Notification.zig+++ b/src/Notification.zig@@ -81,6 +81,7 @@ const EventListeners = struct {frame_network_idle: List = .{},frame_network_almost_idle: List = .{},frame_child_frame_created: List = .{},+ frame_child_frame_detached: List = .{},frame_dom_content_loaded: List = .{},frame_loaded: List = .{},http_request_fail: List = .{},@@ -113,6 +114,7 @@ const Events = union(enum) {frame_network_idle: *const FrameNetworkIdle,frame_network_almost_idle: *const FrameNetworkAlmostIdle,frame_child_frame_created: *const FrameChildFrameCreated,+ frame_child_frame_detached: *const FrameChildFrameDetached,frame_dom_content_loaded: *const FrameDOMContentLoaded,frame_loaded: *const FrameLoaded,http_request_fail: *const RequestFail,@@ -211,6 +213,11 @@ pub const FrameChildFrameCreated = struct {timestamp: u64,};+pub const FrameChildFrameDetached = struct {+ frame_id: u32,+ parent_id: u32,+};+pub const FrameDOMContentLoaded = struct {req_id: u32,frame_id: u32,diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 5f591170a..2a8073cda 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -1196,6 +1196,70 @@ pub fn abortTransfers(self: *Frame) void {http_client.abortOwner(&self._http_owner);}+// Frame was removed from the document, or a popup is closed. Since the window+// and frame can still be referenced (in Zig and in JS), it isn't a complete+// teardown, but we do need it to stop running.+pub fn discard(self: *Frame) !void {+ const page = self.page;+ try page.closed_frames.ensureUnusedCapacity(page.arena, 1);++ if (self.parent) |parent| {+ // this is an iframe+ if (self._parent_notified == false and self._delays_parent_load) {+ try parent.js.scheduler.add(parent, struct {+ fn run(ctx: *anyopaque) !?u32 {+ // the parent no longer waits for us, don't run this+ // synchronously, we're almost certainly in some JS call+ const p: *Frame = @ptrCast(@alignCast(ctx));+ p.pendingLoadCompleted();+ return null;+ }+ }.run, 0, .{ .name = "frame.discardedChildLoaded" });+ }+ if (std.mem.findScalar(*Frame, parent.child_frames.items, self)) |idx| {+ // ordered, to keep child_frames_sorted valid+ _ = parent.child_frames.orderedRemove(idx);+ }+ self._session.notification.dispatch(.frame_child_frame_detached, &.{+ .parent_id = parent._frame_id,+ .frame_id = self._frame_id,+ });+ } else if (std.mem.findScalar(*Frame, page.popups.items, self)) |idx| {+ // this was a popup+ _ = page.popups.swapRemove(idx);+ }+ self._parent_notified = true;++ const window = self.window;+ for (page.popups.items) |popup| {+ if (popup.window._opener == window) {+ popup.window._opener = null;+ }+ }+ if (page.frame.window._opener == window) {+ page.frame.window._opener = null;+ }++ self.neuter();+ self.abortTransfers();+ page.closed_frames.appendAssumeCapacity(self);+}++fn neuter(self: *Frame) void {+ for (self.child_frames.items) |child| {+ // we're not loading anymore, so nothing to notify+ child._parent_notified = true;+ child.neuter();+ }+ self.window._closed = true;+ self.abortDocumentLoad();+ self.cancelQueuedNavigation();+ user_input.forgetFrame(self);+ self._session.idb.detachContext(self.js);+ self.js.scheduler.reset();+ self.window._timers.reset();+}+pub fn stopLoading(self: *Frame) void {var i: usize = 0;while (i < self.child_frames.items.len) : (i += 1) {@@ -2082,7 +2146,7 @@ fn errorPageNavigated(self: *Frame) !void {}pub fn isGoingAway(self: *const Frame) bool {- return self.js.env.terminatePending() or self.hasQueuedNavigation();+ return self.window._closed or self.js.env.terminatePending() or self.hasQueuedNavigation();}fn hasQueuedNavigation(self: *const Frame) bool {@@ -2224,6 +2288,11 @@ pub fn iframeAddedCallback(self: *Frame, iframe: *IFrame) !void {.referrer_policy = self.referrer_policy,.initiator_origin = self.origin,}) catch |err| {+ if (new_frame.window._closed) {+ // A script (e.g. in a srcdoc) removed the iframe while navigating:+ // the frame was discarded, page.closed_frames owns it now.+ return;+ }// extra defensive..maybe navigate added a new frame, and the index it// was added at was removed. Or maybe this frame was removed somehow// (which I don't think is possible)@@ -2238,6 +2307,11 @@ pub fn iframeAddedCallback(self: *Frame, iframe: *IFrame) !void {return error.IFrameLoadError;};+ if (new_frame.window._closed) {+ // discarded while navigating, it's no longer in child_frames+ return;+ }+// window[N] is based on document order. We appended above and rely on// child_frames_sorted to tell window.getFrame whether it has to sort.// Since we expect frames to often be added in document order, do a quick@@ -4190,7 +4264,7 @@ test "Frame: superseded documents omit DOMContentLoaded and load" {}test "Frame: pending or discarded replacements do not resume old load events" {- for ([_]bool{ false, true }) |discard| {+ for ([_]bool{ false, true }) |discard_replacement| {const page = try testing.pageTest("hi.html", .{});defer page.close();const frame = page.frame().?;@@ -4207,12 +4281,12 @@ test "Frame: pending or discarded replacements do not resume old load events" {frame.document._ready_state = .loading;try frame._session.initiateRootNavigation(frame._frame_id, "http://127.0.0.1:9582/src/browser/tests/hi.html?replacement", .{});const replacement = frame.page.replacement.?;- if (discard) frame._session.discardPendingPage(replacement);+ if (discard_replacement) frame._session.discardPendingPage(replacement);try testing.expectEqual(null, frame._queued_navigation);frame.documentIsComplete();const events = try ls.local.exec("events.join('|')", null);try testing.expectEqual("complete", try events.toStringSlice());- if (!discard) frame._session.discardPendingPage(replacement);+ if (!discard_replacement) frame._session.discardPendingPage(replacement);}}diff --git a/src/browser/js/Scheduler.zig b/src/browser/js/Scheduler.zigindex 9e918f043..f8c649387 100644--- a/src/browser/js/Scheduler.zig+++ b/src/browser/js/Scheduler.zig@@ -51,12 +51,15 @@ generation: u64,// considered done.blocking: u32,+resets: u32,+pub fn init(allocator: Allocator) Scheduler {return .{._sequence = 0,.allocator = allocator,.generation = 0,.blocking = 0,+ .resets = 0,.timed = Queue.initContext({}),.immediate = .{},.front = .{},@@ -68,6 +71,7 @@ pub fn deinit(self: *Scheduler) void {}pub fn reset(self: *Scheduler) void {+ self.resets +%= 1;self.finalizeTasks();self.blocking = 0;self.front.clear();@@ -126,21 +130,32 @@ pub fn run(self: *Scheduler) !void {self.generation +%= 1;+ const resets = self.resets;const repeat_in_ms = task.callback(task.ctx) catch |err| {log.debug(.scheduler, "task.callback", .{ .name = task.name, .err = err });continue;};if (repeat_in_ms) |ms| {- // Task cannot be repeated immediately, and they should know that- if (comptime lp.IS_DEBUG) {- std.debug.assert(ms != 0);+ if (resets != self.resets) {+ // The task reset its own scheduler. Since it was running,+ // finalizeTasks skipped over it. (would also be true for a non-+ // repeating task, but in that case we aren't responsible+ // for finalizing it.)+ if (task.finalizer) |func| {+ func(task.ctx);+ }+ } else {+ // Task cannot be repeated immediately, and they should know that+ if (comptime lp.IS_DEBUG) {+ std.debug.assert(ms != 0);+ }+ // A task that endlessly reschedules itself would keep the page+ // alive forever, so repeats never block completion.+ task.blocks_done = false;+ task.key = self.nextKey(now + ms);+ try self.timed.push(self.allocator, task);}- // A task that endlessly reschedules itself would keep the page- // alive forever, so repeats never block completion.- task.blocks_done = false;- task.key = self.nextKey(now + ms);- try self.timed.push(self.allocator, task);}now = lp.datetime.milliTimestamp(.boot);diff --git a/src/browser/tests/frames/remove.html b/src/browser/tests/frames/remove.htmlnew file mode 100644index 000000000..b49cfcaa2--- /dev/null+++ b/src/browser/tests/frames/remove.html@@ -0,0 +1,142 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!-- removed while its src is still loading: the parent's load event must not+ keep waiting on it -->+<iframe id=hanging src="/xhr/hang"></iframe>+<script id=removed_while_loading>+ const hanging = document.getElementById('hanging');+ hanging.remove();+ testing.expectEqual(null, hanging.contentWindow);+ testing.expectEqual(0, window.length);++ // only complete once nothing delays the load event anymore+ testing.onload(() => testing.expectEqual('complete', document.readyState));+</script>++<script id=discards_context type=module>+ {+ // other tests' iframes come and go concurrently, so look for ours+ const frames = () => Array.from({ length: window.length }, (_, i) => window[i]);++ const state = await testing.async();++ window.ticks = 0;+ const f = document.createElement('iframe');+ f.srcdoc = '<script>setInterval(() => parent.ticks += 1, 5)<\/script>';+ f.onload = () => {+ const win = f.contentWindow;+ const doc = f.contentDocument;+ const listed = frames().includes(win);+ f.remove();+ const ticks = window.ticks;+ setTimeout(() => state.resolve({ win, doc, listed, ticks }), 50);+ };+ document.body.appendChild(f);++ await state.done(({ win, doc, listed, ticks }) => {+ testing.expectEqual(true, listed);+ testing.expectEqual(null, f.contentWindow);+ testing.expectEqual(null, f.contentDocument);+ testing.expectEqual(null, doc.defaultView);+ testing.expectEqual(true, win.closed);+ testing.expectEqual(false, frames().includes(win));+ testing.expectEqual(ticks, window.ticks);+ // the document itself is still usable+ testing.expectEqual('BODY', doc.body.tagName);+ });+ }+</script>++<script id=reinsert_loads_new_document type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.srcdoc = '<p>doc</p>';+ const docs = [];+ f.onload = () => {+ docs.push(f.contentDocument);+ if (docs.length === 1) {+ f.remove();+ document.body.appendChild(f);+ } else {+ state.resolve();+ }+ };+ document.body.appendChild(f);++ await state.done(() => {+ testing.expectEqual(2, docs.length);+ testing.expectEqual(false, docs[0] === docs[1]);+ testing.expectEqual(null, docs[0].defaultView);+ testing.expectEqual(f.contentWindow, docs[1].defaultView);+ testing.expectEqual('doc', docs[1].querySelector('p').textContent);+ });+ }+</script>++<script id=removing_ancestor_discards_descendants type=module>+ {+ const state = await testing.async();++ const wrapper = document.createElement('div');+ const f = document.createElement('iframe');+ f.srcdoc = '<iframe srcdoc="<p>inner</p>"></iframe>';+ f.onload = () => {+ const outer = f.contentWindow;+ const inner = outer[0];+ const inner_doc = inner.document;+ wrapper.remove();+ state.resolve({ outer, inner, inner_doc });+ };+ wrapper.appendChild(f);+ document.body.appendChild(wrapper);++ await state.done(({ outer, inner, inner_doc }) => {+ testing.expectEqual(null, f.contentWindow);+ testing.expectEqual(true, outer.closed);+ testing.expectEqual(true, inner.closed);+ testing.expectEqual(null, inner_doc.defaultView);+ });+ }+</script>++<script id=interval_removes_own_iframe type=module>+ {+ const state = await testing.async();++ window.self_removals = 0;+ const f = document.createElement('iframe');+ f.srcdoc = '<script>setInterval(() => { parent.self_removals += 1; frameElement.remove(); }, 5)<\/script>';+ document.body.appendChild(f);+ setTimeout(() => state.resolve(), 100);++ await state.done(() => {+ testing.expectEqual(1, window.self_removals);+ testing.expectEqual(null, f.contentWindow);+ });+ }+</script>++<script id=clear_timer_on_discarded_window type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.srcdoc = '<script>window.timer_id = setInterval(() => {}, 5)<\/script>';+ f.onload = () => {+ const win = f.contentWindow;+ f.remove();+ // the timer was dropped with the scheduler, clearing it must be a no-op+ win.clearInterval(win.timer_id);+ win.clearTimeout(win.timer_id);+ state.resolve(win);+ };+ document.body.appendChild(f);++ await state.done((win) => {+ testing.expectEqual(true, win.closed);+ });+ }+</script>diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 0691a763c..9af766a59 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -1700,6 +1700,9 @@ pub const JsApi = struct {pub const defaultView = bridge.accessor(struct {fn defaultView(self: *const Document) ?*@import("Window.zig") {const frame = self._frame orelse return null;+ if (frame.window._closed) {+ return null;+ }return frame.window;}}.defaultView, null, .{});diff --git a/src/browser/webapi/Timers.zig b/src/browser/webapi/Timers.zigindex e9dfb0b4c..409c74556 100644--- a/src/browser/webapi/Timers.zig+++ b/src/browser/webapi/Timers.zig@@ -155,6 +155,12 @@ pub fn schedule(return timer_id;}+// The scheduler was reset, which finalized every pending callback.+pub fn reset(self: *Timers) void {+ self._callbacks.clearRetainingCapacity();+ self._repeating = 0;+}+pub fn clear(self: *Timers, id: u32) void {var sc = self._callbacks.fetchRemove(id) orelse return;if (sc.value.repeat_ms != null) {diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zigindex 8fd02f2b1..1f6df70d9 100644--- a/src/browser/webapi/Window.zig+++ b/src/browser/webapi/Window.zig@@ -642,52 +642,16 @@ pub fn close(self: *Window) void {// Per spec, close() is only honored on script-opened windows. That// maps exactly to membership in page.popups.const frame = self._frame;- const page = frame.page;-- var popup_index: usize = 0;- while (popup_index < page.popups.items.len) : (popup_index += 1) {- if (page.popups.items[popup_index] == frame) {- break;- }- } else return;-- self._closed = true;-- // Any live Window holding us as its opener must drop the reference —- // our Frame is about to go away, and a stale _frame deref on their- // side would crash.- for (page.popups.items) |popup| {- if (popup.window._opener == self) {- popup.window._opener = null;- }- }- if (page.frame.window._opener == self) {- page.frame.window._opener = null;- }-- _ = page.popups.swapRemove(popup_index);-- // Drop any pending queued navigation for this frame.- if (frame._queued_navigation != null) {- for (page.queued_navigation.items, 0..) |f, i| {- if (f == frame) {- _ = page.queued_navigation.swapRemove(i);- break;- }- }+ if (std.mem.findScalar(*Frame, frame.page.popups.items, frame) == null) {+ return;}- page.session.idb.detachContext(frame.js);- frame.js.scheduler.reset();- frame.abortTransfers();-// Do not teardown the frame. The Window can still be referenced in JS// (window.open(...) returns the window). It would be nice to be able to// free this now (just like it would be nice to be more pro-active about- // freeing workers and iframes), but, for now, we don't have the- // infrastructure to do this safely so doing it on page tear down is our- // only option.- page.closed_frames.append(page.arena, frame) catch @panic("OOM");+ // freeing workers), but, for now, we don't have the infrastructure to do+ // this safely so doing it on page tear down is our only option.+ frame.discard() catch @panic("OOM");}pub fn focus(_: *Window) void {}diff --git a/src/browser/webapi/element/html/IFrame.zig b/src/browser/webapi/element/html/IFrame.zigindex b1cc9d325..ddb46877f 100644--- a/src/browser/webapi/element/html/IFrame.zig+++ b/src/browser/webapi/element/html/IFrame.zig@@ -154,6 +154,15 @@ pub const Build = struct {if (reprocesses(element, name)) try reload(element, frame);}+ /// Removing the iframe destroys its browsing context+ pub fn disconnected(element: *Element, _: *Frame) !void {+ const self = element.as(IFrame);+ self._executed = false;+ const window = self._window orelse return;+ self._window = null;+ try window._frame.discard();+ }+/// Removing srcdoc falls back to src, removing src to about:blank.pub fn attributeRemove(element: *Element, name: String, frame: *Frame) !void {if (reprocesses(element, name)) try reload(element, frame);diff --git a/src/server/cdp/CDP.zig b/src/server/cdp/CDP.zigindex f33d1b21f..13faa5fb5 100644--- a/src/server/cdp/CDP.zig+++ b/src/server/cdp/CDP.zig@@ -609,6 +609,7 @@ pub const BrowserContext = struct {try notification.register(.frame_navigated_within_document, self, onFrameNavigatedWithinDocument);try notification.register(.frame_navigate_failed, self, onFrameNavigateFailed);try notification.register(.frame_child_frame_created, self, onFrameChildFrameCreated);+ try notification.register(.frame_child_frame_detached, self, onFrameChildFrameDetached);try notification.register(.frame_destroyed, self, onFrameDestroyed);try notification.register(.frame_dom_content_loaded, self, onFrameDOMContentLoaded);try notification.register(.frame_loaded, self, onFrameLoaded);@@ -1036,6 +1037,11 @@ pub const BrowserContext = struct {return @import("domains/page.zig").frameChildFrameCreated(self, msg);}+ fn onFrameChildFrameDetached(ctx: *anyopaque, msg: *const Notification.FrameChildFrameDetached) !void {+ const self: *BrowserContext = @ptrCast(@alignCast(ctx));+ return @import("domains/page.zig").frameChildFrameDetached(self, msg);+ }+fn onFrameNetworkIdle(ctx: *anyopaque, msg: *const Notification.FrameNetworkIdle) !void {const self: *BrowserContext = @ptrCast(@alignCast(ctx));return @import("domains/page.zig").frameNetworkIdle(self, msg);diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zigindex ec7a62816..93d27972f 100644--- a/src/server/cdp/domains/page.zig+++ b/src/server/cdp/domains/page.zig@@ -675,6 +675,14 @@ pub fn frameChildFrameCreated(bc: *CDP.BrowserContext, event: *const Notificatio}}+pub fn frameChildFrameDetached(bc: *CDP.BrowserContext, event: *const Notification.FrameChildFrameDetached) !void {+ const session_id = bc.session_id orelse return;+ try bc.cdp.sendEvent("Page.frameDetached", .{+ .frameId = &id.toFrameId(event.frame_id),+ .reason = "remove",+ }, .{ .session_id = session_id });+}+pub fn frameNavigated(arena: Allocator, bc: *CDP.BrowserContext, event: *const Notification.FrameNavigated) !void {// detachTarget could be called, in which case, we still have a frame doing// things, but no session.@@ -1820,6 +1828,28 @@ test "cdp.frame: frameAttached" {}, .{ .session_id = "SID-X" });}+test "cdp.frame: frameDetached" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .session_id = "SID-X", .url = "hi.html" });+ const frame = bc.mainFrame().?;++ var ls: js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();+ try ls.local.eval(+ \\const f = document.createElement('iframe');+ \\document.body.appendChild(f);+ \\f.remove();+ , null);++ try ctx.expectSentEvent("Page.frameDetached", .{+ .frameId = &id.toFrameId(frame._frame_id + 1),+ .reason = "remove",+ }, .{ .session_id = "SID-X" });+}+test "cdp.frame: child navigation preserves node registry" {var ctx = try testing.context();defer ctx.deinit();