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: Added missing slashes Created March 21, 2015, 1:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mobile/android/thirdparty/org/adblockplus/browser/AdblockPlusApiCallback.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.util.Log;
22
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 import org.mozilla.gecko.GeckoAppShell;
26 import org.mozilla.gecko.util.GeckoRequest;
27 import org.mozilla.gecko.util.NativeJSObject;
28
29 @SuppressLint("DefaultLocale")
30 public class AddonBridge
31 {
32 private static final String TAG = "AdblockBrowser.AddonBridge";
33 private static final String REQUEST_NAME = "AdblockPlus:Api";
34
35 public static boolean getBooleanFromJSObject(final NativeJSObject obj, final S tring name,
36 final boolean defaultValue)
37 {
38 try
39 {
40 return obj.getBoolean(name);
41 }
42 catch (final Exception e)
43 {
44 return defaultValue;
45 }
46 }
47
48 private static JSONObject createRequestData(final String action)
49 {
50 final JSONObject obj = new JSONObject();
51 try
52 {
53 obj.put("action", action.toLowerCase());
54 }
55 catch (JSONException e)
56 {
57 // we're only adding sane objects
58 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e);
59 }
60 return obj;
61 }
62
63 private static JSONObject createRequestData(final String action, final boolean enable)
64 {
65 final JSONObject obj = new JSONObject();
66 try
67 {
68 obj.put("action", action.toLowerCase());
69 obj.put("enable", enable);
70 }
71 catch (JSONException e)
72 {
73 // we're only adding sane objects
74 Log.e(TAG, "Creating request data failed with: " + e.getMessage(), e);
75 }
76 return obj;
77 }
78
79 public static void queryBoolean(final AdblockPlusApiCallback callback, final S tring action)
80 {
81 Log.d(TAG, "queryBoolean for " + action);
82 GeckoAppShell.sendRequestToGecko(
83 new ChainedRequest(
84 createRequestData(action),
85 callback,
86 true));
87 }
88
89 public static void setBoolean(final AdblockPlusApiCallback callback, final Str ing action,
90 final boolean enable)
91 {
92 Log.d(TAG, "setBoolean " + enable + " for " + action);
93 GeckoAppShell.sendRequestToGecko(
94 new ChainedRequest(
95 createRequestData(action, enable),
96 callback,
97 true));
98 }
99
100 private static class ChainedRequest extends GeckoRequest
101 {
102 private final JSONObject value;
103 private final AdblockPlusApiCallback apiCallback;
104 private final boolean initCheck;
105
106 public ChainedRequest(final JSONObject value, final AdblockPlusApiCallback c allback,
107 final boolean checkInitState)
108 {
109 super(AddonBridge.REQUEST_NAME,
110 checkInitState ? createRequestData("query_ready_state") : value);
111 this.value = value;
112 this.apiCallback = callback;
113 this.initCheck = checkInitState;
114 }
115
116 private void callSuccessFunction(final NativeJSObject jsObject)
117 {
118 try
119 {
120 if(this.apiCallback != null)
121 {
122 this.apiCallback.onApiRequestSucceeded(jsObject);
123 }
124 }
125 // Yes, catch all, yes, thank you for this totally useless NativeJSObject!
126 catch (final Exception e)
127 {
128 callFailureFunction("unknown error");
129 }
130 }
131
132 private void callFailureFunction(final String msg)
133 {
134 if (this.apiCallback != null)
135 {
136 this.apiCallback.onApiRequestFailed(msg);
137 }
138 }
139
140 private void callFailureFunction(final NativeJSObject jsObject)
141 {
142 try
143 {
144 callFailureFunction(jsObject.getString("error"));
145 }
146 // Yes, catch all, yes, thank you for this totally useless NativeJSObject!
147 catch (final Exception e)
148 {
149 callFailureFunction("unknown error");
150 }
151 }
152
153 @Override
154 public void onError()
155 {
156 if (this.initCheck)
157 {
158 GeckoAppShell.sendRequestToGecko(
159 new ChainedRequest(this.value, this.apiCallback, true));
160 }
161 else
162 {
163 this.callFailureFunction("GeckoRequest error");
164 }
165 }
166
167 @Override
168 public void onResponse(final NativeJSObject jsObject)
169 {
170 if (this.initCheck)
171 {
172 try
173 {
174 if (jsObject.getBoolean("success") && jsObject.getBoolean("value"))
175 {
176 GeckoAppShell.sendRequestToGecko(
177 new ChainedRequest(this.value, this.apiCallback, false));
178 }
179 else
180 {
181 GeckoAppShell.sendRequestToGecko(
182 new ChainedRequest(this.value, this.apiCallback, true));
183 }
184 }
185 catch (Exception e)
186 {
187 GeckoAppShell.sendRequestToGecko(
188 new ChainedRequest(this.value, this.apiCallback, true));
189 }
190 }
191 else
192 {
193 try
194 {
195 if (jsObject.getBoolean("success"))
196 {
197 this.callSuccessFunction(jsObject);
198 }
199 else
200 {
201 this.callFailureFunction(jsObject);
202 }
203 }
204 // Yes, catch all, yes, thank you for this totally useless
205 // NativeJSObject!
206 catch (final Exception e)
207 {
208 callFailureFunction("unknown error");
209 }
210 }
211 }
212 }
213 }
OLDNEW
« no previous file with comments | « mobile/android/thirdparty/org/adblockplus/browser/AdblockPlusApiCallback.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld