viewpoint_core/page/
lifecycle.rs

1//! Page lifecycle management.
2//!
3//! This module contains methods for managing the page lifecycle (close, is_closed).
4
5use tracing::{debug, info, instrument};
6use viewpoint_cdp::protocol::target_domain::CloseTargetParams;
7
8use super::Page;
9use crate::error::PageError;
10
11impl Page {
12    /// Close this page.
13    ///
14    /// # Errors
15    ///
16    /// Returns an error if closing fails.
17    #[instrument(level = "info", skip(self), fields(target_id = %self.target_id))]
18    pub async fn close(&mut self) -> Result<(), PageError> {
19        if self.closed {
20            debug!("Page already closed");
21            return Ok(());
22        }
23
24        info!("Closing page");
25
26        // Clean up route handlers
27        self.route_registry.unroute_all().await;
28        debug!("Route handlers cleaned up");
29
30        self.connection
31            .send_command::<_, serde_json::Value>(
32                "Target.closeTarget",
33                Some(CloseTargetParams {
34                    target_id: self.target_id.clone(),
35                }),
36                None,
37            )
38            .await?;
39
40        // Remove this page from the context's pages list to prevent stale session accumulation
41        if let Some(ref pages) = self.context_pages {
42            let mut pages_guard = pages.write().await;
43            pages_guard.retain(|p| p.target_id() != self.target_id);
44            debug!("Removed page from context's pages list");
45        }
46
47        self.closed = true;
48        info!("Page closed");
49        Ok(())
50    }
51
52    /// Check if this page has been closed.
53    pub fn is_closed(&self) -> bool {
54        self.closed
55    }
56}