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 10 matching lines...) Expand all Loading... |
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.Map; | 26 import java.util.Map; |
27 import java.util.concurrent.CountDownLatch; | 27 import java.util.concurrent.CountDownLatch; |
28 import java.util.concurrent.atomic.AtomicInteger; | 28 import java.util.concurrent.atomic.AtomicInteger; |
29 | 29 |
30 /** | 30 /** |
31 * Provides single instance of AdblockEngine | 31 * Provides single instance of AdblockEngine shared between registered clients |
32 */ | 32 */ |
33 public class SingletonEngineProvider implements AdblockEngineProvider | 33 public class SingleInstanceEngineProvider implements AdblockEngineProvider |
34 { | 34 { |
35 private static final String TAG = Utils.getTag(SingletonEngineProvider.class); | 35 private static final String TAG = Utils.getTag(SingleInstanceEngineProvider.cl
ass); |
36 | 36 |
37 private Context context; | 37 private Context context; |
38 private String basePath; | 38 private String basePath; |
39 private boolean developmentBuild; | 39 private boolean developmentBuild; |
40 private String preloadedPreferenceName; | 40 private String preloadedPreferenceName; |
41 private Map<String, Integer> urlToResourceIdMap; | 41 private Map<String, Integer> urlToResourceIdMap; |
42 private AdblockEngine engine; | 42 private AdblockEngine engine; |
43 private CountDownLatch engineCreated; | 43 private CountDownLatch engineCreated; |
44 private Long v8IsolateProviderPtr; | 44 private Long v8IsolateProviderPtr; |
45 private Runnable engineCreatedCallback; | 45 private Runnable engineCreatedCallback; |
(...skipping 10 matching lines...) Expand all Loading... |
56 * Init with context | 56 * Init with context |
57 * @param context application context | 57 * @param context application context |
58 * @param basePath file system root to store files | 58 * @param basePath file system root to store files |
59 * | 59 * |
60 * Adblock Plus library will download subscription files and s
tore them on | 60 * Adblock Plus library will download subscription files and s
tore them on |
61 * the path passed. The path should exist and the directory co
ntent should not be | 61 * the path passed. The path should exist and the directory co
ntent should not be |
62 * cleared out occasionally. Using `context.getCacheDir().getA
bsolutePath()` is not | 62 * cleared out occasionally. Using `context.getCacheDir().getA
bsolutePath()` is not |
63 * recommended because it can be cleared by the system. | 63 * recommended because it can be cleared by the system. |
64 * @param developmentBuild debug or release? | 64 * @param developmentBuild debug or release? |
65 */ | 65 */ |
66 public SingletonEngineProvider(Context context, String basePath, boolean devel
opmentBuild) | 66 public SingleInstanceEngineProvider(Context context, String basePath, boolean
developmentBuild) |
67 { | 67 { |
68 this.context = context.getApplicationContext(); | 68 this.context = context.getApplicationContext(); |
69 this.basePath = basePath; | 69 this.basePath = basePath; |
70 this.developmentBuild = developmentBuild; | 70 this.developmentBuild = developmentBuild; |
71 } | 71 } |
72 | 72 |
73 @Override | |
74 public AdblockEngine getAdblockEngine() | |
75 { | |
76 return engine; | |
77 } | |
78 | |
79 /** | 73 /** |
80 * Use preloaded subscriptions | 74 * Use preloaded subscriptions |
81 * @param preferenceName Shared Preferences name to store intercepted requests
stats | 75 * @param preferenceName Shared Preferences name to store intercepted requests
stats |
82 * @param urlToResourceIdMap | 76 * @param urlToResourceIdMap |
83 */ | 77 */ |
84 public SingletonEngineProvider preloadSubscriptions(String preferenceName, | 78 public SingleInstanceEngineProvider preloadSubscriptions(String preferenceName
, |
85 Map<String, Integer> urlTo
ResourceIdMap) | 79 Map<String, Integer>
urlToResourceIdMap) |
86 { | 80 { |
87 this.preloadedPreferenceName = preferenceName; | 81 this.preloadedPreferenceName = preferenceName; |
88 this.urlToResourceIdMap = urlToResourceIdMap; | 82 this.urlToResourceIdMap = urlToResourceIdMap; |
89 return this; | 83 return this; |
90 } | 84 } |
91 | 85 |
92 public SingletonEngineProvider useV8IsolateProvider(long ptr) | 86 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) |
93 { | 87 { |
94 this.v8IsolateProviderPtr = ptr; | 88 this.v8IsolateProviderPtr = ptr; |
95 return this; | 89 return this; |
96 } | 90 } |
97 | 91 |
98 public SingletonEngineProvider setEngineCreatedCallback(Runnable callback) | 92 public SingleInstanceEngineProvider setEngineCreatedCallback(Runnable callback
) |
99 { | 93 { |
100 this.engineCreatedCallback = callback; | 94 this.engineCreatedCallback = callback; |
101 return this; | 95 return this; |
102 } | 96 } |
103 | 97 |
104 public SingletonEngineProvider setEngineDisposedCallback(Runnable callback) | 98 public SingleInstanceEngineProvider setEngineDisposedCallback(Runnable callbac
k) |
105 { | 99 { |
106 this.engineDisposedCallback = callback; | 100 this.engineDisposedCallback = callback; |
107 return this; | 101 return this; |
108 } | 102 } |
109 | 103 |
110 private void createAdblock() | 104 private void createAdblock() |
111 { | 105 { |
112 ConnectivityManager connectivityManager = | 106 ConnectivityManager connectivityManager = |
113 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC
E); | 107 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC
E); |
114 IsAllowedConnectionCallback isAllowedConnectionCallback = | 108 IsAllowedConnectionCallback isAllowedConnectionCallback = |
(...skipping 29 matching lines...) Expand all Loading... |
144 | 138 |
145 Log.d(TAG, "AdblockHelper engine created"); | 139 Log.d(TAG, "AdblockHelper engine created"); |
146 | 140 |
147 // sometimes we need to init AdblockEngine instance, eg. set user settings | 141 // sometimes we need to init AdblockEngine instance, eg. set user settings |
148 if (engineCreatedCallback != null) | 142 if (engineCreatedCallback != null) |
149 { | 143 { |
150 engineCreatedCallback.run(); | 144 engineCreatedCallback.run(); |
151 } | 145 } |
152 } | 146 } |
153 | 147 |
154 /** | 148 @Override |
155 * Wait until everything is ready (used for `retain(true)`) | |
156 * Warning: locks current thread | |
157 */ | |
158 public void waitForReady() | |
159 { | |
160 if (engineCreated == null) | |
161 { | |
162 throw new RuntimeException("AdblockHelper Plus usage exception: call retai
n(true) first"); | |
163 } | |
164 | |
165 try | |
166 { | |
167 Log.d(TAG, "Waiting for ready ..."); | |
168 engineCreated.await(); | |
169 Log.d(TAG, "Ready"); | |
170 } | |
171 catch (InterruptedException e) | |
172 { | |
173 Log.w(TAG, "Interrupted", e); | |
174 } | |
175 } | |
176 | |
177 private void disposeAdblock() | |
178 { | |
179 Log.w(TAG, "Disposing adblock engine"); | |
180 | |
181 engine.dispose(); | |
182 engine = null; | |
183 | |
184 // sometimes we need to deinit something after AdblockEngine instance dispos
ed | |
185 // eg. release user settings | |
186 if (engineDisposedCallback != null) | |
187 { | |
188 engineDisposedCallback.run(); | |
189 } | |
190 } | |
191 | |
192 /** | |
193 * Get registered clients count | |
194 * @return registered clients count | |
195 */ | |
196 public int getCounter() | |
197 { | |
198 return referenceCounter.get(); | |
199 } | |
200 | |
201 /** | |
202 * Register AdblockHelper engine client | |
203 * @param asynchronous If `true` engines will be created in background thread
without locking of | |
204 * current thread. Use waitForReady() before getAdblockEng
ine() later. | |
205 * If `false` locks current thread. | |
206 * @return if a new instance is allocated | |
207 */ | |
208 public synchronized boolean retain(boolean asynchronous) | 149 public synchronized boolean retain(boolean asynchronous) |
209 { | 150 { |
210 boolean firstInstance = false; | 151 boolean firstInstance = false; |
211 | 152 |
212 if (referenceCounter.getAndIncrement() == 0) | 153 if (referenceCounter.getAndIncrement() == 0) |
213 { | 154 { |
214 firstInstance = true; | 155 firstInstance = true; |
215 | 156 |
216 if (!asynchronous) | 157 if (!asynchronous) |
217 { | 158 { |
(...skipping 13 matching lines...) Expand all Loading... |
231 | 172 |
232 // unlock waiting client thread | 173 // unlock waiting client thread |
233 engineCreated.countDown(); | 174 engineCreated.countDown(); |
234 } | 175 } |
235 }).start(); | 176 }).start(); |
236 } | 177 } |
237 } | 178 } |
238 return firstInstance; | 179 return firstInstance; |
239 } | 180 } |
240 | 181 |
241 /** | 182 @Override |
242 * Unregister AdblockHelper engine client | 183 public void waitForReady() |
243 * @return `true` if the last instance is destroyed | 184 { |
244 */ | 185 if (engineCreated == null) |
| 186 { |
| 187 throw new IllegalStateException("Usage exception: call retain(true) first"
); |
| 188 } |
| 189 |
| 190 try |
| 191 { |
| 192 Log.d(TAG, "Waiting for ready ..."); |
| 193 engineCreated.await(); |
| 194 Log.d(TAG, "Ready"); |
| 195 } |
| 196 catch (InterruptedException e) |
| 197 { |
| 198 Log.w(TAG, "Interrupted", e); |
| 199 } |
| 200 } |
| 201 |
| 202 @Override |
| 203 public AdblockEngine getEngine() |
| 204 { |
| 205 return engine; |
| 206 } |
| 207 |
| 208 @Override |
245 public synchronized boolean release() | 209 public synchronized boolean release() |
246 { | 210 { |
247 boolean lastInstance = false; | 211 boolean lastInstance = false; |
248 | 212 |
249 if (referenceCounter.decrementAndGet() == 0) | 213 if (referenceCounter.decrementAndGet() == 0) |
250 { | 214 { |
251 lastInstance = true; | 215 lastInstance = true; |
252 | 216 |
253 if (engineCreated != null) | 217 if (engineCreated != null) |
254 { | 218 { |
255 // retained asynchronously | 219 // retained asynchronously |
256 waitForReady(); | 220 waitForReady(); |
257 disposeAdblock(); | 221 disposeAdblock(); |
258 | 222 |
259 // to unlock waiting client in waitForReady() | 223 // to unlock waiting client in waitForReady() |
260 engineCreated.countDown(); | 224 engineCreated.countDown(); |
261 engineCreated = null; | 225 engineCreated = null; |
262 } | 226 } |
263 else | 227 else |
264 { | 228 { |
265 disposeAdblock(); | 229 disposeAdblock(); |
266 } | 230 } |
267 } | 231 } |
268 return lastInstance; | 232 return lastInstance; |
269 } | 233 } |
| 234 |
| 235 private void disposeAdblock() |
| 236 { |
| 237 Log.w(TAG, "Disposing adblock engine"); |
| 238 |
| 239 engine.dispose(); |
| 240 engine = null; |
| 241 |
| 242 // sometimes we need to deinit something after AdblockEngine instance dispos
ed |
| 243 // eg. release user settings |
| 244 if (engineDisposedCallback != null) |
| 245 { |
| 246 engineDisposedCallback.run(); |
| 247 } |
| 248 } |
| 249 |
| 250 @Override |
| 251 public int getCounter() |
| 252 { |
| 253 return referenceCounter.get(); |
| 254 } |
270 } | 255 } |
OLD | NEW |