Skip to content

Commit 85bdca1

Browse files
committed
isolated world
1 parent a698ff8 commit 85bdca1

File tree

5 files changed

+86
-28
lines changed

5 files changed

+86
-28
lines changed

src/browser/browser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ pub const Session = struct {
309309

310310
fn contextCreated(self: *Session, page: *Page) void {
311311
log.debug("inspector context created", .{});
312-
self.inspector.contextCreated(self.executor, "", (page.origin() catch "://") orelse "://", self.aux_data);
312+
self.inspector.contextCreated(self.executor, "", (page.origin() catch "://") orelse "://", aux_data, true);
313313
}
314314

315315
fn notify(self: *const Session, notification: *const Notification) void {

src/cdp/cdp.zig

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,26 @@ pub fn BrowserContext(comptime CDP_T: type) type {
306306
node_registry: Node.Registry,
307307
node_search_list: Node.Search.List,
308308

309+
isolated_world: ?IsolatedWorld,
310+
311+
pub fn createIsolatedWorld(
312+
self: *Self,
313+
world_name: []const u8,
314+
grant_universal_access: bool,
315+
) !void {
316+
if (self.isolated_world != null) return error.AlreadyExists;
317+
318+
const executor = try self.cdp.browser.env.startExecutor(@import("../browser/html/window.zig").Window, &self.session.state, self.session);
319+
errdefer self.cdp.browser.env.stopExecutor(executor);
320+
executor.context.exit();
321+
322+
self.isolated_world = .{
323+
.name = try self.session.arena.allocator().dupe(u8, world_name), // TODO allocator
324+
.grant_universal_access = grant_universal_access,
325+
.executor = executor,
326+
};
327+
}
328+
309329
const Self = @This();
310330

311331
fn init(self: *Self, id: []const u8, cdp: *CDP_T) !void {
@@ -326,6 +346,7 @@ pub fn BrowserContext(comptime CDP_T: type) type {
326346
.page_life_cycle_events = false, // TODO; Target based value
327347
.node_registry = registry,
328348
.node_search_list = undefined,
349+
.isolated_world = null,
329350
};
330351
self.node_search_list = Node.Search.List.init(allocator, &self.node_registry);
331352
}
@@ -437,6 +458,20 @@ pub fn BrowserContext(comptime CDP_T: type) type {
437458
};
438459
}
439460

461+
/// The current understanding. An isolated world lives in the same isolate, but a separated context.
462+
/// Clients creates this to be able to create variables and run code without interfering
463+
/// with the normal namespace and values of the webpage. Similar to the main context we need to pretend to recreate it after
464+
/// a executionContextsCleared event which happens when navigating to a new page. A client can have a command be executed
465+
/// in the isolated world by using its Context ID or the worldName.
466+
/// grantUniveralAccess Indecated whether the isolated world has access to objects like the DOM or other JS Objects.
467+
/// Generally the client needs to resolve a node into the isolated world to be able to work with it.
468+
/// An object id is unique across all contexts, different object ids can refer to the same Node in different contexts.
469+
pub const IsolatedWorld = struct {
470+
name: []const u8,
471+
grant_universal_access: bool,
472+
executor: *@import("../browser/env.zig").Env.Executor,
473+
};
474+
440475
// This is a generic because when we send a result we have two different
441476
// behaviors. Normally, we're sending the result to the client. But in some cases
442477
// we want to capture the result. So we want the command.sendResult to be

src/cdp/domains/dom.zig

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,18 @@ fn resolveNode(cmd: anytype) !void {
127127
objectGroup: ?[]const u8 = null,
128128
executionContextId: ?u32 = null,
129129
})) orelse return error.InvalidParams;
130-
if (params.nodeId == null or params.backendNodeId != null or params.executionContextId != null) {
131-
return error.NotYetImplementedParams;
132-
}
133-
134130
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
135-
const node = bc.node_registry.lookup_by_id.get(params.nodeId.?) orelse return error.UnknownNode;
131+
132+
var executor = bc.session.executor;
133+
if (params.executionContextId) |context_id| {
134+
if (executor.context.debugContextId() != context_id) {
135+
const isolated_world = bc.isolated_world orelse return error.ContextNotFound;
136+
executor = isolated_world.executor;
137+
if (executor.context.debugContextId() != context_id) return error.ContextNotFound;
138+
}
139+
}
140+
const input_node_id = if (params.nodeId) |node_id| node_id else params.backendNodeId orelse return error.InvalidParams;
141+
const node = bc.node_registry.lookup_by_id.get(input_node_id) orelse return error.UnknownNode;
136142

137143
// node._node is a *parser.Node we need this to be able to find its most derived type e.g. Node -> Element -> HTMLElement
138144
// So we use the Node.Union when retrieve the value from the environment

