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

Delta Between Two Patch Sets: jni/Utils.h

Issue 4761138508070912: Issue 1848 - Clean up local reference handling (Closed)
Left Patch Set: Created Jan. 22, 2015, 6:53 p.m.
Right Patch Set: Stupid auto-formatter^^ Created Feb. 4, 2015, 12:04 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 <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 <vector>
23 #include <algorithm> 22 #include <algorithm>
24 #include <jni.h> 23 #include <jni.h>
25 #include <stdexcept> 24 #include <stdexcept>
26 25
27 #include <AdblockPlus.h> 26 #include <AdblockPlus.h>
28 #include <AdblockPlus/tr1_memory.h> 27 #include <AdblockPlus/tr1_memory.h>
29
30 #include "v8/v8stdint.h"
31 28
32 #define PKG(x) "org/adblockplus/libadblockplus/" x 29 #define PKG(x) "org/adblockplus/libadblockplus/" x
33 #define TYP(x) "L" PKG(x) ";" 30 #define TYP(x) "L" PKG(x) ";"
34 31
35 #define ABP_JNI_VERSION JNI_VERSION_1_6 32 #define ABP_JNI_VERSION JNI_VERSION_1_6
36 33
37 void JniThrowException(JNIEnv* env, const std::string& message); 34 void JniThrowException(JNIEnv* env, const std::string& message);
38 35
39 void JniThrowException(JNIEnv* env, const std::exception& e); 36 void JniThrowException(JNIEnv* env, const std::exception& e);
40 37
41 void JniThrowException(JNIEnv* env); 38 void JniThrowException(JNIEnv* env);
42 39
43 class JNIEnvAcquire; 40 class JNIEnvAcquire;
44 41
45 /** 42 /**
46 * This class is _NOT_ thread safe! 43 * This class is _NOT_ thread safe!
47 */ 44 */
48 template<typename T> 45 template<typename T>
49 class JniLocalReference 46 class JniLocalReference
50 { 47 {
51 public: 48 public:
52 explicit JniLocalReference(JNIEnv* jniEnv, T object) 49 JniLocalReference(JNIEnv* jniEnv, T object)
53 : m_JNIEnv(jniEnv), m_Object(object) 50 : jniEnv(jniEnv), object(object)
54 { 51 {
55 52
56 } 53 }
57 54
58 JniLocalReference(const JniLocalReference<T>& other); 55 JniLocalReference(const JniLocalReference<T>& other);
59 56
60 ~JniLocalReference() 57 ~JniLocalReference()
61 { 58 {
62 m_JNIEnv->DeleteLocalRef(m_Object); 59 jniEnv->DeleteLocalRef(object);
63 } 60 }
64 61
65 T operator*() 62 T operator*()
66 { 63 {
67 return m_Object; 64 return object;
68 } 65 }
69 66
70 T Get() 67 T Get()
71 { 68 {
72 return m_Object; 69 return object;
73 } 70 }
74 71
75 private: 72 private:
76 JNIEnv* m_JNIEnv; 73 JNIEnv* jniEnv;
77 T m_Object; 74 T object;
78 }; 75 };
79 76
80 class JNIEnvAcquire 77 class JNIEnvAcquire
81 { 78 {
82 public: 79 public:
83 JNIEnvAcquire(JavaVM* javaVM); 80 JNIEnvAcquire(JavaVM* javaVM);
84 ~JNIEnvAcquire(); 81 ~JNIEnvAcquire();
85 82
86 JNIEnv* operator*() 83 JNIEnv* operator*()
87 { 84 {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 175
179 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) 176 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
180 { 177 {
181 if (!filter.get()) 178 if (!filter.get())
182 { 179 {
183 return 0; 180 return 0;
184 } 181 }
185 182
186 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Filter"))); 183 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Filter")));
187 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V"); 184 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");
188 return env->NewObject(*clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr( filter))); 185 return env->NewObject(*clazz, method,
189 } 186 JniPtrToLong(new AdblockPlus::FilterPtr(filter)));
190 187 }
191 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 188
189 inline jobject NewJniSubscription(JNIEnv* env,
190 const AdblockPlus::SubscriptionPtr& subscription)
192 { 191 {
193 if (!subscription.get()) 192 if (!subscription.get())
194 { 193 {
195 return 0; 194 return 0;
196 } 195 }
197 196
198 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Subscription"))); 197 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Subscription")));
199 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V"); 198 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");
200 return env->NewObject(*clazz, method, JniPtrToLong(new AdblockPlus::Subscripti onPtr(subscription))); 199 return env->NewObject(*clazz, method,
200 JniPtrToLong(new AdblockPlus::SubscriptionPtr(subscription)));
201 } 201 }
202 202
203 #define CATCH_AND_THROW(jEnv) \ 203 #define CATCH_AND_THROW(jEnv) \
204 catch (const std::exception& except) \ 204 catch (const std::exception& except) \
205 { \ 205 { \
206 JniThrowException(jEnv, except); \ 206 JniThrowException(jEnv, except); \
207 } \ 207 } \
208 catch (...) \ 208 catch (...) \
209 { \ 209 { \
210 JniThrowException(jEnv); \ 210 JniThrowException(jEnv); \
211 } 211 }
212 212
213 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ 213 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
214 catch (const std::exception& except) \ 214 catch (const std::exception& except) \
215 { \ 215 { \
216 JniThrowException(jEnv, except); \ 216 JniThrowException(jEnv, except); \
217 return retVal; \ 217 return retVal; \
218 } \ 218 } \
219 catch (...) \ 219 catch (...) \
220 { \ 220 { \
221 JniThrowException(jEnv); \ 221 JniThrowException(jEnv); \
222 return retVal; \ 222 return retVal; \
223 } 223 }
224 224
225 #endif 225 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld