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: address comments@sergz Created June 16, 2017, 11:11 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 | « 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 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jsEnginePtr, jlong isAllowedConnectionCallbackPtr) 49 namespace
50 {
51 struct JniFilterEngine
52 {
53 AdblockPlus::ITimer* timer;
54 AdblockPlus::FilterEnginePtr filterEngine;
55 };
56
57 AdblockPlus::FilterEnginePtr& GetFilterEnginePtrRef(jlong ptr)
58 {
59 return JniLongToTypePtr<JniFilterEngine>(ptr)->filterEngine;
60 }
61 }
62
63 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong jniJsEnginePtr, jl ong isAllowedConnectionCallbackPtr)
49 { 64 {
50 try 65 try
51 { 66 {
52 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(jsEnginePtr); 67 auto jniJsEngine = JniLongToTypePtr<JniJsEngine>(jniJsEnginePtr);
53 AdblockPlus::FilterEnginePtr* filterEngine = NULL; 68 auto jsEngine = jniJsEngine->jsEngine;
69 auto jniFilterEngine = new JniFilterEngine();
70 jniFilterEngine->timer = jniJsEngine->timer;
54 71
55 if (isAllowedConnectionCallbackPtr != 0) 72 if (isAllowedConnectionCallbackPtr != 0)
56 { 73 {
57 AdblockPlus::FilterEngine::CreationParameters creationParameters; 74 AdblockPlus::FilterEngine::CreationParameters creationParameters;
58 JniIsAllowedConnectionTypeCallback* callback = 75 JniIsAllowedConnectionTypeCallback* callback =
59 JniLongToTypePtr<JniIsAllowedConnectionTypeCallback>(isAllowedConnection CallbackPtr); 76 JniLongToTypePtr<JniIsAllowedConnectionTypeCallback>(isAllowedConnection CallbackPtr);
60 77
61 creationParameters.isSubscriptionDowloadAllowedCallback = 78 creationParameters.isSubscriptionDowloadAllowedCallback =
62 [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)
63 { 80 {
64 std::shared_ptr<std::string> allowedConnectionType; 81 std::shared_ptr<std::string> allowedConnectionType;
65 if (allowedConnectionTypeArg) 82 if (allowedConnectionTypeArg)
66 { 83 {
67 allowedConnectionType = std::make_shared<std::string>(*allowedConnecti onTypeArg); 84 allowedConnectionType = std::make_shared<std::string>(*allowedConnecti onTypeArg);
68 } 85 }
69 std::thread([callback, allowedConnectionType, doneCallback] 86 std::thread([callback, allowedConnectionType, doneCallback]
70 { 87 {
71 doneCallback(callback->Callback(allowedConnectionType.get())); 88 doneCallback(callback->Callback(allowedConnectionType.get()));
72 }).detach(); 89 }).detach();
73 }; 90 };
74 91
75 filterEngine = new AdblockPlus::FilterEnginePtr( 92 jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine , creationParameters);
76 AdblockPlus::FilterEngine::Create(jsEngine, creationParameters));
77 } 93 }
78 else 94 else
79 { 95 {
80 filterEngine = new AdblockPlus::FilterEnginePtr( 96 jniFilterEngine->filterEngine = AdblockPlus::FilterEngine::Create(jsEngine );
81 AdblockPlus::FilterEngine::Create(jsEngine));
82 } 97 }
83 98
84 return JniPtrToLong(filterEngine); 99 return JniPtrToLong(jniFilterEngine);
85 } 100 }
86 CATCH_THROW_AND_RETURN(env, 0) 101 CATCH_THROW_AND_RETURN(env, 0)
87 } 102 }
88 103
89 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) 104 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
90 { 105 {
91 delete JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); 106 delete JniLongToTypePtr<JniFilterEngine>(ptr);
92 } 107 }
93 108
94 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) 109 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr)
95 { 110 {
96 try 111 try
97 { 112 {
98 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 113 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
99 114
100 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; 115 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
101 } 116 }
102 CATCH_THROW_AND_RETURN(env, JNI_FALSE); 117 CATCH_THROW_AND_RETURN(env, JNI_FALSE);
103 } 118 }
104 119
105 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText) 120 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText)
106 { 121 {
107 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 122 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
108 std::string text = JniJavaToStdString(env, jText); 123 std::string text = JniJavaToStdString(env, jText);
109 124
110 try 125 try
111 { 126 {
112 return NewJniFilter(env, engine->GetFilter(text)); 127 return NewJniFilter(env, engine->GetFilter(text));
113 } 128 }
114 CATCH_THROW_AND_RETURN(env, 0); 129 CATCH_THROW_AND_RETURN(env, 0);
115 } 130 }
116 131
117 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) 132 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr)
118 { 133 {
119 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 134 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
120 135
121 try 136 try
122 { 137 {
123 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters(); 138 std::vector<AdblockPlus::Filter> filters = engine->GetListedFilters();
124 139
125 jobject list = NewJniArrayList(env); 140 jobject list = NewJniArrayList(env);
126 141
127 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end = filters.end(); it != end; it++) 142 for (std::vector<AdblockPlus::Filter>::iterator it = filters.begin(), end = filters.end(); it != end; it++)
128 { 143 {
129 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, std::move(*it)))); 144 JniAddObjectToList(env, list, *JniLocalReference<jobject>(env, NewJniFilte r(env, std::move(*it))));
130 } 145 }
131 146
132 return list; 147 return list;
133 } 148 }
134 CATCH_THROW_AND_RETURN(env, 0); 149 CATCH_THROW_AND_RETURN(env, 0);
135 } 150 }
136 151
137 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl) 152 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl)
138 { 153 {
139 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 154 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
140 std::string url = JniJavaToStdString(env, jUrl); 155 std::string url = JniJavaToStdString(env, jUrl);
141 156
142 try 157 try
143 { 158 {
144 return NewJniSubscription(env, engine->GetSubscription(url)); 159 return NewJniSubscription(env, engine->GetSubscription(url));
145 } 160 }
146 CATCH_THROW_AND_RETURN(env, 0); 161 CATCH_THROW_AND_RETURN(env, 0);
147 } 162 }
148 163
149 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr , jstring jUrl) 164 static void JNICALL JniShowNextNotification(JNIEnv* env, jclass clazz, jlong ptr , jstring jUrl)
150 { 165 {
151 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 166 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
152 std::string url = JniJavaToStdString(env, jUrl); 167 std::string url = JniJavaToStdString(env, jUrl);
153 168
154 try 169 try
155 { 170 {
156 engine->ShowNextNotification(url); 171 engine->ShowNextNotification(url);
157 } 172 }
158 CATCH_AND_THROW(env); 173 CATCH_AND_THROW(env);
159 } 174 }
160 175
161 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz, 176 static void JNICALL JniSetShowNotificationCallback(JNIEnv* env, jclass clazz,
162 jlong ptr, jlong callbackPtr) 177 jlong ptr, jlong callbackPtr)
163 { 178 {
164 AdblockPlus::FilterEnginePtr& engine = 179 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
165 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
166 180
167 JniShowNotificationCallback* const callback = 181 JniShowNotificationCallback* const callback =
168 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr); 182 JniLongToTypePtr<JniShowNotificationCallback>(callbackPtr);
169 183
170 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific ation) 184 auto showNotificationCallback = [callback](AdblockPlus::Notification&& notific ation)
171 { 185 {
172 callback->Callback(std::move(notification)); 186 callback->Callback(std::move(notification));
173 }; 187 };
174 188
175 try 189 try
176 { 190 {
177 engine->SetShowNotificationCallback(showNotificationCallback); 191 engine->SetShowNotificationCallback(showNotificationCallback);
178 } 192 }
179 CATCH_AND_THROW(env) 193 CATCH_AND_THROW(env)
180 } 194 }
181 195
182 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr) 196 static void JNICALL JniRemoveShowNotificationCallback(JNIEnv* env, jclass clazz, jlong ptr)
183 { 197 {
184 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 198 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
185 199
186 try 200 try
187 { 201 {
188 engine->RemoveShowNotificationCallback(); 202 engine->RemoveShowNotificationCallback();
189 } 203 }
190 CATCH_AND_THROW(env); 204 CATCH_AND_THROW(env);
191 } 205 }
192 206
193 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr) 207 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr)
194 { 208 {
195 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 209 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
196 210
197 try 211 try
198 { 212 {
199 return SubscriptionsToArrayList(env, engine->GetListedSubscriptions()); 213 return SubscriptionsToArrayList(env, engine->GetListedSubscriptions());
200 } 214 }
201 CATCH_THROW_AND_RETURN(env, 0); 215 CATCH_THROW_AND_RETURN(env, 0);
202 } 216 }
203 217
204 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr) 218 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr)
205 { 219 {
206 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 220 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
207 221
208 try 222 try
209 { 223 {
210 return SubscriptionsToArrayList(env, engine->FetchAvailableSubscriptions()); 224 return SubscriptionsToArrayList(env, engine->FetchAvailableSubscriptions());
211 } 225 }
212 CATCH_THROW_AND_RETURN(env, 0); 226 CATCH_THROW_AND_RETURN(env, 0);
213 } 227 }
214 228
215 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz, 229 static void JNICALL JniRemoveUpdateAvailableCallback(JNIEnv* env, jclass clazz,
216 jlong ptr) 230 jlong ptr)
217 { 231 {
218 AdblockPlus::FilterEnginePtr& engine = 232 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
219 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
220 try 233 try
221 { 234 {
222 engine->RemoveUpdateAvailableCallback(); 235 engine->RemoveUpdateAvailableCallback();
223 } 236 }
224 CATCH_AND_THROW(env) 237 CATCH_AND_THROW(env)
225 } 238 }
226 239
227 static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz, 240 static void JNICALL JniSetUpdateAvailableCallback(JNIEnv* env, jclass clazz,
228 jlong ptr, jlong callbackPtr) 241 jlong ptr, jlong callbackPtr)
229 { 242 {
230 AdblockPlus::FilterEnginePtr& engine = 243 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
231 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
232 JniUpdateAvailableCallback* const callback = 244 JniUpdateAvailableCallback* const callback =
233 JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr); 245 JniLongToTypePtr<JniUpdateAvailableCallback>(callbackPtr);
234 246
235 const AdblockPlus::FilterEngine::UpdateAvailableCallback updateAvailableCallba ck = 247 const AdblockPlus::FilterEngine::UpdateAvailableCallback updateAvailableCallba ck =
236 std::bind(&JniUpdateAvailableCallback::Callback, callback, 248 std::bind(&JniUpdateAvailableCallback::Callback, callback,
237 std::placeholders::_1); 249 std::placeholders::_1);
238 try 250 try
239 { 251 {
240 engine->SetUpdateAvailableCallback(updateAvailableCallback); 252 engine->SetUpdateAvailableCallback(updateAvailableCallback);
241 } 253 }
242 CATCH_AND_THROW(env) 254 CATCH_AND_THROW(env)
243 } 255 }
244 256
245 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr) 257 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr)
246 { 258 {
247 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 259 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
248 260
249 try 261 try
250 { 262 {
251 engine->RemoveFilterChangeCallback(); 263 engine->RemoveFilterChangeCallback();
252 } 264 }
253 CATCH_AND_THROW(env) 265 CATCH_AND_THROW(env)
254 } 266 }
255 267
256 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, 268 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz,
257 jlong ptr, jlong filterPtr) 269 jlong ptr, jlong filterPtr)
258 { 270 {
259 AdblockPlus::FilterEnginePtr& engine = 271 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
260 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
261 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( 272 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>(
262 filterPtr); 273 filterPtr);
263 274
264 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue& & jsValue) 275 auto filterCallback = [callback](const std::string& arg, AdblockPlus::JsValue& & jsValue)
265 { 276 {
266 callback->Callback(arg, std::move(jsValue)); 277 callback->Callback(arg, std::move(jsValue));
267 }; 278 };
268 279
269 try 280 try
270 { 281 {
271 engine->SetFilterChangeCallback(filterCallback); 282 engine->SetFilterChangeCallback(filterCallback);
272 } 283 }
273 CATCH_AND_THROW(env) 284 CATCH_AND_THROW(env)
274 } 285 }
275 286
276 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr) 287 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr)
277 { 288 {
278 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 289 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
279 JniUpdateCheckDoneCallback* callback = 290 JniUpdateCheckDoneCallback* callback =
280 JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr); 291 JniLongToTypePtr<JniUpdateCheckDoneCallback>(updaterPtr);
281 292
282 AdblockPlus::FilterEngine::UpdateCheckDoneCallback 293 AdblockPlus::FilterEngine::UpdateCheckDoneCallback
283 updateCheckDoneCallback = 0; 294 updateCheckDoneCallback = 0;
284 295
285 if (updaterPtr) 296 if (updaterPtr)
286 { 297 {
287 updateCheckDoneCallback = 298 updateCheckDoneCallback =
288 std::bind(&JniUpdateCheckDoneCallback::Callback, callback, 299 std::bind(&JniUpdateCheckDoneCallback::Callback, callback,
289 std::placeholders::_1); 300 std::placeholders::_1);
290 } 301 }
291 302
292 try 303 try
293 { 304 {
294 engine->ForceUpdateCheck(updateCheckDoneCallback); 305 engine->ForceUpdateCheck(updateCheckDoneCallback);
295 } 306 }
296 CATCH_AND_THROW(env) 307 CATCH_AND_THROW(env)
297 } 308 }
298 309
299 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, 310 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz,
300 jlong ptr, jstring jDomain) 311 jlong ptr, jstring jDomain)
301 { 312 {
302 AdblockPlus::FilterEnginePtr& engine = 313 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
303 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
304 314
305 std::string domain = JniJavaToStdString(env, jDomain); 315 std::string domain = JniJavaToStdString(env, jDomain);
306 316
307 try 317 try
308 { 318 {
309 std::vector<std::string> selectors = engine->GetElementHidingSelectors( 319 std::vector<std::string> selectors = engine->GetElementHidingSelectors(
310 domain); 320 domain);
311 321
312 jobject list = NewJniArrayList(env); 322 jobject list = NewJniArrayList(env);
313 323
314 for (std::vector<std::string>::iterator it = selectors.begin(), end = 324 for (std::vector<std::string>::iterator it = selectors.begin(), end =
315 selectors.end(); it != end; it++) 325 selectors.end(); it != end; it++)
316 { 326 {
317 JniAddObjectToList(env, list, 327 JniAddObjectToList(env, list,
318 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str()))); 328 *JniLocalReference<jstring>(env, env->NewStringUTF(it->c_str())));
319 } 329 }
320 330
321 return list; 331 return list;
322 } 332 }
323 CATCH_THROW_AND_RETURN(env, 0) 333 CATCH_THROW_AND_RETURN(env, 0)
324 } 334 }
325 335
326 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl) 336 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jobject jContentType, jstring jDocumentUrl)
327 { 337 {
328 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 338 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
329 339
330 std::string url = JniJavaToStdString(env, jUrl); 340 std::string url = JniJavaToStdString(env, jUrl);
331 AdblockPlus::FilterEngine::ContentType contentType = 341 AdblockPlus::FilterEngine::ContentType contentType =
332 ConvertContentType(env, jContentType); 342 ConvertContentType(env, jContentType);
333 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); 343 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl);
334 344
335 try 345 try
336 { 346 {
337 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrl); 347 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrl);
338 348
(...skipping 17 matching lines...) Expand all
356 static_cast<jstring>( 366 static_cast<jstring>(
357 env->GetObjectArrayElement(jArray, i))))); 367 env->GetObjectArrayElement(jArray, i)))));
358 } 368 }
359 } 369 }
360 } 370 }
361 371
362 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, 372 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr,
363 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls) 373 jstring jUrl, jobject jContentType, jobjectArray jDocumentUrls)
364 { 374 {
365 AdblockPlus::FilterEnginePtr& engine = 375 AdblockPlus::FilterEnginePtr& engine =
366 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr); 376 GetFilterEnginePtrRef(ptr);
367 377
368 std::string url = JniJavaToStdString(env, jUrl); 378 std::string url = JniJavaToStdString(env, jUrl);
369 AdblockPlus::FilterEngine::ContentType contentType = 379 AdblockPlus::FilterEngine::ContentType contentType =
370 ConvertContentType(env, jContentType); 380 ConvertContentType(env, jContentType);
371 381
372 std::vector<std::string> documentUrls; 382 std::vector<std::string> documentUrls;
373 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 383 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
374 384
375 try 385 try
376 { 386 {
377 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrls); 387 AdblockPlus::FilterPtr filterPtr = engine->Matches(url, contentType, documen tUrls);
378 388
379 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0); 389 return (filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0);
380 } 390 }
381 CATCH_THROW_AND_RETURN(env, 0) 391 CATCH_THROW_AND_RETURN(env, 0)
382 } 392 }
383 393
384 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr, 394 static jboolean JNICALL JniIsDocumentWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr,
385 jstring jUrl, jobjectArray jDocumentUrls) 395 jstring jUrl, jobjectArray jDocumentUrls)
386 { 396 {
387 AdblockPlus::FilterEnginePtr& engine = 397 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
388 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
389 398
390 std::string url = JniJavaToStdString(env, jUrl); 399 std::string url = JniJavaToStdString(env, jUrl);
391 std::vector<std::string> documentUrls; 400 std::vector<std::string> documentUrls;
392 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 401 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
393 try 402 try
394 { 403 {
395 return engine->IsDocumentWhitelisted(url, documentUrls) ? 404 return engine->IsDocumentWhitelisted(url, documentUrls) ?
396 JNI_TRUE : JNI_FALSE; 405 JNI_TRUE : JNI_FALSE;
397 } 406 }
398 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 407 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
399 } 408 }
400 409
401 static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr, 410 static jboolean JNICALL JniIsElemhideWhitelisted(JNIEnv* env, jclass clazz, jlon g ptr,
402 jstring jUrl, jobjectArray jDocumentUrls) 411 jstring jUrl, jobjectArray jDocumentUrls)
403 { 412 {
404 AdblockPlus::FilterEnginePtr& engine = 413 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
405 *JniLongToTypePtr<AdblockPlus::FilterEnginePtr>(ptr);
406 414
407 std::string url = JniJavaToStdString(env, jUrl); 415 std::string url = JniJavaToStdString(env, jUrl);
408 std::vector<std::string> documentUrls; 416 std::vector<std::string> documentUrls;
409 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls); 417 JavaStringArrayToStringVector(env, jDocumentUrls, documentUrls);
410 try 418 try
411 { 419 {
412 return engine->IsElemhideWhitelisted(url, documentUrls) ? 420 return engine->IsElemhideWhitelisted(url, documentUrls) ?
413 JNI_TRUE : JNI_FALSE; 421 JNI_TRUE : JNI_FALSE;
414 } 422 }
415 CATCH_THROW_AND_RETURN(env, JNI_FALSE) 423 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
416 } 424 }
417 425
418 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref) 426 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref)
419 { 427 {
420 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 428 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
421 429
422 std::string pref = JniJavaToStdString(env, jPref); 430 std::string pref = JniJavaToStdString(env, jPref);
423 431
424 try 432 try
425 { 433 {
426 return NewJniJsValue(env, engine->GetPref(pref)); 434 return NewJniJsValue(env, engine->GetPref(pref));
427 } 435 }
428 CATCH_THROW_AND_RETURN(env, 0) 436 CATCH_THROW_AND_RETURN(env, 0)
429 } 437 }
430 438
431 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue) 439 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue)
432 { 440 {
433 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 441 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
434 442
435 std::string pref = JniJavaToStdString(env, jPref); 443 std::string pref = JniJavaToStdString(env, jPref);
436 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue); 444 const AdblockPlus::JsValue& value = JniGetJsValue(jsValue);
437 445
438 try 446 try
439 { 447 {
440 engine->SetPref(pref, value); 448 engine->SetPref(pref, value);
441 } 449 }
442 CATCH_AND_THROW(env) 450 CATCH_AND_THROW(env)
443 } 451 }
444 452
445 static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j string jurl) 453 static jstring JNICALL JniGetHostFromURL(JNIEnv* env, jclass clazz, jlong ptr, j string jurl)
446 { 454 {
447 if (jurl == NULL) 455 if (jurl == NULL)
448 { 456 {
449 return NULL; 457 return NULL;
450 } 458 }
451 459
452 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 460 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
453 461
454 std::string url = JniJavaToStdString(env, jurl); 462 std::string url = JniJavaToStdString(env, jurl);
455 try 463 try
456 { 464 {
457 std::string host = engine->GetHostFromURL(url); 465 std::string host = engine->GetHostFromURL(url);
458 466
459 return JniStdStringToJava(env, host); 467 return JniStdStringToJava(env, host);
460 } 468 }
461 CATCH_THROW_AND_RETURN(env, 0) 469 CATCH_THROW_AND_RETURN(env, 0)
462 } 470 }
463 471
464 static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue) 472 static void JNICALL JniSetAllowedConnectionType(JNIEnv* env, jclass clazz, jlong ptr, jstring jvalue)
465 { 473 {
466 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 474 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
467 475
468 std::string stdValue; 476 std::string stdValue;
469 const std::string* value = (jvalue != NULL 477 const std::string* value = (jvalue != NULL
470 ? &(stdValue = JniJavaToStdString(env, jvalue)) 478 ? &(stdValue = JniJavaToStdString(env, jvalue))
471 : NULL); 479 : NULL);
472 480
473 try 481 try
474 { 482 {
475 engine->SetAllowedConnectionType(value); 483 engine->SetAllowedConnectionType(value);
476 } 484 }
477 CATCH_AND_THROW(env) 485 CATCH_AND_THROW(env)
478 } 486 }
479 487
480 static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl ong ptr) 488 static jstring JNICALL JniGetAllowedConnectionType(JNIEnv* env, jclass clazz, jl ong ptr)
481 { 489 {
482 try 490 try
483 { 491 {
484 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 492 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
485 std::unique_ptr<std::string> value = engine->GetAllowedConnectionType(); 493 std::unique_ptr<std::string> value = engine->GetAllowedConnectionType();
486 494
487 if (value == NULL) 495 if (value == NULL)
488 { 496 {
489 return NULL; 497 return NULL;
490 } 498 }
491 499
492 return JniStdStringToJava(env, *value.get()); 500 return JniStdStringToJava(env, *value.get());
493 } 501 }
494 CATCH_THROW_AND_RETURN(env, 0) 502 CATCH_THROW_AND_RETURN(env, 0)
495 } 503 }
496 504
497 static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue) 505 static void JNICALL JniSetAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlong ptr, jboolean jvalue)
498 { 506 {
499 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::FilterEn ginePtr>(ptr); 507 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
500 508
501 try 509 try
502 { 510 {
503 engine->SetAAEnabled(jvalue == JNI_TRUE); 511 engine->SetAAEnabled(jvalue == JNI_TRUE);
504 } 512 }
505 CATCH_AND_THROW(env) 513 CATCH_AND_THROW(env)
506 } 514 }
507 515
508 static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo ng ptr) 516 static jboolean JNICALL JniIsAcceptableAdsEnabled(JNIEnv* env, jclass clazz, jlo ng ptr)
509 { 517 {
510 try 518 try
511 { 519 {
512 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 520 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
513 return engine->IsAAEnabled() ? JNI_TRUE : JNI_FALSE; 521 return engine->IsAAEnabled() ? JNI_TRUE : JNI_FALSE;
514 } 522 }
515 CATCH_THROW_AND_RETURN(env, 0) 523 CATCH_THROW_AND_RETURN(env, 0)
516 } 524 }
517 525
518 static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl azz, jlong ptr) 526 static jstring JNICALL JniGetAcceptableAdsSubscriptionURL(JNIEnv* env, jclass cl azz, jlong ptr)
519 { 527 {
520 try 528 try
521 { 529 {
522 AdblockPlus::FilterEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::Filter EnginePtr>(ptr); 530 AdblockPlus::FilterEnginePtr& engine = GetFilterEnginePtrRef(ptr);
523 std::string url = engine->GetAAUrl(); 531 std::string url = engine->GetAAUrl();
524 return JniStdStringToJava(env, url); 532 return JniStdStringToJava(env, url);
525 } 533 }
526 CATCH_THROW_AND_RETURN(env, 0) 534 CATCH_THROW_AND_RETURN(env, 0)
527 } 535 }
528 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
529 static JNINativeMethod methods[] = 560 static JNINativeMethod methods[] =
530 { 561 {
531 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor }, 562 { (char*)"ctor", (char*)"(JJ)J", (void*)JniCtor },
532 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, 563 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun },
533 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter }, 564 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter },
534 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters }, 565 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters },
535 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription }, 566 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription },
536 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh owNextNotification }, 567 { (char*)"showNextNotification", (char*)"(JLjava/lang/String;)V", (void*)JniSh owNextNotification },
537 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif icationCallback }, 568 { (char*)"setShowNotificationCallback", (char*)"(JJ)V", (void*)JniSetShowNotif icationCallback },
538 { (char*)"removeShowNotificationCallback", (char*)"(J)V", (void*)JniRemoveShow NotificationCallback }, 569 { (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 }, 580 { (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 }, 581 { (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 }, 582 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref },
552 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, 583 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref },
553 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", ( void*)JniGetHostFromURL }, 584 { (char*)"getHostFromURL", (char*)"(JLjava/lang/String;)Ljava/lang/String;", ( void*)JniGetHostFromURL },
554 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J niSetAllowedConnectionType }, 585 { (char*)"setAllowedConnectionType", (char*)"(JLjava/lang/String;)V", (void*)J niSetAllowedConnectionType },
555 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn iGetAllowedConnectionType }, 586 { (char*)"getAllowedConnectionType", (char*)"(J)Ljava/lang/String;", (void*)Jn iGetAllowedConnectionType },
556 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled }, 587 { (char*)"setAcceptableAdsEnabled", (char*)"(JZ)V", (void*)JniSetAcceptableAds Enabled },
557 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled }, 588 { (char*)"isAcceptableAdsEnabled", (char*)"(J)Z", (void*)JniIsAcceptableAdsEna bled },
558 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL }, 589 { (char*)"getAcceptableAdsSubscriptionURL", (char*)"(J)Ljava/lang/String;", (v oid*)JniGetAcceptableAdsSubscriptionURL },
559 { (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 }
560 }; 592 };
561 593
562 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)
563 { 595 {
564 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); 596 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
565 } 597 }
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