Skip to content

mobile: Replace direct JNIEnv access with JniHelper#30705

Merged
RyanTheOptimist merged 5 commits intoenvoyproxy:mainfrom
fredyw:jni_helper_update
Nov 2, 2023
Merged

mobile: Replace direct JNIEnv access with JniHelper#30705
RyanTheOptimist merged 5 commits intoenvoyproxy:mainfrom
fredyw:jni_helper_update

Conversation

@fredyw
Copy link
Copy Markdown
Member

@fredyw fredyw commented Nov 2, 2023

This PR replaces direct JNIEnv access, so that we can slowly migrate the code to use JniHelper. Follow-up PRs will migrate the code piecemeal to reduce the risk of breaking existing code.

Risk Level: low (cosmetic changes)
Testing: n/a
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

This PR replaces direct `JNIEnv` access, so that we can slowly migrate
the code to use `JniHelper`. Follow-up PRs will migrate the code
piecemeal to reduce the risk of breaking existing code.

Signed-off-by: Fredy Wijaya <fredyw@google.com>
@repokitteh-read-only
Copy link
Copy Markdown

As a reminder, PRs marked as draft will not be automatically assigned reviewers,
or be handled by maintainer-oncall triage.

Please mark your PR as ready when you want it to be reviewed!

🐱

Caused by: #30705 was opened by fredyw.

see: more, trace.

Signed-off-by: Fredy Wijaya <fredyw@google.com>
@fredyw
Copy link
Copy Markdown
Member Author

fredyw commented Nov 2, 2023

/retest

Signed-off-by: Fredy Wijaya <fredyw@google.com>
Signed-off-by: Fredy Wijaya <fredyw@google.com>
@fredyw
Copy link
Copy Markdown
Member Author

fredyw commented Nov 2, 2023

/retest

1 similar comment
@fredyw
Copy link
Copy Markdown
Member Author

fredyw commented Nov 2, 2023

/retest

@fredyw fredyw marked this pull request as ready for review November 2, 2023 21:44
@fredyw
Copy link
Copy Markdown
Member Author

fredyw commented Nov 2, 2023

/assign @abeyad

Copy link
Copy Markdown
Contributor

@RyanTheOptimist RyanTheOptimist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! One drive-by comment, and one question.

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());
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

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 believe Envoy::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 using Envoy::JNI::get_env() everywhere (or removing it).

Copy link
Copy Markdown
Contributor

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.

jclass jcls_AndroidNetworkLibrary =
Envoy::JNI::find_class("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
jmethodID jmid_isCleartextTrafficPermitted = env->GetStaticMethodID(
jmethodID jmid_isCleartextTrafficPermitted = jni_helper.getEnv()->GetStaticMethodID(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your plan is to (incrementally) replace code like this with jni_helper.GetStaticMethodID(...), right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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 JniHelper::getEnv().

Signed-off-by: Fredy Wijaya <fredyw@google.com>
@fredyw
Copy link
Copy Markdown
Member Author

fredyw commented Nov 2, 2023

/assign @RyanTheOptimist

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());
Copy link
Copy Markdown
Contributor

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.

@RyanTheOptimist RyanTheOptimist enabled auto-merge (squash) November 2, 2023 22:52
@RyanTheOptimist RyanTheOptimist merged commit edf0552 into envoyproxy:main Nov 2, 2023
@fredyw fredyw deleted the jni_helper_update branch November 3, 2023 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants