From 7309fec51da1d2460b8e496dcddd7b3e0565e5b6 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Tue, 29 Apr 2025 13:29:42 +0800 Subject: [PATCH] Fully fake contextCreated emit contextCreated when it's needed, not when it actually happens. I thought we could make this sync-up, but we'd need to create 3 contexts to satisfy both puppeteer and chromedp. So rather than having it partially driven by notifications from Browser, I rather just fake it all for now. --- src/browser/browser.zig | 14 +++----------- src/cdp/cdp.zig | 10 ---------- src/cdp/domains/page.zig | 18 +++++++++++++++--- src/cdp/domains/target.zig | 13 ++++++++++++- src/notification.zig | 5 ----- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/browser/browser.zig b/src/browser/browser.zig index 6dd6638b..a9a17e73 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -173,10 +173,6 @@ pub const Session = struct { const page = &self.page.?; try Page.init(page, page_arena.allocator(), self); - self.notify(&.{ .context_created = .{ - .origin = try page.origin(), - } }); - // start JS env log.debug("start new js scope", .{}); @@ -331,9 +327,9 @@ pub const Page = struct { log.debug("wait: OK", .{}); } - fn origin(self: *const Page) ![]const u8 { + pub fn origin(self: *const Page, arena: Allocator) ![]const u8 { var arr: std.ArrayListUnmanaged(u8) = .{}; - try self.url.origin(arr.writer(self.arena)); + try self.url.origin(arr.writer(arena)); return arr.items; } @@ -369,10 +365,6 @@ pub const Page = struct { .timestamp = timestamp(), } }); - session.notify(&.{ .context_created = .{ - .origin = try self.origin(), - } }); - var response = try request.sendSync(.{}); // would be different than self.url in the case of a redirect @@ -449,7 +441,7 @@ pub const Page = struct { // TODO set the referrer to the document. try self.window.replaceDocument(html_doc); self.window.setStorageShelf( - try self.session.storage_shed.getOrPut(try self.origin()), + try self.session.storage_shed.getOrPut(try self.origin(self.arena)), ); // https://html.spec.whatwg.org/#read-html diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 3e9c38f1..e8ebfde7 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -398,16 +398,6 @@ pub fn BrowserContext(comptime CDP_T: type) type { const self: *Self = @alignCast(@ptrCast(ctx)); switch (notification.*) { - .context_created => |cc| if (self.target_id) |target_id| { - const aux_data = try std.fmt.allocPrint(self.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id}); - self.inspector.contextCreated( - self.session.page.?.scope, - "", - cc.origin, - aux_data, - true, - ); - }, .page_navigate => |*pn| return @import("domains/page.zig").pageNavigate(self, pn), .page_navigated => |*pn| return @import("domains/page.zig").pageNavigated(self, pn), } diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig index 9ef65d6f..676db373 100644 --- a/src/cdp/domains/page.zig +++ b/src/cdp/domains/page.zig @@ -217,10 +217,22 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void // The client will expect us to send new contextCreated events, such that the client has new id's for the active contexts. try cdp.sendEvent("Runtime.executionContextsCleared", null, .{ .session_id = session_id }); - if (bc.isolated_world) |*isolated_world| { - var buffer: [256]u8 = undefined; - const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{bc.target_id.?}); + var buffer: [512]u8 = undefined; + { + var fba = std.heap.FixedBufferAllocator.init(&buffer); + const page = bc.session.currentPage().?; + const aux_data = try std.fmt.allocPrint(fba.allocator(), "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id}); + bc.inspector.contextCreated( + page.scope, + "", + try page.origin(fba.allocator()), + aux_data, + true, + ); + } + if (bc.isolated_world) |*isolated_world| { + const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{target_id}); // Calling contextCreated will assign a new Id to the context and send the contextCreated event bc.inspector.contextCreated( isolated_world.scope, diff --git a/src/cdp/domains/target.zig b/src/cdp/domains/target.zig index a5a7e0bf..17359866 100644 --- a/src/cdp/domains/target.zig +++ b/src/cdp/domains/target.zig @@ -125,7 +125,18 @@ fn createTarget(cmd: anytype) !void { try bc.createIsolatedWorld(); bc.target_id = target_id; - _ = try bc.session.createPage(); + const page = try bc.session.createPage(); + + { + const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id}); + bc.inspector.contextCreated( + page.scope, + "", + try page.origin(cmd.arena), + aux_data, + true, + ); + } // change CDP state bc.security_origin = "://"; diff --git a/src/notification.zig b/src/notification.zig index 5d883590..24310ae4 100644 --- a/src/notification.zig +++ b/src/notification.zig @@ -4,7 +4,6 @@ const browser = @import("browser/browser.zig"); pub const Notification = union(enum) { page_navigate: PageNavigate, page_navigated: PageNavigated, - context_created: ContextCreated, pub const PageNavigate = struct { timestamp: u32, @@ -16,8 +15,4 @@ pub const Notification = union(enum) { timestamp: u32, url: *const URL, }; - - pub const ContextCreated = struct { - origin: []const u8, - }; };