Skip to content

Commit 9fc7d1c

Browse files
committed
refactor(indexeddb): Use IndexeddbCryptoStoreError in CryptoStore impl
1 parent c6f0e47 commit 9fc7d1c

File tree

1 file changed

+44
-134
lines changed

1 file changed

+44
-134
lines changed

crates/matrix-sdk-indexeddb/src/crypto_store.rs

Lines changed: 44 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,38 @@ impl IndexeddbCryptoStore {
317317
fn get_account_info(&self) -> Option<AccountInfo> {
318318
self.account_info.read().unwrap().clone()
319319
}
320+
}
321+
322+
// Small hack to have the following macro invocation act as the appropriate
323+
// trait impl block on wasm, but still be compiled on non-wasm as a regular
324+
// impl block otherwise.
325+
//
326+
// The trait impl doesn't compile on non-wasm due to unfulfilled trait bounds,
327+
// this hack allows us to still have most of rust-analyzer's IDE functionality
328+
// within the impl block without having to set it up to check things against
329+
// the wasm target (which would disable many other parts of the codebase).
330+
#[cfg(target_arch = "wasm32")]
331+
macro_rules! impl_crypto_store {
332+
( $($body:tt)* ) => {
333+
#[async_trait(?Send)]
334+
impl CryptoStore for IndexeddbCryptoStore {
335+
type Error = IndexeddbCryptoStoreError;
336+
337+
$($body)*
338+
}
339+
};
340+
}
320341

