//! The bridge an app's web shell calls: find the host, hear its messages, post the app's. //! //! ```ignore //! if let Some(host) = craft_web_host::web::Host::connect("pdfcraft", &canvas) { //! host.listen(move |message| inbox.push(message)); //! host.post_ready(env!("CARGO_PKG_VERSION"), false); //! } //! ``` use std::cell::Cell; use std::rc::Rc; use wasm_bindgen::closure::Closure; use wasm_bindgen::{JsCast as _, JsValue}; use web_sys::js_sys; use crate::{Inbox, Incoming, Mode, Outgoing, VERSION, file_name, message_type}; /// A file the host passes in, not yet copied into the app's memory: check its size first. pub enum FileData { /// `bytes`: an `ArrayBuffer` or `Uint8Array`, viewed in place. Bytes(js_sys::Uint8Array), /// `file`: a `File` or `Blob`, for the app to read in slices. Blob(web_sys::Blob), } impl FileData { /// The file's length in bytes, from the data itself (a host's `size` field is not trusted). pub fn size(&self) -> f64 { match self { Self::Bytes(array) => f64::from(array.length()), Self::Blob(blob) => blob.size(), } } } /// A message from the host. pub enum Message { /// What the host supports; `handoff`: it routes files the app hands off. Host { version: String, handoff: bool }, /// Open a document as File › Open would. `name` is already cleaned ([`crate::file_name`]). Open { name: String, data: FileData }, /// Attach one PDF page to the current drawing without opening another document. /// Bytes stay a view until the app checks their length. Page numbers start at one. AttachPdf { name: String, bytes: js_sys::Uint8Array, page: u32, insertion: [f64; 3], width: f64, height: f64 }, /// The save with this `id` is stored. Saved { id: String }, /// The host gave a handed-off file to the app that edits it. HandoffDone { name: String, app: String }, } enum Transport { /// The same-origin parent window; only it is heard and answered. Parent { window: web_sys::Window, parent: web_sys::Window, origin: String }, /// The app's canvas; messages are `CustomEvent`s on it. Page { canvas: web_sys::EventTarget }, } /// The connection to the page hosting the app. pub struct Host { app: &'static str, transport: Transport, dirty: Cell>, } impl Host { /// The host for `app` (its id, as in `:ready`) running on `canvas`, or `None` when the /// app is not hosted: no `?host=parent` and no `data-host="page"`, no framing parent, or a /// parent on another origin (messages only go to this origin, so the browser would drop them). pub fn connect(app: &'static str, canvas: &web_sys::Element) -> Option { let window = web_sys::window()?; let query = window.location().search().unwrap_or_default(); let transport = match Mode::detect(&query, canvas.get_attribute("data-host").as_deref())? { Mode::Parent => { let parent = window.parent().ok().flatten()?; if js_sys::Object::is(&parent, &window) { return None; } let origin = window.location().origin().ok()?; // Reading another origin's location throws, which is `None` too. if parent.location().origin().ok()? != origin { return None; } Transport::Parent { window, parent, origin } } Mode::Page => Transport::Page { canvas: canvas.clone().into() }, }; Some(Self { app, transport, dirty: Cell::new(None) }) } pub fn mode(&self) -> Mode { match self.transport { Transport::Parent { .. } => Mode::Parent, Transport::Page { .. } => Mode::Page, } } /// Hear the host's messages for this app. The listener lives as long as the page. The /// listeners are `Fn`: a page host can dispatch an event while one is still running (a /// `saved` for the `save` the handler just posted), and wasm-bindgen refuses to re-enter an /// `FnMut` closure before its body runs, so the message would never reach the queue. pub fn listen(&self, handler: impl FnMut(Message) + 'static) { let app = self.app; let inbox = Rc::new(Inbox::new(handler)); match &self.transport { Transport::Parent { window, parent, origin } => { let (parent, origin, inbox) = (parent.clone(), origin.clone(), inbox.clone()); let on_message = Closure::::new(move |event: web_sys::MessageEvent| { if event.origin() != origin || !event.source().is_some_and(|s| js_sys::Object::is(&s, &parent)) { return; } let data = event.data(); let Some(kind) = string(&data, "type").and_then(|t| Incoming::parse(app, &t)) else { return }; if let Some(message) = read(kind, &data) { inbox.deliver(message); } }); if window.add_event_listener_with_callback("message", on_message.as_ref().unchecked_ref()).is_ok() { on_message.forget(); } } Transport::Page { canvas } => { for kind in Incoming::ALL { let inbox = inbox.clone(); let on_event = Closure::::new(move |event: web_sys::CustomEvent| { if let Some(message) = read(kind, &event.detail()) { inbox.deliver(message); } }); let name = message_type(app, kind.kind()); if canvas.add_event_listener_with_callback(&name, on_event.as_ref().unchecked_ref()).is_ok() { on_event.forget(); } } } } } /// The app is listening: send a document now. `app_version` is the app's own release; /// `reads_files`: it takes a `File`/`Blob` and reads it in slices. pub fn post_ready(&self, app_version: &str, reads_files: bool) -> Result<(), String> { self.post(Outgoing::Ready, &[("version", VERSION.into()), ("appVersion", app_version.into()), ("file", reads_files.into())], None) } /// Unsaved changes appeared or went away. Repeats of the last state reported are not sent. /// The new state is recorded before the send: a page host can report another state while the /// event is still dispatching, and that later report wins. A failed send is forgotten. pub fn post_dirty(&self, dirty: bool) { crate::note_dirty(&self.dirty, dirty, || self.post(Outgoing::Dirty, &[("dirty", dirty.into())], None).is_ok()); } /// Hand the host a file the app would otherwise download: Save, Save As, Export. With an /// `id`, the host answers [`Message::Saved`] once it is stored; without one the save is done /// once it is posted. The bytes go as a transferred `ArrayBuffer`. pub fn post_save(&self, name: &str, bytes: &[u8], id: Option<&str>, save_as: bool) -> Result<(), String> { let buffer = js_sys::Uint8Array::from(bytes).buffer(); let mut fields = vec![("name", JsValue::from(file_name(Some(name), "document"))), ("bytes", buffer.clone().into()), ("saveAs", save_as.into())]; if let Some(id) = id { fields.push(("id", id.into())); } self.post(Outgoing::Save, &fields, Some(&buffer)) } /// The app couldn't start, couldn't take a document, or a save went unconfirmed (`id`). pub fn post_failed(&self, error: &str, id: Option<&str>) -> Result<(), String> { let mut fields = vec![("error", JsValue::from(error))]; if let Some(id) = id { fields.push(("id", id.into())); } self.post(Outgoing::Failed, &fields, None) } /// Pass the host a file another app edits; it answers [`Message::HandoffDone`] or, when no /// app it hosts takes the type, `failed`. Without a host, tell the user which app the file /// needs instead. pub fn post_handoff(&self, name: &str, bytes: &[u8]) -> Result<(), String> { let buffer = js_sys::Uint8Array::from(bytes).buffer(); self.post(Outgoing::Handoff, &[("name", file_name(Some(name), "document").into()), ("bytes", buffer.clone().into())], Some(&buffer)) } fn post(&self, kind: Outgoing, fields: &[(&str, JsValue)], transfer: Option<&js_sys::ArrayBuffer>) -> Result<(), String> { let message_type = message_type(self.app, kind.kind()); let error = |e: JsValue| e.as_string().unwrap_or_else(|| format!("{e:?}")); match &self.transport { Transport::Parent { parent, origin, .. } => { let message = object(&[("type", message_type.as_str().into())], fields)?; match transfer { Some(buffer) => parent.post_message_with_transfer(&message, origin, &js_sys::Array::of1(buffer)), None => parent.post_message(&message, origin), } .map_err(error) } Transport::Page { canvas } => { let init = web_sys::CustomEventInit::new(); init.set_detail(&object(&[], fields)?.into()); let event = web_sys::CustomEvent::new_with_event_init_dict(&message_type, &init).map_err(error)?; canvas.dispatch_event(&event).map(|_| ()).map_err(error) } } } } /// The message a host's object describes, or `None` when a required field is missing. fn read(kind: Incoming, data: &JsValue) -> Option { Some(match kind { Incoming::Host => Message::Host { version: string(data, "version").unwrap_or_else(|| VERSION.to_string()), handoff: get(data, "handoff").as_bool() == Some(true), }, Incoming::Open => { let name = file_name(string(data, "name").as_deref(), "document"); Message::Open { name, data: file_data(data)? } } Incoming::AttachPdf => { let FileData::Bytes(bytes) = file_data(data)? else { return None }; let page = get(data, "page").as_f64()?; if !(page.is_finite() && page >= 1.0 && page <= f64::from(u32::MAX) && page.fract() == 0.0) { return None; } let coords = get(data, "insertion").dyn_into::().ok()?; if coords.length() != 3 { return None; } let mut insertion = [0.0; 3]; for (i, out) in insertion.iter_mut().enumerate() { *out = coords.get(u32::try_from(i).ok()?).as_f64().filter(|v| v.is_finite())?; } let width = get(data, "width").as_f64().filter(|v| v.is_finite() && *v > 0.0)?; let height = get(data, "height").as_f64().filter(|v| v.is_finite() && *v > 0.0)?; Message::AttachPdf { name: file_name(string(data, "name").as_deref(), "document.pdf"), bytes, page: page as u32, insertion, width, height, } } Incoming::Saved => Message::Saved { id: string(data, "id")? }, Incoming::HandoffDone => { Message::HandoffDone { name: string(data, "name").unwrap_or_default(), app: string(data, "app").unwrap_or_default() } } }) } /// `file` (a `Blob`; its `size` field is ignored for the Blob's own) or `bytes` (an `ArrayBuffer` or `Uint8Array`), not copied. fn file_data(data: &JsValue) -> Option { let file = get(data, "file"); if let Some(blob) = file.dyn_ref::() { return Some(FileData::Blob(blob.clone())); } let bytes = get(data, "bytes"); if let Some(array) = bytes.dyn_ref::() { Some(FileData::Bytes(array.clone())) } else if bytes.is_instance_of::() { Some(FileData::Bytes(js_sys::Uint8Array::new(&bytes))) } else { None } } fn object(head: &[(&str, JsValue)], fields: &[(&str, JsValue)]) -> Result { let m = js_sys::Object::new(); for (key, value) in head.iter().chain(fields) { js_sys::Reflect::set(&m, &JsValue::from_str(key), value).map_err(|_| "couldn't build the message".to_string())?; } Ok(m) } fn get(data: &JsValue, key: &str) -> JsValue { js_sys::Reflect::get(data, &JsValue::from_str(key)).unwrap_or(JsValue::UNDEFINED) } fn string(data: &JsValue, key: &str) -> Option { get(data, key).as_string() } /// Browser tests for parsing and delivery of a host's objects, including origin/source checks. #[cfg(test)] mod tests { use super::*; use wasm_bindgen_test::wasm_bindgen_test; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] fn test_attach_pdf_rejects_foreign_origin() { let window = web_sys::window().unwrap(); let origin = window.location().origin().unwrap(); let host = Host { app: "pdf-attach-test", transport: Transport::Parent { window: window.clone(), parent: window.clone(), origin: origin.clone() }, dirty: Cell::new(None), }; let received = Rc::new(Cell::new(0)); let counter = received.clone(); host.listen(move |_| counter.set(counter.get() + 1)); let data = obj(&[ ("type", "pdf-attach-test:attach-pdf".into()), ("name", "page.pdf".into()), ("bytes", buffer(3).into()), ("page", 1.into()), ("insertion", js_sys::Array::of3(&10.into(), &20.into(), &3.into()).into()), ("width", 100.into()), ("height", 80.into()), ]); let send = |origin: &str, source: Option<&web_sys::Window>| { let init = web_sys::MessageEventInit::new(); init.set_origin(origin); init.set_source(source.map(|s| s.as_ref())); init.set_data(&data); let event = web_sys::MessageEvent::new_with_event_init_dict("message", &init).unwrap(); window.dispatch_event(&event).unwrap(); }; send("https://foreign.example", Some(&window)); assert_eq!(received.get(), 0, "a foreign origin changes nothing"); send(&origin, None); assert_eq!(received.get(), 0, "the expected origin with another source changes nothing"); send(&origin, Some(&window)); assert_eq!(received.get(), 1, "the same payload from the authorized parent is delivered"); } fn obj(fields: &[(&str, JsValue)]) -> JsValue { object(&[], fields).expect("an object").into() } #[wasm_bindgen_test] fn attach_pdf_checks_page_rectangle_and_bytes() { let valid = || { obj(&[ ("name", "../sheet.pdf".into()), ("bytes", buffer(3).into()), ("page", 2.into()), ("insertion", js_sys::Array::of3(&10.into(), &20.into(), &3.into()).into()), ("width", 100.into()), ("height", 80.into()), ]) }; let Some(Message::AttachPdf { name, bytes, page, insertion, width, height }) = read(Incoming::AttachPdf, &valid()) else { panic!("valid attachment must be delivered"); }; assert_eq!(name, "sheet.pdf"); assert_eq!(bytes.length(), 3); assert_eq!((page, insertion, width, height), (2, [10.0, 20.0, 3.0], 100.0, 80.0)); for (key, value) in [ ("bytes", JsValue::NULL), ("page", 0.into()), ("page", 1.5.into()), ("page", f64::INFINITY.into()), ("page", 4294967296.0.into()), ("insertion", js_sys::Array::of2(&0.into(), &0.into()).into()), ("insertion", js_sys::Array::of3(&0.into(), &f64::NAN.into(), &0.into()).into()), ("width", 0.into()), ("height", (-1).into()), ("width", "100".into()), ] { let data = valid(); js_sys::Reflect::set(&data, &key.into(), &value).unwrap(); assert!(read(Incoming::AttachPdf, &data).is_none(), "invalid {key} must be dropped"); } } fn buffer(len: u32) -> js_sys::ArrayBuffer { js_sys::ArrayBuffer::new(len) } }