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

Unified Diff: jni/JniFilterEngine.cpp

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Style review fixes Created March 28, 2014, 11 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
Index: jni/JniFilterEngine.cpp
diff --git a/jni/JniFilterEngine.cpp b/jni/JniFilterEngine.cpp
index ff441582e992b354e9ea1ed70d9ffdcdbd053d2c..2f61fe19ddbb4be875b091779b24c31fdbb8e511 100644
--- a/jni/JniFilterEngine.cpp
+++ b/jni/JniFilterEngine.cpp
@@ -19,26 +19,38 @@
#include "Utils.h"
#include "JniCallbacks.h"
+static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::SubscriptionPtr>& subscriptions)
+{
+ jobject list = NewJniArrayList(env);
+
+ for (std::vector<AdblockPlus::SubscriptionPtr>::iterator it = subscriptions.begin(), end = subscriptions.end(); it != end; it++)
+ {
+ JniAddObjectToList(env, list, NewJniSubscription(env, *it));
+ }
+
+ return list;
+}
+
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));
+ AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>(enginePtr);
+ return JniPtrToLong(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);
+ delete JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
}
static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
{
TRY
{
- AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
}
@@ -47,31 +59,31 @@ static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
+ std::string text = JniJava2StdString(env, jText);
TRY
{
AdblockPlus::FilterPtr filter = engine->GetFilter(text);
- return AdblockPlus::Android::NewJniFilter(env, filter);
+ return 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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
TRY
{
std::vector<AdblockPlus::FilterPtr> filters = engine->GetListedFilters();
- jobject list = AdblockPlus::Android::NewJniArrayList(env);
+ jobject list = 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));
+ JniAddObjectToList(env, list, NewJniFilter(env, *it));
}
return list;
@@ -81,62 +93,47 @@ static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
+ std::string url = JniJava2StdString(env, jUrl);
TRY
{
AdblockPlus::SubscriptionPtr subscription = engine->GetSubscription(url);
- return AdblockPlus::Android::NewJniSubscription(env, subscription);
+ return 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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
TRY
{
- std::vector<AdblockPlus::SubscriptionPtr> filters = engine->GetListedSubscriptions();
+ std::vector<AdblockPlus::SubscriptionPtr> subscriptions = 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;
+ return SubscriptionsToArrayList(env, subscriptions);
}
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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
TRY
{
- std::vector<AdblockPlus::SubscriptionPtr> filters = engine->FetchAvailableSubscriptions();
-
- jobject list = AdblockPlus::Android::NewJniArrayList(env);
+ std::vector<AdblockPlus::SubscriptionPtr> subscriptions = engine->FetchAvailableSubscriptions();
- 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;
+ return SubscriptionsToArrayList(env, subscriptions);
}
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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
TRY
{
@@ -147,12 +144,11 @@ static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo
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* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
+ JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(filterPtr);
- AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = std::tr1::bind(&AdblockPlus::Android::JniFilterChangeCallback::Callback,
- callback, std::tr1::placeholders::_1, std::tr1::placeholders::_2);
+ AdblockPlus::FilterEngine::FilterChangeCallback filterCallback =
+ std::tr1::bind(&JniFilterChangeCallback::Callback, callback, std::tr1::placeholders::_1, std::tr1::placeholders::_2);
TRY
{
@@ -163,15 +159,14 @@ static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, jlong
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* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
+ JniUpdaterCallback* callback = JniLongToTypePtr<JniUpdaterCallback>(updaterPtr);
AdblockPlus::FilterEngine::UpdaterCallback updaterCallback = 0;
if (updaterPtr)
{
- updaterCallback = std::tr1::bind(&AdblockPlus::Android::JniUpdaterCallback::Callback, callback, std::tr1::placeholders::_1);
+ updaterCallback = std::tr1::bind(&JniUpdaterCallback::Callback, callback, std::tr1::placeholders::_1);
}
TRY
@@ -183,19 +178,19 @@ static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl
static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, jlong ptr, jstring jDomain)
{
- AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
- std::string domain = AdblockPlus::Android::JniJava2StdString(env, jDomain);
+ std::string domain = JniJava2StdString(env, jDomain);
TRY
{
std::vector<std::string> selectors = engine->GetElementHidingSelectors(domain);
- jobject list = AdblockPlus::Android::NewJniArrayList(env);
+ jobject list = 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()));
+ JniAddObjectToList(env, list, env->NewStringUTF(it->c_str()));
}
return list;
@@ -205,27 +200,27 @@ static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, j
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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<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);
+ std::string url = JniJava2StdString(env, jUrl);
+ std::string contentType = JniJava2StdString(env, jContentType);
+ std::string documentUrl = JniJava2StdString(env, jDocumentUrl);
TRY
{
AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUrl);
- return AdblockPlus::Android::NewJniFilter(env, filter);
+ return 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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr);
- std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl);
- std::string contentType = AdblockPlus::Android::JniJava2StdString(env, jContentType);
+ std::string url = JniJava2StdString(env, jUrl);
+ std::string contentType = JniJava2StdString(env, jContentType);
std::vector<std::string> documentUrls;
@@ -233,40 +228,39 @@ static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, jstr
for (jsize i = 0; i < len; i++)
{
- documentUrls.push_back(
- AdblockPlus::Android::JniJava2StdString(env, static_cast<jstring>(env->GetObjectArrayElement(jDocumentUrls, i))));
+ documentUrls.push_back(JniJava2StdString(env, static_cast<jstring>(env->GetObjectArrayElement(jDocumentUrls, i))));
}
TRY
{
AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUrls);
- return AdblockPlus::Android::NewJniFilter(env, filter);
+ return 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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngine > (ptr);
- std::string pref = AdblockPlus::Android::JniJava2StdString(env, jPref);
+ std::string pref = JniJava2StdString(env, jPref);
TRY
{
AdblockPlus::JsValuePtr value = engine->GetPref(pref);
- return AdblockPlus::Android::NewJniJsValue(env, value);
+ return 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);
+ AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngine > (ptr);
- std::string pref = AdblockPlus::Android::JniJava2StdString(env, jPref);
- AdblockPlus::JsValuePtr value = AdblockPlus::Android::JniGetJsValuePtr(jsValue);
+ std::string pref = JniJava2StdString(env, jPref);
+ AdblockPlus::JsValuePtr value = JniGetJsValuePtr(jsValue);
TRY
{
@@ -277,22 +271,23 @@ static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr
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*)"(JLjava/lang/String;)Ljava/util/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 }, };
+ { (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*)"(JLjava/lang/String;)Ljava/util/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)
{

Powered by Google App Engine
This is Rietveld