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

Unified Diff: jni/JniCallbacks.h

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Fixed wrong signature for getElementHidingSelectors. Created March 20, 2014, 12:17 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « jni/Android.mk ('k') | jni/JniEventCallback.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: jni/JniCallbacks.h
diff --git a/jni/JniCallbacks.h b/jni/JniCallbacks.h
new file mode 100644
index 0000000000000000000000000000000000000000..3cb81acab256785709d2360dc4fbe78123f66b6b
--- /dev/null
+++ b/jni/JniCallbacks.h
@@ -0,0 +1,295 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef JNICALLBACKS_H_
+#define JNICALLBACKS_H_
+
+#include <AdblockPlus.h>
+#include "Utils.h"
+#include "JniJsValue.h"
+
+namespace AdblockPlus
+{
+namespace Android
+{
+
+class JniCallbackBase
+{
+public:
+ JniCallbackBase(JNIEnv* env, jobject callbackObject)
+ {
+ env->GetJavaVM(&m_JavaVM);
+ m_CallbackObject.reset(new JniGlobalReference<jobject>(env, callbackObject));
+ m_ExceptionLoggerClass.reset(new JniGlobalReference<jclass>(env, env->FindClass(PKG("JniExceptionHandler"))));
+ }
+
+ virtual ~JniCallbackBase()
+ {
+ }
+
+ inline JavaVM* GetJavaVM() const
+ {
+ return m_JavaVM;
+ }
+
+ inline jobject GetCallbackObject() const
+ {
+ return m_CallbackObject->get();
+ }
+
+ void LogException(JNIEnv* env, jthrowable throwable) const
+ {
+ jmethodID logMethod = env->GetStaticMethodID(m_ExceptionLoggerClass->get(), "logException", "(Ljava/lang/Throwable;)V");
+ if (logMethod)
+ {
+ env->CallStaticVoidMethod(m_ExceptionLoggerClass->get(), logMethod, throwable);
+ }
+ }
+
+ void CheckAndLogJavaException(JNIEnv* env) const
+ {
+ if (env->ExceptionCheck())
+ {
+ jthrowable throwable = env->ExceptionOccurred();
+ env->ExceptionClear();
+ LogException(env, throwable);
+ }
+ }
+
+private:
+ JavaVM* m_JavaVM;
+ JniGlobalReference<jobject>::PTR m_CallbackObject;
+ JniGlobalReference<jclass>::PTR m_ExceptionLoggerClass;
+};
+
+class JniEventCallback: public JniCallbackBase
+{
+public:
+ JniEventCallback(JNIEnv* env, jobject callbackObject) :
+ JniCallbackBase(env, callbackObject)
+ {
+ }
+
+ virtual ~JniEventCallback()
+ {
+ }
+
+ void Callback(AdblockPlus::JsValueList& params)
+ {
+ AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
+
+ jclass clazz = env->GetObjectClass(GetCallbackObject());
+ jmethodID method = env->GetMethodID(clazz, "eventCallback", "(Ljava/util/List;)V");
+
+ if (method)
+ {
+ jobject jsList = AdblockPlus::Android::JniJsValueList2ArrayList(*env, params);
+ env->CallVoidMethod(GetCallbackObject(), method, jsList);
+
+ }
+ }
+};
+
+class JniUpdaterCallback: public JniCallbackBase
+{
+public:
+ JniUpdaterCallback(JNIEnv* env, jobject callbackObject) :
+ JniCallbackBase(env, callbackObject)
+ {
+ }
+
+ virtual ~JniUpdaterCallback()
+ {
+ }
+
+ void Callback(const std::string& arg)
+ {
+ AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
+
+ jclass clazz = env->GetObjectClass(GetCallbackObject());
+ jmethodID method = env->GetMethodID(clazz, "updateCallback", "(Ljava/lang/String;)V");
+
+ if (method)
+ {
+ jstring jArg = env->NewStringUTF(arg.c_str());
+ env->CallVoidMethod(GetCallbackObject(), method, jArg);
+ }
+
+ CheckAndLogJavaException(*env);
+ }
+};
+
+class JniFilterChangeCallback: public JniCallbackBase
+{
+public:
+ JniFilterChangeCallback(JNIEnv* env, jobject callbackObject) :
+ JniCallbackBase(env, callbackObject)
+ {
+ }
+
+ virtual ~JniFilterChangeCallback()
+ {
+ }
+
+ void Callback(const std::string& arg, const AdblockPlus::JsValuePtr jsValue)
+ {
+ AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
+
+ jclass clazz = env->GetObjectClass(GetCallbackObject());
+ jmethodID method = env->GetMethodID(clazz, "filterChangeCallback", "(Ljava/lang/String;" TYP("JsValue") ")V");
+
+ if (method)
+ {
+ jstring jArg = env->NewStringUTF(arg.c_str());
+ jobject jJsValue = AdblockPlus::Android::NewJniJsValue(*env, jsValue);
+ env->CallVoidMethod(GetCallbackObject(), method, jArg, jJsValue);
+ }
+
+ CheckAndLogJavaException(*env);
+ }
+};
+
+class JniLogSystemCallback: public JniCallbackBase, public AdblockPlus::LogSystem
+{
+public:
+ JniLogSystemCallback(JNIEnv* env, jobject callbackObject) :
+ JniCallbackBase(env, callbackObject), AdblockPlus::LogSystem()
+ {
+ m_LogLevelClass.reset(new JniGlobalReference<jclass>(env, env->FindClass(PKG("LogSystem$LogLevel"))));
+ }
+
+ virtual ~JniLogSystemCallback()
+ {
+ }
+
+ void operator()(AdblockPlus::LogSystem::LogLevel logLevel, const std::string& message, const std::string& source)
+ {
+ AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
+
+ jclass clazz = env->GetObjectClass(GetCallbackObject());
+ jmethodID method = env->GetMethodID(clazz, "logCallback", "(" TYP("LogSystem$LogLevel") "Ljava/lang/String;Ljava/lang/String;)V");
+
+ // TODO: Set log level from Java and handle it here (to reduce C++->Java calls)
+
+ if (method)
+ {
+ const char* enumName = 0;
+
+ switch (logLevel)
+ {
+ default:
+ case AdblockPlus::LogSystem::LOG_LEVEL_TRACE:
+ enumName = "TRACE";
+ break;
+ case AdblockPlus::LogSystem::LOG_LEVEL_LOG:
+ enumName = "LOG";
+ break;
+ case AdblockPlus::LogSystem::LOG_LEVEL_INFO:
+ enumName = "INFO";
+ break;
+ case AdblockPlus::LogSystem::LOG_LEVEL_WARN:
+ enumName = "WARN";
+ break;
+ case AdblockPlus::LogSystem::LOG_LEVEL_ERROR:
+ enumName = "ERROR";
+ break;
+ }
+
+ jclass enumClass = m_LogLevelClass->get();
+ if (enumClass)
+ {
+ jfieldID enumField = env->GetStaticFieldID(enumClass, enumName, TYP("LogSystem$LogLevel"));
+ jobject jLogLevel = env->GetStaticObjectField(enumClass, enumField);
+
+ jstring jMessage = env->NewStringUTF(message.c_str());
+ jstring jSource = env->NewStringUTF(source.c_str());
+
+ env->CallVoidMethod(GetCallbackObject(), method, jLogLevel, jMessage, jSource);
+ }
+
+ CheckAndLogJavaException(*env);
+ }
+ }
+private:
+ JniGlobalReference<jclass>::PTR m_LogLevelClass;
+};
+
+class JniWebRequest: public JniCallbackBase, public AdblockPlus::WebRequest
+{
+public:
+ JniWebRequest(JNIEnv* env, jobject callbackObject) :
+ JniCallbackBase(env, callbackObject), AdblockPlus::WebRequest()
+ {
+ m_TupleClass.reset(new JniGlobalReference<jclass>(env, env->FindClass("com/github/rjeschke/neetutils/collections/Tuple")));
+ m_ServerResponseClass.reset(new JniGlobalReference<jclass>(env, env->FindClass(PKG("ServerResponse"))));
+ }
+
+ virtual ~JniWebRequest()
+ {
+ }
+
+ AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
+ {
+ AdblockPlus::Android::JNIEnvAcquire env(GetJavaVM());
+
+ jclass clazz = env->GetObjectClass(GetCallbackObject());
+ jmethodID method = env->GetMethodID(clazz, "httpGET", "(Ljava/lang/String;Ljava/util/List;)" TYP("ServerResponse"));
+
+ AdblockPlus::ServerResponse sResponse;
+ sResponse.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
+
+ if (method)
+ {
+ jobject arrayList = NewJniArrayList(*env);
+
+ for (AdblockPlus::HeaderList::const_iterator it = requestHeaders.begin(), end = requestHeaders.end(); it != end; it++)
+ {
+ JniAddObjectToList(*env, arrayList, NewTuple(*env, it->first, it->second));
+ }
+
+ jobject response = env->CallObjectMethod(GetCallbackObject(), method, env->NewStringUTF(url.c_str()), arrayList);
+
+ if (!env->ExceptionCheck())
+ {
+ sResponse.status = AdblockPlus::Android::JniGetLongField(*env, m_ServerResponseClass->get(), response, "status");
+ sResponse.responseStatus = AdblockPlus::Android::JniGetIntField(*env, m_ServerResponseClass->get(), response, "responseStatus");
+ sResponse.responseText = AdblockPlus::Android::JniGetStringField(*env, m_ServerResponseClass->get(), response, "response");
+ // TODO: transform Headers
+ }
+ }
+
+ CheckAndLogJavaException(*env);
+
+ return sResponse;
+ }
+private:
+
+ jobject NewTuple(JNIEnv* env, const std::string& a, const std::string& b) const
+ {
+ jmethodID factory = env->GetMethodID(m_TupleClass->get(), "<init>", "(Ljava/lang/Object;Ljava/lang/Object;)V");
+ return env->NewObject(m_TupleClass->get(), factory, env->NewStringUTF(a.c_str()), env->NewStringUTF(b.c_str()));
+ }
+
+ JniGlobalReference<jclass>::PTR m_TupleClass;
+ JniGlobalReference<jclass>::PTR m_ServerResponseClass;
+
+};
+
+} // namespace Android
+} // namespace AdblockPlus
+
+#endif /* JNICALLBACKS_H_ */
« no previous file with comments | « jni/Android.mk ('k') | jni/JniEventCallback.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld