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

Powered by Google App Engine
This is Rietveld