src/cdp/domains/page.zig

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,36 +97,27 @@ fn addScriptToEvaluateOnNewDocument(cmd: anytype) !void {
9797
}, .{});
9898
}
9999

100-
// TODO: hard coded method
101100
fn createIsolatedWorld(cmd: anytype) !void {
102-
_ = cmd.browser_context orelse return error.BrowserContextNotLoaded;
103-
104-
const session_id = cmd.input.session_id orelse return error.SessionIdRequired;
105-
106101
const params = (try cmd.params(struct {
107102
frameId: []const u8,
108103
worldName: []const u8,
109104
grantUniveralAccess: bool,
110105
})) orelse return error.InvalidParams;
106+
if (!params.grantUniveralAccess) {
107+
std.debug.print("grantUniveralAccess == false is not yet implemented", .{});
108+
// When grantUniveralAccess == false and the client attempts to resolve
109+
// or otherwise access a DOM or other JS Object from another context that should fail.
110+
}
111+
const bc = cmd.browser_context orelse return error.BrowserContextNotLoaded;
111112

112-
// noop executionContextCreated event
113-
try cmd.sendEvent("Runtime.executionContextCreated", .{
114-
.context = runtime.ExecutionContextCreated{
115-
.id = 0,
116-
.origin = "",
117-
.name = params.worldName,
118-
// TODO: hard coded ID
119-
.uniqueId = "7102379147004877974.3265385113993241162",
120-
.auxData = .{
121-
.isDefault = false,
122-
.type = "isolated",
123-
.frameId = params.frameId,
124-
},
125-
},
126-
}, .{ .session_id = session_id });
113+
try bc.createIsolatedWorld(params.worldName, params.grantUniveralAccess); // orelse return error.IsolatedWorldAlreadyExists;
114+
115+
// Create the auxdata json from
116+
const aux_json = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}", .{params.frameId});
117+
bc.session.inspector.contextCreated(bc.isolated_world.?.executor, bc.isolated_world.?.name, "", aux_json, false);
127118

128119
return cmd.sendResult(.{
129-
.executionContextId = 0,
120+
.executionContextId = bc.isolated_world.?.executor.context.debugContextId(),
130121
}, .{});
131122
}
132123

@@ -222,7 +213,24 @@ pub fn pageNavigate(bc: anytype, event: *const Notification.PageNavigate) !void
222213

223214
// Send Runtime.executionContextsCleared event
224215
// TODO: noop event, we have no env context at this point, is it necesarry?
216+
// When we actually recreated the context we should have the inspector send this event, see: resetContextGroup
225217
try cdp.sendEvent("Runtime.executionContextsCleared", null, .{ .session_id = session_id });
218+
219+
if (bc.isolated_world != null) {
220+
const aux_json = try std.fmt.allocPrint(
221+
bc.session.arena.allocator(), // TODO change this
222+
"{{\"isDefault\":false,\"type\":\"isolated\",\"frameId\":\"{s}\"}}",
223+
.{bc.target_id.?}, // TODO check this
224+
);
225+
226+
bc.session.inspector.contextCreated(
227+
bc.isolated_world.?.executor,
228+
bc.isolated_world.?.name,
229+
"://",
230+
aux_json,
231+
false,
232+
);
233+
}
226234
}
227235

228236
pub fn pageNavigated(bc: anytype, event: *const Notification.PageNavigated) !void {

src/runtime/js.zig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,14 +1331,23 @@ pub fn Env(comptime S: type, comptime types: anytype) type {
13311331
self.session.dispatchProtocolMessage(self.isolate, msg);
13321332
}
13331333

1334+
// From CDP docs
1335+
// https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExecutionContextDescription
1336+
// ----
1337+
// - name: Human readable name describing given context.
1338+
// - origin: Execution context origin (ie. URL who initialised the request)
1339+
// - auxData: Embedder-specific auxiliary data likely matching
1340+
// {isDefault: boolean, type: 'default'|'isolated'|'worker', frameId: string}
1341+
// - is_default_context: Whether the execution context is default, should match the auxData
13341342
pub fn contextCreated(
13351343
self: *const Inspector,
13361344
executor: *const Executor,
13371345
name: []const u8,
13381346
origin: []const u8,
13391347
aux_data: ?[]const u8,
1348+
is_default_context: bool,
13401349
) void {
1341-
self.inner.contextCreated(executor.context, name, origin, aux_data);
1350+
self.inner.contextCreated(executor.context, name, origin, aux_data, is_default_context);
13421351
}
13431352

13441353
// Retrieves the RemoteObject for a given value.

0 commit comments

Comments
 (0)