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

Delta Between Two Patch Sets: jni/Utils.h

Issue 6606493159784448: New JNI bindings (Closed)
Left Patch Set: Added missing copyright header Created March 14, 2014, 11:37 a.m.
Right Patch Set: Removed TODO from AppInfo. Created April 11, 2014, 1:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « jni/JniWebRequest.cpp ('k') | jni/Utils.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2013 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> 24 #include <stdexcept>
25 25
26 #include <AdblockPlus.h> 26 #include <AdblockPlus.h>
27 #include <AdblockPlus/tr1_memory.h> 27 #include <AdblockPlus/tr1_memory.h>
28 28
29 #include "v8/v8stdint.h" 29 #include "v8/v8stdint.h"
30 30
31 #define PKG(x) "org/adblockplus/android/api/" x 31 #define PKG(x) "org/adblockplus/libadblockplus/" x
32 #define TYP(x) "L" PKG(x) ";" 32 #define TYP(x) "L" PKG(x) ";"
33 33
34 #define ABP_JNI_VERSION JNI_VERSION_1_6 34 #define ABP_JNI_VERSION JNI_VERSION_1_6
35
36 namespace AdblockPlus
37 {
38 namespace Android
39 {
40 35
41 void JniThrowException(JNIEnv* env, const std::string& message); 36 void JniThrowException(JNIEnv* env, const std::string& message);
42 37
43 void JniThrowException(JNIEnv* env, const std::exception& e); 38 void JniThrowException(JNIEnv* env, const std::exception& e);
44 39
45 void JniThrowException(JNIEnv* env); 40 void JniThrowException(JNIEnv* env);
46 41
47 class JNIEnvAcquire 42 class JNIEnvAcquire
48 { 43 {
49 public: 44 public:
50 JNIEnvAcquire(JavaVM* javaVM) : 45 JNIEnvAcquire(JavaVM* javaVM);
51 m_JavaVM(javaVM), m_JNIEnv(0), m_AttachStatus(0) 46 ~JNIEnvAcquire();
47
48 JNIEnv* operator*()
52 { 49 {
53 m_AttachStatus = javaVM->GetEnv((void **)&m_JNIEnv, ABP_JNI_VERSION); 50 return jniEnv;
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 } 51 }
64 52
65 ~JNIEnvAcquire() 53 JNIEnv* operator->()
66 { 54 {
67 if (m_AttachStatus == JNI_EDETACHED) 55 return jniEnv;
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 } 56 }
82 57
83 private: 58 private:
84 JavaVM* m_JavaVM; 59 JavaVM* javaVM;
85 JNIEnv* m_JNIEnv; 60 JNIEnv* jniEnv;
86 int m_AttachStatus; 61 int attachmentStatus;
87 }; 62 };
88 63
89 template<typename T> 64 template<typename T>
90 class JniGlobalReference 65 class JniGlobalReference
91 { 66 {
92 public: 67 public:
93 JniGlobalReference(JNIEnv* env, T reference) 68 JniGlobalReference(JNIEnv* env, T reference)
94 { 69 {
95 env->GetJavaVM(&m_JavaVM); 70 env->GetJavaVM(&javaVM);
96 m_Reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(referenc e))); 71 this->reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(refe rence)));
97 } 72 }
98 73
99 ~JniGlobalReference() 74 ~JniGlobalReference()
100 { 75 {
101 JNIEnvAcquire env(m_JavaVM); 76 JNIEnvAcquire env(javaVM);
102 77 env->DeleteGlobalRef(static_cast<jobject>(reference));
103 env->DeleteGlobalRef(static_cast<jobject>(m_Reference));
104 } 78 }
105 79
106 JniGlobalReference(const JniGlobalReference& other); 80 JniGlobalReference(const JniGlobalReference& other);
107 JniGlobalReference& operator=(const JniGlobalReference& other); 81 JniGlobalReference& operator=(const JniGlobalReference& other);
108 82
109 inline T get() 83 T Get()
110 { 84 {
111 return m_Reference; 85 return reference;
112 } 86 }
113 87
114 typedef std::tr1::shared_ptr<JniGlobalReference<T> > PTR; 88 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
115 89
116 private: 90 private:
117 T m_Reference; 91 T reference;
118 JavaVM* m_JavaVM; 92 JavaVM* javaVM;
119 }; 93 };
120 94
121 inline void* JniLong2Ptr(jlong value) 95 inline void* JniLongToPtr(jlong value)
122 { 96 {
123 return reinterpret_cast<void*>((size_t)value); 97 return reinterpret_cast<void*>((size_t)value);
124 } 98 }
125 99
126 inline jlong JniPtr2Long(void* ptr) 100 inline jlong JniPtrToLong(void* ptr)
127 { 101 {
128 return (jlong)reinterpret_cast<size_t>(ptr); 102 return (jlong)reinterpret_cast<size_t>(ptr);
129 } 103 }
130 104
131 // TODO(rje): It's feeling a bit dirty casting to shared_ptr<T: JsValue> directl y, so maybe 105 // TODO: It's feeling a bit dirty casting to shared_ptr<T: JsValue> directly, so maybe
132 // implement a special cast functions that first reinterpret_casts to shared_ptr <JsValue> and then 106 // 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 107 // 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) 108 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
135 template<typename T> 109 template<typename T>
136 inline T* JniLong2TypePtr(jlong value) 110 inline T* JniLongToTypePtr(jlong value)
137 { 111 {
138 return reinterpret_cast<T*>((size_t)value); 112 return reinterpret_cast<T*>((size_t)value);
139 } 113 }
140 114
141 jobject NewJniArrayList(JNIEnv* env); 115 jobject NewJniArrayList(JNIEnv* env);
142 116
143 std::string JniJava2StdString(JNIEnv* env, jstring str); 117 std::string JniJavaToStdString(JNIEnv* env, jstring str);
144 118
145 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value); 119 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
146 120
147 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name) 121 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name)
148 { 122 {
149 return AdblockPlus::Android::JniJava2StdString(env, 123 return JniJavaToStdString(env, reinterpret_cast<jstring>(env->GetObjectField(j Obj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
150 reinterpret_cast<jstring>(env->GetObjectField(jObj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
151 } 124 }
152 125
153 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 126 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
154 { 127 {
155 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE; 128 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
156 } 129 }
157 130
158 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name) 131 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
159 { 132 {
160 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I")); 133 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I"));
161 } 134 }
162 135
163 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 136 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
164 { 137 {
165 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J")); 138 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J"));
166 } 139 }
167 140
168 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) 141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
169 { 142 {
170 jclass clazz = env->FindClass(PKG("Filter")); 143 jclass clazz = env->FindClass(PKG("Filter"));
171 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 144 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
172 return env->NewObject(clazz, method, JniPtr2Long(new AdblockPlus::FilterPtr(fi lter))); 145 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
173 } 146 }
174 147
175 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 148 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
176 { 149 {
177 jclass clazz = env->FindClass(PKG("Subscription")); 150 jclass clazz = env->FindClass(PKG("Subscription"));
178 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 151 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
179 return env->NewObject(clazz, method, JniPtr2Long(new AdblockPlus::Subscription Ptr(subscription))); 152 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
180 } 153 }
181
182 } // namespace Android
183 } // namespace AdblockPlus
184
185 #define TRY try
186 154
187 #define CATCH_AND_THROW(jEnv) \ 155 #define CATCH_AND_THROW(jEnv) \
188 catch (const std::exception& except) \ 156 catch (const std::exception& except) \
189 { \ 157 { \
190 AdblockPlus::Android::JniThrowException(jEnv, except); \ 158 JniThrowException(jEnv, except); \
191 } \ 159 } \
192 catch (...) \ 160 catch (...) \
193 { \ 161 { \
194 AdblockPlus::Android::JniThrowException(jEnv); \ 162 JniThrowException(jEnv); \
195 } 163 }
196 164
197 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ 165 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
198 catch (const std::exception& except) \ 166 catch (const std::exception& except) \
199 { \ 167 { \
200 AdblockPlus::Android::JniThrowException(jEnv, except); \ 168 JniThrowException(jEnv, except); \
201 return retVal; \ 169 return retVal; \
202 } \ 170 } \
203 catch (...) \ 171 catch (...) \
204 { \ 172 { \
205 AdblockPlus::Android::JniThrowException(jEnv); \ 173 JniThrowException(jEnv); \
206 return retVal; \ 174 return retVal; \
207 } 175 }
208 176
209 const std::string GetString(JNIEnv *pEnv, jstring str); 177 const std::string GetString(JNIEnv *pEnv, jstring str);
210 178
211 template<class T> 179 template<class T>
212 T TrimString(T text) 180 T TrimString(T text)
213 { 181 {
214 // 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
215 T trimmed(text); 183 T trimmed(text);
216 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))));
217 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());
218 return trimmed; 186 return trimmed;
219 } 187 }
220 188
221 #endif 189 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld