bitwarden_wasm_internal/platform/
mod.rs

1use bitwarden_core::Client;
2use bitwarden_state::{repository::RepositoryItem, DatabaseConfiguration};
3use bitwarden_vault::{Cipher, Folder};
4use serde::{Deserialize, Serialize};
5use tsify::Tsify;
6use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
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 async fn initialize_state(
68        &self,
69        configuration: IndexedDbConfiguration,
70    ) -> Result<(), bitwarden_state::registry::StateRegistryError> {
71        let sdk_managed_repositories = vec![
72            // This should list all the SDK-managed repositories
73            <Cipher as RepositoryItem>::data(),
74        ];
75
76        self.0
77            .platform()
78            .state()
79            .initialize_database(configuration.into(), sdk_managed_repositories)
80            .await
81    }
82
83    pub fn register_folder_repository(&self, store: FolderRepository) {
84        let store = store.into_channel_impl();
85        self.0.platform().state().register_client_managed(store)
86    }
87}
88
89impl From<IndexedDbConfiguration> for DatabaseConfiguration {
90    fn from(config: IndexedDbConfiguration) -> Self {
91        bitwarden_state::DatabaseConfiguration::IndexedDb {
92            db_name: config.db_name,
93        }
94    }
95}