| 1 | use std::future::Future; |
| 2 | use std::pin::Pin; |
| 3 | |
| 4 | use serde_json::json; |
| 5 | |
| 6 | use crate::app::{App, find_app_tab, has_login_tab}; |
| 7 | use crate::error::Result; |
| 8 | use crate::events::{TabControl, TabEvents, open}; |
| 9 | use crate::http::list_targets; |
| 10 | |
| 11 | pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; |
| 12 | pub type OpenedTab = (TabControl, Option<TabEvents>); |
| 13 | |
| 14 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 15 | pub struct Diagnosis { |
| 16 | pub login_pending: bool, |
| 17 | pub app_tab_open: bool, |
| 18 | } |
| 19 | |
| 20 | /// Where the signed-in Teams web app lives: a debug port or an embedded webview. |
| 21 | pub trait Transport: Send + Sync { |
| 22 | fn check(&self) -> BoxFuture<'_, Result<()>>; |
| 23 | |
| 24 | /// `None` when the app has no tab. Without `with_events` the tab's protocol events are dropped. |
| 25 | fn open(&self, app: App, with_events: bool) -> BoxFuture<'_, Result<Option<OpenedTab>>>; |
| 26 | |
| 27 | /// Reloads the app so it fetches tokens again. |
| 28 | fn wake(&self, app: App) -> BoxFuture<'_, Result<()>>; |
| 29 | |
| 30 | fn diagnose(&self) -> BoxFuture<'_, Result<Diagnosis>>; |
| 31 | |
| 32 | /// Whether the transport accepts raw CDP methods. WebDriver BiDi does not, so session-level |
| 33 | /// CDP work such as the MSAL cookie must stay off it. |
| 34 | fn supports_cdp(&self) -> bool { |
| 35 | true |
| 36 | } |
| 37 | |
| 38 | /// While held the host must not navigate the app on its own (no parking). |
| 39 | fn hold(&self, _app: App, _held: bool) {} |
| 40 | } |
| 41 | |
| 42 | pub struct CdpTransport { |
| 43 | endpoint: String, |
| 44 | } |
| 45 | |
| 46 | impl CdpTransport { |
| 47 | pub fn new(endpoint: &str) -> Self { |
| 48 | CdpTransport { |
| 49 | endpoint: endpoint.trim_end_matches('/').to_owned(), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl Transport for CdpTransport { |
| 55 | fn check(&self) -> BoxFuture<'_, Result<()>> { |
| 56 | Box::pin(async move { list_targets(&self.endpoint).await.map(drop) }) |
| 57 | } |
| 58 | |
| 59 | fn open(&self, app: App, with_events: bool) -> BoxFuture<'_, Result<Option<OpenedTab>>> { |
| 60 | Box::pin(async move { |
| 61 | let targets = list_targets(&self.endpoint).await?; |
| 62 | let Some(websocket_url) = |
| 63 | find_app_tab(&targets, app).and_then(|target| target.websocket_url.clone()) |
| 64 | else { |
| 65 | return Ok(None); |
| 66 | }; |
| 67 | open(&websocket_url, with_events).await.map(Some) |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | fn wake(&self, app: App) -> BoxFuture<'_, Result<()>> { |
| 72 | Box::pin(async move { |
| 73 | let targets = list_targets(&self.endpoint).await?; |
| 74 | let Some(target) = find_app_tab(&targets, app) else { |
| 75 | return Ok(()); |
| 76 | }; |
| 77 | let parked = app.is_park_url(&target.url); |
| 78 | let (control, _) = |
| 79 | open(target.websocket_url.as_deref().unwrap_or_default(), false).await?; |
| 80 | if parked { |
| 81 | control |
| 82 | .call("Page.navigate", json!({"url": app.start_url()})) |
| 83 | .await?; |
| 84 | } else { |
| 85 | control |
| 86 | .call("Page.reload", json!({"ignoreCache": false})) |
| 87 | .await?; |
| 88 | } |
| 89 | Ok(()) |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | fn diagnose(&self) -> BoxFuture<'_, Result<Diagnosis>> { |
| 94 | Box::pin(async move { |
| 95 | let targets = list_targets(&self.endpoint).await?; |
| 96 | Ok(Diagnosis { |
| 97 | login_pending: has_login_tab(&targets), |
| 98 | app_tab_open: App::ALL |
| 99 | .iter() |
| 100 | .any(|&app| find_app_tab(&targets, app).is_some()), |
| 101 | }) |
| 102 | }) |
| 103 | } |
| 104 | } |