| Left: | ||
| Right: |
| 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 18 matching lines...) Expand all Loading... | |
| 29 import java.util.concurrent.CountDownLatch; | 29 import java.util.concurrent.CountDownLatch; |
| 30 import java.util.concurrent.atomic.AtomicInteger; | 30 import java.util.concurrent.atomic.AtomicInteger; |
| 31 | 31 |
| 32 /** | 32 /** |
| 33 * Provides single instance of AdblockEngine shared between registered clients | 33 * Provides single instance of AdblockEngine shared between registered clients |
| 34 */ | 34 */ |
| 35 public class SingleInstanceEngineProvider implements AdblockEngineProvider | 35 public class SingleInstanceEngineProvider implements AdblockEngineProvider |
| 36 { | 36 { |
| 37 private static final String TAG = Utils.getTag(SingleInstanceEngineProvider.cl ass); | 37 private static final String TAG = Utils.getTag(SingleInstanceEngineProvider.cl ass); |
| 38 | 38 |
| 39 public interface EngineCreatedListener | |
| 40 { | |
| 41 void onAdblockEngineCreated(AdblockEngine engine); | |
| 42 } | |
| 43 | |
| 44 public interface EngineDisposedListener | |
| 45 { | |
| 46 void onAdblockEngineDisposed(); | |
| 47 } | |
| 48 | |
| 39 private Context context; | 49 private Context context; |
| 40 private String basePath; | 50 private String basePath; |
| 41 private boolean developmentBuild; | 51 private boolean developmentBuild; |
| 42 private String preloadedPreferenceName; | 52 private String preloadedPreferenceName; |
| 43 private Map<String, Integer> urlToResourceIdMap; | 53 private Map<String, Integer> urlToResourceIdMap; |
| 44 private AdblockEngine engine; | 54 private AdblockEngine engine; |
| 45 private CountDownLatch engineCreated; | 55 private CountDownLatch engineCreated; |
| 46 private Long v8IsolateProviderPtr; | 56 private Long v8IsolateProviderPtr; |
| 47 private List<Runnable> engineCreatedCallbacks = new LinkedList<Runnable>(); | 57 private List<EngineCreatedListener> engineCreatedListeners = |
| 48 private List<Runnable> engineDisposedCallbacks = new LinkedList<Runnable>(); | 58 new LinkedList<EngineCreatedListener>(); |
| 59 private List<EngineDisposedListener> engineDisposedListeners = | |
| 60 new LinkedList<EngineDisposedListener>(); | |
|
diegocarloslima
2018/01/26 13:04:28
Just one minor thing, the lists could be final
| |
| 49 | 61 |
| 50 /* | 62 /* |
| 51 Simple ARC management for AdblockEngine | 63 Simple ARC management for AdblockEngine |
| 52 Use `retain` and `release` | 64 Use `retain` and `release` |
| 53 */ | 65 */ |
| 54 | 66 |
| 55 private AtomicInteger referenceCounter = new AtomicInteger(0); | 67 private AtomicInteger referenceCounter = new AtomicInteger(0); |
| 56 | 68 |
| 57 /** | 69 /** |
| 58 * Init with context | 70 * Init with context |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 84 this.urlToResourceIdMap = urlToResourceIdMap; | 96 this.urlToResourceIdMap = urlToResourceIdMap; |
| 85 return this; | 97 return this; |
| 86 } | 98 } |
| 87 | 99 |
| 88 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) | 100 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) |
| 89 { | 101 { |
| 90 this.v8IsolateProviderPtr = ptr; | 102 this.v8IsolateProviderPtr = ptr; |
| 91 return this; | 103 return this; |
| 92 } | 104 } |
| 93 | 105 |
| 94 public SingleInstanceEngineProvider addEngineCreatedCallback(Runnable callback ) | 106 public SingleInstanceEngineProvider addEngineCreatedListener(EngineCreatedList ener listener) |
| 95 { | 107 { |
| 96 this.engineCreatedCallbacks.add(callback); | 108 this.engineCreatedListeners.add(listener); |
| 97 return this; | 109 return this; |
| 98 } | 110 } |
| 99 | 111 |
| 100 public void removeEngineCreatedCallback(Runnable callback) | 112 public void removeEngineCreatedListener(EngineCreatedListener listener) |
|
anton
2018/01/24 12:51:08
"remove.." methods are not used anywhere but could
sergei
2018/01/24 13:33:02
I would say if someone wants to remove or clear th
jens
2018/01/26 08:15:22
Even if Sergejs idea is also fine I would prefer t
anton
2018/01/26 08:22:58
Acknowledged.
| |
| 101 { | 113 { |
| 102 this.engineCreatedCallbacks.remove(callback); | 114 this.engineCreatedListeners.remove(listener); |
| 103 } | 115 } |
| 104 | 116 |
| 105 public SingleInstanceEngineProvider addEngineDisposedCallback(Runnable callbac k) | 117 public void clearEngineCreatedListeners() |
| 106 { | 118 { |
| 107 this.engineDisposedCallbacks.add(callback); | 119 this.engineCreatedListeners.clear(); |
| 108 return this; | 120 } |
| 109 } | 121 |
| 110 | 122 public SingleInstanceEngineProvider addEngineDisposedListener(EngineDisposedLi stener listener) |
| 111 public void removeEngineDisposedCallback(Runnable callback) | 123 { |
| 112 { | 124 this.engineDisposedListeners.add(listener); |
| 113 this.engineDisposedCallbacks.remove(callback); | 125 return this; |
| 126 } | |
| 127 | |
| 128 public void removeEngineDisposedListener(EngineDisposedListener listener) | |
| 129 { | |
| 130 this.engineDisposedListeners.remove(listener); | |
| 131 } | |
| 132 | |
| 133 public void clearEngineDisposedListeners() | |
| 134 { | |
| 135 this.engineDisposedListeners.clear(); | |
| 114 } | 136 } |
| 115 | 137 |
| 116 private void createAdblock() | 138 private void createAdblock() |
| 117 { | 139 { |
| 118 ConnectivityManager connectivityManager = | 140 ConnectivityManager connectivityManager = |
| 119 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); | 141 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); |
| 120 IsAllowedConnectionCallback isAllowedConnectionCallback = | 142 IsAllowedConnectionCallback isAllowedConnectionCallback = |
| 121 new IsAllowedConnectionCallbackImpl(connectivityManager); | 143 new IsAllowedConnectionCallbackImpl(connectivityManager); |
| 122 | 144 |
| 123 Log.d(TAG, "Creating adblock engine ..."); | 145 Log.d(TAG, "Creating adblock engine ..."); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 144 context, | 166 context, |
| 145 urlToResourceIdMap, | 167 urlToResourceIdMap, |
| 146 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); | 168 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); |
| 147 } | 169 } |
| 148 | 170 |
| 149 engine = builder.build(); | 171 engine = builder.build(); |
| 150 | 172 |
| 151 Log.d(TAG, "AdblockHelper engine created"); | 173 Log.d(TAG, "AdblockHelper engine created"); |
| 152 | 174 |
| 153 // sometimes we need to init AdblockEngine instance, eg. set user settings | 175 // sometimes we need to init AdblockEngine instance, eg. set user settings |
| 154 for (Runnable callback : engineCreatedCallbacks) | 176 for (EngineCreatedListener listener : engineCreatedListeners) |
| 155 { | 177 { |
| 156 callback.run(); | 178 listener.onAdblockEngineCreated(engine); |
| 157 } | 179 } |
| 158 } | 180 } |
| 159 | 181 |
| 160 @Override | 182 @Override |
| 161 public synchronized boolean retain(boolean asynchronous) | 183 public synchronized boolean retain(boolean asynchronous) |
| 162 { | 184 { |
| 163 boolean firstInstance = false; | 185 boolean firstInstance = false; |
| 164 | 186 |
| 165 if (referenceCounter.getAndIncrement() == 0) | 187 if (referenceCounter.getAndIncrement() == 0) |
| 166 { | 188 { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 | 268 |
| 247 private void disposeAdblock() | 269 private void disposeAdblock() |
| 248 { | 270 { |
| 249 Log.w(TAG, "Disposing adblock engine"); | 271 Log.w(TAG, "Disposing adblock engine"); |
| 250 | 272 |
| 251 engine.dispose(); | 273 engine.dispose(); |
| 252 engine = null; | 274 engine = null; |
| 253 | 275 |
| 254 // sometimes we need to deinit something after AdblockEngine instance dispos ed | 276 // sometimes we need to deinit something after AdblockEngine instance dispos ed |
| 255 // eg. release user settings | 277 // eg. release user settings |
| 256 for (Runnable callback : engineDisposedCallbacks) | 278 for (EngineDisposedListener listener : engineDisposedListeners) |
| 257 { | 279 { |
| 258 callback.run(); | 280 listener.onAdblockEngineDisposed(); |
| 259 } | 281 } |
| 260 } | 282 } |
| 261 | 283 |
| 262 @Override | 284 @Override |
| 263 public int getCounter() | 285 public int getCounter() |
| 264 { | 286 { |
| 265 return referenceCounter.get(); | 287 return referenceCounter.get(); |
| 266 } | 288 } |
| 267 } | 289 } |
| LEFT | RIGHT |