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

Unified Diff: libadblockplus-android/jni/JniFilterEngine.cpp

Issue 29465639: Issue 5309 - Subscriptions update causes ANR (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Patch Set: address comments@sergz Created June 16, 2017, 11: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
« no previous file with comments | « no previous file | libadblockplus-android/jni/JniJsEngine.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: libadblockplus-android/jni/JniFilterEngine.cpp
diff --git a/libadblockplus-android/jni/JniFilterEngine.cpp b/libadblockplus-android/jni/JniFilterEngine.cpp
index c70ec4cbb08aee084140909eef0b7c91427323ae..d1b9fc0b64a0a3db4d0cc843769f7eae63a0f136 100644
--- a/libadblockplus-android/jni/JniFilterEngine.cpp
+++ b/libadblockplus-android/jni/JniFilterEngine.cpp
@@ -19,6 +19,7 @@
#include "Utils.h"
#include "JniCallbacks.h"
#include <thread>
+#include "JniJsEngine.h"
static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Subscription>&& subscriptions)
{
@@ -45,12 +46,28 @@ static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env,
return AdblockPlus::FilterEngine::StringToContentType(value);
}
-static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong isAllowedConnectionCallbackPtr)
+namespace
+{
+ struct JniFilterEngine
+ {
+ AdblockPlus::ITimer* timer;
+ AdblockPlus::FilterEnginePtr filterEngine;
+ };
+
+ AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr)
+ {
+ return JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine;
+ }
+}
+
+static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jniJsEnginePtr, jlong isAllowedConnectionCallbackPtr)
{
try
{
- AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>(jsEnginePtr);
- AdblockPlus::FilterEnginePtr* filterEngine = NULL;
+ auto jniJsEngine = JniLongToTypePtr<JniJsEngine>(jniJsEnginePtr);
+ auto jsEngine = jniJsEngine->jsEngine;
+ auto jniFilterEngine = new JniFilterEngine();
+ jniFilterEngine->timer = jniJsEngine->timer;
if (isAllowedConnectionCallbackPtr != 0)
{
@@ -72,30 +89,28 @@ static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong
}).detach();
};
- filterEngine = new AdblockPlus::FilterEnginePtr(
- AdblockPlus::FilterEngine::Create(jsEngine, creationParameters));
+ jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine, creationParameters);
}
else
{
- filterEngine = new AdblockPlus::FilterEnginePtr(
- AdblockPlus::FilterEngine::Create(jsEngine));
+ jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
}
- return JniPtrToLong(filterEngine);
+ return JniPtrToLong(jniFilterEngine);
}
CATCH_THROW_AND_RETURN(env, 0)
}
static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
{
- delete JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ delete JniLongToTypePtr<JniFilterEngine>(ptr);
}
static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
{
try
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
}
@@ -104,7 +119,7 @@ static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstring jText)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string text = JniJavaToStdString(env, jText);
try
@@ -116,7 +131,7 @@ static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin
static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
@@ -136,7 +151,7 @@ static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jUrl);
try
@@ -148,7 +163,7 @@ static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr,
static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jUrl);
try
@@ -161,8 +176,7 @@ static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr
static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz,
jlong ptr, jlong callbackPtr)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
JniShowNotificationCallback* const callback =
JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr);
@@ -181,7 +195,7 @@ static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz,
static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
@@ -192,7 +206,7 @@ static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz,
static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
@@ -203,7 +217,7 @@ static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon
static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
@@ -215,8 +229,7 @@ static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz,
static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz,
jlong ptr)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
engine->RemoveUpdateAvailableCallback();
@@ -227,8 +240,7 @@ static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz,
static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz,
jlong ptr, jlong callbackPtr)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
JniUpdateAvailableCallback* const callback =
JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr);
@@ -244,7 +256,7 @@ static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz,
static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlong ptr)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
@@ -256,8 +268,7 @@ static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo
static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
jlong ptr, jlong filterPtr)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(
filterPtr);
@@ -275,7 +286,7 @@ static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jlong updaterPtr)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
JniUpdateCheckDoneCallback* callback =
JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr);
@@ -299,8 +310,7 @@ static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl
static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
jlong ptr, jstring jDomain)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string domain = JniJavaToStdString(env, jDomain);
@@ -325,7 +335,7 @@ static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jUrl);
AdblockPlus::FilterEngine::ContentType contentType =
@@ -363,7 +373,7 @@ static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls)
{
AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jUrl);
AdblockPlus::FilterEngine::ContentType contentType =
@@ -384,8 +394,7 @@ static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlong ptr,
jstring jUrl, jobjectArray jDocumentUrls)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jUrl);
std::vector<std::string> documentUrls;
@@ -401,8 +410,7 @@ static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon
static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlong ptr,
jstring jUrl, jobjectArray jDocumentUrls)
{
- AdblockPlus::FilterEnginePtr& engine =
- *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jUrl);
std::vector<std::string> documentUrls;
@@ -417,7 +425,7 @@ static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon
static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string pref = JniJavaToStdString(env, jPref);
@@ -430,7 +438,7 @@ static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring
static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref, jlong jsValue)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string pref = JniJavaToStdString(env, jPref);
const AdblockPlus::JsValue& value = JniGetJsValue(jsValue);
@@ -449,7 +457,7 @@ static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j
return NULL;
}
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = JniJavaToStdString(env, jurl);
try
@@ -463,7 +471,7 @@ static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j
static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string stdValue;
const std::string* value = (jvalue != NULL
@@ -481,7 +489,7 @@ static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl
{
try
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::unique_ptr<std::string> value = engine->GetAllowedConnectionType();
if (value == NULL)
@@ -496,7 +504,7 @@ static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl
static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue)
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
try
{
@@ -509,7 +517,7 @@ static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo
{
try
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
return engine->IsAAEnabled() ? JNI_TRUE : JNI_FALSE;
}
CATCH_THROW_AND_RETURN(env, 0)
@@ -519,13 +527,36 @@ static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl
{
try
{
- AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
std::string url = engine->GetAAUrl();
return JniStdStringToJava(env, url);
}
CATCH_THROW_AND_RETURN(env, 0)
}
+static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong ptr, jstring jSubscriptionUrl)
+{
+ std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl);
+ auto jniFilterEngine = JniLongToTypePtr<JniFilterEngine>(ptr);
+ std::weak_ptr<AdblockPlus::FilterEngine> weakFilterEngine = jniFilterEngine->filterEngine;
+ jniFilterEngine->timer->SetTimer(std::chrono::milliseconds(0), [weakFilterEngine, subscriptionUrl]
+ {
+ auto filterEngine = weakFilterEngine.lock();
+ if (!filterEngine)
+ {
+ return;
+ }
+ for (auto& subscription : filterEngine->GetListedSubscriptions())
+ {
+ if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsString()))
+ {
+ subscription.UpdateFilters();
+ return;
+ }
+ }
+ });
+}
+
static JNINativeMethod methods[] =
{
{ (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor },
@@ -556,7 +587,8 @@ static JNINativeMethod methods[] =
{ (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAdsEnabled },
{ (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEnabled },
{ (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (void*)JniGetAcceptableAdsSubscriptionURL },
- { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
+ { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
+ { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpdateFiltersAsync }
};
extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngine_registerNatives(JNIEnv *env, jclass clazz)
« no previous file with comments | « no previous file | libadblockplus-android/jni/JniJsEngine.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld