| 1 | //! Linux with a Chromium browser: parks the hidden Teams tab on a static page of its own |
| 2 | //! origin, like the WebView2 host does on Windows. The running Teams web app holds close to a |
| 3 | //! gigabyte; the parked page keeps the MSAL cache reachable for token refreshes at a fraction |
| 4 | //! of that. The session wakes the tab when it needs the running app again |
| 5 | //! (`Transport::wake`), and this parks it once more. |
| 6 | //! |
| 7 | //! The quiet period counts from the last time the session opened or woke the tab, not from |
| 8 | //! the last command: commands go over a connection the session keeps, which this transport |
| 9 | //! does not see. Windows counts commands in flight (`webview::park`); here a page-run call |
| 10 | //! that overlaps the navigation loses its page connection (a read retries, a write fails). |
| 11 | |
| 12 | use std::sync::atomic::{AtomicUsize, Ordering}; |
| 13 | use std::sync::{Arc, Mutex}; |
| 14 | use std::time::{Duration, Instant}; |
| 15 | |
| 16 | use serde_json::json; |
| 17 | use session::Transport as _; |
| 18 | |
| 19 | const PARK_DELAY: Duration = Duration::from_secs(60); |
| 20 | const PARK_POLL: Duration = Duration::from_secs(15); |
| 21 | |
| 22 | /// When the session last opened or woke the tab, and who needs it to stay loaded. |
| 23 | pub struct ParkClock { |
| 24 | holds: AtomicUsize, |
| 25 | last_use: Mutex<Instant>, |
| 26 | } |
| 27 | |
| 28 | impl ParkClock { |
| 29 | pub fn new() -> Arc<Self> { |
| 30 | Arc::new(ParkClock { |
| 31 | holds: AtomicUsize::new(0), |
| 32 | last_use: Mutex::new(Instant::now()), |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | /// The tab was just opened or woken: leave it running for the quiet period. |
| 37 | pub fn touch(&self) { |
| 38 | if let Ok(mut last_use) = self.last_use.lock() { |
| 39 | *last_use = Instant::now(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// A call holds the tab: a navigation would cut its page sockets. |
| 44 | pub fn hold(&self, held: bool) { |
| 45 | if held { |
| 46 | self.holds.fetch_add(1, Ordering::SeqCst); |
| 47 | } else { |
| 48 | let _ = self |
| 49 | .holds |
| 50 | .try_update(Ordering::SeqCst, Ordering::SeqCst, |holds| { |
| 51 | Some(holds.saturating_sub(1)) |
| 52 | }); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fn due(&self, now: Instant) -> bool { |
| 57 | self.holds.load(Ordering::SeqCst) == 0 |
| 58 | && self |
| 59 | .last_use |
| 60 | .lock() |
| 61 | .is_ok_and(|last_use| now.duration_since(*last_use) >= PARK_DELAY) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn spawn(clock: Arc<ParkClock>, browser: browser::Browser, endpoint: &str) { |
| 66 | let transport = session::CdpTransport::new(endpoint); |
| 67 | app_base::runtime::handle().spawn(async move { |
| 68 | loop { |
| 69 | tokio::time::sleep(PARK_POLL).await; |
| 70 | if !clock.due(Instant::now()) || browser.is_busy() || browser.is_visible_managed() { |
| 71 | continue; |
| 72 | } |
| 73 | // False for a parked tab, a login page and a Teams that has not signed in yet. |
| 74 | if !browser.teams_has_msal_cache().await || !clock.due(Instant::now()) { |
| 75 | continue; |
| 76 | } |
| 77 | match park(&transport, browser.behind_mcas()).await { |
| 78 | Ok(true) => log::info!("Teams tab parked"), |
| 79 | Ok(false) => {} |
| 80 | Err(error) => log::warn!("parking the Teams tab failed: {error}"), |
| 81 | } |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | async fn park(transport: &session::CdpTransport, mcas: bool) -> session::Result<bool> { |
| 87 | let app = session::App::Teams; |
| 88 | let Some((control, _)) = transport.open(app, false).await? else { |
| 89 | return Ok(false); |
| 90 | }; |
| 91 | control |
| 92 | .call("Page.navigate", json!({ "url": app.park_url(mcas) })) |
| 93 | .await?; |
| 94 | Ok(true) |
| 95 | } |
| 96 | |
| 97 | #[cfg(test)] |
| 98 | mod tests { |
| 99 | use super::*; |
| 100 | |
| 101 | fn clock_used(ago: Duration) -> Arc<ParkClock> { |
| 102 | let clock = ParkClock::new(); |
| 103 | *clock.last_use.lock().unwrap() = Instant::now() - ago; |
| 104 | clock |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn a_tab_parks_only_after_the_quiet_period() { |
| 109 | assert!(!clock_used(Duration::from_secs(5)).due(Instant::now())); |
| 110 | assert!(clock_used(PARK_DELAY).due(Instant::now())); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn a_wake_restarts_the_quiet_period() { |
| 115 | let clock = clock_used(PARK_DELAY); |
| 116 | clock.touch(); |
| 117 | assert!(!clock.due(Instant::now())); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn a_held_tab_never_parks_and_releases_are_counted() { |
| 122 | let clock = clock_used(PARK_DELAY); |
| 123 | clock.hold(true); |
| 124 | clock.hold(true); |
| 125 | clock.hold(false); |
| 126 | assert!(!clock.due(Instant::now())); |
| 127 | clock.hold(false); |
| 128 | assert!(clock.due(Instant::now())); |
| 129 | clock.hold(false); |
| 130 | assert!(clock.due(Instant::now())); |
| 131 | } |
| 132 | } |