Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: give each Window its own History object

Navid EMADf086f18746a3
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index 071b0ee0b..2eac5061c 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -22,7 +22,6 @@ const lp = @import("lightpanda");
const App = @import("../App.zig");
const Config = @import("../Config.zig");
-const History = @import("webapi/History.zig");
const storage = @import("webapi/storage/storage.zig");
const IdbManager = @import("webapi/storage/idb/idb.zig").Manager;
const CacheStore = @import("webapi/cache/Store.zig");
@@ -51,7 +50,6 @@ const Session = @This();
browser: *Browser,
arena: *lp.Arena,
-history: History,
navigation: *Navigation,
storage_shed: storage.Shed,
idb: IdbManager, // Per-origin IndexedDB engines
@@ -167,7 +165,6 @@ pub fn init(self: *Session, browser: *Browser, notification: *Notification) !voi
self.* = .{
.arena = arena,
.arena_pool = arena_pool,
- .history = .{},
.navigation = navigation,
.storage_shed = .{},
.idb = IdbManager.init(allocator),
diff --git a/src/browser/tests/frames/history_per_window.html b/src/browser/tests/frames/history_per_window.html
new file mode 100644
index 000000000..10c63706e
--- /dev/null
+++ b/src/browser/tests/frames/history_per_window.html
@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<!-- Each Window has its own History object. A History shared by every
+ window ends up bound to the realm that touched it first, so another
+ frame's history.pushState() updated that first frame instead.
+ https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-history-interface -->
+
+<script>
+ function loadFrame(name) {
+ return new Promise((resolve) => {
+ const frame = document.createElement('iframe');
+ frame.name = name;
+ frame.onload = () => {
+ // Skip the load of the initial about:blank document.
+ if (frame.contentWindow.location.pathname !== '/echo_method') return;
+ frame.onload = null;
+ resolve(frame);
+ };
+ frame.src = '/echo_method';
+ document.body.appendChild(frame);
+ });
+ }
+
+ // Runs `code` from the frame's own realm, as one of its scripts would.
+ function runInFrame(frame, code) {
+ const script = frame.contentDocument.createElement('script');
+ script.textContent = code;
+ frame.contentDocument.body.appendChild(script);
+ }
+</script>
+
+<script id=history_per_window type=module>
+ {
+ const state = await testing.async();
+ const a = await loadFrame('a');
+ const b = await loadFrame('b');
+
+ const identity = [
+ a.contentWindow.history === b.contentWindow.history,
+ a.contentWindow.history === window.history,
+ ];
+
+ // Touch a's history first: with a shared History, its wrapper (and so
+ // its methods) would belong to a's realm.
+ a.contentWindow.history.pushState(null, '', '#a1');
+ b.contentWindow.history.pushState(null, '', '#b1');
+ const from_parent = [a.contentWindow.location.hash, b.contentWindow.location.hash];
+
+ runInFrame(b, "history.replaceState(null, '', '#b2')");
+ const from_frame = [a.contentWindow.location.hash, b.contentWindow.location.hash];
+
+ a.contentWindow.history.scrollRestoration = 'manual';
+ const scroll_restoration = [
+ a.contentWindow.history.scrollRestoration,
+ b.contentWindow.history.scrollRestoration,
+ ];
+
+ // This document's own history, used after the frames touched theirs:
+ // with a shared History it would push onto frame a instead.
+ history.pushState(null, '', '#top');
+ const from_top = [location.hash, a.contentWindow.location.hash];
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual([false, false], identity);
+ testing.expectEqual(['#a1', '#b1'], from_parent);
+ testing.expectEqual(['#a1', '#b2'], from_frame);
+ testing.expectEqual(['manual', 'auto'], scroll_restoration);
+ testing.expectEqual(['#top', '#a1'], from_top);
+ });
+ }
+</script>
diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig
index f464c3c62..4525be14e 100644
--- a/src/browser/webapi/Window.zig
+++ b/src/browser/webapi/Window.zig
@@ -94,6 +94,10 @@ _on_unhandled_rejection: ?js.Function.Global = null,
_reporting_error: bool = false,
_current_event: ?*Event = null,
_location: *Location,
+// Per Window, not per Session: each realm needs its own History wrapper, so
+// that its methods run against this window's frame. The session history
+// entries they act on are shared (session.navigation).
+_history: History = .{},
_timers: Timers = .{},
_scheduler: Scheduler = .{},
_custom_elements: CustomElementRegistry = .{},
@@ -360,8 +364,8 @@ fn setLocation(self: *Window, url: [:0]const u8, frame: *Frame) !void {
return frame.scheduleNavigation(url, .{ .reason = .script, .kind = .{ .push = null } }, .{ .script = self._frame });
}
-pub fn getHistory(_: *Window, frame: *Frame) *History {
- return &frame._session.history;
+pub fn getHistory(self: *Window) *History {
+ return &self._history;
}
fn getNavigation(_: *Window, frame: *Frame) *Navigation {
diff --git a/src/server/bidi/browsing_context.zig b/src/server/bidi/browsing_context.zig
index 44c0eefa5..2a0833d56 100644
--- a/src/server/bidi/browsing_context.zig
+++ b/src/server/bidi/browsing_context.zig
@@ -325,7 +325,7 @@ pub fn traverse(cmd: *BiDi.Command, ctx: *Context, frame: *Frame, delta: i32) !v
defer ls.deinit();
frame.js.local = &ls.local;
- try frame._session.history.go(delta, frame);
+ try frame.window._history.go(delta, frame);
}
return answerAfterNavigation(cmd, ctx, frame);
}