Left: | ||
Right: |
OLD | NEW |
---|---|
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 | |
23 jclass globalJsValueClass; | |
24 jmethodID jsValueClassCtor; | |
25 | |
26 void JniJsValue_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved) | |
27 { | |
28 // precache for performance and avoid attaching threads | |
29 jclass localJsValueClass = env->FindClass(PKG("JsValue")); | |
30 globalJsValueClass = (jclass)env->NewGlobalRef(localJsValueClass); | |
31 jsValueClassCtor = env->GetMethodID(globalJsValueClass, "<init>", "(J)V"); | |
32 env->DeleteLocalRef(localJsValueClass); | |
33 } | |
34 | |
22 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) | 35 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
23 { | 36 { |
24 try | 37 try |
25 { | 38 { |
26 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; | 39 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; |
27 } | 40 } |
28 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 41 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
29 } | 42 } |
30 | 43 |
31 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) | 44 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 156 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
144 } | 157 } |
145 | 158 |
146 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas s jsValueClass) | 159 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas s jsValueClass) |
147 { | 160 { |
148 if (!jsValue.get()) | 161 if (!jsValue.get()) |
149 { | 162 { |
150 return 0; | 163 return 0; |
151 } | 164 } |
152 | 165 |
153 jclass clazz = jsValueClass ? jsValueClass : env->FindClass(PKG("JsValue")); | |
154 jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); | |
155 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); | 166 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); |
156 jobject ret = env->NewObject(clazz, ctor, ptr); | 167 return env->NewObject(globalJsValueClass, jsValueClassCtor, ptr); |
157 | |
158 if (!jsValueClass) | |
159 { | |
160 env->DeleteLocalRef(clazz); | |
161 } | |
162 | |
163 return ret; | |
164 } | 168 } |
165 | 169 |
166 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) | 170 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) |
167 { | 171 { |
168 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); | 172 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); |
169 } | 173 } |
170 | 174 |
171 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) | 175 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) |
172 { | 176 { |
173 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 177 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, | 216 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
213 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, | 217 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
214 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*) JniGetProperty }, | 218 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*) JniGetProperty }, |
215 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 219 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
216 }; | 220 }; |
217 | 221 |
218 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz) | 222 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz) |
219 { | 223 { |
220 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 224 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
221 } | 225 } |
226 | |
227 void JniJsValue_OnUnload(JavaVM *vm, JNIEnv *env, void *reserved) | |
Felix Dahlke
2016/09/15 13:01:19
Nit: Asterisk should be next to the type according
anton
2016/09/15 13:18:11
Acknowledged.
| |
228 { | |
229 if (globalJsValueClass) | |
230 { | |
231 env->DeleteGlobalRef(globalJsValueClass); | |
232 } | |
233 } | |
OLD | NEW |