| LEFT | RIGHT |
| 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 virtual ~JniEventCallback(); | |
| 84 void Callback(AdblockPlus::JsValueList& params); | 53 void Callback(AdblockPlus::JsValueList& params); |
| 85 }; | 54 }; |
| 86 | 55 |
| 87 class JniUpdaterCallback: public JniCallbackBase | 56 class JniUpdaterCallback : public JniCallbackBase |
| 88 { | 57 { |
| 89 public: | 58 public: |
| 90 JniUpdaterCallback(JNIEnv* env, jobject callbackObject); | 59 JniUpdaterCallback(JNIEnv* env, jobject callbackObject); |
| 91 virtual ~JniUpdaterCallback(); | |
| 92 void Callback(const std::string& arg); | 60 void Callback(const std::string& arg); |
| 93 }; | 61 }; |
| 94 | 62 |
| 95 class JniFilterChangeCallback: public JniCallbackBase | 63 class JniFilterChangeCallback : public JniCallbackBase |
| 96 { | 64 { |
| 97 public: | 65 public: |
| 98 JniFilterChangeCallback(JNIEnv* env, jobject callbackObject); | 66 JniFilterChangeCallback(JNIEnv* env, jobject callbackObject); |
| 99 virtual ~JniFilterChangeCallback(); | |
| 100 void Callback(const std::string& arg, const AdblockPlus::JsValuePtr jsValue); | 67 void Callback(const std::string& arg, const AdblockPlus::JsValuePtr jsValue); |
| 101 }; | 68 }; |
| 102 | 69 |
| 103 class JniLogSystemCallback: public JniCallbackBase, public AdblockPlus::LogSyste
m | 70 class JniLogSystemCallback : public JniCallbackBase, public AdblockPlus::LogSyst
em |
| 104 { | 71 { |
| 105 public: | 72 public: |
| 106 JniLogSystemCallback(JNIEnv* env, jobject callbackObject); | 73 JniLogSystemCallback(JNIEnv* env, jobject callbackObject); |
| 107 virtual ~JniLogSystemCallback(); | |
| 108 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string&
message, const std::string& source); | 74 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string&
message, const std::string& source); |
| 75 |
| 109 private: | 76 private: |
| 110 JniGlobalReference<jclass>::PTR m_LogLevelClass; | 77 JniGlobalReference<jclass>::Ptr logLevelClass; |
| 111 }; | 78 }; |
| 112 | 79 |
| 113 class JniWebRequest: public JniCallbackBase, public AdblockPlus::WebRequest | 80 class JniWebRequest : public JniCallbackBase, public AdblockPlus::WebRequest |
| 114 { | 81 { |
| 115 public: | 82 public: |
| 116 JniWebRequest(JNIEnv* env, jobject callbackObject); | 83 JniWebRequest(JNIEnv* env, jobject callbackObject); |
| 117 virtual ~JniWebRequest(); | |
| 118 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const; | 84 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const; |
| 85 |
| 119 private: | 86 private: |
| 120 jobject NewTuple(JNIEnv* env, const std::string& a, const std::string& b) cons
t; | 87 jobject NewTuple(JNIEnv* env, const std::string& a, const std::string& b) cons
t; |
| 121 | 88 |
| 122 JniGlobalReference<jclass>::PTR m_TupleClass; | 89 JniGlobalReference<jclass>::Ptr tupleClass; |
| 123 JniGlobalReference<jclass>::PTR m_ServerResponseClass; | 90 JniGlobalReference<jclass>::Ptr serverResponseClass; |
| 124 }; | 91 }; |
| 125 | 92 |
| 126 } // namespace Android | 93 #endif /* JNICALLBACKS_H */ |
| 127 } // namespace AdblockPlus | |
| 128 | |
| 129 #endif /* JNICALLBACKS_H_ */ | |
| LEFT | RIGHT |