From fc1b3d539787f131f180e57b45a9936a05ed7763 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Thu, 18 Apr 2024 20:54:30 +0200 Subject: [PATCH] Contextual frameTree Signed-off-by: Francis Bouvier --- src/cdp/cdp.zig | 9 +++++++++ src/cdp/page.zig | 33 +++++++++++++++++++++------------ src/cdp/target.zig | 11 +++++++++-- src/server.zig | 3 +++ 4 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 41dbb2ac..a7e3a0fa 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -71,6 +71,14 @@ pub fn do( }; } +pub const State = struct { + frameID: []const u8 = FrameID, + url: []const u8 = URLBase, + securityOrigin: []const u8 = URLBase, + secureContextType: []const u8 = "Secure", // TODO: enum + loaderID: []const u8 = LoaderID, +}; + // Utils // ----- @@ -240,3 +248,4 @@ pub fn getContent( pub const SessionID = "9559320D92474062597D9875C664CAC0"; pub const URLBase = "chrome://newtab/"; pub const FrameID = "90D14BBD8AED408A0467AC93100BCDBE"; +pub const LoaderID = "CFC8BED824DD2FD56CF1EF33C965C79C"; diff --git a/src/cdp/page.zig b/src/cdp/page.zig index e71cce21..6fcd815a 100644 --- a/src/cdp/page.zig +++ b/src/cdp/page.zig @@ -41,35 +41,44 @@ fn enable( return result(alloc, id, null, null, sessionID); } -const LoaderID = "CFC8BED824DD2FD56CF1EF33C965C79C"; - fn getFrameTree( alloc: std.mem.Allocator, id: u64, scanner: *std.json.Scanner, - _: *Ctx, + ctx: *Ctx, ) ![]const u8 { const sessionID = try cdp.getSessionID(scanner); const FrameTree = struct { frameTree: struct { frame: struct { - id: []const u8 = cdp.FrameID, - loaderId: []const u8 = LoaderID, - url: []const u8 = cdp.URLBase, + id: []const u8, + loaderId: []const u8, + url: []const u8, domainAndRegistry: []const u8 = "", - securityOrigin: []const u8 = cdp.URLBase, - mimeType: []const u8 = "mimeType", + securityOrigin: []const u8, + mimeType: []const u8 = "text/html", adFrameStatus: struct { adFrameType: []const u8 = "none", } = .{}, - secureContextType: []const u8 = "Secure", + secureContextType: []const u8, crossOriginIsolatedContextType: []const u8 = "NotIsolated", gatedAPIFeatures: [][]const u8 = &[0][]const u8{}, - } = .{}, - } = .{}, + }, + }, childFrames: ?[]@This() = null, }; - return result(alloc, id, FrameTree, FrameTree{}, sessionID); + const frameTree = FrameTree{ + .frameTree = .{ + .frame = .{ + .id = ctx.state.frameID, + .url = ctx.state.url, + .securityOrigin = ctx.state.securityOrigin, + .secureContextType = ctx.state.secureContextType, + .loaderId = ctx.state.loaderID, + }, + }, + }; + return result(alloc, id, FrameTree, frameTree, sessionID); } fn setLifecycleEventsEnabled( diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 9436fe55..5a6ab47a 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -178,13 +178,20 @@ fn createTarget( _ = try getParams(alloc, Params, scanner); const sessionID = try cdp.getSessionID(scanner); + // change CDP state + ctx.state.frameID = TargetID; + ctx.state.url = "about:blank"; + ctx.state.securityOrigin = "://"; + ctx.state.secureContextType = "InsecureScheme"; + ctx.state.loaderID = "DD4A76F842AA389647D702B4D805F49A"; + // send attachToTarget event const attached = AttachToTarget{ .sessionId = ContextSessionID, .targetInfo = .{ - .targetId = TargetID, + .targetId = ctx.state.frameID, .title = "", - .url = "about:blank", + .url = ctx.state.url, .browserContextId = ContextID, }, .waitingForDebugger = true, diff --git a/src/server.zig b/src/server.zig index 79ebba04..105d5d97 100644 --- a/src/server.zig +++ b/src/server.zig @@ -25,6 +25,9 @@ pub const Cmd = struct { buf: []u8, // only for read operations err: ?Error = null, + // CDP + state: cdp.State = .{}, + // JS fields js_env: *public.Env, try_catch: public.TryCatch,