Skip to main content

spider_browser/protocol/
protocol_adapter.rs

1//! Unified protocol adapter — wraps either CDPSession or BiDiSession.
2
3use crate::errors::{Result, SpiderError};
4use crate::events::SpiderEventEmitter;
5use crate::protocol::bidi_session::BiDiSession;
6use crate::protocol::cdp_session::CDPSession;
7use crate::protocol::types::get_key_params;
8use serde_json::{json, Value};
9use std::sync::Arc;
10use tokio::sync::mpsc;
11use tracing::{debug, info};
12
13/// Protocol adapter options.
14pub struct ProtocolAdapterOptions {
15    pub command_timeout_ms: Option<u64>,
16}
17
18/// Determines which protocol to use.
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum ProtocolType {
21    Cdp,
22    Bidi,
23    Auto,
24}
25
26/// Unified protocol interface wrapping CDPSession or BiDiSession.
27pub struct ProtocolAdapter {
28    cdp: Option<CDPSession>,
29    bidi: Option<BiDiSession>,
30    protocol: ProtocolType,
31    send_tx: mpsc::UnboundedSender<String>,
32    emitter: SpiderEventEmitter,
33    command_timeout_ms: u64,
34}
35
36impl ProtocolAdapter {
37    pub fn new(
38        send_tx: mpsc::UnboundedSender<String>,
39        emitter: SpiderEventEmitter,
40        browser: &str,
41        opts: Option<ProtocolAdapterOptions>,
42    ) -> Self {
43        let timeout = opts.as_ref()
44            .and_then(|o| o.command_timeout_ms)
45            .unwrap_or(30_000);
46
47        let (cdp, bidi, protocol) = if browser == "auto" {
48            (None, None, ProtocolType::Auto)
49        } else if browser == "firefox" {
50            (None, Some(BiDiSession::new(send_tx.clone(), timeout)), ProtocolType::Bidi)
51        } else {
52            (Some(CDPSession::new(send_tx.clone(), timeout)), None, ProtocolType::Cdp)
53        };
54
55        Self {
56            cdp,
57            bidi,
58            protocol,
59            send_tx,
60            emitter,
61            command_timeout_ms: timeout,
62        }
63    }
64
65    pub fn protocol_type(&self) -> ProtocolType {
66        self.protocol
67    }
68
69    /// Route incoming WebSocket messages to the right session.
70    pub fn route_message(&self, data: &str) {
71        // Check for Spider.* events first
72        if let Ok(msg) = serde_json::from_str::<Value>(data) {
73            if let Some(method) = msg.get("method").and_then(|v| v.as_str()) {
74                if method.starts_with("Spider.") {
75                    self.handle_spider_event(method, msg.get("params").cloned().unwrap_or(json!({})));
76                    return;
77                }
78            }
79        }
80
81        if let Some(ref cdp) = self.cdp {
82            cdp.handle_message(data);
83        } else if let Some(ref bidi) = self.bidi {
84            bidi.handle_message(data);
85        }
86    }
87
88    fn handle_spider_event(&self, method: &str, params: Value) {
89        match method {
90            "Spider.captchaDetected" => {
91                self.emitter.emit("captcha.detected", params);
92            }
93            "Spider.captchaSolving" => {
94                self.emitter.emit("captcha.solving", params);
95            }
96            "Spider.captchaSolved" => {
97                self.emitter.emit("captcha.solved", params);
98            }
99            "Spider.captchaFailed" => {
100                self.emitter.emit("captcha.failed", params);
101            }
102            _ => {
103                debug!("unhandled Spider event: {}", method);
104            }
105        }
106    }
107
108    /// Initialize the protocol session.
109    pub async fn init(&mut self) -> Result<()> {
110        if self.protocol == ProtocolType::Auto {
111            self.auto_detect_and_init().await?;
112            return Ok(());
113        }
114
115        if let Some(ref cdp) = self.cdp {
116            cdp.attach_to_page().await?;
117        } else if let Some(ref bidi) = self.bidi {
118            bidi.get_or_create_context().await?;
119        }
120        Ok(())
121    }
122
123    async fn auto_detect_and_init(&mut self) -> Result<()> {
124        // Try CDP first
125        let cdp = CDPSession::new(self.send_tx.clone(), self.command_timeout_ms);
126        match cdp.attach_to_page().await {
127            Ok(_) => {
128                self.cdp = Some(cdp);
129                self.protocol = ProtocolType::Cdp;
130                info!("auto-detected CDP protocol");
131                return Ok(());
132            }
133            Err(_) => {
134                cdp.destroy();
135            }
136        }
137
138        // Try BiDi
139        let bidi = BiDiSession::new(self.send_tx.clone(), self.command_timeout_ms);
140        bidi.get_or_create_context().await?;
141        self.bidi = Some(bidi);
142        self.protocol = ProtocolType::Bidi;
143        info!("auto-detected BiDi protocol");
144        Ok(())
145    }
146
147    // ------------------------------------------------------------------
148    // Unified interface
149    // ------------------------------------------------------------------
150
151    pub async fn navigate(&self, url: &str) -> Result<()> {
152        if let Some(ref cdp) = self.cdp {
153            cdp.navigate(url).await
154        } else if let Some(ref bidi) = self.bidi {
155            bidi.navigate(url).await
156        } else {
157            Err(SpiderError::Protocol("No protocol session".into()))
158        }
159    }
160
161    pub async fn navigate_fast(&self, url: &str) -> Result<()> {
162        if let Some(ref cdp) = self.cdp {
163            cdp.navigate_fast(url).await
164        } else if let Some(ref bidi) = self.bidi {
165            bidi.navigate(url).await
166        } else {
167            Err(SpiderError::Protocol("No protocol session".into()))
168        }
169    }
170
171    pub async fn navigate_dom(&self, url: &str) -> Result<()> {
172        if let Some(ref cdp) = self.cdp {
173            cdp.navigate_dom(url).await
174        } else if let Some(ref bidi) = self.bidi {
175            bidi.navigate(url).await
176        } else {
177            Err(SpiderError::Protocol("No protocol session".into()))
178        }
179    }
180
181    pub async fn get_html(&self) -> Result<String> {
182        if let Some(ref cdp) = self.cdp {
183            cdp.get_html().await
184        } else if let Some(ref bidi) = self.bidi {
185            bidi.get_html().await
186        } else {
187            Err(SpiderError::Protocol("No protocol session".into()))
188        }
189    }
190
191    pub async fn evaluate(&self, expression: &str) -> Result<Value> {
192        if let Some(ref cdp) = self.cdp {
193            cdp.evaluate(expression).await
194        } else if let Some(ref bidi) = self.bidi {
195            bidi.evaluate(expression).await
196        } else {
197            Err(SpiderError::Protocol("No protocol session".into()))
198        }
199    }
200
201    pub async fn capture_screenshot(&self) -> Result<String> {
202        if let Some(ref cdp) = self.cdp {
203            cdp.capture_screenshot().await
204        } else if let Some(ref bidi) = self.bidi {
205            bidi.capture_screenshot().await
206        } else {
207            Err(SpiderError::Protocol("No protocol session".into()))
208        }
209    }
210
211    pub async fn click_point(&self, x: f64, y: f64) -> Result<()> {
212        if let Some(ref cdp) = self.cdp {
213            cdp.click_point(x, y).await
214        } else if let Some(ref bidi) = self.bidi {
215            bidi.click_point(x, y).await
216        } else {
217            Err(SpiderError::Protocol("No protocol session".into()))
218        }
219    }
220
221    pub async fn right_click_point(&self, x: f64, y: f64) -> Result<()> {
222        if let Some(ref cdp) = self.cdp {
223            cdp.right_click_point(x, y).await
224        } else if let Some(ref bidi) = self.bidi {
225            bidi.perform_actions(json!([{
226                "type": "pointer", "id": "mouse",
227                "actions": [
228                    {"type": "pointerMove", "x": x.round() as i64, "y": y.round() as i64},
229                    {"type": "pointerDown", "button": 2},
230                    {"type": "pointerUp", "button": 2},
231                ]
232            }])).await
233        } else {
234            Err(SpiderError::Protocol("No protocol session".into()))
235        }
236    }
237
238    pub async fn double_click_point(&self, x: f64, y: f64) -> Result<()> {
239        if let Some(ref cdp) = self.cdp {
240            cdp.double_click_point(x, y).await
241        } else if let Some(ref bidi) = self.bidi {
242            bidi.perform_actions(json!([{
243                "type": "pointer", "id": "mouse",
244                "actions": [
245                    {"type": "pointerMove", "x": x.round() as i64, "y": y.round() as i64},
246                    {"type": "pointerDown", "button": 0},
247                    {"type": "pointerUp", "button": 0},
248                    {"type": "pointerDown", "button": 0},
249                    {"type": "pointerUp", "button": 0},
250                ]
251            }])).await
252        } else {
253            Err(SpiderError::Protocol("No protocol session".into()))
254        }
255    }
256
257    pub async fn click_hold_point(&self, x: f64, y: f64, hold_ms: u64) -> Result<()> {
258        if let Some(ref cdp) = self.cdp {
259            cdp.click_hold_point(x, y, hold_ms).await
260        } else if let Some(ref bidi) = self.bidi {
261            bidi.perform_actions(json!([{
262                "type": "pointer", "id": "mouse",
263                "actions": [
264                    {"type": "pointerMove", "x": x.round() as i64, "y": y.round() as i64},
265                    {"type": "pointerDown", "button": 0},
266                    {"type": "pause", "duration": hold_ms},
267                    {"type": "pointerUp", "button": 0},
268                ]
269            }])).await
270        } else {
271            Err(SpiderError::Protocol("No protocol session".into()))
272        }
273    }
274
275    pub async fn hover_point(&self, x: f64, y: f64) -> Result<()> {
276        if let Some(ref cdp) = self.cdp {
277            cdp.hover_point(x, y).await
278        } else if let Some(ref bidi) = self.bidi {
279            bidi.perform_actions(json!([{
280                "type": "pointer", "id": "mouse",
281                "actions": [{"type": "pointerMove", "x": x.round() as i64, "y": y.round() as i64}]
282            }])).await
283        } else {
284            Err(SpiderError::Protocol("No protocol session".into()))
285        }
286    }
287
288    pub async fn drag_point(&self, from_x: f64, from_y: f64, to_x: f64, to_y: f64) -> Result<()> {
289        if let Some(ref cdp) = self.cdp {
290            cdp.drag_point(from_x, from_y, to_x, to_y).await
291        } else if let Some(ref bidi) = self.bidi {
292            let steps = 10;
293            let mut actions = vec![
294                json!({"type": "pointerMove", "x": from_x.round() as i64, "y": from_y.round() as i64}),
295                json!({"type": "pointerDown", "button": 0}),
296            ];
297            for i in 1..=steps {
298                let t = i as f64 / steps as f64;
299                actions.push(json!({
300                    "type": "pointerMove",
301                    "x": (from_x + (to_x - from_x) * t).round() as i64,
302                    "y": (from_y + (to_y - from_y) * t).round() as i64,
303                    "duration": 16,
304                }));
305            }
306            actions.push(json!({"type": "pointerUp", "button": 0}));
307            bidi.perform_actions(json!([{"type": "pointer", "id": "mouse", "actions": actions}])).await
308        } else {
309            Err(SpiderError::Protocol("No protocol session".into()))
310        }
311    }
312
313    pub async fn insert_text(&self, text: &str) -> Result<()> {
314        if let Some(ref cdp) = self.cdp {
315            cdp.insert_text(text).await
316        } else if let Some(ref bidi) = self.bidi {
317            bidi.insert_text(text).await
318        } else {
319            Err(SpiderError::Protocol("No protocol session".into()))
320        }
321    }
322
323    pub async fn press_key(&self, key_name: &str) -> Result<()> {
324        let (key, code, key_code) = get_key_params(key_name);
325        if let Some(ref cdp) = self.cdp {
326            cdp.press_key(key, code, key_code).await
327        } else if let Some(ref bidi) = self.bidi {
328            bidi.perform_actions(json!([{
329                "type": "key", "id": "keyboard",
330                "actions": [
331                    {"type": "keyDown", "value": key},
332                    {"type": "keyUp", "value": key},
333                ]
334            }])).await
335        } else {
336            Err(SpiderError::Protocol("No protocol session".into()))
337        }
338    }
339
340    pub async fn key_down(&self, key_name: &str) -> Result<()> {
341        let (key, code, key_code) = get_key_params(key_name);
342        if let Some(ref cdp) = self.cdp {
343            cdp.key_down(key, code, key_code).await
344        } else if let Some(ref bidi) = self.bidi {
345            bidi.perform_actions(json!([{
346                "type": "key", "id": "keyboard",
347                "actions": [{"type": "keyDown", "value": key}]
348            }])).await
349        } else {
350            Err(SpiderError::Protocol("No protocol session".into()))
351        }
352    }
353
354    pub async fn key_up(&self, key_name: &str) -> Result<()> {
355        let (key, code, key_code) = get_key_params(key_name);
356        if let Some(ref cdp) = self.cdp {
357            cdp.key_up(key, code, key_code).await
358        } else if let Some(ref bidi) = self.bidi {
359            bidi.perform_actions(json!([{
360                "type": "key", "id": "keyboard",
361                "actions": [{"type": "keyUp", "value": key}]
362            }])).await
363        } else {
364            Err(SpiderError::Protocol("No protocol session".into()))
365        }
366    }
367
368    pub async fn set_viewport(&self, width: u32, height: u32, dpr: f64, mobile: bool) -> Result<()> {
369        if let Some(ref cdp) = self.cdp {
370            cdp.set_viewport(width, height, dpr, mobile).await
371        } else if let Some(ref bidi) = self.bidi {
372            bidi.evaluate(&format!("window.resizeTo({width}, {height})")).await?;
373            Ok(())
374        } else {
375            Err(SpiderError::Protocol("No protocol session".into()))
376        }
377    }
378
379    pub fn on_protocol_event(&self, method: &str, handler: Arc<dyn Fn(Value) + Send + Sync>) {
380        if let Some(ref cdp) = self.cdp {
381            cdp.on(method, handler);
382        } else if let Some(ref bidi) = self.bidi {
383            bidi.on(method, handler);
384        }
385    }
386
387    pub fn destroy(&self) {
388        if let Some(ref cdp) = self.cdp {
389            cdp.destroy();
390        }
391        if let Some(ref bidi) = self.bidi {
392            bidi.destroy();
393        }
394    }
395}