Skip to content

Commit e7dcb8a

Browse files
committed
cdp: introduce current page
avoid page struct copy
1 parent 8f8a1fd commit e7dcb8a

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

src/browser/browser.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ pub const Browser = struct {
8282
self.session.deinit();
8383
try Session.init(&self.session, alloc, loop, uri);
8484
}
85+
86+
pub fn currentPage(self: *Browser) ?*Page {
87+
if (self.session.page == null) return null;
88+
89+
return &self.session.page.?;
90+
}
8591
};
8692

8793
// Session is like a browser's tab.

src/cdp/dom.zig

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,10 @@ fn getDocument(
112112
std.debug.assert(input.sessionId != null);
113113
log.debug("Req > id {d}, method {s}", .{ input.id, "DOM.getDocument" });
114114

115-
if (ctx.browser.session.page == null) {
116-
return error.NoPage;
117-
}
118-
119115
// retrieve the root node
120-
const page = ctx.browser.session.page.?;
116+
const page = ctx.browser.currentPage() orelse return error.NoPage;
121117

122-
if (page.doc == null) {
123-
return error.NoDocument;
124-
}
118+
if (page.doc == null) return error.NoDocument;
125119

126120
const root = try parser.documentGetDocumentElement(page.doc.?) orelse {
127121
return error.NoRoot;

src/cdp/page.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn navigate(
333333

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

src/cdp/target.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ fn createTarget(
353353
}
354354

355355
// TODO stop the previous page instead?
356-
if (ctx.browser.session.page != null) return error.pageAlreadyExists;
356+
if (ctx.browser.currentPage() != null) return error.pageAlreadyExists;
357357

358358
// create the page
359359
const p = try ctx.browser.session.createPage();
@@ -464,7 +464,7 @@ fn closeTarget(
464464
null,
465465
);
466466

467-
if (ctx.browser.session.page != null) ctx.browser.session.page.?.end();
467+
if (ctx.browser.currentPage()) |page| page.end();
468468

469469
return "";
470470
}

0 commit comments

Comments
 (0)