matrix_sdk_indexeddb/event_cache_store/builder.rs
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// At the moment, this builder is not public outside of the crate, so we
16// get a few dead code warnings; however, this will eventually be a public
17// type, at which point the line below can be removed.
18#![allow(dead_code)]
19
20use std::{rc::Rc, sync::Arc};
21
22use matrix_sdk_store_encryption::StoreCipher;
23
24use crate::{
25 event_cache_store::{
26 IndexeddbEventCacheStore, error::IndexeddbEventCacheStoreError,
27 migrations::open_and_upgrade_db,
28 },
29 serializer::{indexed_type::IndexedTypeSerializer, safe_encode::types::SafeEncodeSerializer},
30};
31
32/// A type for conveniently building an [`IndexeddbEventCacheStore`]
33pub struct IndexeddbEventCacheStoreBuilder {
34 // The name of the IndexedDB database which will be opened
35 database_name: String,
36 // The store cipher, if any, to use when encrypting data
37 // before it is persisted to the IndexedDB database
38 store_cipher: Option<Arc<StoreCipher>>,
39}
40
41impl Default for IndexeddbEventCacheStoreBuilder {
42 fn default() -> Self {
43 Self { database_name: Self::DEFAULT_DATABASE_NAME.to_owned(), store_cipher: None }
44 }
45}
46
47impl IndexeddbEventCacheStoreBuilder {
48 /// The default name of the IndexedDB database used to back the
49 /// [`IndexeddbEventCacheStore`]
50 pub const DEFAULT_DATABASE_NAME: &'static str = "event_cache";
51
52 /// Sets the name of the IndexedDB database which will be opened. This
53 /// defaults to [`Self::DEFAULT_DATABASE_NAME`].
54 pub fn database_name(mut self, name: String) -> Self {
55 self.database_name = name;
56 self
57 }
58
59 /// Create a new [`IndexeddbEventCacheStoreBuilder`] where the database name
60 /// is constructed by joining the given prefix with
61 /// [`Self::DEFAULT_DATABASE_NAME`] and separated by `::`.
62 pub fn with_prefix(prefix: &str) -> Self {
63 Self {
64 database_name: format!("{}::{}", prefix, Self::DEFAULT_DATABASE_NAME),
65 store_cipher: None,
66 }
67 }
68
69 /// Sets the store cipher to use when encrypting data before it is persisted
70 /// to the IndexedDB database. By default, no store cipher is used -
71 /// i.e., data is not encrypted before it is persisted.
72 pub fn store_cipher(mut self, store_cipher: Arc<StoreCipher>) -> Self {
73 self.store_cipher = Some(store_cipher);
74 self
75 }
76
77 /// Opens the IndexedDB database with the provided name. If successfully
78 /// opened, builds the [`IndexeddbEventCacheStore`] with that database
79 /// and the provided store cipher.
80 pub async fn build(self) -> Result<IndexeddbEventCacheStore, IndexeddbEventCacheStoreError> {
81 Ok(IndexeddbEventCacheStore {
82 inner: Rc::new(open_and_upgrade_db(&self.database_name).await?),
83 serializer: IndexedTypeSerializer::new(SafeEncodeSerializer::new(self.store_cipher)),
84 })
85 }
86}