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 |
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) |
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.
| |
99 { | 101 { |
100 this.engineDisposedCallback = callback; | 102 this.engineCreatedCallbacks.remove(callback); |
103 } | |
104 | |
105 public SingleInstanceEngineProvider addEngineDisposedCallback(Runnable callbac k) | |
106 { | |
107 this.engineDisposedCallbacks.add(callback); | |
101 return this; | 108 return this; |
102 } | 109 } |
103 | 110 |
111 public void removeEngineDisposedCallback(Runnable callback) | |
112 { | |
113 this.engineDisposedCallbacks.remove(callback); | |
114 } | |
115 | |
104 private void createAdblock() | 116 private void createAdblock() |
105 { | 117 { |
106 ConnectivityManager connectivityManager = | 118 ConnectivityManager connectivityManager = |
107 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); | 119 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); |
108 IsAllowedConnectionCallback isAllowedConnectionCallback = | 120 IsAllowedConnectionCallback isAllowedConnectionCallback = |
109 new IsAllowedConnectionCallbackImpl(connectivityManager); | 121 new IsAllowedConnectionCallbackImpl(connectivityManager); |
110 | 122 |
111 Log.d(TAG, "Creating adblock engine ..."); | 123 Log.d(TAG, "Creating adblock engine ..."); |
112 | 124 |
113 AdblockEngine.Builder builder = AdblockEngine | 125 AdblockEngine.Builder builder = AdblockEngine |
(...skipping 18 matching lines...) Expand all Loading... | |
132 context, | 144 context, |
133 urlToResourceIdMap, | 145 urlToResourceIdMap, |
134 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); | 146 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); |
135 } | 147 } |
136 | 148 |
137 engine = builder.build(); | 149 engine = builder.build(); |
138 | 150 |
139 Log.d(TAG, "AdblockHelper engine created"); | 151 Log.d(TAG, "AdblockHelper engine created"); |
140 | 152 |
141 // sometimes we need to init AdblockEngine instance, eg. set user settings | 153 // sometimes we need to init AdblockEngine instance, eg. set user settings |
142 if (engineCreatedCallback != null) | 154 for (Runnable callback : engineCreatedCallbacks) |
143 { | 155 { |
144 engineCreatedCallback.run(); | 156 callback.run(); |
145 } | 157 } |
146 } | 158 } |
147 | 159 |
148 @Override | 160 @Override |
149 public synchronized boolean retain(boolean asynchronous) | 161 public synchronized boolean retain(boolean asynchronous) |
150 { | 162 { |
151 boolean firstInstance = false; | 163 boolean firstInstance = false; |
152 | 164 |
153 if (referenceCounter.getAndIncrement() == 0) | 165 if (referenceCounter.getAndIncrement() == 0) |
154 { | 166 { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 | 246 |
235 private void disposeAdblock() | 247 private void disposeAdblock() |
236 { | 248 { |
237 Log.w(TAG, "Disposing adblock engine"); | 249 Log.w(TAG, "Disposing adblock engine"); |
238 | 250 |
239 engine.dispose(); | 251 engine.dispose(); |
240 engine = null; | 252 engine = null; |
241 | 253 |
242 // sometimes we need to deinit something after AdblockEngine instance dispos ed | 254 // sometimes we need to deinit something after AdblockEngine instance dispos ed |
243 // eg. release user settings | 255 // eg. release user settings |
244 if (engineDisposedCallback != null) | 256 for (Runnable callback : engineDisposedCallbacks) |
245 { | 257 { |
246 engineDisposedCallback.run(); | 258 callback.run(); |
247 } | 259 } |
248 } | 260 } |
249 | 261 |
250 @Override | 262 @Override |
251 public int getCounter() | 263 public int getCounter() |
252 { | 264 { |
253 return referenceCounter.get(); | 265 return referenceCounter.get(); |
254 } | 266 } |
255 } | 267 } |
OLD | NEW |