Skip to content

Commit

Permalink
mobile: JNI code clean up (#34177)
Browse files Browse the repository at this point in the history
This PR does the following:

Removes unused functions.
Fixes typo in the function name.
Updates isClearTextPermitted implementation by converting from C++ String to jstring instead of going through an intermediate envoy_data.
Risk Level: low
Testing: unit tests
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

Signed-off-by: Fredy Wijaya <[email protected]>
  • Loading branch information
fredyw authored May 15, 2024
1 parent 1ae1aa9 commit ec7c0a3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 48 deletions.
23 changes: 12 additions & 11 deletions mobile/library/jni/android_jni_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ namespace JNI {

bool isCleartextPermitted(absl::string_view hostname) {
#if defined(__ANDROID_API__)
envoy_data host = Envoy::Bridge::Utility::copyToBridgeData(hostname);
JniHelper jni_helper(JniHelper::getThreadLocalEnv());
LocalRefUniquePtr<jstring> java_host = envoyDataToJavaString(jni_helper, host);
LocalRefUniquePtr<jclass> jcls_AndroidNetworkLibrary =
LocalRefUniquePtr<jstring> java_host = cppStringToJavaString(jni_helper, std::string(hostname));
LocalRefUniquePtr<jclass> java_android_network_library_class =
findClass("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
jmethodID jmid_isCleartextTrafficPermitted = jni_helper.getStaticMethodId(
jcls_AndroidNetworkLibrary.get(), "isCleartextTrafficPermitted", "(Ljava/lang/String;)Z");
jmethodID java_is_cleartext_traffic_permitted_method_id =
jni_helper.getStaticMethodId(java_android_network_library_class.get(),
"isCleartextTrafficPermitted", "(Ljava/lang/String;)Z");
jboolean result = jni_helper.callStaticBooleanMethod(
jcls_AndroidNetworkLibrary.get(), jmid_isCleartextTrafficPermitted, java_host.get());
release_envoy_data(host);
java_android_network_library_class.get(), java_is_cleartext_traffic_permitted_method_id,
java_host.get());
return result == JNI_TRUE;
#else
UNREFERENCED_PARAMETER(hostname);
Expand All @@ -33,11 +33,12 @@ bool isCleartextPermitted(absl::string_view hostname) {
void tagSocket(int ifd, int uid, int tag) {
#if defined(__ANDROID_API__)
JniHelper jni_helper(JniHelper::getThreadLocalEnv());
LocalRefUniquePtr<jclass> jcls_AndroidNetworkLibrary =
LocalRefUniquePtr<jclass> java_android_network_library_class =
findClass("io.envoyproxy.envoymobile.utilities.AndroidNetworkLibrary");
jmethodID jmid_tagSocket =
jni_helper.getStaticMethodId(jcls_AndroidNetworkLibrary.get(), "tagSocket", "(III)V");
jni_helper.callStaticVoidMethod(jcls_AndroidNetworkLibrary.get(), jmid_tagSocket, ifd, uid, tag);
jmethodID java_tag_socket_method_id =
jni_helper.getStaticMethodId(java_android_network_library_class.get(), "tagSocket", "(III)V");
jni_helper.callStaticVoidMethod(java_android_network_library_class.get(),
java_tag_socket_method_id, ifd, uid, tag);
#else
UNREFERENCED_PARAMETER(ifd);
UNREFERENCED_PARAMETER(uid);
Expand Down
14 changes: 7 additions & 7 deletions mobile/library/jni/jni_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ jvm_http_filter_on_request_headers(envoy_headers input_headers, bool end_stream,
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_headers =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 1);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_headers native_headers =
Envoy::JNI::javaArrayOfObjectArrayToEnvoyHeaders(jni_helper, j_headers.get());

Expand All @@ -291,7 +291,7 @@ jvm_http_filter_on_response_headers(envoy_headers input_headers, bool end_stream
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_headers =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 1);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_headers native_headers =
Envoy::JNI::javaArrayOfObjectArrayToEnvoyHeaders(jni_helper, j_headers.get());

Expand Down Expand Up @@ -346,7 +346,7 @@ static envoy_filter_data_status jvm_http_filter_on_request_data(envoy_data data,
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_data =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 1);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_data native_data = Envoy::JNI::javaByteBufferToEnvoyData(jni_helper, j_data.get());

envoy_headers* pending_headers = nullptr;
Expand Down Expand Up @@ -380,7 +380,7 @@ static envoy_filter_data_status jvm_http_filter_on_response_data(envoy_data data
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_data =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 1);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_data native_data = Envoy::JNI::javaByteBufferToEnvoyData(jni_helper, j_data.get());

envoy_headers* pending_headers = nullptr;
Expand Down Expand Up @@ -443,7 +443,7 @@ jvm_http_filter_on_request_trailers(envoy_headers trailers, envoy_stream_intel s
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_trailers =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 1);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_headers native_trailers =
Envoy::JNI::javaArrayOfObjectArrayToEnvoyHeaders(jni_helper, j_trailers.get());

Expand Down Expand Up @@ -485,7 +485,7 @@ jvm_http_filter_on_response_trailers(envoy_headers trailers, envoy_stream_intel
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_trailers =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 1);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_headers native_trailers =
Envoy::JNI::javaArrayOfObjectArrayToEnvoyHeaders(jni_helper, j_trailers.get());

Expand Down Expand Up @@ -586,7 +586,7 @@ jvm_http_filter_on_resume(const char* method, envoy_headers* headers, envoy_data
Envoy::JNI::LocalRefUniquePtr<jobjectArray> j_trailers =
jni_helper.getObjectArrayElement<jobjectArray>(result.get(), 3);

int unboxed_status = Envoy::JNI::javaIntegerTotInt(jni_helper, status.get());
int unboxed_status = Envoy::JNI::javaIntegerToCppInt(jni_helper, status.get());
envoy_headers* pending_headers =
Envoy::JNI::javaArrayOfObjectArrayToEnvoyHeadersPtr(jni_helper, j_headers.get());
envoy_data* pending_data = Envoy::JNI::javaByteBufferToEnvoyDataPtr(jni_helper, j_data.get());
Expand Down
24 changes: 1 addition & 23 deletions mobile/library/jni/jni_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void jniDeleteConstGlobalRef(const void* context) {
jniDeleteGlobalRef(const_cast<void*>(context));
}

int javaIntegerTotInt(JniHelper& jni_helper, jobject boxed_integer) {
int javaIntegerToCppInt(JniHelper& jni_helper, jobject boxed_integer) {
LocalRefUniquePtr<jclass> jcls_Integer = jni_helper.findClass("java/lang/Integer");
jmethodID jmid_intValue = jni_helper.getMethodId(jcls_Integer.get(), "intValue", "()I");
return jni_helper.callIntMethod(boxed_integer, jmid_intValue);
Expand All @@ -61,12 +61,6 @@ envoy_data javaByteArrayToEnvoyData(JniHelper& jni_helper, jbyteArray j_data, si
return {data_length, native_bytes, free, native_bytes};
}

LocalRefUniquePtr<jstring> envoyDataToJavaString(JniHelper& jni_helper, envoy_data data) {
// Ensure we get a null-terminated string, the data coming in via envoy_data might not be.
std::string str(reinterpret_cast<const char*>(data.bytes), data.length);
return jni_helper.newStringUtf(str.c_str());
}

LocalRefUniquePtr<jbyteArray> envoyDataToJavaByteArray(JniHelper& jni_helper, envoy_data data) {
LocalRefUniquePtr<jbyteArray> j_data = jni_helper.newByteArray(data.length);
PrimitiveArrayCriticalUniquePtr<void> critical_data =
Expand Down Expand Up @@ -121,22 +115,6 @@ envoyFinalStreamIntelToJavaLongArray(JniHelper& jni_helper,
return j_array;
}

LocalRefUniquePtr<jobject> envoyMapToJavaMap(JniHelper& jni_helper, envoy_map map) {
LocalRefUniquePtr<jclass> jcls_hashMap = jni_helper.findClass("java/util/HashMap");
jmethodID jmid_hashMapInit = jni_helper.getMethodId(jcls_hashMap.get(), "<init>", "(I)V");
LocalRefUniquePtr<jobject> j_hashMap =
jni_helper.newObject(jcls_hashMap.get(), jmid_hashMapInit, map.length);
jmethodID jmid_hashMapPut = jni_helper.getMethodId(
jcls_hashMap.get(), "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
for (envoy_map_size_t i = 0; i < map.length; i++) {
LocalRefUniquePtr<jstring> key = envoyDataToJavaString(jni_helper, map.entries[i].key);
LocalRefUniquePtr<jstring> value = envoyDataToJavaString(jni_helper, map.entries[i].value);
LocalRefUniquePtr<jobject> ignored =
jni_helper.callObjectMethod(j_hashMap.get(), jmid_hashMapPut, key.get(), value.get());
}
return j_hashMap;
}

envoy_data javaByteBufferToEnvoyData(JniHelper& jni_helper, jobject j_data) {
// Returns -1 if the buffer is not a direct buffer.
jlong data_length = jni_helper.getDirectBufferCapacity(j_data);
Expand Down
8 changes: 1 addition & 7 deletions mobile/library/jni/jni_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void jniDeleteGlobalRef(void* context);
void jniDeleteConstGlobalRef(const void* context);

/** Converts `java.lang.Integer` to C++ `int`. */
int javaIntegerTotInt(JniHelper& jni_helper, jobject boxed_integer);
int javaIntegerToCppInt(JniHelper& jni_helper, jobject boxed_integer);

/** Converts from Java byte array to `envoy_data`. */
envoy_data javaByteArrayToEnvoyData(JniHelper& jni_helper, jbyteArray j_data);
Expand All @@ -68,12 +68,6 @@ LocalRefUniquePtr<jlongArray>
envoyFinalStreamIntelToJavaLongArray(JniHelper& jni_helper,
envoy_final_stream_intel final_stream_intel);

/** Converts from Java `Map` to `envoy_map`. */
LocalRefUniquePtr<jobject> envoyMapToJavaMap(JniHelper& jni_helper, envoy_map map);

/** Converts from `envoy_data` to Java `String`. */
LocalRefUniquePtr<jstring> envoyDataToJavaString(JniHelper& jni_helper, envoy_data data);

/** Converts from Java `ByteBuffer` to `envoy_data`. */
envoy_data javaByteBufferToEnvoyData(JniHelper& jni_helper, jobject j_data);

Expand Down

0 comments on commit ec7c0a3

Please sign in to comment.