OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2016 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 #ifndef UTILS_H | |
19 #define UTILS_H | |
20 | |
21 #include <string> | |
22 #include <algorithm> | |
23 #include <jni.h> | |
24 #include <stdexcept> | |
25 #include <memory> | |
26 | |
27 #include <AdblockPlus.h> | |
28 | |
29 #define PKG(x) "org/adblockplus/libadblockplus/" x | |
30 #define TYP(x) "L" PKG(x) ";" | |
31 | |
32 #define ABP_JNI_VERSION JNI_VERSION_1_6 | |
33 | |
34 void JniThrowException(JNIEnv* env, const std::string& message); | |
35 | |
36 void JniThrowException(JNIEnv* env, const std::exception& e); | |
37 | |
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 }; | |
76 | |
77 class JNIEnvAcquire | |
78 { | |
79 public: | |
80 JNIEnvAcquire(JavaVM* javaVM); | |
81 ~JNIEnvAcquire(); | |
82 | |
83 JNIEnv* operator*() | |
84 { | |
85 return jniEnv; | |
86 } | |
87 | |
88 JNIEnv* operator->() | |
89 { | |
90 return jniEnv; | |
91 } | |
92 | |
93 private: | |
94 JavaVM* javaVM; | |
95 JNIEnv* jniEnv; | |
96 int attachmentStatus; | |
97 }; | |
98 | |
99 template<typename T> | |
100 class JniGlobalReference | |
101 { | |
102 public: | |
103 JniGlobalReference(JNIEnv* env, T reference) | |
104 { | |
105 env->GetJavaVM(&javaVM); | |
106 this->reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(refe
rence))); | |
107 } | |
108 | |
109 ~JniGlobalReference() | |
110 { | |
111 JNIEnvAcquire env(javaVM); | |
112 env->DeleteGlobalRef(static_cast<jobject>(reference)); | |
113 } | |
114 | |
115 JniGlobalReference(const JniGlobalReference& other); | |
116 JniGlobalReference& operator=(const JniGlobalReference& other); | |
117 | |
118 T Get() | |
119 { | |
120 return reference; | |
121 } | |
122 | |
123 typedef std::shared_ptr<JniGlobalReference<T> > Ptr; | |
124 | |
125 private: | |
126 T reference; | |
127 JavaVM* javaVM; | |
128 }; | |
129 | |
130 inline void* JniLongToPtr(jlong value) | |
131 { | |
132 return reinterpret_cast<void*>((size_t)value); | |
133 } | |
134 | |
135 inline jlong JniPtrToLong(void* ptr) | |
136 { | |
137 return (jlong)reinterpret_cast<size_t>(ptr); | |
138 } | |
139 | |
140 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so
maybe | |
141 // implement a special cast functions that first reinterpret_casts to shared_ptr
<JsValue> and then | |
142 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m
irrored on the Java | |
143 // side (and Java will throw a class cast exception on error) this shouldn't be
an issue (TM) | |
144 template<typename T> | |
145 inline T* JniLongToTypePtr(jlong value) | |
146 { | |
147 return reinterpret_cast<T*>((size_t)value); | |
148 } | |
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 | |
174 jobject NewJniArrayList(JNIEnv* env); | |
175 | |
176 jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter); | |
177 | |
178 jobject NewJniSubscription(JNIEnv* env, | |
179 const AdblockPlus::SubscriptionPtr& subscription); | |
180 | |
181 jobject NewJniNotification(JNIEnv* env, | |
182 const AdblockPlus::NotificationPtr& notification); | |
183 | |
184 #define CATCH_AND_THROW(jEnv) \ | |
185 catch (const std::exception& except) \ | |
186 { \ | |
187 JniThrowException(jEnv, except); \ | |
188 } \ | |
189 catch (...) \ | |
190 { \ | |
191 JniThrowException(jEnv); \ | |
192 } | |
193 | |
194 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ | |
195 catch (const std::exception& except) \ | |
196 { \ | |
197 JniThrowException(jEnv, except); \ | |
198 return retVal; \ | |
199 } \ | |
200 catch (...) \ | |
201 { \ | |
202 JniThrowException(jEnv); \ | |
203 return retVal; \ | |
204 } | |
205 | |
206 #endif | |
OLD | NEW |