OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2013 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> |
| 25 |
| 26 #include <AdblockPlus.h> |
| 27 #include <AdblockPlus/tr1_memory.h> |
| 28 |
| 29 #include "v8/v8stdint.h" |
| 30 |
| 31 #define PKG(x) "org/adblockplus/android/api/" x |
| 32 #define TYP(x) "L" PKG(x) ";" |
| 33 |
| 34 #define ABP_JNI_VERSION JNI_VERSION_1_6 |
| 35 |
| 36 namespace AdblockPlus |
| 37 { |
| 38 namespace Android |
| 39 { |
| 40 |
| 41 void JniThrowException(JNIEnv* env, const std::string& message); |
| 42 |
| 43 void JniThrowException(JNIEnv* env, const std::exception& e); |
| 44 |
| 45 void JniThrowException(JNIEnv* env); |
| 46 |
| 47 class JNIEnvAcquire |
| 48 { |
| 49 public: |
| 50 JNIEnvAcquire(JavaVM* javaVM) : |
| 51 m_JavaVM(javaVM), m_JNIEnv(0), m_AttachStatus(0) |
| 52 { |
| 53 m_AttachStatus = javaVM->GetEnv((void **)&m_JNIEnv, ABP_JNI_VERSION); |
| 54 if (m_AttachStatus == JNI_EDETACHED) |
| 55 { |
| 56 if (javaVM->AttachCurrentThread(&m_JNIEnv, 0)) |
| 57 { |
| 58 // This one is FATAL, we can't recover from this (because without a JVM
we're dead), so |
| 59 // throwing a runtime_exception in a ctor can be tolerated here IMHO |
| 60 throw std::runtime_error("Failed to get JNI environment"); |
| 61 } |
| 62 } |
| 63 } |
| 64 |
| 65 ~JNIEnvAcquire() |
| 66 { |
| 67 if (m_AttachStatus == JNI_EDETACHED) |
| 68 { |
| 69 m_JavaVM->DetachCurrentThread(); |
| 70 } |
| 71 } |
| 72 |
| 73 inline JNIEnv* operator*() |
| 74 { |
| 75 return m_JNIEnv; |
| 76 } |
| 77 |
| 78 inline JNIEnv* operator->() |
| 79 { |
| 80 return m_JNIEnv; |
| 81 } |
| 82 |
| 83 private: |
| 84 JavaVM* m_JavaVM; |
| 85 JNIEnv* m_JNIEnv; |
| 86 int m_AttachStatus; |
| 87 }; |
| 88 |
| 89 template<typename T> |
| 90 class JniGlobalReference |
| 91 { |
| 92 public: |
| 93 JniGlobalReference(JNIEnv* env, T reference) |
| 94 { |
| 95 env->GetJavaVM(&m_JavaVM); |
| 96 m_Reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(referenc
e))); |
| 97 } |
| 98 |
| 99 ~JniGlobalReference() |
| 100 { |
| 101 JNIEnvAcquire env(m_JavaVM); |
| 102 |
| 103 env->DeleteGlobalRef(static_cast<jobject>(m_Reference)); |
| 104 } |
| 105 |
| 106 JniGlobalReference(const JniGlobalReference& other); |
| 107 JniGlobalReference& operator=(const JniGlobalReference& other); |
| 108 |
| 109 inline T get() |
| 110 { |
| 111 return m_Reference; |
| 112 } |
| 113 |
| 114 typedef std::tr1::shared_ptr<JniGlobalReference<T> > PTR; |
| 115 |
| 116 private: |
| 117 T m_Reference; |
| 118 JavaVM* m_JavaVM; |
| 119 }; |
| 120 |
| 121 inline void* JniLong2Ptr(jlong value) |
| 122 { |
| 123 return reinterpret_cast<void*>((size_t)value); |
| 124 } |
| 125 |
| 126 inline jlong JniPtr2Long(void* ptr) |
| 127 { |
| 128 return (jlong)reinterpret_cast<size_t>(ptr); |
| 129 } |
| 130 |
| 131 // TODO(rje): It's feeling a bit dirty casting to shared_ptr<T: JsValue> directl
y, so maybe |
| 132 // implement a special cast functions that first reinterpret_casts to shared_ptr
<JsValue> and then |
| 133 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m
irrored on the Java |
| 134 // side (and Java will throw a class cast exception on error) this shouldn't be
an issue (TM) |
| 135 template<typename T> |
| 136 inline T* JniLong2TypePtr(jlong value) |
| 137 { |
| 138 return reinterpret_cast<T*>((size_t)value); |
| 139 } |
| 140 |
| 141 jobject NewJniArrayList(JNIEnv* env); |
| 142 |
| 143 std::string JniJava2StdString(JNIEnv* env, jstring str); |
| 144 |
| 145 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value); |
| 146 |
| 147 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co
nst char* name) |
| 148 { |
| 149 return AdblockPlus::Android::JniJava2StdString(env, |
| 150 reinterpret_cast<jstring>(env->GetObjectField(jObj, env->GetFieldID(clazz,
name, "Ljava/lang/String;")))); |
| 151 } |
| 152 |
| 153 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch
ar* name) |
| 154 { |
| 155 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR
UE; |
| 156 } |
| 157 |
| 158 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha
r* name) |
| 159 { |
| 160 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I")); |
| 161 } |
| 162 |
| 163 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch
ar* name) |
| 164 { |
| 165 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J")); |
| 166 } |
| 167 |
| 168 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) |
| 169 { |
| 170 jclass clazz = env->FindClass(PKG("Filter")); |
| 171 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); |
| 172 return env->NewObject(clazz, method, JniPtr2Long(new AdblockPlus::FilterPtr(fi
lter))); |
| 173 } |
| 174 |
| 175 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt
r& subscription) |
| 176 { |
| 177 jclass clazz = env->FindClass(PKG("Subscription")); |
| 178 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); |
| 179 return env->NewObject(clazz, method, JniPtr2Long(new AdblockPlus::Subscription
Ptr(subscription))); |
| 180 } |
| 181 |
| 182 } // namespace Android |
| 183 } // namespace AdblockPlus |
| 184 |
| 185 #define TRY try |
| 186 |
| 187 #define CATCH_AND_THROW(jEnv) \ |
| 188 catch (const std::exception& except) \ |
| 189 { \ |
| 190 AdblockPlus::Android::JniThrowException(jEnv, except); \ |
| 191 } \ |
| 192 catch (...) \ |
| 193 { \ |
| 194 AdblockPlus::Android::JniThrowException(jEnv); \ |
| 195 } |
| 196 |
| 197 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ |
| 198 catch (const std::exception& except) \ |
| 199 { \ |
| 200 AdblockPlus::Android::JniThrowException(jEnv, except); \ |
| 201 return retVal; \ |
| 202 } \ |
| 203 catch (...) \ |
| 204 { \ |
| 205 AdblockPlus::Android::JniThrowException(jEnv); \ |
| 206 return retVal; \ |
| 207 } |
24 | 208 |
25 const std::string GetString(JNIEnv *pEnv, jstring str); | 209 const std::string GetString(JNIEnv *pEnv, jstring str); |
26 | 210 |
27 template<class T> | 211 template<class T> |
28 T TrimString(T text) | 212 T TrimString(T text) |
29 { | 213 { |
30 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st
dstring | 214 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st
dstring |
31 T trimmed(text); | 215 T trimmed(text); |
32 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st
d::not1(std::ptr_fun<int, int>(std::isspace)))); | 216 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st
d::not1(std::ptr_fun<int, int>(std::isspace)))); |
33 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt
r_fun<int, int>(std::isspace))).base(), trimmed.end()); | 217 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt
r_fun<int, int>(std::isspace))).base(), trimmed.end()); |
34 return trimmed; | 218 return trimmed; |
35 } | 219 } |
36 | 220 |
37 #endif | 221 #endif |
OLD | NEW |