Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(ffi): small fixes from the audit of the phone's bridge to the core

Beefa4fced066cf
diff --git a/core/nectarlink-ffi/src/lib.rs b/core/nectarlink-ffi/src/lib.rs
index 6f87793..6b109da 100644
--- a/core/nectarlink-ffi/src/lib.rs
+++ b/core/nectarlink-ffi/src/lib.rs
@@ -3264,7 +3264,9 @@ impl NectarlinkNode {
config.downloads_dir = Some(PathBuf::from(options.downloads_dir));
config.usb_port = options.usb_port;
config.key_protector = Some(Arc::new(KeyProtectorAdapter(key_protector)));
- config.capabilities.push(core::MIRROR_VIEW.into());
+ if !config.capabilities.iter().any(|c| c == core::MIRROR_VIEW) {
+ config.capabilities.push(core::MIRROR_VIEW.into());
+ }
let node =
runtime.block_on(Node::start(config, Arc::new(PlatformAdapter(platform, listener.clone()))))?;
@@ -3280,30 +3282,28 @@ impl NectarlinkNode {
}
Err(RecvError::Lagged(missed)) => {
tracing::warn!(missed, "event listener fell behind; resynchronizing state");
- if let Ok(devices) = node_for_events.paired_devices() {
- for d in devices {
- listener.on_event(Event::LinkChanged {
- id: d.id.to_string(),
- link: d.link.into(),
- });
- listener.on_event(Event::WakeInfoChanged {
- id: d.id.to_string(),
- can_wake: d.can_wake,
- });
- listener.on_event(Event::ReconnectChanged {
- id: d.id.to_string(),
- on: d.reconnect,
- });
- listener.on_event(Event::PeerInfoChanged {
+ let devices = node_for_events.paired_devices().unwrap_or_else(|e| {
+ tracing::warn!(error = %e, "can't read paired devices to resynchronize");
+ Vec::new()
+ });
+ for d in devices {
+ listener
+ .on_event(Event::LinkChanged { id: d.id.to_string(), link: d.link.into() });
+ listener.on_event(Event::WakeInfoChanged {
+ id: d.id.to_string(),
+ can_wake: d.can_wake,
+ });
+ listener
+ .on_event(Event::ReconnectChanged { id: d.id.to_string(), on: d.reconnect });
+ listener.on_event(Event::PeerInfoChanged {
+ id: d.id.to_string(),
+ info: d.info.into(),
+ });
+ if let Ok(matrix) = node_for_events.capabilities(d.id) {
+ listener.on_event(Event::Capabilities {
id: d.id.to_string(),
- info: d.info.into(),
+ features: features(&matrix),
});
- if let Ok(matrix) = node_for_events.capabilities(d.id) {
- listener.on_event(Event::Capabilities {
- id: d.id.to_string(),
- features: features(&matrix),
- });
- }
}
}
listener.on_event(Event::QueueChanged);
@@ -4229,8 +4229,11 @@ impl NectarlinkNode {
) -> Result<()> {
let peer = parse_id(&pc_id)?;
let mode = match mode.as_str() {
+ "photo" => core::CameraCaptureMode::Photo,
"scan" => core::CameraCaptureMode::Scan,
- _ => core::CameraCaptureMode::Photo,
+ other => {
+ return Err(NectarlinkError::Internal { reason: format!("unknown capture mode {other:?}") });
+ }
};
let meta = core::CameraCaptureResultMeta {
request_id,
@@ -4266,8 +4269,11 @@ impl NectarlinkNode {
) -> Result<()> {
let peer = parse_id(&peer_id)?;
let mode = match mode.as_str() {
+ "photo" => core::CameraCaptureMode::Photo,
"scan" => core::CameraCaptureMode::Scan,
- _ => core::CameraCaptureMode::Photo,
+ other => {
+ return Err(NectarlinkError::Internal { reason: format!("unknown capture mode {other:?}") });
+ }
};
let node = self.node.clone();
self.run(async move { Ok(node.request_camera_capture(peer, request_id, mode).await?) }).await
@@ -4333,13 +4339,14 @@ impl NectarlinkNode {
};
let node = self.node.clone();
self.block_on_rt(async move {
- use std::io::Write;
+ use tokio::io::AsyncWriteExt;
+ // tokio's file I/O keeps the runtime's two worker threads free.
if let Some(parent) = std::path::Path::new(&local_dest).parent()
&& !parent.as_os_str().is_empty()
{
- let _ = std::fs::create_dir_all(parent);
+ let _ = tokio::fs::create_dir_all(parent).await;
}
- let Ok(mut f) = std::fs::File::create(&local_dest) else {
+ let Ok(mut f) = tokio::fs::File::create(&local_dest).await else {
return false;
};
let mut offset = 0u64;
@@ -4353,7 +4360,7 @@ impl NectarlinkNode {
if chunk.is_empty() {
break;
}
- if f.write_all(&chunk).is_err() {
+ if f.write_all(&chunk).await.is_err() {
return false;
}
offset += chunk.len() as u64;
@@ -4361,7 +4368,7 @@ impl NectarlinkNode {
break;
}
}
- true
+ f.flush().await.is_ok()
})
}
@@ -4687,22 +4694,26 @@ pub extern "system" fn Java_app_nectarlink_android_core_AndroidContext_nativeIns
_class: jni_sys::jclass,
context: jni_sys::jobject,
) {
- static INSTALLED: std::sync::Once = std::sync::Once::new();
- INSTALLED.call_once(|| {
- // SAFETY: `env` is the calling thread's JNI environment, valid for
- // this call; the global reference keeps `context` alive for good,
- // and the JavaVM lives as long as the process.
- unsafe {
- let functions = &**env;
- let mut vm: *mut jni_sys::JavaVM = std::ptr::null_mut();
- if (functions.v1_1.GetJavaVM)(env, &mut vm) != jni_sys::JNI_OK || vm.is_null() {
- return;
- }
- let global = (functions.v1_1.NewGlobalRef)(env, context);
- if global.is_null() {
- return;
- }
- nectarlink_core::install_android_context(vm.cast(), global.cast());
+ // Set only once it worked, so a failed call can be tried again.
+ static INSTALLED: std::sync::Mutex<bool> = std::sync::Mutex::new(false);
+ let mut installed = INSTALLED.lock().unwrap_or_else(|e| e.into_inner());
+ if *installed || env.is_null() || context.is_null() {
+ return;
+ }
+ // SAFETY: `env` is the calling thread's JNI environment (checked
+ // non-null), valid for this call; the global reference keeps `context`
+ // alive for good, and the JavaVM lives as long as the process.
+ unsafe {
+ let functions = &**env;
+ let mut vm: *mut jni_sys::JavaVM = std::ptr::null_mut();
+ if (functions.v1_1.GetJavaVM)(env, &mut vm) != jni_sys::JNI_OK || vm.is_null() {
+ return;
+ }
+ let global = (functions.v1_1.NewGlobalRef)(env, context);
+ if global.is_null() {
+ return;
}
- });
+ nectarlink_core::install_android_context(vm.cast(), global.cast());
+ }
+ *installed = true;
}