Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: mobile/android/thirdparty/org/adblockplus/browser/AddOnBridge.java

Issue 4920541991403520: Create a minimal settings UI (Closed)
Patch Set: Forgot commenting out Created March 22, 2015, 3:40 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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("adblockbrowser-addon-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 void queryBoolean(final AdblockPlusApiCallback callback, final S tring action)
110 {
111 Log.d(TAG, "queryBoolean for " + action);
112 GeckoAppShell.sendRequestToGecko(
113 new ChainedRequest(
114 createRequestData(action),
115 callback));
116 }
117
118 public static void setBoolean(final AdblockPlusApiCallback callback, final Str ing action,
119 final boolean enable)
120 {
121 Log.d(TAG, "setBoolean " + enable + " for " + action);
122 GeckoAppShell.sendRequestToGecko(
123 new ChainedRequest(
124 createRequestData(action, enable),
125 callback));
126 }
127
128 private static class ChainedRequest extends GeckoRequest
129 {
130 private final JSONObject value;
131 private final AdblockPlusApiCallback apiCallback;
132 private final boolean initCheck;
133 private final long creationTime;
134
135 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback,
136 final boolean checkInitState, final long creationTime)
137 {
138 super(AddOnBridge.REQUEST_NAME,
139 checkInitState ? createRequestData("getFiltersLoaded") : value);
140 this.value = value;
141 this.apiCallback = callback;
142 this.initCheck = checkInitState;
143 this.creationTime = creationTime;
144 }
145
146 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback)
147 {
148 this(value, callback, true, System.currentTimeMillis());
149 }
150
151 public ChainedRequest cloneForRetry()
152 {
153 return new ChainedRequest(this.value, this.apiCallback, true, this.creatio nTime);
154 }
155
156 public ChainedRequest cloneForRequest()
157 {
158 return new ChainedRequest(this.value, this.apiCallback, false, this.creati onTime);
159 }
160
161 private void callSuccessFunction(final NativeJSObject jsObject)
162 {
163 try
164 {
165 if (this.apiCallback != null)
166 {
167 this.apiCallback.onApiRequestSucceeded(jsObject);
168 }
169 }
170 catch (final Exception e)
171 {
172 Log.e(TAG, "onApiRequestSucceeded threw exception: " + e.getMessage(), e );
173 }
174 }
175
176 private void callFailureFunction(final String msg)
177 {
178 if (this.apiCallback != null)
179 {
180 this.apiCallback.onApiRequestFailed(msg);
181 }
182 }
183
184 private void callFailureFunction(final NativeJSObject jsObject)
185 {
186 callFailureFunction(getStringFromJSObject(jsObject, "error", "unknown erro r"));
187 }
188
189 private void maybeRetry()
190 {
191 if (System.currentTimeMillis() - this.creationTime > (QUERY_GET_FILTERS_LO ADED_TIMEOUT * 1000))
192 {
193 this.callFailureFunction("getFiltersLoaded timeout");
194 }
195 else
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_GET_FILTERS_LOADED_DELAY);
206 }
207 }
208
209 @Override
210 public void onError()
211 {
212 if (this.initCheck)
213 {
214 this.maybeRetry();
215 }
216 else
217 {
218 this.callFailureFunction("GeckoRequest error");
219 }
220 }
221
222 @Override
223 public void onResponse(final NativeJSObject jsObject)
224 {
225 if (this.initCheck)
226 {
227 if (getBooleanFromJSObject(jsObject, "success", false)
228 && getBooleanFromJSObject(jsObject, "value", false))
229 {
230 GeckoAppShell.sendRequestToGecko(this.cloneForRequest());
231 }
232 else
233 {
234 this.maybeRetry();
235 }
236 }
237 else
238 {
239 if (getBooleanFromJSObject(jsObject, "success", false))
240 {
241 this.callSuccessFunction(jsObject);
242 }
243 else
244 {
245 this.callFailureFunction(jsObject);
246 }
247 }
248 }
249 }
250 }
OLDNEW

Powered by Google App Engine
This is Rietveld