mirror of
				https://github.com/lightpanda-io/browser.git
				synced 2025-10-29 23:23:28 +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(); | ||||
|         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. | ||||
|   | ||||
| @@ -112,16 +112,10 @@ fn getDocument( | ||||
|     std.debug.assert(input.sessionId != null); | ||||
|     log.debug("Req > id {d}, method {s}", .{ input.id, "DOM.getDocument" }); | ||||
|  | ||||
|     if (ctx.browser.session.page == null) { | ||||
|         return error.NoPage; | ||||
|     } | ||||
|  | ||||
|     // retrieve the root node | ||||
|     const page = ctx.browser.session.page.?; | ||||
|     const page = ctx.browser.currentPage() orelse return error.NoPage; | ||||
|  | ||||
|     if (page.doc == null) { | ||||
|         return error.NoDocument; | ||||
|     } | ||||
|     if (page.doc == null) return error.NoDocument; | ||||
|  | ||||
|     const root = try parser.documentGetDocumentElement(page.doc.?) orelse { | ||||
|         return error.NoRoot; | ||||
|   | ||||
| @@ -333,7 +333,7 @@ fn navigate( | ||||
|  | ||||
|     // Launch navigate, the page must have been created by a | ||||
|     // 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; | ||||
|     const auxData = try std.fmt.allocPrint( | ||||
|         alloc, | ||||
|   | ||||
| @@ -353,7 +353,7 @@ fn createTarget( | ||||
|     } | ||||
|  | ||||
|     // 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 | ||||
|     const p = try ctx.browser.session.createPage(); | ||||
| @@ -464,7 +464,7 @@ fn closeTarget( | ||||
|         null, | ||||
|     ); | ||||
|  | ||||
|     if (ctx.browser.session.page != null) ctx.browser.session.page.?.end(); | ||||
|     if (ctx.browser.currentPage()) |page| page.end(); | ||||
|  | ||||
|     return ""; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Pierre Tachoire
					Pierre Tachoire