Index: mobile/android/thirdparty/org/adblockplus/browser/AddonBridge.java |
diff --git a/mobile/android/thirdparty/org/adblockplus/browser/AddonBridge.java b/mobile/android/thirdparty/org/adblockplus/browser/AddonBridge.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a1ea998409ec38b03e2161ec519092b15ef45a59 |
--- /dev/null |
+++ b/mobile/android/thirdparty/org/adblockplus/browser/AddonBridge.java |
@@ -0,0 +1,213 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2015 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+package org.adblockplus.browser; |
+ |
+import android.annotation.SuppressLint; |
+import android.util.Log; |
+ |
+import org.json.JSONException; |
+import org.json.JSONObject; |
+import org.mozilla.gecko.GeckoAppShell; |
+import org.mozilla.gecko.util.GeckoRequest; |
+import org.mozilla.gecko.util.NativeJSObject; |
+ |
+@SuppressLint("DefaultLocale") |
+public class AddonBridge |
+{ |
+ private static final String TAG = "AdblockBrowser.AddonBridge"; |
+ private static final String REQUEST_NAME = "AdblockPlus:Api"; |
+ |
+ public static boolean getBooleanFromJSObject(final NativeJSObject obj, final String name, |
+ final boolean defaultValue) |
+ { |
+ try |
+ { |
+ return obj.getBoolean(name); |
+ } |
+ catch (final Exception e) |
+ { |
+ return defaultValue; |
+ } |
+ } |
+ |
+ private static JSONObject createRequestData(final String action) |
+ { |
+ final JSONObject obj = new JSONObject(); |
+ try |
+ { |
+ obj.put("action", action.toLowerCase()); |
+ } |
+ catch (JSONException e) |
+ { |
+ // we're only adding sane objects |
+ Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
+ } |
+ return obj; |
+ } |
+ |
+ private static JSONObject createRequestData(final String action, final boolean enable) |
+ { |
+ final JSONObject obj = new JSONObject(); |
+ try |
+ { |
+ obj.put("action", action.toLowerCase()); |
+ obj.put("enable", enable); |
+ } |
+ catch (JSONException e) |
+ { |
+ // we're only adding sane objects |
+ Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
+ } |
+ return obj; |
+ } |
+ |
+ public static void queryBoolean(final AdblockPlusApiCallback callback, final String action) |
+ { |
+ Log.d(TAG, "queryBoolean for " + action); |
+ GeckoAppShell.sendRequestToGecko( |
+ new ChainedRequest( |
+ createRequestData(action), |
+ callback, |
+ true)); |
+ } |
+ |
+ public static void setBoolean(final AdblockPlusApiCallback callback, final String action, |
+ final boolean enable) |
+ { |
+ Log.d(TAG, "setBoolean " + enable + " for " + action); |
+ GeckoAppShell.sendRequestToGecko( |
+ new ChainedRequest( |
+ createRequestData(action, enable), |
+ callback, |
+ true)); |
+ } |
+ |
+ private static class ChainedRequest extends GeckoRequest |
+ { |
+ private final JSONObject value; |
+ private final AdblockPlusApiCallback apiCallback; |
+ private final boolean initCheck; |
+ |
+ public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback callback, |
+ final boolean checkInitState) |
+ { |
+ super(AddonBridge.REQUEST_NAME, |
+ checkInitState ? createRequestData("query_ready_state") : value); |
+ this.value = value; |
+ this.apiCallback = callback; |
+ this.initCheck = checkInitState; |
+ } |
+ |
+ private void callSuccessFunction(final NativeJSObject jsObject) |
+ { |
+ try |
+ { |
+ if(this.apiCallback != null) |
+ { |
+ this.apiCallback.onApiRequestSucceeded(jsObject); |
+ } |
+ } |
+ // Yes, catch all, yes, thank you for this totally useless NativeJSObject! |
+ catch (final Exception e) |
+ { |
+ callFailureFunction("unknown error"); |
+ } |
+ } |
+ |
+ private void callFailureFunction(final String msg) |
+ { |
+ if (this.apiCallback != null) |
+ { |
+ this.apiCallback.onApiRequestFailed(msg); |
+ } |
+ } |
+ |
+ private void callFailureFunction(final NativeJSObject jsObject) |
+ { |
+ try |
+ { |
+ callFailureFunction(jsObject.getString("error")); |
+ } |
+ // Yes, catch all, yes, thank you for this totally useless NativeJSObject! |
+ catch (final Exception e) |
+ { |
+ callFailureFunction("unknown error"); |
+ } |
+ } |
+ |
+ @Override |
+ public void onError() |
+ { |
+ if (this.initCheck) |
+ { |
+ GeckoAppShell.sendRequestToGecko( |
+ new ChainedRequest(this.value, this.apiCallback, true)); |
+ } |
+ else |
+ { |
+ this.callFailureFunction("GeckoRequest error"); |
+ } |
+ } |
+ |
+ @Override |
+ public void onResponse(final NativeJSObject jsObject) |
+ { |
+ if (this.initCheck) |
+ { |
+ try |
+ { |
+ if (jsObject.getBoolean("success") && jsObject.getBoolean("value")) |
+ { |
+ GeckoAppShell.sendRequestToGecko( |
+ new ChainedRequest(this.value, this.apiCallback, false)); |
+ } |
+ else |
+ { |
+ GeckoAppShell.sendRequestToGecko( |
+ new ChainedRequest(this.value, this.apiCallback, true)); |
+ } |
+ } |
+ catch (Exception e) |
+ { |
+ GeckoAppShell.sendRequestToGecko( |
+ new ChainedRequest(this.value, this.apiCallback, true)); |
+ } |
+ } |
+ else |
+ { |
+ try |
+ { |
+ if (jsObject.getBoolean("success")) |
+ { |
+ this.callSuccessFunction(jsObject); |
+ } |
+ else |
+ { |
+ this.callFailureFunction(jsObject); |
+ } |
+ } |
+ // Yes, catch all, yes, thank you for this totally useless |
+ // NativeJSObject! |
+ catch (final Exception e) |
+ { |
+ callFailureFunction("unknown error"); |
+ } |
+ } |
+ } |
+ } |
+} |