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