Skip to content

Lightpanda: the headless browser designed for AI and automation

session: one session history per top-level page

Pierre Tachoirec78eed99a323
diff --git a/src/Notification.zig b/src/Notification.zig
index 634fb2863..fc01055e7 100644
--- a/src/Notification.zig
+++ b/src/Notification.zig
@@ -139,7 +139,10 @@ const Events = union(enum) {
};
pub const EventType = std.meta.FieldEnum(Events);
-pub const FrameRemove = struct {};
+pub const FrameRemove = struct {
+ // Root frame id of the Page being removed.
+ frame_id: u32,
+};
pub const FrameNavigate = struct {
req_id: u32,
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index b44988899..3df51cec2 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -854,8 +854,8 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !vo
});
if (self.parent == null) {
- session.navigation._current_navigation_kind = opts.kind;
- try session.navigation.commitNavigation(self);
+ session.navigationFor(self)._current_navigation_kind = opts.kind;
+ try session.navigationFor(self).commitNavigation(self);
}
self.documentIsComplete();
@@ -952,7 +952,7 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !vo
// Record telemetry for navigation
self.recordNavigateTelemetry(std.ascii.startsWithIgnoreCase(self.url, "https://"));
- session.navigation._current_navigation_kind = opts.kind;
+ session.navigationFor(self)._current_navigation_kind = opts.kind;
transfer.submit() catch |err| {
log.debug(.frame, "navigate request", .{ .url = self.url, .err = err, .type = self._type });
@@ -1092,7 +1092,7 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
});
if (target.parent == null) {
- try session.navigation.updateEntries(target.url, opts.kind, target, true);
+ try session.navigationFor(target).updateEntries(target.url, opts.kind, target, true);
}
// `:target` matches off the fragment, which just changed.
@@ -1994,7 +1994,7 @@ fn frameDoneCallback(ctx: *anyopaque) !void {
}
//We need to handle different navigation types differently.
- try self._session.navigation.commitNavigation(self);
+ try self._session.navigationFor(self).commitNavigation(self);
if (self._pending_content_type) |content_type| {
self.document._content_type = content_type;
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index e83423ea7..38511fa4b 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -51,7 +51,10 @@ const Session = @This();
browser: *Browser,
arena: *lp.Arena,
-navigation: *Navigation,
+// Session history, one per top-level browsing context, keyed by its root
+// frame id. Outlives the Page: a root navigation's replacement Page reuses the
+// frame id, and so the history. Removed by closePage.
+navigations: std.AutoHashMapUnmanaged(u32, *Navigation) = .empty,
storage_shed: storage.Shed,
idb: IdbManager, // Per-origin IndexedDB engines
cache_store: CacheStore, // Per-origin CacheStorage
@@ -159,15 +162,9 @@ pub fn init(self: *Session, browser: *Browser, notification: *Notification) !voi
const arena = try arena_pool.acquire(.small, "Session");
errdefer arena.release();
- const navigation = try Factory.chainedWithAllocator(arena.allocator(), .{
- EventTarget{ ._type = .navigation },
- Navigation{ ._proto = undefined },
- });
-
self.* = .{
.arena = arena,
.arena_pool = arena_pool,
- .navigation = navigation,
.storage_shed = .{},
.idb = IdbManager.init(allocator),
.cache_store = CacheStore.init(allocator),
@@ -356,7 +353,7 @@ fn removePageFromList(self: *Session, page: *Page) void {
// which interleaves the OLD-page teardown with the pending-page promotion
// in a specific order.
fn tearDownPage(self: *Session, page: *Page) void {
- self.notification.dispatch(.frame_remove, .{});
+ self.notification.dispatch(.frame_remove, .{ .frame_id = page.frame._frame_id });
self.retire(page);
}
@@ -370,6 +367,8 @@ fn tearDownPage(self: *Session, page: *Page) void {
// and `active`, leaving the session pageless (the caller is responsible
// for any prior teardown of an old page).
fn installNewActivePage(self: *Session, frame_id: u32) !*Frame {
+ _ = try self.getOrCreateNavigation(frame_id);
+
const page = try self.allocatePage(frame_id);
errdefer self.queuePageDestruction(page);
@@ -401,13 +400,14 @@ pub fn createPage(self: *Session) !PageHandle {
// Document with its own session history entry, even before any real
// navigation happens. Without this, navigation.currentEntry crashes on
// a page that hasn't navigated yet.
- _ = try self.navigation.pushEntry(
+ const navigation = self.navigationFor(frame);
+ _ = try navigation.pushEntry(
frame.url,
.{ .source = .navigation, .value = null },
frame,
false,
);
- self.navigation._initial_entry = true;
+ navigation._initial_entry = true;
return .{ .session = self, .frame_id = frame_id };
}
@@ -424,6 +424,30 @@ pub fn closePage(self: *Session, frame_id: u32) void {
self.discardPendingPage(pending);
}
self.tearDownPage(live);
+ // Unlike a root navigation, this browsing context is gone for good. The
+ // Navigation's memory lives in the session arena.
+ _ = self.navigations.remove(frame_id);
+}
+
+fn getOrCreateNavigation(self: *Session, frame_id: u32) !*Navigation {
+ const gop = try self.navigations.getOrPut(self.arena.allocator(), frame_id);
+ if (gop.found_existing) {
+ return gop.value_ptr.*;
+ }
+ errdefer _ = self.navigations.remove(frame_id);
+ gop.value_ptr.* = try Factory.chainedWithAllocator(self.arena.allocator(), .{
+ EventTarget{ ._type = .navigation },
+ Navigation{ ._proto = undefined },
+ });
+ return gop.value_ptr.*;
+}
+
+// The session history of `frame`'s top-level browsing context. Child frames
+// and popups share their page's.
+pub fn navigationFor(self: *const Session, frame: *const Frame) *Navigation {
+ return self.navigations.get(frame.page.frame._frame_id) orelse {
+ lp.assert(false, "Session.navigationFor no navigation", .{ .frame_id = frame._frame_id });
+ };
}
// Does not dispatch notifications. Used in session.deinit() and used in tests.
@@ -435,6 +459,7 @@ pub fn closeAllPages(self: *Session) void {
self.queuePageDestruction(page);
}
self.pages.clearRetainingCapacity();
+ self.navigations.clearRetainingCapacity();
self.processDestroyQueues();
}
@@ -991,7 +1016,7 @@ pub fn commitPendingPage(self: *Session, replacement: *Page) !void {
// Step 1: clear the OLD page's CDP / V8 inspector state while it is still
// walkable (node_registry, inspector context group, isolated worlds). OLD
// is still the live page (index 0); its memory stays alive until step 4.
- self.notification.dispatch(.frame_remove, .{});
+ self.notification.dispatch(.frame_remove, .{ .frame_id = old.frame._frame_id });
// Step 2: register the new page with CDP. `replacement.replaces` is still
// set, so the session still reports an in-flight nav and CDP's frameCreated
@@ -1133,6 +1158,24 @@ test "Session: a root navigation whose submit fails commits an error document" {
try testing.expectEqual("Reason: TestSubmitFailure", try text.toStringSlice());
}
+test "Session: each page has its own session history" {
+ const session = testing.test_session;
+ defer session.closeAllPages();
+
+ const a = (try session.createPage()).frame().?;
+ const b = (try session.createPage()).frame().?;
+ try testing.expect(session.navigationFor(a) != session.navigationFor(b));
+
+ const js = @import("js/js.zig");
+ var ls: js.Local.Scope = undefined;
+ a.js.localScope(&ls);
+ defer ls.deinit();
+ _ = try ls.local.exec("history.pushState(null, '', '#one'); history.pushState(null, '', '#two')", null);
+
+ try testing.expectEqual(3, session.navigationFor(a)._entries.items.len);
+ try testing.expectEqual(1, session.navigationFor(b)._entries.items.len);
+}
+
test "Session: console capture runs no page JS" {
const js = @import("js/js.zig");
diff --git a/src/browser/webapi/History.zig b/src/browser/webapi/History.zig
index 312f9131c..bc6697b47 100644
--- a/src/browser/webapi/History.zig
+++ b/src/browser/webapi/History.zig
@@ -30,11 +30,11 @@ const ScrollRestoration = enum { auto, manual };
_scroll_restoration: ScrollRestoration = .auto,
pub fn getLength(_: *const History, frame: *Frame) u32 {
- return @intCast(frame._session.navigation._entries.items.len);
+ return @intCast(frame._session.navigationFor(frame)._entries.items.len);
}
pub fn getState(_: *const History, frame: *Frame) !?js.Value {
- if (frame._session.navigation.getCurrentEntry()._state.value) |state| {
+ if (frame._session.navigationFor(frame).getCurrentEntry()._state.value) |state| {
const value = try frame.js.local.?.parseJSON(state);
return value;
} else return null;
@@ -59,7 +59,7 @@ pub fn pushState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8
try arena.dupeSentinel(u8, frame.url, 0);
const json = state.toJson(arena.allocator()) catch return error.DataClone;
- _ = try session.navigation.pushEntry(url, .{ .source = .history, .value = json }, frame, true);
+ _ = try session.navigationFor(frame).pushEntry(url, .{ .source = .history, .value = json }, frame, true);
frame.url = url;
// setHref == reinitializing.
@@ -83,7 +83,7 @@ fn replaceState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8,
try arena.dupeSentinel(u8, frame.url, 0);
const json = state.toJson(arena.allocator()) catch return error.DataClone;
- _ = try session.navigation.replaceEntry(url, .{ .source = .history, .value = json }, frame, true);
+ _ = try session.navigationFor(frame).replaceEntry(url, .{ .source = .history, .value = json }, frame, true);
frame.url = url;
// setHref == reinitializing.
@@ -101,19 +101,19 @@ fn replaceState(_: *History, state: js.Value, _: ?[]const u8, _url: ?[]const u8,
fn goInner(delta: i32, frame: *Frame) !void {
// 0 behaves the same as no argument, both reloading the frame.
- const current = frame._session.navigation._index;
+ const current = frame._session.navigationFor(frame)._index;
const index_s: i64 = @intCast(@as(i64, @intCast(current)) + @as(i64, @intCast(delta)));
- if (index_s < 0 or index_s > frame._session.navigation._entries.items.len - 1) {
+ if (index_s < 0 or index_s > frame._session.navigationFor(frame)._entries.items.len - 1) {
return;
}
const index = @as(usize, @intCast(index_s));
- const entry = frame._session.navigation._entries.items[index];
+ const entry = frame._session.navigationFor(frame)._entries.items[index];
// popstate fires on the document that stays; a traversal to another
// document replaces this one instead.
const same_document = entry.sameDocument(frame);
- _ = try frame._session.navigation.navigateInner(entry._url, .{ .traverse = index }, frame);
+ _ = try frame._session.navigationFor(frame).navigateInner(entry._url, .{ .traverse = index }, frame);
if (same_document) {
try firePopState(frame, entry._state.value);
diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig
index 175d0776d..80d7400c2 100644
--- a/src/browser/webapi/Window.zig
+++ b/src/browser/webapi/Window.zig
@@ -355,7 +355,7 @@ pub fn getHistory(self: *Window) *History {
}
fn getNavigation(_: *Window, frame: *Frame) *Navigation {
- return frame._session.navigation;
+ return frame._session.navigationFor(frame);
}
fn setNavigation(self: *Window, value: js.Value) void {
diff --git a/src/browser/webapi/navigation/Navigation.zig b/src/browser/webapi/navigation/Navigation.zig
index c42121cc9..4a4e4e15d 100644
--- a/src/browser/webapi/navigation/Navigation.zig
+++ b/src/browser/webapi/navigation/Navigation.zig
@@ -572,10 +572,10 @@ test "Navigation: about:blank commits entry" {
const frame = try testing.createFrame();
defer testing.test_session.closeAllPages();
- // The test_session is shared; prior tests leak entries into it via
- // session.navigation. Reset it so we exercise a fresh session-style state.
- testing.test_session.navigation._entries.clearRetainingCapacity();
- testing.test_session.navigation._index = 0;
+ // Drop the entry createPage seeds so we exercise an empty stack.
+ const navigation = testing.test_session.navigationFor(frame);
+ navigation._entries.clearRetainingCapacity();
+ navigation._index = 0;
try frame.navigate("about:blank", .{});
try testing.waitForFrame();
@@ -599,8 +599,9 @@ test "Navigation: reload on empty stack seeds an entry" {
const frame = try testing.createFrame();
defer testing.test_session.closeAllPages();
- testing.test_session.navigation._entries.clearRetainingCapacity();
- testing.test_session.navigation._index = 0;
+ const navigation = testing.test_session.navigationFor(frame);
+ navigation._entries.clearRetainingCapacity();
+ navigation._index = 0;
// Mirrors CDP Page.reload arriving on a fresh session: doReload reads
// frame.url (default "about:blank") and routes through the synchronous
diff --git a/src/browser/webapi/navigation/NavigationHistoryEntry.zig b/src/browser/webapi/navigation/NavigationHistoryEntry.zig
index 68ba31490..28c409f39 100644
--- a/src/browser/webapi/navigation/NavigationHistoryEntry.zig
+++ b/src/browser/webapi/navigation/NavigationHistoryEntry.zig
@@ -47,7 +47,7 @@ pub fn id(self: *const NavigationHistoryEntry) []const u8 {
}
pub fn index(self: *const NavigationHistoryEntry, frame: *Frame) i32 {
- const navigation = frame._session.navigation;
+ const navigation = frame._session.navigationFor(frame);
for (navigation._entries.items, 0..) |entry, i| {
if (std.mem.eql(u8, entry._id, self._id)) {
diff --git a/src/server/bidi/browsing_context.zig b/src/server/bidi/browsing_context.zig
index f5ded20e2..44624364a 100644
--- a/src/server/bidi/browsing_context.zig
+++ b/src/server/bidi/browsing_context.zig
@@ -312,7 +312,7 @@ fn startNavigation(cmd: *BiDi.Command, ctx: *Context, frame: *Frame, url: [:0]co
// Whether the session history has an entry `delta` steps from the current one.
pub fn canTraverse(frame: *const Frame, delta: i32) bool {
- const navigation = frame._session.navigation;
+ const navigation = frame._session.navigationFor(frame);
const target = @as(i64, @intCast(navigation._index)) + delta;
return target >= 0 and target < navigation._entries.items.len;
}
diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zig
index 4f49cea65..73f56937a 100644
--- a/src/server/cdp/domains/page.zig
+++ b/src/server/cdp/domains/page.zig
@@ -450,7 +450,8 @@ fn getNavigationHistory(cmd: *CDP.Command) !void {
return error.SessionIdNotLoaded;
}
- const nav = bc.session.navigation;
+ const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
+ const nav = bc.session.navigationFor(frame);
const entries_in = nav._entries.items;
const entries_out = try cmd.arena.alloc(NavigationEntry, entries_in.len);
@@ -485,7 +486,8 @@ fn navigateToHistoryEntry(cmd: *CDP.Command) !void {
}
const session = bc.session;
- const nav = session.navigation;
+ const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
+ const nav = session.navigationFor(frame);
var target_index: ?usize = null;
var target_url: ?[:0]const u8 = null;
@@ -501,8 +503,6 @@ fn navigateToHistoryEntry(cmd: *CDP.Command) !void {
const idx = target_index orelse return error.InvalidParams;
const url = target_url orelse return error.InvalidParams;
- const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
-
const opts = Frame.NavigateOpts{
.reason = .history,
.cdp_id = cmd.input.id,