use std::sync::Arc; use std::time::Duration; use serde_json::{Value, json}; use session::{App, Session, TabControl, TabEvents}; use tokio::sync::{Notify, mpsc, oneshot, watch}; use tokio::time::{MissedTickBehavior, interval, sleep}; use super::RealtimeConfig; use super::callback::{CallbackReply, TrouterCallback, decode_callback, reply_expression}; use super::event::{RealtimeEvent, StatusEvent, StatusKind, decode_payload}; use super::host::ic3_scope; pub(super) const DEFAULT_GLOBAL_NAME: &str = "__chatsvcTrouter"; pub(super) const DEFAULT_BINDING_NAME: &str = "__chatsvcRealtime"; pub(super) const DEFAULT_EPID_KEY: &str = "__chatsvcEpid"; const WORKER_TEMPLATE: &str = include_str!("../../assets/trouter.js"); const FORWARD_CALLBACKS_OFF: &str = "const FORWARD_CALLBACKS = false;"; const REGISTRATION_OFF: &str = "const RINGABLE = false;"; const STOP_STEP_TIMEOUT: Duration = Duration::from_secs(10); /// The page worker with this instance's names baked in. #[derive(Clone)] pub(super) struct PageScript { pub global_name: String, pub binding_name: String, pub worker_source: String, } impl PageScript { pub(super) fn new(config: &RealtimeConfig) -> Self { let names = &config.instance; let quoted = |name: &str| format!("'{name}'"); let mut worker_source = WORKER_TEMPLATE .replace("ed(DEFAULT_GLOBAL_NAME), "ed(&names.global)) .replace("ed(DEFAULT_BINDING_NAME), "ed(&names.binding)) .replace( "ed(DEFAULT_EPID_KEY), "ed(&names.endpoint_storage_key), ); if config.forward_callbacks { worker_source = worker_source.replace(FORWARD_CALLBACKS_OFF, "const FORWARD_CALLBACKS = true;"); } if config.ringable { worker_source = worker_source.replace(REGISTRATION_OFF, "const RINGABLE = true;"); } PageScript { global_name: names.global.clone(), binding_name: names.binding.clone(), worker_source, } } fn ensure_body(&self) -> String { format!( "{}\nreturn window.{}.ensure({{token, host: args.host, activity: args.activity}});", self.worker_source, self.global_name ) } fn set_activity_expression(&self, active: bool) -> String { format!( "window.{name} ? window.{name}.setActivity('{state}') : null", name = self.global_name, state = activity_label(active) ) } fn resend_activity_expression(&self) -> String { format!( "window.{name} ? window.{name}.resendActivity() : null", name = self.global_name ) } fn stop_expression(&self) -> String { format!( "window.{name} ? window.{name}.stop() : null", name = self.global_name ) } } pub(super) struct CallbackPipes { pub output: mpsc::UnboundedSender, pub replies: mpsc::UnboundedReceiver, } pub(super) struct Attached { control: TabControl, events: TabEvents, script_identifier: String, stop_expression: String, } pub(super) async fn attach( session: &Session, script: &PageScript, ) -> crate::error::Result { let (control, events) = session.subscribe(App::Teams).await?; control.enable_events().await?; control.add_binding(&script.binding_name).await?; let script_identifier = control.inject_script(&script.worker_source).await?; Ok(Attached { control, events, script_identifier, stop_expression: script.stop_expression(), }) } impl Attached { pub(super) async fn shutdown(self) { let _ = tokio::time::timeout( STOP_STEP_TIMEOUT, self.control.evaluate(&self.stop_expression), ) .await; let _ = tokio::time::timeout( STOP_STEP_TIMEOUT, self.control.remove_injected_script(&self.script_identifier), ) .await; } } enum DriveEnd { Stopped(Option>), TabLost, } pub(super) struct Runner { session: Session, config: RealtimeConfig, script: PageScript, callbacks: CallbackPipes, host: String, output: mpsc::UnboundedSender, activity: watch::Receiver, resend: Arc, force_refresh: bool, last_error: Option, } impl Runner { pub(super) fn new( session: Session, config: RealtimeConfig, script: PageScript, host: String, output: mpsc::UnboundedSender, callbacks: CallbackPipes, activity: watch::Receiver, resend: Arc, ) -> Self { Runner { session, config, script, callbacks, host, output, activity, resend, force_refresh: false, last_error: None, } } pub(super) async fn ensure(&mut self) -> session::Result { let args = json!({"host": self.host, "activity": activity_label(*self.activity.borrow())}); let outcome = self .session .run_with_token( App::Teams, &ic3_scope(), &self.script.ensure_body(), &args, self.force_refresh, ) .await; if outcome.is_ok() { self.force_refresh = false; } outcome } pub(super) async fn run( mut self, first: Attached, mut stop: oneshot::Receiver>, ) { let mut attached = Some(first); let mut backoff = Duration::from_secs(1); let retry_signal = self.config.retry.clone(); loop { let current = match attached.take() { Some(current) => current, None => match attach(&self.session, &self.script).await { Ok(current) => { backoff = Duration::from_secs(1); current } Err(error) => { self.report_error(format!("reattach_failed: {error}")); tokio::select! { _ = sleep(backoff) => {} _ = &mut stop => return, _ = self.output.closed() => return, _ = async { match &retry_signal { Some(signal) => signal.notified().await, None => std::future::pending().await, } } => backoff = Duration::from_millis(500), } backoff = (backoff * 2).min(self.config.max_reattach_backoff); continue; } }, }; match self.drive(current, &mut stop).await { DriveEnd::Stopped(done) => { if let Some(done) = done { let _ = done.send(()); } return; } DriveEnd::TabLost => { self.send(RealtimeEvent::Status(StatusEvent { kind: StatusKind::Disconnected, detail: "tab_connection_lost".into(), })); sleep(Duration::from_secs(1)).await; } } } } async fn drive( &mut self, mut attached: Attached, stop: &mut oneshot::Receiver>, ) -> DriveEnd { let mut tick = interval(self.config.ensure_interval); tick.set_missed_tick_behavior(MissedTickBehavior::Delay); tick.tick().await; self.ensure_and_report().await; loop { tokio::select! { event = attached.events.recv() => { let Some(event) = event else { return DriveEnd::TabLost }; if let Some(payload) = event.binding_payload(&self.script.binding_name) { self.forward_payload(payload); } else if is_main_frame_navigation(&event.method, &event.params) { sleep(self.config.navigation_settle).await; self.ensure_and_report().await; } } _ = tick.tick() => self.ensure_and_report().await, _ = self.resend.notified() => { let expression = self.script.resend_activity_expression(); let _ = attached.control.evaluate(&expression).await; } changed = self.activity.changed() => { if changed.is_ok() { let expression = self.script.set_activity_expression(*self.activity.borrow_and_update()); let _ = attached.control.evaluate(&expression).await; } } Some(reply) = self.callbacks.replies.recv() => { let expression = reply_expression(&self.script.global_name, &reply); let _ = attached.control.evaluate(&expression).await; } done = &mut *stop => { attached.shutdown().await; return DriveEnd::Stopped(done.ok()); } _ = self.output.closed() => { attached.shutdown().await; return DriveEnd::Stopped(None); } } } } fn forward_payload(&mut self, payload: &str) { if let Some(callback) = decode_callback(payload) { let _ = self.callbacks.output.send(callback); return; } let Ok(event) = decode_payload(payload) else { return; }; if let RealtimeEvent::Status(status) = &event && status.kind == StatusKind::Error && is_auth_failure(&status.detail) { self.force_refresh = true; } self.send(event); } async fn ensure_and_report(&mut self) { match self.ensure().await { Ok(_) => self.last_error = None, Err(error) => self.report_error(format!("ensure_failed: {error}")), } } fn report_error(&mut self, detail: String) { if self.last_error.as_deref() == Some(detail.as_str()) { return; } self.last_error = Some(detail.clone()); self.send(RealtimeEvent::Status(StatusEvent { kind: StatusKind::Error, detail, })); } fn send(&self, event: RealtimeEvent) { let _ = self.output.send(event); } } fn activity_label(active: bool) -> &'static str { if active { "active" } else { "inactive" } } fn is_main_frame_navigation(method: &str, params: &Value) -> bool { method == "Page.frameNavigated" && params.pointer("/frame/parentId").is_none() } fn is_auth_failure(detail: &str) -> bool { detail == "registrar_http_401" || detail == "registrar_http_403" } #[cfg(test)] mod tests { use super::*; #[test] fn only_top_level_navigations_trigger_ensure() { assert!(is_main_frame_navigation( "Page.frameNavigated", &json!({"frame": {"id": "a"}}) )); assert!(!is_main_frame_navigation( "Page.frameNavigated", &json!({"frame": {"id": "b", "parentId": "a"}}) )); assert!(!is_main_frame_navigation("Page.loadEventFired", &json!({}))); } #[test] fn activity_is_labelled_and_set_through_the_page_global() { assert_eq!(activity_label(true), "active"); assert_eq!(activity_label(false), "inactive"); let script = PageScript::new(&RealtimeConfig::default()); assert_eq!( script.set_activity_expression(false), "window.__chatsvcTrouter ? window.__chatsvcTrouter.setActivity('inactive') : null" ); } #[test] fn resend_goes_through_the_page_global() { let script = PageScript::new(&RealtimeConfig::default()); assert_eq!( script.resend_activity_expression(), "window.__chatsvcTrouter ? window.__chatsvcTrouter.resendActivity() : null" ); } #[test] fn worker_reports_activity_on_change_only() { let worker = PageScript::new(&RealtimeConfig::default()).worker_source; assert!(!worker.contains("activity();")); assert_eq!(worker.matches("'user.activity'").count(), 1); } #[test] fn rejected_bearer_forces_token_refresh() { assert!(is_auth_failure("registrar_http_401")); assert!(!is_auth_failure("registrar_http_500")); assert!(!is_auth_failure("registrar_failed")); } #[test] fn ensure_body_embeds_the_worker_once_before_the_call() { let ensure_body = PageScript::new(&RealtimeConfig::default()).ensure_body(); assert!(ensure_body.starts_with("(() => {")); assert!(ensure_body.trim_end().ends_with( "window.__chatsvcTrouter.ensure({token, host: args.host, activity: args.activity});" )); assert_eq!(ensure_body.matches("const VERSION").count(), 1); } #[test] fn second_instance_gets_its_own_page_names_and_callbacks() { let config = RealtimeConfig { instance: super::super::InstanceNames { global: "__callingTrouter".into(), binding: "__callingRealtime".into(), endpoint_storage_key: "__callingEpid".into(), }, forward_callbacks: true, ..RealtimeConfig::default() }; let script = PageScript::new(&config); assert!(!script.worker_source.contains("__chatsvc")); assert!( script .worker_source .contains("const GLOBAL_NAME = '__callingTrouter';") ); assert!( script .worker_source .contains("const FORWARD_CALLBACKS = true;") ); assert_eq!( script.stop_expression(), "window.__callingTrouter ? window.__callingTrouter.stop() : null" ); let default_script = PageScript::new(&RealtimeConfig::default()); assert!(default_script.worker_source.contains(FORWARD_CALLBACKS_OFF)); } #[test] fn only_the_ringable_instance_registers_for_calls() { let ringable = PageScript::new(&RealtimeConfig { ringable: true, ..RealtimeConfig::default() }); assert!(ringable.worker_source.contains("const RINGABLE = true;")); let quiet = PageScript::new(&RealtimeConfig::default()); assert!(quiet.worker_source.contains(REGISTRATION_OFF)); for script in [ringable, quiet] { assert!( script .worker_source .contains("{appId: 'SkypeSpacesWeb', templateKey: 'SkypeSpacesWeb_2.6'}") ); assert!( script .worker_source .contains("{appId: 'TeamsCDLWebWorker', templateKey: 'TeamsCDLWebWorker_2.6'}") ); assert!( script .worker_source .contains("RINGABLE ? {productContext: ''} : {}") ); } } }