342+
#[cfg(not(target_arch = "wasm32"))]
343+
macro_rules! impl_crypto_store {
344+
( $($body:tt)* ) => {
345+
impl IndexeddbCryptoStore {
346+
$($body)*
347+
}
348+
};
349+
}
350+
351+
impl_crypto_store! {
321352
async fn save_changes(&self, changes: Changes) -> Result<()> {
322353
let mut stores: Vec<&str> = [
323354
(changes.account.is_some() || changes.private_identity.is_some(), KEYS::CORE),
@@ -538,7 +569,7 @@ impl IndexeddbCryptoStore {
538569
Ok(users)
539570
}
540571

541-
async fn load_outbound_group_session(
572+
async fn get_outbound_group_session(
542573
&self,
543574
room_id: &RoomId,
544575
) -> Result<Option<OutboundGroupSession>> {
@@ -565,9 +596,13 @@ impl IndexeddbCryptoStore {
565596
Ok(None)
566597
}
567598
}
568-
async fn get_outgoing_key_request_helper(&self, key: &str) -> Result<Option<GossipRequest>> {
599+
600+
async fn get_outgoing_secret_requests(
601+
&self,
602+
request_id: &TransactionId,
603+
) -> Result<Option<GossipRequest>> {
569604
// in this internal we expect key to already be escaped or encrypted
570-
let jskey = JsValue::from_str(key);
605+
let jskey = JsValue::from_str(request_id.as_str());
571606
let dbs = [KEYS::OUTGOING_SECRET_REQUESTS, KEYS::UNSENT_SECRET_REQUESTS];
572607
let tx = self.inner.transaction_on_multi_with_mode(&dbs, IdbTransactionMode::Readonly)?;
573608

@@ -615,6 +650,11 @@ impl IndexeddbCryptoStore {
615650
}
616651
}
617652

653+
async fn save_account(&self, account: ReadOnlyAccount) -> Result<()> {
654+
self.save_changes(Changes { account: Some(account), ..Default::default() })
655+
.await
656+
}
657+
618658
async fn load_identity(&self) -> Result<Option<PrivateCrossSigningIdentity>> {
619659
if let Some(pickle) = self
620660
.inner
@@ -829,7 +869,7 @@ impl IndexeddbCryptoStore {
829869
.await?
830870
.and_then(|i| i.as_string());
831871
if let Some(id) = id {
832-
self.get_outgoing_key_request_helper(&id).await
872+
self.get_outgoing_secret_requests(id.as_str().into()).await
833873
} else {
834874
Ok(None)
835875
}
@@ -921,136 +961,6 @@ impl Drop for IndexeddbCryptoStore {
921961
}
922962
}
923963

924-
#[cfg(target_arch = "wasm32")]
925-
#[async_trait(?Send)]
926-
impl CryptoStore for IndexeddbCryptoStore {
927-
type Error = CryptoStoreError;
928-
929-
async fn load_account(&self) -> Result<Option<ReadOnlyAccount>, CryptoStoreError> {
930-
self.load_account().await.map_err(|e| e.into())
931-
}
932-
933-
async fn save_account(&self, account: ReadOnlyAccount) -> Result<(), CryptoStoreError> {
934-
self.save_changes(Changes { account: Some(account), ..Default::default() })
935-
.await
936-
.map_err(|e| e.into())
937-
}
938-
939-
async fn load_identity(&self) -> Result<Option<PrivateCrossSigningIdentity>, CryptoStoreError> {
940-
self.load_identity().await.map_err(|e| e.into())
941-
}
942-
943-
async fn save_changes(&self, changes: Changes) -> Result<(), CryptoStoreError> {
944-
self.save_changes(changes).await.map_err(|e| e.into())
945-
}
946-
947-
async fn get_sessions(
948-
&self,
949-
sender_key: &str,
950-
) -> Result<Option<Arc<Mutex<Vec<Session>>>>, CryptoStoreError> {
951-
self.get_sessions(sender_key).await.map_err(|e| e.into())
952-
}
953-
954-
async fn get_inbound_group_session(
955-
&self,
956-
room_id: &RoomId,
957-
session_id: &str,
958-
) -> Result<Option<InboundGroupSession>, CryptoStoreError> {
959-
self.get_inbound_group_session(room_id, session_id).await.map_err(|e| e.into())
960-
}
961-
962-
async fn get_inbound_group_sessions(
963-
&self,
964-
) -> Result<Vec<InboundGroupSession>, CryptoStoreError> {
965-
self.get_inbound_group_sessions().await.map_err(|e| e.into())
966-
}
967-
968-
async fn get_outbound_group_session(
969-
&self,
970-
room_id: &RoomId,
971-
) -> Result<Option<OutboundGroupSession>, CryptoStoreError> {
972-
self.load_outbound_group_session(room_id).await.map_err(|e| e.into())
973-
}
974-
975-
async fn inbound_group_session_counts(&self) -> Result<RoomKeyCounts, CryptoStoreError> {
976-
self.inbound_group_session_counts().await.map_err(|e| e.into())
977-
}
978-
979-
async fn inbound_group_sessions_for_backup(
980-
&self,
981-
limit: usize,
982-
) -> Result<Vec<InboundGroupSession>, CryptoStoreError> {
983-
self.inbound_group_sessions_for_backup(limit).await.map_err(|e| e.into())
984-
}
985-
986-
async fn reset_backup_state(&self) -> Result<(), CryptoStoreError> {
987-
self.reset_backup_state().await.map_err(|e| e.into())
988-
}
989-
990-
async fn load_backup_keys(&self) -> Result<BackupKeys, CryptoStoreError> {
991-
self.load_backup_keys().await.map_err(|e| e.into())
992-
}
993-
994-
async fn save_tracked_users(&self, users: &[(&UserId, bool)]) -> Result<(), CryptoStoreError> {
995-
self.save_tracked_users(users).await.map_err(Into::into)
996-
}
997-
998-
async fn load_tracked_users(&self) -> Result<Vec<TrackedUser>, CryptoStoreError> {
999-
self.load_tracked_users().await.map_err(Into::into)
1000-
}
1001-
1002-
async fn get_device(
1003-
&self,
1004-
user_id: &UserId,
1005-
device_id: &DeviceId,
1006-
) -> Result<Option<ReadOnlyDevice>, CryptoStoreError> {
1007-
self.get_device(user_id, device_id).await.map_err(|e| e.into())
1008-
}
1009-
1010-
async fn get_user_devices(
1011-
&self,
1012-
user_id: &UserId,
1013-
) -> Result<HashMap<OwnedDeviceId, ReadOnlyDevice>, CryptoStoreError> {
1014-
self.get_user_devices(user_id).await.map_err(|e| e.into())
1015-
}
1016-
1017-
async fn get_user_identity(
1018-
&self,
1019-
user_id: &UserId,
1020-
) -> Result<Option<ReadOnlyUserIdentities>, CryptoStoreError> {
1021-
self.get_user_identity(user_id).await.map_err(|e| e.into())
1022-
}
1023-
1024-
async fn is_message_known(&self, hash: &OlmMessageHash) -> Result<bool, CryptoStoreError> {
1025-
self.is_message_known(hash).await.map_err(|e| e.into())
1026-
}
1027-
1028-
async fn get_outgoing_secret_requests(
1029-
&self,
1030-
request_id: &TransactionId,
1031-
) -> Result<Option<GossipRequest>, CryptoStoreError> {
1032-
self.get_outgoing_key_request_helper(request_id.as_str()).await.map_err(|e| e.into())
1033-
}
1034-
1035-
async fn get_secret_request_by_info(
1036-
&self,
1037-
key_info: &SecretInfo,
1038-
) -> Result<Option<GossipRequest>, CryptoStoreError> {
1039-
self.get_secret_request_by_info(key_info).await.map_err(|e| e.into())
1040-
}
1041-
1042-
async fn get_unsent_secret_requests(&self) -> Result<Vec<GossipRequest>, CryptoStoreError> {
1043-
self.get_unsent_secret_requests().await.map_err(|e| e.into())
1044-
}
1045-
1046-
async fn delete_outgoing_secret_requests(
1047-
&self,
1048-
request_id: &TransactionId,
1049-
) -> Result<(), CryptoStoreError> {
1050-
self.delete_outgoing_secret_requests(request_id).await.map_err(|e| e.into())
1051-
}
1052-
}
1053-
1054964
#[cfg(all(test, target_arch = "wasm32"))]
1055965
mod tests {
1056966
use matrix_sdk_crypto::cryptostore_integration_tests;

0 commit comments

Comments
 (0)