bitwarden_wasm_internal/platform/
mod.rs

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