Add navigate telemetry

This commit is contained in:
Karl Seguin
2025-03-03 19:49:24 +08:00
parent 2609671982
commit 6b83281539
11 changed files with 59 additions and 44 deletions

View File

@@ -20,7 +20,7 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const json = std.json;
const Loop = @import("jsruntime").Loop;
const App = @import("../app.zig").App;
const asUint = @import("../str/parser.zig").asUint;
const Incrementing = @import("../id.zig").Incrementing;
@@ -34,7 +34,6 @@ pub const TimestampEvent = struct {
};
pub const CDP = CDPT(struct {
const Loop = *@import("jsruntime").Loop;
const Client = *@import("../server.zig").Client;
const Browser = @import("../browser/browser.zig").Browser;
const Session = @import("../browser/browser.zig").Session;
@@ -47,8 +46,6 @@ const BrowserContextIdGen = Incrementing(u32, "BID");
// Generic so that we can inject mocks into it.
pub fn CDPT(comptime TypeProvider: type) type {
return struct {
loop: TypeProvider.Loop,
// Used for sending message to the client and closing on error
client: TypeProvider.Client,
@@ -73,13 +70,13 @@ pub fn CDPT(comptime TypeProvider: type) type {
pub const Browser = TypeProvider.Browser;
pub const Session = TypeProvider.Session;
pub fn init(allocator: Allocator, client: TypeProvider.Client, loop: TypeProvider.Loop) Self {
pub fn init(app: *App, client: TypeProvider.Client) Self {
const allocator = app.allocator;
return .{
.loop = loop,
.client = client,
.allocator = allocator,
.browser_context = null,
.browser = Browser.init(allocator, loop),
.browser = Browser.init(app),
.message_arena = std.heap.ArenaAllocator.init(allocator),
.browser_context_pool = std.heap.MemoryPool(BrowserContext(Self)).init(allocator),
};

View File

@@ -7,6 +7,7 @@ const Testing = @This();
const main = @import("cdp.zig");
const parser = @import("netsurf");
const App = @import("../app.zig").App;
pub const expectEqual = std.testing.expectEqual;
pub const expectError = std.testing.expectError;
@@ -16,10 +17,9 @@ const Browser = struct {
session: ?*Session = null,
arena: std.heap.ArenaAllocator,
pub fn init(allocator: Allocator, loop: anytype) Browser {
_ = loop;
pub fn init(app: *App) Browser {
return .{
.arena = std.heap.ArenaAllocator.init(allocator),
.arena = std.heap.ArenaAllocator.init(app.allocator),
};
}
@@ -112,13 +112,13 @@ const Client = struct {
};
const TestCDP = main.CDPT(struct {
pub const Loop = void;
pub const Browser = Testing.Browser;
pub const Session = Testing.Session;
pub const Client = *Testing.Client;
});
const TestContext = struct {
app: App,
client: ?Client = null,
cdp_: ?TestCDP = null,
arena: std.heap.ArenaAllocator,
@@ -127,6 +127,7 @@ const TestContext = struct {
if (self.cdp_) |*c| {
c.deinit();
}
self.app.deinit();
self.arena.deinit();
}
@@ -135,7 +136,7 @@ const TestContext = struct {
self.client = Client.init(self.arena.allocator());
// Don't use the arena here. We want to detect leaks in CDP.
// The arena is only for test-specific stuff
self.cdp_ = TestCDP.init(std.testing.allocator, &self.client.?, {});
self.cdp_ = TestCDP.init(&self.app, &self.client.?);
}
return &self.cdp_.?;
}
@@ -262,6 +263,7 @@ const TestContext = struct {
pub fn context() TestContext {
return .{
.app = App.init(std.testing.allocator) catch unreachable,
.arena = std.heap.ArenaAllocator.init(std.testing.allocator),
};
}