| 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 |
| 35 void JniJsValue_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved) |
| 36 { |
| 37 if (globalJsValueClass) |
| 38 { |
| 39 env->DeleteGlobalRef(globalJsValueClass); |
| 40 } |
| 41 } |
| 42 |
| 22 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) | 43 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
| 23 { | 44 { |
| 24 try | 45 try |
| 25 { | 46 { |
| 26 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; | 47 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; |
| 27 } | 48 } |
| 28 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 49 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
| 29 } | 50 } |
| 30 | 51 |
| 31 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) | 52 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); | 164 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
| 144 } | 165 } |
| 145 | 166 |
| 146 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
s jsValueClass) | 167 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
s jsValueClass) |
| 147 { | 168 { |
| 148 if (!jsValue.get()) | 169 if (!jsValue.get()) |
| 149 { | 170 { |
| 150 return 0; | 171 return 0; |
| 151 } | 172 } |
| 152 | 173 |
| 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)); | 174 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); |
| 156 jobject ret = env->NewObject(clazz, ctor, ptr); | 175 return env->NewObject(globalJsValueClass, jsValueClassCtor, ptr); |
| 157 | |
| 158 if (!jsValueClass) | |
| 159 { | |
| 160 env->DeleteLocalRef(clazz); | |
| 161 } | |
| 162 | |
| 163 return ret; | |
| 164 } | 176 } |
| 165 | 177 |
| 166 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) | 178 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) |
| 167 { | 179 { |
| 168 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); | 180 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); |
| 169 } | 181 } |
| 170 | 182 |
| 171 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) | 183 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) |
| 172 { | 184 { |
| 173 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 185 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 }, | 224 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
| 213 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, | 225 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
| 214 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)
JniGetProperty }, | 226 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)
JniGetProperty }, |
| 215 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 227 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
| 216 }; | 228 }; |
| 217 | 229 |
| 218 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) | 230 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) |
| 219 { | 231 { |
| 220 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 232 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
| 221 } | 233 } |
| OLD | NEW |