Skip to main content

matrix_sdk_indexeddb/
lib.rs

1#![cfg_attr(not(target_family = "wasm"), allow(unused))]
2
3#[cfg(feature = "state-store")]
4use matrix_sdk_base::store::StoreError;
5use thiserror::Error;
6
7#[cfg(feature = "e2e-encryption")]
8mod crypto_store;
9mod durability;
10mod error;
11#[cfg(feature = "event-cache-store")]
12mod event_cache_store;
13#[cfg(feature = "media-store")]
14mod media_store;
15mod serializer;
16#[cfg(feature = "state-store")]
17mod state_store;
18#[cfg(any(feature = "event-cache-store", feature = "media-store"))]
19mod transaction;
20
21#[cfg(feature = "e2e-encryption")]
22pub use crypto_store::{IndexeddbCryptoStore, IndexeddbCryptoStoreError};
23#[cfg(feature = "state-store")]
24pub use state_store::{
25    IndexeddbStateStore, IndexeddbStateStoreBuilder, IndexeddbStateStoreError,
26    MigrationConflictStrategy,
27};
28
29#[cfg(feature = "event-cache-store")]
30pub use crate::event_cache_store::{
31    IndexeddbEventCacheStore, IndexeddbEventCacheStoreBuilder, IndexeddbEventCacheStoreError,
32};
33#[cfg(feature = "media-store")]
34pub use crate::media_store::{
35    IndexeddbMediaStore, IndexeddbMediaStoreBuilder, IndexeddbMediaStoreError,
36};
37
38/// Structure containing implementations of every type of store using IndexedDB
39/// for persistent storage.
40///
41/// Note that each of the stores is behind a feature flag and will only be
42/// available when its corresponding flag is set.
43pub struct IndexeddbStores {
44    /// An IndexedDB-backed implementation of [`CryptoStore`][1]
45    ///
46    /// [1]: matrix_sdk_crypto::store::CryptoStore
47    #[cfg(feature = "e2e-encryption")]
48    pub crypto: IndexeddbCryptoStore,
49    /// An IndexedDB-backed implementation of [`StateStore`][1]
50    ///
51    /// [1]: matrix_sdk_base::store::StateStore
52    #[cfg(feature = "state-store")]
53    pub state: IndexeddbStateStore,
54    /// An IndexedDB-backed implementation of [`EventCacheStore`][1]
55    ///
56    /// [1]: matrix_sdk_base::event_cache::store::EventCacheStore
57    #[cfg(feature = "event-cache-store")]
58    pub event_cache: IndexeddbEventCacheStore,
59    /// An IndexedDB-backed implementation of [`MediaStore`][1]
60    ///
61    /// [1]: matrix_sdk_base::media::store::MediaStore
62    #[cfg(feature = "media-store")]
63    pub media: IndexeddbMediaStore,
64}
65
66impl IndexeddbStores {
67    /// Opens and returns all stores using the given database name and,
68    /// optionally, a passphrase.
69    ///
70    /// If `e2e-encryption` and `state-store` features are not enabled,
71    /// `passphrase` is ignored. Otherwise `passphrase` is used to import or
72    /// create a [`StoreCipher`][1] which encrypts contents of all stores.
73    ///
74    /// Note that each of the stores is behind a feature flag and will only be
75    /// opened when the corresponding flag is set.
76    ///
77    /// [1]: matrix_sdk_store_encryption::StoreCipher
78    pub async fn open(
79        name: &str,
80        #[allow(unused_variables)] passphrase: Option<&str>,
81    ) -> Result<Self, OpenStoreError> {
82        #[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
83        if let Some(passphrase) = passphrase {
84            return Self::open_with_passphrase(name, passphrase).await;
85        }
86
87        Self::open_without_passphrase(name).await
88    }
89
90    /// Opens and returns all stores using the given database name. Contents of
91    /// the stores are NOT encrypted.
92    ///
93    /// Note that each of the stores is behind a feature flag and will only be
94    /// opened when the corresponding flag is set.
95    #[allow(clippy::unused_async)]
96    pub async fn open_without_passphrase(name: &str) -> Result<Self, OpenStoreError> {
97        #[cfg(feature = "state-store")]
98        let state = IndexeddbStateStore::builder()
99            .name(name.to_owned())
100            .build()
101            .await
102            .map_err(StoreError::from)?;
103
104        #[cfg(feature = "e2e-encryption")]
105        let crypto = IndexeddbCryptoStore::open_with_name(name).await?;
106
107        #[cfg(feature = "event-cache-store")]
108        let event_cache = IndexeddbEventCacheStoreBuilder::with_prefix(name).build().await?;
109
110        #[cfg(feature = "media-store")]
111        let media = IndexeddbMediaStoreBuilder::with_prefix(name).build().await?;
112
113        Ok(Self {
114            #[cfg(feature = "state-store")]
115            state,
116            #[cfg(feature = "e2e-encryption")]
117            crypto,
118            #[cfg(feature = "event-cache-store")]
119            event_cache,
120            #[cfg(feature = "media-store")]
121            media,
122        })
123    }
124
125    /// Opens and returns all stores using the given database name and
126    /// passphrase. Passphrase is used to import or create a [`StoreCipher`][1]
127    /// which encrypts contents of all stores.
128    ///
129    /// Note that [`IndexeddbEventCacheStore`] and [`IndexeddbMediaStore`] are
130    /// behind feature flags and will only be opened when their corresponding
131    /// flags are set.
132    ///
133    /// [1]: matrix_sdk_store_encryption::StoreCipher
134    #[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
135    pub async fn open_with_passphrase(
136        name: &str,
137        passphrase: &str,
138    ) -> Result<Self, OpenStoreError> {
139        let state = IndexeddbStateStore::builder()
140            .name(name.to_owned())
141            .passphrase(passphrase.to_owned())
142            .build()
143            .await
144            .map_err(StoreError::from)?;
145        let store_cipher =
146            state.store_cipher.clone().ok_or(OpenStoreError::FailedToLoadStoreCipher)?;
147
148        let crypto =
149            IndexeddbCryptoStore::open_with_store_cipher(name, Some(store_cipher.clone())).await?;
150
151        #[cfg(feature = "event-cache-store")]
152        let event_cache = IndexeddbEventCacheStoreBuilder::with_prefix(name)
153            .store_cipher(store_cipher.clone())
154            .build()
155            .await?;
156
157        #[cfg(feature = "media-store")]
158        let media = IndexeddbMediaStoreBuilder::with_prefix(name)
159            .store_cipher(store_cipher.clone())
160            .build()
161            .await?;
162
163        Ok(Self {
164            state,
165            crypto,
166            #[cfg(feature = "event-cache-store")]
167            event_cache,
168            #[cfg(feature = "media-store")]
169            media,
170        })
171    }
172}
173
174/// Create a [`IndexeddbStateStore`] and a [`IndexeddbCryptoStore`] that use the
175/// same name and passphrase.
176#[deprecated(
177    note = "this function only opens state and crypto stores, use `IndexeddbStores::open()` instead."
178)]
179#[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
180pub async fn open_stores_with_name(
181    name: &str,
182    passphrase: Option<&str>,
183) -> Result<(IndexeddbStateStore, IndexeddbCryptoStore), OpenStoreError> {
184    let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
185    if let Some(passphrase) = passphrase {
186        builder = builder.passphrase(passphrase.to_owned());
187    }
188
189    let state_store = builder.build().await.map_err(StoreError::from)?;
190    let crypto_store =
191        IndexeddbCryptoStore::open_with_store_cipher(name, state_store.store_cipher.clone())
192            .await?;
193
194    Ok((state_store, crypto_store))
195}
196
197/// Create an [`IndexeddbStateStore`].
198///
199/// If a `passphrase` is given, the store will be encrypted using a key derived
200/// from that passphrase.
201#[cfg(feature = "state-store")]
202pub async fn open_state_store(
203    name: &str,
204    passphrase: Option<&str>,
205) -> Result<IndexeddbStateStore, OpenStoreError> {
206    let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
207    if let Some(passphrase) = passphrase {
208        builder = builder.passphrase(passphrase.to_owned());
209    }
210    let state_store = builder.build().await.map_err(StoreError::from)?;
211
212    Ok(state_store)
213}
214
215/// All the errors that can occur when opening an IndexedDB store.
216#[derive(Error, Debug)]
217pub enum OpenStoreError {
218    /// An error occurred with the state store implementation.
219    #[cfg(feature = "state-store")]
220    #[error(transparent)]
221    State(#[from] StoreError),
222
223    /// An error occurred with the crypto store implementation.
224    #[cfg(feature = "e2e-encryption")]
225    #[error(transparent)]
226    Crypto(#[from] IndexeddbCryptoStoreError),
227
228    /// An error occurred while trying to load a [`StoreCipher`][1] from a
229    /// passphrase
230    ///
231    /// [1]: matrix_sdk_store_encryption::StoreCipher
232    #[cfg(feature = "e2e-encryption")]
233    #[error("failed to load store cipher")]
234    FailedToLoadStoreCipher,
235
236    /// An error occurred with the event cache store implementation.
237    #[cfg(feature = "event-cache-store")]
238    #[error(transparent)]
239    Event(#[from] IndexeddbEventCacheStoreError),
240
241    /// An error occurred with the media store implementation.
242    #[cfg(feature = "media-store")]
243    #[error(transparent)]
244    Media(#[from] IndexeddbMediaStoreError),
245}