Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: remoting/android/java/src/org/chromium/chromoting/HostListManager.java

Issue 1948793002: Implement HostListManager by Refactoring HostListLoader (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reviewer's feedback Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chromoting; 5 package org.chromium.chromoting;
6 6
7 import android.os.Handler; 7 import android.os.Handler;
8 import android.os.HandlerThread; 8 import android.os.HandlerThread;
9 import android.os.Looper; 9 import android.os.Looper;
10 10
11 import org.chromium.base.Log; 11 import org.chromium.base.Log;
12 import org.json.JSONArray; 12 import org.json.JSONArray;
13 import org.json.JSONException; 13 import org.json.JSONException;
14 import org.json.JSONObject; 14 import org.json.JSONObject;
15 15
16 import java.io.IOException; 16 import java.io.IOException;
17 import java.io.OutputStream;
17 import java.net.HttpURLConnection; 18 import java.net.HttpURLConnection;
18 import java.net.MalformedURLException; 19 import java.net.MalformedURLException;
19 import java.net.URL; 20 import java.net.URL;
20 import java.util.ArrayList; 21 import java.util.ArrayList;
21 import java.util.Collections; 22 import java.util.Collections;
22 import java.util.Comparator; 23 import java.util.Comparator;
23 import java.util.Locale; 24 import java.util.Locale;
24 import java.util.Scanner; 25 import java.util.Scanner;
25 26
26 /** Helper for fetching the host list. */ 27 /** Helper for fetching and modifying the host list. */
27 public class HostListLoader { 28 public class HostListManager {
28 public enum Error { 29 public enum Error {
29 AUTH_FAILED, 30 AUTH_FAILED,
30 NETWORK_ERROR, 31 NETWORK_ERROR,
31 SERVICE_UNAVAILABLE, 32 SERVICE_UNAVAILABLE,
32 UNEXPECTED_RESPONSE, 33 UNEXPECTED_RESPONSE,
33 UNKNOWN, 34 UNKNOWN,
34 } 35 }
35 36
36 /** Callback for receiving the host list, or getting notified of an error. * / 37 /** Callback for receiving the host list, or getting notified of an error. * /
37 public interface Callback { 38 public interface Callback {
38 void onHostListReceived(HostInfo[] hosts); 39 void onHostListReceived(HostInfo[] response);
40 void onHostUpdated();
41 void onHostDeleted();
39 void onError(Error error); 42 void onError(Error error);
40 } 43 }
41 44
45 /**
46 * Represents a response from the directory server.
47 * If the request failed, |error| will not be null and |body| will be null.
48 * If the request succeeds, |error| will be null and |body| will not be null .
49 **/
50 private static class Response {
51 public final Error error;
52 public final String body;
53 public Response(Error error, String body) {
54 this.error = error;
55 this.body = body;
56 }
57 }
58
42 private static final String TAG = "Chromoting"; 59 private static final String TAG = "Chromoting";
43 60
44 /** Path from which to download a user's host list JSON object. */ 61 /** Path from which to download a user's host list JSON object. */
45 private static final String HOST_LIST_PATH = 62 private static final String HOST_LIST_PATH =
46 "https://www.googleapis.com/chromoting/v1/@me/hosts"; 63 "https://www.googleapis.com/chromoting/v1/@me/hosts";
47 64
48 /** Callback handler to be used for network operations. */ 65 /** Callback handler to be used for network operations. */
49 private Handler mNetworkThread; 66 private Handler mNetworkThread;
50 67
51 /** Handler for main thread. */ 68 /** Handler for main thread. */
52 private Handler mMainThread; 69 private Handler mMainThread;
53 70
54 public HostListLoader() { 71 public HostListManager() {
55 // Thread responsible for downloading the host list. 72 // Thread responsible for downloading the host list.
56 73
57 mMainThread = new Handler(Looper.getMainLooper()); 74 mMainThread = new Handler(Looper.getMainLooper());
58 } 75 }
59 76
60 private void initNetworkThread() { 77 private void runOnNetworkThread(Runnable runnable) {
61 if (mNetworkThread == null) { 78 if (mNetworkThread == null) {
62 HandlerThread thread = new HandlerThread("network"); 79 HandlerThread thread = new HandlerThread("network");
63 thread.start(); 80 thread.start();
64 mNetworkThread = new Handler(thread.getLooper()); 81 mNetworkThread = new Handler(thread.getLooper());
65 } 82 }
83 mNetworkThread.post(runnable);
66 } 84 }
67 85
68 /** 86 /**
69 * Causes the host list to be fetched on a background thread. This should b e called on the 87 * Causes the host list to be fetched on a background thread. This should b e called on the
70 * main thread, and callbacks will also be invoked on the main thread. On s uccess, 88 * main thread, and callbacks will also be invoked on the main thread. On s uccess,
71 * callback.onHostListReceived() will be called, otherwise callback.onError () will be called 89 * callback.onHostListReceived() will be called, otherwise callback.onError () will be called
72 * with an error-code describing the failure. 90 * with an error-code describing the failure.
73 */ 91 */
74 public void retrieveHostList(String authToken, Callback callback) { 92 public void retrieveHostList(final String authToken, final Callback callback ) {
75 initNetworkThread(); 93 runOnNetworkThread(new Runnable() {
76 final String authTokenFinal = authToken;
77 final Callback callbackFinal = callback;
78 mNetworkThread.post(new Runnable() {
79 @Override 94 @Override
80 public void run() { 95 public void run() {
81 doRetrieveHostList(authTokenFinal, callbackFinal); 96 doRetrieveHostList(authToken, callback);
82 } 97 }
83 }); 98 });
84 } 99 }
85 100
86 private void doRetrieveHostList(String authToken, Callback callback) { 101 private void doRetrieveHostList(String authToken, Callback callback) {
87 HttpURLConnection link = null; 102 Response response = sendRequest(authToken, HOST_LIST_PATH, "GET", null, null);
88 String response = null; 103 if (response.error != null) {
89 try { 104 postError(callback, response.error);
90 link = (HttpURLConnection) new URL(HOST_LIST_PATH).openConnection();
91 link.setRequestProperty("Authorization", "OAuth " + authToken);
92
93 // Listen for the server to respond.
94 int status = link.getResponseCode();
95 switch (status) {
96 case HttpURLConnection.HTTP_OK: // 200
97 break;
98 case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
99 postError(callback, Error.AUTH_FAILED);
100 return;
101 case HttpURLConnection.HTTP_BAD_GATEWAY: // 502
102 case HttpURLConnection.HTTP_UNAVAILABLE: // 503
103 postError(callback, Error.SERVICE_UNAVAILABLE);
104 return;
105 default:
106 postError(callback, Error.UNKNOWN);
107 return;
108 }
109
110 StringBuilder responseBuilder = new StringBuilder();
111 Scanner incoming = new Scanner(link.getInputStream());
112 while (incoming.hasNext()) {
113 responseBuilder.append(incoming.nextLine());
114 }
115 response = String.valueOf(responseBuilder);
116 incoming.close();
117 } catch (MalformedURLException ex) {
118 // This should never happen.
119 throw new RuntimeException("Unexpected error while fetching host lis t: ", ex);
120 } catch (IOException ex) {
121 postError(callback, Error.NETWORK_ERROR);
122 return; 105 return;
123 } finally {
124 if (link != null) {
125 link.disconnect();
126 }
127 } 106 }
128 107
129 // Parse directory response. 108 // Parse directory response.
130 ArrayList<HostInfo> hostList = new ArrayList<HostInfo>(); 109 ArrayList<HostInfo> hostList = new ArrayList<HostInfo>();
131 try { 110 try {
132 JSONObject data = new JSONObject(response).getJSONObject("data"); 111 JSONObject data = new JSONObject(response.body).getJSONObject("data" );
133 if (data.has("items")) { 112 if (data.has("items")) {
134 JSONArray hostsJson = data.getJSONArray("items"); 113 JSONArray hostsJson = data.getJSONArray("items");
135 114
136 int index = 0; 115 int index = 0;
137 while (!hostsJson.isNull(index)) { 116 while (!hostsJson.isNull(index)) {
138 JSONObject hostJson = hostsJson.getJSONObject(index); 117 JSONObject hostJson = hostsJson.getJSONObject(index);
139 // If a host is only recently registered, it may be missing some of the keys 118 // If a host is only recently registered, it may be missing some of the keys
140 // below. It should still be visible in the list, even thoug h a connection 119 // below. It should still be visible in the list, even thoug h a connection
141 // attempt will fail because of the missing keys. The failed attempt will 120 // attempt will fail because of the missing keys. The failed attempt will
142 // trigger reloading of the host-list, by which time the key s will hopefully be 121 // trigger reloading of the host-list, by which time the key s will hopefully be
(...skipping 15 matching lines...) Expand all
158 final Callback callbackFinal = callback; 137 final Callback callbackFinal = callback;
159 final HostInfo[] hosts = hostList.toArray(new HostInfo[hostList.size()]) ; 138 final HostInfo[] hosts = hostList.toArray(new HostInfo[hostList.size()]) ;
160 mMainThread.post(new Runnable() { 139 mMainThread.post(new Runnable() {
161 @Override 140 @Override
162 public void run() { 141 public void run() {
163 callbackFinal.onHostListReceived(hosts); 142 callbackFinal.onHostListReceived(hosts);
164 } 143 }
165 }); 144 });
166 } 145 }
167 146
147 /**
148 * Updates a host on the background thread. On success, callback.onHostUpdat ed() will be called,
149 * otherwise callback.onError() will be called with an error-code describing the failure.
150 */
151 public void putHost(final String authToken, final String hostId, final Strin g hostName,
152 final String publicKey, final Callback callback) {
153 runOnNetworkThread(new Runnable() {
154 @Override
155 public void run() {
156 doPutHost(authToken, hostId, hostName, publicKey, callback);
157 }
158 });
159 }
160
161 private void doPutHost(String authToken, String hostId, String hostName, Str ing publicKey,
162 final Callback callback) {
163 String requestJson;
164 try {
165 JSONObject data = new JSONObject();
166 data.put("hostId", hostId);
167 data.put("hostName", hostName);
168 data.put("publicKey", publicKey);
169 JSONObject request = new JSONObject();
170 request.put("data", data);
171 requestJson = request.toString();
172 } catch (JSONException ex) {
173 Log.e(TAG, "Error creating put host JSON string: %s", ex.getMessage( ));
174 postError(callback, Error.UNKNOWN);
175 return;
176 }
177 Response response = sendRequest(authToken, HOST_LIST_PATH + '/' + hostId , "PUT",
178 "application/json", requestJson);
179 if (response.error != null) {
180 postError(callback, response.error);
181 } else {
182 mMainThread.post(new Runnable() {
183 @Override
184 public void run() {
185 callback.onHostUpdated();
186 }
187 });
188 }
189 }
190
191 /**
192 * Deletes a host on the background thread. On success, callback.onHostUpdat ed() will be called,
193 * otherwise callback.onError() will be called with an error-code describing the failure.
194 */
195 public void deleteHost(final String authToken, final String hostId,
196 final Callback callback) {
197 runOnNetworkThread(new Runnable() {
198 @Override
199 public void run() {
200 doDeleteHost(authToken, hostId, callback);
201 }
202 });
203 }
204
205 private void doDeleteHost(String authToken, String hostId, final Callback ca llback) {
206 Response response = sendRequest(authToken, HOST_LIST_PATH + '/' + hostId , "DELETE",
207 null, null);
208 if (response.error != null) {
209 postError(callback, response.error);
210 } else {
211 mMainThread.post(new Runnable() {
212 @Override
213 public void run() {
214 callback.onHostDeleted();
215 }
216 });
217 }
218 }
219
168 /** Posts error to callback on main thread. */ 220 /** Posts error to callback on main thread. */
169 private void postError(Callback callback, Error error) { 221 private void postError(Callback callback, Error error) {
170 final Callback callbackFinal = callback; 222 final Callback callbackFinal = callback;
171 final Error errorFinal = error; 223 final Error errorFinal = error;
172 mMainThread.post(new Runnable() { 224 mMainThread.post(new Runnable() {
173 @Override 225 @Override
174 public void run() { 226 public void run() {
175 callbackFinal.onError(errorFinal); 227 callbackFinal.onError(errorFinal);
176 } 228 }
177 }); 229 });
178 } 230 }
179 231
180 private static void sortHosts(ArrayList<HostInfo> hosts) { 232 private static void sortHosts(ArrayList<HostInfo> hosts) {
181 Comparator<HostInfo> hostComparator = new Comparator<HostInfo>() { 233 Comparator<HostInfo> hostComparator = new Comparator<HostInfo>() {
182 @Override 234 @Override
183 public int compare(HostInfo a, HostInfo b) { 235 public int compare(HostInfo a, HostInfo b) {
184 if (a.isOnline != b.isOnline) { 236 if (a.isOnline != b.isOnline) {
185 return a.isOnline ? -1 : 1; 237 return a.isOnline ? -1 : 1;
186 } 238 }
187 String aName = a.name.toUpperCase(Locale.getDefault()); 239 String aName = a.name.toUpperCase(Locale.getDefault());
188 String bName = b.name.toUpperCase(Locale.getDefault()); 240 String bName = b.name.toUpperCase(Locale.getDefault());
189 return aName.compareTo(bName); 241 return aName.compareTo(bName);
190 } 242 }
191 }; 243 };
192 Collections.sort(hosts, hostComparator); 244 Collections.sort(hosts, hostComparator);
193 } 245 }
246
247 /**
248 * Sends request to the url and returns the response.
249 * @param authToken auth token
250 * @param url the URL to send the request
251 * @param method /GET/POST/PUT/DELETE/etc.
252 * @param requestContentType The content type of the request body. This can be null.
253 * @param requestBody This can be null.
254 * @return The response.
255 */
256 private static Response sendRequest(String authToken, String url, String met hod,
257 String requestContentType, String reques tBody) {
258 HttpURLConnection link = null;
259 Error error = null;
260 try {
261 link = (HttpURLConnection) new URL(url).openConnection();
262 link.setRequestMethod(method);
263 link.setRequestProperty("Authorization", "OAuth " + authToken);
264 if (requestContentType != null) {
265 link.setRequestProperty("Content-Type", requestContentType);
266 }
267 if (requestBody != null) {
268 byte[] requestBytes = requestBody.getBytes("UTF-8");
269 OutputStream outStream = link.getOutputStream();
270 outStream.write(requestBytes);
271 outStream.close();
272 }
273
274 // Listen for the server to respond.
275 int status = link.getResponseCode();
276 switch (status) {
277 case HttpURLConnection.HTTP_OK: // 200
278 break;
279 case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
280 error = Error.AUTH_FAILED;
281 break;
282 case HttpURLConnection.HTTP_BAD_GATEWAY: // 502
283 case HttpURLConnection.HTTP_UNAVAILABLE: // 503
284 error = Error.SERVICE_UNAVAILABLE;
285 break;
286 default:
287 error = Error.UNKNOWN;
288 }
289
290 if (error != null) {
291 return new Response(error, null);
292 }
293
294 StringBuilder responseBuilder = new StringBuilder();
295 Scanner incoming = new Scanner(link.getInputStream());
296 while (incoming.hasNext()) {
297 responseBuilder.append(incoming.nextLine());
298 }
299 incoming.close();
300 return new Response(null, responseBuilder.toString());
301 } catch (MalformedURLException ex) {
302 // This should never happen.
303 throw new RuntimeException("Unexpected error while fetching host lis t: ", ex);
304 } catch (IOException ex) {
305 return new Response(Error.NETWORK_ERROR, null);
306 } finally {
307 if (link != null) {
308 link.disconnect();
309 }
310 }
311 }
194 } 312 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698