OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2015 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.browser; |
| 19 |
| 20 import android.annotation.SuppressLint; |
| 21 import android.os.Handler; |
| 22 import android.os.HandlerThread; |
| 23 import android.util.Log; |
| 24 |
| 25 import org.json.JSONException; |
| 26 import org.json.JSONObject; |
| 27 import org.mozilla.gecko.GeckoAppShell; |
| 28 import org.mozilla.gecko.util.GeckoRequest; |
| 29 import org.mozilla.gecko.util.NativeJSObject; |
| 30 |
| 31 @SuppressLint("DefaultLocale") |
| 32 public class AddOnBridge |
| 33 { |
| 34 private static final String TAG = "AdblockBrowser.AddOnBridge"; |
| 35 private static final String REQUEST_NAME = "AdblockPlus:Api"; |
| 36 // Timeout for checking filter loading (in seconds) |
| 37 private static final int QUERY_GET_FILTERS_LOADED_TIMEOUT = 30; |
| 38 // How long to wait between retries (in milliseconds) |
| 39 private static final int QUERY_GET_FILTERS_LOADED_DELAY = 500; |
| 40 // Handler+HandlerThread for posting delayed re-tries without interfering with |
| 41 // other threads (e.g. the UI or Gecko thread) |
| 42 private static final HandlerThread HANDLER_THREAD; |
| 43 private static final Handler HANDLER; |
| 44 |
| 45 static |
| 46 { |
| 47 HANDLER_THREAD = new HandlerThread("abp-bridge"); |
| 48 HANDLER_THREAD.setDaemon(true); |
| 49 HANDLER_THREAD.start(); |
| 50 HANDLER = new Handler(HANDLER_THREAD.getLooper()); |
| 51 } |
| 52 |
| 53 public static boolean getBooleanFromJsObject(final NativeJSObject obj, final S
tring name, |
| 54 final boolean defaultValue) |
| 55 { |
| 56 try |
| 57 { |
| 58 return obj.getBoolean(name); |
| 59 } |
| 60 catch (final Exception e) |
| 61 { |
| 62 return defaultValue; |
| 63 } |
| 64 } |
| 65 |
| 66 public static String getStringFromJsObject(final NativeJSObject obj, final Str
ing name, |
| 67 final String defaultValue) |
| 68 { |
| 69 try |
| 70 { |
| 71 return obj.getString(name); |
| 72 } |
| 73 catch (final Exception e) |
| 74 { |
| 75 return defaultValue; |
| 76 } |
| 77 } |
| 78 |
| 79 private static JSONObject createRequestData(final String action) |
| 80 { |
| 81 final JSONObject obj = new JSONObject(); |
| 82 try |
| 83 { |
| 84 obj.put("action", action); |
| 85 } |
| 86 catch (JSONException e) |
| 87 { |
| 88 // we're only adding sane objects |
| 89 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
| 90 } |
| 91 return obj; |
| 92 } |
| 93 |
| 94 private static JSONObject createRequestData(final String action, final boolean
enable) |
| 95 { |
| 96 final JSONObject obj = createRequestData(action); |
| 97 try |
| 98 { |
| 99 obj.put("enable", enable); |
| 100 } |
| 101 catch (JSONException e) |
| 102 { |
| 103 // we're only adding sane objects |
| 104 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
| 105 } |
| 106 return obj; |
| 107 } |
| 108 |
| 109 public static String makeFirstCharacterUppercase(String str) |
| 110 { |
| 111 if (Character.isUpperCase(str.charAt(0))) |
| 112 { |
| 113 return str; |
| 114 } |
| 115 return Character.toString(Character.toUpperCase(str.charAt(0))) + str.substr
ing(1); |
| 116 } |
| 117 |
| 118 public static void queryBoolean(final AdblockPlusApiCallback callback, final S
tring name) |
| 119 { |
| 120 Log.d(TAG, "queryBoolean for " + name); |
| 121 GeckoAppShell.sendRequestToGecko( |
| 122 new ChainedRequest( |
| 123 createRequestData("get" + makeFirstCharacterUppercase(name)), |
| 124 callback)); |
| 125 } |
| 126 |
| 127 public static void setBoolean(final AdblockPlusApiCallback callback, final Str
ing name, |
| 128 final boolean enable) |
| 129 { |
| 130 Log.d(TAG, "setBoolean " + enable + " for " + name); |
| 131 GeckoAppShell.sendRequestToGecko( |
| 132 new ChainedRequest( |
| 133 createRequestData("set" + makeFirstCharacterUppercase(name), enable)
, |
| 134 callback)); |
| 135 } |
| 136 |
| 137 private static class ChainedRequest extends GeckoRequest |
| 138 { |
| 139 private final JSONObject value; |
| 140 private final AdblockPlusApiCallback apiCallback; |
| 141 private final boolean checkForFiltersLoaded; |
| 142 private final long creationTime; |
| 143 |
| 144 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c
allback, |
| 145 final boolean checkForFiltersLoaded, final long creationTime) |
| 146 { |
| 147 super(AddOnBridge.REQUEST_NAME, |
| 148 checkForFiltersLoaded ? createRequestData("getFiltersLoaded") : value)
; |
| 149 this.value = value; |
| 150 this.apiCallback = callback; |
| 151 this.checkForFiltersLoaded = checkForFiltersLoaded; |
| 152 this.creationTime = creationTime; |
| 153 } |
| 154 |
| 155 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c
allback) |
| 156 { |
| 157 this(value, callback, true, System.currentTimeMillis()); |
| 158 } |
| 159 |
| 160 public ChainedRequest cloneForRetry() |
| 161 { |
| 162 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio
nTime); |
| 163 } |
| 164 |
| 165 public ChainedRequest cloneForRequest() |
| 166 { |
| 167 return new ChainedRequest(this.value, this.apiCallback, false, this.creati
onTime); |
| 168 } |
| 169 |
| 170 private void invokeSuccessCallback(final NativeJSObject jsObject) |
| 171 { |
| 172 try |
| 173 { |
| 174 if (this.apiCallback != null) |
| 175 { |
| 176 this.apiCallback.onApiRequestSucceeded(jsObject); |
| 177 } |
| 178 } |
| 179 catch (final Exception e) |
| 180 { |
| 181 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e
); |
| 182 } |
| 183 } |
| 184 |
| 185 private void invokeFailureCallback(final String msg) |
| 186 { |
| 187 if (this.apiCallback != null) |
| 188 { |
| 189 this.apiCallback.onApiRequestFailed(msg); |
| 190 } |
| 191 } |
| 192 |
| 193 private void invokeFailureCallback(final NativeJSObject jsObject) |
| 194 { |
| 195 invokeFailureCallback(getStringFromJsObject(jsObject, "error", "unknown er
ror")); |
| 196 } |
| 197 |
| 198 private void attemptRetry() |
| 199 { |
| 200 if (System.currentTimeMillis() - this.creationTime > (QUERY_GET_FILTERS_LO
ADED_TIMEOUT * 1000)) |
| 201 { |
| 202 this.invokeFailureCallback("getFiltersLoaded timeout"); |
| 203 } |
| 204 else |
| 205 { |
| 206 final ChainedRequest next = this.cloneForRetry(); |
| 207 HANDLER.postDelayed(new Runnable() |
| 208 { |
| 209 @Override |
| 210 public void run() |
| 211 { |
| 212 GeckoAppShell.sendRequestToGecko(next); |
| 213 } |
| 214 }, QUERY_GET_FILTERS_LOADED_DELAY); |
| 215 } |
| 216 } |
| 217 |
| 218 @Override |
| 219 public void onError() |
| 220 { |
| 221 if (this.checkForFiltersLoaded) |
| 222 { |
| 223 this.attemptRetry(); |
| 224 } |
| 225 else |
| 226 { |
| 227 this.invokeFailureCallback("GeckoRequest error"); |
| 228 } |
| 229 } |
| 230 |
| 231 @Override |
| 232 public void onResponse(final NativeJSObject jsObject) |
| 233 { |
| 234 if (this.checkForFiltersLoaded) |
| 235 { |
| 236 if (getBooleanFromJsObject(jsObject, "success", false) |
| 237 && getBooleanFromJsObject(jsObject, "value", false)) |
| 238 { |
| 239 GeckoAppShell.sendRequestToGecko(this.cloneForRequest()); |
| 240 } |
| 241 else |
| 242 { |
| 243 this.attemptRetry(); |
| 244 } |
| 245 } |
| 246 else |
| 247 { |
| 248 if (getBooleanFromJsObject(jsObject, "success", false)) |
| 249 { |
| 250 this.invokeSuccessCallback(jsObject); |
| 251 } |
| 252 else |
| 253 { |
| 254 this.invokeFailureCallback(jsObject); |
| 255 } |
| 256 } |
| 257 } |
| 258 } |
| 259 } |
OLD | NEW |