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

Side by Side Diff: libadblockplus-android/src/org/adblockplus/libadblockplus/android/AdblockEngine.java

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 | « libadblockplus-android/src/org/adblockplus/libadblockplus/FilterEngine.java ('k') | no next file » | 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
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 195
196 private void initRequests() 196 private void initRequests()
197 { 197 {
198 androidWebRequest = new AndroidWebRequest(engine.elemhideEnabled, true); 198 androidWebRequest = new AndroidWebRequest(engine.elemhideEnabled, true);
199 engine.webRequest = androidWebRequest; 199 engine.webRequest = androidWebRequest;
200 200
201 if (urlToResourceIdMap != null) 201 if (urlToResourceIdMap != null)
202 { 202 {
203 AndroidWebRequestResourceWrapper wrapper = new AndroidWebRequestResource Wrapper( 203 AndroidWebRequestResourceWrapper wrapper = new AndroidWebRequestResource Wrapper(
204 context, engine.webRequest, urlToResourceIdMap, resourceStorage); 204 context, engine.webRequest, urlToResourceIdMap, resourceStorage);
205 wrapper.setListener(engine.resourceWrapperListener); 205 wrapper.setListener(new AndroidWebRequestResourceWrapper.Listener()
206 {
207 private AdblockEngine adblockEngine;
208
209 AndroidWebRequestResourceWrapper.Listener init(AdblockEngine adblockEn gine)
210 {
211 this.adblockEngine = adblockEngine;
212 return this;
213 }
214
215 @Override
216 public void onIntercepted(String url, int resourceId)
217 {
218 Log.d(TAG, "Force subscription update for intercepted URL " + url);
219 if (this.adblockEngine.filterEngine != null)
220 {
221 this.adblockEngine.filterEngine.updateFiltersAsync(url);
222 }
223 }
224 }.init(this.engine));
206 225
207 engine.webRequest = wrapper; 226 engine.webRequest = wrapper;
208 } 227 }
209 } 228 }
210 229
211 private void initCallbacks() 230 private void initCallbacks()
212 { 231 {
213 if (engine.updateAvailableCallback != null) 232 if (engine.updateAvailableCallback != null)
214 { 233 {
215 engine.filterEngine.setUpdateAvailableCallback(engine.updateAvailableCal lback); 234 engine.filterEngine.setUpdateAvailableCallback(engine.updateAvailableCal lback);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 271
253 engine.filterEngine = new FilterEngine(engine.jsEngine, isAllowedConnectio nCallback); 272 engine.filterEngine = new FilterEngine(engine.jsEngine, isAllowedConnectio nCallback);
254 } 273 }
255 } 274 }
256 275
257 public static Builder builder(AppInfo appInfo, String basePath) 276 public static Builder builder(AppInfo appInfo, String basePath)
258 { 277 {
259 return new Builder(appInfo, basePath); 278 return new Builder(appInfo, basePath);
260 } 279 }
261 280
262 private final AndroidWebRequestResourceWrapper.Listener resourceWrapperListene r =
263 new AndroidWebRequestResourceWrapper.Listener()
264 {
265 private static final int UPDATE_DELAY_MS = 1 * 1000;
266
267 private final Handler handler = new Handler(Looper.getMainLooper());
268
269 private final Runnable forceUpdateRunnable = new Runnable()
270 {
271 public void run() {
272 // Filter Engine can be already disposed
273 if (filterEngine != null)
274 {
275 Log.d(TAG, "Force update subscriptions");
276 AdblockEngine.this.updateSubscriptions();
277 }
278 }
279 };
280
281 @Override
282 public void onIntercepted(String url, int resourceId)
283 {
284 // we need to force update subscriptions ASAP after preloaded one is retur ned
285 // but we should note that multiple interceptions (for main easylist and A A) and force update once only
286
287 // adding into main thread queue to avoid concurrency issues (start update while updating)
288 // as usually onIntercepted() is invoked in background thread
289 handler.removeCallbacks(forceUpdateRunnable);
290 handler.postDelayed(forceUpdateRunnable, UPDATE_DELAY_MS);
291
292 Log.d(TAG, "Scheduled force update in " + UPDATE_DELAY_MS);
293 }
294 };
295
296 public void dispose() 281 public void dispose()
297 { 282 {
298 Log.w(TAG, "Dispose"); 283 Log.w(TAG, "Dispose");
299 284
300 // engines first 285 // engines first
301 if (this.filterEngine != null) 286 if (this.filterEngine != null)
302 { 287 {
303 if (this.updateAvailableCallback != null) 288 if (this.updateAvailableCallback != null)
304 { 289 {
305 this.filterEngine.removeUpdateAvailableCallback(); 290 this.filterEngine.removeUpdateAvailableCallback();
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 sub.addToList(); 473 sub.addToList();
489 } 474 }
490 finally 475 finally
491 { 476 {
492 sub.dispose(); 477 sub.dispose();
493 } 478 }
494 } 479 }
495 } 480 }
496 } 481 }
497 482
498 public void updateSubscriptions()
499 {
500 for (final Subscription s : this.filterEngine.getListedSubscriptions())
501 {
502 try
503 {
504 s.updateFilters();
505 }
506 finally
507 {
508 s.dispose();
509 }
510 }
511 }
512
513 public void setEnabled(final boolean enabled) 483 public void setEnabled(final boolean enabled)
514 { 484 {
515 this.enabled = enabled; 485 this.enabled = enabled;
516 } 486 }
517 487
518 public boolean isEnabled() 488 public boolean isEnabled()
519 { 489 {
520 return enabled; 490 return enabled;
521 } 491 }
522 492
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 public void setWhitelistedDomains(List<String> domains) 640 public void setWhitelistedDomains(List<String> domains)
671 { 641 {
672 this.whitelistedDomains = domains; 642 this.whitelistedDomains = domains;
673 } 643 }
674 644
675 public List<String> getWhitelistedDomains() 645 public List<String> getWhitelistedDomains()
676 { 646 {
677 return whitelistedDomains; 647 return whitelistedDomains;
678 } 648 }
679 } 649 }
OLDNEW
« no previous file with comments | « libadblockplus-android/src/org/adblockplus/libadblockplus/FilterEngine.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld