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

Delta Between Two Patch Sets: libadblockplus-android/src/org/adblockplus/libadblockplus/android/AdblockEngine.java

Issue 29389555: Issue 5010 - Allow to preload subscription files (Closed)
Left Patch Set: Created March 20, 2017, 8:11 a.m.
Right Patch Set: force downloading actually Created March 30, 2017, 10:12 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 import android.content.Context; 42 import android.content.Context;
43 import android.content.pm.PackageInfo; 43 import android.content.pm.PackageInfo;
44 import android.content.pm.PackageManager.NameNotFoundException; 44 import android.content.pm.PackageManager.NameNotFoundException;
45 import android.os.Build.VERSION; 45 import android.os.Build.VERSION;
46 import android.os.Handler; 46 import android.os.Handler;
47 import android.os.Looper; 47 import android.os.Looper;
48 import android.util.Log; 48 import android.util.Log;
49 49
50 public final class AdblockEngine 50 public final class AdblockEngine
51 { 51 {
52 // default base path to store subscription files in android app
53 public static final String BASE_PATH_DIRECTORY = "adblock";
54
52 private static final String TAG = Utils.getTag(AdblockEngine.class); 55 private static final String TAG = Utils.getTag(AdblockEngine.class);
53 56
54 /* 57 /*
55 * The fields below are volatile because: 58 * The fields below are volatile because:
56 * 59 *
57 * I encountered JNI related bugs/crashes caused by JNI backed Java objects. I t seemed that under 60 * I encountered JNI related bugs/crashes caused by JNI backed Java objects. I t seemed that under
58 * certain conditions the objects were optimized away which resulted in crashe s when trying to 61 * certain conditions the objects were optimized away which resulted in crashe s when trying to
59 * release the object, sometimes even on access. 62 * release the object, sometimes even on access.
60 * 63 *
61 * The only solution that really worked was to declare the variables holding t he references 64 * The only solution that really worked was to declare the variables holding t he references
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 .setVersion(version) 98 .setVersion(version)
96 .setApplicationVersion(sdkVersion) 99 .setApplicationVersion(sdkVersion)
97 .setLocale(locale) 100 .setLocale(locale)
98 .setDevelopmentBuild(developmentBuild) 101 .setDevelopmentBuild(developmentBuild)
99 .build(); 102 .build();
100 } 103 }
101 104
102 /** 105 /**
103 * Builds Adblock engine 106 * Builds Adblock engine
104 */ 107 */
105 public static class Builder 108 public static class Builder
anton 2017/03/20 08:28:45 I had to refactor this monster-ctor to builder as
diegocarloslima 2017/03/20 15:13:12 I liked the Builder approach, but as I pointed out
anton 2017/03/21 05:54:47 the issues are related by the code (the same file
diegocarloslima 2017/03/28 13:47:15 You're right, the two issues don't depend on each
106 { 109 {
107 private Context context; 110 private Context context;
108 private Map<String, Integer> URLtoResourceIdMap; 111 private Map<String, Integer> URLtoResourceIdMap;
109 private AndroidWebRequestResourceWrapper.Storage resourceStorage; 112 private AndroidWebRequestResourceWrapper.Storage resourceStorage;
110 private AndroidWebRequest androidWebRequest; 113 private AndroidWebRequest androidWebRequest;
111 private AppInfo appInfo; 114 private AppInfo appInfo;
112 private String basePath; 115 private String basePath;
113 116
114 private AdblockEngine engine; 117 private AdblockEngine engine;
115 118
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 { 198 {
196 engine.filterEngine.setFilterChangeCallback(engine.filterChangeCallback) ; 199 engine.filterEngine.setFilterChangeCallback(engine.filterChangeCallback) ;
197 } 200 }
198 } 201 }
199 202
200 public AdblockEngine build() 203 public AdblockEngine build()
201 { 204 {
202 initRequests(); 205 initRequests();
203 206
204 // webRequest should be ready to be used passed right after JsEngine is cr eated 207 // webRequest should be ready to be used passed right after JsEngine is cr eated
205 createEngines(); 208 createEngines();
anton 2017/03/20 08:28:45 this is completely strange that webRequest should
206 209
207 initCallbacks(); 210 initCallbacks();
208 211
209 androidWebRequest.updateSubscriptionURLs(engine.filterEngine); 212 androidWebRequest.updateSubscriptionURLs(engine.filterEngine);
210 213
211 return engine; 214 return engine;
212 } 215 }
213 216
214 private void createEngines() 217 private void createEngines()
215 { 218 {
216 engine.jsEngine = new JsEngine(appInfo); 219 engine.jsEngine = new JsEngine(appInfo);
217 engine.jsEngine.setDefaultFileSystem(basePath); 220 engine.jsEngine.setDefaultFileSystem(basePath);
218 221
219 engine.jsEngine.setWebRequest(engine.webRequest); 222 engine.jsEngine.setWebRequest(engine.webRequest);
220 223
221 engine.logSystem = new AndroidLogSystem(); 224 engine.logSystem = new AndroidLogSystem();
222 engine.jsEngine.setLogSystem(engine.logSystem); 225 engine.jsEngine.setLogSystem(engine.logSystem);
223 226
224 engine.filterEngine = new FilterEngine(engine.jsEngine); 227 engine.filterEngine = new FilterEngine(engine.jsEngine);
225 } 228 }
226 } 229 }
227 230
228 public static Builder builder(AppInfo appInfo, String basePath) 231 public static Builder builder(AppInfo appInfo, String basePath)
anton 2017/03/20 08:28:45 i used the same approach (`builder()`) instead of
229 { 232 {
230 return new Builder(appInfo, basePath); 233 return new Builder(appInfo, basePath);
231 } 234 }
232 235
233 private AndroidWebRequestResourceWrapper.Listener resourceWrapperListener = 236 private final AndroidWebRequestResourceWrapper.Listener resourceWrapperListene r =
234 new AndroidWebRequestResourceWrapper.Listener() 237 new AndroidWebRequestResourceWrapper.Listener()
235 { 238 {
236 private static final int UPDATE_DELAY_MS = 1 * 1000; 239 private static final int UPDATE_DELAY_MS = 1 * 1000;
237 240
238 private Handler handler = new Handler(Looper.getMainLooper()); 241 private final Handler handler = new Handler(Looper.getMainLooper());
239 242
240 private Runnable forceUpdateRunnable = new Runnable() 243 private final Runnable forceUpdateRunnable = new Runnable()
241 { 244 {
242 public void run() { 245 public void run() {
243 // Filter Engine can be already disposed 246 // Filter Engine can be already disposed
244 if (filterEngine != null) 247 if (filterEngine != null)
245 { 248 {
246 Log.d(TAG, "Force update subscriptions"); 249 Log.d(TAG, "Force update subscriptions");
247 filterEngine.forceUpdateCheck(updateCheckDoneCallback); 250 List<Subscription> subscriptions = filterEngine.getListedSubscriptions ();
251 for (Subscription eachSubscription : subscriptions)
sergei 2017/03/30 22:14:37 Nit: I would remove "each".
252 {
253 try
254 {
255 eachSubscription.updateFilters();
256 }
257 finally
258 {
259 eachSubscription.dispose();
260 }
261 }
248 } 262 }
249 } 263 }
250 }; 264 };
251 265
252 @Override 266 @Override
253 public void onIntercepted(String url, int resourceId) 267 public void onIntercepted(String url, int resourceId)
254 { 268 {
255 // we need to force update subscriptions ASAP after preloaded one is retur ned 269 // we need to force update subscriptions ASAP after preloaded one is retur ned
anton 2017/03/20 08:28:45 Please think about it while reviewing!
diegocarloslima 2017/03/28 13:47:15 Is this onIntercepted() callback always called by
anton 2017/03/28 13:59:06 It's invoked in background thread (most likely) an
256 // but we should note that multiple interceptions (for main easylist and A A) and force update once only 270 // but we should note that multiple interceptions (for main easylist and A A) and force update once only
257 271
258 // adding into main thread queue to avoid concurrency issues (start update while updating) 272 // adding into main thread queue to avoid concurrency issues (start update while updating)
259 // as usually onIntercepted() is invoked in background thread 273 // as usually onIntercepted() is invoked in background thread
260 handler.removeCallbacks(forceUpdateRunnable); 274 handler.removeCallbacks(forceUpdateRunnable);
261 handler.postDelayed(forceUpdateRunnable, UPDATE_DELAY_MS); 275 handler.postDelayed(forceUpdateRunnable, UPDATE_DELAY_MS);
262 276
263 Log.d(TAG, "Scheduled force update in " + UPDATE_DELAY_MS); 277 Log.d(TAG, "Scheduled force update in " + UPDATE_DELAY_MS);
264 } 278 }
265 }; 279 };
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 public void setWhitelistedDomains(List<String> domains) 589 public void setWhitelistedDomains(List<String> domains)
576 { 590 {
577 this.whitelistedDomains = domains; 591 this.whitelistedDomains = domains;
578 } 592 }
579 593
580 public List<String> getWhitelistedDomains() 594 public List<String> getWhitelistedDomains()
581 { 595 {
582 return whitelistedDomains; 596 return whitelistedDomains;
583 } 597 }
584 } 598 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld