Skip to content

Commit 107fc07

Browse files
Wasm improvements for the bindings
This patch improves the Wasm support of the matrix-sdk-ffi crate. First a uniffi feature needed to be enabled. Secondly a bunch of methods which don't work under Wasm have been stubbed out. Signed-off-by: MTRNord <[email protected]> Co-authored-by: MTRNord <[email protected]>
1 parent 4585d5f commit 107fc07

File tree

3 files changed

+112
-50
lines changed

3 files changed

+112
-50
lines changed

bindings/matrix-sdk-ffi/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ oauth2.workspace = true
8989
[target.'cfg(target_family = "wasm")'.dependencies]
9090
console_error_panic_hook = "0.1.7"
9191
tokio = { workspace = true, features = ["sync", "macros"] }
92-
uniffi.workspace = true
92+
uniffi = { workspace = true, features = ["wasm-unstable-single-threaded"] }
93+
futures-executor.workspace = true
9394

9495
[target.'cfg(not(target_family = "wasm"))'.dependencies]
9596
async-compat.workspace = true

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

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,6 @@ impl Client {
10161016
}
10171017
}
10181018

1019-
#[cfg(not(target_family = "wasm"))]
10201019
#[matrix_sdk_ffi_macros::export]
10211020
impl Client {
10221021
/// Retrieves a media file from the media source
@@ -1030,22 +1029,60 @@ impl Client {
10301029
use_cache: bool,
10311030
temp_dir: Option<String>,
10321031
) -> Result<Arc<MediaFileHandle>, ClientError> {
1033-
let source = (*media_source).clone();
1034-
let mime_type: mime::Mime = mime_type.parse()?;
1032+
#[cfg(not(target_family = "wasm"))]
1033+
{
1034+
let source = (*media_source).clone();
1035+
let mime_type: mime::Mime = mime_type.parse()?;
10351036

1036-
let handle = self
1037-
.inner
1038-
.media()
1039-
.get_media_file(
1040-
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
1041-
filename,
1042-
&mime_type,
1043-
use_cache,
1044-
temp_dir,
1045-
)
1046-
.await?;
1037+
let handle = self
1038+
.inner
1039+
.media()
1040+
.get_media_file(
1041+
&MediaRequestParameters {
1042+
source: source.media_source,
1043+
format: MediaFormat::File,
1044+
},
1045+
filename,
1046+
&mime_type,
1047+
use_cache,
1048+
temp_dir,
1049+
)
1050+
.await?;
1051+
1052+
Ok(Arc::new(MediaFileHandle::new(handle)))
1053+
}
1054+
1055+
/// MediaFileHandle uses SdkMediaFileHandle which requires an
1056+
/// intermediate TempFile which is not available on wasm
1057+
/// platforms due to lack of an accessible file system.
1058+
#[cfg(target_family = "wasm")]
1059+
Err(ClientError::Generic {
1060+
msg: "get_media_file is not supported on wasm platforms".to_owned(),
1061+
details: None,
1062+
})
1063+
}
1064+
1065+
pub async fn set_display_name(&self, name: String) -> Result<(), ClientError> {
1066+
#[cfg(not(target_family = "wasm"))]
1067+
{
1068+
self.inner
1069+
.account()
1070+
.set_display_name(Some(name.as_str()))
1071+
.await
1072+
.context("Unable to set display name")?;
1073+
}
1074+
1075+
#[cfg(target_family = "wasm")]
1076+
{
1077+
self.inner.account().set_display_name(Some(name.as_str())).await.map_err(|e| {
1078+
ClientError::Generic {
1079+
msg: "Unable to set display name".to_owned(),
1080+
details: Some(e.to_string()),
1081+
}
1082+
})?;
1083+
}
10471084

1048-
Ok(Arc::new(MediaFileHandle::new(handle)))
1085+
Ok(())
10491086
}
10501087
}
10511088

@@ -1181,15 +1218,6 @@ impl Client {
11811218
Ok(display_name)
11821219
}
11831220

1184-
pub async fn set_display_name(&self, name: String) -> Result<(), ClientError> {
1185-
self.inner
1186-
.account()
1187-
.set_display_name(Some(name.as_str()))
1188-
.await
1189-
.context("Unable to set display name")?;
1190-
Ok(())
1191-
}
1192-
11931221
pub async fn upload_avatar(&self, mime_type: String, data: Vec<u8>) -> Result<(), ClientError> {
11941222
let mime: Mime = mime_type.parse()?;
11951223
self.inner.account().upload_avatar(&mime, data).await?;
@@ -2541,25 +2569,25 @@ fn gen_transaction_id() -> String {
25412569

25422570
/// A file handle that takes ownership of a media file on disk. When the handle
25432571
/// is dropped, the file will be removed from the disk.
2544-
#[cfg(not(target_family = "wasm"))]
25452572
#[derive(uniffi::Object)]
25462573
pub struct MediaFileHandle {
2574+
#[cfg(not(target_family = "wasm"))]
25472575
inner: std::sync::RwLock<Option<SdkMediaFileHandle>>,
25482576
}
25492577

2550-
#[cfg(not(target_family = "wasm"))]
25512578
impl MediaFileHandle {
2579+
#[cfg(not(target_family = "wasm"))]
25522580
fn new(handle: SdkMediaFileHandle) -> Self {
25532581
Self { inner: std::sync::RwLock::new(Some(handle)) }
25542582
}
25552583
}
25562584

2557-
#[cfg(not(target_family = "wasm"))]
25582585
#[matrix_sdk_ffi_macros::export]
25592586
impl MediaFileHandle {
25602587
/// Get the media file's path.
25612588
pub fn path(&self) -> Result<String, ClientError> {
2562-
Ok(self
2589+
#[cfg(not(target_family = "wasm"))]
2590+
return Ok(self
25632591
.inner
25642592
.read()
25652593
.unwrap()
@@ -2568,24 +2596,37 @@ impl MediaFileHandle {
25682596
.path()
25692597
.to_str()
25702598
.unwrap()
2571-
.to_owned())
2599+
.to_owned());
2600+
#[cfg(target_family = "wasm")]
2601+
Err(ClientError::Generic {
2602+
msg: "MediaFileHandle.path() is not supported on WASM targets".to_string(),
2603+
details: None,
2604+
})
25722605
}
25732606

25742607
pub fn persist(&self, path: String) -> Result<bool, ClientError> {
2575-
let mut guard = self.inner.write().unwrap();
2576-
Ok(
2577-
match guard
2578-
.take()
2579-
.context("MediaFileHandle was already persisted")?
2580-
.persist(path.as_ref())
2581-
{
2582-
Ok(_) => true,
2583-
Err(e) => {
2584-
*guard = Some(e.file);
2585-
false
2586-
}
2587-
},
2588-
)
2608+
#[cfg(not(target_family = "wasm"))]
2609+
{
2610+
let mut guard = self.inner.write().unwrap();
2611+
Ok(
2612+
match guard
2613+
.take()
2614+
.context("MediaFileHandle was already persisted")?
2615+
.persist(path.as_ref())
2616+
{
2617+
Ok(_) => true,
2618+
Err(e) => {
2619+
*guard = Some(e.file);
2620+
false
2621+
}
2622+
},
2623+
)
2624+
}
2625+
#[cfg(target_family = "wasm")]
2626+
Err(ClientError::Generic {
2627+
msg: "MediaFileHandle.persist() is not supported on WASM targets".to_string(),
2628+
details: None,
2629+
})
25892630
}
25902631
}
25912632

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,20 @@ impl ClientBuilder {
152152
system_is_memory_constrained: false,
153153
username: None,
154154
homeserver_cfg: None,
155+
#[cfg(not(target_family = "wasm"))]
155156
user_agent: None,
156157
sliding_sync_version_builder: SlidingSyncVersionBuilder::None,
158+
#[cfg(not(target_family = "wasm"))]
157159
proxy: None,
160+
#[cfg(not(target_family = "wasm"))]
158161
disable_ssl_verification: false,
159162
disable_automatic_token_refresh: false,
160163
cross_process_store_locks_holder_name: None,
161164
enable_oidc_refresh_lock: false,
162165
session_delegate: None,
166+
#[cfg(not(target_family = "wasm"))]
163167
additional_root_certificates: Default::default(),
168+
#[cfg(not(target_family = "wasm"))]
164169
disable_built_in_root_certificates: false,
165170
encryption_settings: EncryptionSettings {
166171
auto_enable_cross_signing: false,
@@ -559,18 +564,23 @@ impl ClientBuilder {
559564
}
560565
}
561566

562-
#[cfg(not(target_family = "wasm"))]
563567
#[matrix_sdk_ffi_macros::export]
564568
impl ClientBuilder {
565569
pub fn proxy(self: Arc<Self>, url: String) -> Arc<Self> {
566570
let mut builder = unwrap_or_clone_arc(self);
567-
builder.proxy = Some(url);
571+
#[cfg(not(target_family = "wasm"))]
572+
{
573+
builder.proxy = Some(url);
574+
}
568575
Arc::new(builder)
569576
}
570577

571578
pub fn disable_ssl_verification(self: Arc<Self>) -> Arc<Self> {
572579
let mut builder = unwrap_or_clone_arc(self);
573-
builder.disable_ssl_verification = true;
580+
#[cfg(not(target_family = "wasm"))]
581+
{
582+
builder.disable_ssl_verification = true;
583+
}
574584
Arc::new(builder)
575585
}
576586

@@ -579,7 +589,11 @@ impl ClientBuilder {
579589
certificates: Vec<CertificateBytes>,
580590
) -> Arc<Self> {
581591
let mut builder = unwrap_or_clone_arc(self);
582-
builder.additional_root_certificates = certificates;
592+
593+
#[cfg(not(target_family = "wasm"))]
594+
{
595+
builder.additional_root_certificates = certificates;
596+
}
583597

584598
Arc::new(builder)
585599
}
@@ -589,13 +603,19 @@ impl ClientBuilder {
589603
/// [`add_root_certificates`][ClientBuilder::add_root_certificates].
590604
pub fn disable_built_in_root_certificates(self: Arc<Self>) -> Arc<Self> {
591605
let mut builder = unwrap_or_clone_arc(self);
592-
builder.disable_built_in_root_certificates = true;
606+
#[cfg(not(target_family = "wasm"))]
607+
{
608+
builder.disable_built_in_root_certificates = true;
609+
}
593610
Arc::new(builder)
594611
}
595612

596613
pub fn user_agent(self: Arc<Self>, user_agent: String) -> Arc<Self> {
597614
let mut builder = unwrap_or_clone_arc(self);
598-
builder.user_agent = Some(user_agent);
615+
#[cfg(not(target_family = "wasm"))]
616+
{
617+
builder.user_agent = Some(user_agent);
618+
}
599619
Arc::new(builder)
600620
}
601621
}

0 commit comments

Comments
 (0)