| 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-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 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 #define TYP(x) "L" PKG(x) ";" | 32 #define TYP(x) "L" PKG(x) ";" |
| 33 | 33 |
| 34 #define ABP_JNI_VERSION JNI_VERSION_1_6 | 34 #define ABP_JNI_VERSION JNI_VERSION_1_6 |
| 35 | 35 |
| 36 void JniThrowException(JNIEnv* env, const std::string& message); | 36 void JniThrowException(JNIEnv* env, const std::string& message); |
| 37 | 37 |
| 38 void JniThrowException(JNIEnv* env, const std::exception& e); | 38 void JniThrowException(JNIEnv* env, const std::exception& e); |
| 39 | 39 |
| 40 void JniThrowException(JNIEnv* env); | 40 void JniThrowException(JNIEnv* env); |
| 41 | 41 |
| 42 class JNIEnvAcquire; | |
| 43 | |
| 44 /** | |
| 45 * This class is _NOT_ thread safe! | |
| 46 */ | |
| 47 template<typename T> | |
| 48 class JniLocalReference | |
| 49 { | |
| 50 public: | |
| 51 explicit JniLocalReference(JNIEnv* jniEnv, T object) | |
|
Felix Dahlke
2015/02/03 05:22:50
Doesn't need to be explicit since there's two argu
René Jeschke
2015/02/03 13:51:26
Done.
| |
| 52 : jniEnv(jniEnv), object(object) | |
| 53 { | |
| 54 | |
| 55 } | |
| 56 | |
| 57 JniLocalReference(const JniLocalReference<T>& other); | |
| 58 | |
| 59 ~JniLocalReference() | |
| 60 { | |
| 61 jniEnv->DeleteLocalRef(object); | |
| 62 } | |
| 63 | |
| 64 T operator*() | |
| 65 { | |
| 66 return object; | |
| 67 } | |
| 68 | |
| 69 T Get() | |
| 70 { | |
| 71 return object; | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 JNIEnv* jniEnv; | |
| 76 T object; | |
| 77 }; | |
| 78 | |
| 42 class JNIEnvAcquire | 79 class JNIEnvAcquire |
| 43 { | 80 { |
| 44 public: | 81 public: |
| 45 JNIEnvAcquire(JavaVM* javaVM); | 82 JNIEnvAcquire(JavaVM* javaVM); |
| 46 ~JNIEnvAcquire(); | 83 ~JNIEnvAcquire(); |
| 47 | 84 |
| 48 JNIEnv* operator*() | 85 JNIEnv* operator*() |
| 49 { | 86 { |
| 50 return jniEnv; | 87 return jniEnv; |
| 51 } | 88 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J")); | 175 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J")); |
| 139 } | 176 } |
| 140 | 177 |
| 141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) | 178 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) |
| 142 { | 179 { |
| 143 if (!filter.get()) | 180 if (!filter.get()) |
| 144 { | 181 { |
| 145 return 0; | 182 return 0; |
| 146 } | 183 } |
| 147 | 184 |
| 148 jclass clazz = env->FindClass(PKG("Filter")); | 185 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Filter"))); |
| 149 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); | 186 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V"); |
| 150 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter))); | 187 return env->NewObject(*clazz, method, |
| 188 JniPtrToLong(new AdblockPlus::FilterPtr(filter))); | |
| 151 } | 189 } |
| 152 | 190 |
| 153 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) | 191 inline jobject NewJniSubscription(JNIEnv* env, |
| 192 const AdblockPlus::SubscriptionPtr& subscription) | |
| 154 { | 193 { |
| 155 if (!subscription.get()) | 194 if (!subscription.get()) |
| 156 { | 195 { |
| 157 return 0; | 196 return 0; |
| 158 } | 197 } |
| 159 | 198 |
| 160 jclass clazz = env->FindClass(PKG("Subscription")); | 199 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Subscription"))); |
| 161 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); | 200 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V"); |
| 162 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription))); | 201 return env->NewObject(*clazz, method, |
| 202 JniPtrToLong(new AdblockPlus::SubscriptionPtr(subscription))); | |
| 163 } | 203 } |
| 164 | 204 |
| 165 #define CATCH_AND_THROW(jEnv) \ | 205 #define CATCH_AND_THROW(jEnv) \ |
| 166 catch (const std::exception& except) \ | 206 catch (const std::exception& except) \ |
| 167 { \ | 207 { \ |
| 168 JniThrowException(jEnv, except); \ | 208 JniThrowException(jEnv, except); \ |
| 169 } \ | 209 } \ |
| 170 catch (...) \ | 210 catch (...) \ |
| 171 { \ | 211 { \ |
| 172 JniThrowException(jEnv); \ | 212 JniThrowException(jEnv); \ |
| 173 } | 213 } |
| 174 | 214 |
| 175 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ | 215 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ |
| 176 catch (const std::exception& except) \ | 216 catch (const std::exception& except) \ |
| 177 { \ | 217 { \ |
| 178 JniThrowException(jEnv, except); \ | 218 JniThrowException(jEnv, except); \ |
| 179 return retVal; \ | 219 return retVal; \ |
| 180 } \ | 220 } \ |
| 181 catch (...) \ | 221 catch (...) \ |
| 182 { \ | 222 { \ |
| 183 JniThrowException(jEnv); \ | 223 JniThrowException(jEnv); \ |
| 184 return retVal; \ | 224 return retVal; \ |
| 185 } | 225 } |
| 186 | 226 |
| 187 #endif | 227 #endif |
| OLD | NEW |