cdp: ensure there is an ID on each request

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-10-15 17:28:18 +02:00
parent 7750956c7b
commit 84c49fbe34
10 changed files with 100 additions and 94 deletions

View File

@@ -57,13 +57,13 @@ const JsVersion = "12.4.254.8";
fn getVersion(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
// ouput
const Res = struct {
@@ -73,13 +73,13 @@ fn getVersion(
userAgent: []const u8 = UserAgent,
jsVersion: []const u8 = JsVersion,
};
return result(alloc, id orelse msg.id.?, Res, .{}, null);
return result(alloc, msg.id, Res, .{}, null);
}
// TODO: noop method
fn setDownloadBehavior(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
@@ -91,10 +91,10 @@ fn setDownloadBehavior(
downloadPath: ?[]const u8 = null,
eventsEnabled: ?bool = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
return result(alloc, id orelse msg.id.?, null, null, null);
return result(alloc, msg.id, null, null, null);
}
// TODO: hard coded ID
@@ -102,7 +102,7 @@ const DevToolsWindowID = 1923710101;
fn getWindowForTarget(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
@@ -111,7 +111,7 @@ fn getWindowForTarget(
const Params = struct {
targetId: ?[]const u8 = null,
};
const msg = try cdp.getMsg(alloc, ?Params, scanner);
const msg = try cdp.getMsg(alloc, _id, ?Params, scanner);
std.debug.assert(msg.sessionID != null);
// output
@@ -125,20 +125,20 @@ fn getWindowForTarget(
windowState: []const u8 = "normal",
} = .{},
};
return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID.?);
return result(alloc, msg.id, Resp, Resp{}, msg.sessionID.?);
}
// TODO: noop method
fn setWindowBounds(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try cdp.getMsg(alloc, void, scanner);
const msg = try cdp.getMsg(alloc, _id, void, scanner);
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -35,6 +35,7 @@ pub const Error = error{
UnknonwDomain,
UnknownMethod,
NoResponse,
RequestWithoutID,
};
pub fn isCdpError(err: anyerror) ?Error {
@@ -276,10 +277,11 @@ fn getSessionId(scanner: *std.json.Scanner, key: []const u8) !?[]const u8 {
pub fn getMsg(
alloc: std.mem.Allocator,
_id: ?u16,
comptime params_T: type,
scanner: *std.json.Scanner,
) !struct { id: ?u16, params: ?params_T, sessionID: ?[]const u8 } {
var id: ?u16 = null;
) !struct { id: u16, params: ?params_T, sessionID: ?[]const u8 } {
var id_msg: ?u16 = null;
var params: ?params_T = null;
var sessionID: ?[]const u8 = null;
@@ -291,9 +293,9 @@ pub fn getMsg(
if (t != .string) {
return error.WrongTokenType;
}
if (id == null) {
id = try getId(scanner, t.string);
if (id != null) continue;
if (_id == null and id_msg == null) {
id_msg = try getId(scanner, t.string);
if (id_msg != null) continue;
}
if (params == null) {
params = try getParams(alloc, params_T, scanner, t.string);
@@ -311,7 +313,11 @@ pub fn getMsg(
);
t = try scanner.next();
if (t != .end_of_document) return error.CDPMsgEnd;
return .{ .id = id, .params = params, .sessionID = sessionID };
// check id
if (_id == null and id_msg == null) return error.RequestWithoutID;
return .{ .id = _id orelse id_msg.?, .params = params, .sessionID = sessionID };
}
// Common

View File

@@ -57,7 +57,7 @@ const MediaFeature = struct {
// TODO: noop method
fn setEmulatedMedia(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
@@ -67,16 +67,16 @@ fn setEmulatedMedia(
media: ?[]const u8 = null,
features: ?[]MediaFeature = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
// TODO: noop method
fn setFocusEmulationEnabled(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
@@ -85,35 +85,35 @@ fn setFocusEmulationEnabled(
const Params = struct {
enabled: bool,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
// TODO: noop method
fn setDeviceMetricsOverride(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try cdp.getMsg(alloc, void, scanner);
const msg = try cdp.getMsg(alloc, _id, void, scanner);
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
// TODO: noop method
fn setTouchEmulationEnabled(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try cdp.getMsg(alloc, void, scanner);
const msg = try cdp.getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -46,11 +46,11 @@ pub fn fetch(
// TODO: noop method
fn disable(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -46,11 +46,11 @@ pub fn log(
fn enable(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -47,23 +47,23 @@ pub fn network(
fn enable(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
// TODO: noop method
fn setCacheDisabled(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -58,12 +58,12 @@ pub fn page(
fn enable(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, msg.id, null, null, msg.sessionID);
}
const Frame = struct {
@@ -83,11 +83,11 @@ const Frame = struct {
fn getFrameTree(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
const msg = try cdp.getMsg(alloc, void, scanner);
const msg = try cdp.getMsg(alloc, _id, void, scanner);
const FrameTree = struct {
frameTree: struct {
@@ -106,12 +106,12 @@ fn getFrameTree(
},
},
};
return result(alloc, id orelse msg.id.?, FrameTree, frameTree, msg.sessionID);
return result(alloc, msg.id, FrameTree, frameTree, msg.sessionID);
}
fn setLifecycleEventsEnabled(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -120,12 +120,12 @@ fn setLifecycleEventsEnabled(
const Params = struct {
enabled: bool,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
ctx.state.page_life_cycle_events = true;
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
const LifecycleEvent = struct {
@@ -138,7 +138,7 @@ const LifecycleEvent = struct {
// TODO: hard coded method
fn addScriptToEvaluateOnNewDocument(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
@@ -150,19 +150,19 @@ fn addScriptToEvaluateOnNewDocument(
includeCommandLineAPI: bool = false,
runImmediately: bool = false,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
const Res = struct {
identifier: []const u8 = "1",
};
return result(alloc, id orelse msg.id.?, Res, Res{}, msg.sessionID);
return result(alloc, msg.id, Res, Res{}, msg.sessionID);
}
// TODO: hard coded method
fn createIsolatedWorld(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -173,7 +173,7 @@ fn createIsolatedWorld(
worldName: []const u8,
grantUniveralAccess: bool,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
std.debug.assert(msg.sessionID != null);
const params = msg.params.?;
@@ -199,12 +199,12 @@ fn createIsolatedWorld(
executionContextId: u8 = 0,
};
return result(alloc, id orelse msg.id.?, Resp, .{}, msg.sessionID);
return result(alloc, msg.id, Resp, .{}, msg.sessionID);
}
fn navigate(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -217,7 +217,7 @@ fn navigate(
frameId: ?[]const u8 = null,
referrerPolicy: ?[]const u8 = null, // TODO: enum
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
std.debug.assert(msg.sessionID != null);
const params = msg.params.?;
@@ -269,7 +269,7 @@ fn navigate(
.frameId = ctx.state.frameID,
.loaderId = ctx.state.loaderID,
};
const res = try result(alloc, id orelse msg.id.?, Resp, resp, msg.sessionID);
const res = try result(alloc, msg.id, Resp, resp, msg.sessionID);
defer alloc.free(res);
std.log.debug("res {s}", .{res});
try server.sendSync(ctx, res);

View File

@@ -45,11 +45,11 @@ pub fn performance(
fn enable(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -79,10 +79,10 @@ fn sendInspector(
userGesture: ?bool = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
const params = msg.params.?;
script = params.expression;
id = _id orelse msg.id.?;
id = msg.id;
} else if (method == .callFunctionOn) {
const Params = struct {
functionDeclaration: []const u8,
@@ -97,10 +97,10 @@ fn sendInspector(
userGesture: ?bool = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
const params = msg.params.?;
script = params.functionDeclaration;
id = _id orelse msg.id.?;
id = msg.id;
}
if (script) |src| {
@@ -162,11 +162,11 @@ pub fn executionContextCreated(
// should we be passing this also to the JS Inspector?
fn runIfWaitingForDebugger(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}

View File

@@ -65,16 +65,16 @@ const BrowserContextID = "65618675CB7D3585A95049E9DFE95EA9";
// TODO: noop method
fn setDiscoverTargets(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
const AttachToTarget = struct {
@@ -99,7 +99,7 @@ const TargetFilter = struct {
// TODO: noop method
fn setAutoAttach(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -111,7 +111,7 @@ fn setAutoAttach(
flatten: bool = true,
filter: ?[]TargetFilter = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
std.log.debug("params {any}", .{msg.params});
// attachedToTarget event
@@ -129,12 +129,12 @@ fn setAutoAttach(
}
// output
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
return result(alloc, msg.id, null, null, msg.sessionID);
}
fn getTargetInfo(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
_: *Ctx,
) ![]const u8 {
@@ -143,7 +143,7 @@ fn getTargetInfo(
const Params = struct {
targetId: ?[]const u8 = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
const TargetInfo = struct {
@@ -162,7 +162,7 @@ fn getTargetInfo(
.targetId = BrowserTargetID,
.type = "browser",
};
return result(alloc, id orelse msg.id.?, TargetInfo, targetInfo, null);
return result(alloc, msg.id, TargetInfo, targetInfo, null);
}
// Browser context are not handled and not in the roadmap for now
@@ -171,13 +171,13 @@ fn getTargetInfo(
// TODO: noop method
fn getBrowserContexts(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
// input
const msg = try getMsg(alloc, void, scanner);
const msg = try getMsg(alloc, _id, void, scanner);
// ouptut
const Resp = struct {
@@ -191,7 +191,7 @@ fn getBrowserContexts(
const contextIDs = [0][]const u8{};
resp = .{ .browserContextIds = &contextIDs };
}
return result(alloc, id orelse msg.id.?, Resp, resp, null);
return result(alloc, msg.id, Resp, resp, null);
}
const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89";
@@ -199,7 +199,7 @@ const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89";
// TODO: noop method
fn createBrowserContext(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -211,7 +211,7 @@ fn createBrowserContext(
proxyBypassList: ?[]const u8 = null,
originsWithUniversalNetworkAccess: ?[][]const u8 = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
ctx.state.contextID = ContextID;
@@ -219,12 +219,12 @@ fn createBrowserContext(
const Resp = struct {
browserContextId: []const u8 = ContextID,
};
return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID);
return result(alloc, msg.id, Resp, Resp{}, msg.sessionID);
}
fn disposeBrowserContext(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -233,10 +233,10 @@ fn disposeBrowserContext(
const Params = struct {
browserContextId: []const u8,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
const res = try result(alloc, id orelse msg.id.?, null, .{}, null);
const res = try result(alloc, msg.id, null, .{}, null);
defer alloc.free(res);
try server.sendSync(ctx, res);
@@ -249,7 +249,7 @@ const LoaderID = "DD4A76F842AA389647D702B4D805F49A";
fn createTarget(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -265,7 +265,7 @@ fn createTarget(
background: bool = false,
forTab: ?bool = null,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// change CDP state
ctx.state.frameID = TargetID;
@@ -291,12 +291,12 @@ fn createTarget(
const Resp = struct {
targetId: []const u8 = TargetID,
};
return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID);
return result(alloc, msg.id, Resp, Resp{}, msg.sessionID);
}
fn closeTarget(
alloc: std.mem.Allocator,
id: ?u16,
_id: ?u16,
scanner: *std.json.Scanner,
ctx: *Ctx,
) ![]const u8 {
@@ -305,13 +305,13 @@ fn closeTarget(
const Params = struct {
targetId: []const u8,
};
const msg = try getMsg(alloc, Params, scanner);
const msg = try getMsg(alloc, _id, Params, scanner);
// output
const Resp = struct {
success: bool = true,
};
const res = try result(alloc, id orelse msg.id.?, Resp, Resp{}, null);
const res = try result(alloc, msg.id, Resp, Resp{}, null);
defer alloc.free(res);
try server.sendSync(ctx, res);