Skip to content

Commit 84c49fb

Browse files
cdp: ensure there is an ID on each request
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 7750956 commit 84c49fb

File tree

10 files changed

+100
-94
lines changed

10 files changed

+100
-94
lines changed

src/cdp/browser.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ const JsVersion = "12.4.254.8";
5757

5858
fn getVersion(
5959
alloc: std.mem.Allocator,
60-
id: ?u16,
60+
_id: ?u16,
6161
scanner: *std.json.Scanner,
6262
_: *Ctx,
6363
) ![]const u8 {
6464

6565
// input
66-
const msg = try getMsg(alloc, void, scanner);
66+
const msg = try getMsg(alloc, _id, void, scanner);
6767

6868
// ouput
6969
const Res = struct {
@@ -73,13 +73,13 @@ fn getVersion(
7373
userAgent: []const u8 = UserAgent,
7474
jsVersion: []const u8 = JsVersion,
7575
};
76-
return result(alloc, id orelse msg.id.?, Res, .{}, null);
76+
return result(alloc, msg.id, Res, .{}, null);
7777
}
7878

7979
// TODO: noop method
8080
fn setDownloadBehavior(
8181
alloc: std.mem.Allocator,
82-
id: ?u16,
82+
_id: ?u16,
8383
scanner: *std.json.Scanner,
8484
_: *Ctx,
8585
) ![]const u8 {
@@ -91,18 +91,18 @@ fn setDownloadBehavior(
9191
downloadPath: ?[]const u8 = null,
9292
eventsEnabled: ?bool = null,
9393
};
94-
const msg = try getMsg(alloc, Params, scanner);
94+
const msg = try getMsg(alloc, _id, Params, scanner);
9595

9696
// output
97-
return result(alloc, id orelse msg.id.?, null, null, null);
97+
return result(alloc, msg.id, null, null, null);
9898
}
9999

100100
// TODO: hard coded ID
101101
const DevToolsWindowID = 1923710101;
102102

103103
fn getWindowForTarget(
104104
alloc: std.mem.Allocator,
105-
id: ?u16,
105+
_id: ?u16,
106106
scanner: *std.json.Scanner,
107107
_: *Ctx,
108108
) ![]const u8 {
@@ -111,7 +111,7 @@ fn getWindowForTarget(
111111
const Params = struct {
112112
targetId: ?[]const u8 = null,
113113
};
114-
const msg = try cdp.getMsg(alloc, ?Params, scanner);
114+
const msg = try cdp.getMsg(alloc, _id, ?Params, scanner);
115115
std.debug.assert(msg.sessionID != null);
116116

117117
// output
@@ -125,20 +125,20 @@ fn getWindowForTarget(
125125
windowState: []const u8 = "normal",
126126
} = .{},
127127
};
128-
return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID.?);
128+
return result(alloc, msg.id, Resp, Resp{}, msg.sessionID.?);
129129
}
130130

131131
// TODO: noop method
132132
fn setWindowBounds(
133133
alloc: std.mem.Allocator,
134-
id: ?u16,
134+
_id: ?u16,
135135
scanner: *std.json.Scanner,
136136
_: *Ctx,
137137
) ![]const u8 {
138138

139139
// input
140-
const msg = try cdp.getMsg(alloc, void, scanner);
140+
const msg = try cdp.getMsg(alloc, _id, void, scanner);
141141

142142
// output
143-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
143+
return result(alloc, msg.id, null, null, msg.sessionID);
144144
}

src/cdp/cdp.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub const Error = error{
3535
UnknonwDomain,
3636
UnknownMethod,
3737
NoResponse,
38+
RequestWithoutID,
3839
};
3940

