OLD | NEW |
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 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 { | 527 { |
521 try | 528 try |
522 { | 529 { |
523 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); | 530 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr); |
524 std::string url = engine->GetAAUrl(); | 531 std::string url = engine->GetAAUrl(); |
525 return JniStdStringToJava(env, url); | 532 return JniStdStringToJava(env, url); |
526 } | 533 } |
527 CATCH_THROW_AND_RETURN(env, 0) | 534 CATCH_THROW_AND_RETURN(env, 0) |
528 } | 535 } |
529 | 536 |
| 537 static void JNICALL JniUpdateFiltersAsync(JNIEnv* env, jclass clazz, jlong ptr,
jstring jSubscriptionUrl) |
| 538 { |
| 539 std::string subscriptionUrl = JniJavaToStdString(env, jSubscriptionUrl); |
| 540 auto jniFilterEngine = JniLongToTypePtr<JniFilterEngine>(ptr); |
| 541 std::weak_ptr<AdblockPlus::FilterEngine> weakFilterEngine = jniFilterEngine->f
ilterEngine; |
| 542 jniFilterEngine->timer->SetTimer(std::chrono::milliseconds(0), [weakFilterEngi
ne, subscriptionUrl] |
| 543 { |
| 544 auto filterEngine = weakFilterEngine.lock(); |
| 545 if (!filterEngine) |
| 546 { |
| 547 return; |
| 548 } |
| 549 for (auto& subscription : filterEngine->GetListedSubscriptions()) |
| 550 { |
| 551 if (stringBeginsWith(subscriptionUrl, subscription.GetProperty("url").AsSt
ring())) |
| 552 { |
| 553 subscription.UpdateFilters(); |
| 554 return; |
| 555 } |
| 556 } |
| 557 }); |
| 558 } |
| 559 |
530 static JNINativeMethod methods[] = | 560 static JNINativeMethod methods[] = |
531 { | 561 { |
532 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor }, | 562 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor }, |
533 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, | 563 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, |
534 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni
GetFilter }, | 564 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni
GetFilter }, |
535 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed
Filters }, | 565 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed
Filters }, |
536 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription")
, (void*)JniGetSubscription }, | 566 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription")
, (void*)JniGetSubscription }, |
537 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh
owNextNotification }, | 567 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh
owNextNotification }, |
538 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif
icationCallback }, | 568 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif
icationCallback }, |
539 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow
NotificationCallback }, | 569 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow
NotificationCallback }, |
(...skipping 10 matching lines...) Expand all Loading... |
550 { (char*)"isDocumentWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri
ng;)Z", (void*)JniIsDocumentWhitelisted }, | 580 { (char*)"isDocumentWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri
ng;)Z", (void*)JniIsDocumentWhitelisted }, |
551 { (char*)"isElemhideWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri
ng;)Z", (void*)JniIsElemhideWhitelisted }, | 581 { (char*)"isElemhideWhitelisted", (char*)"(JLjava/lang/String;[Ljava/lang/Stri
ng;)Z", (void*)JniIsElemhideWhitelisted }, |
552 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG
etPref }, | 582 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG
etPref }, |
553 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, | 583 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, |
554 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", (
void*)JniGetHostFromURL }, | 584 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", (
void*)JniGetHostFromURL }, |
555 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J
niSetAllowedConnectionType }, | 585 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J
niSetAllowedConnectionType }, |
556 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn
iGetAllowedConnectionType }, | 586 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn
iGetAllowedConnectionType }, |
557 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds
Enabled }, | 587 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds
Enabled }, |
558 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna
bled }, | 588 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna
bled }, |
559 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v
oid*)JniGetAcceptableAdsSubscriptionURL }, | 589 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v
oid*)JniGetAcceptableAdsSubscriptionURL }, |
560 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 590 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, |
| 591 { (char*)"updateFiltersAsync", (char*)"(JLjava/lang/String;)V", (void*)JniUpda
teFiltersAsync } |
561 }; | 592 }; |
562 | 593 |
563 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi
ne_registerNatives(JNIEnv *env, jclass clazz) | 594 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_FilterEngi
ne_registerNatives(JNIEnv *env, jclass clazz) |
564 { | 595 { |
565 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 596 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
566 } | 597 } |
OLD | NEW |