mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
cdp: introduce current page
avoid page struct copy
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -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 "";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user