Skip to content

Commit

Permalink
Merge pull request #391 from lightpanda-io/cdp-ctx-sessionid
Browse files Browse the repository at this point in the history
cdp: use an enum for SessionID
  • Loading branch information
krichprollsch authored Jan 30, 2025
2 parents a4e3f03 + 6d53069 commit 27f9963
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/cdp/cdp.zig
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn dispatch(
pub const State = struct {
executionContextId: u32 = 0,
contextID: ?[]const u8 = null,
sessionID: ?[]const u8 = null,
sessionID: SessionID = .CONTEXTSESSIONID0497A05C95417CF4,
frameID: []const u8 = FrameID,
url: []const u8 = URLBase,
securityOrigin: []const u8 = URLBase,
Expand Down Expand Up @@ -225,8 +225,21 @@ pub fn sendEvent(
// ------

// TODO: hard coded IDs
pub const BrowserSessionID = "BROWSERSESSIONID597D9875C664CAC0";
pub const ContextSessionID = "CONTEXTSESSIONID0497A05C95417CF4";
pub const SessionID = enum {
BROWSERSESSIONID597D9875C664CAC0,
CONTEXTSESSIONID0497A05C95417CF4,

pub fn parse(str: []const u8) !SessionID {
inline for (@typeInfo(SessionID).Enum.fields) |enumField| {
if (std.mem.eql(u8, str, enumField.name)) {
return @field(SessionID, enumField.name);
}
}
return error.InvalidSessionID;
}
};
pub const BrowserSessionID = @tagName(SessionID.BROWSERSESSIONID597D9875C664CAC0);
pub const ContextSessionID = @tagName(SessionID.CONTEXTSESSIONID0497A05C95417CF4);
pub const URLBase = "chrome://newtab/";
pub const LoaderID = "LOADERID24DD2FD56CF1EF33C965C79C";
pub const FrameID = "FRAMEIDD8AED408A0467AC93100BCDBE";
Expand Down
7 changes: 6 additions & 1 deletion src/cdp/runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ fn sendInspector(
}
}

ctx.state.sessionID = msg.sessionId;
if (msg.sessionId) |s| {
ctx.state.sessionID = cdp.SessionID.parse(s) catch |err| {
log.err("parse sessionID: {s} {any}", .{ s, err });
return err;
};
}

// remove awaitPromise true params
// TODO: delete when Promise are correctly handled by zig-js-runtime
Expand Down
8 changes: 7 additions & 1 deletion src/cdp/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,13 @@ fn createTarget(
ctx.state.securityOrigin = "://";
ctx.state.secureContextType = "InsecureScheme";
ctx.state.loaderID = LoaderID;
ctx.state.sessionID = msg.sessionId;

if (msg.sessionId) |s| {
ctx.state.sessionID = cdp.SessionID.parse(s) catch |err| {
log.err("parse sessionID: {s} {any}", .{ s, err });
return err;
};
}

// TODO stop the previous page instead?
if (ctx.browser.session.page != null) return error.pageAlreadyExists;
Expand Down
2 changes: 1 addition & 1 deletion src/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub const Ctx = struct {
const s = try std.fmt.allocPrint(
allocator,
tpl,
.{ msg_open, ctx.state.sessionID orelse cdp.ContextSessionID },
.{ msg_open, @tagName(ctx.state.sessionID) },
);

try ctx.send(s);
Expand Down

0 comments on commit 27f9963

Please sign in to comment.