bitwarden_wasm_internal/platform/
mod.rs

1use bitwarden_core::Client;
2use bitwarden_state::DatabaseConfiguration;
3use bitwarden_vault::{Cipher, Folder};
4use serde::{Deserialize, Serialize};
5use tsify::Tsify;
6use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
7
8mod repository;
9pub mod token_provider;
10
11/// Active feature flags for the SDK.
12#[derive(Serialize, Deserialize, Tsify)]
13#[tsify(into_wasm_abi, from_wasm_abi)]
14pub struct FeatureFlags {
15    /// We intentionally use a loose type here to allow for future flags without breaking changes.
16    #[serde(flatten)]
17    flags: std::collections::HashMap<String, bool>,
18}
19
20#[wasm_bindgen]
21pub struct PlatformClient(Client);
22
23impl PlatformClient {
24    pub fn new(client: Client) -> Self {
25        Self(client)
26    }
27}
28
29#[wasm_bindgen]
30impl PlatformClient {
31    pub fn state(&self) -> StateClient {
32        StateClient::new(self.0.clone())
33    }
34
35    /// Load feature flags into the client
36    pub fn load_flags(&self, flags: FeatureFlags) -> Result<(), JsValue> {
37        self.0.internal.load_flags(flags.flags);
38        Ok(())
39    }
40}
41
42#[wasm_bindgen]
43pub struct StateClient(Client);
44
45impl StateClient {
46    pub fn new(client: Client) -> Self {
47        Self(client)
48    }
49}
50
51repository::create_wasm_repository!(CipherRepository, Cipher, "Repository<Cipher>");
52repository::create_wasm_repository!(FolderRepository, Folder, "Repository<Folder>");
53
54#[derive(Serialize, Deserialize, Tsify)]
55#[tsify(into_wasm_abi, from_wasm_abi)]
56pub struct IndexedDbConfiguration {
57    pub db_name: String,
58}
59
60#[wasm_bindgen]
61impl StateClient {
62    pub fn register_cipher_repository(&self, cipher_repository: CipherRepository) {
63        let cipher = cipher_repository.into_channel_impl();
64        self.0.platform().state().register_client_managed(cipher);
65    }
66
67    pub fn register_folder_repository(&self, store: FolderRepository) {
68        let store = store.into_channel_impl();
69        self.0.platform().state().register_client_managed(store)
70    }
71
72    /// Initialize the database for SDK managed repositories.
73    pub async fn initialize_state(
74        &self,
75        configuration: IndexedDbConfiguration,
76    ) -> Result<(), bitwarden_state::registry::StateRegistryError> {
77        let sdk_managed_repositories = bitwarden_state_migrations::get_sdk_managed_migrations();
78
79        self.0
80            .platform()
81            .state()
82            .initialize_database(configuration.into(), sdk_managed_repositories)
83            .await
84    }
85}
86
87impl From<IndexedDbConfiguration> for DatabaseConfiguration {
88    fn from(config: IndexedDbConfiguration) -> Self {
89        bitwarden_state::DatabaseConfiguration::IndexedDb {
90            db_name: config.db_name,
91        }
92    }
93}