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: @sergz Created June 14, 2017, 2:57 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 | « no previous file | libadblockplus-android/jni/JniJsEngine.h » ('j') | libadblockplus-android/jni/JniJsEngine.h » ('J')
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 9001015bd057f904f2f8159778b927afdf64e37a..09edd6481f916726bda83d5113573f7d8e77e0c8 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)
{
@@ -47,18 +48,26 @@ static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env,
namespace
{
+ struct JniFilterEngine
+ {
+ AdblockPlus::ITimer* timer;
+ AdblockPlus::FilterEnginePtr filterEngine;
+ };
+
AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr)
{
- return *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
+ return JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine;
}
}
-static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong isAllowedConnectionCallbackPtr)
+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)
{
@@ -80,23 +89,21 @@ 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)
@@ -527,6 +534,27 @@ static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl
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)
anton 2017/06/15 05:51:53 "{" required here
sergei 2017/06/16 11:12:08 Done.
+ return;
+ for (auto& subscription : filterEngine->GetListedSubscriptions())
anton 2017/06/15 05:51:53 "}" required here
sergei 2017/06/16 11:12:08 Done.
+ {
+ if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsString()))
+ {
+ subscription.UpdateFilters();
+ return;
+ }
+ }
+ });
+}
+
static JNINativeMethod methods[] =
{
{ (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor },
@@ -557,7 +585,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') | libadblockplus-android/jni/JniJsEngine.h » ('J')

Powered by Google App Engine
This is Rietveld