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

Unified Diff: jni/JniFilterEngine.cpp

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Created March 14, 2014, 11:32 a.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/JniFilterChangeCallback.cpp ('k') | jni/JniJsEngine.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: jni/JniFilterEngine.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/jni/JniFilterEngine.cpp
@@ -0,0 +1,300 @@
+/*
+ * 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/>.
+ */
+
+#include <AdblockPlus.h>
+#include "Utils.h"
+#include "JniCallbacks.h"
+
+static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong enginePtr)
+{
+ TRY
+ {
+ AdblockPlus::JsEnginePtr& jsEngine = *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsEnginePtr>(enginePtr);
+ return AdblockPlus::Android::JniPtr2Long(new AdblockPlus::FilterEngine(jsEngine));
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+}
+
+static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ TRY
+ {
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
+ }
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE);
+}
+
+static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstring jText)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+ std::string text = AdblockPlus::Android::JniJava2StdString(env, jText);
+
+ TRY
+ {
+ AdblockPlus::FilterPtr filter = engine->GetFilter(text);
+
+ return AdblockPlus::Android::NewJniFilter(env, filter);
+ }
+ CATCH_THROW_AND_RETURN(env, 0);
+}
+
+static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ TRY
+ {
+ std::vector<AdblockPlus::FilterPtr> filters = engine->GetListedFilters();
+
+ jobject list = AdblockPlus::Android::NewJniArrayList(env);
+
+ for (std::vector<AdblockPlus::FilterPtr>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
+ {
+ AdblockPlus::Android::JniAddObjectToList(env, list, AdblockPlus::Android::NewJniFilter(env, *it));
+ }
+
+ return list;
+ }
+ CATCH_THROW_AND_RETURN(env, 0);
+}
+
+static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+ std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl);
+
+ TRY
+ {
+ AdblockPlus::SubscriptionPtr subscription = engine->GetSubscription(url);
+
+ return AdblockPlus::Android::NewJniSubscription(env, subscription);
+ }
+ CATCH_THROW_AND_RETURN(env, 0);
+}
+
+static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ TRY
+ {
+ std::vector<AdblockPlus::SubscriptionPtr> filters = engine->GetListedSubscriptions();
+
+ jobject list = AdblockPlus::Android::NewJniArrayList(env);
+
+ for (std::vector<AdblockPlus::SubscriptionPtr>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
+ {
+ AdblockPlus::Android::JniAddObjectToList(env, list, AdblockPlus::Android::NewJniSubscription(env, *it));
+ }
+
+ return list;
+ }
+ CATCH_THROW_AND_RETURN(env, 0);
+}
+
+// FIXME: Code duplication
+static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ TRY
+ {
+ std::vector<AdblockPlus::SubscriptionPtr> filters = engine->FetchAvailableSubscriptions();
+
+ jobject list = AdblockPlus::Android::NewJniArrayList(env);
+
+ for (std::vector<AdblockPlus::SubscriptionPtr>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
+ {
+ AdblockPlus::Android::JniAddObjectToList(env, list, AdblockPlus::Android::NewJniSubscription(env, *it));
+ }
+
+ return list;
+ }
+ CATCH_THROW_AND_RETURN(env, 0);
+}
+
+static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlong ptr)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ TRY
+ {
+ engine->RemoveFilterChangeCallback();
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, jlong ptr, jlong filterPtr)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+ AdblockPlus::Android::JniFilterChangeCallback* callback = AdblockPlus::Android::JniLong2TypePtr<
+ AdblockPlus::Android::JniFilterChangeCallback>(filterPtr);
+
+ AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = std::tr1::bind(&AdblockPlus::Android::JniFilterChangeCallback::Callback,
+ callback, std::tr1::placeholders::_1, std::tr1::placeholders::_2);
+
+ TRY
+ {
+ engine->SetFilterChangeCallback(filterCallback);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jlong updaterPtr)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+ AdblockPlus::Android::JniUpdaterCallback* callback = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::Android::JniUpdaterCallback>(
+ updaterPtr);
+
+ AdblockPlus::FilterEngine::UpdaterCallback updaterCallback = 0;
+
+ if (updaterPtr)
+ {
+ updaterCallback = std::tr1::bind(&AdblockPlus::Android::JniUpdaterCallback::Callback, callback, std::tr1::placeholders::_1);
+ }
+
+ TRY
+ {
+ engine->ForceUpdateCheck(updaterCallback);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, jlong ptr, jstring jDomain)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ std::string domain = AdblockPlus::Android::JniJava2StdString(env, jDomain);
+
+ TRY
+ {
+ std::vector<std::string> selectors = engine->GetElementHidingSelectors(domain);
+
+ jobject list = AdblockPlus::Android::NewJniArrayList(env);
+
+ for (std::vector<std::string>::iterator it = selectors.begin(), end = selectors.end(); it != end; it++)
+ {
+ AdblockPlus::Android::JniAddObjectToList(env, list, env->NewStringUTF(it->c_str()));
+ }
+
+ return list;
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jstring jContentType, jstring jDocumentUrl)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl);
+ std::string contentType = AdblockPlus::Android::JniJava2StdString(env, jContentType);
+ std::string documentUrl = AdblockPlus::Android::JniJava2StdString(env, jDocumentUrl);
+
+ TRY
+ {
+ AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUrl);
+
+ return AdblockPlus::Android::NewJniFilter(env, filter);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jstring jContentType, jobjectArray jDocumentUrls)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl);
+ std::string contentType = AdblockPlus::Android::JniJava2StdString(env, jContentType);
+
+ std::vector<std::string> documentUrls;
+
+ jsize len = env->GetArrayLength(jDocumentUrls);
+
+ for (jsize i = 0; i < len; i++)
+ {
+ documentUrls.push_back(
+ AdblockPlus::Android::JniJava2StdString(env, static_cast<jstring>(env->GetObjectArrayElement(jDocumentUrls, i))));
+ }
+
+ TRY
+ {
+ AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUrls);
+
+ return AdblockPlus::Android::NewJniFilter(env, filter);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ std::string pref = AdblockPlus::Android::JniJava2StdString(env, jPref);
+
+ TRY
+ {
+ AdblockPlus::JsValuePtr value = engine->GetPref(pref);
+
+ return AdblockPlus::Android::NewJniJsValue(env, value);
+ }
+ CATCH_THROW_AND_RETURN(env, 0)
+}
+
+static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref, jlong jsValue)
+{
+ AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+
+ std::string pref = AdblockPlus::Android::JniJava2StdString(env, jPref);
+ AdblockPlus::JsValuePtr value = AdblockPlus::Android::JniGetJsValuePtr(jsValue);
+
+ TRY
+ {
+ engine->SetPref(pref, value);
+ }
+ CATCH_AND_THROW(env)
+}
+
+static JNINativeMethod methods[] =
+{
+{ (char*)"ctor", (char*)"(J)J", (void*)JniCtor },
+{ (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun },
+{ (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)JniGetFilter },
+{ (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListedFilters },
+{ (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription"), (void*)JniGetSubscription },
+{ (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGetListedSubscriptions },
+{ (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniFetchAvailableSubscriptions },
+{ (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeCallback },
+{ (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterChangeCallback },
+{ (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck },
+{ (char*)"getElementHidingSelectors", (char*)"(J)Ljava/utils/List;", (void*)JniGetElementHidingSelectors },
+{ (char*)"matches", (char*)"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)" TYP("Filter"), (void*)JniMatches },
+{ (char*)"matches", (char*)"(JLjava/lang/String;Ljava/lang/String;[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany },
+{ (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniGetPref },
+{ (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref },
+{ (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, };
+
+extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_FilterEngine_registerNatives(JNIEnv *env, jclass clazz)
+{
+ env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
+}
« no previous file with comments | « jni/JniFilterChangeCallback.cpp ('k') | jni/JniJsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld