Skip to content

Commit 94d2d28

Browse files
Redirect Runtime domain to JS engine Inspector
Signed-off-by: Francis Bouvier <[email protected]>
1 parent 14a3a66 commit 94d2d28

File tree

8 files changed

+195
-254
lines changed

8 files changed

+195
-254
lines changed

src/browser/browser.zig

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ const log = std.log.scoped(.browser);
5151
pub const Browser = struct {
5252
session: *Session,
5353

54-
pub fn init(alloc: std.mem.Allocator, vm: jsruntime.VM) !Browser {
54+
pub fn init(alloc: std.mem.Allocator) !Browser {
5555
// We want to ensure the caller initialised a VM, but the browser
5656
// doesn't use it directly...
57-
_ = vm;
5857

5958
return Browser{
6059
.session = try Session.init(alloc, "about:blank"),
@@ -91,6 +90,7 @@ pub const Session = struct {
9190
loader: Loader,
9291
env: Env = undefined,
9392
loop: Loop,
93+
inspector: ?jsruntime.Inspector = null,
9494
window: Window,
9595
// TODO move the shed to the browser?
9696
storageShed: storage.Shed,
@@ -122,6 +122,10 @@ pub const Session = struct {
122122
fn deinit(self: *Session) void {
123123
if (self.page) |page| page.end();
124124

125+
if (self.inspector) |inspector| {
126+
inspector.deinit(self.alloc);
127+
}
128+
125129
self.env.deinit();
126130
self.arena.deinit();
127131

@@ -132,9 +136,25 @@ pub const Session = struct {
132136
self.alloc.destroy(self);
133137
}
134138

139+
pub fn setInspector(
140+
self: *Session,
141+
ctx: *anyopaque,
142+
onResp: jsruntime.InspectorOnResponseFn,
143+
onEvent: jsruntime.InspectorOnEventFn,
144+
) !void {
145+
self.inspector = try jsruntime.Inspector.init(self.alloc, self.env, ctx, onResp, onEvent);
146+
self.env.setInspector(self.inspector.?);
147+
}
148+
135149
pub fn createPage(self: *Session) !Page {
136150
return Page.init(self.alloc, self);
137151
}
152+
153+
pub fn callInspector(self: *Session, msg: []const u8) void {
154+
if (self.inspector) |inspector| {
155+
inspector.send(msg, self.env);
156+
}
157+
}
138158
};
139159

140160
// Page navigates to an url.
@@ -219,7 +239,7 @@ pub const Page = struct {
219239
}
220240

221241
// spec reference: https://html.spec.whatwg.org/#document-lifecycle
222-
pub fn navigate(self: *Page, uri: []const u8) !void {
242+
pub fn navigate(self: *Page, uri: []const u8, auxData: ?[]const u8) !void {
223243
const alloc = self.arena.allocator();
224244

225245
log.debug("starting GET {s}", .{uri});
@@ -280,7 +300,7 @@ pub const Page = struct {
280300
log.debug("header content-type: {s}", .{ct.?});
281301
const mime = try Mime.parse(ct.?);
282302
if (mime.eql(Mime.HTML)) {
283-
try self.loadHTMLDoc(req.reader(), mime.charset orelse "utf-8");
303+
try self.loadHTMLDoc(req.reader(), mime.charset orelse "utf-8", auxData);
284304
} else {
285305
log.info("non-HTML document: {s}", .{ct.?});
286306

@@ -290,7 +310,7 @@ pub const Page = struct {
290310
}
291311

292312
// https://html.spec.whatwg.org/#read-html
293-
fn loadHTMLDoc(self: *Page, reader: anytype, charset: []const u8) !void {
313+
fn loadHTMLDoc(self: *Page, reader: anytype, charset: []const u8, auxData: ?[]const u8) !void {
294314
const alloc = self.arena.allocator();
295315

296316
// start netsurf memory arena.
@@ -327,6 +347,11 @@ pub const Page = struct {
327347
log.debug("start js env", .{});
328348
try self.session.env.start();
329349

350+
// inspector
351+
if (self.session.inspector) |inspector| {
352+
inspector.contextCreated(self.session.env, "", self.origin.?, auxData);
353+
}
354+
330355
// replace the user context document with the new one.
331356
try self.session.env.setUserContext(.{
332357
.document = html_doc,

src/cdp/cdp.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const performance = @import("performance.zig").performance;
1616
pub const Error = error{
1717
UnknonwDomain,
1818
UnknownMethod,
19+
NoResponse,
1920
};
2021

2122
pub fn isCdpError(err: anyerror) ?Error {
@@ -85,7 +86,7 @@ pub fn do(
8586
.Target => target(alloc, id, iter.next().?, &scanner, ctx),
8687
.Page => page(alloc, id, iter.next().?, &scanner, ctx),
8788
.Log => log(alloc, id, iter.next().?, &scanner, ctx),
88-
.Runtime => runtime(alloc, id, iter.next().?, &scanner, ctx),
89+
.Runtime => runtime(alloc, id, iter.next().?, &scanner, s, ctx),
8990
.Network => network(alloc, id, iter.next().?, &scanner, ctx),
9091
.Emulation => emulation(alloc, id, iter.next().?, &scanner, ctx),
9192
.Fetch => fetch(alloc, id, iter.next().?, &scanner, ctx),
@@ -199,7 +200,7 @@ fn getParams(
199200
key: []const u8,
200201
) !?T {
201202

202-
// check key key is "params"
203+
// check key is "params"
203204
if (!std.mem.eql(u8, "params", key)) return null;
204205

205206
// skip "params" if not requested
@@ -285,7 +286,8 @@ pub fn getMsg(
285286
// Common
286287
// ------
287288

288-
pub const SessionID = "9559320D92474062597D9875C664CAC0";
289+
pub const BrowserSessionID = "9559320D92474062597D9875C664CAC0";
290+
pub const ContextSessionID = "4FDC2CB760A23A220497A05C95417CF4";
289291
pub const URLBase = "chrome://newtab/";
290292
pub const FrameID = "90D14BBD8AED408A0467AC93100BCDBE";
291293
pub const LoaderID = "CFC8BED824DD2FD56CF1EF33C965C79C";

src/cdp/page.zig

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,38 @@ fn createIsolatedWorld(
144144
alloc: std.mem.Allocator,
145145
id: ?u16,
146146
scanner: *std.json.Scanner,
147-
_: *Ctx,
147+
ctx: *Ctx,
148148
) ![]const u8 {
149-
const msg = try getMsg(alloc, void, scanner);
149+
150+
// input
151+
const Params = struct {
152+
frameId: []const u8,
153+
worldName: []const u8,
154+
grantUniveralAccess: bool,
155+
};
156+
const msg = try getMsg(alloc, Params, scanner);
157+
std.debug.assert(msg.sessionID != null);
158+
const params = msg.params.?;
159+
160+
// noop executionContextCreated event
161+
try Runtime.executionContextCreated(
162+
alloc,
163+
ctx,
164+
0,
165+
"",
166+
params.worldName,
167+
"7102379147004877974.3265385113993241162",
168+
.{
169+
.isDefault = false,
170+
.type = "isolated",
171+
.frameId = params.frameId,
172+
},
173+
msg.sessionID,
174+
);
150175

151176
// output
152177
const Resp = struct {
153-
executionContextId: u8 = 2,
178+
executionContextId: u8 = 0,
154179
};
155180

156181
return result(alloc, id orelse msg.id.?, Resp, .{}, msg.sessionID);
@@ -230,20 +255,14 @@ fn navigate(
230255

231256
// Launch navigate
232257
var p = try ctx.browser.currentSession().createPage();
233-
_ = try p.navigate(params.url);
234-
235-
// Send create runtime context event
236258
ctx.state.executionContextId += 1;
237-
try Runtime.executionContextCreated(
259+
const auxData = try std.fmt.allocPrint(
238260
alloc,
239-
ctx,
240-
ctx.state.executionContextId,
241-
"http://127.0.0.1:1234", // TODO: real domain
242-
"",
243-
"7102379147004877974.3265385113993241162",
244-
.{ .frameId = ctx.state.frameID },
245-
msg.sessionID,
261+
"{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}",
262+
.{ctx.state.frameID},
246263
);
264+
defer alloc.free(auxData);
265+
_ = try p.navigate(params.url, auxData);
247266

248267
// frameNavigated event
249268
const FrameNavigated = struct {

0 commit comments

Comments
 (0)