Skip to content

Commit 9fb51a1

Browse files
Merge pull request #346 from lightpanda-io/target-created
cdp: add TargetCreated event on createTarget message
2 parents 84614e9 + d78e8a7 commit 9fb51a1

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-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: 68 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

@@ -95,6 +97,19 @@ const AttachToTarget = struct {
9597
waitingForDebugger: bool = false,
9698
};
9799

100+
const TargetCreated = struct {
101+
sessionId: []const u8,
102+
targetInfo: struct {
103+
targetId: []const u8,
104+
type: []const u8 = "page",
105+
title: []const u8,
106+
url: []const u8,
107+
attached: bool = true,
108+
canAccessOpener: bool = false,
109+
browserContextId: []const u8,
110+
},
111+
};
112+
98113
const TargetFilter = struct {
99114
type: ?[]const u8 = null,
100115
exclude: ?bool = null,
@@ -328,6 +343,19 @@ fn createTarget(
328343
ctx.state.secureContextType = "InsecureScheme";
329344
ctx.state.loaderID = LoaderID;
330345

346+
// send targetCreated event
347+
const created = TargetCreated{
348+
.sessionId = cdp.ContextSessionID,
349+
.targetInfo = .{
350+
.targetId = ctx.state.frameID,
351+
.title = "about:blank",
352+
.url = ctx.state.url,
353+
.browserContextId = input.params.browserContextId orelse ContextID,
354+
.attached = true,
355+
},
356+
};
357+
try cdp.sendEvent(alloc, ctx, "Target.targetCreated", TargetCreated, created, input.sessionId);
358+
331359
// send attachToTarget event
332360
const attached = AttachToTarget{
333361
.sessionId = cdp.ContextSessionID,
@@ -412,3 +440,43 @@ fn closeTarget(
412440

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

0 commit comments

Comments
 (0)