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 16 matching lines...) Expand all Loading... |
27 import android.content.SharedPreferences; | 27 import android.content.SharedPreferences; |
28 import android.os.Handler; | 28 import android.os.Handler; |
29 import android.os.HandlerThread; | 29 import android.os.HandlerThread; |
30 import android.util.Log; | 30 import android.util.Log; |
31 | 31 |
32 import org.json.JSONArray; | 32 import org.json.JSONArray; |
33 import org.json.JSONException; | 33 import org.json.JSONException; |
34 import org.json.JSONObject; | 34 import org.json.JSONObject; |
35 import org.mozilla.gecko.EventDispatcher; | 35 import org.mozilla.gecko.EventDispatcher; |
36 import org.mozilla.gecko.GeckoAppShell; | 36 import org.mozilla.gecko.GeckoAppShell; |
| 37 import org.mozilla.gecko.util.BundleEventListener; |
37 import org.mozilla.gecko.util.EventCallback; | 38 import org.mozilla.gecko.util.EventCallback; |
| 39 import org.mozilla.gecko.util.GeckoBundle; |
38 import org.mozilla.gecko.util.GeckoRequest; | 40 import org.mozilla.gecko.util.GeckoRequest; |
39 import org.mozilla.gecko.util.NativeEventListener; | |
40 import org.mozilla.gecko.util.NativeJSObject; | 41 import org.mozilla.gecko.util.NativeJSObject; |
41 | 42 |
42 @SuppressLint("DefaultLocale") | 43 @SuppressLint("DefaultLocale") |
43 public class AddOnBridge | 44 public class AddOnBridge |
44 { | 45 { |
45 private static final String TAG = "AdblockBrowser.AddOnBridge"; | 46 private static final String TAG = "AdblockBrowser.AddOnBridge"; |
46 private static final String REQUEST_NAME = "AdblockPlus:Api"; | 47 private static final String REQUEST_NAME = "AdblockPlus:Api"; |
47 // Handler+HandlerThread for posting delayed re-tries without interfering with | 48 // Handler+HandlerThread for posting delayed re-tries without interfering with |
48 // other threads (e.g. the UI or Gecko thread) | 49 // other threads (e.g. the UI or Gecko thread) |
49 private static final HandlerThread PRIVATE_HANDLER_THREAD; | 50 private static final HandlerThread PRIVATE_HANDLER_THREAD; |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 obj.put("enable", enable); | 256 obj.put("enable", enable); |
256 } | 257 } |
257 catch (JSONException e) | 258 catch (JSONException e) |
258 { | 259 { |
259 // we're only adding sane objects | 260 // we're only adding sane objects |
260 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); | 261 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
261 } | 262 } |
262 return obj; | 263 return obj; |
263 } | 264 } |
264 | 265 |
265 public static String makeFirstCharacterUppercase(String str) | 266 private static String makeFirstCharacterUppercase(String str) |
266 { | 267 { |
267 if (Character.isUpperCase(str.charAt(0))) | 268 if (Character.isUpperCase(str.charAt(0))) |
268 { | 269 { |
269 return str; | 270 return str; |
270 } | 271 } |
271 return Character.toString(Character.toUpperCase(str.charAt(0))) + str.substr
ing(1); | 272 return Character.toString(Character.toUpperCase(str.charAt(0))) + str.substr
ing(1); |
272 } | 273 } |
273 | 274 |
274 public static void queryValue(final AdblockPlusApiCallback callback, final Str
ing name) | 275 public static void queryValue(final AdblockPlusApiCallback callback, final Str
ing name) |
275 { | 276 { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 if (resendIfAborted) | 319 if (resendIfAborted) |
319 { | 320 { |
320 storePendingRequest(request); | 321 storePendingRequest(request); |
321 } | 322 } |
322 } | 323 } |
323 | 324 |
324 public static void querySubscriptionListStatus(final AdblockPlusApiCallback ca
llback, | 325 public static void querySubscriptionListStatus(final AdblockPlusApiCallback ca
llback, |
325 final String url) | 326 final String url) |
326 { | 327 { |
327 Log.d(TAG, "querySubscriptionListStatus for " + url); | 328 Log.d(TAG, "querySubscriptionListStatus for " + url); |
328 final Map<String, Object> parameters = new HashMap<String, Object>(); | 329 final Map<String, Object> parameters = new HashMap<>(); |
329 parameters.put("url", url); | 330 parameters.put("url", url); |
330 callFunction(callback, "isSubscriptionListed", parameters); | 331 callFunction(callback, "isSubscriptionListed", parameters); |
331 } | 332 } |
332 | 333 |
333 public static void addSubscription(final AdblockPlusApiCallback callback, | 334 public static void addSubscription(final AdblockPlusApiCallback callback, |
334 final String url, final String title) | 335 final String url, final String title) |
335 { | 336 { |
336 Log.d(TAG, "addSubscription for " + url + " (" + title + ")"); | 337 Log.d(TAG, "addSubscription for " + url + " (" + title + ")"); |
337 final Map<String, Object> parameters = new HashMap<String, Object>(); | 338 final Map<String, Object> parameters = new HashMap<>(); |
338 parameters.put("url", url); | 339 parameters.put("url", url); |
339 if (title != null) | 340 if (title != null) |
340 { | 341 { |
341 parameters.put("title", title); | 342 parameters.put("title", title); |
342 } | 343 } |
343 callFunction(callback, "addSubscription", parameters, true); | 344 callFunction(callback, "addSubscription", parameters, true); |
344 } | 345 } |
345 | 346 |
346 public static void queryActiveSubscriptions(final AdblockPlusApiCallback callb
ack) | 347 public static void queryActiveSubscriptions(final AdblockPlusApiCallback callb
ack) |
347 { | 348 { |
348 Log.d(TAG, "queryActiveSubscriptions"); | 349 Log.d(TAG, "queryActiveSubscriptions"); |
349 final Map<String, Object> parameters = new HashMap<String, Object>(); | 350 final Map<String, Object> parameters = new HashMap<>(); |
350 callFunction(callback, "getActiveSubscriptions", parameters); | 351 callFunction(callback, "getActiveSubscriptions", parameters); |
351 } | 352 } |
352 | 353 |
353 public static void removeSubscription(final AdblockPlusApiCallback callback, | 354 public static void removeSubscription(final AdblockPlusApiCallback callback, |
354 final String url) | 355 final String url) |
355 { | 356 { |
356 Log.d(TAG, "removeSubscription for " + url); | 357 Log.d(TAG, "removeSubscription for " + url); |
357 final Map<String, Object> parameters = new HashMap<String, Object>(); | 358 final Map<String, Object> parameters = new HashMap<>(); |
358 parameters.put("url", url); | 359 parameters.put("url", url); |
359 callFunction(callback, "removeSubscription", parameters, true); | 360 callFunction(callback, "removeSubscription", parameters, true); |
360 } | 361 } |
361 | 362 |
362 public static void queryIsLocal(final AdblockPlusApiCallback callback, | 363 public static void queryIsLocal(final AdblockPlusApiCallback callback, |
363 final String url) | 364 final String url) |
364 { | 365 { |
365 Log.d(TAG, "queryIsLocal for " + url); | 366 Log.d(TAG, "queryIsLocal for " + url); |
366 final Map<String, Object> parameters = new HashMap<String, Object>(); | 367 final Map<String, Object> parameters = new HashMap<>(); |
367 parameters.put("url", url); | 368 parameters.put("url", url); |
368 callFunction(callback, "isLocal", parameters); | 369 callFunction(callback, "isLocal", parameters); |
369 } | 370 } |
370 | 371 |
371 public static void queryIsPageWhitelisted(final AdblockPlusApiCallback callbac
k, | 372 public static void queryIsPageWhitelisted(final AdblockPlusApiCallback callbac
k, |
372 final String url) | 373 final String url) |
373 { | 374 { |
374 Log.d(TAG, "queryIsPageWhitelisted for " + url); | 375 Log.d(TAG, "queryIsPageWhitelisted for " + url); |
375 final Map<String, Object> parameters = new HashMap<String, Object>(); | 376 final Map<String, Object> parameters = new HashMap<>(); |
376 parameters.put("url", url); | 377 parameters.put("url", url); |
377 callFunction(callback, "isPageWhitelisted", parameters); | 378 callFunction(callback, "isPageWhitelisted", parameters); |
378 } | 379 } |
379 | 380 |
380 public static void whitelistSite(final AdblockPlusApiCallback callback, final
String url, | 381 public static void whitelistSite(final AdblockPlusApiCallback callback, final
String url, |
381 final boolean whitelisted) | 382 final boolean whitelisted) |
382 { | 383 { |
383 Log.d(TAG, "whitelistSite for " + url); | 384 Log.d(TAG, "whitelistSite for " + url); |
384 final Map<String, Object> parameters = new HashMap<String, Object>(); | 385 final Map<String, Object> parameters = new HashMap<>(); |
385 parameters.put("url", url); | 386 parameters.put("url", url); |
386 parameters.put("whitelisted", whitelisted); | 387 parameters.put("whitelisted", whitelisted); |
387 callFunction(callback, "whitelistSite", parameters, true); | 388 callFunction(callback, "whitelistSite", parameters, true); |
388 } | 389 } |
389 | 390 |
390 private static class AddOnRequest extends GeckoRequest | 391 private static class AddOnRequest extends GeckoRequest |
391 { | 392 { |
392 private final JSONObject value; | 393 private final JSONObject value; |
393 private final AdblockPlusApiCallback apiCallback; | 394 private final AdblockPlusApiCallback apiCallback; |
394 | 395 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 { | 443 { |
443 this.invokeSuccessCallback(jsObject); | 444 this.invokeSuccessCallback(jsObject); |
444 } | 445 } |
445 else | 446 else |
446 { | 447 { |
447 this.invokeFailureCallback(jsObject); | 448 this.invokeFailureCallback(jsObject); |
448 } | 449 } |
449 } | 450 } |
450 } | 451 } |
451 | 452 |
452 private static class AddOnEventListener implements NativeEventListener | 453 private static class AddOnEventListener implements BundleEventListener |
453 { | 454 { |
454 @Override | 455 @Override |
455 public void handleMessage(String event, NativeJSObject message, EventCallbac
k callback) | 456 public void handleMessage(String event, GeckoBundle message, EventCallback c
allback) |
456 { | 457 { |
457 if (ON_FILTERS_LOAD_EVENT.equals(event)) | 458 if (ON_FILTERS_LOAD_EVENT.equals(event)) |
458 { | 459 { |
459 // The filters have been loaded by the extension. Given that, we can sen
d all pending requests | 460 // The filters have been loaded by the extension. Given that, we can sen
d all pending requests |
460 filtersLoaded = true; | 461 filtersLoaded = true; |
461 sendPendingRequests(); | 462 sendPendingRequests(); |
462 } | 463 } |
463 else if (ON_FILTERS_SAVE_EVENT.equals(event)) | 464 else if (ON_FILTERS_SAVE_EVENT.equals(event)) |
464 { | 465 { |
465 // All changes have been saved by the extension. That way, we can clear
our list of | 466 // All changes have been saved by the extension. That way, we can clear
our list of |
466 // pending requests | 467 // pending requests |
467 // See https://issues.adblockplus.org/ticket/2853 | 468 // See https://issues.adblockplus.org/ticket/2853 |
468 clearPendingRequests(); | 469 clearPendingRequests(); |
469 } | 470 } |
470 } | 471 } |
471 } | 472 } |
472 } | 473 } |
OLD | NEW |