Skip to content

[PM-12612] SDK-Managed Repository support #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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.

28 changes: 27 additions & 1 deletion crates/bitwarden-core/src/platform/state_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::sync::Arc;

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

use crate::Client;

Expand All @@ -25,4 +29,26 @@
pub fn get_client_managed<T: RepositoryItem>(&self) -> Option<Arc<dyn Repository<T>>> {
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
}

Check warning on line 44 in crates/bitwarden-core/src/platform/state_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/platform/state_client.rs#L34-L44

Added lines #L34 - L44 were not covered by tests

/// 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()
}

Check warning on line 53 in crates/bitwarden-core/src/platform/state_client.rs

View check run for this annotation

Codecov / codecov/patch

crates/bitwarden-core/src/platform/state_client.rs#L47-L53

Added lines #L47 - L53 were not covered by tests
}
13 changes: 13 additions & 0 deletions crates/bitwarden-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ 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-next = { workspace = true }

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

[dev-dependencies]
tokio = { workspace = true, features = ["rt"] }
Expand Down
63 changes: 62 additions & 1 deletion crates/bitwarden-state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Cipher {

// Register `Cipher` for use with a `Repository`.
// This should be done in the crate where `Cipher` is defined.
bitwarden_state::register_repository_item!(Cipher, "Cipher");
bitwarden_state::register_repository_item!(Cipher, "Cipher", version: 1);
```

With the registration complete, the next important decision is to select where will the data be
Expand Down Expand Up @@ -173,3 +173,64 @@ 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.

### WASM

Go to `crates/bitwarden-wasm-internal/src/platform/mod.rs` and add a line with your type, as shown:

```rust,ignore
pub async fn initialize_state(
&self,
cipher_repository: CipherRepository,
) -> Result<(), bitwarden_state::registry::StateRegistryError> {
let cipher = cipher_repository.into_channel_impl();
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
}
```

### UniFFI

Go to `crates/bitwarden-uniffi/src/platform/mod.rs` and add a line with your type, as shown:

```rust,ignore
pub async fn initialize_state(
&self,
cipher_repository: Arc<dyn CipherRepository>,
) -> Result<()> {
let cipher = UniffiRepositoryBridge::new(cipher_repository);
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
Loading