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

@@ -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),
}

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.
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,

View File

@@ -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 = "://";