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 ""; +}