Skip to content

Introduce more general notification capabilities #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/app.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const js = @import("runtime/js.zig");
const Loop = @import("runtime/loop.zig").Loop;
const HttpClient = @import("http/client.zig").Client;
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
const Notification = @import("notification.zig").Notification;

const log = std.log.scoped(.app);

Expand All @@ -17,6 +18,7 @@ pub const App = struct {
telemetry: Telemetry,
http_client: HttpClient,
app_dir_path: ?[]const u8,
notification: *Notification,

pub const RunMode = enum {
help,
Expand All @@ -41,19 +43,24 @@ pub const App = struct {
loop.* = try Loop.init(allocator);
errdefer loop.deinit();

const notification = try Notification.init(allocator, null);
errdefer notification.deinit();

const app_dir_path = getAndMakeAppDir(allocator);

app.* = .{
.loop = loop,
.allocator = allocator,
.telemetry = undefined,
.app_dir_path = app_dir_path,
.notification = notification,
.http_client = try HttpClient.init(allocator, 5, .{
.tls_verify_host = config.tls_verify_host,
}),
.config = config,
};
app.telemetry = Telemetry.init(app, config.run_mode);
try app.telemetry.register(app.notification);

return app;
}
Expand All @@ -67,6 +74,7 @@ pub const App = struct {
self.loop.deinit();
allocator.destroy(self.loop);
self.http_client.deinit();
self.notification.deinit();
allocator.destroy(self);
}
};
Expand Down
49 changes: 13 additions & 36 deletions src/browser/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub const Browser = struct {
allocator: Allocator,
http_client: *http.Client,
page_arena: ArenaAllocator,
notification: *Notification,

pub fn init(app: *App) !Browser {
const allocator = app.allocator;
Expand All @@ -67,11 +68,15 @@ pub const Browser = struct {
});
errdefer env.deinit();

const notification = try Notification.init(allocator, app.notification);
errdefer notification.deinit();

return .{
.app = app,
.env = env,
.session = null,
.allocator = allocator,
.notification = notification,
.http_client = &app.http_client,
.page_arena = ArenaAllocator.init(allocator),
};
Expand All @@ -81,13 +86,14 @@ pub const Browser = struct {
self.closeSession();
self.env.deinit();
self.page_arena.deinit();
self.notification.deinit();
}

pub fn newSession(self: *Browser, ctx: anytype) !*Session {
pub fn newSession(self: *Browser) !*Session {
self.closeSession();
self.session = @as(Session, undefined);
const session = &self.session.?;
try Session.init(session, self, ctx);
try Session.init(session, self);
return session;
}

Expand Down Expand Up @@ -119,32 +125,14 @@ pub const Session = struct {

page: ?Page = null,

// recipient of notification, passed as the first parameter to notify
notify_ctx: *anyopaque,
notify_func: *const fn (ctx: *anyopaque, notification: *const Notification) anyerror!void,

fn init(self: *Session, browser: *Browser, ctx: anytype) !void {
const ContextT = @TypeOf(ctx);
const ContextStruct = switch (@typeInfo(ContextT)) {
.@"struct" => ContextT,
.pointer => |ptr| ptr.child,
.void => NoopContext,
else => @compileError("invalid context type"),
};

// ctx can be void, to be able to store it in our *anyopaque field, we
// need to play a little game.
const any_ctx: *anyopaque = if (@TypeOf(ctx) == void) @constCast(@ptrCast(&{})) else ctx;

fn init(self: *Session, browser: *Browser) !void {
var executor = try browser.env.newExecutor();
errdefer executor.deinit();

const allocator = browser.app.allocator;
self.* = .{
.browser = browser,
.executor = executor,
.notify_ctx = any_ctx,
.notify_func = ContextStruct.notify,
.arena = ArenaAllocator.init(allocator),
.storage_shed = storage.Shed.init(allocator),
.cookie_jar = storage.CookieJar.init(allocator),
Expand Down Expand Up @@ -213,12 +201,6 @@ pub const Session = struct {
.reason = .anchor,
});
}

fn notify(self: *const Session, notification: *const Notification) void {
self.notify_func(self.notify_ctx, notification) catch |err| {
log.err("notify {}: {}", .{ std.meta.activeTag(notification.*), err });
};
}
};

// Page navigates to an url.
Expand Down Expand Up @@ -350,20 +332,15 @@ pub const Page = struct {
// redirect)
self.url = request_url;

session.browser.app.telemetry.record(.{ .navigate = .{
.proxy = false,
.tls = std.ascii.eqlIgnoreCase(request_url.scheme(), "https"),
} });

// load the data
var request = try self.newHTTPRequest(.GET, &self.url, .{ .navigation = true });
defer request.deinit();

session.notify(&.{ .page_navigate = .{
session.browser.notification.dispatch(.page_navigate, &.{
.url = &self.url,
.reason = opts.reason,
.timestamp = timestamp(),
} });
});

var response = try request.sendSync(.{});

Expand Down Expand Up @@ -399,10 +376,10 @@ pub const Page = struct {
self.raw_data = arr.items;
}

session.notify(&.{ .page_navigated = .{
session.browser.notification.dispatch(.page_navigated, &.{
.url = &self.url,
.timestamp = timestamp(),
} });
});
}

// https://html.spec.whatwg.org/#read-html
Expand Down
18 changes: 12 additions & 6 deletions src/cdp/cdp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
fn init(self: *Self, id: []const u8, cdp: *CDP_T) !void {
const allocator = cdp.allocator;

const session = try cdp.browser.newSession(self);
const session = try cdp.browser.newSession();
const arena = session.arena.allocator();

const inspector = try cdp.browser.env.newInspector(arena, self);
Expand All @@ -337,6 +337,10 @@ pub fn BrowserContext(comptime CDP_T: type) type {
.inspector = inspector,
};
self.node_search_list = Node.Search.List.init(allocator, &self.node_registry);
errdefer self.deinit();

try cdp.browser.notification.register(.page_navigate, self, onPageNavigate);
try cdp.browser.notification.register(.page_navigated, self, onPageNavigated);
}

pub fn deinit(self: *Self) void {
Expand All @@ -352,6 +356,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
}
self.node_registry.deinit();
self.node_search_list.deinit();
self.cdp.browser.notification.unregisterAll(self);
}

pub fn reset(self: *Self) void {
Expand Down Expand Up @@ -394,13 +399,14 @@ pub fn BrowserContext(comptime CDP_T: type) type {
return if (raw_url.len == 0) null else raw_url;
}

pub fn notify(ctx: *anyopaque, notification: *const Notification) !void {
pub fn onPageNavigate(ctx: *anyopaque, data: *const Notification.PageNavigate) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
return @import("domains/page.zig").pageNavigate(self, data);
}

switch (notification.*) {
.page_navigate => |*pn| return @import("domains/page.zig").pageNavigate(self, pn),
.page_navigated => |*pn| return @import("domains/page.zig").pageNavigated(self, pn),
}
pub fn onPageNavigated(ctx: *anyopaque, data: *const Notification.PageNavigated) !void {
const self: *Self = @alignCast(@ptrCast(ctx));
return @import("domains/page.zig").pageNavigated(self, data);
}

pub fn callInspector(self: *const Self, msg: []const u8) void {
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn main() !void {
var browser = try Browser.init(app);
defer browser.deinit();

var session = try browser.newSession({});
var session = try browser.newSession();

// page
const page = try session.createPage();
Expand Down
Loading
Loading