#pragma once // Chrome DevTools Protocol over the sandbox's CDP WebSocket (computer use). // Connects like the Google SDK's `generate_browser_ws_headers` plus // Playwright's connect_over_cdp: GET /cdp_ws_endpoint on the HTTP data plane // gives a path, the WebSocket URL is the data plane host plus that path, and // the JWT travels in the Sec-WebSocket-Protocol header // ("v1.stream, , , 9222"). Messages are CDP JSON-RPC // ({"id","method","params"[,"sessionId"]}), see // https://chromedevtools.github.io/devtools-protocol/ (flat session mode). // // Reuses the RFC 6455 client of src/google/live (WsClient, open_stream). // Hard caps: message size, buffered events, call time, reconnects, and a // floor between connection attempts. One thread uses a session at a time. #include #include #include #include #include "google/live/ws_client.h" #include "google/sandbox/data_plane.h" namespace molo::google::sandbox { struct CdpOptions { int connect_timeout_ms = 0; // 0: SandboxCaps default int call_timeout_ms = 0; // 0: SandboxCaps default std::string browser_port = "9222"; }; class CdpSession { public: CdpSession(std::shared_ptr plane, CdpOptions options = {}); ~CdpSession(); CdpSession(const CdpSession&) = delete; CdpSession& operator=(const CdpSession&) = delete; // Resolves the endpoint, mints a JWT and runs the upgrade. Idempotent. void connect(); bool connected() const; // Sends a polite close and drops the socket. void close(); // Sends one CDP command and waits for its reply (matched by id). Returns the // "result" object. A CDP "error" object throws GoogleError 502 // CDP_ERROR with Chrome's message. A lost socket before the send is // reconnected (fresh JWT, capped); a loss mid-call throws CDP_CONNECTION_LOST // and the next call reconnects, because commands are never replayed. Session // ids from before a reconnect are void. clients::body::Json call(const std::string& method, const clients::body::Json& params, const std::string& session_id = "", int timeout_ms = 0); // Browser-level helper: Target.getTargets, Target.attachToTarget // {flatten:true} for the first "page" target; returns the sessionId. std::string attach_first_page(); // Events (messages without id) seen while waiting, oldest first; at most // kMaxCdpEvents are kept, older ones are dropped and counted. std::vector take_events(); // Waits for an event with this method (already buffered or newly read). clients::body::Json wait_event(const std::string& method, int timeout_ms = 0); int connects() const { return connects_; } int reconnects() const { return reconnects_; } std::size_t dropped_events() const { return dropped_events_; } const std::string& endpoint_path() const { return endpoint_path_; } static void validate_session_id(const std::string& id); static clients::body::Json request(long id, const std::string& method, const clients::body::Json& params, const std::string& session_id); private: void open_socket(); void ensure_connected(); bool pump(int budget_ms, long wanted_id, clients::body::Json* reply); void push_event(clients::body::Json event); std::shared_ptr plane_; CdpOptions options_; std::unique_ptr ws_; std::string endpoint_path_; long next_id_ = 1; int connects_ = 0; int reconnects_ = 0; long last_connect_ms_ = 0; std::deque events_; std::size_t dropped_events_ = 0; }; } // namespace molo::google::sandbox