summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-22 18:43:49 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-05-23 10:30:03 +0000
commit2f40aca85164bf2aab597cb2b04ea877e3288723 (patch)
tree0d0e991dcb51e8e568aee9be1a9f142e8d4c9e24
parent276a5df145025567ef908173d9c692bb30422308 (diff)
downloadmullvadvpn-2f40aca85164bf2aab597cb2b04ea877e3288723.tar.xz
mullvadvpn-2f40aca85164bf2aab597cb2b04ea877e3288723.zip
Implement `FromJava` for `Constraint`
-rw-r--r--Cargo.lock6
-rw-r--r--mullvad-jni/Cargo.toml2
-rw-r--r--mullvad-jni/src/from_java.rs47
-rw-r--r--mullvad-jni/src/lib.rs2
4 files changed, 52 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0744c310ca..ccf2d2491f 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -698,7 +698,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "jni"
-version = "0.11.0"
+version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cesu8 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1166,7 +1166,7 @@ version = "0.1.0"
dependencies = [
"err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)",
- "jni 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "jni 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonrpc-client-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2847,7 +2847,7 @@ dependencies = [
"checksum ipnetwork 0.14.0 (git+https://github.com/mullvad/ipnetwork?branch=fix-deserialization)" = "<none>"
"checksum ipnetwork 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3d862c86f7867f19b693ec86765e0252d82e53d4240b9b629815675a0714ad1"
"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b"
-"checksum jni 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "294eca097d1dc0bf59de5ab9f7eafa5f77129e9f6464c957ed3ddeb705fb4292"
+"checksum jni 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6a88aedffe8ad596237d23a363a53a6c7c6493d4918864d47673e6a15eb938b"
"checksum jni-sys 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130"
"checksum jsonrpc-client-core 0.5.0 (git+https://github.com/mullvad/jsonrpc-client-rs?rev=68aac55b)" = "<none>"
"checksum jsonrpc-client-core 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f29cb249837420fb0cee7fb0fbf1d22679e121b160e71bb5e0d90b9df241c23e"
diff --git a/mullvad-jni/Cargo.toml b/mullvad-jni/Cargo.toml
index a9b8092d92..bfb4cd7ede 100644
--- a/mullvad-jni/Cargo.toml
+++ b/mullvad-jni/Cargo.toml
@@ -12,7 +12,7 @@ crate_type = ["cdylib"]
[dependencies]
err-derive = "0.1.5"
futures = "0.1"
-jni = "0.11"
+jni = "0.12"
jsonrpc-client-core = "0.5"
lazy_static = "1"
log = "0.4"
diff --git a/mullvad-jni/src/from_java.rs b/mullvad-jni/src/from_java.rs
index 11b98ad753..7a87e10884 100644
--- a/mullvad-jni/src/from_java.rs
+++ b/mullvad-jni/src/from_java.rs
@@ -1,8 +1,10 @@
+use crate::get_class;
use jni::{
objects::{JObject, JString},
JNIEnv,
};
-use std::ops::Deref;
+use mullvad_types::relay_constraints::Constraint;
+use std::{fmt::Debug, ops::Deref};
pub trait FromJava<'env> {
type JavaType: 'env;
@@ -36,3 +38,46 @@ impl<'env> FromJava<'env> for String {
)
}
}
+
+impl<'env, T> FromJava<'env> for Constraint<T>
+where
+ T: Clone + Debug + Eq + FromJava<'env>,
+ T::JavaType: From<JObject<'env>>,
+{
+ type JavaType = JObject<'env>;
+
+ fn from_java(env: &JNIEnv<'env>, source: Self::JavaType) -> Self {
+ if is_instance_of(env, source, "net/mullvad/mullvadvpn/model/Constraint$Any") {
+ Constraint::Any
+ } else if is_instance_of(env, source, "net/mullvad/mullvadvpn/model/Constraint$Only") {
+ let value = get_object_field(env, source, "value", "Ljava/lang/Object;");
+
+ Constraint::Only(T::from_java(env, T::JavaType::from(value)))
+ } else {
+ panic!("Invalid Constraint Java sub-class");
+ }
+ }
+}
+
+fn is_instance_of<'env>(
+ env: &JNIEnv<'env>,
+ object: JObject<'env>,
+ class_name: &'static str,
+) -> bool {
+ let class = get_class(class_name);
+
+ env.is_instance_of(object, &class)
+ .expect("Failed to check if an object is an instance of a specified class")
+}
+
+fn get_object_field<'env>(
+ env: &JNIEnv<'env>,
+ object: JObject<'env>,
+ field_name: &str,
+ field_type: &str,
+) -> JObject<'env> {
+ env.get_field(object, field_name, field_type)
+ .expect("Failed to get field from object")
+ .l()
+ .expect("Field has incorrect Java type")
+}
diff --git a/mullvad-jni/src/lib.rs b/mullvad-jni/src/lib.rs
index 95fccfb5ce..fdd6062cc4 100644
--- a/mullvad-jni/src/lib.rs
+++ b/mullvad-jni/src/lib.rs
@@ -21,6 +21,8 @@ const LOG_FILENAME: &str = "daemon.log";
const CLASSES_TO_LOAD: &[&str] = &[
"java/util/ArrayList",
"net/mullvad/mullvadvpn/model/AccountData",
+ "net/mullvad/mullvadvpn/model/Constraint$Any",
+ "net/mullvad/mullvadvpn/model/Constraint$Only",
"net/mullvad/mullvadvpn/model/Relay",
"net/mullvad/mullvadvpn/model/RelayList",
"net/mullvad/mullvadvpn/model/RelayListCity",