Skip to content

Commit

Permalink
Simplify ConnectivityListener
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Nov 20, 2024
1 parent d8b6010 commit 436f232
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,12 @@ class ConnectivityListener {
fun unregister() {
connectivityManager.unregisterNetworkCallback(callback)
connectivityManager.unregisterNetworkCallback(defaultNetworkCallback)
}

// DROID-1401
// This function has never been used and should most likely be merged into unregister(),
// along with ensuring that the lifecycle of it is correct.
@Suppress("UnusedPrivateMember")
private fun finalize() {
destroySender(senderAddress)
senderAddress = 0L
if (senderAddress != 0L) {
var oldSender = senderAddress
senderAddress = 0L
destroySender(oldSender)
}
}

private external fun notifyConnectivityChange(isConnected: Boolean, senderAddress: Long)
Expand Down
1 change: 0 additions & 1 deletion mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ impl Daemon {

mullvad_api::proxy::ApiConnectionMode::try_delete_cache(&cache_dir).await;
let api_runtime = mullvad_api::Runtime::with_cache(
// FIXME: clone is bad (single sender)
#[cfg(target_os = "android")]
android_dns::AndroidDnsResolver::new(connectivity_listener.clone()),
#[cfg(not(target_os = "android"))]
Expand Down
47 changes: 20 additions & 27 deletions talpid-core/src/connectivity_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use jnix::{
sys::{jboolean, jlong, JNI_TRUE},
JNIEnv, JavaVM,
},
JnixEnv,
FromJava,
FromJava, JnixEnv,
};
use std::{net::IpAddr, sync::{Arc, Weak}};
use std::{net::IpAddr, sync::Arc};
use talpid_types::{android::AndroidContext, net::Connectivity, ErrorExt};

/// Error related to Android connectivity monitor
Expand Down Expand Up @@ -45,7 +44,6 @@ pub struct ConnectivityListener {
jvm: Arc<JavaVM>,
class: GlobalRef,
object: GlobalRef,
_sender: Option<Arc<UnboundedSender<Connectivity>>>,
}

impl ConnectivityListener {
Expand Down Expand Up @@ -96,26 +94,24 @@ impl ConnectivityListener {
jvm: android_context.jvm,
class,
object,
_sender: None,
})
}

/// Register a channel that receives changes about the offline state
///
/// # Note
///
/// The listener is shared by all instances of the struct.
pub fn set_connectivity_listener(
&mut self,
sender: UnboundedSender<Connectivity>,
) -> Result<(), Error> {
let sender = Arc::new(sender);

let weak_sender = Arc::downgrade(&sender);

let weak_sender_ptr = Box::new(weak_sender);
let weak_sender_address = Box::into_raw(weak_sender_ptr) as jlong;
let sender_ptr = Box::into_raw(Box::new(sender)) as jlong;

let result = self.call_method(
"setSenderAddress",
"(J)V",
&[JValue::Long(weak_sender_address)],
&[JValue::Long(sender_ptr)],
JavaType::Primitive(Primitive::Void),
)?;

Expand All @@ -128,8 +124,6 @@ impl ConnectivityListener {
)),
}?;

self._sender = Some(sender);

Ok(())
}

Expand Down Expand Up @@ -222,15 +216,18 @@ pub extern "system" fn Java_net_mullvad_talpid_ConnectivityListener_notifyConnec
sender_address: jlong,
) {
let connected = JNI_TRUE == connected;
let sender_ref = Box::leak(unsafe { get_sender_from_address(sender_address) });
if let Some(sender) = sender_ref.upgrade() {
if sender
.unbounded_send(Connectivity::Status { connected })
.is_err()
{
log::warn!("Failed to send offline change event");
}

let sender = unsafe { Box::from_raw(sender_address as *mut UnboundedSender<Connectivity>) };

if sender
.unbounded_send(Connectivity::Status { connected })
.is_err()
{
log::warn!("Failed to send offline change event");
}

// Do not destroy
std::mem::forget(sender);
}

/// Entry point for Android Java code to return ownership of the sender reference.
Expand All @@ -241,9 +238,5 @@ pub extern "system" fn Java_net_mullvad_talpid_ConnectivityListener_destroySende
_: JObject<'_>,
sender_address: jlong,
) {
let _ = unsafe { get_sender_from_address(sender_address) };
}

unsafe fn get_sender_from_address(address: jlong) -> Box<Weak<UnboundedSender<Connectivity>>> {
Box::from_raw(address as *mut Weak<UnboundedSender<Connectivity>>)
let _ = unsafe { Box::from_raw(sender_address as *mut UnboundedSender<Connectivity>) };
}

0 comments on commit 436f232

Please sign in to comment.