Skip to main content

bitwarden_shared_unlock/wasm/
peer.rs

1use bitwarden_core::UserId;
2use bitwarden_threading::cancellation_token::wasm::{AbortController, AbortControllerExt};
3
4use super::drivers::{JsSharedUnlockDriver, RawJsSharedUnlockDriver};
5use crate::{DeviceEvent, PeerStartError, SharedUnlockClient, SharedUnlockPeer as Peer};
6
7/// Shared-unlock peer for WASM clients.
8#[bitwarden_ffi::wasm_object]
9pub struct SharedUnlockPeer {
10    peer: Peer<JsSharedUnlockDriver>,
11}
12
13#[bitwarden_ffi::wasm_export]
14impl SharedUnlockPeer {
15    /// Creates a new shared-unlock peer
16    #[wasm_bindgen(constructor)]
17    pub fn new(
18        ipc_client: &bitwarden_ipc::wasm::JsIpcClient,
19        driver: RawJsSharedUnlockDriver,
20    ) -> Self {
21        let driver = JsSharedUnlockDriver::new(driver);
22        let peer = Peer::create(driver, ipc_client.client.clone());
23        Self { peer }
24    }
25
26    /// Sets which clients this peer shares one user's unlock state with. Defaults to none.
27    #[wasm_bindgen]
28    pub fn set_destinations(&self, user_id: UserId, destinations: Vec<SharedUnlockClient>) {
29        self.peer.set_destinations(user_id, destinations);
30    }
31
32    /// Starts the shared-unlock peer
33    #[wasm_bindgen]
34    pub async fn start(
35        &self,
36        abort_controller: Option<AbortController>,
37    ) -> Result<(), PeerStartError> {
38        self.peer
39            .start(abort_controller.map(|abort| abort.to_cancellation_token()))
40            .await
41    }
42
43    /// Forwards a device event to the shared-unlock peer state machine.
44    #[wasm_bindgen]
45    pub async fn handle_device_event(&self, event: DeviceEvent) {
46        if let Err(error) = self.peer.handle_device_event(event).await {
47            tracing::error!(?error, "Failed to handle shared unlock device event");
48        }
49    }
50}