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

Side by Side Diff: jni/Utils.h

Issue 4761138508070912: Issue 1848 - Clean up local reference handling (Closed)
Patch Set: Created Jan. 22, 2015, 6:53 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
« 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 <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>
22 #include <algorithm> 23 #include <algorithm>
23 #include <jni.h> 24 #include <jni.h>
24 #include <stdexcept> 25 #include <stdexcept>
25 26
26 #include <AdblockPlus.h> 27 #include <AdblockPlus.h>
27 #include <AdblockPlus/tr1_memory.h> 28 #include <AdblockPlus/tr1_memory.h>
28 29
29 #include "v8/v8stdint.h" 30 #include "v8/v8stdint.h"
30 31
31 #define PKG(x) "org/adblockplus/libadblockplus/" x 32 #define PKG(x) "org/adblockplus/libadblockplus/" x
32 #define TYP(x) "L" PKG(x) ";" 33 #define TYP(x) "L" PKG(x) ";"
33 34
34 #define ABP_JNI_VERSION JNI_VERSION_1_6 35 #define ABP_JNI_VERSION JNI_VERSION_1_6
35 36
36 void JniThrowException(JNIEnv* env, const std::string& message); 37 void JniThrowException(JNIEnv* env, const std::string& message);
37 38
38 void JniThrowException(JNIEnv* env, const std::exception& e); 39 void JniThrowException(JNIEnv* env, const std::exception& e);
39 40
40 void JniThrowException(JNIEnv* env); 41 void JniThrowException(JNIEnv* env);
41 42
43 class JNIEnvAcquire;
44
45 /**
46 * This class is _NOT_ thread safe!
47 */
48 template<typename T>
49 class JniLocalReference
50 {
51 public:
52 explicit JniLocalReference(JNIEnv* jniEnv, T object)
53 : m_JNIEnv(jniEnv), m_Object(object)
54 {
55
56 }
57
58 JniLocalReference(const JniLocalReference<T>& other);
59
60 ~JniLocalReference()
61 {
62 m_JNIEnv->DeleteLocalRef(m_Object);
63 }
64
65 T operator*()
66 {
67 return m_Object;
68 }
69
70 T Get()
71 {
72 return m_Object;
73 }
74
75 private:
76 JNIEnv* m_JNIEnv;
77 T m_Object;
78 };
79
42 class JNIEnvAcquire 80 class JNIEnvAcquire
43 { 81 {
44 public: 82 public:
45 JNIEnvAcquire(JavaVM* javaVM); 83 JNIEnvAcquire(JavaVM* javaVM);
46 ~JNIEnvAcquire(); 84 ~JNIEnvAcquire();
47 85
48 JNIEnv* operator*() 86 JNIEnv* operator*()
49 { 87 {
50 return jniEnv; 88 return jniEnv;
51 } 89 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J")); 176 return (int64_t)env->GetLongField(jObj, env->GetFieldID(clazz, name, "J"));
139 } 177 }
140 178
141 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) 179 inline jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
142 { 180 {
143 if (!filter.get()) 181 if (!filter.get())
144 { 182 {
145 return 0; 183 return 0;
146 } 184 }
147 185
148 jclass clazz = env->FindClass(PKG("Filter")); 186 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Filter")));
149 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 187 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");
150 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr(f ilter))); 188 return env->NewObject(*clazz, method, JniPtrToLong(new AdblockPlus::FilterPtr( filter)));
151 } 189 }
152 190
153 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription) 191 inline jobject NewJniSubscription(JNIEnv* env, const AdblockPlus::SubscriptionPt r& subscription)
154 { 192 {
155 if (!subscription.get()) 193 if (!subscription.get())
156 { 194 {
157 return 0; 195 return 0;
158 } 196 }
159 197
160 jclass clazz = env->FindClass(PKG("Subscription")); 198 JniLocalReference<jclass> clazz(env, env->FindClass(PKG("Subscription")));
161 jmethodID method = env->GetMethodID(clazz, "<init>", "(J)V"); 199 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");
162 return env->NewObject(clazz, method, JniPtrToLong(new AdblockPlus::Subscriptio nPtr(subscription))); 200 return env->NewObject(*clazz, method, JniPtrToLong(new AdblockPlus::Subscripti onPtr(subscription)));
163 } 201 }
164 202
165 #define CATCH_AND_THROW(jEnv) \ 203 #define CATCH_AND_THROW(jEnv) \
166 catch (const std::exception& except) \ 204 catch (const std::exception& except) \
167 { \ 205 { \
168 JniThrowException(jEnv, except); \ 206 JniThrowException(jEnv, except); \
169 } \ 207 } \
170 catch (...) \ 208 catch (...) \
171 { \ 209 { \
172 JniThrowException(jEnv); \ 210 JniThrowException(jEnv); \
173 } 211 }
174 212
175 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \ 213 #define CATCH_THROW_AND_RETURN(jEnv, retVal) \
176 catch (const std::exception& except) \ 214 catch (const std::exception& except) \
177 { \ 215 { \
178 JniThrowException(jEnv, except); \ 216 JniThrowException(jEnv, except); \
179 return retVal; \ 217 return retVal; \
180 } \ 218 } \
181 catch (...) \ 219 catch (...) \
182 { \ 220 { \
183 JniThrowException(jEnv); \ 221 JniThrowException(jEnv); \
184 return retVal; \ 222 return retVal; \
185 } 223 }
186 224
187 #endif 225 #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