From f65a39a3e34d64f96c81139a48f8618d73b4fa53 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Fri, 1 Aug 2025 21:58:24 +0800 Subject: [PATCH] Re-enable telemetry Start work on supporting navigation events (clicks, form submission). --- src/app.zig | 24 +- src/browser/Scheduler.zig | 5 + src/browser/ScriptManager.zig | 56 +-- src/browser/browser.zig | 6 +- src/browser/page.zig | 211 +++++++----- src/browser/session.zig | 12 +- src/browser/xhr/xhr.zig | 55 ++- src/cdp/cdp.zig | 17 + src/cdp/domains/page.zig | 1 - src/http/{client.zig => Client.zig} | 514 ++++++++++++++-------------- src/http/Http.zig | 269 +++++++++++++++ src/http/ca_certs.zig | 93 ----- src/http/errors.zig | 2 +- src/main.zig | 14 +- src/server.zig | 75 ++-- src/telemetry/lightpanda.zig | 29 +- src/telemetry/telemetry.zig | 17 +- src/testing.zig | 6 +- 18 files changed, 818 insertions(+), 588 deletions(-) rename src/http/{client.zig => Client.zig} (51%) create mode 100644 src/http/Http.zig delete mode 100644 src/http/ca_certs.zig diff --git a/src/app.zig b/src/app.zig index 80954855..32f78dd8 100644 --- a/src/app.zig +++ b/src/app.zig @@ -3,9 +3,9 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const log = @import("log.zig"); +const Http = @import("http/Http.zig"); const Loop = @import("runtime/loop.zig").Loop; const Platform = @import("runtime/js.zig").Platform; -const http = @import("http/client.zig"); const Telemetry = @import("telemetry/telemetry.zig").Telemetry; const Notification = @import("notification.zig").Notification; @@ -13,12 +13,12 @@ const Notification = @import("notification.zig").Notification; // Container for global state / objects that various parts of the system // might need. pub const App = struct { + http: Http, loop: *Loop, config: Config, platform: ?*const Platform, allocator: Allocator, telemetry: Telemetry, - http_client: *http.Client, app_dir_path: ?[]const u8, notification: *Notification, @@ -34,8 +34,8 @@ pub const App = struct { platform: ?*const Platform = null, tls_verify_host: bool = true, http_proxy: ?std.Uri = null, - proxy_type: ?http.ProxyType = null, - proxy_auth: ?http.ProxyAuth = null, + proxy_type: ?Http.ProxyType = null, + proxy_auth: ?Http.ProxyAuth = null, }; pub fn init(allocator: Allocator, config: Config) !*App { @@ -51,21 +51,27 @@ pub const App = struct { const notification = try Notification.init(allocator, null); errdefer notification.deinit(); + var http = try Http.init(allocator, .{ + .max_concurrent_transfers = 3, + }); + errdefer http.deinit(); + const app_dir_path = getAndMakeAppDir(allocator); app.* = .{ .loop = loop, + .http = http, .allocator = allocator, .telemetry = undefined, .platform = config.platform, .app_dir_path = app_dir_path, .notification = notification, - .http_client = try http.Client.init(allocator, .{ - .max_concurrent_transfers = 3, - }), .config = config, }; - app.telemetry = Telemetry.init(app, config.run_mode); + + app.telemetry = try Telemetry.init(app, config.run_mode); + errdefer app.telemetry.deinit(); + try app.telemetry.register(app.notification); return app; @@ -79,8 +85,8 @@ pub const App = struct { self.telemetry.deinit(); self.loop.deinit(); allocator.destroy(self.loop); - self.http_client.deinit(); self.notification.deinit(); + self.http.deinit(); allocator.destroy(self); } }; diff --git a/src/browser/Scheduler.zig b/src/browser/Scheduler.zig index 35471498..1326fb77 100644 --- a/src/browser/Scheduler.zig +++ b/src/browser/Scheduler.zig @@ -37,6 +37,11 @@ pub fn init(allocator: Allocator) Scheduler { }; } +pub fn reset(self: *Scheduler) void { + self.primary.clearRetainingCapacity(); + self.secondary.clearRetainingCapacity(); +} + const AddOpts = struct { name: []const u8 = "", }; diff --git a/src/browser/ScriptManager.zig b/src/browser/ScriptManager.zig index 1e0ea768..5cfc7414 100644 --- a/src/browser/ScriptManager.zig +++ b/src/browser/ScriptManager.zig @@ -20,11 +20,11 @@ const std = @import("std"); const log = @import("../log.zig"); const parser = @import("netsurf.zig"); -const http = @import("../http/client.zig"); -const App = @import("../app.zig").App; const Env = @import("env.zig").Env; const Page = @import("page.zig").Page; +const Browser = @import("browser.zig").Browser; +const HttpClient = @import("../http/Client.zig"); const URL = @import("../url.zig").URL; const Allocator = std.mem.Allocator; @@ -48,22 +48,23 @@ scripts: OrderList, // dom_loaded == true, deferred: OrderList, -client: *http.Client, +client: *HttpClient, allocator: Allocator, buffer_pool: BufferPool, script_pool: std.heap.MemoryPool(PendingScript), const OrderList = std.DoublyLinkedList(*PendingScript); -pub fn init(app: *App, page: *Page) ScriptManager { - const allocator = app.allocator; +pub fn init(browser: *Browser, page: *Page) ScriptManager { + // page isn't fully initialized, we can setup our reference, but that's it. + const allocator = browser.allocator; return .{ .page = page, .scripts = .{}, .deferred = .{}, .async_count = 0, .allocator = allocator, - .client = app.http_client, + .client = browser.http_client, .static_scripts_done = false, .buffer_pool = BufferPool.init(allocator, 5), .script_pool = std.heap.MemoryPool(PendingScript).init(allocator), @@ -247,13 +248,16 @@ fn evaluate(self: *ScriptManager) void { } } -fn asyncDone(self: *ScriptManager) void { - self.async_count -= 1; - if (self.async_count == 0 and // there are no more async scripts +pub fn isDone(self: *const ScriptManager) bool { + return self.async_count == 0 and // there are no more async scripts self.static_scripts_done and // and we've finished parsing the HTML to queue all self.scripts.first == null and // and there are no more