From e0bcb625c28410224911d089ec775ff83403bf5e Mon Sep 17 00:00:00 2001 From: sjorsdonkers <72333389+sjorsdonkers@users.noreply.github.com> Date: Tue, 22 Apr 2025 14:37:42 +0200 Subject: [PATCH] browsercontext arena --- src/browser/browser.zig | 1 + src/cdp/cdp.zig | 10 ++++++++-- src/cdp/domains/page.zig | 10 ++++------ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/browser/browser.zig b/src/browser/browser.zig index 26f5bd8e..5823df99 100644 --- a/src/browser/browser.zig +++ b/src/browser/browser.zig @@ -126,6 +126,7 @@ pub const Session = struct { // // The arena is initialised with self.alloc allocator. // all others Session deps use directly self.alloc and not the arena. + // The arena is also used in the BrowserContext arena: std.heap.ArenaAllocator, window: Window, diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 304c4fa4..fa17fec3 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -284,6 +284,9 @@ pub fn BrowserContext(comptime CDP_T: type) type { // RELATION TO SESSION_ID session: *CDP_T.Session, + // Points to the session arena + arena: Allocator, + // Maps to our Page. (There are other types of targets, but we only // deal with "pages" for now). Since we only allow 1 open page at a // time, we only have 1 target_id. @@ -316,6 +319,7 @@ pub fn BrowserContext(comptime CDP_T: type) type { var registry = Node.Registry.init(allocator); errdefer registry.deinit(); + const session = try cdp.browser.newSession(self); self.* = .{ .id = id, .cdp = cdp, @@ -324,7 +328,8 @@ pub fn BrowserContext(comptime CDP_T: type) type { .security_origin = URL_BASE, .secure_context_type = "Secure", // TODO = enum .loader_id = LOADER_ID, - .session = try cdp.browser.newSession(self), + .session = session, + .arena = session.arena.allocator(), .page_life_cycle_events = false, // TODO; Target based value .node_registry = registry, .node_search_list = undefined, @@ -365,7 +370,7 @@ pub fn BrowserContext(comptime CDP_T: type) type { executor.context.exit(); // The default context should remain open self.isolated_world = .{ - .name = try self.session.arena.allocator().dupe(u8, world_name), // TODO allocator + .name = try self.arena.dupe(u8, world_name), .grant_universal_access = grant_universal_access, .executor = executor, }; @@ -468,6 +473,7 @@ pub fn BrowserContext(comptime CDP_T: type) type { }; } +/// see: https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/bindings/core/v8/V8BindingDesign.md#world /// The current understanding. An isolated world lives in the same isolate, but a separated context. /// Clients create this to be able to create variables and run code without interfering with the /// normal namespace and values of the webpage. Similar to the main context we need to pretend to recreate it after diff --git a/src/cdp/domains/page.zig b/src/cdp/domains/page.zig index 4a76e55a..d32b9bee 100644 --- a/src/cdp/domains/page.zig +++ b/src/cdp/domains/page.zig @@ -117,8 +117,8 @@ fn createIsolatedWorld(cmd: anytype) !void { // Create the auxdata json for the contextCreated event // Calling contextCreated will assign a Id to the context and send the contextCreated event - const aux_json = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{params.frameId}); - bc.session.inspector.contextCreated(world.executor, world.name, "", aux_json, false); + const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{params.frameId}); + bc.session.inspector.contextCreated(world.executor, world.name, "", aux_data, false); return cmd.sendResult(.{ .executionContextId = world.executor.context.debugContextId() }, .{}); } @@ -213,16 +213,14 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void }, .{ .session_id = session_id }); } - // Send Runtime.executionContextsCleared event - // TODO: noop event, we have no env context at this point, is it necesarry? // When we actually recreated the context we should have the inspector send this event, see: resetContextGroup // Sending this event will tell the client that the context ids they had are invalid and the context shouls be dropped // 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| { - // TODO change the allocator - const aux_json = try std.fmt.allocPrint(bc.session.arena.allocator(), "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{bc.target_id.?}); + var buffer: [256]u8 = undefined; + const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{bc.target_id.?}); // Calling contextCreated will assign a new Id to the context and send the contextCreated event bc.session.inspector.contextCreated(