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: TRY, abpEngine, statics Created April 1, 2014, 11:40 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
« no previous file with comments | « jni/JniWebRequest.cpp ('k') | jni/Utils.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/libadblockplus/" x
32 #define TYP(x) "L" PKG(x) ";"
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 this->reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(refe rence)));
72 }
73
74 ~JniGlobalReference()
75 {
76 JNIEnvAcquire env(javaVM);
77 env->DeleteGlobalRef(static_cast<jobject>(reference));
78 }
79
80 JniGlobalReference(const JniGlobalReference& other);
81 JniGlobalReference& operator=(const JniGlobalReference& other);
82
83 T Get()
84 {
85 return reference;
86 }
87
88 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
89
90 private:
91 T reference;
92 JavaVM* javaVM;
93 };
94
95 inline void* JniLongToPtr(jlong value)
96 {
97 return reinterpret_cast<void*>((size_t)value);
98 }
99
100 inline jlong JniPtrToLong(void* ptr)
101 {
102 return (jlong)reinterpret_cast<size_t>(ptr);
103 }
104
105 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so maybe
106 // implement a special cast functions that first reinterpret_casts to shared_ptr <JsValue> and then
107 // dynamic_casts to shared_ptr<T: JsValue> ... also as the same inheritance is m irrored on the Java
108 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
109 template<typename T>
110 inline T* JniLongToTypePtr(jlong value)
111 {
112 return reinterpret_cast<T*>((size_t)value);
113 }
114
115 jobject NewJniArrayList(JNIEnv* env);
116
117 std::string JniJavaToStdString(JNIEnv* env, jstring str);
118
119 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
120
121 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name)
122 {
123 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
124 }
125
126 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
127 {
128 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
129 }
130
131 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
132 {
133 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I"));
134 }
135
136 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
137 {
138 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J"));
139 }
140
141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
142 {
143 jclass clazz = env->FindClass(PKG("Filter"));
144 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
145 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
146 }
147
148 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
149 {
150 jclass clazz = env->FindClass(PKG("Subscription"));
151 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
152 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
153 }
154
155 #define CATCH_AND_THROW(jEnv) \
156 catch (const std::exception& except) \
157 { \
158 JniThrowException(jEnv, except); \
159 } \
160 catch (...) \
161 { \
162 JniThrowException(jEnv); \
163 }
164
165 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
166 catch (const std::exception& except) \
167 { \
168 JniThrowException(jEnv, except); \
169 return retVal; \
170 } \
171 catch (...) \
172 { \
173 JniThrowException(jEnv); \
174 return retVal; \
175 }
24 176
25 const std::string GetString(JNIEnv *pEnv, jstring str); 177 const std::string GetString(JNIEnv *pEnv, jstring str);
26 178
27 template<class T> 179 template<class T>
28 T TrimString(T text) 180 T TrimString(T text)
29 { 181 {
30 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 182 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
31 T trimmed(text); 183 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)))); 184 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()); 185 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; 186 return trimmed;
35 } 187 }
36 188
37 #endif 189 #endif
OLDNEW
« no previous file with comments | « jni/JniWebRequest.cpp ('k') | jni/Utils.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld