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.
This commit is contained in:
Karl Seguin
2025-04-29 13:29:42 +08:00
parent 2e01fa738a
commit 7309fec51d
5 changed files with 30 additions and 30 deletions

View File

@@ -173,10 +173,6 @@ pub const Session = struct {
const page = &self.page.?; const page = &self.page.?;
try Page.init(page, page_arena.allocator(), self); try Page.init(page, page_arena.allocator(), self);
self.notify(&.{ .context_created = .{
.origin = try page.origin(),
} });
// start JS env // start JS env
log.debug("start new js scope", .{}); log.debug("start new js scope", .{});
@@ -331,9 +327,9 @@ pub const Page = struct {
log.debug("wait: OK", .{}); 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) = .{}; var arr: std.ArrayListUnmanaged(u8) = .{};
try self.url.origin(arr.writer(self.arena)); try self.url.origin(arr.writer(arena));
return arr.items; return arr.items;
} }
@@ -369,10 +365,6 @@ pub const Page = struct {
.timestamp = timestamp(), .timestamp = timestamp(),
} }); } });
session.notify(&.{ .context_created = .{
.origin = try self.origin(),
} });
var response = try request.sendSync(.{}); var response = try request.sendSync(.{});
// would be different than self.url in the case of a redirect // 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. // TODO set the referrer to the document.
try self.window.replaceDocument(html_doc); try self.window.replaceDocument(html_doc);
self.window.setStorageShelf( 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 // https://html.spec.whatwg.org/#read-html

View File

@@ -398,16 +398,6 @@ pub fn BrowserContext(comptime CDP_T: type) type {
const self: *Self = @alignCast(@ptrCast(ctx)); const self: *Self = @alignCast(@ptrCast(ctx));
switch (notification.*) { 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_navigate => |*pn| return @import("domains/page.zig").pageNavigate(self, pn),
.page_navigated => |*pn| return @import("domains/page.zig").pageNavigated(self, pn), .page_navigated => |*pn| return @import("domains/page.zig").pageNavigated(self, pn),
} }

View File

@@ -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. // 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 }); try cdp.sendEvent("Runtime.executionContextsCleared", null, .{ .session_id = session_id });
if (bc.isolated_world) |*isolated_world| { var buffer: [512]u8 = undefined;
var buffer: [256]u8 = undefined; {
const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{bc.target_id.?}); 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 // Calling contextCreated will assign a new Id to the context and send the contextCreated event
bc.inspector.contextCreated( bc.inspector.contextCreated(
isolated_world.scope, isolated_world.scope,

View File

@@ -125,7 +125,18 @@ fn createTarget(cmd: anytype) !void {
try bc.createIsolatedWorld(); try bc.createIsolatedWorld();
bc.target_id = target_id; 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 // change CDP state
bc.security_origin = "://"; bc.security_origin = "://";

View File

@@ -4,7 +4,6 @@ const browser = @import("browser/browser.zig");
pub const Notification = union(enum) { pub const Notification = union(enum) {
page_navigate: PageNavigate, page_navigate: PageNavigate,
page_navigated: PageNavigated, page_navigated: PageNavigated,
context_created: ContextCreated,
pub const PageNavigate = struct { pub const PageNavigate = struct {
timestamp: u32, timestamp: u32,
@@ -16,8 +15,4 @@ pub const Notification = union(enum) {
timestamp: u32, timestamp: u32,
url: *const URL, url: *const URL,
}; };
pub const ContextCreated = struct {
origin: []const u8,
};
}; };