Skip to content

Lightpanda: the headless browser designed for AI and automation

feat(cdp): store Emulation.setGeolocationOverride on the browser

Rohit56ef7b95ee1b
diff --git a/src/browser/Browser.zig b/src/browser/Browser.zig
index 368b3383b..4893cb609 100644
--- a/src/browser/Browser.zig
+++ b/src/browser/Browser.zig
@@ -29,6 +29,7 @@ const Watchdog = @import("../Watchdog.zig");
const Session = @import("Session.zig");
const Selector = @import("webapi/selector/Selector.zig");
const Viewport = @import("Viewport.zig");
+const GeolocationOverride = @import("GeolocationOverride.zig");
const HttpClient = @import("../network/HttpClient.zig");
const PermissionState = @import("webapi/Permissions.zig").State;
@@ -71,6 +72,13 @@ permissions: std.StringHashMapUnmanaged(PermissionState) = .empty,
// observe the same (possibly overridden) value.
viewport_override: ?Viewport = null,
+// Runtime geolocation override set via Emulation.setGeolocationOverride and
+// cleared via clearGeolocationOverride. Null means no override, which the
+// Geolocation Web API reports as POSITION_UNAVAILABLE. Scoped to the Browser so
+// it persists across page navigations, matching how Chrome scopes the override
+// to the connection.
+geolocation_override: ?GeolocationOverride = null,
+
// used by sessions to allocate pages.
page_pool: std.heap.MemoryPool(Page),
diff --git a/src/browser/GeolocationOverride.zig b/src/browser/GeolocationOverride.zig
new file mode 100644
index 000000000..8834fb89c
--- /dev/null
+++ b/src/browser/GeolocationOverride.zig
@@ -0,0 +1,23 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const GeolocationOverride = @This();
+
+latitude: f64,
+longitude: f64,
+accuracy: f64,
diff --git a/src/cdp/domains/emulation.zig b/src/cdp/domains/emulation.zig
index 56287d684..376049a5c 100644
--- a/src/cdp/domains/emulation.zig
+++ b/src/cdp/domains/emulation.zig
@@ -32,6 +32,8 @@ pub fn processMessage(cmd: *CDP.Command) !void {
clearDeviceMetricsOverride,
setTouchEmulationEnabled,
setUserAgentOverride,
+ setGeolocationOverride,
+ clearGeolocationOverride,
}, cmd.input.action) orelse return error.UnknownMethod;
switch (action) {
@@ -41,6 +43,8 @@ pub fn processMessage(cmd: *CDP.Command) !void {
.clearDeviceMetricsOverride => return clearDeviceMetricsOverride(cmd),
.setTouchEmulationEnabled => return setTouchEmulationEnabled(cmd),
.setUserAgentOverride => return setUserAgentOverride(cmd),
+ .setGeolocationOverride => return setGeolocationOverride(cmd),
+ .clearGeolocationOverride => return clearGeolocationOverride(cmd),
}
}
@@ -171,6 +175,36 @@ pub fn setUserAgentOverride(cmd: *CDP.Command) !void {
return cmd.sendResult(null, .{});
}
+fn setGeolocationOverride(cmd: *CDP.Command) !void {
+ const Params = struct {
+ latitude: ?f64 = null,
+ longitude: ?f64 = null,
+ accuracy: ?f64 = null,
+ };
+ // Absent params emulates "position unavailable" (Chrome semantics), so fall
+ // back to all-null defaults rather than erroring.
+ const params = (try cmd.params(Params)) orelse Params{};
+
+ const browser = &cmd.cdp.browser;
+ if (params.latitude) |lat| {
+ if (params.longitude) |lon| {
+ browser.geolocation_override = .{
+ .latitude = lat,
+ .longitude = lon,
+ .accuracy = params.accuracy orelse 0,
+ };
+ return cmd.sendResult(null, .{});
+ }
+ }
+ browser.geolocation_override = null;
+ return cmd.sendResult(null, .{});
+}
+
+fn clearGeolocationOverride(cmd: *CDP.Command) !void {
+ cmd.cdp.browser.geolocation_override = null;
+ return cmd.sendResult(null, .{});
+}
+
const testing = @import("../testing.zig");
test "cdp.Emulation: setUserAgentOverride with valid user agent" {
@@ -315,3 +349,36 @@ test "cdp.Emulation: setDeviceMetricsOverride and clear" {
try testing.expectEqual(1920, page.getViewport().width);
try testing.expectEqual(1080, page.getViewport().height);
}
+
+test "cdp.Emulation: setGeolocationOverride and clear" {
+ var ctx = try testing.context();
+ defer ctx.deinit();
+
+ const bc = try ctx.loadBrowserContext(.{ .id = "BID-GEO", .url = "cdp/dom1.html" });
+ const browser = bc.session.browser;
+
+ try ctx.processMessage(.{
+ .id = 1,
+ .method = "Emulation.setGeolocationOverride",
+ .params = .{ .latitude = 48.8584, .longitude = 2.2945, .accuracy = 10 },
+ });
+ try ctx.expectSentResult(null, .{ .id = 1 });
+ try testing.expectEqual(48.8584, browser.geolocation_override.?.latitude);
+ try testing.expectEqual(2.2945, browser.geolocation_override.?.longitude);
+
+ // no coordinates => emulate "position unavailable" (stored as null)
+ try ctx.processMessage(.{ .id = 2, .method = "Emulation.setGeolocationOverride" });
+ try ctx.expectSentResult(null, .{ .id = 2 });
+ try testing.expect(browser.geolocation_override == null);
+
+ // re-set then clear
+ try ctx.processMessage(.{
+ .id = 3,
+ .method = "Emulation.setGeolocationOverride",
+ .params = .{ .latitude = 1.0, .longitude = 2.0, .accuracy = 5 },
+ });
+ try ctx.expectSentResult(null, .{ .id = 3 });
+ try ctx.processMessage(.{ .id = 4, .method = "Emulation.clearGeolocationOverride" });
+ try ctx.expectSentResult(null, .{ .id = 4 });
+ try testing.expect(browser.geolocation_override == null);
+}