| Left: | ||
| Right: |
| 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-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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 | 52 |
| 53 private Context context; | 53 private Context context; |
| 54 private String basePath; | 54 private String basePath; |
| 55 private boolean developmentBuild; | 55 private boolean developmentBuild; |
| 56 private String settingsPreferenceName; | 56 private String settingsPreferenceName; |
| 57 private String preloadedPreferenceName; | 57 private String preloadedPreferenceName; |
| 58 private Map<String, Integer> urlToResourceIdMap; | 58 private Map<String, Integer> urlToResourceIdMap; |
| 59 private AdblockEngine engine; | 59 private AdblockEngine engine; |
| 60 private AdblockSettingsStorage storage; | 60 private AdblockSettingsStorage storage; |
| 61 private CountDownLatch engineCreated; | 61 private CountDownLatch engineCreated; |
| 62 private Long v8IsolatePtr; | |
| 62 | 63 |
| 63 /* | 64 /* |
| 64 Simple ARC management for AdblockEngine | 65 Simple ARC management for AdblockEngine |
| 65 Use `retain` and `release` | 66 Use `retain` and `release` |
| 66 */ | 67 */ |
| 67 | 68 |
| 68 private AtomicInteger referenceCounter = new AtomicInteger(0); | 69 private AtomicInteger referenceCounter = new AtomicInteger(0); |
| 69 | 70 |
| 70 // singleton | 71 // singleton |
| 71 protected AdblockHelper() | 72 protected AdblockHelper() |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 * @param preferenceName Shared Preferences name to store intercepted requests stats | 125 * @param preferenceName Shared Preferences name to store intercepted requests stats |
| 125 * @param urlToResourceIdMap | 126 * @param urlToResourceIdMap |
| 126 */ | 127 */ |
| 127 public AdblockHelper preloadSubscriptions(String preferenceName, Map<String, I nteger> urlToResourceIdMap) | 128 public AdblockHelper preloadSubscriptions(String preferenceName, Map<String, I nteger> urlToResourceIdMap) |
| 128 { | 129 { |
| 129 this.preloadedPreferenceName = preferenceName; | 130 this.preloadedPreferenceName = preferenceName; |
| 130 this.urlToResourceIdMap = urlToResourceIdMap; | 131 this.urlToResourceIdMap = urlToResourceIdMap; |
| 131 return this; | 132 return this; |
| 132 } | 133 } |
| 133 | 134 |
| 135 public void useV8Isolate(long ptr) | |
| 136 { | |
| 137 this.v8IsolatePtr = ptr; | |
| 138 } | |
| 139 | |
| 134 private void createAdblock() | 140 private void createAdblock() |
| 135 { | 141 { |
| 136 ConnectivityManager connectivityManager = | 142 ConnectivityManager connectivityManager = |
| 137 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); | 143 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); |
| 138 IsAllowedConnectionCallback isAllowedConnectionCallback = new IsAllowedConne ctionCallbackImpl(connectivityManager); | 144 IsAllowedConnectionCallback isAllowedConnectionCallback = new IsAllowedConne ctionCallbackImpl(connectivityManager); |
| 139 | 145 |
| 140 Log.d(TAG, "Creating adblock engine ..."); | 146 Log.d(TAG, "Creating adblock engine ..."); |
| 141 | 147 |
| 142 // read and apply current settings | 148 // read and apply current settings |
| 143 SharedPreferences settingsPrefs = context.getSharedPreferences( | 149 SharedPreferences settingsPrefs = context.getSharedPreferences( |
| 144 settingsPreferenceName, | 150 settingsPreferenceName, |
| 145 Context.MODE_PRIVATE); | 151 Context.MODE_PRIVATE); |
| 146 storage = new SharedPrefsStorage(settingsPrefs); | 152 storage = new SharedPrefsStorage(settingsPrefs); |
| 147 | 153 |
| 148 AdblockEngine.Builder builder = AdblockEngine | 154 AdblockEngine.Builder builder = AdblockEngine |
| 149 .builder( | 155 .builder( |
| 150 AdblockEngine.generateAppInfo(context, developmentBuild), | 156 AdblockEngine.generateAppInfo(context, developmentBuild), |
| 151 basePath) | 157 basePath) |
| 152 .setIsAllowedConnectionCallback(isAllowedConnectionCallback) | 158 .setIsAllowedConnectionCallback(isAllowedConnectionCallback) |
| 153 .enableElementHiding(true); | 159 .enableElementHiding(true); |
| 154 | 160 |
| 161 if (v8IsolatePtr != null) | |
| 162 { | |
| 163 builder.useV8Isolate(v8IsolatePtr); | |
| 164 } | |
| 165 | |
| 155 // if preloaded subscriptions provided | 166 // if preloaded subscriptions provided |
| 156 if (preloadedPreferenceName != null) | 167 if (preloadedPreferenceName != null) |
| 157 { | 168 { |
| 158 SharedPreferences preloadedSubscriptionsPrefs = context.getSharedPreferenc es( | 169 SharedPreferences preloadedSubscriptionsPrefs = context.getSharedPreferenc es( |
| 159 preloadedPreferenceName, | 170 preloadedPreferenceName, |
| 160 Context.MODE_PRIVATE); | 171 Context.MODE_PRIVATE); |
| 161 builder.preloadSubscriptions( | 172 builder.preloadSubscriptions( |
| 162 context, | 173 context, |
| 163 urlToResourceIdMap, | 174 urlToResourceIdMap, |
| 164 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); | 175 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 public int getCounter() | 243 public int getCounter() |
| 233 { | 244 { |
| 234 return referenceCounter.get(); | 245 return referenceCounter.get(); |
| 235 } | 246 } |
| 236 | 247 |
| 237 /** | 248 /** |
| 238 * Register AdblockHelper engine client | 249 * Register AdblockHelper engine client |
| 239 * @param asynchronous If `true` engines will be created in background thread without locking of | 250 * @param asynchronous If `true` engines will be created in background thread without locking of |
| 240 * current thread. Use waitForReady() before getEngine() l ater. | 251 * current thread. Use waitForReady() before getEngine() l ater. |
| 241 * If `false` locks current thread. | 252 * If `false` locks current thread. |
| 253 * @return if a new instance is allocated | |
| 242 */ | 254 */ |
| 243 public synchronized void retain(boolean asynchronous) | 255 public synchronized boolean retain(boolean asynchronous) |
|
sergei
2017/09/26 09:44:13
It seems this code and `lastInstance` are not used
anton
2017/09/26 10:55:06
They are related but i will split it into separate
| |
| 244 { | 256 { |
| 257 boolean firstInstance = false; | |
| 258 | |
| 245 if (referenceCounter.getAndIncrement() == 0) | 259 if (referenceCounter.getAndIncrement() == 0) |
| 246 { | 260 { |
| 261 firstInstance = true; | |
| 262 | |
| 247 if (!asynchronous) | 263 if (!asynchronous) |
| 248 { | 264 { |
| 249 createAdblock(); | 265 createAdblock(); |
| 250 } | 266 } |
| 251 else | 267 else |
| 252 { | 268 { |
| 253 // latch is required for async (see `waitForReady()`) | 269 // latch is required for async (see `waitForReady()`) |
| 254 engineCreated = new CountDownLatch(1); | 270 engineCreated = new CountDownLatch(1); |
| 255 | 271 |
| 256 new Thread(new Runnable() | 272 new Thread(new Runnable() |
| 257 { | 273 { |
| 258 @Override | 274 @Override |
| 259 public void run() | 275 public void run() |
| 260 { | 276 { |
| 261 createAdblock(); | 277 createAdblock(); |
| 262 | 278 |
| 263 // unlock waiting client thread | 279 // unlock waiting client thread |
| 264 engineCreated.countDown(); | 280 engineCreated.countDown(); |
| 265 } | 281 } |
| 266 }).start(); | 282 }).start(); |
| 267 } | 283 } |
| 268 } | 284 } |
| 285 return firstInstance; | |
| 269 } | 286 } |
| 270 | 287 |
| 271 /** | 288 /** |
| 272 * Unregister AdblockHelper engine client | 289 * Unregister AdblockHelper engine client |
| 290 * @return if the last instance is destroyed | |
| 273 */ | 291 */ |
| 274 public synchronized void release() | 292 public synchronized boolean release() |
| 275 { | 293 { |
| 294 boolean lastInstance = false; | |
| 295 | |
| 276 if (referenceCounter.decrementAndGet() == 0) | 296 if (referenceCounter.decrementAndGet() == 0) |
| 277 { | 297 { |
| 298 lastInstance = true; | |
| 299 | |
| 278 if (engineCreated != null) | 300 if (engineCreated != null) |
| 279 { | 301 { |
| 280 // retained asynchronously | 302 // retained asynchronously |
| 281 waitForReady(); | 303 waitForReady(); |
| 282 disposeAdblock(); | 304 disposeAdblock(); |
| 283 | 305 |
| 284 // to unlock waiting client in waitForReady() | 306 // to unlock waiting client in waitForReady() |
| 285 engineCreated.countDown(); | 307 engineCreated.countDown(); |
| 286 engineCreated = null; | 308 engineCreated = null; |
| 287 } | 309 } |
| 288 else | 310 else |
| 289 { | 311 { |
| 290 disposeAdblock(); | 312 disposeAdblock(); |
| 291 } | 313 } |
| 292 } | 314 } |
| 315 return lastInstance; | |
| 293 } | 316 } |
| 294 } | 317 } |
| OLD | NEW |