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

Side by Side 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: rebase Created July 5, 2017, 1:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | libadblockplus-android/jni/JniJsEngine.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <AdblockPlus.h> 18 #include <AdblockPlus.h>
19 #include "Utils.h" 19 #include "Utils.h"
20 #include "JniCallbacks.h" 20 #include "JniCallbacks.h"
21 #include <thread> 21 #include <thread>
22 #include "JniJsEngine.h"
22 23
23 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su bscription>&& subscriptions) 24 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su bscription>&& subscriptions)
24 { 25 {
25 jobject list = NewJniArrayList(env); 26 jobject list = NewJniArrayList(env);
26 27
27 for (std::vector<AdblockPlus::Subscription>::iterator it = subscriptions.begin (), end = subscriptions.end(); it != end; it++) 28 for (std::vector<AdblockPlus::Subscription>::iterator it = subscriptions.begin (), end = subscriptions.end(); it != end; it++)
28 { 29 {
29 JniAddObjectToList(env, list, NewJniSubscription(env, std::move(*it))); 30 JniAddObjectToList(env, list, NewJniSubscription(env, std::move(*it)));
30 } 31 }
31 32
32 return list; 33 return list;
33 } 34 }
34 35
35 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env, 36 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env,
36 jobject jContentType) 37 jobject jContentType)
37 { 38 {
38 JniLocalReference<jclass> contentTypeClass(env, 39 JniLocalReference<jclass> contentTypeClass(env,
39 env->GetObjectClass(jContentType)); 40 env->GetObjectClass(jContentType));
40 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name", 41 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name",
41 "()Ljava/lang/String;"); 42 "()Ljava/lang/String;");
42 JniLocalReference<jstring> jValue(env, 43 JniLocalReference<jstring> jValue(env,
43 (jstring) env->CallObjectMethod(jContentType, nameMethod)); 44 (jstring) env->CallObjectMethod(jContentType, nameMethod));
44 const std::string value = JniJavaToStdString(env, *jValue); 45 const std::string value = JniJavaToStdString(env, *jValue);
45 return AdblockPlus::FilterEngine::StringToContentType(value); 46 return AdblockPlus::FilterEngine::StringToContentType(value);
46 } 47 }
47 48
48 namespace 49 namespace
49 { 50 {
51 struct JniFilterEngine
52 {
53 AdblockPlus::ITimer* timer;
54 AdblockPlus::FilterEnginePtr filterEngine;
55 };
56
50 AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr) 57 AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr)
51 { 58 {
52 return *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); 59 return JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine;
53 } 60 }
54 } 61 }
55 62
56 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong isAllowedConnectionCallbackPtr) 63 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jniJsEnginePtr, jl ong isAllowedConnectionCallbackPtr)
57 { 64 {
58 try 65 try
59 { 66 {
60 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(jsEnginePtr); 67 auto jniJsEngine = JniLongToTypePtr<JniJsEngine>(jniJsEnginePtr);
61 AdblockPlus::FilterEnginePtr* filterEngine = NULL; 68 auto jsEngine = jniJsEngine->jsEngine;
69 auto jniFilterEngine = new JniFilterEngine();
70 jniFilterEngine->timer = jniJsEngine->timer;
62 71
63 if (isAllowedConnectionCallbackPtr != 0) 72 if (isAllowedConnectionCallbackPtr != 0)
64 { 73 {
65 AdblockPlus::FilterEngine::CreationParameters creationParameters; 74 AdblockPlus::FilterEngine::CreationParameters creationParameters;
66 JniIsAllowedConnectionTypeCallback* callback = 75 JniIsAllowedConnectionTypeCallback* callback =
67 JniLongToTypePtr<JniIsAllowedConnectionTypeCallback>(isAllowedConnection CallbackPtr); 76 JniLongToTypePtr<JniIsAllowedConnectionTypeCallback>(isAllowedConnection CallbackPtr);
68 77
69 creationParameters.isSubscriptionDowloadAllowedCallback = 78 creationParameters.isSubscriptionDowloadAllowedCallback =
70 [callback](const std::string* allowedConnectionTypeArg, const std::funct ion<void(bool)>& doneCallback) 79 [callback](const std::string* allowedConnectionTypeArg, const std::funct ion<void(bool)>& doneCallback)
71 { 80 {
72 std::shared_ptr<std::string> allowedConnectionType; 81 std::shared_ptr<std::string> allowedConnectionType;
73 if (allowedConnectionTypeArg) 82 if (allowedConnectionTypeArg)
74 { 83 {
75 allowedConnectionType = std::make_shared<std::string>(*allowedConnecti onTypeArg); 84 allowedConnectionType = std::make_shared<std::string>(*allowedConnecti onTypeArg);
76 } 85 }
77 std::thread([callback, allowedConnectionType, doneCallback] 86 std::thread([callback, allowedConnectionType, doneCallback]
78 { 87 {
79 doneCallback(callback->Callback(allowedConnectionType.get())); 88 doneCallback(callback->Callback(allowedConnectionType.get()));
80 }).detach(); 89 }).detach();
81 }; 90 };
82 91
83 filterEngine = new AdblockPlus::FilterEnginePtr( 92 jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine , creationParameters);
84 AdblockPlus::FilterEngine::Create(jsEngine, creationParameters));
85 } 93 }
86 else 94 else
87 { 95 {
88 filterEngine = new AdblockPlus::FilterEnginePtr( 96 jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine );
89 AdblockPlus::FilterEngine::Create(jsEngine));
90 } 97 }
91 98
92 return JniPtrToLong(filterEngine); 99 return JniPtrToLong(jniFilterEngine);
93 } 100 }
94 CATCH_THROW_AND_RETURN(env, 0) 101 CATCH_THROW_AND_RETURN(env, 0)
95 } 102 }
96 103
97 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 104 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
98 { 105 {
99 delete JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); 106 delete JniLongToTypePtr<JniFilterEngine>(ptr);
100 } 107 }
101 108
102 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) 109 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
103 { 110 {
104 try 111 try
105 { 112 {
106 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 113 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
107 114
108 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; 115 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
109 } 116 }
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 { 526 {
520 try 527 try
521 { 528 {
522 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); 529 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
523 std::string url = engine->GetAAUrl(); 530 std::string url = engine->GetAAUrl();
524 return JniStdStringToJava(env, url); 531 return JniStdStringToJava(env, url);
525 } 532 }
526 CATCH_THROW_AND_RETURN(env, 0) 533 CATCH_THROW_AND_RETURN(env, 0)
527 } 534 }
528 535
536 static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong ptr, jstring jSubscriptionUrl)
537 {
538 std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl);
539 auto jniFilterEngine = JniLongToTypePtr<JniFilterEngine>(ptr);
540 std::weak_ptr<AdblockPlus::FilterEngine> weakFilterEngine = jniFilterEngine->f ilterEngine;
541 jniFilterEngine->timer->SetTimer(std::chrono::milliseconds(0), [weakFilterEngi ne, subscriptionUrl]
542 {
543 auto filterEngine = weakFilterEngine.lock();
544 if (!filterEngine)
545 {
546 return;
547 }
548 for (auto& subscription : filterEngine->GetListedSubscriptions())
549 {
550 if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsSt ring()))
551 {
552 subscription.UpdateFilters();
553 return;
554 }
555 }
556 });
557 }
558
529 static JNINativeMethod methods[] = 559 static JNINativeMethod methods[] =
530 { 560 {
531 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor }, 561 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor },
532 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, 562 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun },
533 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter }, 563 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter },
534 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters }, 564 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters },
535 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription }, 565 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription },
536 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh owNextNotification }, 566 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh owNextNotification },
537 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif icationCallback }, 567 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif icationCallback },
538 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow NotificationCallback }, 568 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow NotificationCallback },
(...skipping 10 matching lines...) Expand all
549 { (char*)"isDocumentWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsDocumentWhitelisted }, 579 { (char*)"isDocumentWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsDocumentWhitelisted },
550 { (char*)"isElemhideWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsElemhideWhitelisted }, 580 { (char*)"isElemhideWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsElemhideWhitelisted },
551 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref }, 581 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref },
552 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, 582 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref },
553 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", ( void*)JniGetHostFromURL }, 583 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", ( void*)JniGetHostFromURL },
554 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J niSetAllowedConnectionType }, 584 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J niSetAllowedConnectionType },
555 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn iGetAllowedConnectionType }, 585 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn iGetAllowedConnectionType },
556 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled }, 586 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled },
557 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled }, 587 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled },
558 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL }, 588 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL },
559 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } 589 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
590 { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpda teFiltersAsync }
560 }; 591 };
561 592
562 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz) 593 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz)
563 { 594 {
564 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 595 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
565 } 596 }
OLDNEW
« 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