Skip to content

Commit b31d111

Browse files
committed
Error on null page/scope
1 parent b537b21 commit b31d111

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/cdp/cdp.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
377377

378378
self.isolated_world = .{
379379
.name = try self.arena.dupe(u8, world_name),
380-
.scope = undefined,
380+
.scope = null,
381381
.executor = executor,
382382
.grant_universal_access = grant_universal_access,
383383
};
@@ -519,7 +519,8 @@ const IsolatedWorld = struct {
519519
self.executor.deinit();
520520
self.scope = null;
521521
}
522-
pub fn removeContext(self: *IsolatedWorld) void {
522+
pub fn removeContext(self: *IsolatedWorld) !void {
523+
if (self.scope == null) return error.NoIsolatedContextToRemove;
523524
self.executor.endScope();
524525
self.scope = null;
525526
}
@@ -530,6 +531,7 @@ const IsolatedWorld = struct {
530531
// This also means this pointer becomes invalid after removePage untill a new page is created.
531532
// Currently we have only 1 page/frame and thus also only 1 state in the isolate world.
532533
pub fn createContext(self: *IsolatedWorld, page: *Page) !void {
534+
if (self.scope != null) return error.Only1IsolatedContextSupported;
533535
self.scope = try self.executor.startScope(&page.window, &page.state, {}, false);
534536
}
535537
};

src/cdp/domains/page.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn createIsolatedWorld(cmd: anytype) !void {
114114
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
115115

116116
const world = try bc.createIsolatedWorld(params.worldName, params.grantUniveralAccess);
117-
const page = bc.session.currentPage().?;
117+
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
118118
try pageCreated(bc, page);
119119
const scope = world.scope.?;
120120

@@ -147,7 +147,7 @@ fn navigate(cmd: anytype) !void {
147147

148148
const url = try URL.parse(params.url, "https");
149149

150-
var page = bc.session.currentPage().?;
150+
var page = bc.session.currentPage() orelse return error.PageNotLoaded;
151151
bc.loader_id = bc.cdp.loader_id_gen.next();
152152
try cmd.sendResult(.{
153153
.frameId = target_id,
@@ -223,7 +223,7 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
223223
var buffer: [512]u8 = undefined;
224224
{
225225
var fba = std.heap.FixedBufferAllocator.init(&buffer);
226-
const page = bc.session.currentPage().?;
226+
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
227227
const aux_data = try std.fmt.allocPrint(fba.allocator(), "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
228228
bc.inspector.contextCreated(
229229
page.scope,
@@ -249,7 +249,7 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
249249
pub fn pageRemove(bc: anytype) !void {
250250
// The main page is going to be removed, we need to remove contexts from other worlds first.
251251
if (bc.isolated_world) |*isolated_world| {
252-
isolated_world.removeContext();
252+
try isolated_world.removeContext();
253253
}
254254
}
255255

0 commit comments

Comments
 (0)