Skip to content

Commit

Permalink
Merge pull request #346 from lightpanda-io/target-created
Browse files Browse the repository at this point in the history
cdp: add TargetCreated event on createTarget message
  • Loading branch information
krichprollsch authored Jan 8, 2025
2 parents 84614e9 + d78e8a7 commit 9fb51a1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/cdp/cdp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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),
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/cdp/msg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
68 changes: 68 additions & 0 deletions src/cdp/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const Methods = enum {
disposeBrowserContext,
createTarget,
closeTarget,
sendMessageToTarget,
};

pub fn target(
Expand All @@ -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),
};
}

Expand Down Expand Up @@ -95,6 +97,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,
Expand Down Expand Up @@ -328,6 +343,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,
Expand Down Expand Up @@ -412,3 +440,43 @@ fn closeTarget(

return "";
}

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 {
message: []const u8,
sessionId: []const u8,
};
try cdp.sendEvent(
alloc,
ctx,
"Target.receivedMessageFromTarget",
ReceivedMessageFromTarget,
.{
.message = res,
.sessionId = input.params.sessionId,
},
null,
);

return "";
}

0 comments on commit 9fb51a1

Please sign in to comment.