Left: | ||
Right: |
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 ready checking (in seconds) | |
37 private static final int QUERY_READY_STATE_TIMEOUT = 45; | |
38 // How long to wait between retries (in milliseconds) | |
39 private static final int QUERY_READY_STATE_DELAY = 250; | |
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-addon-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); | |
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, | |
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) | |
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.toLowerCase()); | |
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) | |
Felix Dahlke
2015/03/22 15:51:45
s/enable/parameter/?
| |
95 { | |
96 final JSONObject obj = new JSONObject(); | |
97 try | |
98 { | |
99 obj.put("action", action.toLowerCase()); | |
100 obj.put("enable", enable); | |
101 } | |
102 catch (JSONException e) | |
103 { | |
104 // we're only adding sane objects | |
105 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e); | |
106 } | |
107 return obj; | |
108 } | |
109 | |
110 public static void queryBoolean(final AdblockPlusApiCallback callback, final S tring action) | |
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 { | |
112 Log.d(TAG, "queryBoolean for " + action); | |
113 GeckoAppShell.sendRequestToGecko( | |
114 new ChainedRequest( | |
115 createRequestData(action), | |
116 callback)); | |
117 } | |
118 | |
119 public static void setBoolean(final AdblockPlusApiCallback callback, final Str ing action, | |
120 final boolean enable) | |
121 { | |
122 Log.d(TAG, "setBoolean " + enable + " for " + action); | |
123 GeckoAppShell.sendRequestToGecko( | |
124 new ChainedRequest( | |
125 createRequestData(action, enable), | |
126 callback)); | |
127 } | |
128 | |
129 private static class ChainedRequest extends GeckoRequest | |
130 { | |
131 private final JSONObject value; | |
132 private final AdblockPlusApiCallback apiCallback; | |
133 private final boolean initCheck; | |
134 private final long creationTime; | |
135 | |
136 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback, | |
137 final boolean checkInitState, 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 { | |
139 super(AddOnBridge.REQUEST_NAME, | |
140 checkInitState ? createRequestData("query_ready_state") : value); | |
141 this.value = value; | |
142 this.apiCallback = callback; | |
143 this.initCheck = checkInitState; | |
144 this.creationTime = creationTime; | |
145 } | |
146 | |
147 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback) | |
148 { | |
149 this(value, callback, true, System.currentTimeMillis()); | |
150 } | |
151 | |
152 public ChainedRequest cloneForRetry() | |
153 { | |
154 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio nTime); | |
155 } | |
156 | |
157 public ChainedRequest cloneForRequest() | |
158 { | |
159 return new ChainedRequest(this.value, this.apiCallback, false, this.creati onTime); | |
160 } | |
161 | |
162 private void callSuccessFunction(final NativeJSObject jsObject) | |
Felix Dahlke
2015/03/22 15:51:45
"invokeSuccessCallback"?
René Jeschke
2015/03/22 16:44:55
Ok
| |
163 { | |
164 try | |
165 { | |
166 if (this.apiCallback != null) | |
167 { | |
168 this.apiCallback.onApiRequestSucceeded(jsObject); | |
169 } | |
170 } | |
171 catch (final Exception e) | |
172 { | |
173 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e ); | |
174 } | |
175 } | |
176 | |
177 private void callFailureFunction(final String msg) | |
178 { | |
179 if (this.apiCallback != null) | |
180 { | |
181 this.apiCallback.onApiRequestFailed(msg); | |
182 } | |
183 } | |
184 | |
185 private void callFailureFunction(final NativeJSObject jsObject) | |
186 { | |
187 callFailureFunction(getStringFromJSObject(jsObject, "error", "unknown erro r")); | |
188 } | |
189 | |
190 private void maybeReTry() | |
Felix Dahlke
2015/03/22 15:51:45
How about "attemptRetry"?
René Jeschke
2015/03/22 16:44:55
Yup.
| |
191 { | |
192 if (System.currentTimeMillis() - this.creationTime > (QUERY_READY_STATE_TI MEOUT * 1000)) | |
193 { | |
194 this.callFailureFunction("query_ready_state timeout"); | |
195 } | |
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 | |
197 final ChainedRequest next = this.cloneForRetry(); | |
198 HANDLER.postDelayed(new Runnable() | |
199 { | |
200 @Override | |
201 public void run() | |
202 { | |
203 GeckoAppShell.sendRequestToGecko(next); | |
204 } | |
205 }, QUERY_READY_STATE_DELAY); | |
206 } | |
207 | |
208 @Override | |
209 public void onError() | |
210 { | |
211 if (this.initCheck) | |
212 { | |
213 this.maybeReTry(); | |
214 } | |
215 else | |
216 { | |
217 this.callFailureFunction("GeckoRequest error"); | |
218 } | |
219 } | |
220 | |
221 @Override | |
222 public void onResponse(final NativeJSObject jsObject) | |
223 { | |
224 if (this.initCheck) | |
225 { | |
226 if (getBooleanFromJSObject(jsObject, "success", false) | |
227 && getBooleanFromJSObject(jsObject, "value", false)) | |
228 { | |
229 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 } | |
231 else | |
232 { | |
233 this.maybeReTry(); | |
234 } | |
235 } | |
236 else | |
237 { | |
238 if (getBooleanFromJSObject(jsObject, "success", false)) | |
239 { | |
240 this.callSuccessFunction(jsObject); | |
241 } | |
242 else | |
243 { | |
244 this.callFailureFunction(jsObject); | |
245 } | |
246 } | |
247 } | |
248 } | |
249 } | |
OLD | NEW |