Skip to content

Lightpanda: the headless browser designed for AI and automation

bidi: Add browser.getClientWindow

Karl Seguine337c9bea6fc
diff --git a/src/server/bidi/browser.zig b/src/server/bidi/browser.zig
index 92dac1599..51666ea7c 100644
--- a/src/server/bidi/browser.zig
+++ b/src/server/bidi/browser.zig
@@ -29,6 +29,7 @@ const log = lp.log;
pub fn processMessage(cmd: *BiDi.Command, action: []const u8) !void {
const command = std.meta.stringToEnum(enum {
close,
+ getClientWindows,
getUserContexts,
createUserContext,
removeUserContext,
@@ -36,6 +37,7 @@ pub fn processMessage(cmd: *BiDi.Command, action: []const u8) !void {
switch (command) {
.close => return close(cmd),
+ .getClientWindows => return getClientWindows(cmd),
.getUserContexts => return getUserContexts(cmd),
.createUserContext => return createUserContext(cmd),
.removeUserContext => return removeUserContext(cmd),
@@ -50,6 +52,33 @@ fn close(cmd: *BiDi.Command) !void {
bidi.inbox.push(arena, .quit);
}
+const ClientWindowInfo = struct {
+ active: bool,
+ clientWindow: []const u8,
+ state: []const u8,
+ x: i32,
+ y: i32,
+ width: u32,
+ height: u32,
+};
+
+fn getClientWindows(cmd: *BiDi.Command) !void {
+ const bidi = cmd.bidi;
+ if (bidi.browsing_context == null) {
+ return cmd.sendResult(.{ .clientWindows = &[_]ClientWindowInfo{} });
+ }
+ const viewport = bidi.browser.getViewport();
+ return cmd.sendResult(.{ .clientWindows = &[_]ClientWindowInfo{.{
+ .active = true,
+ .clientWindow = browsing_context.client_window,
+ .state = "normal",
+ .x = 0,
+ .y = 0,
+ .width = viewport.width,
+ .height = viewport.height,
+ }} });
+}
+
const UserContextInfo = struct { userContext: []const u8 };
fn getUserContexts(cmd: *BiDi.Command) !void {
@@ -205,3 +234,35 @@ test "bidi.browser: user contexts" {
try ctx.processMessage(.{ .id = 14, .method = "browser.createUserContext", .params = struct {}{} });
try ctx.expectSentError("unsupported operation", null, .{ .id = 14 });
}
+
+test "bidi.browser: client windows" {
+ 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.getClientWindows" });
+ try ctx.expectSentResult(.{ .clientWindows = .{} }, .{ .id = 2 });
+
+ try ctx.processMessage(.{ .id = 3, .method = "browsingContext.create", .params = .{ .type = "tab" } });
+ try ctx.expectSentEvent("browsingContext.contextCreated", .{ .clientWindow = "default" });
+
+ try ctx.processMessage(.{ .id = 4, .method = "browsingContext.getTree" });
+ try ctx.expectSentResult(.{ .contexts = .{.{ .clientWindow = "default" }} }, .{ .id = 4 });
+
+ try ctx.processMessage(.{ .id = 5, .method = "browser.getClientWindows" });
+ try ctx.expectSentResult(.{ .clientWindows = .{.{
+ .active = true,
+ .clientWindow = "default",
+ .state = "normal",
+ .x = 0,
+ .y = 0,
+ .width = 1920,
+ .height = 1080,
+ }} }, .{ .id = 5 });
+}
diff --git a/src/server/bidi/browsing_context.zig b/src/server/bidi/browsing_context.zig
index 2bee9419b..f5ded20e2 100644
--- a/src/server/bidi/browsing_context.zig
+++ b/src/server/bidi/browsing_context.zig
@@ -38,6 +38,9 @@ const remote_value = @import("remote_value.zig");
const log = lp.log;
+// The one browser.ClientWindow every browsing context lives in.
+pub const client_window = "default";
+
pub const Context = struct {
// The BiDi navigable id. Stable across navigations.
id: [36]u8,
@@ -103,6 +106,7 @@ fn getTree(cmd: *BiDi.Command) !void {
.url = url,
.context = &ctx.id,
.userContext = bidi.user_context.id(),
+ .clientWindow = client_window,
}},
});
}
@@ -155,6 +159,7 @@ pub fn openContext(bidi: *BiDi) !*Context {
.context = &ctx.id,
.url = "about:blank",
.userContext = bidi.user_context.id(),
+ .clientWindow = client_window,
.originalOpener = null,
.children = null,
});
@@ -374,6 +379,7 @@ pub fn destroy(cmd: *BiDi.Command, ctx: *Context) !void {
.context = &gone.id,
.url = url,
.userContext = bidi.user_context.id(),
+ .clientWindow = client_window,
.originalOpener = null,
.children = null,
});
@@ -776,6 +782,7 @@ const Info = struct {
context: []const u8,
url: []const u8,
userContext: []const u8,
+ clientWindow: []const u8,
originalOpener: ?[]const u8 = null,
children: []const Info = &.{},
};