Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android/jni/Utils.h

Issue 29678581: Issue 6000 - Rename "libadblockplus-android" (Closed)
Patch Set: Created Jan. 24, 2018, 6:53 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #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 JniUtils_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved);
35
36 void JniUtils_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved);
37
38 void JniThrowException(JNIEnv* env, const std::string& message);
39
40 void JniThrowException(JNIEnv* env, const std::exception& e);
41
42 void JniThrowException(JNIEnv* env);
43
44 class JNIEnvAcquire;
45
46 /**
47 * This class is _NOT_ thread safe!
48 */
49 template<typename T>
50 class JniLocalReference
51 {
52 public:
53 JniLocalReference(JNIEnv* jniEnv, T object)
54 : jniEnv(jniEnv), object(object)
55 {
56
57 }
58
59 JniLocalReference(const JniLocalReference<T>& other);
60
61 ~JniLocalReference()
62 {
63 jniEnv->DeleteLocalRef(object);
64 }
65
66 T operator*()
67 {
68 return object;
69 }
70
71 T Get()
72 {
73 return object;
74 }
75
76 private:
77 JNIEnv* jniEnv;
78 T object;
79 };
80
81 class JNIEnvAcquire
82 {
83 public:
84 JNIEnvAcquire(JavaVM* javaVM);
85 ~JNIEnvAcquire();
86
87 JNIEnv* operator*()
88 {
89 return jniEnv;
90 }
91
92 JNIEnv* operator->()
93 {
94 return jniEnv;
95 }
96
97 private:
98 JavaVM* javaVM;
99 JNIEnv* jniEnv;
100 int attachmentStatus;
101 };
102
103 template<typename T>
104 class JniGlobalReference
105 {
106 public:
107 JniGlobalReference(JNIEnv* env, T reference)
108 {
109 env->GetJavaVM(&javaVM);
110 this->reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(refe rence)));
111 }
112
113 ~JniGlobalReference()
114 {
115 JNIEnvAcquire env(javaVM);
116 env->DeleteGlobalRef(static_cast<jobject>(reference));
117 }
118
119 JniGlobalReference(const JniGlobalReference& other);
120 JniGlobalReference& operator=(const JniGlobalReference& other);
121
122 T Get()
123 {
124 return reference;
125 }
126
127 typedef std::shared_ptr<JniGlobalReference<T> > Ptr;
128
129 private:
130 T reference;
131 JavaVM* javaVM;
132 };
133
134 inline void* JniLongToPtr(jlong value)
135 {
136 return reinterpret_cast<void*>((size_t)value);
137 }
138
139 inline jlong JniPtrToLong(void* ptr)
140 {
141 return (jlong)reinterpret_cast<size_t>(ptr);
142 }
143
144 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so maybe
145 // implement a special cast functions that first reinterpret_casts to shared_ptr <JsValue> and then
146 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m irrored on the Java
147 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
148 template<typename T>
149 inline T* JniLongToTypePtr(jlong value)
150 {
151 return reinterpret_cast<T*>((size_t)value);
152 }
153
154 std::string JniJavaToStdString(JNIEnv* env, jstring str);
155 jstring JniStdStringToJava(JNIEnv* env, std::string str);
156 bool stringBeginsWith(const std::string& string, const std::string& beginning);
157
158 jmethodID JniGetAddToListMethod(JNIEnv* env, jobject list);
159
160 void JniAddObjectToList(JNIEnv* env, jobject list, jmethodID addMethod, jobject value);
161
162 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
163
164 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name)
165 {
166 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
167 }
168
169 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
170 {
171 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
172 }
173
174 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
175 {
176 return (int32_t)env->GetIntField(jObj, env->GetFieldID(clazz, name, "I"));
177 }
178
179 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
180 {
181 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J"));
182 }
183
184 inline jobjectArray JniGetStringArrayField(JNIEnv* env, jclass clazz, jobject jO bj, const char* name)
185 {
186 return (jobjectArray)env->GetObjectField(jObj, env->GetFieldID(clazz, name, "[ Ljava/lang/String;"));
187 }
188
189 jobject NewJniArrayList(JNIEnv* env);
190
191 jobject NewJniFilter(JNIEnv* env, AdblockPlus::Filter&& filter);
192
193 jobject NewJniSubscription(JNIEnv* env, AdblockPlus::Subscription&& subscription );
194
195 jobject NewJniNotification(JNIEnv* env, AdblockPlus::Notification&& notification );
196
197 #define CATCH_AND_THROW(jEnv) \
198 catch (const std::exception& except) \
199 { \
200 JniThrowException(jEnv, except); \
201 } \
202 catch (...) \
203 { \
204 JniThrowException(jEnv); \
205 }
206
207 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
208 catch (const std::exception& except) \
209 { \
210 JniThrowException(jEnv, except); \
211 return retVal; \
212 } \
213 catch (...) \
214 { \
215 JniThrowException(jEnv); \
216 return retVal; \
217 }
218
219 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld