mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
server: newSession on disposeBrowserContext
Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
@@ -64,6 +64,11 @@ pub const Browser = struct {
|
||||
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 {
|
||||
return self.session;
|
||||
}
|
||||
|
||||
@@ -226,7 +226,7 @@ fn disposeBrowserContext(
|
||||
alloc: std.mem.Allocator,
|
||||
id: ?u16,
|
||||
scanner: *std.json.Scanner,
|
||||
_: *Ctx,
|
||||
ctx: *Ctx,
|
||||
) ![]const u8 {
|
||||
|
||||
// input
|
||||
@@ -236,7 +236,11 @@ fn disposeBrowserContext(
|
||||
const msg = try getMsg(alloc, Params, scanner);
|
||||
|
||||
// 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
|
||||
|
||||
@@ -82,7 +82,10 @@ pub const Cmd = struct {
|
||||
}
|
||||
|
||||
// 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
|
||||
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 {
|
||||
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
|
||||
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
|
||||
|
||||
pub fn sendInspector(self: *Cmd, msg: []const u8) void {
|
||||
|
||||
Reference in New Issue
Block a user