Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions crates/bitwarden-core/src/platform/state_client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::Arc;

use bitwarden_state::{
registry::RepositoryNotFoundError,
repository::{Repository, RepositoryItem},
registry::{RepositoryNotFoundError, StateRegistryError},
repository::{Repository, RepositoryItem, RepositoryItemData},
DatabaseConfiguration,
};

use crate::Client;
Expand Down Expand Up @@ -30,4 +31,26 @@ impl StateClient {
) -> Result<Arc<dyn Repository<T>>, RepositoryNotFoundError> {
self.client.internal.repository_map.get_client_managed()
}

/// Initialize the database for SDK managed repositories.
pub async fn initialize_database(
&self,
configuration: DatabaseConfiguration,
repositories: Vec<RepositoryItemData>,
) -> Result<(), StateRegistryError> {
self.client
.internal
.repository_map
.initialize_database(configuration, repositories)
.await
}

/// Get a SDK managed state repository for a specific type, if it exists.
pub fn get_sdk_managed<
T: RepositoryItem + serde::ser::Serialize + serde::de::DeserializeOwned,
>(
&self,
) -> Result<impl Repository<T>, StateRegistryError> {
self.client.internal.repository_map.get_sdk_managed()
}
}
15 changes: 14 additions & 1 deletion crates/bitwarden-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ keywords.workspace = true

[features]
uniffi = []
wasm = []
wasm = ["bitwarden-threading/wasm"]

[dependencies]
async-trait = { workspace = true }
bitwarden-error = { workspace = true }
bitwarden-threading = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }

[target.'cfg(target_arch="wasm32")'.dependencies]
indexed-db = ">=0.4.2, <0.5"
js-sys = { workspace = true }
tsify = { workspace = true }

[target.'cfg(not(target_arch="wasm32"))'.dependencies]
rusqlite = { version = ">=0.37.0, <0.38", features = ["bundled"] }

[dev-dependencies]
tokio = { workspace = true, features = ["rt"] }
Expand Down
77 changes: 74 additions & 3 deletions crates/bitwarden-state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ stored:
- If the SDK itself will handle data storage, we call that approach `SDK-Managed State`. The
implementation of this is will a work in progress.

Note that these approaches aren't mutually exclusive: a repository item can use both client and SDK
managed state at the same time. However, this mixed approach is only recommended during migration
scenarios to avoid potential confusion.

## Client-Managed State

With `Client-Managed State` the application and SDK will both access the same data pool, which
Expand All @@ -53,7 +57,7 @@ impl StateClient {
}
```

#### How to use it on web clients
#### How to initialize Client-Managed State on the web clients

Once we have the function defined in `bitwarden-wasm-internal`, we can use it from the web clients.
For that, the first thing we need to do is create a mapper between the client and SDK types. This
Expand Down Expand Up @@ -113,7 +117,7 @@ impl StateClient {
}
```

#### How to use it on iOS
#### How to initialize Client-Managed State on iOS

Once we have the function defined in `bitwarden-uniffi`, we can use it from the iOS application:

Expand Down Expand Up @@ -144,7 +148,7 @@ let store = CipherStoreImpl(cipherDataStore: self.cipherDataStore, userId: userI
try await self.clientService.platform().store().registerCipherStore(store: store);
```

### How to use it on Android
### How to initialize Client-Managed State on Android

Once we have the function defined in `bitwarden-uniffi`, we can use it from the Android application:

Expand Down Expand Up @@ -173,3 +177,70 @@ class CipherStoreImpl: CipherStore {

getClient(userId = userId).platform().store().registerCipherStore(CipherStoreImpl());
```

## SDK-Managed State

With `SDK-Managed State`, the SDK will be exclusively responsible for the data storage. This means
that the clients don't need to make any changes themselves, as the implementation is internal to the
SDK. To add support for an SDK managed `Repository`, it needs to be added to the initialization code
for WASM and UniFFI. This example shows how to add support for `Cipher`s.

### How to initialize SDK-Managed State on WASM

Go to `crates/bitwarden-wasm-internal/src/platform/mod.rs` and add a line with your type, as shown
below. In this example we're registering `Cipher` as both client and SDK managed to show how both
are done, but you can also just do one or the other.

```rust,ignore
pub async fn initialize_state(
&self,
cipher_repository: CipherRepository,
) -> Result<(), bitwarden_state::registry::StateRegistryError> {
let cipher = cipher_repository.into_channel_impl();
// Register the provided repository as client managed state
self.0.platform().state().register_client_managed(cipher);

let sdk_managed_repositories = vec![
// This should list all the SDK-managed repositories
<Cipher as RepositoryItem>::data(),
// Add your type here
];

self.0
.platform()
.state()
.initialize_database(sdk_managed_repositories)
.await
}
```

### How to initialize SDK-Managed State on UniFFI

Go to `crates/bitwarden-uniffi/src/platform/mod.rs` and add a line with your type, as shown below.
In this example we're registering `Cipher` as both client and SDK managed to show how both are done,
but you can also just do one or the other.

```rust,ignore
pub async fn initialize_state(
&self,
cipher_repository: Arc<dyn CipherRepository>,
) -> Result<()> {
let cipher = UniffiRepositoryBridge::new(cipher_repository);
// Register the provided repository as client managed state
self.0.platform().state().register_client_managed(cipher);

let sdk_managed_repositories = vec![
// This should list all the SDK-managed repositories
<Cipher as RepositoryItem>::data(),
// Add your type here
];

self.0
.platform()
.state()
.initialize_database(sdk_managed_repositories)
.await
.map_err(Error::StateRegistry)?;
Ok(())
}
```
4 changes: 4 additions & 0 deletions crates/bitwarden-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ pub mod repository;

/// This module provides a registry for managing repositories of different types.
pub mod registry;

pub(crate) mod sdk_managed;

pub use sdk_managed::DatabaseConfiguration;
Loading