Lightpanda: the headless browser designed for AI and automation
resolve pending bidi results
Karl Seguin8aef07743840
diff --git a/src/Notification.zig b/src/Notification.zigindex b989628c6..48bc53737 100644--- a/src/Notification.zig+++ b/src/Notification.zig@@ -72,6 +72,7 @@ mem_pool: std.heap.MemoryPool(Listener),const EventListeners = struct {frame_remove: List = .{},+ frame_destroyed: List = .{},frame_created: List = .{},frame_navigate: List = .{},frame_navigated: List = .{},@@ -102,6 +103,7 @@ const EventListeners = struct {const Events = union(enum) {frame_remove: FrameRemove,+ frame_destroyed: *const Frame,frame_created: *Frame,frame_navigate: *const FrameNavigate,frame_navigated: *const FrameNavigated,diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex df3b41694..e6b25894d 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -507,7 +507,9 @@ pub fn deinit(self: *Frame) void {// Unregister CookieStore from session notifications before the JS// context (and thus the scheduler) is destroyed, otherwise a late// mutation could schedule a callback that never runs.- if (self.window._cookie_store) |cs| cs.detach();+ if (self.window._cookie_store) |cs| {+ cs.detach();+ }const page = self._page;@@ -560,6 +562,8 @@ pub fn deinit(self: *Frame) void {browser.reportJsHeap();}+ // fired the last moment the js context is still alive+ page.session.notification.dispatch(.frame_destroyed, self);browser.env.destroyContext(self.js);// Must be after context is destroyed. A finalizer can reach into the *Workerdiff --git a/src/server/Driver.zig b/src/server/Driver.zigindex b2399ec4f..2e41315e2 100644--- a/src/server/Driver.zig+++ b/src/server/Driver.zig@@ -184,8 +184,8 @@ fn pageWait(self: *const Driver) ?PageWait {return .{ .session = bc.session, .frame_id = page.frame_id };},.bidi => |bidi| {- const context = bidi.context orelse return null;- return .{ .session = bidi.session, .frame_id = context.frame_id };+ const context = bidi.browsing_context orelse return null;+ return .{ .session = bidi.user_context.session, .frame_id = context.frame_id };},}}diff --git a/src/server/Server.zig b/src/server/Server.zigindex 97491bb10..6463852a9 100644--- a/src/server/Server.zig+++ b/src/server/Server.zig@@ -1208,6 +1208,8 @@ test "server: bidi errors" {}test "server: bidi browsingContext" {+ testing.silenceLog(&.{.not_implemented});+var c = try createTestClient();defer c.deinit();try c.handshake("/session");diff --git a/src/server/bidi/BiDi.zig b/src/server/bidi/BiDi.zigindex f83136508..0f19c0fe0 100644--- a/src/server/bidi/BiDi.zig+++ b/src/server/bidi/BiDi.zig@@ -31,8 +31,8 @@ const NodeRegistry = @import("../../NodeRegistry.zig");const Driver = @import("../Driver.zig");const Connection = @import("../Connection.zig");+const script = @import("script.zig");const remote_value = @import("remote_value.zig");-const browsing_context = @import("browsing_context.zig");const posix = std.posix;const Allocator = std.mem.Allocator;@@ -57,9 +57,14 @@ session_arena: std.heap.ArenaAllocator,session_id: ?[36]u8 = null,browser: Browser,-session: *Session,notification: *Notification,-context: ?browsing_context.Context = null,++// browsing context is the page+browsing_context: ?@import("browsing_context.zig").Context = null,++// A user context is a Session. There's a "default" and we allow 1 more to+// be created (limited because a Browser can only have 1 Session).+user_context: UserContext,// Issues the script module's sharedIdsnode_registry: NodeRegistry,@@ -67,6 +72,9 @@ node_registry: NodeRegistry,// Values the client owns via `resultOwnership: "root"`handles: remote_value.Handles,+// Commands awaiting promise resolution+pending: std.ArrayList(*script.Pending) = .empty,+subscriptions: std.ArrayList(Subscription) = .empty,const Subscription = struct {@@ -86,7 +94,7 @@ pub fn init(self: *BiDi, app: *App, socket: posix.socket_t) !void {.link = undefined,.conn = undefined,.browser = undefined,- .session = undefined,+ .user_context = undefined,.notification = undefined,.node_registry = .init(allocator),.handles = .{ .allocator = allocator },@@ -109,18 +117,25 @@ pub fn init(self: *BiDi, app: *App, socket: posix.socket_t) !void {.socket = socket,.handles = http_client.handles,};-- // the remaining failures are OOM-class; no point cleaning upself.notification = try Notification.init(allocator);- self.session = try self.browser.newSession(self.notification);- try browsing_context.registerNotifications(self);+ errdefer self.notification.deinit();++ try self.newUserContext("default");+ try @import("browsing_context.zig").registerNotifications(self);}pub fn deinit(self: *BiDi) void {+ const allocator = self.app.allocator;+self.notification.unregisterAll(self);+ // Cancel first, so that any completions during session teardown are still valid+ script.Pending.cancelAll(self);self.handles.deinit();self.browser.closeSession();+ // Now we can destroy+ script.Pending.destroyAll(self);+ self.pending.deinit(allocator);self.node_registry.deinit();self.notification.deinit();@@ -130,10 +145,36 @@ pub fn deinit(self: *BiDi) void {self.session_arena.deinit();}+pub fn replaceSession(self: *BiDi, id: []const u8) !void {+ self.resetRealm();+ try self.newUserContext(id);+}++fn newUserContext(self: *BiDi, id: []const u8) !void {+ const session = try self.browser.newSession(self.notification);+ self.user_context = .{ .session = session, .id_len = @intCast(id.len), .id_buf = undefined };+ @memcpy(self.user_context.id_buf[0..id.len], id);+}++pub const UserContext = struct {+ id_len: u8,+ id_buf: [36]u8, // "default" or uuid+ session: *Session,++ pub fn id(self: *const UserContext) []const u8 {+ return self.id_buf[0..self.id_len];+ }++ pub fn isDefault(self: *const UserContext) bool {+ return std.mem.eql(u8, self.id(), "default");+ }+};+pub fn resetRealm(self: *BiDi) void {+ script.Pending.realmReset(self);self.handles.releaseAll();self.node_registry.reset();- if (self.context) |*ctx| {+ if (self.browsing_context) |*ctx| {uuidv4(&ctx.realm_id);}}@@ -203,7 +244,7 @@ fn dispatch(self: *BiDi, arena: Allocator, id: u64, method: []const u8, data: [].session => return @import("session.zig").processMessage(&cmd),.script => return @import("script.zig").processMessage(&cmd),.browser => return @import("browser.zig").processMessage(&cmd),- .browsingContext => return browsing_context.processMessage(&cmd),+ .browsingContext => return @import("browsing_context.zig").processMessage(&cmd),}}diff --git a/src/server/bidi/browser.zig b/src/server/bidi/browser.zigindex 6e5053790..d5909783e 100644--- a/src/server/bidi/browser.zig+++ b/src/server/bidi/browser.zig@@ -17,18 +17,28 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.const std = @import("std");+const lp = @import("lightpanda");++const uuidv4 = @import("../../id.zig").uuidv4;const BiDi = @import("BiDi.zig");+const browsing_context = @import("browsing_context.zig");++const log = lp.log;pub fn processMessage(cmd: *const BiDi.Command) !void {const command = std.meta.stringToEnum(enum {close,getUserContexts,+ createUserContext,+ removeUserContext,}, cmd.action) orelse return error.UnknownCommand;switch (command) {.close => return close(cmd),.getUserContexts => return getUserContexts(cmd),+ .createUserContext => return createUserContext(cmd),+ .removeUserContext => return removeUserContext(cmd),}}@@ -40,10 +50,158 @@ fn close(cmd: *const BiDi.Command) !void {browser.http_client.inbox.push(arena, .close);}+const UserContextInfo = struct { userContext: []const u8 };+fn getUserContexts(cmd: *const BiDi.Command) !void {- return cmd.sendResult(.{- .userContexts = &[_]struct { userContext: []const u8 }{- .{ .userContext = "default" },- },+ const bidi = cmd.bidi;+ var infos: [2]UserContextInfo = .{ .{ .userContext = "default" }, undefined };+ var len: usize = 1;+ if (bidi.user_context.isDefault() == false) {+ len = 2;+ infos[1] = .{ .userContext = bidi.user_context.id() };+ }+ return cmd.sendResult(.{ .userContexts = infos[0..len] });+}++fn createUserContext(cmd: *const BiDi.Command) !void {+ const p = try cmd.params(struct {+ proxy: ?std.json.Value = null,+ acceptInsecureCerts: ?bool = null,+ unhandledPromptBehavior: ?std.json.Value = null,+ });+ if (p.proxy != null or p.acceptInsecureCerts != null or p.unhandledPromptBehavior != null) {+ log.warn(.not_implemented, "bidi.createUserContext", .{ .proxy = p.proxy != null, .acceptInsecureCerts = p.acceptInsecureCerts, .unhandledPromptBehavior = p.unhandledPromptBehavior != null });+ return cmd.sendError("unsupported operation", "user context options are not supported");+ }++ const bidi = cmd.bidi;+ if (bidi.user_context.isDefault() == false) {+ log.warn(.not_implemented, "bidi.createUserContext", .{ .feature = "multiple user contexts" });+ return cmd.sendError("unsupported operation", "only one user context can exist besides the default");+ }++ if (bidi.browsing_context != null) {+ // Can't create a new UserContext (Session) if we have a browing context (Page) already opened+ return cmd.sendError("unsupported operation", "close the browsing context first");+ }++ var id: [36]u8 = undefined;+ uuidv4(&id);+ try bidi.replaceSession(&id);++ return cmd.sendResult(.{ .userContext = &id });+}++fn removeUserContext(cmd: *const BiDi.Command) !void {+ const p = try cmd.params(struct {+ userContext: []const u8,+ });++ if (std.mem.eql(u8, p.userContext, "default")) {+ return cmd.sendError("invalid argument", "the default user context cannot be removed");+ }++ const bidi = cmd.bidi;+ if (std.mem.eql(u8, p.userContext, bidi.user_context.id()) == false) {+ return cmd.sendError("no such user context", "unknown user context");+ }++ if (bidi.browsing_context) |*ctx| {+ try browsing_context.destroy(cmd, ctx);+ }+ try bidi.replaceSession("default");++ return cmd.sendResult(struct {}{});+}++const testing = @import("testing.zig");+test "bidi.browser: user contexts" {+ testing.silenceLog(&.{.not_implemented});+ var ctx = try testing.context();+ defer ctx.deinit();++ try ctx.startSession();+ try ctx.processMessage(.{+ .id = 1,+ .method = "session.subscribe",+ .params = .{ .events = .{"browsingContext"} },+ });++ try ctx.processMessage(.{ .id = 2, .method = "browser.getUserContexts" });+ try ctx.expectSentResult(.{ .userContexts = .{.{ .userContext = "default" }} }, .{ .id = 2 });++ try ctx.processMessage(.{ .id = 3, .method = "browser.createUserContext", .params = struct {}{} });+ const user_context = try testing.arena.dupe(u8, ctx.bidi().user_context.id());+ try ctx.expectSentResult(.{ .userContext = user_context }, .{ .id = 3 });++ try ctx.processMessage(.{ .id = 4, .method = "browser.getUserContexts" });+ try ctx.expectSentResult(.{+ .userContexts = .{ .{ .userContext = "default" }, .{ .userContext = user_context } },+ }, .{ .id = 4 });++ // A user context is the browser's one Session, so a second one can't+ // exist alongside it, and neither can a page in the default.+ try ctx.processMessage(.{ .id = 5, .method = "browser.createUserContext", .params = struct {}{} });+ try ctx.expectSentError("unsupported operation", null, .{ .id = 5 });++ try ctx.processMessage(.{+ .id = 6,+ .method = "browsingContext.create",+ .params = .{ .type = "tab", .userContext = "default" },+ });+ try ctx.expectSentError("no such user context", null, .{ .id = 6 });++ try ctx.processMessage(.{+ .id = 7,+ .method = "browsingContext.create",+ .params = .{ .type = "tab", .userContext = user_context },});+ const context_id = try testing.arena.dupe(u8, &(ctx.bidi().browsing_context orelse return error.NoBrowsingContext).id);+ try ctx.expectSentResult(.{ .context = context_id }, .{ .id = 7 });+ try ctx.expectSentEvent("browsingContext.contextCreated", .{ .context = context_id, .userContext = user_context });++ try ctx.processMessage(.{+ .id = 8,+ .method = "script.evaluate",+ .params = .{ .expression = "1 + 1", .target = .{ .context = context_id } },+ });+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "number", .value = 2 } }, .{ .id = 8 });++ try ctx.processMessage(.{+ .id = 9,+ .method = "browser.removeUserContext",+ .params = .{ .userContext = "default" },+ });+ try ctx.expectSentError("invalid argument", null, .{ .id = 9 });++ // Removal takes the browsing context with it and restores the default.+ try ctx.processMessage(.{+ .id = 10,+ .method = "browser.removeUserContext",+ .params = .{ .userContext = user_context },+ });+ try ctx.expectSentResult(null, .{ .id = 10 });+ try ctx.expectSentEvent("browsingContext.contextDestroyed", .{ .context = context_id, .userContext = user_context });+ try testing.expectEqual(null, ctx.bidi().browsing_context);+ try testing.expectEqual(true, ctx.bidi().user_context.isDefault());++ try ctx.processMessage(.{+ .id = 11,+ .method = "browser.removeUserContext",+ .params = .{ .userContext = user_context },+ });+ try ctx.expectSentError("no such user context", null, .{ .id = 11 });++ try ctx.processMessage(.{ .id = 12, .method = "browsingContext.create", .params = .{ .type = "tab" } });+ try ctx.expectSentEvent("browsingContext.contextCreated", .{ .userContext = "default" });+ try ctx.processMessage(.{+ .id = 13,+ .method = "script.evaluate",+ .params = .{ .expression = "2 + 2", .target = .{ .context = &(ctx.bidi().browsing_context orelse return error.NoBrowsingContext).id } },+ });+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "number", .value = 4 } }, .{ .id = 13 });++ // With a page open in the default, its Session can't be swapped out.+ try ctx.processMessage(.{ .id = 14, .method = "browser.createUserContext", .params = struct {}{} });+ try ctx.expectSentError("unsupported operation", null, .{ .id = 14 });}diff --git a/src/server/bidi/browsing_context.zig b/src/server/bidi/browsing_context.zigindex c21807559..23b936e9d 100644--- a/src/server/bidi/browsing_context.zig+++ b/src/server/bidi/browsing_context.zig@@ -19,12 +19,15 @@const std = @import("std");const lp = @import("lightpanda");-const URL = @import("../../browser/URL.zig");const uuidv4 = @import("../../id.zig").uuidv4;++const URL = @import("../../browser/URL.zig");+const Frame = @import("../../browser/Frame.zig");const Session = @import("../../browser/Session.zig");const Notification = @import("../../Notification.zig");const BiDi = @import("BiDi.zig");+const script = @import("script.zig");const log = lp.log;@@ -69,24 +72,40 @@ pub fn processMessage(cmd: *const BiDi.Command) !void {fn getTree(cmd: *const BiDi.Command) !void {const bidi = cmd.bidi;- const ctx = &(bidi.context orelse {+ const ctx = &(bidi.browsing_context orelse {return cmd.sendResult(.{ .contexts = &[_]Info{} });});- const url: []const u8 = if (bidi.session.currentFrame()) |frame| frame.url else "about:blank";+ const url: []const u8 = if (bidi.user_context.session.currentFrame()) |frame| frame.url else "about:blank";return cmd.sendResult(.{- .contexts = &[_]Info{.{ .context = &ctx.id, .url = url }},+ .contexts = &[_]Info{.{+ .url = url,+ .context = &ctx.id,+ .userContext = bidi.user_context.id(),+ }},});}-// {type: "tab" | "window"} ignoredfn create(cmd: *const BiDi.Command) !void {+ const p = try cmd.params(struct {+ type: enum { tab, window },+ userContext: ?[]const u8 = null,+ referenceContext: ?[]const u8 = null,+ background: bool = false,+ });+const bidi = cmd.bidi;- if (bidi.context != null) {+ if (bidi.browsing_context != null) {+ log.warn(.not_implemented, "bidi.BrowingContext", .{ .feature = "multiple user contexts" });return cmd.sendError("unsupported operation", "only a single browsing context is supported");}- const page = bidi.session.createPage() catch |err| {+ const requested = p.userContext orelse "default";+ if (std.mem.eql(u8, requested, bidi.user_context.id()) == false) {+ return cmd.sendError("no such user context", "unknown user context");+ }++ const page = bidi.user_context.session.createPage() catch |err| {log.err(.bidi, "create page", .{ .err = err });return cmd.sendError("unknown error", "failed to create page");};@@ -100,12 +119,12 @@ fn create(cmd: *const BiDi.Command) !void {uuidv4(&ctx.id);uuidv4(&ctx.realm_id);uuidv4(&ctx.navigation_id);- bidi.context = ctx;+ bidi.browsing_context = ctx;try cmd.sendEvent("browsingContext.contextCreated", .{.context = &ctx.id,.url = "about:blank",- .userContext = "default",+ .userContext = bidi.user_context.id(),.originalOpener = null,.children = null,});@@ -134,7 +153,7 @@ fn navigate(cmd: *const BiDi.Command) !void {const bidi = cmd.bidi;const ctx = (try requireContext(cmd, p.context)) orelse return;- const frame = bidi.session.currentFrame() orelse {+ const frame = bidi.user_context.session.currentFrame() orelse {return cmd.sendError("unknown error", "no frame");};const encoded_url = URL.resolveNavigation(frame.call_arena, p.url, .{}) catch {@@ -160,7 +179,7 @@ fn navigate(cmd: *const BiDi.Command) !void {const nav_result = if (frame._load_state == .waiting)frame.navigate(encoded_url, .{ .reason = .address_bar, .kind = .{ .push = null } })else- bidi.session.initiateRootNavigation(frame._frame_id, encoded_url, .{ .reason = .address_bar, .kind = .{ .push = null } });+ bidi.user_context.session.initiateRootNavigation(frame._frame_id, encoded_url, .{ .reason = .address_bar, .kind = .{ .push = null } });nav_result catch |err| {log.warn(.bidi, "navigate", .{ .err = err });@@ -178,33 +197,39 @@ fn close(cmd: *const BiDi.Command) !void {context: []const u8,});- const bidi = cmd.bidi;const ctx = (try requireContext(cmd, p.context)) orelse return;+ try destroy(cmd, ctx);+ return cmd.sendResult(struct {}{});+}++// Closes the page behind `ctx` and reports it gone.+pub fn destroy(cmd: *const BiDi.Command, ctx: *Context) !void {+ const bidi = cmd.bidi;try rejectPending(bidi, ctx, "browsing context closed");- const ctx_id = ctx.id;++ // ctx points into bidi.browsing_context, which is about to be cleared+ const gone = ctx.*;// the frame's url doesn't survive closePage- const url: []const u8 = if (bidi.session.currentFrame()) |frame|+ const url: []const u8 = if (bidi.user_context.session.currentFrame()) |frame|try cmd.arena.dupe(u8, frame.url)else"about:blank";- bidi.session.closePage(ctx.frame_id);- bidi.context = null;+ bidi.user_context.session.closePage(gone.frame_id);+ bidi.browsing_context = null;try cmd.sendEvent("browsingContext.contextDestroyed", .{- .context = &ctx_id,+ .context = &gone.id,.url = url,- .userContext = "default",+ .userContext = bidi.user_context.id(),.originalOpener = null,.children = null,});-- return cmd.sendResult(struct {}{});}pub fn requireContext(cmd: *const BiDi.Command, context: []const u8) !?*Context {- if (cmd.bidi.context) |*ctx| {+ if (cmd.bidi.browsing_context) |*ctx| {if (std.mem.eql(u8, &ctx.id, context)) {return ctx;}@@ -236,6 +261,7 @@ pub fn registerNotifications(bidi: *BiDi) !void {const notification = bidi.notification;try notification.register(.frame_loaded, bidi, onFrameLoaded);try notification.register(.frame_remove, bidi, onFrameRemove);+ try notification.register(.frame_destroyed, bidi, onFrameDestroyed);try notification.register(.frame_navigate, bidi, onFrameNavigate);try notification.register(.frame_navigate_failed, bidi, onFrameNavigateFailed);try notification.register(.frame_dom_content_loaded, bidi, onFrameDOMContentLoaded);@@ -247,6 +273,11 @@ fn onFrameRemove(ptr: *anyopaque, _: Notification.FrameRemove) !void {bidi.resetRealm();}+fn onFrameDestroyed(ptr: *anyopaque, frame: *const Frame) !void {+ const bidi: *BiDi = @ptrCast(@alignCast(ptr));+ script.Pending.contextDestroyed(bidi, frame.js.id);+}+fn onFrameNavigate(ptr: *anyopaque, msg: *const Notification.FrameNavigate) !void {const bidi: *BiDi = @ptrCast(@alignCast(ptr));const ctx = rootContext(bidi, msg.frame_id) orelse return;@@ -299,7 +330,7 @@ fn frameLifecycleEvent(const bidi: *BiDi = @ptrCast(@alignCast(ptr));const ctx = rootContext(bidi, frame_id) orelse return;- const url: []const u8 = if (bidi.session.currentFrame()) |frame| frame.url else "";+ const url: []const u8 = if (bidi.user_context.session.currentFrame()) |frame| frame.url else "";try bidi.sendEvent(method, .{.context = &ctx.id,.navigation = &ctx.navigation_id,@@ -320,7 +351,7 @@ fn frameLifecycleEvent(// Only the root frame maps to the BiDi context; child-frame events are// dropped until child navigables are supported.fn rootContext(bidi: *BiDi, frame_id: u32) ?*Context {- if (bidi.context) |*ctx| {+ if (bidi.browsing_context) |*ctx| {if (ctx.frame_id == frame_id) {return ctx;}@@ -333,7 +364,7 @@ fn rootContext(bidi: *BiDi, frame_id: u32) ?*Context {const Info = struct {context: []const u8,url: []const u8,- userContext: []const u8 = "default",+ userContext: []const u8,originalOpener: ?[]const u8 = null,children: []const Info = &.{},};diff --git a/src/server/bidi/remote_value.zig b/src/server/bidi/remote_value.zigindex 405a98c09..f995b4c1a 100644--- a/src/server/bidi/remote_value.zig+++ b/src/server/bidi/remote_value.zig@@ -288,13 +288,13 @@ pub const Serializer = struct {// Locals, not globals: everything happens inside the caller's scope.seen: std.ArrayList(js.Object) = .empty,- pub fn init(cmd: *const BiDi.Command, frame: *Frame, local: *const js.Local, opts: Options) Serializer {+ pub fn init(bidi: *BiDi, arena: Allocator, frame: *Frame, local: *const js.Local, opts: Options) Serializer {return .{.frame = frame,.local = local,- .arena = cmd.arena,- .handles = &cmd.bidi.handles,- .registry = &cmd.bidi.node_registry,+ .arena = arena,+ .handles = &bidi.handles,+ .registry = &bidi.node_registry,.opts = opts,};}diff --git a/src/server/bidi/script.zig b/src/server/bidi/script.zigindex 1e625dd71..ddf449f64 100644--- a/src/server/bidi/script.zig+++ b/src/server/bidi/script.zig@@ -27,6 +27,7 @@ const remote_value = @import("remote_value.zig");const browsing_context = @import("browsing_context.zig");const log = lp.log;+const Allocator = std.mem.Allocator;pub fn processMessage(cmd: *const BiDi.Command) !void {const command = std.meta.stringToEnum(enum {@@ -69,7 +70,7 @@ fn evaluate(cmd: *const BiDi.Command) !void {});const ctx = (try resolveTarget(cmd, p.target)) orelse return;- const frame = cmd.bidi.session.currentFrame() orelse {+ const frame = cmd.bidi.user_context.session.currentFrame() orelse {return cmd.sendError("no such frame", "no frame");};@@ -81,11 +82,12 @@ fn evaluate(cmd: *const BiDi.Command) !void {try_catch.init(&ls.local);defer try_catch.deinit();+ const reply: Reply = .init(cmd, ctx);const value = ls.local.exec(p.expression, "bidi.evaluate") catch |err| {- return sendException(cmd, ctx, frame, &ls.local, &try_catch, err);+ return sendException(&reply, frame, &ls.local, &try_catch, err);};- return settle(cmd, ctx, frame, &ls.local, value, p.awaitPromise, .{+ return settle(&reply, frame, &ls.local, value, p.awaitPromise, .{.own_root = p.resultOwnership == .root,.max_dom_depth = p.serializationOptions.maxDomDepth,.max_object_depth = p.serializationOptions.maxObjectDepth,@@ -106,7 +108,7 @@ fn callFunction(cmd: *const BiDi.Command) !void {const bidi = cmd.bidi;const ctx = (try resolveTarget(cmd, p.target)) orelse return;- const frame = bidi.session.currentFrame() orelse {+ const frame = bidi.user_context.session.currentFrame() orelse {return cmd.sendError("no such frame", "no frame");};@@ -123,8 +125,9 @@ fn callFunction(cmd: *const BiDi.Command) !void {// the functionDeclaration has a trailing comment.const arena = cmd.arena;const source = try std.fmt.allocPrint(arena, "({s}\n)", .{p.functionDeclaration});+ const reply: Reply = .init(cmd, ctx);const declaration = ls.local.exec(source, "bidi.callFunction") catch |err| {- return sendException(cmd, ctx, frame, &ls.local, &try_catch, err);+ return sendException(&reply, frame, &ls.local, &try_catch, err);};if (declaration.isFunction() == false) {return cmd.sendError("invalid argument", "functionDeclaration is not a function");@@ -140,10 +143,10 @@ fn callFunction(cmd: *const BiDi.Command) !void {const this = if (p.this) |t| ((try toJs(cmd, &ls.local, t)) orelse return) else try ls.local.zigValueToJs({}, .{});const value = function.callWithThisRethrow(js.Value, this, arguments) catch |err| {- return sendException(cmd, ctx, frame, &ls.local, &try_catch, err);+ return sendException(&reply, frame, &ls.local, &try_catch, err);};- return settle(cmd, ctx, frame, &ls.local, value, p.awaitPromise, .{+ return settle(&reply, frame, &ls.local, value, p.awaitPromise, .{.own_root = p.resultOwnership == .root,.max_dom_depth = p.serializationOptions.maxDomDepth,.max_object_depth = p.serializationOptions.maxObjectDepth,@@ -169,11 +172,37 @@ fn disown(cmd: *const BiDi.Command) !void {return cmd.sendResult(struct {}{});}-// Answers `cmd` with an EvaluateResultSuccess, resolving the promise first if-// the client asked for it.+// What answering a command takes, so the same code serves both an immediate+// reply and one sent from a promise callback after the Command (and its+// message_arena) is gone.+const Reply = struct {+ id: u64,+ bidi: *BiDi,+ arena: Allocator,+ realm_id: [36]u8,++ fn init(cmd: *const BiDi.Command, ctx: *const browsing_context.Context) Reply {+ return .{+ .id = cmd.id,+ .bidi = cmd.bidi,+ .arena = cmd.arena,+ .realm_id = ctx.realm_id,+ };+ }++ fn sendResult(self: *const Reply, result: anytype) !void {+ return self.bidi.sendResult(self.id, result);+ }++ fn sendError(self: *const Reply, code: []const u8, message: []const u8) !void {+ return self.bidi.sendError(self.id, code, message);+ }+};++// Answers with an EvaluateResultSuccess, resolving the promise first if the+// client asked for it.fn settle(- cmd: *const BiDi.Command,- ctx: *const browsing_context.Context,+ reply: *const Reply,frame: *Frame,local: *const js.Local,value: js.Value,@@ -186,33 +215,153 @@ fn settle(const promise = value.toPromise();promise.markAsHandled();- // A promise that only needs the microtask queue — an async function- // with no real await — settles right here. One waiting on I/O won't:- // answering that means holding the command open across event-loop- // ticks, which is machinery we don't have yet.+ // some promises can be settled immediately.local.runMicrotasks();switch (promise.state()) {- .pending => return cmd.sendError("unsupported operation", "awaitPromise on a pending promise"),.fulfilled => result = promise.result(),- .rejected => return sendRejection(cmd, ctx, frame, local, promise.result()),+ .pending => return Pending.await(reply, promise, opts),+ .rejected => return sendRejection(reply, frame, local, promise.result()),}}- const remote = (try serialize(cmd, frame, local, result, opts)) orelse return;- return cmd.sendResult(.{+ const remote = (try serialize(reply, frame, local, result, opts)) orelse return;+ return reply.sendResult(.{.type = "success",.result = remote,- .realm = &ctx.realm_id,+ .realm = &reply.realm_id,});}+// A script command waiting a promise resolution.+pub const Pending = struct {+ reply: Reply,+ arena: *lp.Arena,+ js_context_id: usize,+ cancelled: bool = false,+ opts: remote_value.Options,++ fn await(reply: *const Reply, promise: js.Promise, opts: remote_value.Options) !void {+ const bidi = reply.bidi;+ const arena = try bidi.app.arena_pool.acquire(.small, "bidi Pending");+ errdefer arena.release();++ const pending = try arena.allocator().create(Pending);+ pending.* = .{+ .opts = opts,+ .arena = arena,+ .js_context_id = promise.local.ctx.id,+ .reply = .{ // clone reply, injecting our Pending's arena+ .id = reply.id,+ .bidi = reply.bidi,+ .arena = arena.allocator(),+ .realm_id = reply.realm_id,+ },+ };+ try bidi.pending.append(bidi.app.allocator, pending);+ errdefer _ = bidi.pending.pop();++ const local = promise.local;+ _ = try promise.thenAndCatch(+ local.newCallback(onFulfilled, pending),+ local.newCallback(onRejected, pending),+ );+ }++ fn deinit(self: *Pending) void {+ self.arena.release();+ }++ fn onFulfilled(self: *Pending, value: js.Value, exec: *const js.Execution) void {+ self.finish(value, .fulfilled, exec);+ }++ fn onRejected(self: *Pending, reason: js.Value, exec: *const js.Execution) void {+ self.finish(reason, .rejected, exec);+ }++ fn finish(+ self: *Pending,+ value: js.Value,+ state: enum { fulfilled, rejected },+ exec: *const js.Execution,+ ) void {+ defer self.destroy();+ if (self.cancelled) {+ return;+ }++ const reply = &self.reply;+ const local = exec.js.local.?;+ const frame = reply.bidi.user_context.session.currentFrame() orelse {+ reply.sendError("no such frame", "no frame") catch {};+ return;+ };++ const result = switch (state) {+ .fulfilled => settle(reply, frame, local, value, false, self.opts),+ .rejected => sendRejection(reply, frame, local, value),+ };++ result catch |err| {+ log.err(.bidi, "await promise", .{ .err = err, .id = reply.id });+ };+ }++ fn destroy(self: *Pending) void {+ const bidi = self.reply.bidi;+ for (bidi.pending.items, 0..) |pending, i| {+ if (pending == self) {+ _ = bidi.pending.swapRemove(i);+ break;+ }+ }+ self.deinit();+ }++ // The realm is being replaced: answer the client now. The Pending remains+ // alive until it fires or Frame.deinit -> ... -> contextDestroyed is called+ pub fn realmReset(bidi: *BiDi) void {+ for (bidi.pending.items) |pending| {+ if (pending.cancelled) {+ continue;+ }+ pending.cancelled = true;+ pending.reply.sendError("no such realm", "realm destroyed while awaiting a promise") catch {};+ }+ }++ // A frame (and it's JS context) is being destroyed.+ pub fn contextDestroyed(bidi: *BiDi, js_context_id: usize) void {+ var i = bidi.pending.items.len;+ while (i > 0) {+ i -= 1;+ const pending = bidi.pending.items[i];+ if (pending.js_context_id == js_context_id) {+ _ = bidi.pending.swapRemove(i);+ pending.deinit();+ }+ }+ }++ pub fn cancelAll(bidi: *BiDi) void {+ for (bidi.pending.items) |pending| {+ pending.cancelled = true;+ }+ }++ pub fn destroyAll(bidi: *BiDi) void {+ while (bidi.pending.pop()) |pending| {+ pending.deinit();+ }+ }+};+// A JS throw isn't a command failure: the command succeeds and reports the// exception in its result. The thrown value is always serialized with the// default options rather than the command'sfn sendException(- cmd: *const BiDi.Command,- ctx: *const browsing_context.Context,+ reply: *const Reply,frame: *Frame,local: *const js.Local,try_catch: *js.TryCatch,@@ -222,13 +371,13 @@ fn sendException(return err;}- const arena = cmd.arena;+ const arena = reply.arena;const caught = try_catch.caughtOrError(arena, err);const thrown = try_catch.exceptionValue();const exception: remote_value.Remote = blk: {const value = thrown orelse break :blk .{ .body = .undefined };- break :blk (try serialize(cmd, frame, local, value, .{})) orelse return;+ break :blk (try serialize(reply, frame, local, value, .{})) orelse return;};// `text` is the exception stringified ("Error: nope"), not just its message@@ -238,30 +387,27 @@ fn sendException(break :blk value.toStringSliceWithAlloc(arena) catch fallback;};- return sendExceptionDetails(cmd, ctx, exception, text, caught.line);+ return sendExceptionDetails(reply, exception, text, caught.line);}fn sendRejection(- cmd: *const BiDi.Command,- ctx: *const browsing_context.Context,+ reply: *const Reply,frame: *Frame,local: *const js.Local,reason: js.Value,) !void {- const arena = cmd.arena;- const text = reason.toStringSliceWithAlloc(arena) catch "promise rejected";- const exception = (try serialize(cmd, frame, local, reason, .{})) orelse return;- return sendExceptionDetails(cmd, ctx, exception, text, null);+ const text = reason.toStringSliceWithAlloc(reply.arena) catch "promise rejected";+ const exception = (try serialize(reply, frame, local, reason, .{})) orelse return;+ return sendExceptionDetails(reply, exception, text, null);}fn sendExceptionDetails(- cmd: *const BiDi.Command,- ctx: *const browsing_context.Context,+ reply: *const Reply,exception: remote_value.Remote,text: []const u8,line: ?u32,) !void {- return cmd.sendResult(.{+ return reply.sendResult(.{.type = "exception",.exceptionDetails = .{.text = text,@@ -270,25 +416,25 @@ fn sendExceptionDetails(.columnNumber = 0, // required by the spec, even if we don't know it.stackTrace = .{ .callFrames = &[_]struct {}{} },},- .realm = &ctx.realm_id,+ .realm = &reply.realm_id,});}fn serialize(- cmd: *const BiDi.Command,+ reply: *const Reply,frame: *Frame,local: *const js.Local,value: js.Value,opts: remote_value.Options,) !?remote_value.Remote {- var serializer = remote_value.Serializer.init(cmd, frame, local, opts);+ var serializer = remote_value.Serializer.init(reply.bidi, reply.arena, frame, local, opts);return serializer.run(value) catch |err| {if (err == error.OutOfMemory) {return err;}log.warn(.bidi, "serialize", .{ .err = err });- try cmd.sendError("unknown error", "cannot serialize result");+ try reply.sendError("unknown error", "cannot serialize result");return null;};}@@ -327,7 +473,7 @@ fn resolveTarget(cmd: *const BiDi.Command, target: Target) !?*browsing_context.C}if (target.realm) |realm| {- if (bidi.context) |*ctx| {+ if (bidi.browsing_context) |*ctx| {if (std.mem.eql(u8, &ctx.realm_id, realm)) {return ctx;}@@ -846,18 +992,98 @@ test "bidi.script: evaluate awaitPromise" {.result = .{ .type = "promise" },}, .{ .id = 3 });- // A promise waiting on a timer can't settle without running the event- // loop, which means holding the command open across ticks.+ // A promise waiting on a timer needs the event loop: the command stays+ // open and is answered from the callback.try ctx.processMessage(.{.id = 4,.method = "script.evaluate",.params = .{- .expression = "new Promise(r => setTimeout(r, 0))",+ .expression = "new Promise(r => setTimeout(() => r({n: 42}), 5))",+ .target = .{ .context = context_id },+ .awaitPromise = true,+ },+ });+ try ctx.expectNotAnswered(4);+ try ctx.wait();+ try ctx.expectSentResult(.{+ .type = "success",+ .result = .{ .type = "object", .value = .{.{ "n", .{ .type = "number", .value = 42 } }} },+ }, .{ .id = 4 });++ try ctx.processMessage(.{+ .id = 5,+ .method = "script.callFunction",+ .params = .{+ .functionDeclaration = "() => new Promise((_, reject) => setTimeout(() => reject(new Error('late')), 5))",+ .target = .{ .context = context_id },+ .awaitPromise = true,+ },+ });+ try ctx.wait();+ try ctx.expectSentResult(.{+ .type = "exception",+ .exceptionDetails = .{ .text = "Error: late", .exception = .{ .type = "error" } },+ }, .{ .id = 5 });++ // resultOwnership applies to the settled value, not the promise.+ try ctx.processMessage(.{+ .id = 6,+ .method = "script.evaluate",+ .params = .{+ .expression = "new Promise(r => setTimeout(() => r({}), 5))",.target = .{ .context = context_id },.awaitPromise = true,+ .resultOwnership = "root",},});- try ctx.expectSentError("unsupported operation", "awaitPromise on a pending promise", .{ .id = 4 });+ try ctx.wait();+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "object", .handle = "1" } }, .{ .id = 6 });+}++test "bidi.script: awaitPromise across a navigation" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const context_id = try ctx.createContext(.{ .url = "bidi/values.html" });++ // A promise that never settles: the navigation is what answers it, with+ // an error, so the client isn't left hanging on a realm that's gone.+ try ctx.processMessage(.{+ .id = 1,+ .method = "script.evaluate",+ .params = .{+ .expression = "new Promise(() => {})",+ .target = .{ .context = context_id },+ .awaitPromise = true,+ },+ });+ try ctx.expectNotAnswered(1);++ try ctx.processMessage(.{+ .id = 2,+ .method = "browsingContext.navigate",+ .params = .{+ .context = context_id,+ .url = testing.test_server ++ "bidi/values.html",+ .wait = "complete",+ },+ });+ try ctx.wait();+ try ctx.expectSentError("no such realm", null, .{ .id = 1 });+ try ctx.expectSentResult(null, .{ .id = 2 });++ // The new realm works as usual, timers included.+ try ctx.processMessage(.{+ .id = 3,+ .method = "script.evaluate",+ .params = .{+ .expression = "new Promise(r => setTimeout(() => r('after'), 5))",+ .target = .{ .context = context_id },+ .awaitPromise = true,+ },+ });+ try ctx.wait();+ try ctx.expectSentResult(.{ .type = "success", .result = .{ .type = "string", .value = "after" } }, .{ .id = 3 });}test "bidi.script: callFunction arguments" {@@ -1070,7 +1296,7 @@ test "bidi.script: target validation" {try ctx.expectSentError("unsupported operation", "sandboxes are not supported", .{ .id = 3 });// The realm reported alongside a result is also accepted as a target.- const realm = try testing.arena.dupe(u8, &ctx.bidi().context.?.realm_id);+ const realm = try testing.arena.dupe(u8, &ctx.bidi().browsing_context.?.realm_id);try ctx.processMessage(.{.id = 4,.method = "script.evaluate",diff --git a/src/server/bidi/testing.zig b/src/server/bidi/testing.zigindex 1d70a205a..7e1c4f8d0 100644--- a/src/server/bidi/testing.zig+++ b/src/server/bidi/testing.zig@@ -95,7 +95,7 @@ const TestContext = struct {try self.startSession();try self.processMessage(.{ .id = command_id_create, .method = "browsingContext.create", .params = .{ .type = "tab" } });- const ctx = &(self.bidi().context orelse return error.NoBrowsingContext);+ const ctx = &(self.bidi().browsing_context orelse return error.NoBrowsingContext);const context_id = try base.arena_allocator.dupe(u8, &ctx.id);if (opts.url) |url| {@@ -117,13 +117,13 @@ const TestContext = struct {// when the command already answered.pub fn wait(self: *TestContext) !void {const b = self.bidi();- const ctx = b.context orelse return error.NoBrowsingContext;- var runner = b.session.runner(.{});+ const ctx = b.browsing_context orelse return error.NoBrowsingContext;+ var runner = b.user_context.session.runner(.{});try runner.waitForFrame(ctx.frame_id, 2000, .{ .until = .done });}pub fn frame(self: *TestContext) !*Frame {- return self.bidi().session.currentFrame() orelse error.NoFrame;+ return self.bidi().user_context.session.currentFrame() orelse error.NoFrame;}const SentOpts = struct {@@ -190,6 +190,18 @@ const TestContext = struct {return self.received.items[index];}+ // No reply for `id` yet: the command is being held open.+ pub fn expectNotAnswered(self: *TestContext, id: usize) !void {+ try self.read();+ for (self.received.items) |received| {+ const got = received.object.get("id") orelse continue;+ if (got == .integer and got.integer == id) {+ self.dumpReceived();+ return error.UnexpectedAnswer;+ }+ }+ }+pub fn expectSentCount(self: *TestContext, expected: usize) !void {try self.read();try expectEqual(expected, self.received.items.len);