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 26 matching lines...) Expand all Loading... |
37 private static final int QUERY_GET_FILTERS_LOADED_TIMEOUT = 30; | 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_GET_FILTERS_LOADED_DELAY = 500; | 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("adblockbrowser-addon-bridge"); | 47 HANDLER_THREAD = new HandlerThread("abp-bridge"); |
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, |
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 } |
(...skipping 22 matching lines...) Expand all Loading... |
99 obj.put("enable", enable); | 99 obj.put("enable", enable); |
100 } | 100 } |
101 catch (JSONException e) | 101 catch (JSONException e) |
102 { | 102 { |
103 // we're only adding sane objects | 103 // we're only adding sane objects |
104 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); | 104 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); |
105 } | 105 } |
106 return obj; | 106 return obj; |
107 } | 107 } |
108 | 108 |
109 public static void queryBoolean(final AdblockPlusApiCallback callback, final S
tring action) | 109 public static String makeFirstCharacterUppercase(String str) |
110 { | 110 { |
111 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); |
112 GeckoAppShell.sendRequestToGecko( | 121 GeckoAppShell.sendRequestToGecko( |
113 new ChainedRequest( | 122 new ChainedRequest( |
114 createRequestData(action), | 123 createRequestData("get" + makeFirstCharacterUppercase(name)), |
115 callback)); | 124 callback)); |
116 } | 125 } |
117 | 126 |
118 public static void setBoolean(final AdblockPlusApiCallback callback, final Str
ing action, | 127 public static void setBoolean(final AdblockPlusApiCallback callback, final Str
ing name, |
119 final boolean enable) | 128 final boolean enable) |
120 { | 129 { |
121 Log.d(TAG, "setBoolean " + enable + " for " + action); | 130 Log.d(TAG, "setBoolean " + enable + " for " + name); |
122 GeckoAppShell.sendRequestToGecko( | 131 GeckoAppShell.sendRequestToGecko( |
123 new ChainedRequest( | 132 new ChainedRequest( |
124 createRequestData(action, enable), | 133 createRequestData("set" + makeFirstCharacterUppercase(name), enable)
, |
125 callback)); | 134 callback)); |
126 } | 135 } |
127 | 136 |
128 private static class ChainedRequest extends GeckoRequest | 137 private static class ChainedRequest extends GeckoRequest |
129 { | 138 { |
130 private final JSONObject value; | 139 private final JSONObject value; |
131 private final AdblockPlusApiCallback apiCallback; | 140 private final AdblockPlusApiCallback apiCallback; |
132 private final boolean initCheck; | 141 private final boolean checkForFiltersLoaded; |
133 private final long creationTime; | 142 private final long creationTime; |
134 | 143 |
135 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c
allback, | 144 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c
allback, |
136 final boolean checkInitState, final long creationTime) | 145 final boolean checkForFiltersLoaded, final long creationTime) |
137 { | 146 { |
138 super(AddOnBridge.REQUEST_NAME, | 147 super(AddOnBridge.REQUEST_NAME, |
139 checkInitState ? createRequestData("getFiltersLoaded") : value); | 148 checkForFiltersLoaded ? createRequestData("getFiltersLoaded") : value)
; |
140 this.value = value; | 149 this.value = value; |
141 this.apiCallback = callback; | 150 this.apiCallback = callback; |
142 this.initCheck = checkInitState; | 151 this.checkForFiltersLoaded = checkForFiltersLoaded; |
143 this.creationTime = creationTime; | 152 this.creationTime = creationTime; |
144 } | 153 } |
145 | 154 |
146 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c
allback) | 155 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c
allback) |
147 { | 156 { |
148 this(value, callback, true, System.currentTimeMillis()); | 157 this(value, callback, true, System.currentTimeMillis()); |
149 } | 158 } |
150 | 159 |
151 public ChainedRequest cloneForRetry() | 160 public ChainedRequest cloneForRetry() |
152 { | 161 { |
153 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio
nTime); | 162 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio
nTime); |
154 } | 163 } |
155 | 164 |
156 public ChainedRequest cloneForRequest() | 165 public ChainedRequest cloneForRequest() |
157 { | 166 { |
158 return new ChainedRequest(this.value, this.apiCallback, false, this.creati
onTime); | 167 return new ChainedRequest(this.value, this.apiCallback, false, this.creati
onTime); |
159 } | 168 } |
160 | 169 |
161 private void callSuccessFunction(final NativeJSObject jsObject) | 170 private void invokeSuccessCallback(final NativeJSObject jsObject) |
162 { | 171 { |
163 try | 172 try |
164 { | 173 { |
165 if (this.apiCallback != null) | 174 if (this.apiCallback != null) |
166 { | 175 { |
167 this.apiCallback.onApiRequestSucceeded(jsObject); | 176 this.apiCallback.onApiRequestSucceeded(jsObject); |
168 } | 177 } |
169 } | 178 } |
170 catch (final Exception e) | 179 catch (final Exception e) |
171 { | 180 { |
172 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e
); | 181 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e
); |
173 } | 182 } |
174 } | 183 } |
175 | 184 |
176 private void callFailureFunction(final String msg) | 185 private void invokeFailureCallback(final String msg) |
177 { | 186 { |
178 if (this.apiCallback != null) | 187 if (this.apiCallback != null) |
179 { | 188 { |
180 this.apiCallback.onApiRequestFailed(msg); | 189 this.apiCallback.onApiRequestFailed(msg); |
181 } | 190 } |
182 } | 191 } |
183 | 192 |
184 private void callFailureFunction(final NativeJSObject jsObject) | 193 private void invokeFailureCallback(final NativeJSObject jsObject) |
185 { | 194 { |
186 callFailureFunction(getStringFromJSObject(jsObject, "error", "unknown erro
r")); | 195 invokeFailureCallback(getStringFromJsObject(jsObject, "error", "unknown er
ror")); |
187 } | 196 } |
188 | 197 |
189 private void maybeRetry() | 198 private void attemptRetry() |
190 { | 199 { |
191 if (System.currentTimeMillis() - this.creationTime > (QUERY_GET_FILTERS_LO
ADED_TIMEOUT * 1000)) | 200 if (System.currentTimeMillis() - this.creationTime > (QUERY_GET_FILTERS_LO
ADED_TIMEOUT * 1000)) |
192 { | 201 { |
193 this.callFailureFunction("getFiltersLoaded timeout"); | 202 this.invokeFailureCallback("getFiltersLoaded timeout"); |
194 } | 203 } |
195 else | 204 else |
196 { | 205 { |
197 final ChainedRequest next = this.cloneForRetry(); | 206 final ChainedRequest next = this.cloneForRetry(); |
198 HANDLER.postDelayed(new Runnable() | 207 HANDLER.postDelayed(new Runnable() |
199 { | 208 { |
200 @Override | 209 @Override |
201 public void run() | 210 public void run() |
202 { | 211 { |
203 GeckoAppShell.sendRequestToGecko(next); | 212 GeckoAppShell.sendRequestToGecko(next); |
204 } | 213 } |
205 }, QUERY_GET_FILTERS_LOADED_DELAY); | 214 }, QUERY_GET_FILTERS_LOADED_DELAY); |
206 } | 215 } |
207 } | 216 } |
208 | 217 |
209 @Override | 218 @Override |
210 public void onError() | 219 public void onError() |
211 { | 220 { |
212 if (this.initCheck) | 221 if (this.checkForFiltersLoaded) |
213 { | 222 { |
214 this.maybeRetry(); | 223 this.attemptRetry(); |
215 } | 224 } |
216 else | 225 else |
217 { | 226 { |
218 this.callFailureFunction("GeckoRequest error"); | 227 this.invokeFailureCallback("GeckoRequest error"); |
219 } | 228 } |
220 } | 229 } |
221 | 230 |
222 @Override | 231 @Override |
223 public void onResponse(final NativeJSObject jsObject) | 232 public void onResponse(final NativeJSObject jsObject) |
224 { | 233 { |
225 if (this.initCheck) | 234 if (this.checkForFiltersLoaded) |
226 { | 235 { |
227 if (getBooleanFromJSObject(jsObject, "success", false) | 236 if (getBooleanFromJsObject(jsObject, "success", false) |
228 && getBooleanFromJSObject(jsObject, "value", false)) | 237 && getBooleanFromJsObject(jsObject, "value", false)) |
229 { | 238 { |
230 GeckoAppShell.sendRequestToGecko(this.cloneForRequest()); | 239 GeckoAppShell.sendRequestToGecko(this.cloneForRequest()); |
231 } | 240 } |
232 else | 241 else |
233 { | 242 { |
234 this.maybeRetry(); | 243 this.attemptRetry(); |
235 } | 244 } |
236 } | 245 } |
237 else | 246 else |
238 { | 247 { |
239 if (getBooleanFromJSObject(jsObject, "success", false)) | 248 if (getBooleanFromJsObject(jsObject, "success", false)) |
240 { | 249 { |
241 this.callSuccessFunction(jsObject); | 250 this.invokeSuccessCallback(jsObject); |
242 } | 251 } |
243 else | 252 else |
244 { | 253 { |
245 this.callFailureFunction(jsObject); | 254 this.invokeFailureCallback(jsObject); |
246 } | 255 } |
247 } | 256 } |
248 } | 257 } |
249 } | 258 } |
250 } | 259 } |
LEFT | RIGHT |