From fe7f6bee1cfb1f03282de763a4a20f7ecb40a72e Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 19 Mar 2025 18:37:39 +0100 Subject: [PATCH] cdp: create a cdp state for target_auto_attach --- src/cdp/cdp.zig | 3 +++ src/cdp/target.zig | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 0fbf6177..d7975664 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -54,6 +54,9 @@ pub fn CDPT(comptime TypeProvider: type) type { // The active browser browser: Browser, + // when true, any target creation must be attached. + target_auto_attach: bool = false, + target_id_gen: TargetIdGen = .{}, session_id_gen: SessionIdGen = .{}, browser_context_id_gen: BrowserContextIdGen = .{}, diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 70b4c4fc..5469f7db 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -150,8 +150,11 @@ fn createTarget(cmd: anytype) !void { }, }, .{}); - // only if setAutoAttach is true? - try doAttachtoTarget(cmd, target_id); + // attach to the target only if auto attach is set. + if (cmd.cdp.target_auto_attach) { + try doAttachtoTarget(cmd, target_id); + } + bc.target_id = target_id; try cmd.sendResult(.{ @@ -313,17 +316,24 @@ fn setDiscoverTargets(cmd: anytype) !void { } fn setAutoAttach(cmd: anytype) !void { - // const params = (try cmd.params(struct { - // autoAttach: bool, - // waitForDebuggerOnStart: bool, - // flatten: bool = true, - // filter: ?[]TargetFilter = null, - // })) orelse return error.InvalidParams; + const params = (try cmd.params(struct { + autoAttach: bool, + waitForDebuggerOnStart: bool, + flatten: bool = true, + // filter: ?[]TargetFilter = null, + })) orelse return error.InvalidParams; - // TODO: should set a flag to send Target.attachedToTarget events + // set a flag to send Target.attachedToTarget events + cmd.cdp.target_auto_attach = params.autoAttach; try cmd.sendResult(null, .{}); + if (cmd.cdp.target_auto_attach == false) { + // TODO: detach from all currently attached targets. + return; + } + + // autoAttach is set to true, we must attach to all existing targets. if (cmd.browser_context) |bc| { if (bc.target_id == null) { // hasn't attached yet @@ -468,6 +478,18 @@ test "cdp.target: createTarget" { defer ctx.deinit(); try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } }); + // should create a browser context + const bc = ctx.cdp().browser_context.?; + try ctx.expectSentEvent("Target.targetCreated", .{ .targetInfo = .{ .url = "about:blank", .title = "about:blank", .attached = false, .type = "page", .canAccessOpener = false, .browserContextId = bc.id, .targetId = bc.target_id.? } }, .{}); + } + + { + var ctx = testing.context(); + defer ctx.deinit(); + // active auto attach to get the Target.attachedToTarget event. + try ctx.processMessage(.{ .id = 9, .method = "Target.setAutoAttach", .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false } }); + try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } }); + // should create a browser context const bc = ctx.cdp().browser_context.?; try ctx.expectSentEvent("Target.targetCreated", .{ .targetInfo = .{ .url = "about:blank", .title = "about:blank", .attached = false, .type = "page", .canAccessOpener = false, .browserContextId = bc.id, .targetId = bc.target_id.? } }, .{}); @@ -491,8 +513,6 @@ test "cdp.target: createTarget" { try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 }); try ctx.expectSentEvent("Target.targetCreated", .{ .targetInfo = .{ .url = "about:blank", .title = "about:blank", .attached = false, .type = "page", .canAccessOpener = false, .browserContextId = "BID-9", .targetId = bc.target_id.? } }, .{}); - - try ctx.expectSentEvent("Target.attachedToTarget", .{ .sessionId = bc.session_id.?, .targetInfo = .{ .url = "chrome://newtab/", .title = "about:blank", .attached = true, .type = "page", .canAccessOpener = false, .browserContextId = "BID-9", .targetId = bc.target_id.? } }, .{}); } }