bitwarden_wasm_internal/platform/
repository.rs

1/*!
2 * To support clients implementing the [Repository] trait in a [::wasm_bindgen] environment,
3 * we need to deal with an `extern "C"` interface, as that is what [::wasm_bindgen] supports:
4 *
5 * This looks something like this:
6 *
7 * ```rust,ignore
8 * #[wasm_bindgen]
9 * extern "C" {
10 *     pub type CipherRepository;
11 *
12 *     #[wasm_bindgen(method, catch)]
13 *     async fn get(this: &CipherRepository, id: String) -> Result<JsValue, JsValue>;
14 * }
15 * ```
16 *
17 * As you can see, this has a few limitations:
18 * - The type must be known at compile time, so we cannot use generics directly, which means we
19 *   can't use the existing [Repository] trait directly.
20 * - The return type must be [JsValue], so we need to convert our types to and from [JsValue].
21 *
22 * To facilitate this, we provide some utilities:
23 * - [WasmRepository] trait, which defines the methods as we expect them to come from
24 *   [::wasm_bindgen], using [JsValue]. This is generic and should be implemented for each
25 *   concrete repository we define, but the implementation should be very straightforward.
26 * - [WasmRepositoryChannel] struct, which wraps a [WasmRepository] in a [ThreadBoundRunner] and
27 *   implements the [Repository] trait. This has a few special considerations:
28 *   - It uses [tsify::serde_wasm_bindgen] to convert between [JsValue] and our types, so we can
29 *     use the existing [Repository] trait.
30 *   - It runs the calls in a thread-bound manner, so we can safely call the [WasmRepository]
31 *     methods from any thread.
32 * - The [create_wasm_repositories] macro, defines the [::wasm_bindgen] interface and implements
33 *   the [WasmRepository] trait for you.
34 */
35
36use std::{future::Future, marker::PhantomData, rc::Rc};
37
38use bitwarden_state::repository::{Repository, RepositoryError, RepositoryItem};
39use bitwarden_threading::ThreadBoundRunner;
40use serde::{Serialize, de::DeserializeOwned};
41use wasm_bindgen::{JsValue, prelude::wasm_bindgen};
42
43/// This trait defines the methods that a [::wasm_bindgen] repository must implement.
44/// The trait itself exists to provide a generic way of handling the [::wasm_bindgen] interface,
45/// which is !Send + !Sync, and only deals with [JsValue].
46pub(crate) trait WasmRepository<T> {
47    async fn get(&self, id: String) -> Result<JsValue, JsValue>;
48    async fn list(&self) -> Result<JsValue, JsValue>;
49    async fn set(&self, id: String, value: T) -> Result<JsValue, JsValue>;
50    async fn remove(&self, id: String) -> Result<JsValue, JsValue>;
51}
52
53/// This struct wraps a [WasmRepository] in a [ThreadBoundRunner] to allow it to be used as a
54/// [Repository] in a thread-safe manner. It implements the [Repository] trait directly, by
55/// converting the values as needed with [tsify::serde_wasm_bindgen].
56pub(crate) struct WasmRepositoryChannel<T, R: WasmRepository<T> + 'static>(
57    ThreadBoundRunner<R>,
58    PhantomData<T>,
59);
60
61impl<T, R: WasmRepository<T> + 'static> WasmRepositoryChannel<T, R> {
62    pub(crate) fn new(repository: R) -> Self {
63        Self(ThreadBoundRunner::new(repository), PhantomData)
64    }
65}
66
67#[async_trait::async_trait]
68impl<T: RepositoryItem + Serialize + DeserializeOwned, R: WasmRepository<T> + 'static> Repository<T>
69    for WasmRepositoryChannel<T, R>
70{
71    async fn get(&self, id: String) -> Result<Option<T>, RepositoryError> {
72        run_convert(&self.0, |s| async move { s.get(id).await }).await
73    }
74    async fn list(&self) -> Result<Vec<T>, RepositoryError> {
75        run_convert(&self.0, |s| async move { s.list().await }).await
76    }
77    async fn set(&self, id: String, value: T) -> Result<(), RepositoryError> {
78        run_convert(&self.0, |s| async move { s.set(id, value).await.and(UNIT) }).await
79    }
80    async fn remove(&self, id: String) -> Result<(), RepositoryError> {
81        run_convert(&self.0, |s| async move { s.remove(id).await.and(UNIT) }).await
82    }
83}
84
85#[wasm_bindgen(typescript_custom_section)]
86const REPOSITORY_CUSTOM_TS_TYPE: &'static str = r#"
87export interface Repository<T> {
88    get(id: string): Promise<T | null>;
89    list(): Promise<T[]>;
90    set(id: string, value: T): Promise<void>;
91    remove(id: string): Promise<void>;
92}
93"#;
94
95/// This macro generates a [::wasm_bindgen] interface for a repository type, and provides the
96/// implementation of [WasmRepository] and a way to convert it into something that implements
97/// the [Repository] trait.
98macro_rules! create_wasm_repositories {
99    ( $container_name:ident ; $( $qualified_type_name:ty, $type_name:ident, $field_name:ident, $repo_name:ident );+ $(;)? ) => {
100
101        const _: () = {
102            #[wasm_bindgen(typescript_custom_section)]
103            const REPOSITORIES_CUSTOM_TS_TYPE: &'static str = concat!(
104                "export interface ",
105                stringify!($container_name),
106                "{\n",
107                $( stringify!($field_name), ": Repository<", stringify!($type_name), "> | null;\n", )+
108                "}\n"
109            );
110        };
111
112        #[wasm_bindgen]
113        extern "C" {
114            #[wasm_bindgen(typescript_type = $container_name)]
115            pub type $container_name;
116
117            $(
118                #[wasm_bindgen(method, getter)]
119                pub fn $field_name(this: &$container_name) -> Option<$repo_name>;
120            )+
121        }
122
123        impl $container_name {
124            pub fn register_all(self, client: &bitwarden_core::platform::StateClient) {
125                $(
126                    if let Some(repo) = self.$field_name() {
127                        let repo = repo.into_channel_impl();
128                        client.register_client_managed(repo);
129                    }
130                )+
131            }
132        }
133
134        $(
135            #[wasm_bindgen]
136            extern "C" {
137                #[wasm_bindgen]
138                pub type $repo_name;
139
140                #[wasm_bindgen(method, catch)]
141                async fn get(
142                    this: &$repo_name,
143                    id: String,
144                ) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue>;
145                #[wasm_bindgen(method, catch)]
146                async fn list(this: &$repo_name)
147                    -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue>;
148                #[wasm_bindgen(method, catch)]
149                async fn set(
150                    this: &$repo_name,
151                    id: String,
152                    value: $qualified_type_name,
153                ) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue>;
154                #[wasm_bindgen(method, catch)]
155                async fn remove(
156                    this: &$repo_name,
157                    id: String,
158                ) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue>;
159            }
160
161            impl $crate::platform::repository::WasmRepository<$qualified_type_name> for $repo_name {
162                async fn get(
163                    &self,
164                    id: String,
165                ) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue> {
166                    self.get(id).await
167                }
168                async fn list(&self) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue> {
169                    self.list().await
170                }
171                async fn set(
172                    &self,
173                    id: String,
174                    value: $qualified_type_name,
175                ) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue> {
176                    self.set(id, value).await
177                }
178                async fn remove(
179                    &self,
180                    id: String,
181                ) -> Result<::wasm_bindgen::JsValue, ::wasm_bindgen::JsValue> {
182                    self.remove(id).await
183                }
184            }
185
186            impl $repo_name {
187                pub fn into_channel_impl(
188                    self,
189                ) -> ::std::sync::Arc<impl bitwarden_state::repository::Repository<$qualified_type_name>> {
190                    use $crate::platform::repository::WasmRepositoryChannel;
191                    ::std::sync::Arc::new(WasmRepositoryChannel::new(self))
192                }
193            }
194        )+
195    };
196}
197pub(crate) use create_wasm_repositories;
198
199const UNIT: Result<JsValue, JsValue> = Ok(JsValue::UNDEFINED);
200
201/// Utility function that runs a closure in a thread-bound manner, and converts the Result from
202/// [Result<JsValue, JsValue>] to a typed [Result<T, RepositoryError>].
203async fn run_convert<T: 'static, Func, Fut, Ret>(
204    runner: &::bitwarden_threading::ThreadBoundRunner<T>,
205    f: Func,
206) -> Result<Ret, RepositoryError>
207where
208    Func: FnOnce(Rc<T>) -> Fut + Send + 'static,
209    Fut: Future<Output = Result<JsValue, JsValue>>,
210    Ret: serde::de::DeserializeOwned + Send + Sync + 'static,
211{
212    runner
213        .run_in_thread(|state| async move { convert_result(f(state).await) })
214        .await
215        .expect("Task should not panic")
216}
217
218/// Converts a [Result<JsValue, JsValue>] to a typed [Result<T, RepositoryError>] using
219/// [tsify::serde_wasm_bindgen]
220fn convert_result<T: serde::de::DeserializeOwned>(
221    result: Result<JsValue, JsValue>,
222) -> Result<T, RepositoryError> {
223    result
224        .map_err(|e| RepositoryError::Internal(format!("{e:?}")))
225        .and_then(|value| {
226            ::tsify::serde_wasm_bindgen::from_value(value)
227                .map_err(|e| RepositoryError::Internal(e.to_string()))
228        })
229}