4041
pub fn isCdpError(err: anyerror) ?Error {
@@ -276,10 +277,11 @@ fn getSessionId(scanner: *std.json.Scanner, key: []const u8) !?[]const u8 {
276277

277278
pub fn getMsg(
278279
alloc: std.mem.Allocator,
280+
_id: ?u16,
279281
comptime params_T: type,
280282
scanner: *std.json.Scanner,
281-
) !struct { id: ?u16, params: ?params_T, sessionID: ?[]const u8 } {
282-
var id: ?u16 = null;
283+
) !struct { id: u16, params: ?params_T, sessionID: ?[]const u8 } {
284+
var id_msg: ?u16 = null;
283285
var params: ?params_T = null;
284286
var sessionID: ?[]const u8 = null;
285287

@@ -291,9 +293,9 @@ pub fn getMsg(
291293
if (t != .string) {
292294
return error.WrongTokenType;
293295
}
294-
if (id == null) {
295-
id = try getId(scanner, t.string);
296-
if (id != null) continue;
296+
if (_id == null and id_msg == null) {
297+
id_msg = try getId(scanner, t.string);
298+
if (id_msg != null) continue;
297299
}
298300
if (params == null) {
299301
params = try getParams(alloc, params_T, scanner, t.string);
@@ -311,7 +313,11 @@ pub fn getMsg(
311313
);
312314
t = try scanner.next();
313315
if (t != .end_of_document) return error.CDPMsgEnd;
314-
return .{ .id = id, .params = params, .sessionID = sessionID };
316+
317+
// check id
318+
if (_id == null and id_msg == null) return error.RequestWithoutID;
319+
320+
return .{ .id = _id orelse id_msg.?, .params = params, .sessionID = sessionID };
315321
}
316322

317323
// Common

src/cdp/emulation.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const MediaFeature = struct {
5757
// TODO: noop method
5858
fn setEmulatedMedia(
5959
alloc: std.mem.Allocator,
60-
id: ?u16,
60+
_id: ?u16,
6161
scanner: *std.json.Scanner,
6262
_: *Ctx,
6363
) ![]const u8 {
@@ -67,16 +67,16 @@ fn setEmulatedMedia(
6767
media: ?[]const u8 = null,
6868
features: ?[]MediaFeature = null,
6969
};
70-
const msg = try getMsg(alloc, Params, scanner);
70+
const msg = try getMsg(alloc, _id, Params, scanner);
7171

7272
// output
73-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
73+
return result(alloc, msg.id, null, null, msg.sessionID);
7474
}
7575

7676
// TODO: noop method
7777
fn setFocusEmulationEnabled(
7878
alloc: std.mem.Allocator,
79-
id: ?u16,
79+
_id: ?u16,
8080
scanner: *std.json.Scanner,
8181
_: *Ctx,
8282
) ![]const u8 {
@@ -85,35 +85,35 @@ fn setFocusEmulationEnabled(
8585
const Params = struct {
8686
enabled: bool,
8787
};
88-
const msg = try getMsg(alloc, Params, scanner);
88+
const msg = try getMsg(alloc, _id, Params, scanner);
8989

9090
// output
91-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
91+
return result(alloc, msg.id, null, null, msg.sessionID);
9292
}
9393

9494
// TODO: noop method
9595
fn setDeviceMetricsOverride(
9696
alloc: std.mem.Allocator,
97-
id: ?u16,
97+
_id: ?u16,
9898
scanner: *std.json.Scanner,
9999
_: *Ctx,
100100
) ![]const u8 {
101101

102102
// input
103-
const msg = try cdp.getMsg(alloc, void, scanner);
103+
const msg = try cdp.getMsg(alloc, _id, void, scanner);
104104

105105
// output
106-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
106+
return result(alloc, msg.id, null, null, msg.sessionID);
107107
}
108108

109109
// TODO: noop method
110110
fn setTouchEmulationEnabled(
111111
alloc: std.mem.Allocator,
112-
id: ?u16,
112+
_id: ?u16,
113113
scanner: *std.json.Scanner,
114114
_: *Ctx,
115115
) ![]const u8 {
116-
const msg = try cdp.getMsg(alloc, void, scanner);
116+
const msg = try cdp.getMsg(alloc, _id, void, scanner);
117117

118-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
118+
return result(alloc, msg.id, null, null, msg.sessionID);
119119
}

src/cdp/fetch.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ pub fn fetch(
4646
// TODO: noop method
4747
fn disable(
4848
alloc: std.mem.Allocator,
49-
id: ?u16,
49+
_id: ?u16,
5050
scanner: *std.json.Scanner,
5151
_: *Ctx,
5252
) ![]const u8 {
53-
const msg = try getMsg(alloc, void, scanner);
53+
const msg = try getMsg(alloc, _id, void, scanner);
5454

55-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
55+
return result(alloc, msg.id, null, null, msg.sessionID);
5656
}

src/cdp/log.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ pub fn log(
4646

4747
fn enable(
4848
alloc: std.mem.Allocator,
49-
id: ?u16,
49+
_id: ?u16,
5050
scanner: *std.json.Scanner,
5151
_: *Ctx,
5252
) ![]const u8 {
53-
const msg = try getMsg(alloc, void, scanner);
53+
const msg = try getMsg(alloc, _id, void, scanner);
5454

55-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
55+
return result(alloc, msg.id, null, null, msg.sessionID);
5656
}

src/cdp/network.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ pub fn network(
4747

4848
fn enable(
4949
alloc: std.mem.Allocator,
50-
id: ?u16,
50+
_id: ?u16,
5151
scanner: *std.json.Scanner,
5252
_: *Ctx,
5353
) ![]const u8 {
54-
const msg = try getMsg(alloc, void, scanner);
54+
const msg = try getMsg(alloc, _id, void, scanner);
5555

56-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
56+
return result(alloc, msg.id, null, null, msg.sessionID);
5757
}
5858

5959
// TODO: noop method
6060
fn setCacheDisabled(
6161
alloc: std.mem.Allocator,
62-
id: ?u16,
62+
_id: ?u16,
6363
scanner: *std.json.Scanner,
6464
_: *Ctx,
6565
) ![]const u8 {
66-
const msg = try getMsg(alloc, void, scanner);
66+
const msg = try getMsg(alloc, _id, void, scanner);
6767

68-
return result(alloc, id orelse msg.id.?, null, null, msg.sessionID);
68+
return result(alloc, msg.id, null, null, msg.sessionID);
6969
}

0 commit comments

Comments
 (0)