server: newSession on disposeBrowserContext

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-10-07 21:14:55 +02:00
parent 4c225e515d
commit 76a9034668
3 changed files with 29 additions and 4 deletions

View File

@@ -64,6 +64,11 @@ pub const Browser = struct {
self.session.deinit(); self.session.deinit();
} }
pub fn newSession(self: *Browser, alloc: std.mem.Allocator, loop: *jsruntime.Loop) !void {
self.session.deinit();
self.session = try Session.init(alloc, loop, "about:blank");
}
pub fn currentSession(self: *Browser) *Session { pub fn currentSession(self: *Browser) *Session {
return self.session; return self.session;
} }

View File

@@ -226,7 +226,7 @@ fn disposeBrowserContext(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
id: ?u16, id: ?u16,
scanner: *std.json.Scanner, scanner: *std.json.Scanner,
_: *Ctx, ctx: *Ctx,
) ![]const u8 { ) ![]const u8 {
// input // input
@@ -236,7 +236,11 @@ fn disposeBrowserContext(
const msg = try getMsg(alloc, Params, scanner); const msg = try getMsg(alloc, Params, scanner);
// output // output
return result(alloc, id orelse msg.id.?, null, {}, null); const res = try result(alloc, id orelse msg.id.?, null, .{}, null);
defer alloc.free(res);
try server.sendSync(ctx, res);
return error.DisposeBrowserContext;
} }
// TODO: hard coded IDs // TODO: hard coded IDs

View File

@@ -82,7 +82,10 @@ pub const Cmd = struct {
} }
// read and execute input // read and execute input
self.msg_buf.read(self.alloc(), input, self, Cmd.do) catch unreachable; self.msg_buf.read(self.alloc(), input, self, Cmd.do) catch |err| {
std.log.err("do error: {any}", .{err});
return;
};
// continue receving incomming messages asynchronously // continue receving incomming messages asynchronously
self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf); self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
@@ -99,7 +102,13 @@ pub const Cmd = struct {
} }
fn do(self: *Cmd, cmd: []const u8) anyerror!void { fn do(self: *Cmd, cmd: []const u8) anyerror!void {
const res = try cdp.do(self.alloc(), cmd, self); const res = cdp.do(self.alloc(), cmd, self) catch |err| {
if (err == error.DisposeBrowserContext) {
try self.newSession();
return;
}
return err;
};
// send result // send result
if (!std.mem.eql(u8, res, "")) { if (!std.mem.eql(u8, res, "")) {
@@ -108,6 +117,13 @@ pub const Cmd = struct {
} }
} }
fn newSession(self: *Cmd) !void {
std.log.info("new session", .{});
try self.browser.newSession(self.alloc(), self.loop);
const cmd_opaque = @as(*anyopaque, @ptrCast(self));
try self.browser.currentSession().setInspector(cmd_opaque, Cmd.onInspectorResp, Cmd.onInspectorNotif);
}
// Inspector // Inspector
pub fn sendInspector(self: *Cmd, msg: []const u8) void { pub fn sendInspector(self: *Cmd, msg: []const u8) void {