mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
Merge pull request #346 from lightpanda-io/target-created
cdp: add TargetCreated event on createTarget message
This commit is contained in:
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
@@ -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 "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user