Skip to content

Commit

Permalink
chore: update jni v0.21
Browse files Browse the repository at this point in the history
  • Loading branch information
flisky committed Jan 16, 2024
1 parent ab78c95 commit 4a65853
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 56 deletions.
90 changes: 79 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions rustls-platform-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ docsrs = ["jni", "once_cell"]
rustls = { version = "0.21", features = ["dangerous_configuration", "tls12", "logging"] }
log = { version = "0.4" }
base64 = { version = "0.21", optional = true } # Only used when the `cert-logging` feature is enabled.
jni = { version = "0.19", default-features = false, optional = true } # Only used during doc generation
jni = { version = "0.21", default-features = false, optional = true } # Only used during doc generation
once_cell = { version = "1.9", optional = true } # Only used during doc generation.

[target.'cfg(all(unix, not(target_os = "android"), not(target_os = "macos"), not(target_os = "ios")))'.dependencies]
Expand All @@ -42,7 +42,7 @@ webpki = { package = "rustls-webpki", version = "0.101", features = ["alloc", "s

[target.'cfg(target_os = "android")'.dependencies]
rustls-platform-verifier-android = { path = "../android-release-support", version = "0.1.0" }
jni = { version = "0.19", default-features = false }
jni = { version = "0.21", default-features = false }
webpki = { package = "rustls-webpki", version = "0.101", features = ["alloc", "std"] }
once_cell = "1.9"
android_logger = { version = "0.13", optional = true } # Only used during testing.
Expand Down
40 changes: 20 additions & 20 deletions rustls-platform-verifier/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl Global {

Ok(Context {
env,
context: JObject::from(context),
loader: JObject::from(loader),
context,
loader,
})
}
}
Expand All @@ -74,7 +74,7 @@ fn global() -> &'static Global {
/// nothing else in your application needs access the Android runtime.
///
/// Initialization must be done before any verification is attempted.
pub fn init_hosted(env: &JNIEnv, context: JObject) -> Result<(), JNIError> {
pub fn init_hosted(env: &mut JNIEnv, context: &JObject) -> Result<(), JNIError> {
GLOBAL.get_or_try_init(|| -> Result<_, JNIError> {
let loader =
env.call_method(context, "getClassLoader", "()Ljava/lang/ClassLoader;", &[])?;
Expand Down Expand Up @@ -126,34 +126,35 @@ impl From<JNIError> for Error {

pub(super) struct Context<'a> {
env: JNIEnv<'a>,
context: JObject<'a>,
loader: JObject<'a>,
context: &'a GlobalRef,
loader: &'a GlobalRef,
}

impl<'a> Context<'a> {
/// Borrow a reference to the JNI Environment executing the Android application
pub(super) fn env(&self) -> &JNIEnv<'a> {
&self.env
pub(super) fn env(&mut self) -> &mut JNIEnv<'a> {
&mut self.env
}

/// Borrow the `applicationContext` from the Android application
/// <https://developer.android.com/reference/android/app/Application>
pub(super) fn application_context(&self) -> &JObject<'a> {
&self.context
pub(super) fn application_context(&self) -> GlobalRef {
self.context.clone()
}

/// Load a class from the application class loader
///
/// This should be used instead of `JNIEnv::find_class` to ensure all classes
/// in the application can be found.
pub(super) fn load_class(&self, name: &str) -> Result<JClass<'a>, Error> {
pub(super) fn load_class(&mut self, name: &str) -> Result<JClass<'a>, Error> {
let loader = self.loader.clone();
let env = self.env();
let name = env.new_string(name)?;
let class = env.call_method(
self.loader,
&loader,
"loadClass",
"(Ljava/lang/String;)Ljava/lang/Class;",
&[JValue::from(name)],
&[JValue::from(&name)],
)?;

Ok(JObject::try_from(class)?.into())
Expand All @@ -165,17 +166,16 @@ impl<'a> Context<'a> {
/// are cleared.
pub(super) fn with_context<F, T>(f: F) -> Result<T, Error>
where
F: FnOnce(&Context) -> Result<T, Error>,
F: FnOnce(&mut Context) -> Result<T, Error>,
{
let context = global().context()?;
let env = context.env();
let mut context = global().context()?;

// 16 is the default capacity in the JVM, we can make this configurable if necessary
env.push_local_frame(16)?;
context.env.push_local_frame(16)?;

let res = f(&context);
let res = f(&mut context);

env.pop_local_frame(JObject::null())?;
unsafe { context.env.pop_local_frame(&JObject::null()) }?;

res
}
Expand All @@ -196,13 +196,13 @@ impl CachedClass {
}

/// Gets the cached class reference, loaded on first use
pub(super) fn get<'a: 'b, 'b>(&'a self, cx: &Context<'b>) -> Result<JClass<'b>, Error> {
pub(super) fn get<'a: 'b, 'b>(&'a self, cx: &mut Context<'b>) -> Result<&JClass<'b>, Error> {
let class = self.class.get_or_try_init(|| -> Result<_, Error> {
let class = cx.load_class(self.name)?;

Ok(cx.env().new_global_ref(class)?)
})?;

Ok(JClass::from(class.as_obj()))
Ok(class.as_obj().into())
}
}
Loading

0 comments on commit 4a65853

Please sign in to comment.