From 2f3a5818596ff972e35755b2a4be3e61e55dfb91 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Tue, 1 Oct 2024 17:48:54 +0200 Subject: [PATCH] Add TODOs and comments Signed-off-by: Francis Bouvier --- src/cdp/browser.zig | 36 +++++++++++++++++++-------------- src/cdp/cdp.zig | 1 + src/cdp/emulation.zig | 6 ++++-- src/cdp/fetch.zig | 1 + src/cdp/network.zig | 1 + src/cdp/page.zig | 47 +++++++++++++++++++++++++++++++------------ src/cdp/runtime.zig | 2 ++ src/cdp/target.zig | 20 ++++++++++++++++-- 8 files changed, 82 insertions(+), 32 deletions(-) diff --git a/src/cdp/browser.zig b/src/cdp/browser.zig index e58a8bd2..e6cfa54e 100644 --- a/src/cdp/browser.zig +++ b/src/cdp/browser.zig @@ -30,6 +30,7 @@ pub fn browser( }; } +// TODO: hard coded data const ProtocolVersion = "1.3"; const Product = "Chrome/124.0.6367.29"; const Revision = "@9e6ded5ac1ff5e38d930ae52bd9aec09bd1a68e4"; @@ -42,32 +43,30 @@ fn getVersion( scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + + // input const msg = try getMsg(alloc, void, scanner); + // ouput const Res = struct { - protocolVersion: []const u8, - product: []const u8, - revision: []const u8, - userAgent: []const u8, - jsVersion: []const u8, + protocolVersion: []const u8 = ProtocolVersion, + product: []const u8 = Product, + revision: []const u8 = Revision, + userAgent: []const u8 = UserAgent, + jsVersion: []const u8 = JsVersion, }; - - const res = Res{ - .protocolVersion = ProtocolVersion, - .product = Product, - .revision = Revision, - .userAgent = UserAgent, - .jsVersion = JsVersion, - }; - return result(alloc, id orelse msg.id.?, Res, res, null); + return result(alloc, id orelse msg.id.?, Res, .{}, null); } +// TODO: noop method fn setDownloadBehavior( alloc: std.mem.Allocator, id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + + // input const Params = struct { behavior: []const u8, browserContextId: ?[]const u8 = null, @@ -75,9 +74,12 @@ fn setDownloadBehavior( eventsEnabled: ?bool = null, }; const msg = try getMsg(alloc, Params, scanner); + + // output return result(alloc, id orelse msg.id.?, null, null, null); } +// TODO: hard coded ID const DevToolsWindowID = 1923710101; fn getWindowForTarget( @@ -108,13 +110,17 @@ fn getWindowForTarget( return result(alloc, id orelse msg.id.?, Resp, Resp{}, msg.sessionID.?); } +// TODO: noop method fn setWindowBounds( alloc: std.mem.Allocator, id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { - // NOTE: noop + + // input const msg = try cdp.getMsg(alloc, void, scanner); + + // output return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 87f1477b..8dbfde52 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -286,6 +286,7 @@ pub fn getMsg( // Common // ------ +// TODO: hard coded IDs pub const BrowserSessionID = "9559320D92474062597D9875C664CAC0"; pub const ContextSessionID = "4FDC2CB760A23A220497A05C95417CF4"; pub const URLBase = "chrome://newtab/"; diff --git a/src/cdp/emulation.zig b/src/cdp/emulation.zig index 1ff18e2e..7bdf7842 100644 --- a/src/cdp/emulation.zig +++ b/src/cdp/emulation.zig @@ -36,6 +36,7 @@ const MediaFeature = struct { value: []const u8, }; +// TODO: noop method fn setEmulatedMedia( alloc: std.mem.Allocator, id: ?u16, @@ -51,10 +52,10 @@ fn setEmulatedMedia( const msg = try getMsg(alloc, Params, scanner); // output - // TODO: dummy return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } +// TODO: noop method fn setFocusEmulationEnabled( alloc: std.mem.Allocator, id: ?u16, @@ -69,10 +70,10 @@ fn setFocusEmulationEnabled( const msg = try getMsg(alloc, Params, scanner); // output - // TODO: dummy return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } +// TODO: noop method fn setDeviceMetricsOverride( alloc: std.mem.Allocator, id: ?u16, @@ -87,6 +88,7 @@ fn setDeviceMetricsOverride( return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } +// TODO: noop method fn setTouchEmulationEnabled( alloc: std.mem.Allocator, id: ?u16, diff --git a/src/cdp/fetch.zig b/src/cdp/fetch.zig index ca327e40..558552a4 100644 --- a/src/cdp/fetch.zig +++ b/src/cdp/fetch.zig @@ -25,6 +25,7 @@ pub fn fetch( }; } +// TODO: noop method fn disable( alloc: std.mem.Allocator, id: ?u16, diff --git a/src/cdp/network.zig b/src/cdp/network.zig index 7d55577d..c0c79213 100644 --- a/src/cdp/network.zig +++ b/src/cdp/network.zig @@ -38,6 +38,7 @@ fn enable( return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } +// TODO: noop method fn setCacheDisabled( alloc: std.mem.Allocator, id: ?u16, diff --git a/src/cdp/page.zig b/src/cdp/page.zig index 38b3b8f4..95d6da08 100644 --- a/src/cdp/page.zig +++ b/src/cdp/page.zig @@ -117,6 +117,7 @@ const LifecycleEvent = struct { timestamp: f32 = undefined, }; +// TODO: hard coded method fn addScriptToEvaluateOnNewDocument( alloc: std.mem.Allocator, id: ?u16, @@ -140,6 +141,7 @@ fn addScriptToEvaluateOnNewDocument( return result(alloc, id orelse msg.id.?, Res, Res{}, msg.sessionID); } +// TODO: hard coded method fn createIsolatedWorld( alloc: std.mem.Allocator, id: ?u16, @@ -202,6 +204,7 @@ fn navigate( // change state ctx.state.url = params.url; + // TODO: hard coded ID ctx.state.loaderID = "AF8667A203C5392DBE9AC290044AA4C2"; var life_event = LifecycleEvent{ @@ -211,6 +214,7 @@ fn navigate( var ts_event: cdp.TimestampEvent = undefined; // frameStartedLoading event + // TODO: event partially hard coded const FrameStartedLoading = struct { frameId: []const u8, }; @@ -250,7 +254,10 @@ fn navigate( std.log.debug("res {s}", .{res}); try server.sendSync(ctx, res); - // Send clear runtime contexts event (noop) + // TODO: at this point do we need async the following actions to be async? + + // Send Runtime.executionContextsCleared event + // TODO: noop event, we have no env context at this point, is it necesarry? try sendEvent(alloc, ctx, "Runtime.executionContextsCleared", void, {}, msg.sessionID); // Launch navigate @@ -258,12 +265,30 @@ fn navigate( ctx.state.executionContextId += 1; const auxData = try std.fmt.allocPrint( alloc, + // TODO: we assume this is the default web page, is it right? "{{\"isDefault\":true,\"type\":\"default\",\"frameId\":\"{s}\"}}", .{ctx.state.frameID}, ); defer alloc.free(auxData); _ = try p.navigate(params.url, auxData); + // Events + + // lifecycle init event + // TODO: partially hard coded + if (ctx.state.page_life_cycle_events) { + life_event.name = "init"; + life_event.timestamp = 343721.796037; + try sendEvent( + alloc, + ctx, + "Page.lifecycleEvent", + LifecycleEvent, + life_event, + msg.sessionID, + ); + } + // frameNavigated event const FrameNavigated = struct { frame: Frame, @@ -286,20 +311,9 @@ fn navigate( frame_navigated, msg.sessionID, ); - if (ctx.state.page_life_cycle_events) { - life_event.name = "load"; - life_event.timestamp = 343721.824655; - try sendEvent( - alloc, - ctx, - "Page.lifecycleEvent", - LifecycleEvent, - life_event, - msg.sessionID, - ); - } // domContentEventFired event + // TODO: partially hard coded ts_event = .{ .timestamp = 343721.803338 }; try sendEvent( alloc, @@ -309,6 +323,9 @@ fn navigate( ts_event, msg.sessionID, ); + + // lifecycle DOMContentLoaded event + // TODO: partially hard coded if (ctx.state.page_life_cycle_events) { life_event.name = "DOMContentLoaded"; life_event.timestamp = 343721.803338; @@ -323,6 +340,7 @@ fn navigate( } // loadEventFired event + // TODO: partially hard coded ts_event = .{ .timestamp = 343721.824655 }; try sendEvent( alloc, @@ -332,6 +350,9 @@ fn navigate( ts_event, msg.sessionID, ); + + // lifecycle DOMContentLoaded event + // TODO: partially hard coded if (ctx.state.page_life_cycle_events) { life_event.name = "load"; life_event.timestamp = 343721.824655; diff --git a/src/cdp/runtime.zig b/src/cdp/runtime.zig index 8554cdcc..63199ffc 100644 --- a/src/cdp/runtime.zig +++ b/src/cdp/runtime.zig @@ -140,6 +140,8 @@ pub fn executionContextCreated( try cdp.sendEvent(alloc, ctx, "Runtime.executionContextCreated", Params, params, sessionID); } +// TODO: noop method +// should we be passing this also to the JS Inspector? fn runIfWaitingForDebugger( alloc: std.mem.Allocator, id: ?u16, diff --git a/src/cdp/target.zig b/src/cdp/target.zig index 9e7175aa..0531ca26 100644 --- a/src/cdp/target.zig +++ b/src/cdp/target.zig @@ -39,18 +39,23 @@ pub fn target( }; } +// TODO: hard coded IDs const PageTargetID = "CFCD6EC01573CF29BB638E9DC0F52DDC"; const BrowserTargetID = "2d2bdef9-1c95-416f-8c0e-83f3ab73a30c"; const BrowserContextID = "65618675CB7D3585A95049E9DFE95EA9"; +// TODO: noop method fn setDiscoverTargets( alloc: std.mem.Allocator, id: ?u16, scanner: *std.json.Scanner, _: *Ctx, ) ![]const u8 { + + // input const msg = try getMsg(alloc, void, scanner); + // output return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } @@ -73,6 +78,7 @@ const TargetFilter = struct { exclude: ?bool = null, }; +// TODO: noop method fn setAutoAttach( alloc: std.mem.Allocator, id: ?u16, @@ -90,6 +96,7 @@ fn setAutoAttach( const msg = try getMsg(alloc, Params, scanner); std.log.debug("params {any}", .{msg.params}); + // attachedToTarget event if (msg.sessionID == null) { const attached = AttachToTarget{ .sessionId = cdp.BrowserSessionID, @@ -103,6 +110,7 @@ fn setAutoAttach( try cdp.sendEvent(alloc, ctx, "Target.attachedToTarget", AttachToTarget, attached, null); } + // output return result(alloc, id orelse msg.id.?, null, null, msg.sessionID); } @@ -142,12 +150,15 @@ fn getTargetInfo( // Browser context are not handled and not in the roadmap for now // The following methods are "fake" +// TODO: noop method fn getBrowserContexts( alloc: std.mem.Allocator, id: ?u16, scanner: *std.json.Scanner, ctx: *Ctx, ) ![]const u8 { + + // input const msg = try getMsg(alloc, void, scanner); // ouptut @@ -167,6 +178,7 @@ fn getBrowserContexts( const ContextID = "22648B09EDCCDD11109E2D4FEFBE4F89"; +// TODO: noop method fn createBrowserContext( alloc: std.mem.Allocator, id: ?u16, @@ -205,10 +217,13 @@ fn disposeBrowserContext( }; const msg = try getMsg(alloc, Params, scanner); + // output return result(alloc, id orelse msg.id.?, null, {}, null); } +// TODO: hard coded IDs const TargetID = "57356548460A8F29706A2ADF14316298"; +const LoaderID = "DD4A76F842AA389647D702B4D805F49A"; fn createTarget( alloc: std.mem.Allocator, @@ -235,7 +250,7 @@ fn createTarget( ctx.state.url = "about:blank"; ctx.state.securityOrigin = "://"; ctx.state.secureContextType = "InsecureScheme"; - ctx.state.loaderID = "DD4A76F842AA389647D702B4D805F49A"; + ctx.state.loaderID = LoaderID; // send attachToTarget event const attached = AttachToTarget{ @@ -277,7 +292,7 @@ fn closeTarget( const res = try result(alloc, id orelse msg.id.?, Resp, Resp{}, null); try server.sendSync(ctx, res); - // events + // Inspector.detached event const InspectorDetached = struct { reason: []const u8 = "Render process gone.", }; @@ -290,6 +305,7 @@ fn closeTarget( msg.sessionID orelse cdp.ContextSessionID, ); + // detachedFromTarget event const TargetDetached = struct { sessionId: []const u8, targetId: []const u8,