Skip to content

Commit 76a9034

Browse files
server: newSession on disposeBrowserContext
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 4c225e5 commit 76a9034

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/browser/browser.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ pub const Browser = struct {
6464
self.session.deinit();
6565
}
6666

67+
pub fn newSession(self: *Browser, alloc: std.mem.Allocator, loop: *jsruntime.Loop) !void {
68+
self.session.deinit();
69+
self.session = try Session.init(alloc, loop, "about:blank");
70+
}
71+
6772
pub fn currentSession(self: *Browser) *Session {
6873
return self.session;
6974
}

src/cdp/target.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn disposeBrowserContext(
226226
alloc: std.mem.Allocator,
227227
id: ?u16,
228228
scanner: *std.json.Scanner,
229-
_: *Ctx,
229+
ctx: *Ctx,
230230
) ![]const u8 {
231231

232232
// input
@@ -236,7 +236,11 @@ fn disposeBrowserContext(
236236
const msg = try getMsg(alloc, Params, scanner);
237237

238238
// output
239-
return result(alloc, id orelse msg.id.?, null, {}, null);
239+
const res = try result(alloc, id orelse msg.id.?, null, .{}, null);
240+
defer alloc.free(res);
241+
try server.sendSync(ctx, res);
242+
243+
return error.DisposeBrowserContext;
240244
}
241245

242246
// TODO: hard coded IDs

src/server.zig

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ pub const Cmd = struct {
8282
}
8383

8484
// read and execute input
85-
self.msg_buf.read(self.alloc(), input, self, Cmd.do) catch unreachable;
85+
self.msg_buf.read(self.alloc(), input, self, Cmd.do) catch |err| {
86+
std.log.err("do error: {any}", .{err});
87+
return;
88+
};
8689

8790
// continue receving incomming messages asynchronously
8891
self.loop.io.recv(*Cmd, self, cbk, completion, self.socket, self.buf);
@@ -99,7 +102,13 @@ pub const Cmd = struct {
99102
}
100103

101104
fn do(self: *Cmd, cmd: []const u8) anyerror!void {
102-
const res = try cdp.do(self.alloc(), cmd, self);
105+
const res = cdp.do(self.alloc(), cmd, self) catch |err| {
106+
if (err == error.DisposeBrowserContext) {
107+
try self.newSession();
108+
return;
109+
}
110+
return err;
111+
};
103112

104113
// send result
105114
if (!std.mem.eql(u8, res, "")) {
@@ -108,6 +117,13 @@ pub const Cmd = struct {
108117
}
109118
}
110119

120+
fn newSession(self: *Cmd) !void {
121+
std.log.info("new session", .{});
122+
try self.browser.newSession(self.alloc(), self.loop);
123+
const cmd_opaque = @as(*anyopaque, @ptrCast(self));
124+
try self.browser.currentSession().setInspector(cmd_opaque, Cmd.onInspectorResp, Cmd.onInspectorNotif);
125+
}
126+
111127
// Inspector
112128

113129
pub fn sendInspector(self: *Cmd, msg: []const u8) void {

0 commit comments

Comments
 (0)