|
| 1 | +// Copyright 2025 The Matrix.org Foundation C.I.C. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License |
| 14 | + |
| 15 | +use std::sync::Arc; |
| 16 | + |
| 17 | +use matrix_sdk_base::event_cache::store::MemoryStore; |
| 18 | +use matrix_sdk_store_encryption::StoreCipher; |
| 19 | +use web_sys::DomException; |
| 20 | + |
| 21 | +use crate::{ |
| 22 | + event_cache_store::{ |
| 23 | + migrations::open_and_upgrade_db, serializer::IndexeddbEventCacheStoreSerializer, |
| 24 | + IndexeddbEventCacheStore, |
| 25 | + }, |
| 26 | + serializer::IndexeddbSerializer, |
| 27 | +}; |
| 28 | + |
| 29 | +/// A type for conveniently building an [`IndexeddbEventCacheStore`] |
| 30 | +pub struct IndexeddbEventCacheStoreBuilder { |
| 31 | + // The name of the IndexedDB database which will be opened |
| 32 | + database_name: String, |
| 33 | + // The store cipher, if any, to use when encrypting data |
| 34 | + // before it is persisted to the IndexedDB database |
| 35 | + store_cipher: Option<Arc<StoreCipher>>, |
| 36 | +} |
| 37 | + |
| 38 | +impl Default for IndexeddbEventCacheStoreBuilder { |
| 39 | + fn default() -> Self { |
| 40 | + Self { database_name: Self::DEFAULT_DATABASE_NAME.to_owned(), store_cipher: None } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl IndexeddbEventCacheStoreBuilder { |
| 45 | + /// The default name of the IndexedDB database used to back the |
| 46 | + /// [`IndexeddbEventCacheStore`] |
| 47 | + pub const DEFAULT_DATABASE_NAME: &'static str = "event_cache"; |
| 48 | + |
| 49 | + /// Sets the name of the IndexedDB database which will be opened. This |
| 50 | + /// defaults to [`Self::DEFAULT_DATABASE_NAME`]. |
| 51 | + pub fn database_name(mut self, name: String) -> Self { |
| 52 | + self.database_name = name; |
| 53 | + self |
| 54 | + } |
| 55 | + |
| 56 | + /// Sets the store cipher to use when encrypting data before it is persisted |
| 57 | + /// to the IndexedDB database. By default, no store cipher is used - |
| 58 | + /// i.e., data is not encrypted before it is persisted. |
| 59 | + pub fn store_cipher(mut self, store_cipher: Arc<StoreCipher>) -> Self { |
| 60 | + self.store_cipher = Some(store_cipher); |
| 61 | + self |
| 62 | + } |
| 63 | + |
| 64 | + /// Opens the IndexedDB database with the provided name. If successfully |
| 65 | + /// opened, builds the [`IndexeddbEventCacheStore`] with that database |
| 66 | + /// and the provided store cipher. |
| 67 | + pub async fn build(self) -> Result<IndexeddbEventCacheStore, DomException> { |
| 68 | + Ok(IndexeddbEventCacheStore { |
| 69 | + inner: open_and_upgrade_db(&self.database_name).await?, |
| 70 | + serializer: IndexeddbEventCacheStoreSerializer::new(IndexeddbSerializer::new( |
| 71 | + self.store_cipher, |
| 72 | + )), |
| 73 | + memory_store: MemoryStore::new(), |
| 74 | + }) |
| 75 | + } |
| 76 | +} |
0 commit comments