re-enable minimum viable CDP server

This commit is contained in:
Karl Seguin
2025-10-28 18:56:03 +08:00
parent cdd31353c5
commit d3973172e8
25 changed files with 1512 additions and 1399 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -24,12 +24,12 @@ const log = @import("../log.zig");
const js = @import("../browser/js/js.zig");
const polyfill = @import("../browser/polyfill/polyfill.zig");
const App = @import("../app.zig").App;
const Browser = @import("../browser/browser.zig").Browser;
const Session = @import("../browser/session.zig").Session;
const Page = @import("../browser/page.zig").Page;
const App = @import("../App.zig");
const Browser = @import("../browser/Browser.zig");
const Session = @import("../browser/Session.zig");
const Page = @import("../browser/Page.zig");
const Incrementing = @import("../id.zig").Incrementing;
const Notification = @import("../notification.zig").Notification;
const Notification = @import("../Notification.zig");
const LogInterceptor = @import("domains/log.zig").LogInterceptor;
const InterceptState = @import("domains/fetch.zig").InterceptState;
@@ -37,7 +37,7 @@ pub const URL_BASE = "chrome://newtab/";
pub const LOADER_ID = "LOADERID24DD2FD56CF1EF33C965C79C";
pub const CDP = CDPT(struct {
const Client = *@import("../server.zig").Client;
const Client = *@import("../Server.zig").Client;
});
const SessionIdGen = Incrementing(u32, "SID");
@@ -117,7 +117,7 @@ pub fn CDPT(comptime TypeProvider: type) type {
// timeouts (or http events) which are ready to be processed.
pub fn hasPage() bool {}
pub fn pageWait(self: *Self, ms: i32) Session.WaitResult {
pub fn pageWait(self: *Self, ms: u32) Session.WaitResult {
const session = &(self.browser.session orelse return .no_page);
return session.wait(ms);
}
@@ -203,7 +203,8 @@ pub fn CDPT(comptime TypeProvider: type) type {
},
5 => switch (@as(u40, @bitCast(domain[0..5].*))) {
asUint(u40, "Fetch") => return @import("domains/fetch.zig").processMessage(command),
asUint(u40, "Input") => return @import("domains/input.zig").processMessage(command),
// @ZIGDOM
// asUint(u40, "Input") => return @import("domains/input.zig").processMessage(command),
else => {},
},
6 => switch (@as(u48, @bitCast(domain[0..6].*))) {
@@ -286,7 +287,8 @@ pub fn CDPT(comptime TypeProvider: type) type {
}
pub fn BrowserContext(comptime CDP_T: type) type {
const Node = @import("Node.zig");
// @ZIGMOD
// const Node = @import("Node.zig");
return struct {
id: []const u8,
@@ -326,8 +328,9 @@ pub fn BrowserContext(comptime CDP_T: type) type {
security_origin: []const u8,
page_life_cycle_events: bool,
secure_context_type: []const u8,
node_registry: Node.Registry,
node_search_list: Node.Search.List,
// @ZIGDOM
// node_registry: Node.Registry,
// node_search_list: Node.Search.List,
inspector: js.Inspector,
isolated_worlds: std.ArrayListUnmanaged(IsolatedWorld),
@@ -360,8 +363,9 @@ pub fn BrowserContext(comptime CDP_T: type) type {
const inspector = try cdp.browser.env.newInspector(arena, self);
var registry = Node.Registry.init(allocator);
errdefer registry.deinit();
// @ZIGDOM
// var registry = Node.Registry.init(allocator);
// errdefer registry.deinit();
self.* = .{
.id = id,
@@ -374,8 +378,9 @@ pub fn BrowserContext(comptime CDP_T: type) type {
.secure_context_type = "Secure", // TODO = enum
.loader_id = LOADER_ID,
.page_life_cycle_events = false, // TODO; Target based value
.node_registry = registry,
.node_search_list = undefined,
// @ZIGDOM
// .node_registry = registry,
// .node_search_list = undefined,
.isolated_worlds = .empty,
.inspector = inspector,
.notification_arena = cdp.notification_arena.allocator(),
@@ -383,7 +388,8 @@ pub fn BrowserContext(comptime CDP_T: type) type {
.captured_responses = .empty,
.log_interceptor = LogInterceptor(Self).init(allocator, self),
};
self.node_search_list = Node.Search.List.init(allocator, &self.node_registry);
// ZIGDOM
// self.node_search_list = Node.Search.List.init(allocator, &self.node_registry);
errdefer self.deinit();
try cdp.browser.notification.register(.page_remove, self, onPageRemove);
@@ -418,8 +424,9 @@ pub fn BrowserContext(comptime CDP_T: type) type {
world.deinit();
}
self.isolated_worlds.clearRetainingCapacity();
self.node_registry.deinit();
self.node_search_list.deinit();
// @ZIGDOM
// self.node_registry.deinit();
// self.node_search_list.deinit();
self.cdp.browser.notification.unregisterAll(self);
if (self.http_proxy_changed) {
@@ -433,8 +440,10 @@ pub fn BrowserContext(comptime CDP_T: type) type {
}
pub fn reset(self: *Self) void {
self.node_registry.reset();
self.node_search_list.reset();
// @ZIGDOM
_ = self;
// self.node_registry.reset();
// self.node_search_list.reset();
}
pub fn createIsolatedWorld(self: *Self, world_name: []const u8, grant_universal_access: bool) !*IsolatedWorld {
@@ -453,19 +462,20 @@ pub fn BrowserContext(comptime CDP_T: type) type {
return world;
}
pub fn nodeWriter(self: *Self, root: *const Node, opts: Node.Writer.Opts) Node.Writer {
return .{
.root = root,
.depth = opts.depth,
.exclude_root = opts.exclude_root,
.registry = &self.node_registry,
};
}
// @ZIGDOM
// pub fn nodeWriter(self: *Self, root: *const Node, opts: Node.Writer.Opts) Node.Writer {
// return .{
// .root = root,
// .depth = opts.depth,
// .exclude_root = opts.exclude_root,
// .registry = &self.node_registry,
// };
// }
pub fn getURL(self: *const Self) ?[]const u8 {
pub fn getURL(self: *const Self) ?[:0]const u8 {
const page = self.session.currentPage() orelse return null;
const raw_url = page.url.raw;
return if (raw_url.len == 0) null else raw_url;
const url = page.url;
return if (url.len == 0) null else url;
}
pub fn networkEnable(self: *Self) !void {

File diff suppressed because it is too large Load Diff

View File

@@ -23,7 +23,7 @@ const log = @import("../../log.zig");
const network = @import("network.zig");
const Http = @import("../../http/Http.zig");
const Notification = @import("../../notification.zig").Notification;
const Notification = @import("../../Notification.zig");
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {

View File

@@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Page = @import("../../browser/page.zig").Page;
const Page = @import("../../browser/Page.zig");
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {

View File

@@ -101,7 +101,7 @@ pub fn LogInterceptor(comptime BC: type) type {
.fatal => "error",
},
.text = self.allocating.written(),
.timestamp = @import("../../datetime.zig").milliTimestamp(),
.timestamp = @import("../../datetime.zig").milliTimestamp(.monotonic),
},
}, .{
.session_id = self.bc.session_id,

View File

@@ -21,7 +21,7 @@ const Allocator = std.mem.Allocator;
const CdpStorage = @import("storage.zig");
const Transfer = @import("../../http/Client.zig").Transfer;
const Notification = @import("../../notification.zig").Notification;
const Notification = @import("../../Notification.zig");
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {
@@ -87,7 +87,7 @@ fn setExtraHTTPHeaders(cmd: anytype) !void {
return cmd.sendResult(null, .{});
}
const Cookie = @import("../../browser/storage/storage.zig").Cookie;
const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
// Only matches the cookie on provided parameters
fn cookieMatches(cookie: *const Cookie, name: []const u8, domain: ?[]const u8, path: ?[]const u8) bool {
@@ -173,7 +173,7 @@ fn getCookies(cmd: anytype) !void {
const params = (try cmd.params(GetCookiesParam)) orelse GetCookiesParam{};
// If not specified, use the URLs of the page and all of its subframes. TODO subframes
const page_url = if (bc.session.page) |*page| page.url.raw else null; // @speed: avoid repasing the URL
const page_url = if (bc.session.page) |page| page.url else null;
const param_urls = params.urls orelse &[_][]const u8{page_url orelse return error.InvalidParams};
var urls = try std.ArrayListUnmanaged(CdpStorage.PreparedUri).initCapacity(cmd.arena, param_urls.len);
@@ -247,7 +247,7 @@ pub fn httpRequestStart(arena: Allocator, bc: anytype, msg: *const Notification.
.requestId = try std.fmt.allocPrint(arena, "REQ-{d}", .{transfer.id}),
.frameId = target_id,
.loaderId = bc.loader_id,
.documentUrl = DocumentUrlWriter.init(&page.url.uri),
.documentUrl = page.url,
.request = TransferAsRequestWriter.init(transfer),
.initiator = .{ .type = "other" },
}, .{ .session_id = session_id });
@@ -416,34 +416,35 @@ const TransferAsResponseWriter = struct {
}
};
const DocumentUrlWriter = struct {
uri: *std.Uri,
// @ZIGDOM - do we still need this? just send the full URL?
// const DocumentUrlWriter = struct {
// uri: *std.Uri,
fn init(uri: *std.Uri) DocumentUrlWriter {
return .{
.uri = uri,
};
}
// fn init(uri: *std.Uri) DocumentUrlWriter {
// return .{
// .uri = uri,
// };
// }
pub fn jsonStringify(self: *const DocumentUrlWriter, jws: anytype) !void {
self._jsonStringify(jws) catch return error.WriteFailed;
}
fn _jsonStringify(self: *const DocumentUrlWriter, jws: anytype) !void {
const writer = jws.writer;
// pub fn jsonStringify(self: *const DocumentUrlWriter, jws: anytype) !void {
// self._jsonStringify(jws) catch return error.WriteFailed;
// }
// fn _jsonStringify(self: *const DocumentUrlWriter, jws: anytype) !void {
// const writer = jws.writer;
try jws.beginWriteRaw();
try writer.writeByte('\"');
try self.uri.writeToStream(writer, .{
.scheme = true,
.authentication = true,
.authority = true,
.path = true,
.query = true,
});
try writer.writeByte('\"');
jws.endWriteRaw();
}
};
// try jws.beginWriteRaw();
// try writer.writeByte('\"');
// try self.uri.writeToStream(writer, .{
// .scheme = true,
// .authentication = true,
// .authority = true,
// .path = true,
// .query = true,
// });
// try writer.writeByte('\"');
// jws.endWriteRaw();
// }
// };
fn idFromRequestId(request_id: []const u8) !u64 {
if (!std.mem.startsWith(u8, request_id, "REQ-")) {

View File

@@ -17,8 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Page = @import("../../browser/page.zig").Page;
const Notification = @import("../../notification.zig").Notification;
const Page = @import("../../browser/Page.zig");
const Notification = @import("../../Notification.zig");
const Allocator = std.mem.Allocator;
@@ -134,7 +134,7 @@ fn createIsolatedWorld(cmd: anytype) !void {
fn navigate(cmd: anytype) !void {
const params = (try cmd.params(struct {
url: []const u8,
url: [:0]const u8,
// referrer: ?[]const u8 = null,
// transitionType: ?[]const u8 = null, // TODO: enum
// frameId: ?[]const u8 = null,
@@ -253,7 +253,8 @@ pub fn pageNavigate(arena: Allocator, bc: anytype, event: *const Notification.Pa
bc.inspector.contextCreated(
page.js,
"",
try page.origin(arena),
"", // @ZIGDOM
// try page.origin(arena),
aux_data,
true,
);
@@ -360,7 +361,7 @@ pub fn pageNetworkAlmostIdle(bc: anytype, event: *const Notification.PageNetwork
return sendPageLifecycle(bc, "networkAlmostIdle", event.timestamp);
}
fn sendPageLifecycle(bc: anytype, name: []const u8, timestamp: u32) !void {
fn sendPageLifecycle(bc: anytype, name: []const u8, timestamp: u64) !void {
// detachTarget could be called, in which case, we still have a page doing
// things, but no session.
const session_id = bc.session_id orelse return;
@@ -379,7 +380,7 @@ const LifecycleEvent = struct {
frameId: []const u8,
loaderId: ?[]const u8,
name: []const u8,
timestamp: u32,
timestamp: u64,
};
const testing = @import("../testing.zig");

View File

@@ -19,9 +19,9 @@
const std = @import("std");
const log = @import("../../log.zig");
const Cookie = @import("../../browser/storage/storage.zig").Cookie;
const CookieJar = @import("../../browser/storage/storage.zig").CookieJar;
pub const PreparedUri = @import("../../browser/storage/cookie.zig").PreparedUri;
const Cookie = @import("../../browser/webapi/storage/storage.zig").Cookie;
const CookieJar = @import("../../browser/webapi/storage/storage.zig").Jar;
pub const PreparedUri = @import("../../browser/webapi/storage/cookie.zig").PreparedUri;
pub fn processMessage(cmd: anytype) !void {
const action = std.meta.stringToEnum(enum {

View File

@@ -143,13 +143,14 @@ fn createTarget(cmd: anytype) !void {
bc.target_id = target_id;
var page = try bc.session.createPage();
const page = try bc.session.createPage();
{
const aux_data = try std.fmt.allocPrint(cmd.arena, "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{target_id});
bc.inspector.contextCreated(
page.js,
"",
try page.origin(cmd.arena),
"", // @ZIGDOM
// try page.origin(arena),
aux_data,
true,
);

View File

@@ -24,7 +24,6 @@ const ArenaAllocator = std.heap.ArenaAllocator;
const Testing = @This();
const main = @import("cdp.zig");
const parser = @import("../browser/netsurf.zig");
const base = @import("../testing.zig");
pub const allocator = base.allocator;