viewpoint_cdp/
lib.rs

1//! # Viewpoint CDP - Chrome DevTools Protocol Client
2//!
3//! Low-level Chrome DevTools Protocol (CDP) implementation over WebSocket,
4//! providing the foundational transport layer for Viewpoint browser automation.
5//!
6//! This crate handles:
7//! - WebSocket connection management to Chrome/Chromium browsers
8//! - CDP message serialization and deserialization
9//! - Command/response handling with async/await
10//! - Event subscription and streaming
11//! - Session management for multiple targets (pages, workers)
12//!
13//! ## Features
14//!
15//! - **Async WebSocket**: Non-blocking WebSocket communication with Chromium
16//! - **Type-safe Protocol**: Strongly-typed CDP domains (Page, Runtime, Network, etc.)
17//! - **Event Streaming**: Subscribe to CDP events with async streams
18//! - **Session Multiplexing**: Handle multiple page sessions over a single connection
19//! - **Error Handling**: Comprehensive error types for CDP and transport errors
20//!
21//! ## Quick Start
22//!
23//! ```no_run
24//! use viewpoint_cdp::{CdpConnection, protocol::target_domain::GetTargetsParams};
25//!
26//! # async fn example() -> Result<(), viewpoint_cdp::CdpError> {
27//! // Connect to a running Chrome instance
28//! let conn = CdpConnection::connect("ws://localhost:9222/devtools/browser/...").await?;
29//!
30//! // Send a CDP command
31//! let result: viewpoint_cdp::protocol::target_domain::GetTargetsResult =
32//!     conn.send_command("Target.getTargets", Some(GetTargetsParams::default()), None).await?;
33//!
34//! for target in result.target_infos {
35//!     println!("Target: {} - {}", target.target_type, target.url);
36//! }
37//! # Ok(())
38//! # }
39//! ```
40//!
41//! ## Discovering Chrome WebSocket URL
42//!
43//! Chrome exposes a JSON API for discovering the WebSocket URL:
44//!
45//! ```no_run
46//! use viewpoint_cdp::{discover_websocket_url, CdpConnectionOptions};
47//!
48//! # async fn example() -> Result<(), viewpoint_cdp::CdpError> {
49//! // Get WebSocket URL from HTTP endpoint
50//! let options = CdpConnectionOptions::default();
51//! let ws_url = discover_websocket_url("http://localhost:9222", &options).await?;
52//! println!("WebSocket URL: {}", ws_url);
53//! # Ok(())
54//! # }
55//! ```
56//!
57//! ## Sending Commands
58//!
59//! Commands are sent with optional session IDs for page-specific operations:
60//!
61//! ```no_run
62//! use viewpoint_cdp::CdpConnection;
63//! use viewpoint_cdp::protocol::page::NavigateParams;
64//!
65//! # async fn example(conn: &CdpConnection, session_id: &str) -> Result<(), viewpoint_cdp::CdpError> {
66//! // Browser-level command (no session)
67//! let version: viewpoint_cdp::BrowserVersion = conn.send_command(
68//!     "Browser.getVersion",
69//!     None::<()>,
70//!     None  // No session ID for browser-level commands
71//! ).await?;
72//!
73//! // Page-level command (with session)
74//! let result: viewpoint_cdp::protocol::page::NavigateResult = conn.send_command(
75//!     "Page.navigate",
76//!     Some(NavigateParams {
77//!         url: "https://example.com".to_string(),
78//!         referrer: None,
79//!         transition_type: None,
80//!         frame_id: None,
81//!     }),
82//!     Some(session_id)  // Target a specific page
83//! ).await?;
84//! # Ok(())
85//! # }
86//! ```
87//!
88//! ## Subscribing to Events
89//!
90//! Subscribe to CDP events using the event channel:
91//!
92//! ```no_run
93//! use viewpoint_cdp::{CdpConnection, CdpEvent};
94//!
95//! # async fn example(conn: &CdpConnection) -> Result<(), viewpoint_cdp::CdpError> {
96//! let mut events = conn.subscribe_events();
97//!
98//! // Process events in a loop
99//! while let Ok(event) = events.recv().await {
100//!     match &event.method[..] {
101//!         "Page.loadEventFired" => {
102//!             println!("Page loaded!");
103//!         }
104//!         "Network.requestWillBeSent" => {
105//!             println!("Network request: {:?}", event.params);
106//!         }
107//!         _ => {}
108//!     }
109//! }
110//! # Ok(())
111//! # }
112//! ```
113//!
114//! ## Connection Options
115//!
116//! Configure connection behavior with options:
117//!
118//! ```no_run
119//! use viewpoint_cdp::{CdpConnection, CdpConnectionOptions};
120//! use std::time::Duration;
121//!
122//! # async fn example() -> Result<(), viewpoint_cdp::CdpError> {
123//! let options = CdpConnectionOptions::new()
124//!     .timeout(Duration::from_secs(30));
125//!
126//! let conn = CdpConnection::connect_with_options(
127//!     "ws://localhost:9222/devtools/browser/...",
128//!     &options
129//! ).await?;
130//! # Ok(())
131//! # }
132//! ```
133//!
134//! ## Protocol Domains
135//!
136//! The [`protocol`] module contains typed definitions for CDP domains:
137//!
138//! - `target_domain` - Target management (pages, workers, service workers)
139//! - `page` - Page navigation and lifecycle
140//! - `runtime` - JavaScript execution
141//! - `network` - Network monitoring and interception
142//! - `fetch` - Network request interception
143//! - `dom` - DOM inspection and manipulation
144//! - `input` - Input device simulation
145//! - `emulation` - Device and media emulation
146//! - And many more...
147//!
148//! ## Error Handling
149//!
150//! The [`CdpError`] type covers all possible errors:
151//!
152//! ```no_run
153//! use viewpoint_cdp::{CdpConnection, CdpError};
154//!
155//! # async fn example() -> Result<(), CdpError> {
156//! let result = CdpConnection::connect("ws://invalid:9999/...").await;
157//!
158//! match result {
159//!     Ok(conn) => println!("Connected!"),
160//!     Err(CdpError::ConnectionFailed(e)) => println!("Connection error: {}", e),
161//!     Err(CdpError::Protocol { code, message }) => {
162//!         println!("CDP error {}: {}", code, message);
163//!     }
164//!     Err(e) => println!("Other error: {}", e),
165//! }
166//! # Ok(())
167//! # }
168//! ```
169//!
170//! ## Module Organization
171//!
172//! - [`connection`] - WebSocket connection management
173//! - [`transport`] - Message types and serialization
174//! - [`protocol`] - CDP domain type definitions
175//! - [`error`] - Error types
176
177pub mod connection;
178pub mod error;
179pub mod protocol;
180pub mod transport;
181
182pub use connection::{BrowserVersion, CdpConnection, CdpConnectionOptions, discover_websocket_url};
183pub use error::CdpError;
184pub use transport::{CdpEvent, CdpMessage, CdpRequest, CdpResponse};