Skip to content

Lightpanda: the headless browser designed for AI and automation

cdp: accept the emulation and setup calls drivers send at context creation

Adrià Arrufat06167f7a88bc
diff --git a/src/cdp/domains/emulation.zig b/src/cdp/domains/emulation.zig
index 445129f0f..820e63a57 100644
--- a/src/cdp/domains/emulation.zig
+++ b/src/cdp/domains/emulation.zig
@@ -35,6 +35,10 @@ pub fn processMessage(cmd: *CDP.Command) !void {
setUserAgentOverride,
setGeolocationOverride,
clearGeolocationOverride,
+ setLocaleOverride,
+ setTimezoneOverride,
+ setScriptExecutionDisabled,
+ setDefaultBackgroundColorOverride,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -46,6 +50,11 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.setUserAgentOverride => return setUserAgentOverride(cmd),
.setGeolocationOverride => return setGeolocationOverride(cmd),
.clearGeolocationOverride => return clearGeolocationOverride(cmd),
+ .setLocaleOverride => return setLocaleOverride(cmd),
+ .setTimezoneOverride => return setTimezoneOverride(cmd),
+ .setScriptExecutionDisabled => return setScriptExecutionDisabled(cmd),
+ // Nothing is painted with a background.
+ .setDefaultBackgroundColorOverride => return cmd.sendResult(null, .{}),
}
}
@@ -202,8 +211,61 @@ fn clearGeolocationOverride(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
+// Accepted so drivers can finish context setup; Intl/Date keep the host's
+// locale and timezone.
+fn setLocaleOverride(cmd: *CDP.Command) !void {
+ const Params = struct { locale: ?[]const u8 = null };
+ const params = (try cmd.params(Params)) orelse Params{};
+ if (params.locale) |v| {
+ log.warn(.not_implemented, "Emulation.setLocaleOverride", .{ .locale = v });
+ }
+ return cmd.sendResult(null, .{});
+}
+
+fn setTimezoneOverride(cmd: *CDP.Command) !void {
+ const params = (try cmd.params(struct {
+ timezoneId: []const u8,
+ })) orelse return error.InvalidParams;
+ if (params.timezoneId.len > 0) {
+ log.warn(.not_implemented, "Emulation.setTimezoneOverride", .{ .timezoneId = params.timezoneId });
+ }
+ return cmd.sendResult(null, .{});
+}
+
+fn setScriptExecutionDisabled(cmd: *CDP.Command) !void {
+ const params = (try cmd.params(struct {
+ value: bool,
+ })) orelse return error.InvalidParams;
+ if (params.value) {
+ return cmd.sendError(-32000, "Disabling script execution is not supported", .{});
+ }
+ return cmd.sendResult(null, .{});
+}
+
const testing = @import("../testing.zig");
+test "cdp.Emulation: locale, timezone and script execution stubs" {
+ testing.silenceLog(&.{.not_implemented});
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-EMU" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Emulation.setLocaleOverride", .params = .{ .locale = "fr-FR" } });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+
+ try ctx.processMessage(.{ .id = 2, .method = "Emulation.setTimezoneOverride", .params = .{ .timezoneId = "Europe/Paris" } });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+
+ try ctx.processMessage(.{ .id = 3, .method = "Emulation.setScriptExecutionDisabled", .params = .{ .value = false } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+
+ try ctx.processMessage(.{ .id = 4, .method = "Emulation.setScriptExecutionDisabled", .params = .{ .value = true } });
+ try ctx.expectSentError(-32000, "Disabling script execution is not supported", .{ .id = 4 });
+
+ try ctx.processMessage(.{ .id = 5, .method = "Emulation.setDefaultBackgroundColorOverride", .params = .{ .color = .{ .r = 0, .g = 0, .b = 0, .a = 0 } } });
+ try ctx.expectSentResult(null, .{ .id = 5 });
+}
+
test "cdp.Emulation: setUserAgentOverride with valid user agent" {
var ctx = try testing.context();
defer ctx.deinit();
diff --git a/src/cdp/domains/network.zig b/src/cdp/domains/network.zig
index df83a30a0..9271ee9ce 100644
--- a/src/cdp/domains/network.zig
+++ b/src/cdp/domains/network.zig
@@ -57,6 +57,8 @@ pub fn processMessage(cmd: *CDP.Command) !void {
getAllCookies,
getResponseBody,
getRequestPostData,
+ emulateNetworkConditions,
+ setBypassServiceWorker,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -76,9 +78,30 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.getAllCookies => return getAllCookies(cmd),
.getResponseBody => return getResponseBody(cmd),
.getRequestPostData => return getRequestPostData(cmd),
+ .emulateNetworkConditions => return emulateNetworkConditions(cmd),
+ // There are no service workers to bypass.
+ .setBypassServiceWorker => return cmd.sendResult(null, .{}),
}
}
+fn emulateNetworkConditions(cmd: *CDP.Command) !void {
+ const params = (try cmd.params(struct {
+ offline: bool,
+ latency: f64,
+ downloadThroughput: f64,
+ uploadThroughput: f64,
+ })) orelse return error.InvalidParams;
+
+ if (params.offline) {
+ return cmd.sendError(-32000, "Offline emulation is not supported", .{});
+ }
+ // -1 disables a throughput limit.
+ if (params.latency > 0 or params.downloadThroughput > 0 or params.uploadThroughput > 0) {
+ log.warn(.not_implemented, "Network.emulateNetworkConditions", .{ .param = "throttling" });
+ }
+ return cmd.sendResult(null, .{});
+}
+
fn enable(cmd: *CDP.Command) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
try bc.networkEnable();
@@ -678,6 +701,22 @@ fn keyFromRequestId(request_id: []const u8) !CDP.BrowserContext.CapturedKey {
}
const testing = @import("../testing.zig");
+test "cdp.network emulateNetworkConditions and setBypassServiceWorker" {
+ testing.silenceLog(&.{.not_implemented});
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-NET" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Network.emulateNetworkConditions", .params = .{ .offline = false, .latency = 0, .downloadThroughput = -1, .uploadThroughput = -1 } });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+
+ try ctx.processMessage(.{ .id = 2, .method = "Network.emulateNetworkConditions", .params = .{ .offline = true, .latency = 0, .downloadThroughput = -1, .uploadThroughput = -1 } });
+ try ctx.expectSentError(-32000, "Offline emulation is not supported", .{ .id = 2 });
+
+ try ctx.processMessage(.{ .id = 3, .method = "Network.setBypassServiceWorker", .params = .{ .bypass = true } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+}
+
test "cdp.network setExtraHTTPHeaders" {
var ctx = try testing.context();
defer ctx.deinit();
diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig
index 98d52b245..b65960baf 100644
--- a/src/cdp/domains/page.zig
+++ b/src/cdp/domains/page.zig
@@ -49,6 +49,9 @@ pub fn processMessage(cmd: *CDP.Command) !void {
printToPDF,
getLayoutMetrics,
handleJavaScriptDialog,
+ setBypassCSP,
+ bringToFront,
+ setInterceptFileChooserDialog,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -68,6 +71,9 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.printToPDF => return printToPDF(cmd),
.getLayoutMetrics => return getLayoutMetrics(cmd),
.handleJavaScriptDialog => return handleJavaScriptDialog(cmd),
+ // CSP isn't enforced, there is a single page, and no file chooser
+ // ever opens: each of these already holds.
+ .setBypassCSP, .bringToFront, .setInterceptFileChooserDialog => return cmd.sendResult(null, .{}),
}
}
@@ -1127,6 +1133,21 @@ fn getLayoutMetrics(cmd: *CDP.Command) !void {
}
const testing = @import("../testing.zig");
+test "cdp.frame: setup no-ops" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+ _ = try ctx.loadBrowserContext(.{ .id = "BID-NOOP" });
+
+ try ctx.processMessage(.{ .id = 1, .method = "Page.setBypassCSP", .params = .{ .enabled = true } });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+
+ try ctx.processMessage(.{ .id = 2, .method = "Page.bringToFront" });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+
+ try ctx.processMessage(.{ .id = 3, .method = "Page.setInterceptFileChooserDialog", .params = .{ .enabled = true } });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+}
+
test "cdp.frame: getFrameTree" {
var ctx = try testing.context();
defer ctx.deinit();