Lightpanda: the headless browser designed for AI and automation
feat(webapi): add navigator.geolocation with async error delivery
Rohit0e875e2b0333
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zigindex d74723ee5..2c527d6ac 100644--- a/src/browser/js/bridge.zig+++ b/src/browser/js/bridge.zig@@ -1200,6 +1200,7 @@ pub const PageJsApis = flattenTypes(&.{@import("../webapi/PluginArray.zig"),@import("../webapi/MutationObserver.zig"),@import("../webapi/IntersectionObserver.zig"),+ @import("../webapi/Geolocation.zig"),@import("../webapi/CustomElementRegistry.zig"),@import("../webapi/ResizeObserver.zig"),@import("../webapi/IdleDeadline.zig"),diff --git a/src/browser/tests/geolocation.html b/src/browser/tests/geolocation.htmlnew file mode 100644index 000000000..dc3b2d78e--- /dev/null+++ b/src/browser/tests/geolocation.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<script src="./testing.js"></script>++<script id=geo_shape>+ testing.expectTrue('geolocation' in navigator);+ testing.expectEqual('function', typeof navigator.geolocation.getCurrentPosition);+ testing.expectEqual(2, GeolocationPositionError.POSITION_UNAVAILABLE);+</script>++<script id=geo_no_override type=module>+ {+ const state = await testing.async();+ navigator.geolocation.getCurrentPosition(+ () => { testing.fail('should not succeed without an override'); state.resolve(); },+ (err) => { state.resolve(); state.done(() => { testing.expectEqual(2, err.code); }); }+ );+ }+</script>diff --git a/src/browser/webapi/Geolocation.zig b/src/browser/webapi/Geolocation.zignew file mode 100644index 000000000..92564a391--- /dev/null+++ b/src/browser/webapi/Geolocation.zig@@ -0,0 +1,165 @@+// 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 std = @import("std");+const lp = @import("lightpanda");+const js = @import("../js/js.zig");+const Frame = @import("../Frame.zig");+const log = lp.log;++const Geolocation = @This();+_pad: bool = false, // avoid zero-size struct pointer collisions (see Permissions.zig)++pub const PositionOptions = struct {+ enableHighAccuracy: bool = false,+ timeout: ?u32 = null,+ maximumAge: u32 = 0,+};++// getCurrentPosition (and, later, watchPosition) must resolve asynchronously+// (WPT/spec requirement): the callbacks can never run synchronously inside+// the call that registered them. Rather than adding new Context/Frame/+// observers plumbing to coalesce deliveries (like IntersectionObserver+// does), each request schedules its own one-shot Task on the frame's+// existing js.Scheduler (frame.js.scheduler) — a simpler, already-proven+// async primitive (see FileReader.ReadTask) that gives us "run later,+// off this call stack" for free, including cleanup on frame teardown via+// the finalizer. watchPosition (Task 4) can layer repeat-in-ms scheduling+// on top of the same Task if it lands here.+const Task = struct {+ success: js.Function.Global,+ error_cb: ?js.Function.Global,+ frame: *Frame,++ fn deinit(self: *Task) void {+ self.success.release();+ if (self.error_cb) |cb| cb.release();+ self.frame._factory.destroy(self);+ }++ // Scheduler finalizer: called if the frame/scheduler is torn down before+ // this task got a chance to run, so the callbacks don't leak.+ fn cancelled(ctx: *anyopaque) void {+ const self: *Task = @ptrCast(@alignCast(ctx));+ self.deinit();+ }++ fn run(ctx: *anyopaque) anyerror!?u32 {+ const self: *Task = @ptrCast(@alignCast(ctx));+ defer self.deinit();+ deliverError(self.frame, self.error_cb);+ return null;+ }+};++// Task 2 only implements the no-override error path: without a+// Browser.geolocation_override, position is unavailable. Reading the+// override into a GeolocationPosition for the success callback is Task 3.+fn deliverError(frame: *Frame, error_cb: ?js.Function.Global) void {+ const err_cb = error_cb orelse return;++ const browser = frame._session.browser;+ const perm = browser.permissions.get("geolocation") orelse .prompt;+ const code: u16 = if (perm == .denied) 1 else 2;+ const message: []const u8 = if (perm == .denied) "User denied Geolocation" else "Position unavailable";++ const err = frame.arena.create(GeolocationPositionError) catch |alloc_err| {+ log.err(.frame, "geolocation.deliverError.alloc", .{ .err = alloc_err });+ return;+ };+ err.* = .{ ._code = code, ._message = message };++ var ls: js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ var caught: js.TryCatch.Caught = .{};+ ls.toLocal(err_cb).tryCall(void, .{err}, &caught) catch |call_err| {+ log.err(.frame, "geolocation.deliverError", .{ .err = call_err, .caught = caught });+ };+}++pub fn getCurrentPosition(+ _: *Geolocation,+ success: js.Function.Global,+ error_cb: ?js.Function.Global,+ options: ?PositionOptions,+ frame: *Frame,+) !void {+ _ = options; // unused until Task 3 (maximumAge/timeout) and watchPosition (Task 4)++ errdefer success.release();+ errdefer if (error_cb) |cb| cb.release();++ const task = try frame._factory.create(Task{+ .success = success,+ .error_cb = error_cb,+ .frame = frame,+ });+ errdefer frame._factory.destroy(task);++ try frame.js.scheduler.add(task, Task.run, 0, .{+ .name = "Geolocation.getCurrentPosition",+ .finalizer = Task.cancelled,+ });+}++pub const JsApi = struct {+ pub const bridge = js.Bridge(Geolocation);+ pub const Meta = struct {+ pub const name = "Geolocation";+ pub const prototype_chain = bridge.prototypeChain();+ pub var class_id: bridge.ClassId = undefined;+ pub const empty_with_no_proto = true;+ };+ pub const getCurrentPosition = bridge.function(Geolocation.getCurrentPosition, .{});+ // watchPosition / clearWatch come in Task 4+};++pub const GeolocationPositionError = struct {+ _code: u16,+ _message: []const u8,+ pub fn getCode(self: *const GeolocationPositionError) u16 {+ return self._code;+ }+ pub fn getMessage(self: *const GeolocationPositionError) []const u8 {+ return self._message;+ }+ pub const JsApi = struct {+ pub const bridge = js.Bridge(GeolocationPositionError);+ pub const Meta = struct {+ pub const name = "GeolocationPositionError";+ pub const prototype_chain = bridge.prototypeChain();+ pub var class_id: bridge.ClassId = undefined;+ };+ pub const code = bridge.accessor(GeolocationPositionError.getCode, null, .{});+ pub const message = bridge.accessor(GeolocationPositionError.getMessage, null, .{});+ pub const PERMISSION_DENIED = bridge.property(1, .{ .template = true });+ pub const POSITION_UNAVAILABLE = bridge.property(2, .{ .template = true });+ pub const TIMEOUT = bridge.property(3, .{ .template = true });+ };+};++pub fn registerTypes() []const type {+ return &.{ Geolocation, GeolocationPositionError };+}++const testing = @import("../../testing.zig");+test "WebApi: Geolocation" {+ try testing.htmlRunner("geolocation.html", .{});+}diff --git a/src/browser/webapi/Navigator.zig b/src/browser/webapi/Navigator.zigindex b692c50ed..49ae99d2d 100644--- a/src/browser/webapi/Navigator.zig+++ b/src/browser/webapi/Navigator.zig@@ -28,11 +28,13 @@ const Permissions = @import("Permissions.zig");const StorageManager = @import("StorageManager.zig");const NavigatorUAData = @import("NavigatorUAData.zig");const ModelContext = @import("ModelContext.zig");+const Geolocation = @import("Geolocation.zig");const Navigator = @This();_pad: bool = false,_plugins: PluginArray = .{},_permissions: Permissions = .{},+_geolocation: Geolocation = .{},_storage: StorageManager = .{},_ua_data: NavigatorUAData = .{},@@ -137,6 +139,10 @@ pub fn getPermissions(self: *Navigator) *Permissions {return &self._permissions;}+pub fn getGeolocation(self: *Navigator) *Geolocation {+ return &self._geolocation;+}+pub fn getStorage(self: *Navigator) *StorageManager {return &self._storage;}@@ -254,6 +260,10 @@ pub const JsApi = struct {// window onlypub const plugins = bridge.accessor(Navigator.getPlugins, null, .{ .exposed = .window });pub const modelContext = bridge.accessor(Navigator.getModelContext, null, .{ .exposed = .window });+ // Geolocation.zig/GeolocationPositionError.zig are only registered in+ // PageJsApis (not WorkerJsApis), so this accessor must stay window-only+ // too, matching plugins/modelContext above.+ pub const geolocation = bridge.accessor(Navigator.getGeolocation, null, .{ .exposed = .window });pub const registerProtocolHandler = bridge.function(Navigator.registerProtocolHandler, .{ .exposed = .window });pub const unregisterProtocolHandler = bridge.function(Navigator.unregisterProtocolHandler, .{ .exposed = .window });};