Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android/src/org/adblockplus/libadblockplus/android/SingleInstanceEngineProvider.java

Issue 29678590: Issue 6307 - Introduce external engine created callback (Closed)
Patch Set: introduced custom listener interfaces Created Jan. 26, 2018, 12:46 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « libadblockplus-android/build.gradle ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
39 public interface EngineCreatedListener
40 {
41 void onAdblockEngineCreated(AdblockEngine engine);
42 }
43
44 public interface EngineDisposedListener
45 {
46 void onAdblockEngineDisposed();
47 }
48
37 private Context context; 49 private Context context;
38 private String basePath; 50 private String basePath;
39 private boolean developmentBuild; 51 private boolean developmentBuild;
40 private String preloadedPreferenceName; 52 private String preloadedPreferenceName;
41 private Map<String, Integer> urlToResourceIdMap; 53 private Map<String, Integer> urlToResourceIdMap;
42 private AdblockEngine engine; 54 private AdblockEngine engine;
43 private CountDownLatch engineCreated; 55 private CountDownLatch engineCreated;
44 private Long v8IsolateProviderPtr; 56 private Long v8IsolateProviderPtr;
45 private Runnable engineCreatedCallback; 57 private List<EngineCreatedListener> engineCreatedListeners =
46 private Runnable engineDisposedCallback; 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
47 61
48 /* 62 /*
49 Simple ARC management for AdblockEngine 63 Simple ARC management for AdblockEngine
50 Use `retain` and `release` 64 Use `retain` and `release`
51 */ 65 */
52 66
53 private AtomicInteger referenceCounter = new AtomicInteger(0); 67 private AtomicInteger referenceCounter = new AtomicInteger(0);
54 68
55 /** 69 /**
56 * Init with context 70 * Init with context
(...skipping 25 matching lines...) Expand all
82 this.urlToResourceIdMap = urlToResourceIdMap; 96 this.urlToResourceIdMap = urlToResourceIdMap;
83 return this; 97 return this;
84 } 98 }
85 99
86 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr) 100 public SingleInstanceEngineProvider useV8IsolateProvider(long ptr)
87 { 101 {
88 this.v8IsolateProviderPtr = ptr; 102 this.v8IsolateProviderPtr = ptr;
89 return this; 103 return this;
90 } 104 }
91 105
92 public SingleInstanceEngineProvider setEngineCreatedCallback(Runnable callback ) 106 public SingleInstanceEngineProvider addEngineCreatedListener(EngineCreatedList ener listener)
93 { 107 {
94 this.engineCreatedCallback = callback; 108 this.engineCreatedListeners.add(listener);
95 return this; 109 return this;
96 } 110 }
97 111
98 public SingleInstanceEngineProvider setEngineDisposedCallback(Runnable callbac k) 112 public void removeEngineCreatedListener(EngineCreatedListener listener)
99 { 113 {
100 this.engineDisposedCallback = callback; 114 this.engineCreatedListeners.remove(listener);
115 }
116
117 public void clearEngineCreatedListeners()
118 {
119 this.engineCreatedListeners.clear();
120 }
121
122 public SingleInstanceEngineProvider addEngineDisposedListener(EngineDisposedLi stener listener)
123 {
124 this.engineDisposedListeners.add(listener);
101 return this; 125 return this;
102 } 126 }
103 127
128 public void removeEngineDisposedListener(EngineDisposedListener listener)
129 {
130 this.engineDisposedListeners.remove(listener);
131 }
132
133 public void clearEngineDisposedListeners()
134 {
135 this.engineDisposedListeners.clear();
136 }
137
104 private void createAdblock() 138 private void createAdblock()
105 { 139 {
106 ConnectivityManager connectivityManager = 140 ConnectivityManager connectivityManager =
107 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E); 141 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVIC E);
108 IsAllowedConnectionCallback isAllowedConnectionCallback = 142 IsAllowedConnectionCallback isAllowedConnectionCallback =
109 new IsAllowedConnectionCallbackImpl(connectivityManager); 143 new IsAllowedConnectionCallbackImpl(connectivityManager);
110 144
111 Log.d(TAG, "Creating adblock engine ..."); 145 Log.d(TAG, "Creating adblock engine ...");
112 146
113 AdblockEngine.Builder builder = AdblockEngine 147 AdblockEngine.Builder builder = AdblockEngine
(...skipping 18 matching lines...) Expand all
132 context, 166 context,
133 urlToResourceIdMap, 167 urlToResourceIdMap,
134 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs)); 168 new AndroidWebRequestResourceWrapper.SharedPrefsStorage(preloadedSubscri ptionsPrefs));
135 } 169 }
136 170
137 engine = builder.build(); 171 engine = builder.build();
138 172
139 Log.d(TAG, "AdblockHelper engine created"); 173 Log.d(TAG, "AdblockHelper engine created");
140 174
141 // sometimes we need to init AdblockEngine instance, eg. set user settings 175 // sometimes we need to init AdblockEngine instance, eg. set user settings
142 if (engineCreatedCallback != null) 176 for (EngineCreatedListener listener : engineCreatedListeners)
143 { 177 {
144 engineCreatedCallback.run(); 178 listener.onAdblockEngineCreated(engine);
145 } 179 }
146 } 180 }
147 181
148 @Override 182 @Override
149 public synchronized boolean retain(boolean asynchronous) 183 public synchronized boolean retain(boolean asynchronous)
150 { 184 {
151 boolean firstInstance = false; 185 boolean firstInstance = false;
152 186
153 if (referenceCounter.getAndIncrement() == 0) 187 if (referenceCounter.getAndIncrement() == 0)
154 { 188 {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 268
235 private void disposeAdblock() 269 private void disposeAdblock()
236 { 270 {
237 Log.w(TAG, "Disposing adblock engine"); 271 Log.w(TAG, "Disposing adblock engine");
238 272
239 engine.dispose(); 273 engine.dispose();
240 engine = null; 274 engine = null;
241 275
242 // sometimes we need to deinit something after AdblockEngine instance dispos ed 276 // sometimes we need to deinit something after AdblockEngine instance dispos ed
243 // eg. release user settings 277 // eg. release user settings
244 if (engineDisposedCallback != null) 278 for (EngineDisposedListener listener : engineDisposedListeners)
245 { 279 {
246 engineDisposedCallback.run(); 280 listener.onAdblockEngineDisposed();
247 } 281 }
248 } 282 }
249 283
250 @Override 284 @Override
251 public int getCounter() 285 public int getCounter()
252 { 286 {
253 return referenceCounter.get(); 287 return referenceCounter.get();
254 } 288 }
255 } 289 }
OLDNEW
« no previous file with comments | « libadblockplus-android/build.gradle ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld