Skip to content

Commit 7829bdb

Browse files
Merge pull request #347 from lightpanda-io/send-message-to-target
cdp: add Target.sendMessageToTarget support
2 parents 5fc763a + 90ba6de commit 7829bdb

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

src/cdp/cdp.zig

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const emulation = @import("emulation.zig").emulation;
3131
const fetch = @import("fetch.zig").fetch;
3232
const performance = @import("performance.zig").performance;
3333
const IncomingMessage = @import("msg.zig").IncomingMessage;
34+
const Input = @import("msg.zig").Input;
3435

3536
const log_cdp = std.log.scoped(.cdp);
3637

@@ -69,12 +70,20 @@ pub fn do(
6970
alloc: std.mem.Allocator,
7071
s: []const u8,
7172
ctx: *Ctx,
72-
) ![]const u8 {
73+
) anyerror![]const u8 {
7374

7475
// incoming message parser
7576
var msg = IncomingMessage.init(alloc, s);
7677
defer msg.deinit();
7778

79+
return dispatch(alloc, &msg, ctx);
80+
}
81+
82+
pub fn dispatch(
83+
alloc: std.mem.Allocator,
84+
msg: *IncomingMessage,
85+
ctx: *Ctx,
86+
) anyerror![]const u8 {
7887
const method = try msg.getMethod();
7988

8089
// retrieve domain from method
@@ -85,15 +94,15 @@ pub fn do(
8594
// select corresponding domain
8695
const action = iter.next() orelse return error.BadMethod;
8796
return switch (domain) {
88-
.Browser => browser(alloc, &msg, action, ctx),
89-
.Target => target(alloc, &msg, action, ctx),
90-
.Page => page(alloc, &msg, action, ctx),
91-
.Log => log(alloc, &msg, action, ctx),
92-
.Runtime => runtime(alloc, &msg, action, ctx),
93-
.Network => network(alloc, &msg, action, ctx),
94-
.Emulation => emulation(alloc, &msg, action, ctx),
95-
.Fetch => fetch(alloc, &msg, action, ctx),
96-
.Performance => performance(alloc, &msg, action, ctx),
97+
.Browser => browser(alloc, msg, action, ctx),
98+
.Target => target(alloc, msg, action, ctx),
99+
.Page => page(alloc, msg, action, ctx),
100+
.Log => log(alloc, msg, action, ctx),
101+
.Runtime => runtime(alloc, msg, action, ctx),
102+
.Network => network(alloc, msg, action, ctx),
103+
.Emulation => emulation(alloc, msg, action, ctx),
104+
.Fetch => fetch(alloc, msg, action, ctx),
105+
.Performance => performance(alloc, msg, action, ctx),
97106
};
98107
}
99108

src/cdp/msg.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ pub const IncomingMessage = struct {
130130
// asking for getParams, we don't know how to parse them.
131131
fn scanParams(self: *IncomingMessage) !void {
132132
const tt = try self.scanner.peekNextTokenType();
133-
if (tt != .object_begin) return error.InvalidParams;
133+
// accept object begin or null JSON value.
134+
if (tt != .object_begin and tt != .null) return error.InvalidParams;
134135
try self.scanner.skipValue();
135136
self.params_skip = true;
136137
}

src/cdp/target.zig

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const Methods = enum {
3838
disposeBrowserContext,
3939
createTarget,
4040
closeTarget,
41+
sendMessageToTarget,
4142
};
4243

4344
pub fn target(
@@ -58,6 +59,7 @@ pub fn target(
5859
.disposeBrowserContext => disposeBrowserContext(alloc, msg, ctx),
5960
.createTarget => createTarget(alloc, msg, ctx),
6061
.closeTarget => closeTarget(alloc, msg, ctx),
62+
.sendMessageToTarget => sendMessageToTarget(alloc, msg, ctx),
6163
};
6264
}
6365

@@ -438,3 +440,44 @@ fn closeTarget(
438440

439441
return "";
440442
}
443+
444+
// noop
445+
fn sendMessageToTarget(
446+
alloc: std.mem.Allocator,
447+
msg: *IncomingMessage,
448+
ctx: *Ctx,
449+
) ![]const u8 {
450+
// input
451+
const Params = struct {
452+
message: []const u8,
453+
sessionId: []const u8,
454+
};
455+
const input = try Input(Params).get(alloc, msg);
456+
defer input.deinit();
457+
log.debug("Req > id {d}, method {s}", .{ input.id, "target.sendMessageToTarget" });
458+
459+
// get the wrapped message.
460+
var wmsg = IncomingMessage.init(alloc, input.params.message);
461+
defer wmsg.deinit();
462+
463+
const res = try cdp.dispatch(alloc, &wmsg, ctx);
464+
465+
// receivedMessageFromTarget event
466+
const ReceivedMessageFromTarget = struct {
467+
sessionId: []const u8,
468+
message: []const u8,
469+
};
470+
try cdp.sendEvent(
471+
alloc,
472+
ctx,
473+
"Target.receivedMessageFromTarget",
474+
ReceivedMessageFromTarget,
475+
.{
476+
.sessionId = input.params.sessionId,
477+
.message = res,
478+
},
479+
input.params.sessionId,
480+
);
481+
482+
return "";
483+
}

0 commit comments

Comments
 (0)