Skip to main content

nativeview_cli/pair/
host_reg.rs

1//! RAII Wave-1 Direct-FFI [`nativeview_abi::DirectHostRegistry`] registration
2//! (`PairHostRegistration`) forwarding host capability calls (`clipboard`, `linking`, `system-env`)
3//! from `nv pair` apps to the paired remote device.
4
5use std::sync::Arc;
6
7use nativeview_abi::OwnedHostOps;
8use nativeview_clipboard::spec::wire::WireClipboardClient;
9use nativeview_clipboard::{box_clipboard_host_ops, NvModClipboardHostOps};
10use nativeview_linking::spec::wire::WireLinkingClient;
11use nativeview_linking::{box_linking_host_ops, NvModLinkingHostOps};
12use nativeview_system_env::spec::wire::WireSystemEnvClient;
13use nativeview_system_env::{box_system_env_host_ops, NvModSystemEnvHostOps};
14
15use crate::pair_host::PairByteTransport;
16
17/// RAII owner of Wave-1 OS capability `OwnedHostOps` tables backed by a [`PairByteTransport`].
18///
19/// Calling [`Self::register_global`] installs them into [`nativeview_abi::DirectHostRegistry::global`]
20/// for the lifetime of the pairing session; dropping [`PairHostRegistration`] shuts down the
21/// transport and unregisters all ops tables cleanly.
22pub struct PairHostRegistration {
23    transport: PairByteTransport,
24    clipboard: OwnedHostOps<NvModClipboardHostOps>,
25    linking: OwnedHostOps<NvModLinkingHostOps>,
26    system_env: OwnedHostOps<NvModSystemEnvHostOps>,
27}
28
29impl PairHostRegistration {
30    /// Boxes all Wave-1 generated `Wire<Mod>Client<PairByteTransport>` tables without modifying
31    /// `DirectHostRegistry::global` (call [`Self::register_global`] for production `nv pair`).
32    pub fn new(transport: PairByteTransport) -> Self {
33        Self {
34            clipboard: box_clipboard_host_ops(Arc::new(WireClipboardClient::new(
35                transport.clone(),
36            ))),
37            linking: box_linking_host_ops(Arc::new(WireLinkingClient::new(transport.clone()))),
38            system_env: box_system_env_host_ops(Arc::new(WireSystemEnvClient::new(
39                transport.clone(),
40            ))),
41            transport,
42        }
43    }
44
45    /// Registers all wire-backed host ops tables into [`nativeview_abi::DirectHostRegistry::global`].
46    /// Returns any failed `(module_name, status_code)` entries so callers can log or abort.
47    pub fn register_global(&self) -> Result<(), Vec<(&'static str, i32)>> {
48        let mut errs = Vec::new();
49        let mut check = |name: &'static str, status: nativeview_abi::NvModStatus| {
50            if !status.is_ok() {
51                errs.push((name, status as i32));
52            }
53        };
54        check("clipboard", self.clipboard.register_global());
55        check("linking", self.linking.register_global());
56        check("system-env", self.system_env.register_global());
57        if errs.is_empty() {
58            Ok(())
59        } else {
60            Err(errs)
61        }
62    }
63
64    pub fn transport(&self) -> &PairByteTransport {
65        &self.transport
66    }
67
68    pub fn clipboard_ops(&self) -> &OwnedHostOps<NvModClipboardHostOps> {
69        &self.clipboard
70    }
71
72    pub fn linking_ops(&self) -> &OwnedHostOps<NvModLinkingHostOps> {
73        &self.linking
74    }
75
76    pub fn system_env_ops(&self) -> &OwnedHostOps<NvModSystemEnvHostOps> {
77        &self.system_env
78    }
79}
80
81impl Drop for PairHostRegistration {
82    fn drop(&mut self) {
83        self.transport.shutdown();
84    }
85}