LEFT | RIGHT |
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 |
(...skipping 19 matching lines...) Expand all Loading... |
30 import org.adblockplus.libadblockplus.Filter; | 30 import org.adblockplus.libadblockplus.Filter; |
31 import org.adblockplus.libadblockplus.FilterChangeCallback; | 31 import org.adblockplus.libadblockplus.FilterChangeCallback; |
32 import org.adblockplus.libadblockplus.FilterEngine; | 32 import org.adblockplus.libadblockplus.FilterEngine; |
33 import org.adblockplus.libadblockplus.FilterEngine.ContentType; | 33 import org.adblockplus.libadblockplus.FilterEngine.ContentType; |
34 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; | 34 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
35 import org.adblockplus.libadblockplus.JsValue; | 35 import org.adblockplus.libadblockplus.JsValue; |
36 import org.adblockplus.libadblockplus.LogSystem; | 36 import org.adblockplus.libadblockplus.LogSystem; |
37 import org.adblockplus.libadblockplus.Platform; | 37 import org.adblockplus.libadblockplus.Platform; |
38 import org.adblockplus.libadblockplus.ShowNotificationCallback; | 38 import org.adblockplus.libadblockplus.ShowNotificationCallback; |
39 import org.adblockplus.libadblockplus.Subscription; | 39 import org.adblockplus.libadblockplus.Subscription; |
40 import org.adblockplus.libadblockplus.UpdateAvailableCallback; | |
41 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; | |
42 import org.adblockplus.libadblockplus.Updater; | |
43 import org.adblockplus.libadblockplus.WebRequest; | 40 import org.adblockplus.libadblockplus.WebRequest; |
44 | 41 |
45 import android.content.Context; | 42 import android.content.Context; |
46 import android.content.pm.PackageInfo; | 43 import android.content.pm.PackageInfo; |
47 import android.content.pm.PackageManager; | 44 import android.content.pm.PackageManager; |
48 import android.os.Build.VERSION; | 45 import android.os.Build.VERSION; |
49 import android.util.Log; | 46 import android.util.Log; |
50 | 47 |
51 public final class AdblockEngine | 48 public final class AdblockEngine |
52 { | 49 { |
53 // default base path to store subscription files in android app | 50 // default base path to store subscription files in android app |
54 public static final String BASE_PATH_DIRECTORY = "adblock"; | 51 public static final String BASE_PATH_DIRECTORY = "adblock"; |
55 | 52 |
56 private static final String TAG = Utils.getTag(AdblockEngine.class); | 53 private static final String TAG = Utils.getTag(AdblockEngine.class); |
57 | 54 |
58 /* | 55 /* |
59 * The fields below are volatile because: | 56 * The fields below are volatile because: |
60 * | 57 * |
61 * I encountered JNI related bugs/crashes caused by JNI backed Java objects. I
t seemed that under | 58 * I encountered JNI related bugs/crashes caused by JNI backed Java objects. I
t seemed that under |
62 * certain conditions the objects were optimized away which resulted in crashe
s when trying to | 59 * certain conditions the objects were optimized away which resulted in crashe
s when trying to |
63 * release the object, sometimes even on access. | 60 * release the object, sometimes even on access. |
64 * | 61 * |
65 * The only solution that really worked was to declare the variables holding t
he references | 62 * The only solution that really worked was to declare the variables holding t
he references |
66 * volatile, this seems to prevent the JNI from 'optimizing away' those object
s (as a volatile | 63 * volatile, this seems to prevent the JNI from 'optimizing away' those object
s (as a volatile |
67 * variable might be changed at any time from any thread). | 64 * variable might be changed at any time from any thread). |
68 */ | 65 */ |
69 private volatile Platform platform; | 66 private volatile Platform platform; |
70 private volatile FilterEngine filterEngine; | 67 private volatile FilterEngine filterEngine; |
71 private volatile Updater updater; | |
72 private volatile LogSystem logSystem; | 68 private volatile LogSystem logSystem; |
73 private volatile WebRequest webRequest; | 69 private volatile WebRequest webRequest; |
74 private volatile UpdateAvailableCallback updateAvailableCallback; | |
75 private volatile UpdateCheckDoneCallback updateCheckDoneCallback; | |
76 private volatile FilterChangeCallback filterChangeCallback; | 70 private volatile FilterChangeCallback filterChangeCallback; |
77 private volatile ShowNotificationCallback showNotificationCallback; | 71 private volatile ShowNotificationCallback showNotificationCallback; |
78 private volatile boolean elemhideEnabled; | 72 private volatile boolean elemhideEnabled; |
79 private volatile boolean enabled = true; | 73 private volatile boolean enabled = true; |
80 private volatile List<String> whitelistedDomains; | 74 private volatile List<String> whitelistedDomains; |
81 | 75 |
82 public static AppInfo generateAppInfo(final Context context, boolean developme
ntBuild, | 76 public static AppInfo generateAppInfo(final Context context, boolean developme
ntBuild, |
83 String application, String applicationVe
rsion) | 77 String application, String applicationVe
rsion) |
84 { | 78 { |
85 final String sdkVersion = String.valueOf(VERSION.SDK_INT); | 79 final String sdkVersion = String.valueOf(VERSION.SDK_INT); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 164 |
171 public Builder setIsAllowedConnectionCallback(IsAllowedConnectionCallback ca
llback) | 165 public Builder setIsAllowedConnectionCallback(IsAllowedConnectionCallback ca
llback) |
172 { | 166 { |
173 this.isAllowedConnectionCallback = callback; | 167 this.isAllowedConnectionCallback = callback; |
174 return this; | 168 return this; |
175 } | 169 } |
176 | 170 |
177 public Builder useV8IsolateProvider(long v8IsolateProviderPtr) | 171 public Builder useV8IsolateProvider(long v8IsolateProviderPtr) |
178 { | 172 { |
179 this.v8IsolateProviderPtr = v8IsolateProviderPtr; | 173 this.v8IsolateProviderPtr = v8IsolateProviderPtr; |
180 return this; | |
181 } | |
182 | |
183 public Builder setUpdateAvailableCallback(UpdateAvailableCallback callback) | |
184 { | |
185 engine.updateAvailableCallback = callback; | |
186 return this; | |
187 } | |
188 | |
189 public Builder setUpdateCheckDoneCallback(UpdateCheckDoneCallback callback) | |
190 { | |
191 engine.updateCheckDoneCallback = callback; | |
192 return this; | 174 return this; |
193 } | 175 } |
194 | 176 |
195 public Builder setShowNotificationCallback(ShowNotificationCallback callback
) | 177 public Builder setShowNotificationCallback(ShowNotificationCallback callback
) |
196 { | 178 { |
197 engine.showNotificationCallback = callback; | 179 engine.showNotificationCallback = callback; |
198 return this; | 180 return this; |
199 } | 181 } |
200 | 182 |
201 public Builder setFilterChangeCallback(FilterChangeCallback callback) | 183 public Builder setFilterChangeCallback(FilterChangeCallback callback) |
(...skipping 23 matching lines...) Expand all Loading... |
225 } | 207 } |
226 } | 208 } |
227 }); | 209 }); |
228 | 210 |
229 engine.webRequest = wrapper; | 211 engine.webRequest = wrapper; |
230 } | 212 } |
231 } | 213 } |
232 | 214 |
233 private void initCallbacks() | 215 private void initCallbacks() |
234 { | 216 { |
235 if (engine.updater != null && engine.updateAvailableCallback != null) | |
236 { | |
237 engine.updater.setUpdateAvailableCallback(engine.updateAvailableCallback
); | |
238 } | |
239 | |
240 if (engine.showNotificationCallback != null) | 217 if (engine.showNotificationCallback != null) |
241 { | 218 { |
242 engine.filterEngine.setShowNotificationCallback(engine.showNotificationC
allback); | 219 engine.filterEngine.setShowNotificationCallback(engine.showNotificationC
allback); |
243 } | 220 } |
244 | 221 |
245 if (engine.filterChangeCallback != null) | 222 if (engine.filterChangeCallback != null) |
246 { | 223 { |
247 engine.filterEngine.setFilterChangeCallback(engine.filterChangeCallback)
; | 224 engine.filterEngine.setFilterChangeCallback(engine.filterChangeCallback)
; |
248 } | 225 } |
249 } | 226 } |
250 | 227 |
251 public AdblockEngine build() | 228 public AdblockEngine build() |
252 { | 229 { |
253 return build(false); | |
254 } | |
255 | |
256 public AdblockEngine buildWithUpdateCheck() | |
257 { | |
258 return build(true); | |
259 } | |
260 | |
261 private AdblockEngine build(boolean updateCheckEnabled) | |
262 { | |
263 initRequests(); | 230 initRequests(); |
264 | 231 |
265 // webRequest should be ready to be used passed right after JsEngine is cr
eated | 232 // webRequest should be ready to be used passed right after JsEngine is cr
eated |
266 createEngines(updateCheckEnabled); | 233 createEngines(); |
267 | 234 |
268 initCallbacks(); | 235 initCallbacks(); |
269 | 236 |
270 if (!engine.elemhideEnabled) | 237 if (!engine.elemhideEnabled) |
271 { | 238 { |
272 androidWebRequest.updateSubscriptionURLs(engine.filterEngine); | 239 androidWebRequest.updateSubscriptionURLs(engine.filterEngine); |
273 } | 240 } |
274 | 241 |
275 return engine; | 242 return engine; |
276 } | 243 } |
277 | 244 |
278 private void createEngines(boolean updateCheckEnabled) | 245 private void createEngines() |
279 { | 246 { |
280 engine.logSystem = new AndroidLogSystem(); | 247 engine.logSystem = new AndroidLogSystem(); |
281 engine.platform = new Platform(engine.logSystem, engine.webRequest, basePa
th); | 248 engine.platform = new Platform(engine.logSystem, engine.webRequest, basePa
th); |
282 if (v8IsolateProviderPtr != null) | 249 if (v8IsolateProviderPtr != null) |
283 { | 250 { |
284 engine.platform.setUpJsEngine(appInfo, v8IsolateProviderPtr); | 251 engine.platform.setUpJsEngine(appInfo, v8IsolateProviderPtr); |
285 } | 252 } |
286 else | 253 else |
287 { | 254 { |
288 engine.platform.setUpJsEngine(appInfo); | 255 engine.platform.setUpJsEngine(appInfo); |
289 } | 256 } |
290 engine.platform.setUpFilterEngine(isAllowedConnectionCallback); | 257 engine.platform.setUpFilterEngine(isAllowedConnectionCallback); |
291 engine.filterEngine = engine.platform.getFilterEngine(); | 258 engine.filterEngine = engine.platform.getFilterEngine(); |
292 if (updateCheckEnabled) | |
293 { | |
294 engine.updater = engine.platform.getUpdater(); | |
295 } | |
296 } | 259 } |
297 } | 260 } |
298 | 261 |
299 public static Builder builder(AppInfo appInfo, String basePath) | 262 public static Builder builder(AppInfo appInfo, String basePath) |
300 { | 263 { |
301 return new Builder(appInfo, basePath); | 264 return new Builder(appInfo, basePath); |
302 } | 265 } |
303 | 266 |
304 public void dispose() | 267 public void dispose() |
305 { | 268 { |
306 Log.w(TAG, "Dispose"); | 269 Log.w(TAG, "Dispose"); |
307 | |
308 if (this.updater != null) | |
309 { | |
310 if (this.updateAvailableCallback != null) | |
311 { | |
312 this.updater.removeUpdateAvailableCallback(); | |
313 } | |
314 } | |
315 | 270 |
316 // engines first | 271 // engines first |
317 if (this.filterEngine != null) | 272 if (this.filterEngine != null) |
318 { | 273 { |
| 274 |
319 if (this.filterChangeCallback != null) | 275 if (this.filterChangeCallback != null) |
320 { | 276 { |
321 this.filterEngine.removeFilterChangeCallback(); | 277 this.filterEngine.removeFilterChangeCallback(); |
322 } | 278 } |
323 | 279 |
324 if (this.showNotificationCallback != null) | 280 if (this.showNotificationCallback != null) |
325 { | 281 { |
326 this.filterEngine.removeShowNotificationCallback(); | 282 this.filterEngine.removeShowNotificationCallback(); |
327 } | 283 } |
328 | 284 |
329 this.platform.dispose(); | 285 this.platform.dispose(); |
330 this.platform = null; | 286 this.platform = null; |
331 } | 287 } |
332 | 288 |
333 // callbacks then | 289 // callbacks then |
334 if (this.updateAvailableCallback != null) | |
335 { | |
336 this.updateAvailableCallback.dispose(); | |
337 this.updateAvailableCallback = null; | |
338 } | |
339 | |
340 if (this.updateCheckDoneCallback != null) | |
341 { | |
342 this.updateCheckDoneCallback.dispose(); | |
343 this.updateCheckDoneCallback = null; | |
344 } | |
345 | |
346 if (this.filterChangeCallback != null) | 290 if (this.filterChangeCallback != null) |
347 { | 291 { |
348 this.filterChangeCallback.dispose(); | 292 this.filterChangeCallback.dispose(); |
349 this.filterChangeCallback = null; | 293 this.filterChangeCallback = null; |
350 } | 294 } |
351 | 295 |
352 if (this.showNotificationCallback != null) | 296 if (this.showNotificationCallback != null) |
353 { | 297 { |
354 this.showNotificationCallback.dispose(); | 298 this.showNotificationCallback.dispose(); |
355 this.showNotificationCallback = null; | 299 this.showNotificationCallback = null; |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
644 || !this.elemhideEnabled | 588 || !this.elemhideEnabled |
645 || this.isDomainWhitelisted(url, referrerChainArray) | 589 || this.isDomainWhitelisted(url, referrerChainArray) |
646 || this.isDocumentWhitelisted(url, referrerChainArray) | 590 || this.isDocumentWhitelisted(url, referrerChainArray) |
647 || this.isElemhideWhitelisted(url, referrerChainArray)) | 591 || this.isElemhideWhitelisted(url, referrerChainArray)) |
648 { | 592 { |
649 return new ArrayList<String>(); | 593 return new ArrayList<String>(); |
650 } | 594 } |
651 return this.filterEngine.getElementHidingSelectors(domain); | 595 return this.filterEngine.getElementHidingSelectors(domain); |
652 } | 596 } |
653 | 597 |
654 public void checkForUpdates() | |
655 { | |
656 if (updater == null) throw new IllegalStateException(); | |
657 this.updater.forceUpdateCheck(this.updateCheckDoneCallback); | |
658 } | |
659 | |
660 public FilterEngine getFilterEngine() | 598 public FilterEngine getFilterEngine() |
661 { | 599 { |
662 return this.filterEngine; | 600 return this.filterEngine; |
663 } | 601 } |
664 | 602 |
665 public void setWhitelistedDomains(List<String> domains) | 603 public void setWhitelistedDomains(List<String> domains) |
666 { | 604 { |
667 this.whitelistedDomains = domains; | 605 this.whitelistedDomains = domains; |
668 } | 606 } |
669 | 607 |
670 public List<String> getWhitelistedDomains() | 608 public List<String> getWhitelistedDomains() |
671 { | 609 { |
672 return whitelistedDomains; | 610 return whitelistedDomains; |
673 } | 611 } |
674 } | 612 } |
LEFT | RIGHT |