Skip to content

Commit

Permalink
fixup: android sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Sep 21, 2023
1 parent 99c59c2 commit 6152935
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
3 changes: 2 additions & 1 deletion mullvad-daemon/src/management_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use mullvad_types::{
use std::path::PathBuf;
use std::{
convert::{TryFrom, TryInto},
str::FromStr,
sync::{Arc, Mutex},
time::Duration,
};
Expand Down Expand Up @@ -598,7 +599,7 @@ impl ManagementService for ManagementServiceImpl {
let (tx, rx) = oneshot::channel();
self.send_command_to_daemon(DaemonCommand::DeleteCustomList(
tx,
mullvad_types::custom_list::Id::parse_str(&request.into_inner())
mullvad_types::custom_list::Id::from_str(&request.into_inner())
.map_err(|_| Status::invalid_argument("invalid ID"))?,
))?;
self.wait_for_result(rx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeSet;
use std::{collections::BTreeSet, str::FromStr};

use crate::types::{proto, FromProtobufTypeError};
use mullvad_types::{
Expand Down Expand Up @@ -53,7 +53,7 @@ impl TryFrom<proto::CustomList> for mullvad_types::custom_list::CustomList {
.map(GeographicLocationConstraint::try_from)
.collect::<Result<BTreeSet<_>, Self::Error>>()?;
Ok(Self {
id: Id::try_from(custom_list.id.as_str())
id: Id::from_str(&custom_list.id)
.map_err(|_| FromProtobufTypeError::InvalidArgument("Invalid list ID"))?,
name: custom_list.name,
locations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use mullvad_types::{
custom_list::Id,
relay_constraints::{Constraint, RelaySettingsUpdate},
};
use std::str::FromStr;
use talpid_types::net::TunnelType;

impl TryFrom<&proto::WireguardConstraints>
Expand Down Expand Up @@ -523,7 +524,7 @@ impl TryFrom<proto::LocationConstraint>
}
Some(proto::location_constraint::Type::CustomList(list_id)) => {
let location = LocationConstraint::CustomList {
list_id: Id::try_from(list_id.as_str()).map_err(|_| {
list_id: Id::from_str(&list_id).map_err(|_| {
FromProtobufTypeError::InvalidArgument("Id could not be parsed to a uuid")
})?,
};
Expand Down
59 changes: 57 additions & 2 deletions mullvad-types/src/custom_list.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,66 @@
use crate::relay_constraints::GeographicLocationConstraint;
#[cfg(target_os = "android")]
use jnix::{jni::objects::JString, FromJava, IntoJava, JnixEnv};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeSet,
ops::{Deref, DerefMut},
str::FromStr,
};

pub type Id = uuid::Uuid;
#[derive(Default, Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Id(uuid::Uuid);

impl Deref for Id {
type Target = uuid::Uuid;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Id {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl FromStr for Id {
type Err = <uuid::Uuid as FromStr>::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
uuid::Uuid::from_str(s).map(Id)
}
}

#[cfg(target_os = "android")]
impl<'env, 'sub_env> FromJava<'env, JString<'sub_env>> for Id
where
'env: 'sub_env,
{
const JNI_SIGNATURE: &'static str = "Ljava/lang/String;";

fn from_java(env: &JnixEnv<'env>, source: JString<'sub_env>) -> Self {
let s = env
.get_string(source)
.expect("Failed to convert from Java String");
Id::from_str(&s).expect("invalid ID")
}
}

#[cfg(target_os = "android")]
impl<'borrow, 'env: 'borrow> IntoJava<'borrow, 'env> for Id {
const JNI_SIGNATURE: &'static str = "Ljava/lang/String;";

type JavaType = AutoLocal<'env, 'borrow>;

fn into_java(self, env: &'borrow JnixEnv<'env>) -> Self::JavaType {
let s = self.to_string();
let jstring = env.new_string(&s).expect("Failed to create Java String");

env.auto_local(jstring)
}
}

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CustomListsSettings {
Expand Down Expand Up @@ -61,7 +116,7 @@ pub struct CustomList {
impl CustomList {
pub fn new(name: String) -> Self {
CustomList {
id: uuid::Uuid::new_v4(),
id: Id(uuid::Uuid::new_v4()),
name,
locations: BTreeSet::new(),
}
Expand Down
2 changes: 1 addition & 1 deletion mullvad-types/src/relay_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl<'a> fmt::Display for RelaySettingsFormatter<'a> {

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(target_os = "android", derive(IntoJava, FromJava))]
#[cfg_attr(target_os = "android", derive(FromJava, IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
pub enum LocationConstraint {
Location(GeographicLocationConstraint),
Expand Down

0 comments on commit 6152935

Please sign in to comment.