Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
117 commits
Select commit Hold shift + click to select a range
c57bb9e
WIP: CDP
francisbouvier Apr 15, 2024
babac69
Remove alloc from CmdContext struct
francisbouvier Apr 15, 2024
defab0c
Free msg at the right place
francisbouvier Apr 15, 2024
cfd6fc9
Working sendLater (I/O timeout)
francisbouvier Apr 15, 2024
95a64b7
Handle concurrent calls to sendLater
francisbouvier Apr 15, 2024
e908cb0
Use send as normal behavior in cmdCallback
francisbouvier Apr 15, 2024
ffbfd36
Add stringify function in cdp
francisbouvier Apr 15, 2024
5e1fe65
send Target.attachedToTarget after Target.setAutoAttach
francisbouvier Apr 15, 2024
9805710
Big refacto
francisbouvier Apr 15, 2024
b124220
Add Page domain
francisbouvier Apr 15, 2024
a708a7f
Add Page.getFrameTree
francisbouvier Apr 15, 2024
626fae0
Add Log domain
francisbouvier Apr 15, 2024
e073e33
Add Runtime domain
francisbouvier Apr 15, 2024
0a03dcb
Add Page.setLifecycleEventsEnabled
francisbouvier Apr 15, 2024
86b1c85
Add Page.addScriptToEvaluateOnNewDocument
francisbouvier Apr 15, 2024
aff2250
Add Emulation domain
francisbouvier Apr 15, 2024
67bbd99
Add Network domain
francisbouvier Apr 15, 2024
211fa3d
Handle several JSON msg in 1 read
francisbouvier Apr 16, 2024
26eda90
Add setFocusEmulationEnabled
francisbouvier Apr 16, 2024
36dbc28
Add Runtime.runIfWaitingForDebugger
francisbouvier Apr 16, 2024
9e8b765
Allow method with sessionId and use it when appropriate (*.enable)
francisbouvier Apr 16, 2024
05c5d06
Change Page.addScriptToEvaluateOnNewDocument
francisbouvier Apr 16, 2024
21afa1f
Do not emit optional null value in JSON output
francisbouvier Apr 17, 2024
4d8cdc6
Handle sessionId in result
francisbouvier Apr 17, 2024
e59fc90
Return a result in Page.getFrameTree
francisbouvier Apr 18, 2024
1620138
Return sessionId in Emulation.setFocusEmulationEnabled
francisbouvier Apr 18, 2024
e4ae2df
Add some optional params in methods
francisbouvier Apr 18, 2024
43a558f
Make getParams return nullable
francisbouvier Apr 18, 2024
490eb40
Add method cdp function
francisbouvier Apr 18, 2024
69f5bb9
Add sessionId in Runime.runIfWaitingForDebugger response
francisbouvier Apr 18, 2024
06f161c
Add Target.getTargetInfo
francisbouvier Apr 18, 2024
0506a7b
Add Browser.createBrowserContext
francisbouvier Apr 18, 2024
9974b56
Add Target.createTarget
francisbouvier Apr 18, 2024
f02de77
Add getContent
francisbouvier Apr 18, 2024
508741c
Add Browser.getWindowForTarget
francisbouvier Apr 18, 2024
fc1b3d5
Contextual frameTree
francisbouvier Apr 18, 2024
c7ba567
Handle non-empty void params in getContent
francisbouvier Apr 18, 2024
aec7455
Add Emulation.setDeviceMetricsOverride
francisbouvier Apr 18, 2024
c54b50e
Add Browser.setWindowBounds
francisbouvier Apr 18, 2024
9ce574a
Add Page.createIsolatedWorld
francisbouvier Apr 18, 2024
4f0b071
Fix getContent algo
francisbouvier Apr 19, 2024
1a1cd03
Add dummy Page.navigate
francisbouvier Apr 19, 2024
ed38705
Basic version using Browser
francisbouvier Apr 19, 2024
9e13ffb
Add sendEvent utility function
francisbouvier Apr 19, 2024
1b1b7cd
Add page_life_cycle_events in CDP state
francisbouvier Apr 19, 2024
4a31dd8
Let Page.navigate do actually navigation
francisbouvier Apr 19, 2024
7a03562
Typo fix Page.LifecycleEvent
francisbouvier Apr 23, 2024
28d5c68
Use sendEvent in Runtime.executionContextCreated and expose it
francisbouvier Apr 23, 2024
3396c70
Send Runtime.executionContextCreated events in Page.navigate
francisbouvier Apr 23, 2024
96906df
Implement own protocol to handle msg size
francisbouvier Apr 23, 2024
ba12945
Move read input from Cmd callback to allow unit tests
francisbouvier Apr 24, 2024
bafdca3
MsgBuffer to handle both combined and multipart read
francisbouvier May 22, 2024
c57e50c
Handle Runtime.evaluate (no-op)
francisbouvier May 27, 2024
bfb9db2
Basic Runtime.evaluate run
francisbouvier May 30, 2024
3ad19df
Handle CDP msg with order <id, method> and <method, id>
francisbouvier May 30, 2024
dc1456f
Handle CDP messages with different order
francisbouvier Jun 7, 2024
fa82160
Add target.getBrowserContexts
francisbouvier Jun 7, 2024
ad8c9fa
Add target.setDiscoverTargets
francisbouvier Jun 7, 2024
1929eed
Add contextID in state
francisbouvier Jun 7, 2024
7d67d13
Add network.setCacheDisabled
francisbouvier Jun 7, 2024
cecc03e
Add fetch.disable
francisbouvier Jun 7, 2024
08c11ac
Add performance.enable
francisbouvier Jun 7, 2024
9120b9c
Add emulation.setTouchEmulationEnabled
francisbouvier Jun 7, 2024
7abb727
Fix call to Runtime.executionContextCreated in Page.navigate
francisbouvier Jun 12, 2024
4099696
Add Runtime.addBinding
francisbouvier Jun 12, 2024
4d756b5
Add a dumpFile utility function
francisbouvier Jun 17, 2024
9319e4a
Handle Runtime.callFunctionOn
francisbouvier Jun 17, 2024
5eae158
Add some optional fields in Runtime.evaluate
francisbouvier Jun 19, 2024
0f8b47b
Move MsgBuffer in it's own file for unit test purpose
francisbouvier Jun 19, 2024
aca64ee
Uniformize calling name conventions
francisbouvier Jun 19, 2024
ea410c8
Fix changes in Zig 0.12 std lib
francisbouvier Jun 19, 2024
4140903
Adapt to refacto in js_exec from zig-js-runtime
francisbouvier Jul 8, 2024
14a3a66
Fix response of runtime.Evaluate
francisbouvier Jul 9, 2024
94d2d28
Redirect Runtime domain to JS engine Inspector
francisbouvier Oct 1, 2024
1675f69
Add Target.closeTarget
francisbouvier Oct 1, 2024
8bdd2a1
Add Target.disposeBrowserContext
francisbouvier Oct 1, 2024
2f3a581
Add TODOs and comments
francisbouvier Oct 1, 2024
5ab1d2a
Add License in new cdp files
francisbouvier Oct 1, 2024
9c913b2
Move loop outside Browser
francisbouvier Oct 7, 2024
4c225e5
server: let the caller of sendSync free the string
francisbouvier Oct 7, 2024
76a9034
server: newSession on disposeBrowserContext
francisbouvier Oct 7, 2024
49adb61
server: handle close and re-open connection
francisbouvier Oct 8, 2024
c35c09d
server: timeout mechanism
francisbouvier Oct 8, 2024
b0ff325
server: move to TCP conn
francisbouvier Oct 8, 2024
c8a91d4
server: merge Cmd and Accept in Ctx
francisbouvier Oct 8, 2024
cea38a1
server: rename buf in read_buf
francisbouvier Oct 8, 2024
a2f65eb
server: simplify onInspector methods
francisbouvier Oct 8, 2024
f9b0977
Simplify browser session.setInspector
francisbouvier Oct 8, 2024
15414f5
server: remove unused sendLater
francisbouvier Oct 8, 2024
ff0bbc3
server: simplify Send I/O
francisbouvier Oct 8, 2024
9400dd7
Add cli options for server (host, port, timeout)
francisbouvier Oct 9, 2024
c564702
server: formatting
francisbouvier Oct 9, 2024
2bc58be
server: rename public -> jsruntime
francisbouvier Oct 9, 2024
bf56345
msg: comments typos
francisbouvier Oct 9, 2024
0d89b98
cdp: ensure token is a string when needed in parser
francisbouvier Oct 9, 2024
eaf5c6f
cdp: ensure method action is present
francisbouvier Oct 9, 2024
fa4920b
browser: rename setInspector -> initInspector
francisbouvier Oct 9, 2024
28593d9
browser: panic if callInspector without Inspector
francisbouvier Oct 9, 2024
e53b9d9
browser: add comment for auxData param in page.navigate
francisbouvier Oct 9, 2024
17c6418
msg: return error if input does not have "size:"
francisbouvier Oct 9, 2024
7ad03fb
cdp: fix a comment on page.navigate
francisbouvier Oct 9, 2024
4b495f2
cdp: add comment on hard coded ID for page.createIsolatedWorld
francisbouvier Oct 9, 2024
fd6c25d
msg: improve comments on reallocation
francisbouvier Oct 11, 2024
ec68059
msg: set a hard limit max size
francisbouvier Oct 11, 2024
cbf6348
server: panic if sendInspector without an inspector
francisbouvier Oct 12, 2024
efca715
browser: put back VM is an arg for browser init
francisbouvier Oct 12, 2024
ea9af21
Remove heap allocation for Session
francisbouvier Oct 15, 2024
7750956
msg: Add a more complex test case with 2 multipart messages combined
francisbouvier Oct 15, 2024
84c49fb
cdp: ensure there is an ID on each request
francisbouvier Oct 15, 2024
8e05f09
server, cdp: improve logging
francisbouvier Oct 15, 2024
7bc7da5
browser: back on createPage returning a Page (pointer)
francisbouvier Oct 16, 2024
2311765
Remove some dead code
francisbouvier Oct 16, 2024
462485b
Update zig-v8 and zig-js-runtime deps
francisbouvier Oct 16, 2024
b0634cd
Adapt wpt and shell to zig-js-runtime changes
francisbouvier Oct 16, 2024
7f08d08
Update zig-v8 again
francisbouvier Oct 16, 2024
e450072
ci: add zig v8 version into the cache key
krichprollsch Oct 16, 2024
8d83dfa
ci: force ubuntu version (24.04)
francisbouvier Oct 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions src/browser/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,33 @@ const log = std.log.scoped(.browser);
// A browser contains only one session.
// TODO allow multiple sessions per browser.
pub const Browser = struct {
session: *Session,
session: Session = undefined,

const uri = "about:blank";

pub fn init(alloc: std.mem.Allocator, loop: *Loop, vm: jsruntime.VM) !Browser {
pub fn init(self: *Browser, alloc: std.mem.Allocator, loop: *Loop, vm: jsruntime.VM) !void {
// We want to ensure the caller initialised a VM, but the browser
// doesn't use it directly...
_ = vm;

return Browser{
.session = try Session.init(alloc, loop, "about:blank"),
};
try Session.init(&self.session, alloc, loop, uri);
}

pub fn deinit(self: *Browser) void {
self.session.deinit();
}

pub fn newSession(self: *Browser, alloc: std.mem.Allocator, loop: *jsruntime.Loop) !void {
pub fn newSession(
self: *Browser,
alloc: std.mem.Allocator,
loop: *jsruntime.Loop,
) !void {
self.session.deinit();
self.session = try Session.init(alloc, loop, "about:blank");
try Session.init(&self.session, alloc, loop, uri);
}

pub fn currentSession(self: *Browser) *Session {
return self.session;
return &self.session;
}
};

Expand Down Expand Up @@ -99,13 +103,12 @@ pub const Session = struct {
window: Window,
// TODO move the shed to the browser?
storageShed: storage.Shed,
page: ?*Page = null,
_page: ?Page = null,
httpClient: HttpClient,

jstypes: [Types.len]usize = undefined,

fn init(alloc: std.mem.Allocator, loop: *Loop, uri: []const u8) !*Session {
var self = try alloc.create(Session);
fn init(self: *Session, alloc: std.mem.Allocator, loop: *Loop, uri: []const u8) !void {
self.* = Session{
.uri = uri,
.alloc = alloc,
Expand All @@ -116,15 +119,13 @@ pub const Session = struct {
.httpClient = undefined,
};

self.env = try Env.init(self.arena.allocator(), loop, null);
Env.init(&self.env, self.arena.allocator(), loop, null);
self.httpClient = .{ .allocator = alloc, .loop = loop };
try self.env.load(&self.jstypes);

return self;
}

fn deinit(self: *Session) void {
if (self.page) |page| page.end();
if (self._page) |*p| p.end();

if (self.inspector) |inspector| {
inspector.deinit(self.alloc);
Expand All @@ -136,7 +137,6 @@ pub const Session = struct {
self.httpClient.deinit();
self.loader.deinit();
self.storageShed.deinit();
self.alloc.destroy(self);
}

pub fn initInspector(
Expand All @@ -158,8 +158,17 @@ pub const Session = struct {
}
}

pub fn createPage(self: *Session) !Page {
return Page.init(self.alloc, self);
pub fn createPage(self: *Session) !void {
if (self._page != null) return error.SessionPageExists;
const p: Page = undefined;
self._page = p;
Page.init(&self._page.?, self.alloc, self);
}

// shortcut
pub fn page(self: *Session) *Page {
if (self._page) |*p| return p;
@panic("No Page on this session");
}
};

Expand All @@ -181,16 +190,14 @@ pub const Page = struct {
raw_data: ?[]const u8 = null,

fn init(
self: *Page,
alloc: std.mem.Allocator,
session: *Session,
) !Page {
if (session.page != null) return error.SessionPageExists;
var page = Page{
) void {
self.* = .{
.arena = std.heap.ArenaAllocator.init(alloc),
.session = session,
};
session.page = &page;
return page;
}

// reset js env and mem arena.
Expand Down
4 changes: 2 additions & 2 deletions src/cdp/page.zig
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ fn navigate(
try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, msg.sessionID);

// Launch navigate
var p = try ctx.browser.currentSession().createPage();
try ctx.browser.session.createPage();
ctx.state.executionContextId += 1;
const auxData = try std.fmt.allocPrint(
alloc,
Expand All @@ -290,7 +290,7 @@ fn navigate(
.{ctx.state.frameID},
);
defer alloc.free(auxData);
_ = try p.navigate(params.url, auxData);
try ctx.browser.session.page().navigate(params.url, auxData);

// Events

Expand Down
10 changes: 1 addition & 9 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,10 @@ pub fn main() !void {
defer srv.close();
std.log.info("Listening on: {s}:{d}...", .{ host, port });

// create v8 vm
const vm = jsruntime.VM.init();
defer vm.deinit();

// loop
var loop = try jsruntime.Loop.init(arena.allocator());
defer loop.deinit();

// browser
var browser = try Browser.init(arena.allocator(), &loop, vm);
defer browser.deinit();

// listen
try server.listen(&browser, &loop, srv.sockfd.?, std.time.ns_per_s * @as(u64, timeout));
try server.listen(arena.allocator(), &loop, srv.sockfd.?, std.time.ns_per_s * @as(u64, timeout));
}
14 changes: 7 additions & 7 deletions src/main_get.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ pub fn main() !void {
var loop = try jsruntime.Loop.init(allocator);
defer loop.deinit();

var browser = try Browser.init(allocator, &loop, vm);
var browser = Browser{};
try Browser.init(&browser, allocator, &loop, vm);
defer browser.deinit();

var page = try browser.currentSession().createPage();
defer page.deinit();
try browser.session.createPage();

try page.navigate(url, null);
defer page.end();
try browser.session.page().navigate(url, null);
defer browser.session.page().end();

try page.wait();
try browser.session.page().wait();

if (dump) {
try page.dump(std.io.getStdOut());
try browser.session.page().dump(std.io.getStdOut());
}
}
21 changes: 15 additions & 6 deletions src/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ pub const Ctx = struct {

// allocator of the current session
inline fn alloc(self: *Ctx) std.mem.Allocator {
return self.browser.currentSession().alloc;
return self.browser.session.alloc;
}

// JS env of the current session
inline fn env(self: Ctx) jsruntime.Env {
return self.browser.currentSession().env;
return self.browser.session.env;
}

// actions
Expand Down Expand Up @@ -300,7 +300,7 @@ pub const Ctx = struct {

fn newSession(self: *Ctx) !void {
try self.browser.newSession(self.alloc(), self.loop);
try self.browser.currentSession().initInspector(
try self.browser.session.initInspector(
self,
Ctx.onInspectorResp,
Ctx.onInspectorNotif,
Expand Down Expand Up @@ -398,12 +398,21 @@ pub fn sendSync(ctx: *Ctx, msg: []const u8) !void {
// ------

pub fn listen(
browser: *Browser,
alloc: std.mem.Allocator,
loop: *jsruntime.Loop,
server_socket: std.posix.socket_t,
timeout: u64,
) anyerror!void {

// create v8 vm
const vm = jsruntime.VM.init();
defer vm.deinit();

// browser
var browser: Browser = undefined;
try Browser.init(&browser, alloc, loop, vm);
defer browser.deinit();

// create buffers
var read_buf: [BufReadSize]u8 = undefined;
var msg_buf = try MsgBuffer.init(loop.alloc, BufReadSize * 256); // 256KB
Expand All @@ -417,7 +426,7 @@ pub fn listen(
// for accepting connections and receving messages
var ctx = Ctx{
.loop = loop,
.browser = browser,
.browser = &browser,
.sessionNew = true,
.read_buf = &read_buf,
.msg_buf = &msg_buf,
Expand All @@ -426,7 +435,7 @@ pub fn listen(
.conn_completion = &conn_completion,
.timeout_completion = &timeout_completion,
};
try browser.currentSession().initInspector(
try browser.session.initInspector(
&ctx,
Ctx.onInspectorResp,
Ctx.onInspectorNotif,
Expand Down
Loading