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

Side by Side Diff: libadblockplus-android/jni/JniFilterEngine.cpp

Issue 29536629: Issue 5556 - Update to use libadblockplus revision hg:566f64c8a2a8 (Closed) Base URL: github.com:abby-sergz/libadblockplus-android.git
Patch Set: address comments Created Sept. 8, 2017, 9:39 a.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 | « libadblockplus-android/jni/JniFilter.cpp ('k') | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 #include "JniPlatform.h"
23 23
24 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su bscription>&& subscriptions) 24 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su bscription>&& subscriptions)
25 { 25 {
26 jobject list = NewJniArrayList(env); 26 jobject list = NewJniArrayList(env);
27 27
28 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++)
29 { 29 {
30 JniAddObjectToList(env, list, NewJniSubscription(env, std::move(*it))); 30 JniAddObjectToList(env, list, NewJniSubscription(env, std::move(*it)));
31 } 31 }
32 32
33 return list; 33 return list;
34 } 34 }
35 35
36 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env, 36 static AdblockPlus::FilterEngine::ContentType ConvertContentType(JNIEnv *env,
37 jobject jContentType) 37 jobject jContentType)
38 { 38 {
39 JniLocalReference<jclass> contentTypeClass(env, 39 JniLocalReference<jclass> contentTypeClass(env,
40 env->GetObjectClass(jContentType)); 40 env->GetObjectClass(jContentType));
41 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name", 41 jmethodID nameMethod = env->GetMethodID(*contentTypeClass, "name",
42 "()Ljava/lang/String;"); 42 "()Ljava/lang/String;");
43 JniLocalReference<jstring> jValue(env, 43 JniLocalReference<jstring> jValue(env,
44 (jstring) env->CallObjectMethod(jContentType, nameMethod)); 44 (jstring) env->CallObjectMethod(jContentType, nameMethod));
45 const std::string value = JniJavaToStdString(env, *jValue); 45 const std::string value = JniJavaToStdString(env, *jValue);
46 return AdblockPlus::FilterEngine::StringToContentType(value); 46 return AdblockPlus::FilterEngine::StringToContentType(value);
47 } 47 }
48 48
49 namespace 49 namespace
50 { 50 {
51 struct JniFilterEngine 51 AdblockPlus::FilterEngine& GetFilterEngineRef(jlong jniPlatformPtr)
52 { 52 {
53 AdblockPlus::ITimer* timer; 53 return JniLongToTypePtr<JniPlatform>(jniPlatformPtr)->platform->GetFilterEng ine();
54 AdblockPlus::FilterEnginePtr filterEngine;
55 };
56
57 AdblockPlus::FilterEngine& GetFilterEngineRef(jlong ptr)
58 {
59 return *JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine;
60 } 54 }
61 } 55 }
62 56
63 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jniJsEnginePtr, jo bject jIsSubscriptionDownloadAllowedCallback)
64 {
65 try
66 {
67 auto jniJsEngine = JniLongToTypePtr<JniJsEngine>(jniJsEnginePtr);
68 auto jsEngine = jniJsEngine->jsEngine;
69 auto jniFilterEngine = new JniFilterEngine();
70 jniFilterEngine->timer = jniJsEngine->timer;
71
72 if (jIsSubscriptionDownloadAllowedCallback)
73 {
74 AdblockPlus::FilterEngine::CreationParameters creationParameters;
75 auto callback = std::make_shared<JniIsAllowedConnectionTypeCallback>(env, jIsSubscriptionDownloadAllowedCallback);
76
77 creationParameters.isSubscriptionDownloadAllowedCallback =
78 [callback](const std::string* allowedConnectionTypeArg, const std::funct ion<void(bool)>& doneCallback)
79 {
80 std::shared_ptr<std::string> allowedConnectionType;
81 if (allowedConnectionTypeArg)
82 {
83 allowedConnectionType = std::make_shared<std::string>(*allowedConnecti onTypeArg);
84 }
85 std::thread([callback, allowedConnectionType, doneCallback]
86 {
87 doneCallback(callback->Callback(allowedConnectionType.get()));
88 }).detach();
89 };
90
91 jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine , creationParameters);
92 }
93 else
94 {
95 jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine );
96 }
97
98 return JniPtrToLong(jniFilterEngine);
99 }
100 CATCH_THROW_AND_RETURN(env, 0)
101 }
102
103 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
104 {
105 delete JniLongToTypePtr<JniFilterEngine>(ptr);
106 }
107
108 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) 57 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
109 { 58 {
110 try 59 try
111 { 60 {
112 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr); 61 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
113 62
114 return engine.IsFirstRun() ? JNI_TRUE : JNI_FALSE; 63 return engine.IsFirstRun() ? JNI_TRUE : JNI_FALSE;
115 } 64 }
116 CATCH_THROW_AND_RETURN(env, JNI_FALSE); 65 CATCH_THROW_AND_RETURN(env, JNI_FALSE);
117 } 66 }
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 { 474 {
526 try 475 try
527 { 476 {
528 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr); 477 AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);
529 std::string url = engine.GetAAUrl(); 478 std::string url = engine.GetAAUrl();
530 return JniStdStringToJava(env, url); 479 return JniStdStringToJava(env, url);
531 } 480 }
532 CATCH_THROW_AND_RETURN(env, 0) 481 CATCH_THROW_AND_RETURN(env, 0)
533 } 482 }
534 483
535 static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong ptr, jstring jSubscriptionUrl) 484 static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong jniPl atformPtr, jstring jSubscriptionUrl)
536 { 485 {
537 std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl); 486 std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl);
538 auto jniFilterEngine = JniLongToTypePtr<JniFilterEngine>(ptr); 487 auto jniPlatform = JniLongToTypePtr<JniPlatform>(jniPlatformPtr);
539 jniFilterEngine->timer->SetTimer(std::chrono::milliseconds(0), [jniFilterEngin e, subscriptionUrl] 488 jniPlatform->scheduler([jniPlatform, subscriptionUrl]
540 { 489 {
541 auto& filterEngine = *jniFilterEngine->filterEngine; 490 auto& filterEngine = jniPlatform->platform->GetFilterEngine();
542 for (auto& subscription : filterEngine.GetListedSubscriptions()) 491 for (auto& subscription : filterEngine.GetListedSubscriptions())
543 { 492 {
544 if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsSt ring())) 493 if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsSt ring()))
545 { 494 {
546 subscription.UpdateFilters(); 495 subscription.UpdateFilters();
547 return; 496 return;
548 } 497 }
549 } 498 }
550 }); 499 });
551 } 500 }
552 501
553 static JNINativeMethod methods[] = 502 static JNINativeMethod methods[] =
554 { 503 {
555 { (char*)"ctor", (char*)"(J" TYP("IsAllowedConnectionCallback") ")J", (void*)J niCtor },
556 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, 504 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun },
557 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter }, 505 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter },
558 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters }, 506 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters },
559 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription }, 507 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription },
560 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh owNextNotification }, 508 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh owNextNotification },
561 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif icationCallback }, 509 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif icationCallback },
562 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow NotificationCallback }, 510 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow NotificationCallback },
563 { (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGet ListedSubscriptions }, 511 { (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGet ListedSubscriptions },
564 { (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)J niFetchAvailableSubscriptions }, 512 { (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)J niFetchAvailableSubscriptions },
565 { (char*)"setUpdateAvailableCallback", (char*)"(JJ)V", (void*)JniSetUpdateAvai lableCallback }, 513 { (char*)"setUpdateAvailableCallback", (char*)"(JJ)V", (void*)JniSetUpdateAvai lableCallback },
566 { (char*)"removeUpdateAvailableCallback", (char*)"(J)V", (void*)JniRemoveUpdat eAvailableCallback }, 514 { (char*)"removeUpdateAvailableCallback", (char*)"(J)V", (void*)JniRemoveUpdat eAvailableCallback },
567 { (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeC allback }, 515 { (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeC allback },
568 { (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterCh angeCallback }, 516 { (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterCh angeCallback },
569 { (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck }, 517 { (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck },
570 { (char*)"getElementHidingSelectors", (char*)"(JLjava/lang/String;)Ljava/util/ List;", (void*)JniGetElementHidingSelectors }, 518 { (char*)"getElementHidingSelectors", (char*)"(JLjava/lang/String;)Ljava/util/ List;", (void*)JniGetElementHidingSelectors },
571 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "Ljava/lang/String;)" TYP("Filter"), (void*)JniMatches }, 519 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "Ljava/lang/String;)" TYP("Filter"), (void*)JniMatches },
572 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany }, 520 { (char*)"matches", (char*)"(JLjava/lang/String;" TYP("FilterEngine$ContentTyp e") "[Ljava/lang/String;)" TYP("Filter"), (void*)JniMatchesMany },
573 { (char*)"isDocumentWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsDocumentWhitelisted }, 521 { (char*)"isDocumentWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsDocumentWhitelisted },
574 { (char*)"isElemhideWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsElemhideWhitelisted }, 522 { (char*)"isElemhideWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri ng;)Z", (void*)JniIsElemhideWhitelisted },
575 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref }, 523 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref },
576 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, 524 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref },
577 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", ( void*)JniGetHostFromURL }, 525 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", ( void*)JniGetHostFromURL },
578 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J niSetAllowedConnectionType }, 526 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J niSetAllowedConnectionType },
579 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn iGetAllowedConnectionType }, 527 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn iGetAllowedConnectionType },
580 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled }, 528 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled },
581 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled }, 529 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled },
582 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL }, 530 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL },
583 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor },
584 { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpda teFiltersAsync } 531 { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpda teFiltersAsync }
585 }; 532 };
586 533
587 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz) 534 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi ne_registerNatives(JNIEnv *env, jclass clazz)
588 { 535 {
589 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 536 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
590 } 537 }
OLDNEW
« no previous file with comments | « libadblockplus-android/jni/JniFilter.cpp ('k') | libadblockplus-android/jni/JniJsEngine.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld