From 5fc763a738313a4523d22561fd7f41e121f0acf1 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 7 Jan 2025 10:34:47 +0100 Subject: [PATCH 1/3] cdp: add TargetCreated event on createTarget message --- src/cdp/target.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 5f63acdf..71d9cd39 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -95,6 +95,19 @@ const AttachToTarget = struct { waitingForDebugger: bool = false, }; +const TargetCreated = struct { + sessionId: []const u8, + targetInfo: struct { + targetId: []const u8, + type: []const u8 = "page", + title: []const u8, + url: []const u8, + attached: bool = true, + canAccessOpener: bool = false, + browserContextId: []const u8, + }, +}; + const TargetFilter = struct { type: ?[]const u8 = null, exclude: ?bool = null, @@ -328,6 +341,19 @@ fn createTarget( ctx.state.secureContextType = "InsecureScheme"; ctx.state.loaderID = LoaderID; + // send targetCreated event + const created = TargetCreated{ + .sessionId = cdp.ContextSessionID, + .targetInfo = .{ + .targetId = ctx.state.frameID, + .title = "about:blank", + .url = ctx.state.url, + .browserContextId = input.params.browserContextId orelse ContextID, + .attached = true, + }, + }; + try cdp.sendEvent(alloc, ctx, "Target.targetCreated", TargetCreated, created, input.sessionId); + // send attachToTarget event const attached = AttachToTarget{ .sessionId = cdp.ContextSessionID, From 90ba6deba2527cf34c12f25c59b5ba74cf353fd0 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 7 Jan 2025 16:00:44 +0100 Subject: [PATCH 2/3] cdp: add Target.sendMessageToTarget support see https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-sendMessageToTarget --- src/cdp/cdp.zig | 29 +++++++++++++++++++---------- src/cdp/msg.zig | 3 ++- src/cdp/target.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 387434aa..1c1f25b5 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -31,6 +31,7 @@ const emulation = @import("emulation.zig").emulation; const fetch = @import("fetch.zig").fetch; const performance = @import("performance.zig").performance; const IncomingMessage = @import("msg.zig").IncomingMessage; +const Input = @import("msg.zig").Input; const log_cdp = std.log.scoped(.cdp); @@ -69,12 +70,20 @@ pub fn do( alloc: std.mem.Allocator, s: []const u8, ctx: *Ctx, -) ![]const u8 { +) anyerror![]const u8 { // incoming message parser var msg = IncomingMessage.init(alloc, s); defer msg.deinit(); + return dispatch(alloc, &msg, ctx); +} + +pub fn dispatch( + alloc: std.mem.Allocator, + msg: *IncomingMessage, + ctx: *Ctx, +) anyerror![]const u8 { const method = try msg.getMethod(); // retrieve domain from method @@ -85,15 +94,15 @@ pub fn do( // select corresponding domain const action = iter.next() orelse return error.BadMethod; return switch (domain) { - .Browser => browser(alloc, &msg, action, ctx), - .Target => target(alloc, &msg, action, ctx), - .Page => page(alloc, &msg, action, ctx), - .Log => log(alloc, &msg, action, ctx), - .Runtime => runtime(alloc, &msg, action, ctx), - .Network => network(alloc, &msg, action, ctx), - .Emulation => emulation(alloc, &msg, action, ctx), - .Fetch => fetch(alloc, &msg, action, ctx), - .Performance => performance(alloc, &msg, action, ctx), + .Browser => browser(alloc, msg, action, ctx), + .Target => target(alloc, msg, action, ctx), + .Page => page(alloc, msg, action, ctx), + .Log => log(alloc, msg, action, ctx), + .Runtime => runtime(alloc, msg, action, ctx), + .Network => network(alloc, msg, action, ctx), + .Emulation => emulation(alloc, msg, action, ctx), + .Fetch => fetch(alloc, msg, action, ctx), + .Performance => performance(alloc, msg, action, ctx), }; } diff --git a/src/cdp/msg.zig b/src/cdp/msg.zig index 988ba7cf..fdc364c5 100644 --- a/src/cdp/msg.zig +++ b/src/cdp/msg.zig @@ -130,7 +130,8 @@ pub const IncomingMessage = struct { // asking for getParams, we don't know how to parse them. fn scanParams(self: *IncomingMessage) !void { const tt = try self.scanner.peekNextTokenType(); - if (tt != .object_begin) return error.InvalidParams; + // accept object begin or null JSON value. + if (tt != .object_begin and tt != .null) return error.InvalidParams; try self.scanner.skipValue(); self.params_skip = true; } diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 71d9cd39..bb9d5395 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -38,6 +38,7 @@ const Methods = enum { disposeBrowserContext, createTarget, closeTarget, + sendMessageToTarget, }; pub fn target( @@ -58,6 +59,7 @@ pub fn target( .disposeBrowserContext => disposeBrowserContext(alloc, msg, ctx), .createTarget => createTarget(alloc, msg, ctx), .closeTarget => closeTarget(alloc, msg, ctx), + .sendMessageToTarget => sendMessageToTarget(alloc, msg, ctx), }; } @@ -438,3 +440,44 @@ fn closeTarget( return ""; } + +// noop +fn sendMessageToTarget( + alloc: std.mem.Allocator, + msg: *IncomingMessage, + ctx: *Ctx, +) ![]const u8 { + // input + const Params = struct { + message: []const u8, + sessionId: []const u8, + }; + const input = try Input(Params).get(alloc, msg); + defer input.deinit(); + log.debug("Req > id {d}, method {s}", .{ input.id, "target.sendMessageToTarget" }); + + // get the wrapped message. + var wmsg = IncomingMessage.init(alloc, input.params.message); + defer wmsg.deinit(); + + const res = try cdp.dispatch(alloc, &wmsg, ctx); + + // receivedMessageFromTarget event + const ReceivedMessageFromTarget = struct { + sessionId: []const u8, + message: []const u8, + }; + try cdp.sendEvent( + alloc, + ctx, + "Target.receivedMessageFromTarget", + ReceivedMessageFromTarget, + .{ + .sessionId = input.params.sessionId, + .message = res, + }, + input.params.sessionId, + ); + + return ""; +} From d78e8a725dcc93430540f21d7cc42012b5daee7f Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Tue, 7 Jan 2025 16:41:18 +0100 Subject: [PATCH 3/3] cdp: remove useless parameter --- src/cdp/target.zig | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cdp/target.zig b/src/cdp/target.zig index bb9d5395..c4b17365 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -441,7 +441,6 @@ fn closeTarget( return ""; } -// noop fn sendMessageToTarget( alloc: std.mem.Allocator, msg: *IncomingMessage, @@ -464,8 +463,8 @@ fn sendMessageToTarget( // receivedMessageFromTarget event const ReceivedMessageFromTarget = struct { - sessionId: []const u8, message: []const u8, + sessionId: []const u8, }; try cdp.sendEvent( alloc, @@ -473,10 +472,10 @@ fn sendMessageToTarget( "Target.receivedMessageFromTarget", ReceivedMessageFromTarget, .{ - .sessionId = input.params.sessionId, .message = res, + .sessionId = input.params.sessionId, }, - input.params.sessionId, + null, ); return "";