 Issue 29678590:
  Issue 6307 - Introduce external engine created callback  (Closed)
    
  
    Issue 29678590:
  Issue 6307 - Introduce external engine created callback  (Closed) 
  | 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>(); | 
| 
diegocarloslima
2018/01/26 12:30:01
I would prefer the callbacks to be interfaces inst
 | |
| 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) | 
| 101 { | 113 { | 
| 102 this.engineCreatedCallbacks.remove(callback); | 114 this.engineCreatedListeners.remove(listener); | 
| 103 } | 115 } | 
| 104 | 116 | 
| 105 public void clearEngineCreatedCallbacks() | 117 public void clearEngineCreatedListeners() | 
| 106 { | 118 { | 
| 107 this.engineCreatedCallbacks.clear(); | 119 this.engineCreatedListeners.clear(); | 
| 108 } | 120 } | 
| 109 | 121 | 
| 110 public SingleInstanceEngineProvider addEngineDisposedCallback(Runnable callbac k) | 122 public SingleInstanceEngineProvider addEngineDisposedListener(EngineDisposedLi stener listener) | 
| 111 { | 123 { | 
| 112 this.engineDisposedCallbacks.add(callback); | 124 this.engineDisposedListeners.add(listener); | 
| 113 return this; | 125 return this; | 
| 114 } | 126 } | 
| 115 | 127 | 
| 116 public void removeEngineDisposedCallback(Runnable callback) | 128 public void removeEngineDisposedListener(EngineDisposedListener listener) | 
| 117 { | 129 { | 
| 118 this.engineDisposedCallbacks.remove(callback); | 130 this.engineDisposedListeners.remove(listener); | 
| 119 } | 131 } | 
| 120 | 132 | 
| 121 public void clearEngineDisposedCallbacks() | 133 public void clearEngineDisposedListeners() | 
| 122 { | 134 { | 
| 123 this.engineDisposedCallbacks.clear(); | 135 this.engineDisposedListeners.clear(); | 
| 124 } | 136 } | 
| 125 | 137 | 
| 126 private void createAdblock() | 138 private void createAdblock() | 
| 127 { | 139 { | 
| 128 ConnectivityManager connectivityManager = | 140 ConnectivityManager connectivityManager = | 
| 129 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); | 141 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); | 
| 130 IsAllowedConnectionCallback isAllowedConnectionCallback = | 142 IsAllowedConnectionCallback isAllowedConnectionCallback = | 
| 131 new IsAllowedConnectionCallbackImpl(connectivityManager); | 143 new IsAllowedConnectionCallbackImpl(connectivityManager); | 
| 132 | 144 | 
| 133 Log.d(TAG, "Creating adblock engine ..."); | 145 Log.d(TAG, "Creating adblock engine ..."); | 
| (...skipping 20 matching lines...) Expand all Loading... | |
| 154 context, | 166 context, | 
| 155 urlToResourceIdMap, | 167 urlToResourceIdMap, | 
| 156 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); | 168 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); | 
| 157 } | 169 } | 
| 158 | 170 | 
| 159 engine = builder.build(); | 171 engine = builder.build(); | 
| 160 | 172 | 
| 161 Log.d(TAG, "AdblockHelper engine created"); | 173 Log.d(TAG, "AdblockHelper engine created"); | 
| 162 | 174 | 
| 163 // sometimes we need to init AdblockEngine instance, eg. set user settings | 175 // sometimes we need to init AdblockEngine instance, eg. set user settings | 
| 164 for (Runnable callback : engineCreatedCallbacks) | 176 for (EngineCreatedListener listener : engineCreatedListeners) | 
| 165 { | 177 { | 
| 166 callback.run(); | 178 listener.onAdblockEngineCreated(engine); | 
| 167 } | 179 } | 
| 168 } | 180 } | 
| 169 | 181 | 
| 170 @Override | 182 @Override | 
| 171 public synchronized boolean retain(boolean asynchronous) | 183 public synchronized boolean retain(boolean asynchronous) | 
| 172 { | 184 { | 
| 173 boolean firstInstance = false; | 185 boolean firstInstance = false; | 
| 174 | 186 | 
| 175 if (referenceCounter.getAndIncrement() == 0) | 187 if (referenceCounter.getAndIncrement() == 0) | 
| 176 { | 188 { | 
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 | 268 | 
| 257 private void disposeAdblock() | 269 private void disposeAdblock() | 
| 258 { | 270 { | 
| 259 Log.w(TAG, "Disposing adblock engine"); | 271 Log.w(TAG, "Disposing adblock engine"); | 
| 260 | 272 | 
| 261 engine.dispose(); | 273 engine.dispose(); | 
| 262 engine = null; | 274 engine = null; | 
| 263 | 275 | 
| 264 // sometimes we need to deinit something after AdblockEngine instance dispos ed | 276 // sometimes we need to deinit something after AdblockEngine instance dispos ed | 
| 265 // eg. release user settings | 277 // eg. release user settings | 
| 266 for (Runnable callback : engineDisposedCallbacks) | 278 for (EngineDisposedListener listener : engineDisposedListeners) | 
| 267 { | 279 { | 
| 268 callback.run(); | 280 listener.onAdblockEngineDisposed(); | 
| 269 } | 281 } | 
| 270 } | 282 } | 
| 271 | 283 | 
| 272 @Override | 284 @Override | 
| 273 public int getCounter() | 285 public int getCounter() | 
| 274 { | 286 { | 
| 275 return referenceCounter.get(); | 287 return referenceCounter.get(); | 
| 276 } | 288 } | 
| 277 } | 289 } | 
| LEFT | RIGHT |