OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 #ifndef JNICALLBACKS_H_ |
| 19 #define JNICALLBACKS_H_ |
| 20 |
| 21 #include <AdblockPlus.h> |
| 22 #include "Utils.h" |
| 23 #include "JniJsValue.h" |
| 24 |
| 25 namespace AdblockPlus |
| 26 { |
| 27 namespace Android |
| 28 { |
| 29 |
| 30 class JniCallbackBase |
| 31 { |
| 32 public: |
| 33 JniCallbackBase(JNIEnv* env, jobject callbackObject) |
| 34 { |
| 35 env->GetJavaVM(&m_JavaVM); |
| 36 m_CallbackObject.reset(new JniGlobalReference<jobject>(env, callbackObject))
; |
| 37 m_ExceptionLoggerClass.reset(new JniGlobalReference<jclass>(env, env->FindCl
ass(PKG("JniExceptionHandler")))); |
| 38 } |
| 39 |
| 40 virtual ~JniCallbackBase() |
| 41 { |
| 42 } |
| 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 } |
| 72 |
| 73 private: |
| 74 JavaVM* m_JavaVM; |
| 75 JniGlobalReference<jobject>::PTR m_CallbackObject; |
| 76 JniGlobalReference<jclass>::PTR m_ExceptionLoggerClass; |
| 77 }; |
| 78 |
| 79 class JniEventCallback: public JniCallbackBase |
| 80 { |
| 81 public: |
| 82 JniEventCallback(JNIEnv* env, jobject callbackObject) : |
| 83 JniCallbackBase(env, callbackObject) |
| 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 }; |
| 106 |
| 107 class JniUpdaterCallback: public JniCallbackBase |
| 108 { |
| 109 public: |
| 110 JniUpdaterCallback(JNIEnv* env, jobject callbackObject) : |
| 111 JniCallbackBase(env, callbackObject) |
| 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 }; |
| 135 |
| 136 class JniFilterChangeCallback: public JniCallbackBase |
| 137 { |
| 138 public: |
| 139 JniFilterChangeCallback(JNIEnv* env, jobject callbackObject) : |
| 140 JniCallbackBase(env, callbackObject) |
| 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 }; |
| 165 |
| 166 class JniLogSystemCallback: public JniCallbackBase, public AdblockPlus::LogSyste
m |
| 167 { |
| 168 public: |
| 169 JniLogSystemCallback(JNIEnv* env, jobject callbackObject) : |
| 170 JniCallbackBase(env, callbackObject), AdblockPlus::LogSystem() |
| 171 { |
| 172 m_LogLevelClass.reset(new JniGlobalReference<jclass>(env, env->FindClass(PKG
("LogSystem$LogLevel")))); |
| 173 } |
| 174 |
| 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: |
| 228 JniGlobalReference<jclass>::PTR m_LogLevelClass; |
| 229 }; |
| 230 |
| 231 class JniWebRequest: public JniCallbackBase, public AdblockPlus::WebRequest |
| 232 { |
| 233 public: |
| 234 JniWebRequest(JNIEnv* env, jobject callbackObject) : |
| 235 JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest() |
| 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 |
| 241 virtual ~JniWebRequest() |
| 242 { |
| 243 } |
| 244 |
| 245 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const |
| 246 { |
| 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 }; |
| 291 |
| 292 } // namespace Android |
| 293 } // namespace AdblockPlus |
| 294 |
| 295 #endif /* JNICALLBACKS_H_ */ |
OLD | NEW |