Skip to content

Commit 27f9963

Browse files
Merge pull request #391 from lightpanda-io/cdp-ctx-sessionid
cdp: use an enum for SessionID
2 parents a4e3f03 + 6d53069 commit 27f9963

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/cdp/cdp.zig

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn dispatch(
121121
pub const State = struct {
122122
executionContextId: u32 = 0,
123123
contextID: ?[]const u8 = null,
124-
sessionID: ?[]const u8 = null,
124+
sessionID: SessionID = .CONTEXTSESSIONID0497A05C95417CF4,
125125
frameID: []const u8 = FrameID,
126126
url: []const u8 = URLBase,
127127
securityOrigin: []const u8 = URLBase,
@@ -225,8 +225,21 @@ pub fn sendEvent(
225225
// ------
226226

227227
// TODO: hard coded IDs
228-
pub const BrowserSessionID = "BROWSERSESSIONID597D9875C664CAC0";
229-
pub const ContextSessionID = "CONTEXTSESSIONID0497A05C95417CF4";
228+
pub const SessionID = enum {
229+
BROWSERSESSIONID597D9875C664CAC0,
230+
CONTEXTSESSIONID0497A05C95417CF4,
231+
232+
pub fn parse(str: []const u8) !SessionID {
233+
inline for (@typeInfo(SessionID).Enum.fields) |enumField| {
234+
if (std.mem.eql(u8, str, enumField.name)) {
235+
return @field(SessionID, enumField.name);
236+
}
237+
}
238+
return error.InvalidSessionID;
239+
}
240+
};
241+
pub const BrowserSessionID = @tagName(SessionID.BROWSERSESSIONID597D9875C664CAC0);
242+
pub const ContextSessionID = @tagName(SessionID.CONTEXTSESSIONID0497A05C95417CF4);
230243
pub const URLBase = "chrome://newtab/";
231244
pub const LoaderID = "LOADERID24DD2FD56CF1EF33C965C79C";
232245
pub const FrameID = "FRAMEIDD8AED408A0467AC93100BCDBE";

src/cdp/runtime.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ fn sendInspector(
117117
}
118118
}
119119

120-
ctx.state.sessionID = msg.sessionId;
120+
if (msg.sessionId) |s| {
121+
ctx.state.sessionID = cdp.SessionID.parse(s) catch |err| {
122+
log.err("parse sessionID: {s} {any}", .{ s, err });
123+
return err;
124+
};
125+
}
121126

122127
// remove awaitPromise true params
123128
// TODO: delete when Promise are correctly handled by zig-js-runtime

src/cdp/target.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,13 @@ fn createTarget(
344344
ctx.state.securityOrigin = "://";
345345
ctx.state.secureContextType = "InsecureScheme";
346346
ctx.state.loaderID = LoaderID;
347-
ctx.state.sessionID = msg.sessionId;
347+
348+
if (msg.sessionId) |s| {
349+
ctx.state.sessionID = cdp.SessionID.parse(s) catch |err| {
350+
log.err("parse sessionID: {s} {any}", .{ s, err });
351+
return err;
352+
};
353+
}
348354

349355
// TODO stop the previous page instead?
350356
if (ctx.browser.session.page != null) return error.pageAlreadyExists;

src/server.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ pub const Ctx = struct {
348348
const s = try std.fmt.allocPrint(
349349
allocator,
350350
tpl,
351-
.{ msg_open, ctx.state.sessionID orelse cdp.ContextSessionID },
351+
.{ msg_open, @tagName(ctx.state.sessionID) },
352352
);
353353

354354
try ctx.send(s);

0 commit comments

Comments
 (0)