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

Side by Side Diff: jni/Utils.h

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Reuploaded full diff Created March 28, 2014, 3:56 p.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
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-2014 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
Felix Dahlke 2014/03/28 17:28:41 PKG is fine, but I'd personally vote for "PACKAGE"
René Jeschke 2014/03/31 09:10:38 I prefer PKG because it's only 3 chars. As there a
Felix Dahlke 2014/03/31 10:43:23 OK, fine with having no prefix. Also fine with PKG
René Jeschke 2014/04/11 12:25:53 Done.
32 #define TYP(x) "L" PKG(x) ";"
Felix Dahlke 2014/03/28 17:28:41 Correct me if I'm wrong, but it seems the proper n
René Jeschke 2014/03/31 09:10:38 That's not quite correct. The name of the class co
Felix Dahlke 2014/03/31 10:43:23 Na it's not, we should both be able to understand
René Jeschke 2014/04/11 12:25:53 Done.
33
34 #define ABP_JNI_VERSION JNI_VERSION_1_6
35
36 void JniThrowException(JNIEnv* env, const std::string& message);
37
38 void JniThrowException(JNIEnv* env, const std::exception& e);
39
40 void JniThrowException(JNIEnv* env);
41
42 class JNIEnvAcquire
43 {
44 public:
45 JNIEnvAcquire(JavaVM* javaVM);
46 ~JNIEnvAcquire();
47
48 JNIEnv* operator*()
49 {
50 return jniEnv;
51 }
52
53 JNIEnv* operator->()
54 {
55 return jniEnv;
56 }
57
58 private:
59 JavaVM* javaVM;
60 JNIEnv* jniEnv;
61 int attachmentStatus;
62 };
63
64 template<typename T>
65 class JniGlobalReference
66 {
67 public:
68 JniGlobalReference(JNIEnv* env, T reference)
69 {
70 env->GetJavaVM(&javaVM);
71 reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(reference) ));
72 }
73
74 ~JniGlobalReference()
75 {
76 JNIEnvAcquire env(javaVM);
77
78 env->DeleteGlobalRef(static_cast<jobject>(reference));
79 }
80
81 JniGlobalReference(const JniGlobalReference& other);
82 JniGlobalReference& operator=(const JniGlobalReference& other);
83
84 T get()
Felix Dahlke 2014/03/28 17:28:41 All functions should start with upper case letters
René Jeschke 2014/04/11 12:25:53 Done.
85 {
86 return reference;
87 }
88
89 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
90
91 private:
92 T reference;
93 JavaVM* javaVM;
94 };
95
96 inline void* JniLongToPtr(jlong value)
97 {
98 return reinterpret_cast<void*>((size_t)value);
99 }
100
101 inline jlong JniPtrToLong(void* ptr)
102 {
103 return (jlong)reinterpret_cast<size_t>(ptr);
104 }
105
106 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so maybe
107 // implement a special cast functions that first reinterpret_casts to shared_ptr <JsValue> and then
108 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m irrored on the Java
109 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
110 template<typename T>
111 inline T* JniLongToTypePtr(jlong value)
112 {
113 return reinterpret_cast<T*>((size_t)value);
114 }
115
116 jobject NewJniArrayList(JNIEnv* env);
117
118 std::string JniJavaToStdString(JNIEnv* env, jstring str);
119
120 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
121
122 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name)
123 {
124 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
125 }
126
127 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
128 {
129 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
130 }
131
132 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
133 {
134 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I"));
135 }
136
137 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
138 {
139 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J"));
140 }
141
142 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
143 {
144 jclass clazz = env->FindClass(PKG("Filter"));
145 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
146 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
147 }
148
149 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
150 {
151 jclass clazz = env->FindClass(PKG("Subscription"));
152 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
153 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
154 }
155
156 #define TRY try
Felix Dahlke 2014/03/28 17:28:41 I believe we can live without this one :D
René Jeschke 2014/03/31 09:10:38 It is just for symmetry reasons as: try { // ...
Felix Dahlke 2014/03/31 10:43:23 Using "TRY" suggests that some magic is going on h
René Jeschke 2014/04/11 12:25:53 Done.
157
158 #define CATCH_AND_THROW(jEnv) \
Felix Dahlke 2014/03/28 17:28:41 How about "CATCH_AND_RETHROW"?
René Jeschke 2014/03/31 09:10:38 Rethrow would imply that I would catch the C++ exc
Felix Dahlke 2014/03/31 10:43:23 Fair enough.
René Jeschke 2014/04/11 12:25:53 Done.
159 catch (const std::exception& except) \
160 { \
161 JniThrowException(jEnv, except); \
162 } \
163 catch (...) \
164 { \
165 JniThrowException(jEnv); \
166 }
167
168 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
169 catch (const std::exception& except) \
170 { \
171 JniThrowException(jEnv, except); \
172 return retVal; \
173 } \
174 catch (...) \
175 { \
176 JniThrowException(jEnv); \
177 return retVal; \
178 }
24 179
25 const std::string GetString(JNIEnv *pEnv, jstring str); 180 const std::string GetString(JNIEnv *pEnv, jstring str);
26 181
27 template<class T> 182 template<class T>
28 T TrimString(T text) 183 T TrimString(T text)
29 { 184 {
30 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 185 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
31 T trimmed(text); 186 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)))); 187 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()); 188 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; 189 return trimmed;
35 } 190 }
36 191
37 #endif 192 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld