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    ///
30    /// Returning an error means this particular send failed. The client only stops processing
31    /// messages when the underlying error is fatal (see
32    /// [`IpcErrorKind`](crate::IpcErrorKind)); recoverable errors leave the client running, so the
33    /// send can be retried and existing subscriptions remain valid.
34    async fn send(&self, message: OutgoingMessage) -> Result<(), SendError>;
35
36    /// Subscribe to receive messages, optionally filtered by topic.
37    /// Setting the topic to `None` will receive all messages.
38    async fn subscribe(
39        &self,
40        topic: Option<String>,
41    ) -> Result<IpcClientSubscription, SubscribeError>;
42
43    /// Register an RPC handler using its type-erased form.
44    /// Prefer using
45    /// [`IpcClientExt::register_rpc_handler`](crate::IpcClientExt::register_rpc_handler)
46    /// instead.
47    #[doc(hidden)]
48    async fn register_rpc_handler_erased(&self, name: &str, handler: Box<dyn ErasedRpcHandler>);
49}