Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: libadblockplus-android/jni/JniJsValue.cpp

Issue 29345737: Issue 4146 - Fix failing UpdateCheckTest.testApplicationUpdateAvailable test in libadblockplus-andr… (Closed)
Patch Set: codestyle, moved unload Created Sept. 15, 2016, 1:28 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: libadblockplus-android/jni/JniJsValue.cpp
diff --git a/libadblockplus-android/jni/JniJsValue.cpp b/libadblockplus-android/jni/JniJsValue.cpp
index 86af61459baaeaa468e7a832230b60fc35cb98bb..6464b1eae10f09b28da34c71f8c3a9affc2b4a1f 100644
--- a/libadblockplus-android/jni/JniJsValue.cpp
+++ b/libadblockplus-android/jni/JniJsValue.cpp
@@ -19,6 +19,27 @@
#include "Utils.h"
#include "JniJsValue.h"
+// precached in JNI_OnLoad and released in JNI_OnUnload
+jclass globalJsValueClass;
+jmethodID jsValueClassCtor;
+
+void JniJsValue_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved)
+{
+ // precache for performance and avoid attaching threads
+ jclass localJsValueClass = env->FindClass(PKG("JsValue"));
+ globalJsValueClass = (jclass)env->NewGlobalRef(localJsValueClass);
+ jsValueClassCtor = env->GetMethodID(globalJsValueClass, "<init>", "(J)V");
+ env->DeleteLocalRef(localJsValueClass);
+}
+
+void JniJsValue_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved)
+{
+ if (globalJsValueClass)
+ {
+ env->DeleteGlobalRef(globalJsValueClass);
+ }
+}
+
static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr)
{
try
@@ -150,17 +171,8 @@ jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
return 0;
}
- jclass clazz = jsValueClass ? jsValueClass : env->FindClass(PKG("JsValue"));
- jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V");
jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue));
- jobject ret = env->NewObject(clazz, ctor, ptr);
-
- if (!jsValueClass)
- {
- env->DeleteLocalRef(clazz);
- }
-
- return ret;
+ return env->NewObject(globalJsValueClass, jsValueClassCtor, ptr);
}
AdblockPlus::JsValue* JniGetJsValue(jlong ptr)

Powered by Google App Engine
This is Rietveld