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 virtual ~JniEventCallback(); |
| 84 void Callback(AdblockPlus::JsValueList& params); |
| 85 }; |
| 86 |
| 87 class JniUpdaterCallback: public JniCallbackBase |
| 88 { |
| 89 public: |
| 90 JniUpdaterCallback(JNIEnv* env, jobject callbackObject); |
| 91 virtual ~JniUpdaterCallback(); |
| 92 void Callback(const std::string& arg); |
| 93 }; |
| 94 |
| 95 class JniFilterChangeCallback: public JniCallbackBase |
| 96 { |
| 97 public: |
| 98 JniFilterChangeCallback(JNIEnv* env, jobject callbackObject); |
| 99 virtual ~JniFilterChangeCallback(); |
| 100 void Callback(const std::string& arg, const AdblockPlus::JsValuePtr jsValue); |
| 101 }; |
| 102 |
| 103 class JniLogSystemCallback: public JniCallbackBase, public AdblockPlus::LogSyste
m |
| 104 { |
| 105 public: |
| 106 JniLogSystemCallback(JNIEnv* env, jobject callbackObject); |
| 107 virtual ~JniLogSystemCallback(); |
| 108 void operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string&
message, const std::string& source); |
| 109 private: |
| 110 JniGlobalReference<jclass>::PTR m_LogLevelClass; |
| 111 }; |
| 112 |
| 113 class JniWebRequest: public JniCallbackBase, public AdblockPlus::WebRequest |
| 114 { |
| 115 public: |
| 116 JniWebRequest(JNIEnv* env, jobject callbackObject); |
| 117 virtual ~JniWebRequest(); |
| 118 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::Hea
derList& requestHeaders) const; |
| 119 private: |
| 120 jobject NewTuple(JNIEnv* env, const std::string& a, const std::string& b) cons
t; |
| 121 |
| 122 JniGlobalReference<jclass>::PTR m_TupleClass; |
| 123 JniGlobalReference<jclass>::PTR m_ServerResponseClass; |
| 124 }; |
| 125 |
| 126 } // namespace Android |
| 127 } // namespace AdblockPlus |
| 128 |
| 129 #endif /* JNICALLBACKS_H_ */ |
OLD | NEW |