viewpoint-core 0.3.4

High-level browser automation API for Viewpoint
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Page event handling for dialogs, downloads, file choosers, console, and errors.
//!
//! This module provides the event management infrastructure for page-level events.

// Allow dead code for event scaffolding (various specs)

mod download_handling;
mod event_listener;
mod page_handlers;
mod types;

use std::collections::HashMap;
use std::future::Future;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::{Mutex, RwLock, oneshot};
use tracing::{debug, warn};
use viewpoint_cdp::CdpConnection;

use super::console::ConsoleMessage;
use super::dialog::Dialog;
use super::download::Download;
use super::file_chooser::FileChooser;
use super::frame::Frame;
use super::page_error::PageError as PageErrorInfo;
use crate::error::PageError;

pub use types::{
    ConsoleHandler, DialogHandler, DownloadHandler, FileChooserHandler, FrameAttachedHandler,
    FrameDetachedHandler, FrameNavigatedHandler, PageErrorHandler,
};

use download_handling::DownloadTracker;

/// Page event manager for handling CDP events.
pub struct PageEventManager {
    /// CDP connection.
    connection: Arc<CdpConnection>,
    /// Session ID.
    session_id: String,
    /// Dialog handler.
    dialog_handler: Arc<RwLock<Option<DialogHandler>>>,
    /// Download handler.
    download_handler: Arc<RwLock<Option<DownloadHandler>>>,
    /// File chooser handler.
    file_chooser_handler: Arc<RwLock<Option<FileChooserHandler>>>,
    /// Console message handler.
    console_handler: Arc<RwLock<Option<ConsoleHandler>>>,
    /// Page error handler.
    pageerror_handler: Arc<RwLock<Option<PageErrorHandler>>>,
    /// Frame attached handler.
    frameattached_handler: Arc<RwLock<Option<FrameAttachedHandler>>>,
    /// Frame navigated handler.
    framenavigated_handler: Arc<RwLock<Option<FrameNavigatedHandler>>>,
    /// Frame detached handler.
    framedetached_handler: Arc<RwLock<Option<FrameDetachedHandler>>>,
    /// Active downloads.
    downloads: Arc<Mutex<HashMap<String, DownloadTracker>>>,
    /// Download directory.
    download_dir: PathBuf,
    /// Whether file chooser interception is enabled.
    file_chooser_intercepted: Arc<RwLock<bool>>,
    /// One-shot sender for wait_for_dialog.
    wait_for_dialog_tx: Arc<Mutex<Option<oneshot::Sender<Dialog>>>>,
    /// One-shot sender for wait_for_download.
    wait_for_download_tx: Arc<Mutex<Option<oneshot::Sender<Download>>>>,
    /// One-shot sender for wait_for_file_chooser.
    wait_for_file_chooser_tx: Arc<Mutex<Option<oneshot::Sender<FileChooser>>>>,
    /// One-shot sender for wait_for_console.
    wait_for_console_tx: Arc<Mutex<Option<oneshot::Sender<ConsoleMessage>>>>,
    /// One-shot sender for wait_for_pageerror.
    wait_for_pageerror_tx: Arc<Mutex<Option<oneshot::Sender<PageErrorInfo>>>>,
}

// Manual Debug implementation since handlers don't implement Debug
impl std::fmt::Debug for PageEventManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PageEventManager")
            .field("session_id", &self.session_id)
            .field("download_dir", &self.download_dir)
            .finish_non_exhaustive()
    }
}

