mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
cdp: create a cdp state for target_auto_attach
This commit is contained in:
@@ -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 = .{},
|
||||
|
||||
@@ -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.? } }, .{});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user