Skip to content

Commit 7309fec

Browse files
committed
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.
1 parent 2e01fa7 commit 7309fec

File tree

5 files changed

+30
-30
lines changed

5 files changed

+30
-30
lines changed

src/browser/browser.zig

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,6 @@ pub const Session = struct {
173173
const page = &self.page.?;
174174
try Page.init(page, page_arena.allocator(), self);
175175

176-
self.notify(&.{ .context_created = .{
177-
.origin = try page.origin(),
178-
} });
179-
180176
// start JS env
181177
log.debug("start new js scope", .{});
182178

@@ -331,9 +327,9 @@ pub const Page = struct {
331327
log.debug("wait: OK", .{});
332328
}
333329

334-
fn origin(self: *const Page) ![]const u8 {
330+
pub fn origin(self: *const Page, arena: Allocator) ![]const u8 {
335331
var arr: std.ArrayListUnmanaged(u8) = .{};
336-
try self.url.origin(arr.writer(self.arena));
332+
try self.url.origin(arr.writer(arena));
337333
return arr.items;
338334
}
339335

@@ -369,10 +365,6 @@ pub const Page = struct {
369365
.timestamp = timestamp(),
370366
} });
371367

372-
session.notify(&.{ .context_created = .{
373-
.origin = try self.origin(),
374-
} });
375-
376368
var response = try request.sendSync(.{});
377369

378370
// would be different than self.url in the case of a redirect
@@ -449,7 +441,7 @@ pub const Page = struct {
449441
// TODO set the referrer to the document.
450442
try self.window.replaceDocument(html_doc);
451443
self.window.setStorageShelf(
452-
try self.session.storage_shed.getOrPut(try self.origin()),
444+
try self.session.storage_shed.getOrPut(try self.origin(self.arena)),
453445
);
454446

455447
// https://html.spec.whatwg.org/#read-html

src/cdp/cdp.zig

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,6 @@ pub fn BrowserContext(comptime CDP_T: type) type {
398398
const self: *Self = @alignCast(@ptrCast(ctx));
399399

400400
switch (notification.*) {
401-
.context_created => |cc| if (self.target_id) |target_id| {
402-
const aux_data = try std.fmt.allocPrint(self.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
403-
self.inspector.contextCreated(
404-
self.session.page.?.scope,
405-
"",
406-
cc.origin,
407-
aux_data,
408-
true,
409-
);
410-
},
411401
.page_navigate => |*pn| return @import("domains/page.zig").pageNavigate(self, pn),
412402
.page_navigated => |*pn| return @import("domains/page.zig").pageNavigated(self, pn),
413403
}

src/cdp/domains/page.zig

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,22 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
217217
// The client will expect us to send new contextCreated events, such that the client has new id's for the active contexts.
218218
try cdp.sendEvent("Runtime.executionContextsCleared", null, .{ .session_id = session_id });
219219

220-
if (bc.isolated_world) |*isolated_world| {
221-
var buffer: [256]u8 = undefined;
222-
const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{bc.target_id.?});
220+
var buffer: [512]u8 = undefined;
221+
{
222+
var fba = std.heap.FixedBufferAllocator.init(&buffer);
223+
const page = bc.session.currentPage().?;
224+
const aux_data = try std.fmt.allocPrint(fba.allocator(), "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
225+
bc.inspector.contextCreated(
226+
page.scope,
227+
"",
228+
try page.origin(fba.allocator()),
229+
aux_data,
230+
true,
231+
);
232+
}
223233

234+
if (bc.isolated_world) |*isolated_world| {
235+
const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{target_id});
224236
// Calling contextCreated will assign a new Id to the context and send the contextCreated event
225237
bc.inspector.contextCreated(
226238
isolated_world.scope,

src/cdp/domains/target.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,18 @@ fn createTarget(cmd: anytype) !void {
125125
try bc.createIsolatedWorld();
126126
bc.target_id = target_id;
127127

128-
_ = try bc.session.createPage();
128+
const page = try bc.session.createPage();
129+
130+
{
131+
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
132+
bc.inspector.contextCreated(
133+
page.scope,
134+
"",
135+
try page.origin(cmd.arena),
136+
aux_data,
137+
true,
138+
);
139+
}
129140

130141
// change CDP state
131142
bc.security_origin = "://";

src/notification.zig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const browser = @import("browser/browser.zig");
44
pub const Notification = union(enum) {
55
page_navigate: PageNavigate,
66
page_navigated: PageNavigated,
7-
context_created: ContextCreated,
87

98
pub const PageNavigate = struct {
109
timestamp: u32,
@@ -16,8 +15,4 @@ pub const Notification = union(enum) {
1615
timestamp: u32,
1716
url: *const URL,
1817
};
19-
20-
pub const ContextCreated = struct {
21-
origin: []const u8,
22-
};
2318
};

0 commit comments

Comments
 (0)