Skip to content

Commit aec2675

Browse files
committed
browser: split page start from page navigate
1 parent 90fffb7 commit aec2675

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/browser/browser.zig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ pub const Page = struct {
195195
};
196196
}
197197

198+
// start js env.
199+
pub fn start(self: *Page) !void {
200+
// start JS env
201+
log.debug("start js env", .{});
202+
try self.session.env.start();
203+
204+
// add global objects
205+
log.debug("setup global env", .{});
206+
try self.session.env.bindGlobal(&self.session.window);
207+
}
208+
198209
// reset js env and mem arena.
199210
pub fn end(self: *Page) void {
200211
self.session.env.stop();
@@ -349,11 +360,6 @@ pub const Page = struct {
349360

350361
// https://html.spec.whatwg.org/#read-html
351362

352-
// start JS env
353-
// TODO load the js env concurrently with the HTML parsing.
354-
log.debug("start js env", .{});
355-
try self.session.env.start();
356-
357363
// load polyfills
358364
try polyfill.load(alloc, self.session.env);
359365

@@ -368,10 +374,6 @@ pub const Page = struct {
368374
.httpClient = &self.session.httpClient,
369375
});
370376

371-
// add global objects
372-
log.debug("setup global env", .{});
373-
try self.session.env.bindGlobal(&self.session.window);
374-
375377
// browse the DOM tree to retrieve scripts
376378
// TODO execute the synchronous scripts during the HTL parsing.
377379
// TODO fetch the script resources concurrently but execute them in the

src/cdp/page.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ fn navigate(
331331
// TODO: noop event, we have no env context at this point, is it necesarry?
332332
try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, input.sessionId);
333333

334-
// Launch navigate
335-
const p = try ctx.browser.session.createPage();
334+
// Launch navigate, the page must have been created by a
335+
// target.createTarget.
336+
var p = ctx.browser.session.page orelse return error.NoPage;
336337
ctx.state.executionContextId += 1;
337338
const auxData = try std.fmt.allocPrint(
338339
alloc,

src/cdp/target.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ fn createTarget(
329329
ctx.state.loaderID = LoaderID;
330330
ctx.state.sessionID = msg.sessionId;
331331

332+
// TODO stop the previous page instead?
333+
if (ctx.browser.session.page != null) return error.pageAlreadyExists;
334+
335+
// create the page
336+
const p = try ctx.browser.session.createPage();
337+
// start the js env
338+
try p.start();
339+
332340
// send attachToTarget event
333341
const attached = AttachToTarget{
334342
.sessionId = cdp.ContextSessionID,
@@ -412,5 +420,7 @@ fn closeTarget(
412420
null,
413421
);
414422

423+
if (ctx.browser.session.page != null) ctx.browser.session.page.?.end();
424+
415425
return "";
416426
}

src/main.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ pub fn main() !void {
326326

327327
// page
328328
const page = try browser.session.createPage();
329+
try page.start();
330+
defer page.end();
329331

330332
_ = page.navigate(opts.url, null) catch |err| switch (err) {
331333
error.UnsupportedUriScheme, error.UriMissingHost => {

0 commit comments

Comments
 (0)