Skip to content

Commit 3287304

Browse files
committed
change(ffi): return errors if the client or utd delegates were already set
1 parent 529a854 commit 3287304

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -753,11 +753,20 @@ impl Client {
753753
self.inner.available_sliding_sync_versions().await.into_iter().map(Into::into).collect()
754754
}
755755

756+
/// Sets the [ClientDelegate] which will inform about authentication errors.
757+
/// Returns an error if the delegate was already set.
756758
pub fn set_delegate(
757759
self: Arc<Self>,
758760
delegate: Option<Box<dyn ClientDelegate>>,
759-
) -> Option<Arc<TaskHandle>> {
760-
delegate.map(|delegate| {
761+
) -> Result<Option<Arc<TaskHandle>>, ClientError> {
762+
if self.delegate.get().is_some() {
763+
return Err(ClientError::Generic {
764+
msg: "Delegate already initialized".to_owned(),
765+
details: None,
766+
});
767+
}
768+
769+
Ok(delegate.map(|delegate| {
761770
let mut session_change_receiver = self.inner.subscribe_to_session_changes();
762771
let client_clone = self.clone();
763772
let session_change_task = get_runtime_handle().spawn(async move {
@@ -774,11 +783,24 @@ impl Client {
774783
});
775784

776785
self.delegate.get_or_init(|| Arc::from(delegate));
786+
777787
Arc::new(TaskHandle::new(session_change_task))
778-
})
788+
}))
779789
}
780790

781-
pub async fn set_utd_delegate(self: Arc<Self>, utd_delegate: Box<dyn UnableToDecryptDelegate>) {
791+
/// Sets the [UnableToDecryptDelegate] which will inform about UTDs.
792+
/// Returns an error if the delegate was already set.
793+
pub async fn set_utd_delegate(
794+
self: Arc<Self>,
795+
utd_delegate: Box<dyn UnableToDecryptDelegate>,
796+
) -> Result<(), ClientError> {
797+
if self.utd_hook_manager.get().is_some() {
798+
return Err(ClientError::Generic {
799+
msg: "UTD delegate already initialized".to_owned(),
800+
details: None,
801+
});
802+
}
803+
782804
// UTDs detected before this duration may be reclassified as "late decryption"
783805
// events (or discarded, if they get decrypted fast enough).
784806
const UTD_HOOK_GRACE_PERIOD: Duration = Duration::from_secs(60);
@@ -796,6 +818,8 @@ impl Client {
796818
}
797819

798820
self.utd_hook_manager.get_or_init(|| Arc::new(utd_hook_manager));
821+
822+
Ok(())
799823
}
800824

801825
pub fn session(&self) -> Result<Session, ClientError> {

0 commit comments

Comments
 (0)