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

Side by Side Diff: libadblockplus-android/jni/JniJsValue.cpp

Issue 29354787: Issue 4442 - Cache class and ctors references in JNI_Load/UnLoad (Closed)
Patch Set: reverted to NewTuple Created Nov. 21, 2016, 8:25 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « libadblockplus-android/jni/JniJsValue.h ('k') | libadblockplus-android/jni/JniLibrary.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include "Utils.h" 19 #include "Utils.h"
20 #include "JniJsValue.h" 20 #include "JniJsValue.h"
21 21
22 // precached in JNI_OnLoad and released in JNI_OnUnload 22 // precached in JNI_OnLoad and released in JNI_OnUnload
23 jclass globalJsValueClass; 23 JniGlobalReference<jclass>* jsValueClass;
24 jmethodID jsValueClassCtor; 24 jmethodID jsValueCtor;
25 25
26 void JniJsValue_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved) 26 void JniJsValue_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved)
27 { 27 {
28 // precache for performance and avoid attaching threads 28 jsValueClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("JsValue ")));
29 jclass localJsValueClass = env->FindClass(PKG("JsValue")); 29 jsValueCtor = env->GetMethodID(jsValueClass->Get(), "<init>", "(J)V");
30 globalJsValueClass = (jclass)env->NewGlobalRef(localJsValueClass);
31 jsValueClassCtor = env->GetMethodID(globalJsValueClass, "<init>", "(J)V");
32 env->DeleteLocalRef(localJsValueClass);
33 } 30 }
34 31
35 void JniJsValue_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved) 32 void JniJsValue_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved)
36 { 33 {
37 if (globalJsValueClass) 34 if (jsValueClass)
38 { 35 {
39 env->DeleteGlobalRef(globalJsValueClass); 36 delete jsValueClass;
37 jsValueClass = NULL;
40 } 38 }
41 } 39 }
42 40
41 jclass GetJsValueClass()
42 {
43 return (jsValueClass != NULL ? jsValueClass->Get() : NULL);
44 }
45
43 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) 46 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr)
44 { 47 {
45 try 48 try
46 { 49 {
47 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; 50 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE;
48 } 51 }
49 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 52 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
50 } 53 }
51 54
52 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) 55 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr)
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 return NewJniJsValue(env, JniGetJsValue(ptr)->GetProperty(JniJavaToStdString (env, name))); 160 return NewJniJsValue(env, JniGetJsValue(ptr)->GetProperty(JniJavaToStdString (env, name)));
158 } 161 }
159 CATCH_THROW_AND_RETURN(env, 0) 162 CATCH_THROW_AND_RETURN(env, 0)
160 } 163 }
161 164
162 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 165 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
163 { 166 {
164 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); 167 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr);
165 } 168 }
166 169
167 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas s jsValueClass) 170 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas s jsValueClassArg)
168 { 171 {
169 if (!jsValue.get()) 172 if (!jsValue.get())
170 { 173 {
171 return 0; 174 return 0;
172 } 175 }
173 176
174 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); 177 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue));
175 return env->NewObject(globalJsValueClass, jsValueClassCtor, ptr); 178 return env->NewObject(jsValueClass->Get(), jsValueCtor, ptr);
176 } 179 }
177 180
178 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) 181 AdblockPlus::JsValue* JniGetJsValue(jlong ptr)
179 { 182 {
180 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); 183 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get();
181 } 184 }
182 185
183 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) 186 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr)
184 { 187 {
185 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); 188 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, 227 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean },
225 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, 228 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList },
226 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*) JniGetProperty }, 229 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*) JniGetProperty },
227 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 230 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
228 }; 231 };
229 232
230 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz) 233 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz)
231 { 234 {
232 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 235 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
233 } 236 }
OLDNEW
« no previous file with comments | « libadblockplus-android/jni/JniJsValue.h ('k') | libadblockplus-android/jni/JniLibrary.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld