Merge pull request #592 from lightpanda-io/isolated-polyfill-+-create-when-needed

Isolated polyfill & create world when needed
This commit is contained in:
Karl Seguin
2025-05-05 15:03:05 +08:00
committed by GitHub
7 changed files with 86 additions and 38 deletions

View File

@@ -197,7 +197,7 @@ fn resolveNode(cmd: anytype) !void {
if (params.executionContextId) |context_id| {
if (scope.context.debugContextId() != context_id) {
const isolated_world = bc.isolated_world orelse return error.ContextNotFound;
scope = isolated_world.scope;
scope = isolated_world.scope orelse return error.ContextNotFound;
if (scope.context.debugContextId() != context_id) return error.ContextNotFound;
}

View File

@@ -20,6 +20,7 @@ const std = @import("std");
const runtime = @import("runtime.zig");
const URL = @import("../../url.zig").URL;
const Notification = @import("../../notification.zig").Notification;
const Page = @import("../../browser/browser.zig").Page;
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
@@ -112,15 +113,17 @@ fn createIsolatedWorld(cmd: anytype) !void {
}
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
const world = &bc.isolated_world.?;
world.name = try bc.arena.dupe(u8, params.worldName);
world.grant_universal_access = params.grantUniveralAccess;
const world = try bc.createIsolatedWorld(params.worldName, params.grantUniveralAccess);
const page = bc.session.currentPage() orelse return error.PageNotLoaded;
try pageCreated(bc, page);
const scope = world.scope.?;
// Create the auxdata json for the contextCreated event
// Calling contextCreated will assign a Id to the context and send the contextCreated event
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{params.frameId});
bc.inspector.contextCreated(world.scope, world.name, "", aux_data, false);
bc.inspector.contextCreated(scope, world.name, "", aux_data, false);
return cmd.sendResult(.{ .executionContextId = world.scope.context.debugContextId() }, .{});
return cmd.sendResult(.{ .executionContextId = scope.context.debugContextId() }, .{});
}
fn navigate(cmd: anytype) !void {
@@ -144,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,
@@ -220,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,
@@ -230,12 +233,11 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
true,
);
}
if (bc.isolated_world) |*isolated_world| {
const aux_json = try std.fmt.bufPrint(&buffer, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{target_id});
// Calling contextCreated will assign a new Id to the context and send the contextCreated event
bc.inspector.contextCreated(
isolated_world.scope,
isolated_world.scope.?,
isolated_world.name,
"://",
aux_json,
@@ -244,6 +246,23 @@ 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| {
try isolated_world.removeContext();
}
}
pub fn pageCreated(bc: anytype, page: *Page) !void {
if (bc.isolated_world) |*isolated_world| {
// We need to recreate the isolated world context
try isolated_world.createContext(page);
const polyfill = @import("../../browser/polyfill/polyfill.zig");
try polyfill.load(bc.arena, isolated_world.scope.?);
}
}
pub fn pageNavigated(bc: anytype, event: *const Notification.PageNavigated) !void {
// I don't think it's possible that we get these notifications and don't
// have these things setup.

View File

@@ -125,8 +125,6 @@ fn createTarget(cmd: anytype) !void {
bc.target_id = target_id;
var page = try bc.session.createPage();
try bc.createIsolatedWorld(page);
{
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
bc.inspector.contextCreated(