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 | 17 |
18 package org.adblockplus.browser; | 18 package org.adblockplus.browser; |
19 | 19 |
20 import java.io.IOException; | 20 import java.io.IOException; |
21 import java.io.StringReader; | 21 import java.io.StringReader; |
22 import java.lang.ref.WeakReference; | 22 import java.lang.ref.WeakReference; |
23 import java.util.ArrayList; | 23 import java.util.ArrayList; |
24 import java.util.HashMap; | 24 import java.util.HashMap; |
25 import java.util.Iterator; | 25 import java.util.Iterator; |
26 import java.util.List; | 26 import java.util.List; |
27 import java.util.concurrent.ConcurrentHashMap; | 27 import java.util.concurrent.ConcurrentHashMap; |
28 import java.util.concurrent.Semaphore; | 28 import java.util.concurrent.Semaphore; |
29 | 29 |
30 import org.mozilla.gecko.util.NativeJSObject; | 30 import org.mozilla.gecko.util.GeckoBundle; |
31 import org.mozilla.gecko.util.ThreadUtils; | 31 import org.mozilla.gecko.util.ThreadUtils; |
32 import org.xmlpull.v1.XmlPullParser; | 32 import org.xmlpull.v1.XmlPullParser; |
33 import org.xmlpull.v1.XmlPullParserException; | 33 import org.xmlpull.v1.XmlPullParserException; |
34 | 34 |
35 import android.util.Log; | 35 import android.util.Log; |
36 import android.util.Xml; | 36 import android.util.Xml; |
37 | 37 |
38 final class SubscriptionContainer implements AdblockPlusApiCallback | 38 final class SubscriptionContainer implements AdblockPlusApiCallback |
39 { | 39 { |
40 private static final String TAG = SubscriptionContainer.class.getName(); | 40 private static final String TAG = SubscriptionContainer.class.getName(); |
41 private final ConcurrentHashMap<String, Boolean> enableState = new ConcurrentH
ashMap<>(); | 41 private final ConcurrentHashMap<String, Boolean> enableState = new ConcurrentH
ashMap<>(); |
42 private final Semaphore entriesReady = new Semaphore(0); | 42 private final Semaphore entriesReady = new Semaphore(0); |
43 private final List<SubscriptionContainer.Subscription> entries = new ArrayList
<>(); | 43 private final List<SubscriptionContainer.Subscription> entries = new ArrayList
<>(); |
44 private final HashMap<String, SubscriptionContainer.Subscription> urlMap = new
HashMap<>(); | 44 private final HashMap<String, SubscriptionContainer.Subscription> urlMap = new
HashMap<>(); |
45 private final List<WeakReference<SubscriptionListener>> subscriptionListeners
= new ArrayList<>(); | 45 private final List<WeakReference<SubscriptionListener>> subscriptionListeners
= new ArrayList<>(); |
46 | 46 |
47 private SubscriptionContainer() | 47 private SubscriptionContainer() |
48 { | 48 { |
49 // prevent external instantiation | 49 // prevent external instantiation |
50 } | 50 } |
51 | 51 |
52 public final static SubscriptionContainer create() | 52 public final static SubscriptionContainer create() |
53 { | 53 { |
54 return create(true); | 54 return create(true); |
55 } | 55 } |
56 | 56 |
57 public final static SubscriptionContainer create(final boolean refresh) | 57 public final static SubscriptionContainer create(final boolean refresh) |
58 { | 58 { |
59 final SubscriptionContainer sc = new SubscriptionContainer(); | 59 final SubscriptionContainer sc = new SubscriptionContainer(); |
60 AddOnBridge.queryValue(sc, "subscriptionsXml"); | 60 AddOnBridge.queryValue("subscriptionsXml", sc); |
61 sc.entriesReady.acquireUninterruptibly(); | 61 sc.entriesReady.acquireUninterruptibly(); |
62 | 62 |
63 for (final SubscriptionContainer.Subscription e : sc.entries) | 63 for (final SubscriptionContainer.Subscription e : sc.entries) |
64 { | 64 { |
65 sc.urlMap.put(e.url, e); | 65 sc.urlMap.put(e.url, e); |
66 sc.enableState.put(e.url, Boolean.FALSE); | 66 sc.enableState.put(e.url, Boolean.FALSE); |
67 } | 67 } |
68 | 68 |
69 if (refresh) | 69 if (refresh) |
70 { | 70 { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 } | 146 } |
147 } | 147 } |
148 | 148 |
149 if (shouldAddListener) | 149 if (shouldAddListener) |
150 { | 150 { |
151 this.subscriptionListeners.add(new WeakReference<>(listener)); | 151 this.subscriptionListeners.add(new WeakReference<>(listener)); |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 @Override | 155 @Override |
156 public void onApiRequestSucceeded(NativeJSObject jsObject) | 156 public void onApiRequestSucceeded(GeckoBundle bundle) |
157 { | 157 { |
158 final XmlPullParser parser = Xml.newPullParser(); | 158 final XmlPullParser parser = Xml.newPullParser(); |
159 try | 159 try |
160 { | 160 { |
161 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); | 161 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false); |
162 parser.setInput(new StringReader(AddOnBridge.getStringFromJsObject(jsObjec
t, "value", ""))); | 162 parser.setInput(new StringReader(bundle.getString("value", ""))); |
163 parser.nextTag(); | 163 parser.nextTag(); |
164 parser.require(XmlPullParser.START_TAG, null, "subscriptions"); | 164 parser.require(XmlPullParser.START_TAG, null, "subscriptions"); |
165 while (parser.next() != XmlPullParser.END_TAG) | 165 while (parser.next() != XmlPullParser.END_TAG) |
166 { | 166 { |
167 if (parser.getEventType() != XmlPullParser.START_TAG) | 167 if (parser.getEventType() != XmlPullParser.START_TAG) |
168 { | 168 { |
169 continue; | 169 continue; |
170 } | 170 } |
171 if ("subscription".equals(parser.getName())) | 171 if ("subscription".equals(parser.getName())) |
172 { | 172 { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 final SubscriptionChangeAction.Mode mode) | 228 final SubscriptionChangeAction.Mode mode) |
229 { | 229 { |
230 return new SubscriptionChangeAction(subscription, parent, mode).post(); | 230 return new SubscriptionChangeAction(subscription, parent, mode).post(); |
231 } | 231 } |
232 | 232 |
233 public SubscriptionContainer.SubscriptionChangeAction post() | 233 public SubscriptionContainer.SubscriptionChangeAction post() |
234 { | 234 { |
235 switch (this.mode) | 235 switch (this.mode) |
236 { | 236 { |
237 case QUERY_SUBSCRIPTION_ENABLED: | 237 case QUERY_SUBSCRIPTION_ENABLED: |
238 AddOnBridge.querySubscriptionListStatus(this, this.subscription.url); | 238 AddOnBridge.querySubscriptionListStatus(this.subscription.url, this); |
239 break; | 239 break; |
240 case ENABLE_SUBSCRIPTION: | 240 case ENABLE_SUBSCRIPTION: |
241 AddOnBridge.addSubscription(this, this.subscription.url, this.subscrip
tion.title); | 241 AddOnBridge.addSubscription(this.subscription.url, this.subscription.t
itle, this); |
242 break; | 242 break; |
243 case DISABLE_SUBSCRIPTION: | 243 case DISABLE_SUBSCRIPTION: |
244 AddOnBridge.removeSubscription(this, this.subscription.url); | 244 AddOnBridge.removeSubscription(this.subscription.url, this); |
245 break; | 245 break; |
246 default: | 246 default: |
247 break; | 247 break; |
248 } | 248 } |
249 return this; | 249 return this; |
250 } | 250 } |
251 | 251 |
252 @Override | 252 @Override |
253 public void onApiRequestSucceeded(NativeJSObject jsObject) | 253 public void onApiRequestSucceeded(GeckoBundle bundle) |
254 { | 254 { |
255 switch (this.mode) | 255 switch (this.mode) |
256 { | 256 { |
257 case QUERY_SUBSCRIPTION_ENABLED: | 257 case QUERY_SUBSCRIPTION_ENABLED: |
258 try | 258 try |
259 { | 259 { |
260 this.parent.enableState.put(this.subscription.url, | 260 this.parent.enableState.put(this.subscription.url, bundle.getBoolean
("value")); |
261 Boolean.valueOf(AddOnBridge.getBooleanFromJsObject(jsObject, "va
lue", false))); | |
262 } | 261 } |
263 finally | 262 finally |
264 { | 263 { |
265 this.parent.entriesReady.release(); | 264 this.parent.entriesReady.release(); |
266 } | 265 } |
267 break; | 266 break; |
268 case ENABLE_SUBSCRIPTION: | 267 case ENABLE_SUBSCRIPTION: |
269 case DISABLE_SUBSCRIPTION: | 268 case DISABLE_SUBSCRIPTION: |
270 this.parent.enableState.put(this.subscription.url, this.mode == Mode.E
NABLE_SUBSCRIPTION); | 269 this.parent.enableState.put(this.subscription.url, this.mode == Mode.E
NABLE_SUBSCRIPTION); |
271 final Iterator<WeakReference<SubscriptionListener>> iterator = this.pa
rent.subscriptionListeners.iterator(); | 270 final Iterator<WeakReference<SubscriptionListener>> iterator = this.pa
rent.subscriptionListeners.iterator(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 { | 331 { |
333 return this.specialization + " (" + this.title + ") @ " + this.url; | 332 return this.specialization + " (" + this.title + ") @ " + this.url; |
334 } | 333 } |
335 } | 334 } |
336 | 335 |
337 public interface SubscriptionListener | 336 public interface SubscriptionListener |
338 { | 337 { |
339 void onSubscriptionUpdated(); | 338 void onSubscriptionUpdated(); |
340 } | 339 } |
341 } | 340 } |
OLD | NEW |