| 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 12 matching lines...) Expand all Loading... |
| 23 import android.annotation.SuppressLint; | 23 import android.annotation.SuppressLint; |
| 24 import android.content.Context; | 24 import android.content.Context; |
| 25 import android.content.SharedPreferences; | 25 import android.content.SharedPreferences; |
| 26 import android.os.Handler; | 26 import android.os.Handler; |
| 27 import android.os.HandlerThread; | 27 import android.os.HandlerThread; |
| 28 import android.util.Log; | 28 import android.util.Log; |
| 29 | 29 |
| 30 import org.json.JSONArray; | 30 import org.json.JSONArray; |
| 31 import org.json.JSONException; | 31 import org.json.JSONException; |
| 32 import org.mozilla.gecko.EventDispatcher; | 32 import org.mozilla.gecko.EventDispatcher; |
| 33 import org.mozilla.gecko.GeckoAppShell; | |
| 34 import org.mozilla.gecko.util.BundleEventListener; | 33 import org.mozilla.gecko.util.BundleEventListener; |
| 35 import org.mozilla.gecko.util.EventCallback; | 34 import org.mozilla.gecko.util.EventCallback; |
| 36 import org.mozilla.gecko.util.GeckoBundle; | 35 import org.mozilla.gecko.util.GeckoBundle; |
| 37 | 36 |
| 38 @SuppressLint("DefaultLocale") | 37 @SuppressLint("DefaultLocale") |
| 39 public class AddOnBridge | 38 public class ExtensionBridge |
| 40 { | 39 { |
| 41 private static final String TAG = "AdblockBrowser.AddOnBridge"; | 40 private static final String TAG = "AdblockBrowser.ExtensionBridge"; |
| 42 private static final String REQUEST_NAME = "AdblockPlus:Api"; | 41 private static final String REQUEST_NAME = "Abb:Api"; |
| 43 // Handler+HandlerThread for posting delayed re-tries without interfering with | 42 // Handler+HandlerThread for posting delayed re-tries without interfering with |
| 44 // other threads (e.g. the UI or Gecko thread) | 43 // other threads (e.g. the UI or Gecko thread) |
| 45 private static final HandlerThread PRIVATE_HANDLER_THREAD; | 44 private static final HandlerThread PRIVATE_HANDLER_THREAD; |
| 46 private static final Handler PRIVATE_HANDLER; | 45 private static final Handler PRIVATE_HANDLER; |
| 47 // Global handler, for e.g. UI tasks | 46 // Global handler, for e.g. UI tasks |
| 48 private static final HandlerThread GLOBAL_HANDLER_THREAD; | 47 private static final HandlerThread GLOBAL_HANDLER_THREAD; |
| 49 private static final Handler GLOBAL_HANDLER; | 48 private static final Handler GLOBAL_HANDLER; |
| 50 // Sometimes, the app is killed before the extension is able to save all chang
es regarding | 49 // Sometimes, the app is killed before the extension is able to save all chang
es regarding |
| 51 // AddOnBridge requests. Given that, we need to store the pending requests on
SharedPrefs, | 50 // ExtensionBridge requests. Given that, we need to store the pending requests
on SharedPrefs, |
| 52 // so we can resend them to the extension once the app restarts | 51 // so we can resend them to the extension once the app restarts |
| 53 // See https://issues.adblockplus.org/ticket/2853 | 52 // See https://issues.adblockplus.org/ticket/2853 |
| 54 private static final AddOnEventListener ADD_ON_EVENT_LISTENER = new AddOnEvent
Listener(); | 53 private static final AddOnEventListener ADD_ON_EVENT_LISTENER = new AddOnEvent
Listener(); |
| 55 private static final String ON_FILTERS_LOAD_EVENT = "Abb:OnFiltersLoad"; | 54 private static final String ON_LOADED_EVENT = "Abb:OnLoaded"; |
| 56 private static final String ON_FILTERS_SAVE_EVENT = "Abb:OnFiltersSave"; | 55 private static final String ON_FILTERS_SAVED_EVENT = "Abb:OnFiltersSaved"; |
| 57 private static final List<GeckoBundle> PENDING_REQUESTS = new ArrayList<>(); | 56 private static final List<GeckoBundle> PENDING_REQUESTS = new ArrayList<>(); |
| 58 private static final String PENDING_REQUESTS_PREFS_KEY = "PENDING_REQUESTS_PRE
FS_KEY"; | 57 private static final String PENDING_REQUESTS_PREFS_KEY = "PENDING_REQUESTS_PRE
FS_KEY"; |
| 59 | 58 |
| 60 private static SharedPreferences sharedPrefs; | 59 private static SharedPreferences sharedPrefs; |
| 61 private static boolean filtersLoaded; | 60 private static boolean filtersLoaded; |
| 62 | 61 |
| 63 static | 62 static |
| 64 { | 63 { |
| 65 PRIVATE_HANDLER_THREAD = new HandlerThread("abp-private-handler"); | 64 PRIVATE_HANDLER_THREAD = new HandlerThread("abp-private-handler"); |
| 66 PRIVATE_HANDLER_THREAD.setDaemon(true); | 65 PRIVATE_HANDLER_THREAD.setDaemon(true); |
| 67 PRIVATE_HANDLER_THREAD.start(); | 66 PRIVATE_HANDLER_THREAD.start(); |
| 68 PRIVATE_HANDLER = new Handler(PRIVATE_HANDLER_THREAD.getLooper()); | 67 PRIVATE_HANDLER = new Handler(PRIVATE_HANDLER_THREAD.getLooper()); |
| 69 | 68 |
| 70 GLOBAL_HANDLER_THREAD = new HandlerThread("abp-global-handler"); | 69 GLOBAL_HANDLER_THREAD = new HandlerThread("abp-global-handler"); |
| 71 GLOBAL_HANDLER_THREAD.setDaemon(true); | 70 GLOBAL_HANDLER_THREAD.setDaemon(true); |
| 72 GLOBAL_HANDLER_THREAD.start(); | 71 GLOBAL_HANDLER_THREAD.start(); |
| 73 GLOBAL_HANDLER = new Handler(GLOBAL_HANDLER_THREAD.getLooper()); | 72 GLOBAL_HANDLER = new Handler(GLOBAL_HANDLER_THREAD.getLooper()); |
| 74 } | 73 } |
| 75 | 74 |
| 76 public static void init(Context context) | 75 public static void init(Context context) |
| 77 { | 76 { |
| 78 sharedPrefs = context.getSharedPreferences(AddOnBridge.class.getName(), Cont
ext.MODE_PRIVATE); | 77 sharedPrefs = context.getSharedPreferences(ExtensionBridge.class.getName(),
Context.MODE_PRIVATE); |
| 79 EventDispatcher.getInstance().registerGeckoThreadListener( | 78 EventDispatcher.getInstance().registerGeckoThreadListener( |
| 80 ADD_ON_EVENT_LISTENER, ON_FILTERS_LOAD_EVENT, ON_FILTERS_SAVE_EVENT); | 79 ADD_ON_EVENT_LISTENER, ON_LOADED_EVENT, ON_FILTERS_SAVED_EVENT); |
| 81 loadPendingRequests(); | 80 loadPendingRequests(); |
| 82 } | 81 } |
| 83 | 82 |
| 84 public static void postToHandler(Runnable runnable) | 83 public static void postToHandler(Runnable runnable) |
| 85 { | 84 { |
| 86 GLOBAL_HANDLER.post(runnable); | 85 GLOBAL_HANDLER.post(runnable); |
| 87 } | 86 } |
| 88 | 87 |
| 89 public static void postToHandlerDelayed(Runnable runnable, long delayMillis) | 88 public static void postToHandlerDelayed(Runnable runnable, long delayMillis) |
| 90 { | 89 { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 103 final GeckoBundle data = new GeckoBundle(); | 102 final GeckoBundle data = new GeckoBundle(); |
| 104 data.putBoolean("enable", enable); | 103 data.putBoolean("enable", enable); |
| 105 callFunction("set" + makeFirstCharacterUppercase(name), data, true, callback
); | 104 callFunction("set" + makeFirstCharacterUppercase(name), data, true, callback
); |
| 106 } | 105 } |
| 107 | 106 |
| 108 public static void querySubscriptionListStatus(String url, AdblockPlusApiCallb
ack callback) | 107 public static void querySubscriptionListStatus(String url, AdblockPlusApiCallb
ack callback) |
| 109 { | 108 { |
| 110 Log.d(TAG, "querySubscriptionListStatus for " + url); | 109 Log.d(TAG, "querySubscriptionListStatus for " + url); |
| 111 final GeckoBundle data = new GeckoBundle(); | 110 final GeckoBundle data = new GeckoBundle(); |
| 112 data.putString("url", url); | 111 data.putString("url", url); |
| 113 callFunction("isSubscriptionListed", data, callback); | 112 callFunction("isSubscriptionEnabled", data, callback); |
| 114 } | 113 } |
| 115 | 114 |
| 116 public static void addSubscription(String url, String title, AdblockPlusApiCal
lback callback) | 115 public static void addSubscription(String url, String title, AdblockPlusApiCal
lback callback) |
| 117 { | 116 { |
| 118 Log.d(TAG, "addSubscription for " + url + " (" + title + ")"); | 117 Log.d(TAG, "addSubscription for " + url + " (" + title + ")"); |
| 119 final GeckoBundle data = new GeckoBundle(); | 118 final GeckoBundle data = new GeckoBundle(); |
| 120 data.putString("url", url); | 119 data.putString("url", url); |
| 121 if (title != null) | 120 if (title != null) |
| 122 { | 121 { |
| 123 data.putString("title", title); | 122 data.putString("title", title); |
| 124 } | 123 } |
| 125 callFunction("addSubscription", data, true, callback); | 124 callFunction("addSubscription", data, true, callback); |
| 126 } | 125 } |
| 127 | 126 |
| 128 public static void queryActiveSubscriptions(AdblockPlusApiCallback callback) | 127 public static void queryActiveSubscriptions(AdblockPlusApiCallback callback) |
| 129 { | 128 { |
| 130 Log.d(TAG, "queryActiveSubscriptions"); | 129 Log.d(TAG, "queryActiveSubscriptions"); |
| 131 callFunction("getActiveSubscriptions", null, callback); | 130 callFunction("getEnabledSubscriptions", null, callback); |
| 132 } | 131 } |
| 133 | 132 |
| 134 public static void removeSubscription(String url, AdblockPlusApiCallback callb
ack) | 133 public static void removeSubscription(String url, AdblockPlusApiCallback callb
ack) |
| 135 { | 134 { |
| 136 Log.d(TAG, "removeSubscription for " + url); | 135 Log.d(TAG, "removeSubscription for " + url); |
| 137 final GeckoBundle data = new GeckoBundle(); | 136 final GeckoBundle data = new GeckoBundle(); |
| 138 data.putString("url", url); | 137 data.putString("url", url); |
| 139 callFunction("removeSubscription", data, true, callback); | 138 callFunction("removeSubscription", data, true, callback); |
| 140 } | 139 } |
| 141 | 140 |
| 142 public static void queryWhitelistedWebsites(final AdblockPlusApiCallback callb
ack) { | 141 public static void queryWhitelistedWebsites(final AdblockPlusApiCallback callb
ack) { |
| 143 Log.d(TAG, "queryWhitelistedWebsites"); | 142 Log.d(TAG, "queryWhitelistedWebsites"); |
| 144 callFunction("getWhitelistedWebsites", null, callback); | 143 callFunction("getWhitelistedDomains", null, callback); |
| 145 } | 144 } |
| 146 | 145 |
| 147 public static void queryIsWebsiteWhitelisted(String url, AdblockPlusApiCallbac
k callback) | 146 public static void queryIsWebsiteWhitelisted(String url, AdblockPlusApiCallbac
k callback) |
| 148 { | 147 { |
| 149 Log.d(TAG, "queryIsWebsiteWhitelisted for " + url); | 148 Log.d(TAG, "queryIsWebsiteWhitelisted for " + url); |
| 150 final GeckoBundle data = new GeckoBundle(); | 149 final GeckoBundle data = new GeckoBundle(); |
| 151 data.putString("url", url); | 150 data.putString("url", url); |
| 152 data.putString("host", UrlUtils.getHostFromUrl(url)); | 151 data.putString("host", UrlUtils.getHostFromUrl(url)); |
| 153 callFunction("isWebsiteWhitelisted", data, callback); | 152 callFunction("isDomainWhitelisted", data, callback); |
| 154 } | 153 } |
| 155 | 154 |
| 156 public static void whitelistWebsite(String url, boolean whitelisted, AdblockPl
usApiCallback callback) | 155 public static void whitelistWebsite(String url, boolean whitelisted, AdblockPl
usApiCallback callback) |
| 157 { | 156 { |
| 158 Log.d(TAG, "whitelistWebsite for " + url); | 157 Log.d(TAG, "whitelistWebsite for " + url); |
| 159 final GeckoBundle data = new GeckoBundle(); | 158 final GeckoBundle data = new GeckoBundle(); |
| 160 data.putString("url", UrlUtils.formatUrl(url)); | 159 data.putString("url", UrlUtils.formatUrl(url)); |
| 161 data.putString("host", UrlUtils.getHostFromUrl(url)); | 160 data.putString("host", UrlUtils.getHostFromUrl(url)); |
| 162 data.putBoolean("whitelisted", whitelisted); | 161 data.putBoolean("enable", whitelisted); |
| 163 callFunction("whitelistWebsite", data, true, callback); | 162 callFunction("whitelistDomain", data, true, callback); |
| 164 } | 163 } |
| 165 | 164 |
| 166 private static String makeFirstCharacterUppercase(String str) | 165 private static String makeFirstCharacterUppercase(String str) |
| 167 { | 166 { |
| 168 if (Character.isUpperCase(str.charAt(0))) | 167 if (Character.isUpperCase(str.charAt(0))) |
| 169 { | 168 { |
| 170 return str; | 169 return str; |
| 171 } | 170 } |
| 172 return Character.toString(Character.toUpperCase(str.charAt(0))) + str.substr
ing(1); | 171 return Character.toString(Character.toUpperCase(str.charAt(0))) + str.substr
ing(1); |
| 173 } | 172 } |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 callback.onApiRequestFailed(error); | 340 callback.onApiRequestFailed(error); |
| 342 } | 341 } |
| 343 }); | 342 }); |
| 344 } | 343 } |
| 345 | 344 |
| 346 private static class AddOnEventListener implements BundleEventListener | 345 private static class AddOnEventListener implements BundleEventListener |
| 347 { | 346 { |
| 348 @Override | 347 @Override |
| 349 public void handleMessage(String event, GeckoBundle message, EventCallback c
allback) | 348 public void handleMessage(String event, GeckoBundle message, EventCallback c
allback) |
| 350 { | 349 { |
| 351 if (ON_FILTERS_LOAD_EVENT.equals(event)) | 350 Log.d(TAG, "handleMessage event: " + event + " message: " + message); |
| 351 if (ON_LOADED_EVENT.equals(event)) |
| 352 { | 352 { |
| 353 // The filters have been loaded by the extension. Given that, we can sen
d all pending requests | 353 // The extension has been loaded. Given that, we can send all pending re
quests |
| 354 filtersLoaded = true; | 354 filtersLoaded = true; |
| 355 sendPendingRequests(); | 355 sendPendingRequests(); |
| 356 } | 356 } |
| 357 else if (ON_FILTERS_SAVE_EVENT.equals(event)) | 357 else if (ON_FILTERS_SAVED_EVENT.equals(event)) |
| 358 { | 358 { |
| 359 // All changes have been saved by the extension. That way, we can clear
our list of | 359 // All changes have been saved by the extension. That way, we can clear
our list of |
| 360 // pending requests | 360 // pending requests |
| 361 // See https://issues.adblockplus.org/ticket/2853 | 361 // See https://issues.adblockplus.org/ticket/2853 |
| 362 clearPendingRequests(); | 362 clearPendingRequests(); |
| 363 } | 363 } |
| 364 } | 364 } |
| 365 } | 365 } |
| 366 } | 366 } |
| OLD | NEW |