| 1 | use std::cell::Cell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::*; |
| 5 | use session::trusted_url::{HostRule, trusted_https_url}; |
| 6 | |
| 7 | #[cfg(windows)] |
| 8 | mod native_windows; |
| 9 | #[cfg(windows)] |
| 10 | use native_windows as native; |
| 11 | #[cfg(not(windows))] |
| 12 | mod native_stub; |
| 13 | #[cfg(not(windows))] |
| 14 | use native_stub as native; |
| 15 | |
| 16 | use native::Native; |
| 17 | pub use native::available; |
| 18 | #[cfg(windows)] |
| 19 | pub use native::configure; |
| 20 | |
| 21 | const SHAREPOINT_DOMAIN: &str = "sharepoint.com"; |
| 22 | const SHAREPOINT_HOSTS: [HostRule; 1] = [HostRule::Domain(SHAREPOINT_DOMAIN)]; |
| 23 | const PERSONAL_SITE: &str = "/personal/"; |
| 24 | const TEAM_SITES: [&str; 2] = ["/sites/", "/teams/"]; |
| 25 | const PLAYER_OPTIONS: &str = "%7B%22ust%22%3Atrue%2C%22hv%22%3A%22CopyEmbedCode%22%7D"; |
| 26 | |
| 27 | /// SharePoint's embeddable player for a video stored in OneDrive or a team site. |
| 28 | pub fn player_url(content_url: &str) -> Option<String> { |
| 29 | let parsed = trusted_https_url(content_url, &SHAREPOINT_HOSTS)?; |
| 30 | let host = parsed.host_str()?; |
| 31 | if host == SHAREPOINT_DOMAIN { |
| 32 | return None; |
| 33 | } |
| 34 | let path = parsed.path(); |
| 35 | let site_prefix = std::iter::once(PERSONAL_SITE) |
| 36 | .chain(TEAM_SITES) |
| 37 | .find(|prefix| path.starts_with(prefix))?; |
| 38 | let site_name = path[site_prefix.len()..].split('/').next()?; |
| 39 | if site_name.is_empty() || path.len() <= site_prefix.len() + site_name.len() + 1 { |
| 40 | return None; |
| 41 | } |
| 42 | let site = format!("https://{host}{site_prefix}{site_name}"); |
| 43 | Some(format!( |
| 44 | "{site}/_layouts/15/embed.aspx?id={path}&embed={PLAYER_OPTIONS}" |
| 45 | )) |
| 46 | } |
| 47 | |
| 48 | struct Playing { |
| 49 | key: SharedString, |
| 50 | window: WindowId, |
| 51 | native: Native, |
| 52 | painted_in: Rc<Cell<u64>>, |
| 53 | } |
| 54 | |
| 55 | #[derive(Default)] |
| 56 | pub struct InlineVideo { |
| 57 | playing: Option<Playing>, |
| 58 | frame: Rc<Cell<u64>>, |
| 59 | } |
| 60 | |
| 61 | impl Global for InlineVideo {} |
| 62 | |
| 63 | pub fn play(key: SharedString, content_url: &str, window: &mut Window, cx: &mut App) -> bool { |
| 64 | let Some(url) = player_url(content_url).filter(|_| available()) else { |
| 65 | return false; |
| 66 | }; |
| 67 | stop(cx); |
| 68 | let Some((native, mut new_windows)) = Native::open(&url, window) else { |
| 69 | return false; |
| 70 | }; |
| 71 | cx.spawn(async move |cx| { |
| 72 | while let Some(target) = new_windows.recv().await { |
| 73 | cx.update(|cx| app_base::open_link::open_url(cx, &target)); |
| 74 | } |
| 75 | }) |
| 76 | .detach(); |
| 77 | let state = cx.default_global::<InlineVideo>(); |
| 78 | state.playing = Some(Playing { |
| 79 | key, |
| 80 | window: window.window_handle().window_id(), |
| 81 | native, |
| 82 | painted_in: Rc::new(Cell::new(u64::MAX)), |
| 83 | }); |
| 84 | window.refresh(); |
| 85 | true |
| 86 | } |
| 87 | |
| 88 | pub fn stop(cx: &mut App) { |
| 89 | if !cx.has_global::<InlineVideo>() { |
| 90 | return; |
| 91 | } |
| 92 | if let Some(playing) = cx.global_mut::<InlineVideo>().playing.take() { |
| 93 | playing.native.close(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// Keeps the player on the slot while the slot is painted. |
| 98 | pub fn slot(key: SharedString) -> impl IntoElement { |
| 99 | canvas( |
| 100 | move |bounds, window, cx| { |
| 101 | let Some(state) = cx.try_global::<InlineVideo>() else { |
| 102 | return; |
| 103 | }; |
| 104 | let Some(playing) = state.playing.as_ref().filter(|playing| playing.key == key) else { |
| 105 | return; |
| 106 | }; |
| 107 | playing.painted_in.set(state.frame.get()); |
| 108 | let visible = window.content_mask().bounds.intersect(&bounds); |
| 109 | let hidden: Vec<Bounds<Pixels>> = clipped_parts(bounds, visible); |
| 110 | playing.native.place( |
| 111 | bounds, |
| 112 | &hidden, |
| 113 | window.scale_factor(), |
| 114 | visible.size.height > px(0.), |
| 115 | ); |
| 116 | }, |
| 117 | |_, _, _, _| {}, |
| 118 | ) |
| 119 | .absolute() |
| 120 | .inset_0() |
| 121 | } |
| 122 | |
| 123 | /// Paints after the timeline: closes a player whose slot was not painted this frame. |
| 124 | pub fn sentinel() -> impl IntoElement { |
| 125 | canvas( |
| 126 | |_, window, cx| { |
| 127 | let window_id = window.window_handle().window_id(); |
| 128 | let Some(state) = cx.try_global::<InlineVideo>() else { |
| 129 | return false; |
| 130 | }; |
| 131 | let frame = state.frame.get(); |
| 132 | state.frame.set(frame.wrapping_add(1)); |
| 133 | state.playing.as_ref().is_some_and(|playing| { |
| 134 | playing.window == window_id && playing.painted_in.get() != frame |
| 135 | }) |
| 136 | }, |
| 137 | |_, orphaned, _, cx| { |
| 138 | if orphaned { |
| 139 | stop(cx); |
| 140 | } |
| 141 | }, |
| 142 | ) |
| 143 | .absolute() |
| 144 | .size_0() |
| 145 | } |
| 146 | |
| 147 | fn clipped_parts(bounds: Bounds<Pixels>, visible: Bounds<Pixels>) -> Vec<Bounds<Pixels>> { |
| 148 | if visible.size.height <= px(0.) || visible.size.width <= px(0.) { |
| 149 | return Vec::new(); |
| 150 | } |
| 151 | let mut parts = Vec::new(); |
| 152 | if visible.top() > bounds.top() { |
| 153 | parts.push(Bounds::from_corners( |
| 154 | bounds.origin, |
| 155 | point(bounds.right(), visible.top()), |
| 156 | )); |
| 157 | } |
| 158 | if visible.bottom() < bounds.bottom() { |
| 159 | parts.push(Bounds::from_corners( |
| 160 | point(bounds.left(), visible.bottom()), |
| 161 | bounds.bottom_right(), |
| 162 | )); |
| 163 | } |
| 164 | parts |
| 165 | } |
| 166 | |
| 167 | #[cfg(test)] |
| 168 | mod tests { |
| 169 | use gpui_kit::{Bounds, point, px, size}; |
| 170 | |
| 171 | use super::{clipped_parts, player_url}; |
| 172 | |
| 173 | #[test] |
| 174 | fn a_onedrive_chat_video_plays_in_the_personal_site_player() { |
| 175 | let url = player_url( |
| 176 | "https://contoso-my.sharepoint.com/personal/ann_contoso_com/Documents/Chat%20Files/clip.mp4", |
| 177 | ); |
| 178 | let expected = "https://contoso-my.sharepoint.com/personal/ann_contoso_com/_layouts/15/embed.aspx?id=/personal/ann_contoso_com/Documents/Chat%20Files/clip.mp4&embed=%7B%22ust%22%3Atrue%2C%22hv%22%3A%22CopyEmbedCode%22%7D"; |
| 179 | assert_eq!(url.as_deref(), Some(expected)); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn a_channel_video_plays_in_the_team_site_player() { |
| 184 | let url = player_url( |
| 185 | "https://contoso.sharepoint.com/sites/Dev/Shared%20Documents/General/demo.mp4?web=1", |
| 186 | ) |
| 187 | .unwrap(); |
| 188 | assert!(url.starts_with("https://contoso.sharepoint.com/sites/Dev/_layouts/15/embed.aspx?id=/sites/Dev/Shared%20Documents/General/demo.mp4&")); |
| 189 | } |
| 190 | |
| 191 | #[test] |
| 192 | fn other_hosts_have_no_inline_player() { |
| 193 | assert_eq!(player_url("https://example.com/personal/a/clip.mp4"), None); |
| 194 | assert_eq!( |
| 195 | player_url("https://evil.example?a.sharepoint.com/personal/x/y.mp4"), |
| 196 | None |
| 197 | ); |
| 198 | assert_eq!( |
| 199 | player_url("https://evil.example\\.sharepoint.com/personal/x/y.mp4"), |
| 200 | None |
| 201 | ); |
| 202 | assert_eq!(player_url("https://contoso.sharepoint.com/clip.mp4"), None); |
| 203 | assert_eq!( |
| 204 | player_url("https://contoso-my.sharepoint.com/personal/"), |
| 205 | None |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn only_the_parts_outside_the_clip_are_cut_away() { |
| 211 | let bounds = Bounds::new(point(px(0.), px(0.)), size(px(100.), px(100.))); |
| 212 | let visible = Bounds::new(point(px(0.), px(30.)), size(px(100.), px(50.))); |
| 213 | let parts = clipped_parts(bounds, visible); |
| 214 | assert_eq!(parts.len(), 2); |
| 215 | assert_eq!(parts[0].size.height, px(30.)); |
| 216 | assert_eq!(parts[1].origin.y, px(80.)); |
| 217 | assert!(clipped_parts(bounds, bounds).is_empty()); |
| 218 | } |
| 219 | } |