Skip to content

Commit f89efd8

Browse files
Merge pull request #481 from lightpanda-io/auto-attach
cdp: implement target.setAutoAttach and target.detachFromTarget
2 parents 85caa09 + 7607ab2 commit f89efd8

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

src/cdp/cdp.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ pub fn CDPT(comptime TypeProvider: type) type {
5454
// The active browser
5555
browser: Browser,
5656

57+
// when true, any target creation must be attached.
58+
target_auto_attach: bool = false,
59+
5760
target_id_gen: TargetIdGen = .{},
5861
session_id_gen: SessionIdGen = .{},
5962
browser_context_id_gen: BrowserContextIdGen = .{},

src/cdp/target.zig

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ fn createTarget(cmd: anytype) !void {
150150
},
151151
}, .{});
152152

153-
// only if setAutoAttach is true?
154-
try doAttachtoTarget(cmd, target_id);
153+
// attach to the target only if auto attach is set.
154+
if (cmd.cdp.target_auto_attach) {
155+
try doAttachtoTarget(cmd, target_id);
156+
}
157+
155158
bc.target_id = target_id;
156159

157160
try cmd.sendResult(.{
@@ -302,8 +305,18 @@ fn sendMessageToTarget(cmd: anytype) !void {
302305
}, .{});
303306
}
304307

305-
// noop
306308
fn detachFromTarget(cmd: anytype) !void {
309+
// TODO check if sessionId/targetId match.
310+
// const params = (try cmd.params(struct {
311+
// sessionId: ?[]const u8,
312+
// targetId: ?[]const u8,
313+
// })) orelse return error.InvalidParams;
314+
315+
if (cmd.browser_context) |bc| {
316+
bc.session_id = null;
317+
// TODO should we send a Target.detachedFromTarget event?
318+
}
319+
307320
return cmd.sendResult(null, .{});
308321
}
309322

@@ -313,17 +326,28 @@ fn setDiscoverTargets(cmd: anytype) !void {
313326
}
314327

315328
fn setAutoAttach(cmd: anytype) !void {
316-
// const params = (try cmd.params(struct {
317-
// autoAttach: bool,
318-
// waitForDebuggerOnStart: bool,
319-
// flatten: bool = true,
320-
// filter: ?[]TargetFilter = null,
321-
// })) orelse return error.InvalidParams;
329+
const params = (try cmd.params(struct {
330+
autoAttach: bool,
331+
waitForDebuggerOnStart: bool,
332+
flatten: bool = true,
333+
// filter: ?[]TargetFilter = null,
334+
})) orelse return error.InvalidParams;
322335

323-
// TODO: should set a flag to send Target.attachedToTarget events
336+
// set a flag to send Target.attachedToTarget events
337+
cmd.cdp.target_auto_attach = params.autoAttach;
324338

325339
try cmd.sendResult(null, .{});
326340

341+
if (cmd.cdp.target_auto_attach == false) {
342+
// detach from all currently attached targets.
343+
if (cmd.browser_context) |bc| {
344+
bc.session_id = null;
345+
// TODO should we send a Target.detachedFromTarget event?
346+
}
347+
return;
348+
}
349+
350+
// autoAttach is set to true, we must attach to all existing targets.
327351
if (cmd.browser_context) |bc| {
328352
if (bc.target_id == null) {
329353
// hasn't attached yet
@@ -468,6 +492,18 @@ test "cdp.target: createTarget" {
468492
defer ctx.deinit();
469493
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
470494

495+
// should create a browser context
496+
const bc = ctx.cdp().browser_context.?;
497+
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.? } }, .{});
498+
}
499+
500+
{
501+
var ctx = testing.context();
502+
defer ctx.deinit();
503+
// active auto attach to get the Target.attachedToTarget event.
504+
try ctx.processMessage(.{ .id = 9, .method = "Target.setAutoAttach", .params = .{ .autoAttach = true, .waitForDebuggerOnStart = false } });
505+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .url = "about/blank" } });
506+
471507
// should create a browser context
472508
const bc = ctx.cdp().browser_context.?;
473509
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 +527,6 @@ test "cdp.target: createTarget" {
491527

492528
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 });
493529
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.? } }, .{});
494-
495-
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.? } }, .{});
496530
}
497531
}
498532

@@ -608,3 +642,39 @@ test "cdp.target: getTargetInfo" {
608642
}, .{ .id = 11 });
609643
}
610644
}
645+
646+
test "cdp.target: issue#474: attach to just created target" {
647+
var ctx = testing.context();
648+
defer ctx.deinit();
649+
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
650+
{
651+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-9" } });
652+
try testing.expectEqual(true, bc.target_id != null);
653+
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 });
654+
655+
try ctx.processMessage(.{ .id = 11, .method = "Target.attachToTarget", .params = .{ .targetId = bc.target_id.? } });
656+
const session_id = bc.session_id.?;
657+
try ctx.expectSentResult(.{ .sessionId = session_id }, .{ .id = 11 });
658+
}
659+
}
660+
661+
test "cdp.target: detachFromTarget" {
662+
var ctx = testing.context();
663+
defer ctx.deinit();
664+
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
665+
{
666+
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-9" } });
667+
try testing.expectEqual(true, bc.target_id != null);
668+
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 });
669+
670+
try ctx.processMessage(.{ .id = 11, .method = "Target.attachToTarget", .params = .{ .targetId = bc.target_id.? } });
671+
try ctx.expectSentResult(.{ .sessionId = bc.session_id.? }, .{ .id = 11 });
672+
673+
try ctx.processMessage(.{ .id = 12, .method = "Target.detachFromTarget", .params = .{ .targetId = bc.target_id.? } });
674+
try testing.expectEqual(null, bc.session_id);
675+
try ctx.expectSentResult(null, .{ .id = 12 });
676+
677+
try ctx.processMessage(.{ .id = 13, .method = "Target.attachToTarget", .params = .{ .targetId = bc.target_id.? } });
678+
try ctx.expectSentResult(.{ .sessionId = bc.session_id.? }, .{ .id = 13 });
679+
}
680+
}

0 commit comments

Comments
 (0)