Skip to main content

bitwarden_ipc/
ipc_client_trait.rs

1use bitwarden_threading::cancellation_token::CancellationToken;
2
3use crate::{
4    error::{AlreadyRunningError, SendError, SubscribeError},
5    ipc_client::IpcClientSubscription,
6    message::OutgoingMessage,
7    rpc::exec::handler::ErasedRpcHandler,
8};
9
10/// Dyn-compatible trait for IPC client operations.
11///
12/// This trait provides the core IPC operations that consumers can use without
13/// being tied to a specific concrete `IpcClient` implementation. For generic
14/// convenience methods (typed subscriptions, RPC requests), use the
15/// [`IpcClientExt`](crate::IpcClientExt) extension trait which is automatically
16/// implemented for all `IpcClient` implementors.
17#[async_trait::async_trait]
18pub trait IpcClient: Send + Sync {
19    /// Start the IPC client, which will begin listening for incoming messages and processing them.
20    async fn start(
21        &self,
22        cancellation_token: Option<CancellationToken>,
23    ) -> Result<(), AlreadyRunningError>;
24
25    /// Check if the IPC client task is currently running.
26    fn is_running(&self) -> bool;
27
28    /// Send a message over IPC.
29    async fn send(&self, message: OutgoingMessage) -> Result<(), SendError>;
30
31    /// Subscribe to receive messages, optionally filtered by topic.
32    /// Setting the topic to `None` will receive all messages.
33    async fn subscribe(
34        &self,
35        topic: Option<String>,
36    ) -> Result<IpcClientSubscription, SubscribeError>;
37
38    /// Register an RPC handler using its type-erased form.
39    /// Prefer using
40    /// [`IpcClientExt::register_rpc_handler`](crate::IpcClientExt::register_rpc_handler)
41    /// instead.
42    #[doc(hidden)]
43    async fn register_rpc_handler_erased(&self, name: &str, handler: Box<dyn ErasedRpcHandler>);
44}