-
Notifications
You must be signed in to change notification settings - Fork 5.3k
mobile: Replace direct JNIEnv access with JniHelper #30705
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
Changes from all commits
2b4cf56
05d6a72
90fd1d4
3fe4dd3
deb7ac8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,15 +17,15 @@ | |
| bool is_cleartext_permitted(absl::string_view hostname) { | ||
| #if defined(__ANDROID_API__) | ||
| envoy_data host = Envoy::Data::Utility::copyToBridgeData(hostname); | ||
| JNIEnv* env = Envoy::JNI::get_env(); | ||
| jstring java_host = Envoy::JNI::native_data_to_string(env, host); | ||
| Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); | ||
| jstring java_host = Envoy::JNI::native_data_to_string(jni_helper, host); | ||
| jclass jcls_AndroidNetworkLibrary = | ||
| Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary"); | ||
| jmethodID jmid_isCleartextTrafficPermitted = env->GetStaticMethodID( | ||
| jmethodID jmid_isCleartextTrafficPermitted = jni_helper.getEnv()->GetStaticMethodID( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your plan is to (incrementally) replace code like this with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. That's the plan. I will try to make small changes at a time, so it's easier to debug in case something breaks. Once everything has been fully migrated, I'll start removing |
||
| jcls_AndroidNetworkLibrary, "isCleartextTrafficPermitted", "(Ljava/lang/String;)Z"); | ||
| jboolean result = env->CallStaticBooleanMethod(jcls_AndroidNetworkLibrary, | ||
| jmid_isCleartextTrafficPermitted, java_host); | ||
| env->DeleteLocalRef(java_host); | ||
| jboolean result = jni_helper.getEnv()->CallStaticBooleanMethod( | ||
| jcls_AndroidNetworkLibrary, jmid_isCleartextTrafficPermitted, java_host); | ||
| jni_helper.getEnv()->DeleteLocalRef(java_host); | ||
| release_envoy_data(host); | ||
| return result == JNI_TRUE; | ||
| #else | ||
|
|
@@ -36,12 +36,13 @@ bool is_cleartext_permitted(absl::string_view hostname) { | |
|
|
||
| void tag_socket(int ifd, int uid, int tag) { | ||
| #if defined(__ANDROID_API__) | ||
| JNIEnv* env = Envoy::JNI::get_env(); | ||
| Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); | ||
| jclass jcls_AndroidNetworkLibrary = | ||
| Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary"); | ||
| jmethodID jmid_tagSocket = | ||
| env->GetStaticMethodID(jcls_AndroidNetworkLibrary, "tagSocket", "(III)V"); | ||
| env->CallStaticVoidMethod(jcls_AndroidNetworkLibrary, jmid_tagSocket, ifd, uid, tag); | ||
| jni_helper.getEnv()->GetStaticMethodID(jcls_AndroidNetworkLibrary, "tagSocket", "(III)V"); | ||
| jni_helper.getEnv()->CallStaticVoidMethod(jcls_AndroidNetworkLibrary, jmid_tagSocket, ifd, uid, | ||
| tag); | ||
| #else | ||
| UNREFERENCED_PARAMETER(ifd); | ||
| UNREFERENCED_PARAMETER(uid); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to have JniHelper call
Envoy::JNI::get_env()in the constructor to simplify usage?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately the use of
Envoy::JNI::get_env()is not consistent. Some will use it and some won't. I believeEnvoy::JNI::get_env()is also very Android specific, which means it won't work when running on HotSpot VM (non Android). In the future PR, I plan on fixing this as well, which could possibly be usingEnvoy::JNI::get_env()everywhere (or removing it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. How confusing! Thanks for working on this.