Error on null page/scope

This commit is contained in:
sjorsdonkers
2025-05-05 08:46:21 +02:00
parent f6f744aea1
commit 9f72c98967
2 changed files with 8 additions and 6 deletions

View File

@@ -377,7 +377,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
self.isolated_world = .{
.name = try self.arena.dupe(u8, world_name),
.scope = undefined,
.scope = null,
.executor = executor,
.grant_universal_access = grant_universal_access,
};
@@ -519,7 +519,8 @@ const IsolatedWorld = struct {
self.executor.deinit();
self.scope = null;
}
pub fn removeContext(self: *IsolatedWorld) void {
pub fn removeContext(self: *IsolatedWorld) !void {
if (self.scope == null) return error.NoIsolatedContextToRemove;
self.executor.endScope();
self.scope = null;
}
@@ -530,6 +531,7 @@ const IsolatedWorld = struct {
// This also means this pointer becomes invalid after removePage untill a new page is created.
// Currently we have only 1 page/frame and thus also only 1 state in the isolate world.
pub fn createContext(self: *IsolatedWorld, page: *Page) !void {
if (self.scope != null) return error.Only1IsolatedContextSupported;
self.scope = try self.executor.startScope(&page.window, &page.state, {}, false);
}
};

View File

@@ -114,7 +114,7 @@ fn createIsolatedWorld(cmd: anytype) !void {
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const world = try bc.createIsolatedWorld(params.worldName, params.grantUniveralAccess);
const page = bc.session.currentPage().?;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
try pageCreated(bc, page);
const scope = world.scope.?;
@@ -147,7 +147,7 @@ fn navigate(cmd: anytype) !void {
const url = try URL.parse(params.url, "https");
var page = bc.session.currentPage().?;
var page = bc.session.currentPage() orelse return error.PageNotLoaded;
bc.loader_id = bc.cdp.loader_id_gen.next();
try cmd.sendResult(.{
.frameId = target_id,
@@ -223,7 +223,7 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
var buffer: [512]u8 = undefined;
{
var fba = std.heap.FixedBufferAllocator.init(&buffer);
const page = bc.session.currentPage().?;
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
const aux_data = try std.fmt.allocPrint(fba.allocator(), "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
bc.inspector.contextCreated(
page.scope,
@@ -249,7 +249,7 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
pub fn pageRemove(bc: anytype) !void {
// The main page is going to be removed, we need to remove contexts from other worlds first.
if (bc.isolated_world) |*isolated_world| {
isolated_world.removeContext();
try isolated_world.removeContext();
}
}