| LEFT | RIGHT |
| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 #ifndef UTILS_H | 18 #ifndef UTILS_H |
| 19 #define UTILS_H | 19 #define UTILS_H |
| 20 | 20 |
| 21 #include <string> | 21 #include <string> |
| 22 #include <algorithm> | 22 #include <algorithm> |
| 23 #include <jni.h> | 23 #include <jni.h> |
| 24 #include <stdexcept> | 24 #include <stdexcept> |
| 25 | 25 |
| 26 #include <AdblockPlus.h> | 26 #include <AdblockPlus.h> |
| 27 #include <AdblockPlus/tr1_memory.h> | 27 #include <AdblockPlus/tr1_memory.h> |
| 28 | 28 |
| 29 #include "v8/v8stdint.h" | |
| 30 | |
| 31 #define PKG(x) "org/adblockplus/libadblockplus/" x | 29 #define PKG(x) "org/adblockplus/libadblockplus/" x |
| 32 #define TYP(x) "L" PKG(x) ";" | 30 #define TYP(x) "L" PKG(x) ";" |
| 33 | 31 |
| 34 #define ABP_JNI_VERSION JNI_VERSION_1_6 | 32 #define ABP_JNI_VERSION JNI_VERSION_1_6 |
| 35 | 33 |
| 36 void JniThrowException(JNIEnv* env, const std::string& message); | 34 void JniThrowException(JNIEnv* env, const std::string& message); |
| 37 | 35 |
| 38 void JniThrowException(JNIEnv* env, const std::exception& e); | 36 void JniThrowException(JNIEnv* env, const std::exception& e); |
| 39 | 37 |
| 40 void JniThrowException(JNIEnv* env); | 38 void JniThrowException(JNIEnv* env); |
| 39 |
| 40 class JNIEnvAcquire; |
| 41 |
| 42 /** |
| 43 * This class is _NOT_ thread safe! |
| 44 */ |
| 45 template<typename T> |
| 46 class JniLocalReference |
| 47 { |
| 48 public: |
| 49 JniLocalReference(JNIEnv* jniEnv, T object) |
| 50 : jniEnv(jniEnv), object(object) |
| 51 { |
| 52 |
| 53 } |
| 54 |
| 55 JniLocalReference(const JniLocalReference<T>& other); |
| 56 |
| 57 ~JniLocalReference() |
| 58 { |
| 59 jniEnv->DeleteLocalRef(object); |
| 60 } |
| 61 |
| 62 T operator*() |
| 63 { |
| 64 return object; |
| 65 } |
| 66 |
| 67 T Get() |
| 68 { |
| 69 return object; |
| 70 } |
| 71 |
| 72 private: |
| 73 JNIEnv* jniEnv; |
| 74 T object; |
| 75 }; |
| 41 | 76 |
| 42 class JNIEnvAcquire | 77 class JNIEnvAcquire |
| 43 { | 78 { |
| 44 public: | 79 public: |
| 45 JNIEnvAcquire(JavaVM* javaVM); | 80 JNIEnvAcquire(JavaVM* javaVM); |
| 46 ~JNIEnvAcquire(); | 81 ~JNIEnvAcquire(); |
| 47 | 82 |
| 48 JNIEnv* operator*() | 83 JNIEnv* operator*() |
| 49 { | 84 { |
| 50 return jniEnv; | 85 return jniEnv; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so
maybe | 140 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so
maybe |
| 106 // implement a special cast functions that first reinterpret_casts to shared_ptr
<JsValue> and then | 141 // implement a special cast functions that first reinterpret_casts to shared_ptr
<JsValue> and then |
| 107 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m
irrored on the Java | 142 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m
irrored on the Java |
| 108 // side (and Java will throw a class cast exception on error) this shouldn't be
an issue (TM) | 143 // side (and Java will throw a class cast exception on error) this shouldn't be
an issue (TM) |
| 109 template<typename T> | 144 template<typename T> |
| 110 inline T* JniLongToTypePtr(jlong value) | 145 inline T* JniLongToTypePtr(jlong value) |
| 111 { | 146 { |
| 112 return reinterpret_cast<T*>((size_t)value); | 147 return reinterpret_cast<T*>((size_t)value); |
| 113 } | 148 } |
| 114 | 149 |
| 150 std::string JniJavaToStdString(JNIEnv* env, jstring str); |
| 151 |
| 152 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value); |
| 153 |
| 154 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co
nst char* name) |
| 155 { |
| 156 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j
Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;")))); |
| 157 } |
| 158 |
| 159 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch
ar* name) |
| 160 { |
| 161 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR
UE; |
| 162 } |
| 163 |
| 164 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha
r* name) |
| 165 { |
| 166 return (int32_t)env->GetIntField(jObj, env->GetFieldID(clazz, name, "I")); |
| 167 } |
| 168 |
| 169 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch
ar* name) |
| 170 { |
| 171 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J")); |
| 172 } |
| 173 |
| 115 jobject NewJniArrayList(JNIEnv* env); | 174 jobject NewJniArrayList(JNIEnv* env); |
| 116 | 175 |
| 117 std::string JniJavaToStdString(JNIEnv* env, jstring str); | 176 jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter); |
| 118 | 177 |
| 119 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value); | 178 jobject NewJniSubscription(JNIEnv* env, |
| 120 | 179 const AdblockPlus::SubscriptionPtr& subscription); |
| 121 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co
nst char* name) | 180 |
| 122 { | 181 jobject NewJniNotification(JNIEnv* env, |
| 123 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j
Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;")))); | 182 const AdblockPlus::NotificationPtr& notification); |
| 124 } | |
| 125 | |
| 126 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch
ar* name) | |
| 127 { | |
| 128 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR
UE; | |
| 129 } | |
| 130 | |
| 131 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha
r* name) | |
| 132 { | |
| 133 return (int32_t)env->GetIntField(jObj, env->GetFieldID(clazz, name, "I")); | |
| 134 } | |
| 135 | |
| 136 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch
ar* name) | |
| 137 { | |
| 138 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J")); | |
| 139 } | |
| 140 | |
| 141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) | |
| 142 { | |
| 143 if (!filter.get()) | |
| 144 { | |
| 145 return 0; | |
| 146 } | |
| 147 | |
| 148 jclass clazz = env->FindClass(PKG("Filter")); | |
| 149 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); | |
| 150 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f
ilter))); | |
| 151 } | |
| 152 | |
| 153 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt
r& subscription) | |
| 154 { | |
| 155 if (!subscription.get()) | |
| 156 { | |
| 157 return 0; | |
| 158 } | |
| 159 | |
| 160 jclass clazz = env->FindClass(PKG("Subscription")); | |
| 161 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); | |
| 162 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio
nPtr(subscription))); | |
| 163 } | |
| 164 | |
| 165 inline jobject NewJniNotification(JNIEnv* env, const AdblockPlus::NotificationPt
r& notification) | |
| 166 { | |
| 167 if (!notification.get()) | |
| 168 { | |
| 169 return 0; | |
| 170 } | |
| 171 | |
| 172 jclass clazz = env->FindClass(PKG("Notification")); | |
| 173 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); | |
| 174 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Notificatio
nPtr(notification))); | |
| 175 } | |
| 176 | 183 |
| 177 #define CATCH_AND_THROW(jEnv) \ | 184 #define CATCH_AND_THROW(jEnv) \ |
| 178 catch (const std::exception& except) \ | 185 catch (const std::exception& except) \ |
| 179 { \ | 186 { \ |
| 180 JniThrowException(jEnv, except); \ | 187 JniThrowException(jEnv, except); \ |
| 181 } \ | 188 } \ |
| 182 catch (...) \ | 189 catch (...) \ |
| 183 { \ | 190 { \ |
| 184 JniThrowException(jEnv); \ | 191 JniThrowException(jEnv); \ |
| 185 } | 192 } |
| 186 | 193 |
| 187 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ | 194 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ |
| 188 catch (const std::exception& except) \ | 195 catch (const std::exception& except) \ |
| 189 { \ | 196 { \ |
| 190 JniThrowException(jEnv, except); \ | 197 JniThrowException(jEnv, except); \ |
| 191 return retVal; \ | 198 return retVal; \ |
| 192 } \ | 199 } \ |
| 193 catch (...) \ | 200 catch (...) \ |
| 194 { \ | 201 { \ |
| 195 JniThrowException(jEnv); \ | 202 JniThrowException(jEnv); \ |
| 196 return retVal; \ | 203 return retVal; \ |
| 197 } | 204 } |
| 198 | 205 |
| 199 #endif | 206 #endif |
| LEFT | RIGHT |