cdp: introduce current page

avoid page struct copy
This commit is contained in:
Pierre Tachoire
2025-02-05 17:48:18 +01:00
parent 8f8a1fda85
commit e7dcb8a605
4 changed files with 11 additions and 11 deletions

View File

@@ -82,6 +82,12 @@ pub const Browser = struct {
self.session.deinit(); self.session.deinit();
try Session.init(&self.session, alloc, loop, uri); try Session.init(&self.session, alloc, loop, uri);
} }
pub fn currentPage(self: *Browser) ?*Page {
if (self.session.page == null) return null;
return &self.session.page.?;
}
}; };
// Session is like a browser's tab. // Session is like a browser's tab.

View File

@@ -112,16 +112,10 @@ fn getDocument(
std.debug.assert(input.sessionId != null); std.debug.assert(input.sessionId != null);
log.debug("Req > id {d}, method {s}", .{ input.id, "DOM.getDocument" }); log.debug("Req > id {d}, method {s}", .{ input.id, "DOM.getDocument" });
if (ctx.browser.session.page == null) {
return error.NoPage;
}
// retrieve the root node // retrieve the root node
const page = ctx.browser.session.page.?; const page = ctx.browser.currentPage() orelse return error.NoPage;
if (page.doc == null) { if (page.doc == null) return error.NoDocument;
return error.NoDocument;
}
const root = try parser.documentGetDocumentElement(page.doc.?) orelse { const root = try parser.documentGetDocumentElement(page.doc.?) orelse {
return error.NoRoot; return error.NoRoot;

View File

@@ -333,7 +333,7 @@ fn navigate(
// Launch navigate, the page must have been created by a // Launch navigate, the page must have been created by a
// target.createTarget. // target.createTarget.
var p = ctx.browser.session.page orelse return error.NoPage; var p = ctx.browser.currentPage() orelse return error.NoPage;
ctx.state.executionContextId += 1; ctx.state.executionContextId += 1;
const auxData = try std.fmt.allocPrint( const auxData = try std.fmt.allocPrint(
alloc, alloc,

View File

@@ -353,7 +353,7 @@ fn createTarget(
} }
// TODO stop the previous page instead? // TODO stop the previous page instead?
if (ctx.browser.session.page != null) return error.pageAlreadyExists; if (ctx.browser.currentPage() != null) return error.pageAlreadyExists;
// create the page // create the page
const p = try ctx.browser.session.createPage(); const p = try ctx.browser.session.createPage();
@@ -464,7 +464,7 @@ fn closeTarget(
null, null,
); );
if (ctx.browser.session.page != null) ctx.browser.session.page.?.end(); if (ctx.browser.currentPage()) |page| page.end();
return ""; return "";
} }