use std::sync::Arc;
use nativeview_abi::OwnedHostOps;
use nativeview_clipboard::spec::wire::WireClipboardClient;
use nativeview_clipboard::{box_clipboard_host_ops, NvModClipboardHostOps};
use nativeview_linking::spec::wire::WireLinkingClient;
use nativeview_linking::{box_linking_host_ops, NvModLinkingHostOps};
use nativeview_system_env::spec::wire::WireSystemEnvClient;
use nativeview_system_env::{box_system_env_host_ops, NvModSystemEnvHostOps};
use crate::pair_host::PairByteTransport;
pub struct PairHostRegistration {
transport: PairByteTransport,
clipboard: OwnedHostOps<NvModClipboardHostOps>,
linking: OwnedHostOps<NvModLinkingHostOps>,
system_env: OwnedHostOps<NvModSystemEnvHostOps>,
}
impl PairHostRegistration {
pub fn new(transport: PairByteTransport) -> Self {
Self {
clipboard: box_clipboard_host_ops(Arc::new(WireClipboardClient::new(
transport.clone(),
))),
linking: box_linking_host_ops(Arc::new(WireLinkingClient::new(transport.clone()))),
system_env: box_system_env_host_ops(Arc::new(WireSystemEnvClient::new(
transport.clone(),
))),
transport,
}
}
pub fn register_global(&self) -> Result<(), Vec<(&'static str, i32)>> {
let mut errs = Vec::new();
let mut check = |name: &'static str, status: nativeview_abi::NvModStatus| {
if !status.is_ok() {
errs.push((name, status as i32));
}
};
check("clipboard", self.clipboard.register_global());
check("linking", self.linking.register_global());
check("system-env", self.system_env.register_global());
if errs.is_empty() {
Ok(())
} else {
Err(errs)
}
}
pub fn transport(&self) -> &PairByteTransport {
&self.transport
}
pub fn clipboard_ops(&self) -> &OwnedHostOps<NvModClipboardHostOps> {
&self.clipboard
}
pub fn linking_ops(&self) -> &OwnedHostOps<NvModLinkingHostOps> {
&self.linking
}
pub fn system_env_ops(&self) -> &OwnedHostOps<NvModSystemEnvHostOps> {
&self.system_env
}
}
impl Drop for PairHostRegistration {
fn drop(&mut self) {
self.transport.shutdown();
}
}