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: Style review fixes Created March 28, 2014, 11 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
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 *
(...skipping 12 matching lines...) Expand all
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/android/api/" 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 35
36 namespace AdblockPlus
37 {
38 namespace Android
39 {
40
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) :
Felix Dahlke 2014/03/28 11:27:18 Colon on next line s'il vous plaît.
51 m_JavaVM(javaVM), m_JNIEnv(0), m_AttachStatus(0) 46 javaVM(javaVM), jniEnv(0), attachmentStatus(0)
52 { 47 {
53 m_AttachStatus = javaVM->GetEnv((void **)&m_JNIEnv, ABP_JNI_VERSION); 48 attachmentStatus = javaVM->GetEnv((void **)&jniEnv, ABP_JNI_VERSION);
Felix Dahlke 2014/03/28 11:27:18 Same as JniCallbacks.h, non-trivial non-template f
54 if (m_AttachStatus == JNI_EDETACHED) 49 if (attachmentStatus == JNI_EDETACHED)
55 { 50 {
56 if (javaVM->AttachCurrentThread(&m_JNIEnv, 0)) 51 if (javaVM->AttachCurrentThread(&jniEnv, 0))
57 { 52 {
58 // This one is FATAL, we can't recover from this (because without a JVM we're dead), so 53 // 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 54 // throwing a runtime_exception in a ctor can be tolerated here IMHO
60 throw std::runtime_error("Failed to get JNI environment"); 55 throw std::runtime_error("Failed to get JNI environment");
61 } 56 }
62 } 57 }
63 } 58 }
64 59
65 ~JNIEnvAcquire() 60 ~JNIEnvAcquire()
66 { 61 {
67 if (m_AttachStatus == JNI_EDETACHED) 62 if (attachmentStatus == JNI_EDETACHED)
68 { 63 {
69 m_JavaVM->DetachCurrentThread(); 64 javaVM->DetachCurrentThread();
70 } 65 }
71 } 66 }
72 67
73 inline JNIEnv* operator*() 68 inline JNIEnv* operator*()
74 { 69 {
75 return m_JNIEnv; 70 return jniEnv;
76 } 71 }
77 72
78 inline JNIEnv* operator->() 73 inline JNIEnv* operator->()
79 { 74 {
80 return m_JNIEnv; 75 return jniEnv;
81 } 76 }
82 77
83 private: 78 private:
84 JavaVM* m_JavaVM; 79 JavaVM* javaVM;
85 JNIEnv* m_JNIEnv; 80 JNIEnv* jniEnv;
86 int m_AttachStatus; 81 int attachmentStatus;
87 }; 82 };
88 83
89 template<typename T> 84 template<typename T>
90 class JniGlobalReference 85 class JniGlobalReference
91 { 86 {
92 public: 87 public:
93 JniGlobalReference(JNIEnv* env, T reference) 88 JniGlobalReference(JNIEnv* env, T reference)
94 { 89 {
95 env->GetJavaVM(&m_JavaVM); 90 env->GetJavaVM(&javaVM);
96 m_Reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(referenc e))); 91 reference = static_cast<T>(env->NewGlobalRef(static_cast<jobject>(reference) ));
97 } 92 }
98 93
99 ~JniGlobalReference() 94 ~JniGlobalReference()
100 { 95 {
101 JNIEnvAcquire env(m_JavaVM); 96 JNIEnvAcquire env(javaVM);
102 97
103 env->DeleteGlobalRef(static_cast<jobject>(m_Reference)); 98 env->DeleteGlobalRef(static_cast<jobject>(reference));
104 } 99 }
105 100
106 JniGlobalReference(const JniGlobalReference& other); 101 JniGlobalReference(const JniGlobalReference& other);
107 JniGlobalReference& operator=(const JniGlobalReference& other); 102 JniGlobalReference& operator=(const JniGlobalReference& other);
108 103
109 inline T get() 104 inline T get()
110 { 105 {
111 return m_Reference; 106 return reference;
112 } 107 }
113 108
114 typedef std::tr1::shared_ptr<JniGlobalReference<T> > PTR; 109 typedef std::tr1::shared_ptr<JniGlobalReference<T> > Ptr;
115 110
116 private: 111 private:
117 T m_Reference; 112 T reference;
118 JavaVM* m_JavaVM; 113 JavaVM* javaVM;
119 }; 114 };
120 115
121 inline void* JniLong2Ptr(jlong value) 116 inline void* JniLongToPtr(jlong value)
122 { 117 {
123 return reinterpret_cast<void*>((size_t)value); 118 return reinterpret_cast<void*>((size_t)value);
124 } 119 }
125 120
126 inline jlong JniPtr2Long(void* ptr) 121 inline jlong JniPtrToLong(void* ptr)
127 { 122 {
128 return (jlong)reinterpret_cast<size_t>(ptr); 123 return (jlong)reinterpret_cast<size_t>(ptr);
129 } 124 }
130 125
131 // TODO(rje): It's feeling a bit dirty casting to shared_ptr<T: JsValue> directl y, so maybe 126 // 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 127 // 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 128 // 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) 129 // side (and Java will throw a class cast exception on error) this shouldn't be an issue (TM)
135 template<typename T> 130 template<typename T>
136 inline T* JniLong2TypePtr(jlong value) 131 inline T* JniLongToTypePtr(jlong value)
137 { 132 {
138 return reinterpret_cast<T*>((size_t)value); 133 return reinterpret_cast<T*>((size_t)value);
139 } 134 }
140 135
141 jobject NewJniArrayList(JNIEnv* env); 136 jobject NewJniArrayList(JNIEnv* env);
142 137
143 std::string JniJava2StdString(JNIEnv* env, jstring str); 138 std::string JniJava2StdString(JNIEnv* env, jstring str);
144 139
145 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value); 140 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value);
146 141
147 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name) 142 inline std::string JniGetStringField(JNIEnv* env, jclass clazz, jobject jObj, co nst char* name)
148 { 143 {
149 return AdblockPlus::Android::JniJava2StdString(env, 144 return JniJava2StdString(env, reinterpret_cast<jstring>(env->GetObjectField(jO bj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
150 reinterpret_cast<jstring>(env->GetObjectField(jObj, env->GetFieldID(clazz, name, "Ljava/lang/String;"))));
151 } 145 }
152 146
153 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 147 inline bool JniGetBooleanField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
154 { 148 {
155 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE; 149 return env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "Z")) == JNI_TR UE;
156 } 150 }
157 151
158 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name) 152 inline int32_t JniGetIntField(JNIEnv* env, jclass clazz, jobject jObj, const cha r* name)
159 { 153 {
160 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I")); 154 return (int32_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "I"));
161 } 155 }
162 156
163 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name) 157 inline int64_t JniGetLongField(JNIEnv* env, jclass clazz, jobject jObj, const ch ar* name)
164 { 158 {
165 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J")); 159 return (int64_t)env->GetBooleanField(jObj, env->GetFieldID(clazz, name, "J"));
166 } 160 }
167 161
168 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) 162 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
169 { 163 {
170 jclass clazz = env->FindClass(PKG("Filter")); 164 jclass clazz = env->FindClass(PKG("Filter"));
171 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 165 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
172 return env->NewObject(clazz, method, JniPtr2Long(new AdblockPlus::FilterPtr(fi lter))); 166 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter)));
173 } 167 }
174 168
175 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 169 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
176 { 170 {
177 jclass clazz = env->FindClass(PKG("Subscription")); 171 jclass clazz = env->FindClass(PKG("Subscription"));
178 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 172 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V");
179 return env->NewObject(clazz, method, JniPtr2Long(new AdblockPlus::Subscription Ptr(subscription))); 173 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription)));
180 } 174 }
181 175
182 } // namespace Android
183 } // namespace AdblockPlus
184
185 #define TRY try 176 #define TRY try
186 177
187 #define CATCH_AND_THROW(jEnv) \ 178 #define CATCH_AND_THROW(jEnv) \
188 catch (const std::exception& except) \ 179 catch (const std::exception& except) \
189 { \ 180 { \
190 AdblockPlus::Android::JniThrowException(jEnv, except); \ 181 JniThrowException(jEnv, except); \
191 } \ 182 } \
192 catch (...) \ 183 catch (...) \
193 { \ 184 { \
194 AdblockPlus::Android::JniThrowException(jEnv); \ 185 JniThrowException(jEnv); \
195 } 186 }
196 187
197 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ 188 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
198 catch (const std::exception& except) \ 189 catch (const std::exception& except) \
199 { \ 190 { \
200 AdblockPlus::Android::JniThrowException(jEnv, except); \ 191 JniThrowException(jEnv, except); \
201 return retVal; \ 192 return retVal; \
202 } \ 193 } \
203 catch (...) \ 194 catch (...) \
204 { \ 195 { \
205 AdblockPlus::Android::JniThrowException(jEnv); \ 196 JniThrowException(jEnv); \
206 return retVal; \ 197 return retVal; \
207 } 198 }
208 199
209 const std::string GetString(JNIEnv *pEnv, jstring str); 200 const std::string GetString(JNIEnv *pEnv, jstring str);
210 201
211 template<class T> 202 template<class T>
212 T TrimString(T text) 203 T TrimString(T text)
213 { 204 {
214 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 205 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
215 T trimmed(text); 206 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)))); 207 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()); 208 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; 209 return trimmed;
219 } 210 }
220 211
221 #endif 212 #endif
OLDNEW
« jni/JniJsEngine.cpp ('K') | « jni/JniWebRequest.cpp ('k') | jni/Utils.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld