Add sendEvent utility function

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-04-19 17:11:31 +02:00
parent ed38705efd
commit 9e13ffb8ff
4 changed files with 67 additions and 23 deletions

View File

@@ -129,14 +129,14 @@ pub fn result(
return stringify(alloc, resp);
}
// caller owns the slice returned
pub fn method(
pub fn sendEvent(
alloc: std.mem.Allocator,
ctx: *Ctx,
name: []const u8,
comptime T: type,
params: T,
sessionID: ?[]const u8,
) ![]const u8 {
) !void {
const Resp = struct {
method: []const u8,
params: T,
@@ -144,7 +144,9 @@ pub fn method(
};
const resp = Resp{ .method = name, .params = params, .sessionId = sessionID };
return stringify(alloc, resp);
const event_msg = try stringify(alloc, resp);
std.log.debug("event {s}", .{event_msg});
try server.sendSync(ctx, event_msg);
}
pub fn getParams(

View File

@@ -168,6 +168,8 @@ fn navigate(
ctx.state.url = content.params.url;
ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2";
var page = try ctx.browser.currentSession().createPage();
// output
const Resp = struct {
frameId: []const u8,

View File

@@ -31,12 +31,68 @@ fn enable(
alloc: std.mem.Allocator,
id: u64,
scanner: *std.json.Scanner,
_: *Ctx,
ctx: *Ctx,
) ![]const u8 {
_ = ctx;
// input
const sessionID = try cdp.getSessionID(scanner);
// output
// const uniqueID = "1367118932354479079.-1471398151593995849";
// const mainCtx = try executionContextCreated(
// alloc,
// 1,
// cdp.URLBase,
// "",
// uniqueID,
// .{},
// sessionID,
// );
// std.log.debug("res {s}", .{mainCtx});
// try server.sendAsync(ctx, mainCtx);
return result(alloc, id, null, null, sessionID);
}
const AuxData = struct {
isDefault: bool = true,
type: []const u8 = "default",
frameId: []const u8 = cdp.FrameID,
};
const ExecutionContextDescription = struct {
id: u64,
origin: []const u8,
name: []const u8,
uniqueId: []const u8,
auxData: ?AuxData = null,
};
fn executionContextCreated(
alloc: std.mem.Allocator,
id: u64,
origin: []const u8,
name: []const u8,
uniqueID: []const u8,
auxData: ?AuxData,
sessionID: ?[]const u8,
) ![]const u8 {
const Params = struct {
context: ExecutionContextDescription,
};
const params = Params{
.context = .{
.id = id,
.origin = origin,
.name = name,
.uniqueId = uniqueID,
.auxData = auxData,
},
};
return try cdp.method(alloc, "Runtime.executionContextCreated", Params, params, sessionID);
}
fn runIfWaitingForDebugger(
alloc: std.mem.Allocator,
id: u64,

View File

@@ -81,15 +81,7 @@ fn tagetSetAutoAttach(
.browserContextId = BrowserContextID,
},
};
const event = try cdp.method(
alloc,
"Target.attachedToTarget",
AttachToTarget,
attached,
null,
);
std.log.debug("event {s}", .{event});
try server.sendSync(ctx, event);
try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, null);
}
return result(alloc, id, null, null, sessionID);
@@ -196,15 +188,7 @@ fn createTarget(
},
.waitingForDebugger = true,
};
const event = try cdp.method(
alloc,
"Target.attachedToTarget",
AttachToTarget,
attached,
sessionID,
);
std.log.debug("event {s}", .{event});
try server.sendSync(ctx, event);
try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, sessionID);
// output
const Resp = struct {