Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 1 addition & 3 deletions rustls-platform-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ dbg = []
# by the platform's verifier.
cert-logging = ["base64"]
# Used for nicely documenting the Android-specific APIs. This feature is not stable.
docsrs = ["jni", "once_cell"]
docsrs = ["jni"]

[dependencies]
rustls = { version = "0.23.27", default-features = false, features = ["std"] }
log = { version = "0.4" }
base64 = { version = "0.22", optional = true } # Only used when the `cert-logging` feature is enabled.
jni = { version = "0.22.4", 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_vendor = "apple"), not(target_arch = "wasm32")))'.dependencies]
rustls-native-certs = "0.8"
webpki = { package = "rustls-webpki", version = "0.103", default-features = false }

[target.'cfg(target_os = "android")'.dependencies]
once_cell = "1.9"
rustls-platform-verifier-android = { path = "../android-release-support", version = "0.2.0" }
jni = { version = "0.22", default-features = false }
webpki = { package = "rustls-webpki", version = "0.103", default-features = false }
Expand Down
57 changes: 31 additions & 26 deletions rustls-platform-verifier/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use jni::errors::Error as JNIError;
use jni::objects::{Global, JClass, JClassLoader, JObject};
use jni::strings::JNIStr;
use jni::{jni_sig, jni_str, Env, JavaVM};
use once_cell::sync::OnceCell;
use std::sync::OnceLock;

static GLOBAL: OnceCell<GlobalStorage> = OnceCell::new();
static GLOBAL: OnceLock<GlobalStorage> = OnceLock::new();

/// A layer to access the Android runtime which is hosting the current
/// application process.
Expand Down Expand Up @@ -95,23 +95,25 @@ fn global() -> &'static GlobalStorage {
/// This method will setup and store an environment locally. This is useful if nothing else in your
/// application needs to access the Android runtime.
pub fn init_with_env(env: &mut Env, context: JObject) -> Result<(), JNIError> {
GLOBAL.get_or_try_init(|| -> Result<_, JNIError> {
let loader = env
.call_method(
&context,
jni_str!("getClassLoader"),
jni_sig!(() -> JClassLoader),
&[],
)?
.l()?;
let loader = env.cast_local::<JClassLoader>(loader)?;

Ok(GlobalStorage::Internal {
java_vm: env.get_java_vm()?,
context: env.new_global_ref(context)?,
loader: env.new_global_ref(loader)?,
})
})?;
if GLOBAL.get().is_some() {
return Ok(());
}

let loader = env
.call_method(
&context,
jni_str!("getClassLoader"),
jni_sig!(() -> JClassLoader),
&[],
)?
.l()?;
let loader = env.cast_local::<JClassLoader>(loader)?;

let _ = GLOBAL.set(GlobalStorage::Internal {
java_vm: env.get_java_vm()?,
context: env.new_global_ref(context)?,
loader: env.new_global_ref(loader)?,
});
Ok(())
}

Expand Down Expand Up @@ -225,25 +227,28 @@ where
/// Loads and caches a class on first use
pub(super) struct CachedClass {
name: &'static JNIStr,
class: OnceCell<Global<JClass<'static>>>,
class: OnceLock<Global<JClass<'static>>>,
}

impl CachedClass {
/// Creates a lazily initialized class reference to the class with `name`.
pub(super) const fn new(name: &'static JNIStr) -> Self {
Self {
name,
class: OnceCell::new(),
class: OnceLock::new(),
}
}

/// Gets the cached class reference, loaded on first use
pub(super) fn get(&self, cx: &mut LocalContext) -> Result<&JClass<'static>, 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)?)
})?;
let class = self.class.get().map_or_else(
|| -> Result<_, Error> {
let class = cx.load_class(self.name)?;
let class = cx.env.new_global_ref(class)?;
Ok(self.class.get_or_init(|| class))
},
Ok,
)?;

Ok(class)
}
Expand Down
Loading