From 03b57c94521bcf784d86b1c7866e6c0668b375e8 Mon Sep 17 00:00:00 2001 From: Fredy Wijaya Date: Wed, 8 Nov 2023 20:20:37 +0000 Subject: [PATCH] mobile: Part 6: Update JNI usages with JniHelper (#30782) This updates the following methods: - `GetObjectArrayElement` - `GetObjectClass` Signed-off-by: Fredy Wijaya --- mobile/library/common/jni/jni_interface.cc | 291 ++++++++++----------- mobile/library/common/jni/jni_utility.cc | 32 --- mobile/library/common/jni/jni_utility.h | 3 - 3 files changed, 132 insertions(+), 194 deletions(-) diff --git a/mobile/library/common/jni/jni_interface.cc b/mobile/library/common/jni/jni_interface.cc index c8b43ee12ce7..88cb7449fe60 100644 --- a/mobile/library/common/jni/jni_interface.cc +++ b/mobile/library/common/jni/jni_interface.cc @@ -42,13 +42,13 @@ static void jvm_on_engine_running(void* context) { jni_log("[Envoy]", "jvm_on_engine_running"); Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(context); - jclass jcls_JvmonEngineRunningContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmonEngineRunningContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onEngineRunning = jni_helper.getMethodId( - jcls_JvmonEngineRunningContext, "invokeOnEngineRunning", "()Ljava/lang/Object;"); + jcls_JvmonEngineRunningContext.get(), "invokeOnEngineRunning", "()Ljava/lang/Object;"); Envoy::JNI::LocalRefUniquePtr unused = jni_helper.callObjectMethod(j_context, jmid_onEngineRunning); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmonEngineRunningContext); // TODO(goaway): This isn't re-used by other engine callbacks, so it's safe to delete here. // This will need to be updated for https://github.com/envoyproxy/envoy-mobile/issues/332 jni_helper.getEnv()->DeleteGlobalRef(j_context); @@ -63,13 +63,13 @@ static void jvm_on_log(envoy_data data, const void* context) { Envoy::JNI::LocalRefUniquePtr str = Envoy::JNI::native_data_to_string(jni_helper, data); jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmLoggerContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmLoggerContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onLog = - jni_helper.getMethodId(jcls_JvmLoggerContext, "log", "(Ljava/lang/String;)V"); + jni_helper.getMethodId(jcls_JvmLoggerContext.get(), "log", "(Ljava/lang/String;)V"); jni_helper.callVoidMethod(j_context, jmid_onLog, str.get()); release_envoy_data(data); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmLoggerContext); } static void jvm_on_exit(void*) { @@ -91,14 +91,14 @@ static void jvm_on_track(envoy_map events, const void* context) { jobject events_hashmap = Envoy::JNI::native_map_to_map(jni_helper, events); jobject j_context = static_cast(const_cast(context)); - jclass jcls_EnvoyEventTracker = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_EnvoyEventTracker = + jni_helper.getObjectClass(j_context); jmethodID jmid_onTrack = - jni_helper.getMethodId(jcls_EnvoyEventTracker, "track", "(Ljava/util/Map;)V"); + jni_helper.getMethodId(jcls_EnvoyEventTracker.get(), "track", "(Ljava/util/Map;)V"); jni_helper.callVoidMethod(j_context, jmid_onTrack, events_hashmap); release_envoy_map(events); jni_helper.getEnv()->DeleteLocalRef(events_hashmap); - jni_helper.getEnv()->DeleteLocalRef(jcls_EnvoyEventTracker); } extern "C" JNIEXPORT jint JNICALL @@ -208,8 +208,10 @@ Java_io_envoyproxy_envoymobile_engine_JniLibrary_dumpStats(JNIEnv* env, static void passHeaders(const char* method, const Envoy::Types::ManagedEnvoyHeaders& headers, jobject j_context) { Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); - jmethodID jmid_passHeader = jni_helper.getMethodId(jcls_JvmCallbackContext, method, "([B[BZ)V"); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); + jmethodID jmid_passHeader = + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), method, "([B[BZ)V"); jboolean start_headers = JNI_TRUE; for (envoy_map_size_t i = 0; i < headers.get().length; i++) { @@ -233,8 +235,6 @@ static void passHeaders(const char* method, const Envoy::Types::ManagedEnvoyHead // consider this and/or periodically popping the frame. start_headers = JNI_FALSE; } - - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); } // Platform callback implementation @@ -248,9 +248,10 @@ static void* jvm_on_headers(const char* method, const Envoy::Types::ManagedEnvoy jobject j_context = static_cast(context); passHeaders("passHeader", headers, j_context); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onHeaders = - jni_helper.getMethodId(jcls_JvmCallbackContext, method, "(JZ[J)Ljava/lang/Object;"); + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), method, "(JZ[J)Ljava/lang/Object;"); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); // Note: be careful of JVM types. Before we casted to jlong we were getting integer problems. @@ -263,7 +264,6 @@ static void* jvm_on_headers(const char* method, const Envoy::Types::ManagedEnvoy bool exception_cleared = Envoy::JNI::Exception::checkAndClear(method); jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); if (!exception_cleared) { return result; @@ -310,16 +310,14 @@ jvm_http_filter_on_request_headers(envoy_headers input_headers, bool end_stream, /*headers*/ {}}; } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result, 0); - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 1)); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result, 0); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result, 1); - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_headers native_headers = Envoy::JNI::to_native_headers(jni_helper, j_headers); + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_headers native_headers = Envoy::JNI::to_native_headers(jni_helper, j_headers.get()); jni_helper.getEnv()->DeleteLocalRef(result); - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_headers); return (envoy_filter_headers_status){/*status*/ unboxed_status, /*headers*/ native_headers}; @@ -339,16 +337,14 @@ jvm_http_filter_on_response_headers(envoy_headers input_headers, bool end_stream /*headers*/ {}}; } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result, 0); - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 1)); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result, 0); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result, 1); - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_headers native_headers = Envoy::JNI::to_native_headers(jni_helper, j_headers); + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_headers native_headers = Envoy::JNI::to_native_headers(jni_helper, j_headers.get()); jni_helper.getEnv()->DeleteLocalRef(result); - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_headers); return (envoy_filter_headers_status){/*status*/ unboxed_status, /*headers*/ native_headers}; @@ -360,9 +356,10 @@ static void* jvm_on_data(const char* method, envoy_data data, bool end_stream, Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(context); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onData = - jni_helper.getMethodId(jcls_JvmCallbackContext, method, "([BZ[J)Ljava/lang/Object;"); + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), method, "([BZ[J)Ljava/lang/Object;"); jbyteArray j_data = Envoy::JNI::native_data_to_array(jni_helper, data); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); @@ -373,7 +370,6 @@ static void* jvm_on_data(const char* method, envoy_data data, bool end_stream, jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); jni_helper.getEnv()->DeleteLocalRef(j_data); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); release_envoy_data(data); return result; @@ -398,24 +394,22 @@ static envoy_filter_data_status jvm_http_filter_on_request_data(envoy_data data, /*pending_headers*/ {}}; } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result, 0); - jobject j_data = static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 1)); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result, 0); + Envoy::JNI::LocalRefUniquePtr j_data = + jni_helper.getObjectArrayElement(result, 1); - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_data native_data = Envoy::JNI::buffer_to_native_data(jni_helper, j_data); + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_data native_data = Envoy::JNI::buffer_to_native_data(jni_helper, j_data.get()); envoy_headers* pending_headers = nullptr; // Avoid out-of-bounds access to array when checking for optional pending entities. if (unboxed_status == kEnvoyFilterDataStatusResumeIteration) { - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 2)); - pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers); - jni_helper.getEnv()->DeleteLocalRef(j_headers); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result, 2); + pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers.get()); } jni_helper.getEnv()->DeleteLocalRef(result); - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_data); return (envoy_filter_data_status){/*status*/ unboxed_status, /*data*/ native_data, @@ -436,24 +430,22 @@ static envoy_filter_data_status jvm_http_filter_on_response_data(envoy_data data /*pending_headers*/ {}}; } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result, 0); - jobject j_data = static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 1)); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result, 0); + Envoy::JNI::LocalRefUniquePtr j_data = + jni_helper.getObjectArrayElement(result, 1); - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_data native_data = Envoy::JNI::buffer_to_native_data(jni_helper, j_data); + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_data native_data = Envoy::JNI::buffer_to_native_data(jni_helper, j_data.get()); envoy_headers* pending_headers = nullptr; // Avoid out-of-bounds access to array when checking for optional pending entities. if (unboxed_status == kEnvoyFilterDataStatusResumeIteration) { - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 2)); - pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers); - jni_helper.getEnv()->DeleteLocalRef(j_headers); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result, 2); + pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers.get()); } jni_helper.getEnv()->DeleteLocalRef(result); - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_data); return (envoy_filter_data_status){/*status*/ unboxed_status, /*data*/ native_data, @@ -475,9 +467,10 @@ static void* jvm_on_trailers(const char* method, envoy_headers trailers, jobject j_context = static_cast(context); passHeaders("passHeader", trailers, j_context); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onTrailers = - jni_helper.getMethodId(jcls_JvmCallbackContext, method, "(J[J)Ljava/lang/Object;"); + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), method, "(J[J)Ljava/lang/Object;"); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); // Note: be careful of JVM types. Before we casted to jlong we were getting integer problems. @@ -488,7 +481,6 @@ static void* jvm_on_trailers(const char* method, envoy_headers trailers, .release(); jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); return result; } @@ -513,30 +505,26 @@ jvm_http_filter_on_request_trailers(envoy_headers trailers, envoy_stream_intel s /*pending_data*/ {}}; } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result, 0); - jobjectArray j_trailers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 1)); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result, 0); + Envoy::JNI::LocalRefUniquePtr j_trailers = + jni_helper.getObjectArrayElement(result, 1); - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_headers native_trailers = Envoy::JNI::to_native_headers(jni_helper, j_trailers); + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_headers native_trailers = Envoy::JNI::to_native_headers(jni_helper, j_trailers.get()); envoy_headers* pending_headers = nullptr; envoy_data* pending_data = nullptr; // Avoid out-of-bounds access to array when checking for optional pending entities. if (unboxed_status == kEnvoyFilterTrailersStatusResumeIteration) { - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 2)); - pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers); - jni_helper.getEnv()->DeleteLocalRef(j_headers); - - jobject j_data = static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 3)); - pending_data = Envoy::JNI::buffer_to_native_data_ptr(jni_helper, j_data); - jni_helper.getEnv()->DeleteLocalRef(j_data); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result, 2); + pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers.get()); + + Envoy::JNI::LocalRefUniquePtr j_data = jni_helper.getObjectArrayElement(result, 3); + pending_data = Envoy::JNI::buffer_to_native_data_ptr(jni_helper, j_data.get()); } jni_helper.getEnv()->DeleteLocalRef(result); - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_trailers); return (envoy_filter_trailers_status){/*status*/ unboxed_status, /*trailers*/ native_trailers, @@ -559,30 +547,26 @@ jvm_http_filter_on_response_trailers(envoy_headers trailers, envoy_stream_intel /*pending_data*/ {}}; } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result, 0); - jobjectArray j_trailers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 1)); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result, 0); + Envoy::JNI::LocalRefUniquePtr j_trailers = + jni_helper.getObjectArrayElement(result, 1); - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_headers native_trailers = Envoy::JNI::to_native_headers(jni_helper, j_trailers); + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_headers native_trailers = Envoy::JNI::to_native_headers(jni_helper, j_trailers.get()); envoy_headers* pending_headers = nullptr; envoy_data* pending_data = nullptr; // Avoid out-of-bounds access to array when checking for optional pending entities. if (unboxed_status == kEnvoyFilterTrailersStatusResumeIteration) { - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 2)); - pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers); - jni_helper.getEnv()->DeleteLocalRef(j_headers); - - jobject j_data = static_cast(jni_helper.getEnv()->GetObjectArrayElement(result, 3)); - pending_data = Envoy::JNI::buffer_to_native_data_ptr(jni_helper, j_data); - jni_helper.getEnv()->DeleteLocalRef(j_data); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result, 2); + pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers.get()); + + Envoy::JNI::LocalRefUniquePtr j_data = jni_helper.getObjectArrayElement(result, 3); + pending_data = Envoy::JNI::buffer_to_native_data_ptr(jni_helper, j_data.get()); } jni_helper.getEnv()->DeleteLocalRef(result); - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_trailers); return (envoy_filter_trailers_status){/*status*/ unboxed_status, /*trailers*/ native_trailers, @@ -597,7 +581,8 @@ static void jvm_http_filter_set_request_callbacks(envoy_http_filter_callbacks ca Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); envoy_http_filter_callbacks* on_heap_callbacks = static_cast(safe_malloc(sizeof(envoy_http_filter_callbacks))); @@ -605,10 +590,8 @@ static void jvm_http_filter_set_request_callbacks(envoy_http_filter_callbacks ca jlong callback_handle = reinterpret_cast(on_heap_callbacks); jmethodID jmid_setRequestFilterCallbacks = - jni_helper.getMethodId(jcls_JvmCallbackContext, "setRequestFilterCallbacks", "(J)V"); + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), "setRequestFilterCallbacks", "(J)V"); jni_helper.callVoidMethod(j_context, jmid_setRequestFilterCallbacks, callback_handle); - - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); } static void jvm_http_filter_set_response_callbacks(envoy_http_filter_callbacks callbacks, @@ -618,7 +601,8 @@ static void jvm_http_filter_set_response_callbacks(envoy_http_filter_callbacks c Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); envoy_http_filter_callbacks* on_heap_callbacks = static_cast(safe_malloc(sizeof(envoy_http_filter_callbacks))); @@ -626,10 +610,8 @@ static void jvm_http_filter_set_response_callbacks(envoy_http_filter_callbacks c jlong callback_handle = reinterpret_cast(on_heap_callbacks); jmethodID jmid_setResponseFilterCallbacks = - jni_helper.getMethodId(jcls_JvmCallbackContext, "setResponseFilterCallbacks", "(J)V"); + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), "setResponseFilterCallbacks", "(J)V"); jni_helper.callVoidMethod(j_context, jmid_setResponseFilterCallbacks, callback_handle); - - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); } static envoy_filter_resume_status @@ -656,38 +638,32 @@ jvm_http_filter_on_resume(const char* method, envoy_headers* headers, envoy_data } jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); - jclass jcls_JvmCallbackContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmCallbackContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onResume = - jni_helper.getMethodId(jcls_JvmCallbackContext, method, "(J[BJZ[J)Ljava/lang/Object;"); + jni_helper.getMethodId(jcls_JvmCallbackContext.get(), method, "(J[BJZ[J)Ljava/lang/Object;"); // Note: be careful of JVM types. Before we casted to jlong we were getting integer problems. // TODO: make this cast safer. Envoy::JNI::LocalRefUniquePtr result = jni_helper.callObjectMethod( j_context, jmid_onResume, headers_length, j_in_data, trailers_length, end_stream ? JNI_TRUE : JNI_FALSE, j_stream_intel); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmCallbackContext); jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); if (j_in_data != nullptr) { jni_helper.getEnv()->DeleteLocalRef(j_in_data); } - jobject status = jni_helper.getEnv()->GetObjectArrayElement(result.get(), 0); - jobjectArray j_headers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result.get(), 1)); - jobject j_data = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result.get(), 2)); - jobjectArray j_trailers = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(result.get(), 3)); - - int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status); - envoy_headers* pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers); - envoy_data* pending_data = Envoy::JNI::buffer_to_native_data_ptr(jni_helper, j_data); - envoy_headers* pending_trailers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_trailers); - - jni_helper.getEnv()->DeleteLocalRef(status); - jni_helper.getEnv()->DeleteLocalRef(j_headers); - jni_helper.getEnv()->DeleteLocalRef(j_data); - jni_helper.getEnv()->DeleteLocalRef(j_trailers); + Envoy::JNI::LocalRefUniquePtr status = jni_helper.getObjectArrayElement(result.get(), 0); + Envoy::JNI::LocalRefUniquePtr j_headers = + jni_helper.getObjectArrayElement(result.get(), 1); + Envoy::JNI::LocalRefUniquePtr j_data = jni_helper.getObjectArrayElement(result.get(), 2); + Envoy::JNI::LocalRefUniquePtr j_trailers = + jni_helper.getObjectArrayElement(result.get(), 3); + + int unboxed_status = Envoy::JNI::unbox_integer(jni_helper, status.get()); + envoy_headers* pending_headers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_headers.get()); + envoy_data* pending_data = Envoy::JNI::buffer_to_native_data_ptr(jni_helper, j_data.get()); + envoy_headers* pending_trailers = Envoy::JNI::to_native_headers_ptr(jni_helper, j_trailers.get()); return (envoy_filter_resume_status){/*status*/ unboxed_status, /*pending_headers*/ pending_headers, @@ -718,9 +694,10 @@ static void* call_jvm_on_complete(envoy_stream_intel stream_intel, Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(context); - jclass jcls_JvmObserverContext = jni_helper.getEnv()->GetObjectClass(j_context); - jmethodID jmid_onComplete = - jni_helper.getMethodId(jcls_JvmObserverContext, "onComplete", "([J[J)Ljava/lang/Object;"); + Envoy::JNI::LocalRefUniquePtr jcls_JvmObserverContext = + jni_helper.getObjectClass(j_context); + jmethodID jmid_onComplete = jni_helper.getMethodId(jcls_JvmObserverContext.get(), "onComplete", + "([J[J)Ljava/lang/Object;"); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); jlongArray j_final_stream_intel = @@ -731,7 +708,6 @@ static void* call_jvm_on_complete(envoy_stream_intel stream_intel, jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); jni_helper.getEnv()->DeleteLocalRef(j_final_stream_intel); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmObserverContext); return result; } @@ -741,9 +717,10 @@ static void* call_jvm_on_error(envoy_error error, envoy_stream_intel stream_inte Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(context); - jclass jcls_JvmObserverContext = jni_helper.getEnv()->GetObjectClass(j_context); - jmethodID jmid_onError = - jni_helper.getMethodId(jcls_JvmObserverContext, "onError", "(I[BI[J[J)Ljava/lang/Object;"); + Envoy::JNI::LocalRefUniquePtr jcls_JvmObserverContext = + jni_helper.getObjectClass(j_context); + jmethodID jmid_onError = jni_helper.getMethodId(jcls_JvmObserverContext.get(), "onError", + "(I[BI[J[J)Ljava/lang/Object;"); jbyteArray j_error_message = Envoy::JNI::native_data_to_array(jni_helper, error.message); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); @@ -758,7 +735,6 @@ static void* call_jvm_on_error(envoy_error error, envoy_stream_intel stream_inte jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); jni_helper.getEnv()->DeleteLocalRef(j_final_stream_intel); jni_helper.getEnv()->DeleteLocalRef(j_error_message); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmObserverContext); release_envoy_error(error); return result; } @@ -777,9 +753,10 @@ static void* call_jvm_on_cancel(envoy_stream_intel stream_intel, Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(context); - jclass jcls_JvmObserverContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmObserverContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onCancel = - jni_helper.getMethodId(jcls_JvmObserverContext, "onCancel", "([J[J)Ljava/lang/Object;"); + jni_helper.getMethodId(jcls_JvmObserverContext.get(), "onCancel", "([J[J)Ljava/lang/Object;"); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); jlongArray j_final_stream_intel = @@ -791,7 +768,6 @@ static void* call_jvm_on_cancel(envoy_stream_intel stream_intel, jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); jni_helper.getEnv()->DeleteLocalRef(j_final_stream_intel); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmObserverContext); return result; } @@ -827,9 +803,10 @@ static void* jvm_on_send_window_available(envoy_stream_intel stream_intel, void* Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(context); - jclass jcls_JvmObserverContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmObserverContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_onSendWindowAvailable = jni_helper.getMethodId( - jcls_JvmObserverContext, "onSendWindowAvailable", "([J)Ljava/lang/Object;"); + jcls_JvmObserverContext.get(), "onSendWindowAvailable", "([J)Ljava/lang/Object;"); jlongArray j_stream_intel = Envoy::JNI::native_stream_intel_to_array(jni_helper, stream_intel); @@ -837,7 +814,6 @@ static void* jvm_on_send_window_available(envoy_stream_intel stream_intel, void* jni_helper.callObjectMethod(j_context, jmid_onSendWindowAvailable, j_stream_intel).release(); jni_helper.getEnv()->DeleteLocalRef(j_stream_intel); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmObserverContext); return result; } @@ -848,15 +824,16 @@ static envoy_data jvm_kv_store_read(envoy_data key, const void* context) { jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmKeyValueStoreContext = jni_helper.getEnv()->GetObjectClass(j_context); - jmethodID jmid_read = jni_helper.getMethodId(jcls_JvmKeyValueStoreContext, "read", "([B)[B"); + Envoy::JNI::LocalRefUniquePtr jcls_JvmKeyValueStoreContext = + jni_helper.getObjectClass(j_context); + jmethodID jmid_read = + jni_helper.getMethodId(jcls_JvmKeyValueStoreContext.get(), "read", "([B)[B"); jbyteArray j_key = Envoy::JNI::native_data_to_array(jni_helper, key); Envoy::JNI::LocalRefUniquePtr j_value = jni_helper.callObjectMethod(j_context, jmid_read, j_key); envoy_data native_data = Envoy::JNI::array_to_native_data(jni_helper, j_value.get()); jni_helper.getEnv()->DeleteLocalRef(j_key); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmKeyValueStoreContext); return native_data; } @@ -867,13 +844,14 @@ static void jvm_kv_store_remove(envoy_data key, const void* context) { jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmKeyValueStoreContext = jni_helper.getEnv()->GetObjectClass(j_context); - jmethodID jmid_remove = jni_helper.getMethodId(jcls_JvmKeyValueStoreContext, "remove", "([B)V"); + Envoy::JNI::LocalRefUniquePtr jcls_JvmKeyValueStoreContext = + jni_helper.getObjectClass(j_context); + jmethodID jmid_remove = + jni_helper.getMethodId(jcls_JvmKeyValueStoreContext.get(), "remove", "([B)V"); jbyteArray j_key = Envoy::JNI::native_data_to_array(jni_helper, key); jni_helper.callVoidMethod(j_context, jmid_remove, j_key); jni_helper.getEnv()->DeleteLocalRef(j_key); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmKeyValueStoreContext); } static void jvm_kv_store_save(envoy_data key, envoy_data value, const void* context) { @@ -882,15 +860,16 @@ static void jvm_kv_store_save(envoy_data key, envoy_data value, const void* cont jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmKeyValueStoreContext = jni_helper.getEnv()->GetObjectClass(j_context); - jmethodID jmid_save = jni_helper.getMethodId(jcls_JvmKeyValueStoreContext, "save", "([B[B)V"); + Envoy::JNI::LocalRefUniquePtr jcls_JvmKeyValueStoreContext = + jni_helper.getObjectClass(j_context); + jmethodID jmid_save = + jni_helper.getMethodId(jcls_JvmKeyValueStoreContext.get(), "save", "([B[B)V"); jbyteArray j_key = Envoy::JNI::native_data_to_array(jni_helper, key); jbyteArray j_value = Envoy::JNI::native_data_to_array(jni_helper, value); jni_helper.callVoidMethod(j_context, jmid_save, j_key, j_value); jni_helper.getEnv()->DeleteLocalRef(j_value); jni_helper.getEnv()->DeleteLocalRef(j_key); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmKeyValueStoreContext); } // JvmFilterFactoryContext @@ -905,9 +884,10 @@ static const void* jvm_http_filter_init(const void* context) { jni_log_fmt("[Envoy]", "j_context: %p", j_context); - jclass jcls_JvmFilterFactoryContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmFilterFactoryContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_create = - jni_helper.getMethodId(jcls_JvmFilterFactoryContext, "create", + jni_helper.getMethodId(jcls_JvmFilterFactoryContext.get(), "create", "()Lio/envoyproxy/envoymobile/engine/JvmFilterContext;"); Envoy::JNI::LocalRefUniquePtr j_filter = @@ -915,8 +895,6 @@ static const void* jvm_http_filter_init(const void* context) { jni_log_fmt("[Envoy]", "j_filter: %p", j_filter.get()); jobject retained_filter = jni_helper.getEnv()->NewGlobalRef(j_filter.get()); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmFilterFactoryContext); - return retained_filter; } @@ -925,15 +903,14 @@ static const void* jvm_http_filter_init(const void* context) { static envoy_data jvm_get_string(const void* context) { Envoy::JNI::JniHelper jni_helper(Envoy::JNI::get_env()); jobject j_context = static_cast(const_cast(context)); - jclass jcls_JvmStringAccessorContext = jni_helper.getEnv()->GetObjectClass(j_context); + Envoy::JNI::LocalRefUniquePtr jcls_JvmStringAccessorContext = + jni_helper.getObjectClass(j_context); jmethodID jmid_getString = - jni_helper.getMethodId(jcls_JvmStringAccessorContext, "getEnvoyString", "()[B"); + jni_helper.getMethodId(jcls_JvmStringAccessorContext.get(), "getEnvoyString", "()[B"); Envoy::JNI::LocalRefUniquePtr j_data = jni_helper.callObjectMethod(j_context, jmid_getString); envoy_data native_data = Envoy::JNI::array_to_native_data(jni_helper, j_data.get()); - jni_helper.getEnv()->DeleteLocalRef(jcls_JvmStringAccessorContext); - return native_data; } @@ -1198,11 +1175,10 @@ std::vector javaObjectArrayToStringVector(Envoy::JNI::JniHelper& jn for (envoy_map_size_t i = 0; i < length; ++i) { // Copy native byte array for header key - jbyteArray j_str = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(entries, i)); - std::string str = javaByteArrayToString(jni_helper, j_str); - ret.push_back(javaByteArrayToString(jni_helper, j_str)); - jni_helper.getEnv()->DeleteLocalRef(j_str); + Envoy::JNI::LocalRefUniquePtr j_str = + jni_helper.getObjectArrayElement(entries, i); + std::string str = javaByteArrayToString(jni_helper, j_str.get()); + ret.push_back(javaByteArrayToString(jni_helper, j_str.get())); } return ret; @@ -1221,16 +1197,13 @@ javaObjectArrayToStringPairVector(Envoy::JNI::JniHelper& jni_helper, jobjectArra for (envoy_map_size_t i = 0; i < length; i += 2) { // Copy native byte array for header key - jbyteArray j_key = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(entries, i)); - jbyteArray j_value = - static_cast(jni_helper.getEnv()->GetObjectArrayElement(entries, i + 1)); - std::string first = javaByteArrayToString(jni_helper, j_key); - std::string second = javaByteArrayToString(jni_helper, j_value); + Envoy::JNI::LocalRefUniquePtr j_key = + jni_helper.getObjectArrayElement(entries, i); + Envoy::JNI::LocalRefUniquePtr j_value = + jni_helper.getObjectArrayElement(entries, i + 1); + std::string first = javaByteArrayToString(jni_helper, j_key.get()); + std::string second = javaByteArrayToString(jni_helper, j_value.get()); ret.push_back(std::make_pair(first, second)); - - jni_helper.getEnv()->DeleteLocalRef(j_key); - jni_helper.getEnv()->DeleteLocalRef(j_value); } return ret; diff --git a/mobile/library/common/jni/jni_utility.cc b/mobile/library/common/jni/jni_utility.cc index 4592467e46f6..ff0970405be2 100644 --- a/mobile/library/common/jni/jni_utility.cc +++ b/mobile/library/common/jni/jni_utility.cc @@ -351,38 +351,6 @@ MatcherData::Type StringToType(std::string type_as_string) { return MatcherData::EXACT; } -std::vector javaObjectArrayToMatcherData(JniHelper& jni_helper, jobjectArray array, - std::string& cluster_name_out) { - const size_t len = jni_helper.getArrayLength(array); - std::vector ret; - if (len == 0) { - return ret; - } - ASSERT((len - 1) % 3 == 0); - if ((len - 1) % 3 != 0) { - return ret; - } - - JavaArrayOfByteToString( - jni_helper, static_cast(jni_helper.getEnv()->GetObjectArrayElement(array, 0)), - &cluster_name_out); - for (size_t i = 1; i < len; i += 3) { - std::string name; - std::string type_as_string; - std::string value; - LocalRefUniquePtr element1 = jni_helper.getObjectArrayElement(array, i); - JavaArrayOfByteToString(jni_helper, element1.get(), &name); - LocalRefUniquePtr element2 = - jni_helper.getObjectArrayElement(array, i + 1); - JavaArrayOfByteToString(jni_helper, element2.get(), &type_as_string); - LocalRefUniquePtr element3 = - jni_helper.getObjectArrayElement(array, i + 2); - JavaArrayOfByteToString(jni_helper, element3.get(), &value); - ret.emplace_back(MatcherData(name, StringToType(type_as_string), value)); - } - return ret; -} - void javaByteArrayToProto(JniHelper& jni_helper, jbyteArray source, Envoy::Protobuf::MessageLite* dest) { ArrayElementsUniquePtr bytes = diff --git a/mobile/library/common/jni/jni_utility.h b/mobile/library/common/jni/jni_utility.h index 0e11bfbe751d..cc4ed9b2c9ee 100644 --- a/mobile/library/common/jni/jni_utility.h +++ b/mobile/library/common/jni/jni_utility.h @@ -113,9 +113,6 @@ void JavaArrayOfByteToBytesVector(JniHelper& jni_helper, jbyteArray array, void JavaArrayOfByteToString(JniHelper& jni_helper, jbyteArray jbytes, std::string* out); -std::vector javaObjectArrayToMatcherData(JniHelper& jni_helper, jobjectArray array, - std::string& cluster_out); - /** Parses the proto from Java's byte array and stores the output into `dest` proto. */ void javaByteArrayToProto(JniHelper& jni_helper, jbyteArray source, Envoy::Protobuf::MessageLite* dest);