| 1 | use std::future::Future; |
| 2 | use std::pin::Pin; |
| 3 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 4 | |
| 5 | use serde_json::json; |
| 6 | |
| 7 | use crate::app::{App, find_app_tab, has_login_tab}; |
| 8 | use crate::error::Result; |
| 9 | use crate::events::{TabControl, TabEvents, open}; |
| 10 | use crate::http::{list_targets, new_tab}; |
| 11 | |
| 12 | pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>; |
| 13 | pub type OpenedTab = (TabControl, Option<TabEvents>); |
| 14 | |
| 15 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 16 | pub struct Diagnosis { |
| 17 | pub login_pending: bool, |
| 18 | pub app_tab_open: bool, |
| 19 | } |
| 20 | |
| 21 | /// Where the signed-in Teams and Outlook web apps live: a debug port or an embedded webview. |
| 22 | pub trait Transport: Send + Sync { |
| 23 | fn check(&self) -> BoxFuture<'_, Result<()>>; |
| 24 | |
| 25 | /// `None` when the app has no tab. Without `with_events` the tab's protocol events are dropped. |
| 26 | fn open(&self, app: App, with_events: bool) -> BoxFuture<'_, Result<Option<OpenedTab>>>; |
| 27 | |
| 28 | /// Reloads the app so it fetches tokens again. |
| 29 | fn wake(&self, app: App) -> BoxFuture<'_, Result<()>>; |
| 30 | |
| 31 | fn diagnose(&self) -> BoxFuture<'_, Result<Diagnosis>>; |
| 32 | |
| 33 | /// While held the host must not navigate the app on its own (no parking). |
| 34 | fn hold(&self, _app: App, _held: bool) {} |
| 35 | } |
| 36 | |
| 37 | pub struct CdpTransport { |
| 38 | endpoint: String, |
| 39 | outlook_requested: AtomicBool, |
| 40 | } |
| 41 | |
| 42 | impl CdpTransport { |
| 43 | pub fn new(endpoint: &str) -> Self { |
| 44 | CdpTransport { |
| 45 | endpoint: endpoint.trim_end_matches('/').to_owned(), |
| 46 | outlook_requested: AtomicBool::new(false), |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// Some tenants grant the Teams web client no Graph chat scopes; Outlook on the web has them and signs in silently next to a signed-in Teams. |
| 52 | fn wants_outlook_tab(targets: &[crate::app::Target], app: App) -> bool { |
| 53 | app == App::Outlook && find_app_tab(targets, App::Teams).is_some() && !has_login_tab(targets) |
| 54 | } |
| 55 | |
| 56 | impl Transport for CdpTransport { |
| 57 | fn check(&self) -> BoxFuture<'_, Result<()>> { |
| 58 | Box::pin(async move { list_targets(&self.endpoint).await.map(drop) }) |
| 59 | } |
| 60 | |
| 61 | fn open(&self, app: App, with_events: bool) -> BoxFuture<'_, Result<Option<OpenedTab>>> { |
| 62 | Box::pin(async move { |
| 63 | let targets = list_targets(&self.endpoint).await?; |
| 64 | let Some(websocket_url) = find_app_tab(&targets, app).and_then(|target| target.websocket_url.clone()) else { |
| 65 | if wants_outlook_tab(&targets, app) && !self.outlook_requested.swap(true, Ordering::SeqCst) { |
| 66 | new_tab(&self.endpoint, App::Outlook.start_url()).await?; |
| 67 | } |
| 68 | return Ok(None); |
| 69 | }; |
| 70 | open(&websocket_url, with_events).await.map(Some) |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | fn wake(&self, app: App) -> BoxFuture<'_, Result<()>> { |
| 75 | Box::pin(async move { |
| 76 | let targets = list_targets(&self.endpoint).await?; |
| 77 | let Some(target) = find_app_tab(&targets, app) else { |
| 78 | return Ok(()); |
| 79 | }; |
| 80 | let parked = app.is_park_url(&target.url); |
| 81 | let (control, _) = open(target.websocket_url.as_deref().unwrap_or_default(), false).await?; |
| 82 | if parked { |
| 83 | control.call("Page.navigate", json!({"url": app.start_url()})).await?; |
| 84 | } else { |
| 85 | control.call("Page.reload", json!({"ignoreCache": false})).await?; |
| 86 | } |
| 87 | Ok(()) |
| 88 | }) |
| 89 | } |
| 90 | |
| 91 | fn diagnose(&self) -> BoxFuture<'_, Result<Diagnosis>> { |
| 92 | Box::pin(async move { |
| 93 | let targets = list_targets(&self.endpoint).await?; |
| 94 | Ok(Diagnosis { |
| 95 | login_pending: has_login_tab(&targets), |
| 96 | app_tab_open: App::ALL.iter().any(|&app| find_app_tab(&targets, app).is_some()), |
| 97 | }) |
| 98 | }) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | #[cfg(test)] |
| 103 | mod tests { |
| 104 | use super::*; |
| 105 | use crate::app::Target; |
| 106 | |
| 107 | fn page(url: &str) -> Target { |
| 108 | Target { kind: "page".to_owned(), url: url.to_owned(), websocket_url: Some("ws://tab".to_owned()) } |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn outlook_opens_only_next_to_a_signed_in_teams_tab() { |
| 113 | let teams = page("https://teams.cloud.microsoft/"); |
| 114 | let login = page("https://login.microsoftonline.com/common/oauth2"); |
| 115 | assert!(wants_outlook_tab(&[teams.clone()], App::Outlook)); |
| 116 | assert!(!wants_outlook_tab(&[teams.clone()], App::Teams)); |
| 117 | assert!(!wants_outlook_tab(&[], App::Outlook)); |
| 118 | assert!(!wants_outlook_tab(&[teams, login], App::Outlook)); |
| 119 | } |
| 120 | } |