impl PageEventManager {
    /// Create a new page event manager.
    pub fn new(connection: Arc<CdpConnection>, session_id: String) -> Self {
        let download_dir = std::env::temp_dir().join("viewpoint-downloads");
        let manager = Self {
            connection: connection.clone(),
            session_id: session_id.clone(),
            dialog_handler: Arc::new(RwLock::new(None)),
            download_handler: Arc::new(RwLock::new(None)),
            file_chooser_handler: Arc::new(RwLock::new(None)),
            console_handler: Arc::new(RwLock::new(None)),
            pageerror_handler: Arc::new(RwLock::new(None)),
            frameattached_handler: Arc::new(RwLock::new(None)),
            framenavigated_handler: Arc::new(RwLock::new(None)),
            framedetached_handler: Arc::new(RwLock::new(None)),
            downloads: Arc::new(Mutex::new(HashMap::new())),
            download_dir,
            file_chooser_intercepted: Arc::new(RwLock::new(false)),
            wait_for_dialog_tx: Arc::new(Mutex::new(None)),
            wait_for_download_tx: Arc::new(Mutex::new(None)),
            wait_for_file_chooser_tx: Arc::new(Mutex::new(None)),
            wait_for_console_tx: Arc::new(Mutex::new(None)),
            wait_for_pageerror_tx: Arc::new(Mutex::new(None)),
        };

        // Start the event listener
        manager.start_event_listener();

        manager
    }

    /// Start the background event listener for console, pageerror, dialog, frame, and download events.
    fn start_event_listener(&self) {
        event_listener::start_event_listener(
            self.connection.clone(),
            self.session_id.clone(),
            self.console_handler.clone(),
            self.pageerror_handler.clone(),
            self.dialog_handler.clone(),
            self.frameattached_handler.clone(),
            self.framenavigated_handler.clone(),
            self.framedetached_handler.clone(),
            self.wait_for_console_tx.clone(),
            self.wait_for_pageerror_tx.clone(),
            self.wait_for_dialog_tx.clone(),
            // Download handling
            self.download_handler.clone(),
            self.downloads.clone(),
            self.wait_for_download_tx.clone(),
        );
    }

    /// Set the dialog handler.
    pub async fn set_dialog_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(Dialog) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<(), PageError>> + Send + 'static,
    {
        let mut dialog_handler = self.dialog_handler.write().await;
        *dialog_handler = Some(Box::new(move |dialog| Box::pin(handler(dialog))));
    }

    /// Remove the dialog handler.
    pub async fn remove_dialog_handler(&self) {
        let mut dialog_handler = self.dialog_handler.write().await;
        *dialog_handler = None;
    }

