mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Merge pull request #347 from lightpanda-io/send-message-to-target
cdp: add Target.sendMessageToTarget support
This commit is contained in:
@@ -31,6 +31,7 @@ const emulation = @import("emulation.zig").emulation;
|
|||||||
const fetch = @import("fetch.zig").fetch;
|
const fetch = @import("fetch.zig").fetch;
|
||||||
const performance = @import("performance.zig").performance;
|
const performance = @import("performance.zig").performance;
|
||||||
const IncomingMessage = @import("msg.zig").IncomingMessage;
|
const IncomingMessage = @import("msg.zig").IncomingMessage;
|
||||||
|
const Input = @import("msg.zig").Input;
|
||||||
|
|
||||||
const log_cdp = std.log.scoped(.cdp);
|
const log_cdp = std.log.scoped(.cdp);
|
||||||
|
|
||||||
@@ -69,12 +70,20 @@ pub fn do(
|
|||||||
alloc: std.mem.Allocator,
|
alloc: std.mem.Allocator,
|
||||||
s: []const u8,
|
s: []const u8,
|
||||||
ctx: *Ctx,
|
ctx: *Ctx,
|
||||||
) ![]const u8 {
|
) anyerror![]const u8 {
|
||||||
|
|
||||||
// incoming message parser
|
// incoming message parser
|
||||||
var msg = IncomingMessage.init(alloc, s);
|
var msg = IncomingMessage.init(alloc, s);
|
||||||
defer msg.deinit();
|
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();
|
const method = try msg.getMethod();
|
||||||
|
|
||||||
// retrieve domain from method
|
// retrieve domain from method
|
||||||
@@ -85,15 +94,15 @@ pub fn do(
|
|||||||
// select corresponding domain
|
// select corresponding domain
|
||||||
const action = iter.next() orelse return error.BadMethod;
|
const action = iter.next() orelse return error.BadMethod;
|
||||||
return switch (domain) {
|
return switch (domain) {
|
||||||
.Browser => browser(alloc, &msg, action, ctx),
|
.Browser => browser(alloc, msg, action, ctx),
|
||||||
.Target => target(alloc, &msg, action, ctx),
|
.Target => target(alloc, msg, action, ctx),
|
||||||
.Page => page(alloc, &msg, action, ctx),
|
.Page => page(alloc, msg, action, ctx),
|
||||||
.Log => log(alloc, &msg, action, ctx),
|
.Log => log(alloc, msg, action, ctx),
|
||||||
.Runtime => runtime(alloc, &msg, action, ctx),
|
.Runtime => runtime(alloc, msg, action, ctx),
|
||||||
.Network => network(alloc, &msg, action, ctx),
|
.Network => network(alloc, msg, action, ctx),
|
||||||
.Emulation => emulation(alloc, &msg, action, ctx),
|
.Emulation => emulation(alloc, msg, action, ctx),
|
||||||
.Fetch => fetch(alloc, &msg, action, ctx),
|
.Fetch => fetch(alloc, msg, action, ctx),
|
||||||
.Performance => performance(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.
|
// asking for getParams, we don't know how to parse them.
|
||||||
fn scanParams(self: *IncomingMessage) !void {
|
fn scanParams(self: *IncomingMessage) !void {
|
||||||
const tt = try self.scanner.peekNextTokenType();
|
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();
|
try self.scanner.skipValue();
|
||||||
self.params_skip = true;
|
self.params_skip = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ const Methods = enum {
|
|||||||
disposeBrowserContext,
|
disposeBrowserContext,
|
||||||
createTarget,
|
createTarget,
|
||||||
closeTarget,
|
closeTarget,
|
||||||
|
sendMessageToTarget,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn target(
|
pub fn target(
|
||||||
@@ -58,6 +59,7 @@ pub fn target(
|
|||||||
.disposeBrowserContext => disposeBrowserContext(alloc, msg, ctx),
|
.disposeBrowserContext => disposeBrowserContext(alloc, msg, ctx),
|
||||||
.createTarget => createTarget(alloc, msg, ctx),
|
.createTarget => createTarget(alloc, msg, ctx),
|
||||||
.closeTarget => closeTarget(alloc, msg, ctx),
|
.closeTarget => closeTarget(alloc, msg, ctx),
|
||||||
|
.sendMessageToTarget => sendMessageToTarget(alloc, msg, ctx),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -438,3 +440,44 @@ fn closeTarget(
|
|||||||
|
|
||||||
return "";
|
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 "";
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user