Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Android init methods for more flexibility #159

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 16 additions & 14 deletions rustls-platform-verifier/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,35 @@ fn global() -> &'static Global {
.expect("Expect rustls-platform-verifier to be initialized")
}

/// Initializes and stores the required context for the Android platform.
///
/// This method will setup and store an environment locally. This is useful if
/// nothing else in your application needs access the Android runtime.
/// Initialize given a typical Android NDK `JNIEnv` and `jobject` context.
mcginty marked this conversation as resolved.
Show resolved Hide resolved
///
/// Initialization must be done before any verification is attempted.
pub fn init_hosted(env: &mut JNIEnv, context: JObject) -> Result<(), JNIError> {
/// This method will setup and store an environment locally. This is useful if nothing else in your
/// application needs access the Android runtime.
cpu marked this conversation as resolved.
Show resolved Hide resolved
pub fn init_with_env(env: &mut JNIEnv, context: JObject) -> Result<(), JNIError> {
GLOBAL.get_or_try_init(|| -> Result<_, JNIError> {
let loader =
env.call_method(&context, "getClassLoader", "()Ljava/lang/ClassLoader;", &[])?;
let global = Global::Internal {

Ok(Global::Internal {
java_vm: env.get_java_vm()?,
context: env.new_global_ref(context)?,
loader: env.new_global_ref(JObject::try_from(loader)?)?,
};

Ok(global)
})
})?;

Ok(())
}

/// *Deprecated*: This is the original method name for [`init_with_env`] and is functionally
/// identical.
pub fn init_hosted(env: &mut JNIEnv, context: JObject) -> Result<(), JNIError> {
init_with_env(env, context)
}

/// Initializes and stores the required context for the Android platform.
///
/// This method utilizes an existing Android runtime environment and set anything
/// else up on its own. This is useful if your application already interacts with
/// the runtime and has pre-existing handles.
/// This method utilizes an existing Android runtime environment and set anything else up on its
cpu marked this conversation as resolved.
Show resolved Hide resolved
/// own. This is useful if your application already interacts with the runtime and has pre-existing
/// handles.
complexspaces marked this conversation as resolved.
Show resolved Hide resolved
///
/// This function will never panic, and is therefore safe to use at FFI boundaries.
///
Expand Down
2 changes: 1 addition & 1 deletion rustls-platform-verifier/src/tests/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod android {
.with_max_level(log::Level::Trace.to_level_filter())
.with_filter(log_filter),
);
crate::android::init_hosted(env, cx).unwrap();
crate::android::init_with_env(env, cx).unwrap();
crate::tests::ensure_global_state();
std::panic::set_hook(Box::new(|info| {
let msg = if let Some(msg) = info.payload().downcast_ref::<&'static str>() {
Expand Down