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

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

Issue 29361445: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Patch Set: fixed 2 more members in AdblockEngine Created Dec. 16, 2016, 8:31 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 package org.adblockplus.libadblockplus.android.settings;
19
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.util.Log;
23
24 import org.adblockplus.libadblockplus.android.AdblockEngine;
25 import org.adblockplus.libadblockplus.android.Utils;
26
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.atomic.AtomicInteger;
29
30 /**
31 * Adblock shared resources
32 * (singleton)
33 */
34 public class Adblock
35 {
36 private static final String TAG = Utils.getTag(Adblock.class);
37
38 /**
39 * Suggested preference name
40 */
41 public static final String PREFERENCE_NAME = "ADBLOCK";
42 private static Adblock _instance;
43
44 private Context context;
45 private boolean developmentBuild;
46 private String preferenceName;
47 private AdblockEngine engine;
48 private AdblockSettingsStorage storage;
49 private CountDownLatch engineCreated;
50
51 /*
52 Simple ARC management for AdblockEngine
53 Use `retain` and `release`
54 */
55
56 private AtomicInteger referenceCounter = new AtomicInteger(0);
57
58 // singleton
59 protected Adblock()
60 {
61 // prevents instantiation
62 }
63
64 /**
65 * Use to get Adblock instance
66 * @return adblock instance
67 */
68 public static synchronized Adblock get()
69 {
70 if (_instance == null)
71 {
72 _instance = new Adblock();
73 }
74
75 return _instance;
76 }
77
78 public AdblockEngine getEngine()
79 {
80 return engine;
81 }
82
83 public AdblockSettingsStorage getStorage()
84 {
85 return storage;
86 }
87
88 /**
89 * Init with context
90 * @param context application context
91 * @param developmentBuild debug or release?
92 * @param preferenceName Shared Preferences name
93 */
94 public void init(Context context, boolean developmentBuild, String preferenceN ame)
95 {
96 this.context = context.getApplicationContext();
97 this.developmentBuild = developmentBuild;
98 this.preferenceName = preferenceName;
99 }
100
101 private void createAdblock()
102 {
103 Log.d(TAG, "Creating adblock engine ...");
104
105 // read and apply current settings
106 SharedPreferences prefs = context.getSharedPreferences(preferenceName, Conte xt.MODE_PRIVATE);
107 storage = new AdblockSettingsSharedPrefsStorage(prefs);
108
109 // latch is required for async (see `waitForReady()`)
110 engineCreated = new CountDownLatch(1);
111
112 engine = AdblockEngine.create(
113 AdblockEngine.generateAppInfo(context, developmentBuild),
114 context.getCacheDir().getAbsolutePath(),
115 true); // `true` as we need element hiding
116 Log.d(TAG, "Adblock engine created");
117
118 AdblockSettings settings = storage.load();
119 if (settings != null)
120 {
121 Log.d(TAG, "Applying saved adblock settings to adblock engine");
122 // apply last saved settings to adblock engine
123
124 // all the settings except `enabled` and whitelisted domains are saved by adblock engine itself
125 engine.setEnabled(settings.isAdblockEnabled());
126 engine.setWhitelistedDomains(settings.getWhitelistedDomains());
127 }
128 else
129 {
130 Log.w(TAG, "No saved adblock settings");
131 }
132
133 // unlock waiting client thread
134 engineCreated.countDown();
135 }
136
137 /**
138 * Wait until everything is ready (used for `retain(true)`)
139 * Warning: locks current thread
140 */
141 public void waitForReady()
142 {
143 if (engineCreated == null)
144 {
145 throw new RuntimeException("Adblock Plus usage exception: call retain(...) first");
146 }
147
148 try
149 {
150 Log.d(TAG, "Waiting for ready ...");
151 engineCreated.await();
152 Log.d(TAG, "Ready");
153 }
154 catch (InterruptedException e)
155 {
156 Log.w(TAG, "Interrupted", e);
157 }
158 }
159
160 private void disposeAdblock()
161 {
162 Log.w(TAG, "Disposing adblock engine");
163
164 engine.dispose();
165 engine = null;
166
167 // to unlock waiting client in WaitForReady()
168 engineCreated.countDown();
169 engineCreated = null;
170
171 storage = null;
172 }
173
174 /**
175 * Get registered clients count
176 * @return registered clients count
177 */
178 public int getCounter()
179 {
180 return referenceCounter.get();
181 }
182
183 /**
184 * Register Adblock engine client
185 * @param asynchronous If `true` engines will be created in background thread without locking of
186 * current thread. Use waitForReady() before getEngine() l ater.
187 * If `false` locks current thread.
188 */
189 public synchronized void retain(boolean asynchronous)
190 {
191 if (referenceCounter.getAndIncrement() == 0)
192 {
193 if (!asynchronous)
194 {
195 createAdblock();
196 }
197 else
198 {
199 new Thread(new Runnable()
200 {
201 @Override
202 public void run()
203 {
204 createAdblock();
205 }
206 }).start();
207 }
208 }
209 }
210
211 /**
212 * Unregister Adblock engine client
213 */
214 public synchronized void release()
215 {
216 if (referenceCounter.decrementAndGet() == 0)
217 {
218 waitForReady();
219 disposeAdblock();
220 }
221 }
222 }
OLDNEW

Powered by Google App Engine
This is Rietveld