| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 import org.json.JSONException; | 25 import org.json.JSONException; |
| 26 import org.json.JSONObject; | 26 import org.json.JSONObject; |
| 27 import org.mozilla.gecko.GeckoAppShell; | 27 import org.mozilla.gecko.GeckoAppShell; |
| 28 import org.mozilla.gecko.util.GeckoRequest; | 28 import org.mozilla.gecko.util.GeckoRequest; |
| 29 import org.mozilla.gecko.util.NativeJSObject; | 29 import org.mozilla.gecko.util.NativeJSObject; |
| 30 | 30 |
| 31 @SuppressLint("DefaultLocale") | 31 @SuppressLint("DefaultLocale") |
| 32 public class AddOnBridge | 32 public class AddOnBridge |
| 33 { | 33 { |
| 34 private static final String TAG = "AdblockBrowser.AddonBridge"; | 34 private static final String TAG = "AdblockBrowser.AddOnBridge"; |
| 35 private static final String REQUEST_NAME = "AdblockPlus:Api"; | 35 private static final String REQUEST_NAME = "AdblockPlus:Api"; |
| 36 // Timeout for ready checking (in seconds) | 36 // Timeout for checking filter loading (in seconds) |
| 37 private static final int QUERY_READY_STATE_TIMEOUT = 45; | 37 private static final int QUERY_GET_FILTERS_LOADED_TIMEOUT = 30; |
| 38 // How long to wait between retries (in milliseconds) | 38 // How long to wait between retries (in milliseconds) |
| 39 private static final int QUERY_READY_STATE_DELAY = 250; | 39 private static final int QUERY_GET_FILTERS_LOADED_DELAY = 500; |
| 40 // Handler+HandlerThread for posting delayed re-tries without interfering with | 40 // Handler+HandlerThread for posting delayed re-tries without interfering with |
| 41 // other threads (e.g. the UI or Gecko thread) | 41 // other threads (e.g. the UI or Gecko thread) |
| 42 private static final HandlerThread HANDLER_THREAD; | 42 private static final HandlerThread HANDLER_THREAD; |
| 43 private static final Handler HANDLER; | 43 private static final Handler HANDLER; |
| 44 | 44 |
| 45 static | 45 static |
| 46 { | 46 { |
| 47 HANDLER_THREAD = new HandlerThread("abp-addon-bridge"); | 47 HANDLER_THREAD = new HandlerThread("abp-bridge"); |
|
Felix Dahlke
2015/03/22 15:51:45
"abp-bridge" would do. Technically, there's no dif
René Jeschke
2015/03/22 16:44:55
Done.
| |
| 48 HANDLER_THREAD.setDaemon(true); | 48 HANDLER_THREAD.setDaemon(true); |
| 49 HANDLER_THREAD.start(); | 49 HANDLER_THREAD.start(); |
| 50 HANDLER = new Handler(HANDLER_THREAD.getLooper()); | 50 HANDLER = new Handler(HANDLER_THREAD.getLooper()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 public static boolean getBooleanFromJSObject(final NativeJSObject obj, final S tring name, | 53 public static boolean getBooleanFromJsObject(final NativeJSObject obj, final S tring name, |
|
Felix Dahlke
2015/03/22 15:51:45
Naming Nit: getBooleanFromJsObject, likewise get*F
René Jeschke
2015/03/22 16:44:55
Done.
| |
| 54 final boolean defaultValue) | 54 final boolean defaultValue) |
| 55 { | 55 { |
| 56 try | 56 try |
| 57 { | 57 { |
| 58 return obj.getBoolean(name); | 58 return obj.getBoolean(name); |
| 59 } | 59 } |
| 60 catch (final Exception e) | 60 catch (final Exception e) |
| 61 { | 61 { |
| 62 return defaultValue; | 62 return defaultValue; |
| 63 } | 63 } |
| 64 } | 64 } |
| 65 | 65 |
| 66 public static String getStringFromJSObject(final NativeJSObject obj, final Str ing name, | 66 public static String getStringFromJsObject(final NativeJSObject obj, final Str ing name, |
| 67 final String defaultValue) | 67 final String defaultValue) |
| 68 { | 68 { |
| 69 try | 69 try |
| 70 { | 70 { |
| 71 return obj.getString(name); | 71 return obj.getString(name); |
| 72 } | 72 } |
| 73 catch (final Exception e) | 73 catch (final Exception e) |
| 74 { | 74 { |
| 75 return defaultValue; | 75 return defaultValue; |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 private static JSONObject createRequestData(final String action) | 79 private static JSONObject createRequestData(final String action) |
| 80 { | 80 { |
| 81 final JSONObject obj = new JSONObject(); | 81 final JSONObject obj = new JSONObject(); |
| 82 try | 82 try |
| 83 { | 83 { |
| 84 obj.put("action", action.toLowerCase()); | 84 obj.put("action", action); |
| 85 } | 85 } |
| 86 catch (JSONException e) | 86 catch (JSONException e) |
| 87 { | 87 { |
| 88 // we're only adding sane objects | 88 // we're only adding sane objects |
| 89 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); | 89 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
| 90 } | 90 } |
| 91 return obj; | 91 return obj; |
| 92 } | 92 } |
| 93 | 93 |
| 94 private static JSONObject createRequestData(final String action, final boolean enable) | 94 private static JSONObject createRequestData(final String action, final boolean enable) |
|
Felix Dahlke
2015/03/22 15:51:45
s/enable/parameter/?
| |
| 95 { | 95 { |
| 96 final JSONObject obj = new JSONObject(); | 96 final JSONObject obj = createRequestData(action); |
| 97 try | 97 try |
| 98 { | 98 { |
| 99 obj.put("action", action.toLowerCase()); | |
| 100 obj.put("enable", enable); | 99 obj.put("enable", enable); |
| 101 } | 100 } |
| 102 catch (JSONException e) | 101 catch (JSONException e) |
| 103 { | 102 { |
| 104 // we're only adding sane objects | 103 // we're only adding sane objects |
| 105 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); | 104 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
| 106 } | 105 } |
| 107 return obj; | 106 return obj; |
| 108 } | 107 } |
| 109 | 108 |
| 110 public static void queryBoolean(final AdblockPlusApiCallback callback, final S tring action) | 109 public static String makeFirstCharacterUppercase(String str) |
|
Felix Dahlke
2015/03/22 15:51:45
IMHO this is a bit confusing: queryBoolean sounds
René Jeschke
2015/03/22 16:44:55
It is called 'query' because it does not 'get' a b
Felix Dahlke
2015/03/22 17:44:34
OK, fair enough.
| |
| 111 { | 110 { |
| 112 Log.d(TAG, "queryBoolean for " + action); | 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); | |
| 113 GeckoAppShell.sendRequestToGecko( | 121 GeckoAppShell.sendRequestToGecko( |
| 114 new ChainedRequest( | 122 new ChainedRequest( |
| 115 createRequestData(action), | 123 createRequestData("get" + makeFirstCharacterUppercase(name)), |
| 116 callback)); | 124 callback)); |
| 117 } | 125 } |
| 118 | 126 |
| 119 public static void setBoolean(final AdblockPlusApiCallback callback, final Str ing action, | 127 public static void setBoolean(final AdblockPlusApiCallback callback, final Str ing name, |
| 120 final boolean enable) | 128 final boolean enable) |
| 121 { | 129 { |
| 122 Log.d(TAG, "setBoolean " + enable + " for " + action); | 130 Log.d(TAG, "setBoolean " + enable + " for " + name); |
| 123 GeckoAppShell.sendRequestToGecko( | 131 GeckoAppShell.sendRequestToGecko( |
| 124 new ChainedRequest( | 132 new ChainedRequest( |
| 125 createRequestData(action, enable), | 133 createRequestData("set" + makeFirstCharacterUppercase(name), enable) , |
| 126 callback)); | 134 callback)); |
| 127 } | 135 } |
| 128 | 136 |
| 129 private static class ChainedRequest extends GeckoRequest | 137 private static class ChainedRequest extends GeckoRequest |
| 130 { | 138 { |
| 131 private final JSONObject value; | 139 private final JSONObject value; |
| 132 private final AdblockPlusApiCallback apiCallback; | 140 private final AdblockPlusApiCallback apiCallback; |
| 133 private final boolean initCheck; | 141 private final boolean checkForFiltersLoaded; |
| 134 private final long creationTime; | 142 private final long creationTime; |
| 135 | 143 |
| 136 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback, | 144 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback, |
| 137 final boolean checkInitState, final long creationTime) | 145 final boolean checkForFiltersLoaded, final long creationTime) |
|
Felix Dahlke
2015/03/22 15:51:45
I don't fully understand this one -
René Jeschke
2015/03/22 16:44:55
We start an asynchronous chain of requests here:
T
Felix Dahlke
2015/03/22 17:44:34
Alright, get it now - so we're checking for filter
| |
| 138 { | 146 { |
| 139 super(AddOnBridge.REQUEST_NAME, | 147 super(AddOnBridge.REQUEST_NAME, |
| 140 checkInitState ? createRequestData("query_ready_state") : value); | 148 checkForFiltersLoaded ? createRequestData("getFiltersLoaded") : value) ; |
| 141 this.value = value; | 149 this.value = value; |
| 142 this.apiCallback = callback; | 150 this.apiCallback = callback; |
| 143 this.initCheck = checkInitState; | 151 this.checkForFiltersLoaded = checkForFiltersLoaded; |
| 144 this.creationTime = creationTime; | 152 this.creationTime = creationTime; |
| 145 } | 153 } |
| 146 | 154 |
| 147 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback) | 155 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback) |
| 148 { | 156 { |
| 149 this(value, callback, true, System.currentTimeMillis()); | 157 this(value, callback, true, System.currentTimeMillis()); |
| 150 } | 158 } |
| 151 | 159 |
| 152 public ChainedRequest cloneForRetry() | 160 public ChainedRequest cloneForRetry() |
| 153 { | 161 { |
| 154 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio nTime); | 162 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio nTime); |
| 155 } | 163 } |
| 156 | 164 |
| 157 public ChainedRequest cloneForRequest() | 165 public ChainedRequest cloneForRequest() |
| 158 { | 166 { |
| 159 return new ChainedRequest(this.value, this.apiCallback, false, this.creati onTime); | 167 return new ChainedRequest(this.value, this.apiCallback, false, this.creati onTime); |
| 160 } | 168 } |
| 161 | 169 |
| 162 private void callSuccessFunction(final NativeJSObject jsObject) | 170 private void invokeSuccessCallback(final NativeJSObject jsObject) |
|
Felix Dahlke
2015/03/22 15:51:45
"invokeSuccessCallback"?
René Jeschke
2015/03/22 16:44:55
Ok
| |
| 163 { | 171 { |
| 164 try | 172 try |
| 165 { | 173 { |
| 166 if (this.apiCallback != null) | 174 if (this.apiCallback != null) |
| 167 { | 175 { |
| 168 this.apiCallback.onApiRequestSucceeded(jsObject); | 176 this.apiCallback.onApiRequestSucceeded(jsObject); |
| 169 } | 177 } |
| 170 } | 178 } |
| 171 catch (final Exception e) | 179 catch (final Exception e) |
| 172 { | 180 { |
| 173 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e ); | 181 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e ); |
| 174 } | 182 } |
| 175 } | 183 } |
| 176 | 184 |
| 177 private void callFailureFunction(final String msg) | 185 private void invokeFailureCallback(final String msg) |
| 178 { | 186 { |
| 179 if (this.apiCallback != null) | 187 if (this.apiCallback != null) |
| 180 { | 188 { |
| 181 this.apiCallback.onApiRequestFailed(msg); | 189 this.apiCallback.onApiRequestFailed(msg); |
| 182 } | 190 } |
| 183 } | 191 } |
| 184 | 192 |
| 185 private void callFailureFunction(final NativeJSObject jsObject) | 193 private void invokeFailureCallback(final NativeJSObject jsObject) |
| 186 { | 194 { |
| 187 callFailureFunction(getStringFromJSObject(jsObject, "error", "unknown erro r")); | 195 invokeFailureCallback(getStringFromJsObject(jsObject, "error", "unknown er ror")); |
| 188 } | 196 } |
| 189 | 197 |
| 190 private void maybeReTry() | 198 private void attemptRetry() |
|
Felix Dahlke
2015/03/22 15:51:45
How about "attemptRetry"?
René Jeschke
2015/03/22 16:44:55
Yup.
| |
| 191 { | 199 { |
| 192 if (System.currentTimeMillis() - this.creationTime > (QUERY_READY_STATE_TI MEOUT * 1000)) | 200 if (System.currentTimeMillis() - this.creationTime > (QUERY_GET_FILTERS_LO ADED_TIMEOUT * 1000)) |
| 193 { | 201 { |
| 194 this.callFailureFunction("query_ready_state timeout"); | 202 this.invokeFailureCallback("getFiltersLoaded timeout"); |
| 195 } | 203 } |
|
Felix Dahlke
2015/03/22 15:51:45
Shouldn't we return here, or am I missing somethin
René Jeschke
2015/03/22 16:44:55
Yeah, already changed.
| |
| 196 | 204 else |
| 197 final ChainedRequest next = this.cloneForRetry(); | 205 { |
| 198 HANDLER.postDelayed(new Runnable() | 206 final ChainedRequest next = this.cloneForRetry(); |
| 199 { | 207 HANDLER.postDelayed(new Runnable() |
| 200 @Override | 208 { |
| 201 public void run() | 209 @Override |
| 202 { | 210 public void run() |
| 203 GeckoAppShell.sendRequestToGecko(next); | 211 { |
| 204 } | 212 GeckoAppShell.sendRequestToGecko(next); |
| 205 }, QUERY_READY_STATE_DELAY); | 213 } |
| 214 }, QUERY_GET_FILTERS_LOADED_DELAY); | |
| 215 } | |
| 206 } | 216 } |
| 207 | 217 |
| 208 @Override | 218 @Override |
| 209 public void onError() | 219 public void onError() |
| 210 { | 220 { |
| 211 if (this.initCheck) | 221 if (this.checkForFiltersLoaded) |
| 212 { | 222 { |
| 213 this.maybeReTry(); | 223 this.attemptRetry(); |
| 214 } | 224 } |
| 215 else | 225 else |
| 216 { | 226 { |
| 217 this.callFailureFunction("GeckoRequest error"); | 227 this.invokeFailureCallback("GeckoRequest error"); |
| 218 } | 228 } |
| 219 } | 229 } |
| 220 | 230 |
| 221 @Override | 231 @Override |
| 222 public void onResponse(final NativeJSObject jsObject) | 232 public void onResponse(final NativeJSObject jsObject) |
| 223 { | 233 { |
| 224 if (this.initCheck) | 234 if (this.checkForFiltersLoaded) |
| 225 { | 235 { |
| 226 if (getBooleanFromJSObject(jsObject, "success", false) | 236 if (getBooleanFromJsObject(jsObject, "success", false) |
| 227 && getBooleanFromJSObject(jsObject, "value", false)) | 237 && getBooleanFromJsObject(jsObject, "value", false)) |
| 228 { | 238 { |
| 229 GeckoAppShell.sendRequestToGecko(this.cloneForRequest()); | 239 GeckoAppShell.sendRequestToGecko(this.cloneForRequest()); |
|
Felix Dahlke
2015/03/22 15:51:45
So.... Not entirely sure I understand what initChe
René Jeschke
2015/03/22 16:44:55
Yes, we are.
onResponse() happens on the main Gec
| |
| 230 } | 240 } |
| 231 else | 241 else |
| 232 { | 242 { |
| 233 this.maybeReTry(); | 243 this.attemptRetry(); |
| 234 } | 244 } |
| 235 } | 245 } |
| 236 else | 246 else |
| 237 { | 247 { |
| 238 if (getBooleanFromJSObject(jsObject, "success", false)) | 248 if (getBooleanFromJsObject(jsObject, "success", false)) |
| 239 { | 249 { |
| 240 this.callSuccessFunction(jsObject); | 250 this.invokeSuccessCallback(jsObject); |
| 241 } | 251 } |
| 242 else | 252 else |
| 243 { | 253 { |
| 244 this.callFailureFunction(jsObject); | 254 this.invokeFailureCallback(jsObject); |
| 245 } | 255 } |
| 246 } | 256 } |
| 247 } | 257 } |
| 248 } | 258 } |
| 249 } | 259 } |
| LEFT | RIGHT |