| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 #include <string> | |
| 19 | |
| 20 #include "Utils.h" | |
| 21 | |
| 22 // precached in JNI_OnLoad and released in JNI_OnUnload | |
| 23 JniGlobalReference<jclass>* arrayListClass; | |
| 24 jmethodID arrayListCtor; | |
| 25 | |
| 26 JniGlobalReference<jclass>* filterClass; | |
| 27 jmethodID filterCtor; | |
| 28 | |
| 29 JniGlobalReference<jclass>* subscriptionClass; | |
| 30 jmethodID subscriptionCtor; | |
| 31 | |
| 32 JniGlobalReference<jclass>* notificationClass; | |
| 33 jmethodID notificationCtor; | |
| 34 | |
| 35 JniGlobalReference<jclass>* exceptionClass; | |
| 36 | |
| 37 void JniUtils_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved) | |
| 38 { | |
| 39 arrayListClass = new JniGlobalReference<jclass>(env, env->FindClass("java/util
/ArrayList")); | |
| 40 arrayListCtor = env->GetMethodID(arrayListClass->Get(), "<init>", "()V"); | |
| 41 | |
| 42 filterClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Filter")
)); | |
| 43 filterCtor = env->GetMethodID(filterClass->Get(), "<init>", "(J)V"); | |
| 44 | |
| 45 subscriptionClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Su
bscription"))); | |
| 46 subscriptionCtor = env->GetMethodID(subscriptionClass->Get(), "<init>", "(J)V"
); | |
| 47 | |
| 48 notificationClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("No
tification"))); | |
| 49 notificationCtor = env->GetMethodID(notificationClass->Get(), "<init>", "(J)V"
); | |
| 50 | |
| 51 exceptionClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Adblo
ckPlusException"))); | |
| 52 } | |
| 53 | |
| 54 void JniUtils_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved) | |
| 55 { | |
| 56 if (arrayListClass) | |
| 57 { | |
| 58 delete arrayListClass; | |
| 59 arrayListClass = NULL; | |
| 60 } | |
| 61 | |
| 62 if (filterClass) | |
| 63 { | |
| 64 delete filterClass; | |
| 65 filterClass = NULL; | |
| 66 } | |
| 67 | |
| 68 if (subscriptionClass) | |
| 69 { | |
| 70 delete subscriptionClass; | |
| 71 subscriptionClass = NULL; | |
| 72 } | |
| 73 | |
| 74 if (notificationClass) | |
| 75 { | |
| 76 delete notificationClass; | |
| 77 notificationClass = NULL; | |
| 78 } | |
| 79 | |
| 80 if (exceptionClass) | |
| 81 { | |
| 82 delete exceptionClass; | |
| 83 exceptionClass = NULL; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 std::string JniJavaToStdString(JNIEnv* env, jstring str) | |
| 88 { | |
| 89 if (!str) | |
| 90 { | |
| 91 return std::string(); | |
| 92 } | |
| 93 | |
| 94 const char* cStr = env->GetStringUTFChars(str, 0); | |
| 95 std::string ret(cStr); | |
| 96 env->ReleaseStringUTFChars(str, cStr); | |
| 97 | |
| 98 return ret; | |
| 99 } | |
| 100 | |
| 101 jstring JniStdStringToJava(JNIEnv* env, std::string str) | |
| 102 { | |
| 103 return env->NewStringUTF(str.c_str()); | |
| 104 } | |
| 105 | |
| 106 bool stringBeginsWith(const std::string& string, const std::string& beginning) | |
| 107 { | |
| 108 return string.compare(0, beginning.length(), beginning) == 0; | |
| 109 } | |
| 110 | |
| 111 jobject NewJniArrayList(JNIEnv* env) | |
| 112 { | |
| 113 return env->NewObject(arrayListClass->Get(), arrayListCtor); | |
| 114 } | |
| 115 | |
| 116 jmethodID JniGetAddToListMethod(JNIEnv* env, jobject list) | |
| 117 { | |
| 118 JniLocalReference<jclass> clazz(env, env->GetObjectClass(list)); | |
| 119 return env->GetMethodID(*clazz, "add", "(Ljava/lang/Object;)Z"); | |
| 120 } | |
| 121 | |
| 122 void JniAddObjectToList(JNIEnv* env, jobject list, jmethodID addMethod, jobject
value) | |
| 123 { | |
| 124 env->CallBooleanMethod(list, addMethod, value); | |
| 125 } | |
| 126 | |
| 127 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value) | |
| 128 { | |
| 129 jmethodID addMethod = JniGetAddToListMethod(env, list); | |
| 130 JniAddObjectToList(env, list, addMethod, value); | |
| 131 } | |
| 132 | |
| 133 void JniThrowException(JNIEnv* env, const std::string& message) | |
| 134 { | |
| 135 env->ThrowNew(exceptionClass->Get(), message.c_str()); | |
| 136 } | |
| 137 | |
| 138 void JniThrowException(JNIEnv* env, const std::exception& e) | |
| 139 { | |
| 140 JniThrowException(env, e.what()); | |
| 141 } | |
| 142 | |
| 143 void JniThrowException(JNIEnv* env) | |
| 144 { | |
| 145 JniThrowException(env, "Unknown exception from libadblockplus"); | |
| 146 } | |
| 147 | |
| 148 JNIEnvAcquire::JNIEnvAcquire(JavaVM* javaVM) | |
| 149 : javaVM(javaVM), jniEnv(0), attachmentStatus(0) | |
| 150 { | |
| 151 attachmentStatus = javaVM->GetEnv((void **)&jniEnv, ABP_JNI_VERSION); | |
| 152 if (attachmentStatus == JNI_EDETACHED) | |
| 153 { | |
| 154 if (javaVM->AttachCurrentThread(&jniEnv, 0)) | |
| 155 { | |
| 156 // This one is FATAL, we can't recover from this (because without a JVM we
're dead), so | |
| 157 // throwing a runtime_exception in a ctor can be tolerated here IMHO | |
| 158 throw std::runtime_error("Failed to get JNI environment"); | |
| 159 } | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 JNIEnvAcquire::~JNIEnvAcquire() | |
| 164 { | |
| 165 if (attachmentStatus == JNI_EDETACHED) | |
| 166 { | |
| 167 javaVM->DetachCurrentThread(); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 template<typename T> | |
| 172 static jobject NewJniObject(JNIEnv* env, T&& value, jclass clazz, jmethodID ctor
) | |
| 173 { | |
| 174 return env->NewObject(clazz, ctor, JniPtrToLong(new T(std::forward<T>(value)))
); | |
| 175 } | |
| 176 | |
| 177 jobject NewJniFilter(JNIEnv* env, AdblockPlus::Filter&& filter) | |
| 178 { | |
| 179 return NewJniObject<AdblockPlus::Filter>( | |
| 180 env, std::move(filter), filterClass->Get(), filterCtor); | |
| 181 } | |
| 182 | |
| 183 jobject NewJniSubscription(JNIEnv* env, AdblockPlus::Subscription&& subscription
) | |
| 184 { | |
| 185 return NewJniObject<AdblockPlus::Subscription>( | |
| 186 env, std::move(subscription), subscriptionClass->Get(), subscriptionCtor); | |
| 187 } | |
| 188 | |
| 189 jobject NewJniNotification(JNIEnv* env, AdblockPlus::Notification&& notification
) | |
| 190 { | |
| 191 return NewJniObject<AdblockPlus::Notification>( | |
| 192 env, std::move(notification), notificationClass->Get(), notificationCtor); | |
| 193 } | |
| OLD | NEW |