| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 package org.adblockplus.libadblockplus.android; | 17 package org.adblockplus.libadblockplus.android; |
| 18 | 18 |
| 19 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; | 19 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
| 20 | 20 |
| 21 import android.content.Context; | 21 import android.content.Context; |
| 22 import android.content.SharedPreferences; | 22 import android.content.SharedPreferences; |
| 23 import android.net.ConnectivityManager; | 23 import android.net.ConnectivityManager; |
| 24 import android.util.Log; | 24 import android.util.Log; |
| 25 | 25 |
| 26 import java.util.LinkedList; |
| 27 import java.util.List; |
| 26 import java.util.Map; | 28 import java.util.Map; |
| 27 import java.util.concurrent.CountDownLatch; | 29 import java.util.concurrent.CountDownLatch; |
| 28 import java.util.concurrent.atomic.AtomicInteger; | 30 import java.util.concurrent.atomic.AtomicInteger; |
| 29 | 31 |
| 30 /** | 32 /** |
| 31 * Provides single instance of AdblockEngine shared between registered clients | 33 * Provides single instance of AdblockEngine shared between registered clients |
| 32 */ | 34 */ |
| 33 public class SingleInstanceEngineProvider implements AdblockEngineProvider | 35 public class SingleInstanceEngineProvider implements AdblockEngineProvider |
| 34 { | 36 { |
| 35 private static final String TAG = Utils.getTag(SingleInstanceEngineProvider.cl
ass); | 37 private static final String TAG = Utils.getTag(SingleInstanceEngineProvider.cl
ass); |
| 36 | 38 |
| 37 private Context context; | 39 private Context context; |
| 38 private String basePath; | 40 private String basePath; |
| 39 private boolean developmentBuild; | 41 private boolean developmentBuild; |
| 40 private String preloadedPreferenceName; | 42 private String preloadedPreferenceName; |
| 41 private Map<String, Integer> urlToResourceIdMap; | 43 private Map<String, Integer> urlToResourceIdMap; |
| 42 private AdblockEngine engine; | 44 private AdblockEngine engine; |
| 43 private CountDownLatch engineCreated; | 45 private CountDownLatch engineCreated; |
| 44 private Long v8IsolateProviderPtr; | 46 private Long v8IsolateProviderPtr; |
| 45 private Runnable engineCreatedCallback; | 47 private List<Runnable> engineCreatedCallbacks = new LinkedList<Runnable>(); |
| 46 private Runnable engineDisposedCallback; | 48 private List<Runnable> engineDisposedCallbacks = new LinkedList<Runnable>(); |
| 47 | 49 |
| 48 /* | 50 /* |
| 49 Simple ARC management for AdblockEngine | 51 Simple ARC management for AdblockEngine |
| 50 Use `retain` and `release` | 52 Use `retain` and `release` |
| 51 */ | 53 */ |
| 52 | 54 |
| 53 private AtomicInteger referenceCounter = new AtomicInteger(0); | 55 private AtomicInteger referenceCounter = new AtomicInteger(0); |
| 54 | 56 |
| 55 /** | 57 /** |
| 56 * Init with context | 58 * Init with context |
| (...skipping 25 matching lines...) Expand all Loading... |
| 82 this.urlToResourceIdMap = urlToResourceIdMap; | 84 this.urlToResourceIdMap = urlToResourceIdMap; |
| 83 return this; | 85 return this; |
| 84 } | 86 } |
| 85 | 87 |
| 86 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) | 88 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) |
| 87 { | 89 { |
| 88 this.v8IsolateProviderPtr = ptr; | 90 this.v8IsolateProviderPtr = ptr; |
| 89 return this; | 91 return this; |
| 90 } | 92 } |
| 91 | 93 |
| 92 public SingleInstanceEngineProvider setEngineCreatedCallback(Runnable callback
) | 94 public SingleInstanceEngineProvider addEngineCreatedCallback(Runnable callback
) |
| 93 { | 95 { |
| 94 this.engineCreatedCallback = callback; | 96 this.engineCreatedCallbacks.add(callback); |
| 95 return this; | 97 return this; |
| 96 } | 98 } |
| 97 | 99 |
| 98 public SingleInstanceEngineProvider setEngineDisposedCallback(Runnable callbac
k) | 100 public void removeEngineCreatedCallback(Runnable callback) |
| 99 { | 101 { |
| 100 this.engineDisposedCallback = callback; | 102 this.engineCreatedCallbacks.remove(callback); |
| 103 } |
| 104 |
| 105 public void clearEngineCreatedCallbacks() |
| 106 { |
| 107 this.engineCreatedCallbacks.clear(); |
| 108 } |
| 109 |
| 110 public SingleInstanceEngineProvider addEngineDisposedCallback(Runnable callbac
k) |
| 111 { |
| 112 this.engineDisposedCallbacks.add(callback); |
| 101 return this; | 113 return this; |
| 102 } | 114 } |
| 103 | 115 |
| 116 public void removeEngineDisposedCallback(Runnable callback) |
| 117 { |
| 118 this.engineDisposedCallbacks.remove(callback); |
| 119 } |
| 120 |
| 121 public void clearEngineDisposedCallbacks() |
| 122 { |
| 123 this.engineDisposedCallbacks.clear(); |
| 124 } |
| 125 |
| 104 private void createAdblock() | 126 private void createAdblock() |
| 105 { | 127 { |
| 106 ConnectivityManager connectivityManager = | 128 ConnectivityManager connectivityManager = |
| 107 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC
E); | 129 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC
E); |
| 108 IsAllowedConnectionCallback isAllowedConnectionCallback = | 130 IsAllowedConnectionCallback isAllowedConnectionCallback = |
| 109 new IsAllowedConnectionCallbackImpl(connectivityManager); | 131 new IsAllowedConnectionCallbackImpl(connectivityManager); |
| 110 | 132 |
| 111 Log.d(TAG, "Creating adblock engine ..."); | 133 Log.d(TAG, "Creating adblock engine ..."); |
| 112 | 134 |
| 113 AdblockEngine.Builder builder = AdblockEngine | 135 AdblockEngine.Builder builder = AdblockEngine |
| (...skipping 18 matching lines...) Expand all Loading... |
| 132 context, | 154 context, |
| 133 urlToResourceIdMap, | 155 urlToResourceIdMap, |
| 134 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri
ptionsPrefs)); | 156 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri
ptionsPrefs)); |
| 135 } | 157 } |
| 136 | 158 |
| 137 engine = builder.build(); | 159 engine = builder.build(); |
| 138 | 160 |
| 139 Log.d(TAG, "AdblockHelper engine created"); | 161 Log.d(TAG, "AdblockHelper engine created"); |
| 140 | 162 |
| 141 // sometimes we need to init AdblockEngine instance, eg. set user settings | 163 // sometimes we need to init AdblockEngine instance, eg. set user settings |
| 142 if (engineCreatedCallback != null) | 164 for (Runnable callback : engineCreatedCallbacks) |
| 143 { | 165 { |
| 144 engineCreatedCallback.run(); | 166 callback.run(); |
| 145 } | 167 } |
| 146 } | 168 } |
| 147 | 169 |
| 148 @Override | 170 @Override |
| 149 public synchronized boolean retain(boolean asynchronous) | 171 public synchronized boolean retain(boolean asynchronous) |
| 150 { | 172 { |
| 151 boolean firstInstance = false; | 173 boolean firstInstance = false; |
| 152 | 174 |
| 153 if (referenceCounter.getAndIncrement() == 0) | 175 if (referenceCounter.getAndIncrement() == 0) |
| 154 { | 176 { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 256 |
| 235 private void disposeAdblock() | 257 private void disposeAdblock() |
| 236 { | 258 { |
| 237 Log.w(TAG, "Disposing adblock engine"); | 259 Log.w(TAG, "Disposing adblock engine"); |
| 238 | 260 |
| 239 engine.dispose(); | 261 engine.dispose(); |
| 240 engine = null; | 262 engine = null; |
| 241 | 263 |
| 242 // sometimes we need to deinit something after AdblockEngine instance dispos
ed | 264 // sometimes we need to deinit something after AdblockEngine instance dispos
ed |
| 243 // eg. release user settings | 265 // eg. release user settings |
| 244 if (engineDisposedCallback != null) | 266 for (Runnable callback : engineDisposedCallbacks) |
| 245 { | 267 { |
| 246 engineDisposedCallback.run(); | 268 callback.run(); |
| 247 } | 269 } |
| 248 } | 270 } |
| 249 | 271 |
| 250 @Override | 272 @Override |
| 251 public int getCounter() | 273 public int getCounter() |
| 252 { | 274 { |
| 253 return referenceCounter.get(); | 275 return referenceCounter.get(); |
| 254 } | 276 } |
| 255 } | 277 } |
| OLD | NEW |