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

Delta Between Two Patch Sets: jni/JniCallbacks.h

Issue 6606493159784448: New JNI bindings (Closed)
Left Patch Set: Created March 14, 2014, 11:32 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/Android.mk ('k') | jni/JniCallbacks.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-2014 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 JNICALLBACKS_H_ 18 #ifndef JNICALLBACKS_H
19 #define JNICALLBACKS_H_ 19 #define JNICALLBACKS_H
20 20
21 #include <AdblockPlus.h> 21 #include <AdblockPlus.h>
22 #include "Utils.h" 22 #include "Utils.h"
23 #include "JniJsValue.h" 23 #include "JniJsValue.h"
24 24
25 namespace AdblockPlus
26 {
27 namespace Android
28 {
29
30 class JniCallbackBase 25 class JniCallbackBase
31 { 26 {
32 public: 27 public:
33 JniCallbackBase(JNIEnv* env, jobject callbackObject) 28 JniCallbackBase(JNIEnv* env, jobject callbackObject);
29 virtual ~JniCallbackBase();
30 void LogException(JNIEnv* env, jthrowable throwable) const;
31 void CheckAndLogJavaException(JNIEnv* env) const;
32
33 JavaVM* GetJavaVM() const
34 { 34 {
35 env->GetJavaVM(&m_JavaVM); 35 return javaVM;
36 m_CallbackObject.reset(new JniGlobalReference<jobject>(env, callbackObject)) ;
37 m_ExceptionLoggerClass.reset(new JniGlobalReference<jclass>(env, env->FindCl ass(PKG("JniExceptionHandler"))));
38 } 36 }
39 37
40 virtual ~JniCallbackBase() 38 jobject GetCallbackObject() const
41 { 39 {
42 } 40 return callbackObject->Get();
43
44 inline JavaVM* GetJavaVM() const
45 {
46 return m_JavaVM;
47 }
48
49 inline jobject GetCallbackObject() const
50 {
51 return m_CallbackObject->get();
52 }
53
54 void LogException(JNIEnv* env, jthrowable throwable) const
55 {
56 jmethodID logMethod = env->GetStaticMethodID(m_ExceptionLoggerClass->get(), "logException", "(Ljava/lang/Throwable;)V");
57 if (logMethod)
58 {
59 env->CallStaticVoidMethod(m_ExceptionLoggerClass->get(), logMethod, throwa ble);
60 }
61 }
62
63 void CheckAndLogJavaException(JNIEnv* env) const
64 {
65 if (env->ExceptionCheck())
66 {
67 jthrowable throwable = env->ExceptionOccurred();
68 env->ExceptionClear();
69 LogException(env, throwable);
70 }
71 } 41 }
72 42
73 private: 43 private:
74 JavaVM* m_JavaVM; 44 JavaVM* javaVM;
75 JniGlobalReference<jobject>::PTR m_CallbackObject; 45 JniGlobalReference<jobject>::Ptr callbackObject;
76 JniGlobalReference<jclass>::PTR m_ExceptionLoggerClass; 46 JniGlobalReference<jclass>::Ptr exceptionLoggerClass;
77 }; 47 };
78 48
79 class JniEventCallback: public JniCallbackBase 49 class JniEventCallback : public JniCallbackBase
80 { 50 {
81 public: 51 public:
82 JniEventCallback(JNIEnv* env, jobject callbackObject) : 52 JniEventCallback(JNIEnv* env, jobject callbackObject);
83 JniCallbackBase(env, callbackObject) 53 void Callback(AdblockPlus::JsValueList& params);
84 {
85 }
86
87 virtual ~JniEventCallback()
88 {
89 }
90
91 void Callback(AdblockPlus::JsValueList& params)
92 {
93 AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
94
95 jclass clazz = env->GetObjectClass(GetCallbackObject());
96 jmethodID method = env->GetMethodID(clazz, "eventCallback", "(Ljava/util/Lis t;)V");
97
98 if (method)
99 {
100 jobject jsList = AdblockPlus::Android::JniJsValueList2ArrayList(*env, para ms);
101 env->CallVoidMethod(GetCallbackObject(), method, jsList);
102
103 }
104 }
105 }; 54 };
106 55
107 class JniUpdaterCallback: public JniCallbackBase 56 class JniUpdaterCallback : public JniCallbackBase
108 { 57 {
109 public: 58 public:
110 JniUpdaterCallback(JNIEnv* env, jobject callbackObject) : 59 JniUpdaterCallback(JNIEnv* env, jobject callbackObject);
111 JniCallbackBase(env, callbackObject) 60 void Callback(const std::string& arg);
112 {
113 }
114
115 virtual ~JniUpdaterCallback()
116 {
117 }
118
119 void Callback(const std::string& arg)
120 {
121 AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
122
123 jclass clazz = env->GetObjectClass(GetCallbackObject());
124 jmethodID method = env->GetMethodID(clazz, "updateCallback", "(Ljava/lang/St ring;)V");
125
126 if (method)
127 {
128 jstring jArg = env->NewStringUTF(arg.c_str());
129 env->CallVoidMethod(GetCallbackObject(), method, jArg);
130 }
131
132 CheckAndLogJavaException(*env);
133 }
134 }; 61 };
135 62
136 class JniFilterChangeCallback: public JniCallbackBase 63 class JniFilterChangeCallback : public JniCallbackBase
137 { 64 {
138 public: 65 public:
139 JniFilterChangeCallback(JNIEnv* env, jobject callbackObject) : 66 JniFilterChangeCallback(JNIEnv* env, jobject callbackObject);
140 JniCallbackBase(env, callbackObject) 67 void Callback(const std::string& arg, const AdblockPlus::JsValuePtr jsValue);
141 {
142 }
143
144 virtual ~JniFilterChangeCallback()
145 {
146 }
147
148 void Callback(const std::string& arg, const AdblockPlus::JsValuePtr jsValue)
149 {
150 AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
151
152 jclass clazz = env->GetObjectClass(GetCallbackObject());
153 jmethodID method = env->GetMethodID(clazz, "filterChangeCallback", "(Ljava/l ang/String;" TYP("JsValue") ")V");
154
155 if (method)
156 {
157 jstring jArg = env->NewStringUTF(arg.c_str());
158 jobject jJsValue = AdblockPlus::Android::NewJniJsValue(*env, jsValue);
159 env->CallVoidMethod(GetCallbackObject(), method, jArg, jJsValue);
160 }
161
162 CheckAndLogJavaException(*env);
163 }
164 }; 68 };
165 69
166 class JniLogSystemCallback: public JniCallbackBase, public AdblockPlus::LogSyste m 70 class JniLogSystemCallback : public JniCallbackBase, public AdblockPlus::LogSyst em
167 { 71 {
168 public: 72 public:
169 JniLogSystemCallback(JNIEnv* env, jobject callbackObject) : 73 JniLogSystemCallback(JNIEnv* env, jobject callbackObject);
170 JniCallbackBase(env, callbackObject), AdblockPlus::LogSystem() 74 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string& message, const std::string& source);
171 {
172 m_LogLevelClass.reset(new JniGlobalReference<jclass>(env, env->FindClass(PKG ("LogSystem$LogLevel"))));
173 }
174 75
175 virtual ~JniLogSystemCallback()
176 {
177 }
178
179 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string& message, const std::string& source)
180 {
181 AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
182
183 jclass clazz = env->GetObjectClass(GetCallbackObject());
184 jmethodID method = env->GetMethodID(clazz, "logCallback", "(" TYP("LogSystem $LogLevel") "Ljava/lang/String;Ljava/lang/String;)V");
185
186 // TODO: Set log level from Java and handle it here (to reduce C++->Java cal ls)
187
188 if (method)
189 {
190 const char* enumName = 0;
191
192 switch (logLevel)
193 {
194 default:
195 case AdblockPlus::LogSystem::LOG_LEVEL_TRACE:
196 enumName = "TRACE";
197 break;
198 case AdblockPlus::LogSystem::LOG_LEVEL_LOG:
199 enumName = "LOG";
200 break;
201 case AdblockPlus::LogSystem::LOG_LEVEL_INFO:
202 enumName = "INFO";
203 break;
204 case AdblockPlus::LogSystem::LOG_LEVEL_WARN:
205 enumName = "WARN";
206 break;
207 case AdblockPlus::LogSystem::LOG_LEVEL_ERROR:
208 enumName = "ERROR";
209 break;
210 }
211
212 jclass enumClass = m_LogLevelClass->get();
213 if (enumClass)
214 {
215 jfieldID enumField = env->GetStaticFieldID(enumClass, enumName, TYP("Log System$LogLevel"));
216 jobject jLogLevel = env->GetStaticObjectField(enumClass, enumField);
217
218 jstring jMessage = env->NewStringUTF(message.c_str());
219 jstring jSource = env->NewStringUTF(source.c_str());
220
221 env->CallVoidMethod(GetCallbackObject(), method, jLogLevel, jMessage, jS ource);
222 }
223
224 CheckAndLogJavaException(*env);
225 }
226 }
227 private: 76 private:
228 JniGlobalReference<jclass>::PTR m_LogLevelClass; 77 JniGlobalReference<jclass>::Ptr logLevelClass;
229 }; 78 };
230 79
231 class JniWebRequest: public JniCallbackBase, public AdblockPlus::WebRequest 80 class JniWebRequest : public JniCallbackBase, public AdblockPlus::WebRequest
232 { 81 {
233 public: 82 public:
234 JniWebRequest(JNIEnv* env, jobject callbackObject) : 83 JniWebRequest(JNIEnv* env, jobject callbackObject);
235 JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest() 84 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea derList& requestHeaders) const;
236 {
237 m_TupleClass.reset(new JniGlobalReference<jclass>(env, env->FindClass("com/g ithub/rjeschke/neetutils/collections/Tuple")));
238 m_ServerResponseClass.reset(new JniGlobalReference<jclass>(env, env->FindCla ss(PKG("ServerResponse"))));
239 }
240 85
241 virtual ~JniWebRequest() 86 private:
242 { 87 jobject NewTuple(JNIEnv* env, const std::string& a, const std::string& b) cons t;
243 }
244 88
245 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea derList& requestHeaders) const 89 JniGlobalReference<jclass>::Ptr tupleClass;
246 { 90 JniGlobalReference<jclass>::Ptr serverResponseClass;
247 AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
248
249 jclass clazz = env->GetObjectClass(GetCallbackObject());
250 jmethodID method = env->GetMethodID(clazz, "httpGET", "(Ljava/lang/String;Lj ava/util/List;)" TYP("ServerResponse"));
251
252 AdblockPlus::ServerResponse sResponse;
253 sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
254
255 if (method)
256 {
257 jobject arrayList = NewJniArrayList(*env);
258
259 for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), end = requestHeaders.end(); it != end; it++)
260 {
261 JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second ));
262 }
263
264 jobject response = env->CallObjectMethod(GetCallbackObject(), method, env- >NewStringUTF(url.c_str()), arrayList);
265
266 if (!env->ExceptionCheck())
267 {
268 sResponse.status = AdblockPlus::Android::JniGetLongField(*env, m_ServerR esponseClass->get(), response, "status");
269 sResponse.responseStatus = AdblockPlus::Android::JniGetIntField(*env, m_ ServerResponseClass->get(), response, "responseStatus");
270 sResponse.responseText = AdblockPlus::Android::JniGetStringField(*env, m _ServerResponseClass->get(), response, "response");
271 // TODO: transform Headers
272 }
273 }
274
275 CheckAndLogJavaException(*env);
276
277 return sResponse;
278 }
279 private:
280
281 jobject NewTuple(JNIEnv* env, const std::string& a, const std::string& b) cons t
282 {
283 jmethodID factory = env->GetMethodID(m_TupleClass->get(), "<init>", "(Ljava/ lang/Object;Ljava/lang/Object;)V");
284 return env->NewObject(m_TupleClass->get(), factory, env->NewStringUTF(a.c_st r()), env->NewStringUTF(b.c_str()));
285 }
286
287 JniGlobalReference<jclass>::PTR m_TupleClass;
288 JniGlobalReference<jclass>::PTR m_ServerResponseClass;
289
290 }; 91 };
291 92
292 } // namespace Android 93 #endif /* JNICALLBACKS_H */
293 } // namespace AdblockPlus
294
295 #endif /* JNICALLBACKS_H_ */
LEFTRIGHT

Powered by Google App Engine
This is Rietveld