|
| 1 | +//! What a call app shows, as Hex takes it. |
| 2 | +//! |
| 3 | +//! Every call app draws its call differently, and each gets an adapter: one |
| 4 | +//! pure function from a snapshot of the app's windows to an |
| 5 | +//! [`Observation`], which says who is on the call and who holds the floor, |
| 6 | +//! or why that cannot be told. The snapshot is plain data |
| 7 | +//! ([`accessibility::tree`](crate::accessibility::tree)), so an adapter |
| 8 | +//! touches no app, no clock and no file, and is tested on trees written by |
| 9 | +//! hand. Reading another app is one adapter and one line in |
| 10 | +//! [`registry`](super::registry). |
| 11 | +//! |
| 12 | +//! An adapter tells people apart by whatever the app does, most often a |
| 13 | +//! user id inside a DOM id. That is an [`AppParticipantId`], and it is the |
| 14 | +//! one thing here that must never be saved or logged. It cannot be: it does |
| 15 | +//! not serialize, it prints without its text, and nothing reads the text |
| 16 | +//! back out of it. The [`reader`](super::reader) gives each one a key of |
| 17 | +//! the meeting's own (`p1`, `p2`) and forgets it when the recording ends. |
| 18 | +//! |
| 19 | +//! macOS only: the snapshot is read by Hex's Accessibility client, and |
| 20 | +//! Windows and Linux Hex have none (UI Automation, AT-SPI) yet. |
| 21 | +
|
| 22 | +use std::fmt; |
| 23 | + |
| 24 | +use super::SpeakerName; |
| 25 | +use super::evidence::Untrusted; |
| 26 | +use crate::accessibility::tree::Snapshot; |
| 27 | + |
| 28 | +/// A call app's own id for someone on a call. It tells two tiles of one |
| 29 | +/// person from two people, and that is all it can do: two of them can be |
| 30 | +/// compared, and nothing else. |
| 31 | +#[derive(Clone, Eq, Hash, PartialEq)] |
| 32 | +pub struct AppParticipantId(String); |
| 33 | + |
| 34 | +impl AppParticipantId { |
| 35 | + /// The id an app shows for someone. `None` when there is nothing in |
| 36 | + /// it: a blank id would make one person of everyone it was read for. |
| 37 | + pub fn parse(shown: &str) -> Option<Self> { |
| 38 | + let id = shown.trim(); |
| 39 | + (!id.is_empty()).then(|| Self(id.to_owned())) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl fmt::Debug for AppParticipantId { |
| 44 | + /// Without the text, so that no log line or panic message can carry it. |
| 45 | + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | + formatter.write_str("AppParticipantId(..)") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Someone a call app showed on the call at one look. |
| 51 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 52 | +pub struct ObservedParticipant { |
| 53 | + pub id: AppParticipantId, |
| 54 | + /// What the app calls them, cleaned. Nobody when it shows nothing Hex |
| 55 | + /// can use as a name. |
| 56 | + pub name: Option<SpeakerName>, |
| 57 | + /// The person using Hex. |
| 58 | + pub is_local: bool, |
| 59 | +} |
| 60 | + |
| 61 | +/// What one look at a call app's windows showed. |
| 62 | +#[derive(Clone, Debug, Eq, PartialEq)] |
| 63 | +pub enum Observation { |
| 64 | + /// The call, read whole. |
| 65 | + Active { |
| 66 | + /// Everyone the app shows on the call. Two tiles of one person are |
| 67 | + /// one person: the first of them stands. |
| 68 | + roster: Vec<ObservedParticipant>, |
| 69 | + /// The one person the app marks as speaking, or nobody. They are |
| 70 | + /// in the roster, now or at an earlier look: a mark on someone the |
| 71 | + /// app never showed says nothing about the floor. |
| 72 | + floor: Option<AppParticipantId>, |
| 73 | + }, |
| 74 | + /// None of the app's windows is a call. |
| 75 | + NoCallWindow, |
| 76 | + /// A call window is there, and what it shows cannot be relied on. |
| 77 | + Untrusted(Untrusted), |
| 78 | +} |
| 79 | + |
| 80 | +impl Observation { |
| 81 | + /// What `adapter` makes of one snapshot, under the two rules that hold |
| 82 | + /// for every app and so are no adapter's to remember. A snapshot a |
| 83 | + /// budget cut short may be missing a tile, so no adapter is shown it. |
| 84 | + /// And an app with no window has no call window. |
| 85 | + pub fn of(snapshot: &Snapshot, adapter: &dyn CallAppAdapter) -> Self { |
| 86 | + if !snapshot.cut_short.is_empty() { |
| 87 | + Self::Untrusted(Untrusted::CutShort) |
| 88 | + } else if snapshot.windows.is_empty() { |
| 89 | + Self::NoCallWindow |
| 90 | + } else { |
| 91 | + adapter.observe(snapshot) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Reads one call app's windows. The same snapshot always gets the same |
| 97 | +/// answer and nothing is touched, so one adapter serves every recording. |
| 98 | +pub trait CallAppAdapter: Sync { |
| 99 | + /// What the app's windows show. Asked only through |
| 100 | + /// [`Observation::of`]: of a snapshot read whole, with a window in it. |
| 101 | + fn observe(&self, snapshot: &Snapshot) -> Observation; |
| 102 | +} |
| 103 | + |
| 104 | +/// An adapter is most often just a function. |
| 105 | +impl<F: Fn(&Snapshot) -> Observation + Sync> CallAppAdapter for F { |
| 106 | + fn observe(&self, snapshot: &Snapshot) -> Observation { |
| 107 | + self(snapshot) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod tests { |
| 113 | + use super::*; |
| 114 | + use crate::accessibility::tree::{Cut, Node}; |
| 115 | + |
| 116 | + fn window() -> Node { |
| 117 | + Node { |
| 118 | + role: Some("AXWindow".into()), |
| 119 | + ..Default::default() |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + fn nobody_speaking(_: &Snapshot) -> Observation { |
| 124 | + Observation::Active { |
| 125 | + roster: Vec::new(), |
| 126 | + floor: None, |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + fn never_asked(_: &Snapshot) -> Observation { |
| 131 | + panic!("no adapter is shown this snapshot") |
| 132 | + } |
| 133 | + |
| 134 | + #[test] |
| 135 | + fn a_snapshot_read_whole_is_the_adapters_to_read() { |
| 136 | + let snapshot = Snapshot { |
| 137 | + windows: vec![window()], |
| 138 | + ..Default::default() |
| 139 | + }; |
| 140 | + assert_eq!( |
| 141 | + Observation::of(&snapshot, &nobody_speaking), |
| 142 | + nobody_speaking(&snapshot) |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + #[test] |
| 147 | + fn a_snapshot_cut_short_is_never_shown_to_an_adapter() { |
| 148 | + for cut in [Cut::Nodes, Cut::Depth, Cut::Deadline] { |
| 149 | + // Whatever was read before the budget ran out, even nothing. |
| 150 | + for windows in [vec![window()], Vec::new()] { |
| 151 | + let snapshot = Snapshot { |
| 152 | + windows, |
| 153 | + cut_short: vec![cut], |
| 154 | + ..Default::default() |
| 155 | + }; |
| 156 | + assert_eq!( |
| 157 | + Observation::of(&snapshot, &never_asked), |
| 158 | + Observation::Untrusted(Untrusted::CutShort) |
| 159 | + ); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn an_app_with_no_window_has_no_call_window() { |
| 166 | + assert_eq!( |
| 167 | + Observation::of(&Snapshot::default(), &never_asked), |
| 168 | + Observation::NoCallWindow |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn an_app_id_is_never_blank_and_never_printed() { |
| 174 | + let id = AppParticipantId::parse(" U024BE7LH ").unwrap(); |
| 175 | + assert_eq!(id, AppParticipantId::parse("U024BE7LH").unwrap()); |
| 176 | + assert_ne!(id, AppParticipantId::parse("U024BE7LJ").unwrap()); |
| 177 | + for blank in ["", " ", "\n"] { |
| 178 | + assert_eq!(AppParticipantId::parse(blank), None, "{blank:?}"); |
| 179 | + } |
| 180 | + |
| 181 | + let seen = Observation::Active { |
| 182 | + roster: vec![ObservedParticipant { |
| 183 | + id: id.clone(), |
| 184 | + name: SpeakerName::parse("Priya Nair"), |
| 185 | + is_local: false, |
| 186 | + }], |
| 187 | + floor: Some(id), |
| 188 | + }; |
| 189 | + let printed = format!("{seen:?}"); |
| 190 | + assert!(!printed.contains("U024BE7LH") && !printed.contains("Priya")); |
| 191 | + } |
| 192 | +} |
0 commit comments