browser: split page start from page navigate

This commit is contained in:
Pierre Tachoire
2024-12-19 10:00:49 +01:00
parent 8e2cb36597
commit 2fed239ece
4 changed files with 29 additions and 14 deletions

View File

@@ -214,6 +214,20 @@ pub const Page = struct {
};
}
// start js env.
pub fn start(self: *Page) !void {
// start JS env
log.debug("start js env", .{});
try self.session.env.start();
// register the module loader
try self.session.env.setModuleLoadFn(self.session, Session.fetchModule);
// add global objects
log.debug("setup global env", .{});
try self.session.env.bindGlobal(&self.session.window);
}
// reset js env and mem arena.
pub fn end(self: *Page) void {
self.session.env.stop();
@@ -373,14 +387,6 @@ pub const Page = struct {
// https://html.spec.whatwg.org/#read-html
// start JS env
// TODO load the js env concurrently with the HTML parsing.
log.debug("start js env", .{});
try self.session.env.start();
// register the module loader
try self.session.env.setModuleLoadFn(self.session, Session.fetchModule);
// load polyfills
try polyfill.load(alloc, self.session.env);
@@ -395,10 +401,6 @@ pub const Page = struct {
.httpClient = &self.session.httpClient,
});
// add global objects
log.debug("setup global env", .{});
try self.session.env.bindGlobal(&self.session.window);
// browse the DOM tree to retrieve scripts
// TODO execute the synchronous scripts during the HTL parsing.
// TODO fetch the script resources concurrently but execute them in the

View File

@@ -331,8 +331,9 @@ fn navigate(
// TODO: noop event, we have no env context at this point, is it necesarry?
try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, input.sessionId);
// Launch navigate
const p = try ctx.browser.session.createPage();
// Launch navigate, the page must have been created by a
// target.createTarget.
var p = ctx.browser.session.page orelse return error.NoPage;
ctx.state.executionContextId += 1;
const auxData = try std.fmt.allocPrint(
alloc,

View File

@@ -344,6 +344,14 @@ fn createTarget(
ctx.state.loaderID = LoaderID;
ctx.state.sessionID = msg.sessionId;
// TODO stop the previous page instead?
if (ctx.browser.session.page != null) return error.pageAlreadyExists;
// create the page
const p = try ctx.browser.session.createPage();
// start the js env
try p.start();
// send targetCreated event
const created = TargetCreated{
.sessionId = cdp.ContextSessionID,
@@ -440,6 +448,8 @@ fn closeTarget(
null,
);
if (ctx.browser.session.page != null) ctx.browser.session.page.?.end();
return "";
}

View File

@@ -326,6 +326,8 @@ pub fn main() !void {
// page
const page = try browser.session.createPage();
try page.start();
defer page.end();
_ = page.navigate(opts.url, null) catch |err| switch (err) {
error.UnsupportedUriScheme, error.UriMissingHost => {