    /// Set the download handler.
    pub async fn set_download_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(Download) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut download_handler = self.download_handler.write().await;
        *download_handler = Some(Box::new(move |download| Box::pin(handler(download))));
    }

    /// Set the file chooser handler.
    pub async fn set_file_chooser_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(FileChooser) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut file_chooser_handler = self.file_chooser_handler.write().await;
        *file_chooser_handler = Some(Box::new(move |chooser| Box::pin(handler(chooser))));
    }

    /// Set the console handler.
    pub async fn set_console_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(ConsoleMessage) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut console_handler = self.console_handler.write().await;
        *console_handler = Some(Box::new(move |message| Box::pin(handler(message))));
    }

    /// Remove the console handler.
    pub async fn remove_console_handler(&self) {
        let mut console_handler = self.console_handler.write().await;
        *console_handler = None;
    }

    /// Set the page error handler.
    pub async fn set_pageerror_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(PageErrorInfo) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut pageerror_handler = self.pageerror_handler.write().await;
        *pageerror_handler = Some(Box::new(move |error| Box::pin(handler(error))));
    }

    /// Remove the page error handler.
    pub async fn remove_pageerror_handler(&self) {
        let mut pageerror_handler = self.pageerror_handler.write().await;
        *pageerror_handler = None;
    }

    /// Set the frame attached handler.
    pub async fn set_frameattached_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(Frame) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut frameattached_handler = self.frameattached_handler.write().await;
        *frameattached_handler = Some(Box::new(move |frame| Box::pin(handler(frame))));
    }

    /// Remove the frame attached handler.
    pub async fn remove_frameattached_handler(&self) {
        let mut frameattached_handler = self.frameattached_handler.write().await;
        *frameattached_handler = None;
    }

    /// Set the frame navigated handler.
    pub async fn set_framenavigated_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(Frame) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut framenavigated_handler = self.framenavigated_handler.write().await;
        *framenavigated_handler = Some(Box::new(move |frame| Box::pin(handler(frame))));
    }

    /// Remove the frame navigated handler.
    pub async fn remove_framenavigated_handler(&self) {
        let mut framenavigated_handler = self.framenavigated_handler.write().await;
        *framenavigated_handler = None;
    }

    /// Set the frame detached handler.
    pub async fn set_framedetached_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(Frame) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = ()> + Send + 'static,
    {
        let mut framedetached_handler = self.framedetached_handler.write().await;
        *framedetached_handler = Some(Box::new(move |frame| Box::pin(handler(frame))));
    }

    /// Remove the frame detached handler.
    pub async fn remove_framedetached_handler(&self) {
        let mut framedetached_handler = self.framedetached_handler.write().await;
        *framedetached_handler = None;
    }

    /// Handle frame attached event externally.
    pub async fn handle_frame_attached(&self, frame: Frame) {
        let handler = self.frameattached_handler.read().await;
        if let Some(ref h) = *handler {
            h(frame).await;
        }
    }

    /// Handle frame navigated event externally.
    pub async fn handle_frame_navigated(&self, frame: Frame) {
        let handler = self.framenavigated_handler.read().await;
        if let Some(ref h) = *handler {
            h(frame).await;
        }
    }

    /// Handle frame detached event externally.
    pub async fn handle_frame_detached(&self, frame: Frame) {
        let handler = self.framedetached_handler.read().await;
        if let Some(ref h) = *handler {
            h(frame).await;
        }
    }

    /// Set whether to intercept file chooser dialogs.
    pub async fn set_intercept_file_chooser(&self, enabled: bool) -> Result<(), PageError> {
        *self.file_chooser_intercepted.write().await = enabled;

        self.connection
            .send_command::<_, serde_json::Value>(
                "Page.setInterceptFileChooserDialog",
                Some(serde_json::json!({ "enabled": enabled })),
                Some(&self.session_id),
            )
            .await?;

        Ok(())
    }

    /// Set the download behavior.
    pub async fn set_download_behavior(&self, allow: bool) -> Result<(), PageError> {
        // Ensure download directory exists
        if allow {
            tokio::fs::create_dir_all(&self.download_dir)
                .await
                .map_err(|e| {
                    PageError::EvaluationFailed(format!("Failed to create download directory: {e}"))
                })?;
        }

        let behavior = if allow { "allow" } else { "deny" };

        self.connection
            .send_command::<_, serde_json::Value>(
                "Browser.setDownloadBehavior",
                Some(serde_json::json!({
                    "behavior": behavior,
                    "downloadPath": self.download_dir.to_string_lossy(),
                    "eventsEnabled": true,
                })),
                Some(&self.session_id),
            )
            .await?;

        Ok(())
    }

    /// Handle a dialog event from CDP.
    pub async fn handle_dialog_event(
        &self,
        dialog_type: viewpoint_cdp::protocol::DialogType,
        message: String,
        default_prompt: Option<String>,
    ) {
        let dialog = Dialog::new(
            self.connection.clone(),
            self.session_id.clone(),
            dialog_type,
            message,
            default_prompt,
        );

        // Check if there's a waiter
        {
            let mut waiter = self.wait_for_dialog_tx.lock().await;
            if let Some(tx) = waiter.take() {
                let _ = tx.send(dialog);
                return;
            }
        }

        // Check if there's a handler
        let handler = self.dialog_handler.read().await;
        if let Some(ref h) = *handler {
            if let Err(e) = h(dialog).await {
                warn!("Dialog handler failed: {}", e);
            }
        } else {
            // Auto-dismiss if no handler
            debug!("Auto-dismissing dialog (no handler registered)");
            if let Err(e) = dialog.dismiss().await {
                warn!("Failed to auto-dismiss dialog: {}", e);
            }
        }
    }

    /// Handle download begin event.
    pub async fn handle_download_begin(
        &self,
        guid: String,
        suggested_filename: String,
        url: String,
    ) {
        download_handling::handle_download_begin(
            &self.connection,
            &self.session_id,
            &self.downloads,
            &self.download_handler,
            &self.wait_for_download_tx,
            guid,
            suggested_filename,
            url,
        )
        .await;
    }

    /// Handle download progress event.
    pub async fn handle_download_progress(&self, guid: String, state: &str) {
        download_handling::handle_download_progress(&self.downloads, guid, state).await;
    }

    /// Handle file chooser event.
    pub async fn handle_file_chooser_event(
        &self,
        frame_id: String,
        mode: viewpoint_cdp::protocol::page::FileChooserMode,
        backend_node_id: Option<i32>,
    ) {
        download_handling::handle_file_chooser_event(
            &self.connection,
            &self.session_id,
            &self.file_chooser_handler,
            &self.wait_for_file_chooser_tx,
            frame_id,
            mode,
            backend_node_id,
        )
        .await;
    }

    /// Wait for a dialog to appear.
    pub async fn wait_for_dialog(&self, timeout: Duration) -> Result<Dialog, PageError> {
        let (tx, rx) = oneshot::channel();
        {
            let mut waiter = self.wait_for_dialog_tx.lock().await;
            *waiter = Some(tx);
        }

        tokio::time::timeout(timeout, rx)
            .await
            .map_err(|_| PageError::EvaluationFailed("Timeout waiting for dialog".to_string()))?
            .map_err(|_| PageError::EvaluationFailed("Dialog wait cancelled".to_string()))
    }

    /// Wait for a download to start.
    pub async fn wait_for_download(&self, timeout: Duration) -> Result<Download, PageError> {
        download_handling::wait_for_download(&self.wait_for_download_tx, timeout).await
    }

    /// Register a download waiter and return the receiver.
    /// Use this when you need to register before performing an action.
    pub async fn register_download_waiter(&self) -> oneshot::Receiver<Download> {
        download_handling::register_download_waiter(&self.wait_for_download_tx).await
    }

    /// Await a previously registered download waiter with timeout.
    pub async fn await_download_waiter(
        &self,
        rx: oneshot::Receiver<Download>,
        timeout: Duration,
    ) -> Result<Download, PageError> {
        download_handling::await_download_waiter(rx, timeout).await
    }

    /// Wait for a file chooser to open.
    pub async fn wait_for_file_chooser(&self, timeout: Duration) -> Result<FileChooser, PageError> {
        download_handling::wait_for_file_chooser(&self.wait_for_file_chooser_tx, timeout).await
    }

    /// Wait for a console message.
    pub async fn wait_for_console(&self, timeout: Duration) -> Result<ConsoleMessage, PageError> {
        let (tx, rx) = oneshot::channel();
        {
            let mut waiter = self.wait_for_console_tx.lock().await;
            *waiter = Some(tx);
        }

        tokio::time::timeout(timeout, rx)
            .await
            .map_err(|_| {
                PageError::EvaluationFailed("Timeout waiting for console message".to_string())
            })?
            .map_err(|_| PageError::EvaluationFailed("Console wait cancelled".to_string()))
    }

    /// Wait for a page error.
    pub async fn wait_for_pageerror(&self, timeout: Duration) -> Result<PageErrorInfo, PageError> {
        let (tx, rx) = oneshot::channel();
        {
            let mut waiter = self.wait_for_pageerror_tx.lock().await;
            *waiter = Some(tx);
        }

        tokio::time::timeout(timeout, rx)
            .await
            .map_err(|_| PageError::EvaluationFailed("Timeout waiting for page error".to_string()))?
            .map_err(|_| PageError::EvaluationFailed("Page error wait cancelled".to_string()))
    }
}