browser: pass the session to the page

This commit is contained in:
Pierre Tachoire
2024-01-16 09:26:30 +01:00
parent c2a842b80e
commit 69bbd62edf

View File

@@ -95,9 +95,7 @@ pub const Session = struct {
pub fn createPage(self: *Session) !Page { pub fn createPage(self: *Session) !Page {
return Page.init( return Page.init(
self.arena.allocator(), self.arena.allocator(),
&self.loader, self,
&self.env,
&self.window,
); );
} }
}; };
@@ -107,9 +105,7 @@ pub const Session = struct {
// end() to stop the previous navigation before starting a new one. // end() to stop the previous navigation before starting a new one.
pub const Page = struct { pub const Page = struct {
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
loader: *Loader, session: *Session,
env: *Env,
window: *Window,
doc: ?*parser.Document = null, doc: ?*parser.Document = null,
// handle url // handle url
@@ -120,20 +116,16 @@ pub const Page = struct {
fn init( fn init(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
loader: *Loader, session: *Session,
env: *Env,
window: *Window,
) Page { ) Page {
return Page{ return Page{
.alloc = alloc, .alloc = alloc,
.loader = loader, .session = session,
.env = env,
.window = window,
}; };
} }
pub fn end(self: *Page) void { pub fn end(self: *Page) void {
self.env.stop(); self.session.env.stop();
// TODO unload document: https://html.spec.whatwg.org/#unloading-documents // TODO unload document: https://html.spec.whatwg.org/#unloading-documents
} }
@@ -172,7 +164,7 @@ pub const Page = struct {
// TODO handle fragment in url. // TODO handle fragment in url.
// load the data // load the data
var resp = try self.loader.get(self.alloc, self.uri); var resp = try self.session.loader.get(self.alloc, self.uri);
defer resp.deinit(); defer resp.deinit();
const req = resp.req; const req = resp.req;
@@ -218,20 +210,20 @@ pub const Page = struct {
// TODO inject the URL to the document including the fragment. // TODO inject the URL to the document including the fragment.
// TODO set the referrer to the document. // TODO set the referrer to the document.
self.window.replaceDocument(doc); self.session.window.replaceDocument(doc);
// https://html.spec.whatwg.org/#read-html // https://html.spec.whatwg.org/#read-html
// start JS env // start JS env
// TODO load the js env concurrently with the HTML parsing. // TODO load the js env concurrently with the HTML parsing.
log.debug("start js env", .{}); log.debug("start js env", .{});
try self.env.start(self.alloc); try self.session.env.start(self.alloc);
// add global objects // add global objects
log.debug("setup global env", .{}); log.debug("setup global env", .{});
try self.env.addObject(self.window, "window"); try self.session.env.addObject(self.session.window, "window");
try self.env.addObject(self.window, "self"); try self.session.env.addObject(self.session.window, "self");
try self.env.addObject(html_doc, "document"); try self.session.env.addObject(html_doc, "document");
// browse the DOM tree to retrieve scripts // browse the DOM tree to retrieve scripts
// TODO execute the synchronous scripts during the HTL parsing. // TODO execute the synchronous scripts during the HTL parsing.
@@ -359,7 +351,7 @@ pub const Page = struct {
if (opt_text) |text| { if (opt_text) |text| {
// TODO handle charset attribute // TODO handle charset attribute
var res = jsruntime.JSResult{}; var res = jsruntime.JSResult{};
try self.env.run(self.alloc, text, "", &res, null); try self.session.env.run(self.alloc, text, "", &res, null);
defer res.deinit(self.alloc); defer res.deinit(self.alloc);
if (res.success) { if (res.success) {
@@ -390,7 +382,7 @@ pub const Page = struct {
const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src); const u = std.Uri.parse(src) catch try std.Uri.parseWithoutScheme(src);
const ru = try std.Uri.resolve(self.uri, u, false, self.alloc); const ru = try std.Uri.resolve(self.uri, u, false, self.alloc);
var fetchres = try self.loader.fetch(self.alloc, ru); var fetchres = try self.session.loader.fetch(self.alloc, ru);
defer fetchres.deinit(); defer fetchres.deinit();
log.info("fech script {any}: {d}", .{ ru, fetchres.status }); log.info("fech script {any}: {d}", .{ ru, fetchres.status });
@@ -403,7 +395,7 @@ pub const Page = struct {
if (fetchres.body == null) return FetchError.NoBody; if (fetchres.body == null) return FetchError.NoBody;
var res = jsruntime.JSResult{}; var res = jsruntime.JSResult{};
try self.env.run(self.alloc, fetchres.body.?, src, &res, null); try self.session.env.run(self.alloc, fetchres.body.?, src, &res, null);
defer res.deinit(self.alloc); defer res.deinit(self.alloc);
if (res.success) { if (res.success) {