Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 /** Represents a response from the directory server. **/ | |
| 46 private static class Response { | |
| 47 public final Error error; | |
| 48 public final String body; | |
| 49 public Response(Error error, String body) { | |
| 50 this.error = error; | |
| 51 this.body = body; | |
| 52 } | |
| 53 } | |
| 54 | |
| 42 private static final String TAG = "Chromoting"; | 55 private static final String TAG = "Chromoting"; |
| 43 | 56 |
| 44 /** Path from which to download a user's host list JSON object. */ | 57 /** Path from which to download a user's host list JSON object. */ |
| 45 private static final String HOST_LIST_PATH = | 58 private static final String HOST_LIST_PATH = |
| 46 "https://www.googleapis.com/chromoting/v1/@me/hosts"; | 59 "https://www.googleapis.com/chromoting/v1/@me/hosts"; |
| 47 | 60 |
| 48 /** Callback handler to be used for network operations. */ | 61 /** Callback handler to be used for network operations. */ |
| 49 private Handler mNetworkThread; | 62 private Handler mNetworkThread; |
| 50 | 63 |
| 51 /** Handler for main thread. */ | 64 /** Handler for main thread. */ |
| 52 private Handler mMainThread; | 65 private Handler mMainThread; |
| 53 | 66 |
| 54 public HostListLoader() { | 67 public HostListManager() { |
| 55 // Thread responsible for downloading the host list. | 68 // Thread responsible for downloading the host list. |
| 56 | 69 |
| 57 mMainThread = new Handler(Looper.getMainLooper()); | 70 mMainThread = new Handler(Looper.getMainLooper()); |
| 58 } | 71 } |
| 59 | 72 |
| 60 private void initNetworkThread() { | 73 private void runOnNetworkThread(Runnable runnable) { |
| 61 if (mNetworkThread == null) { | 74 if (mNetworkThread == null) { |
| 62 HandlerThread thread = new HandlerThread("network"); | 75 HandlerThread thread = new HandlerThread("network"); |
| 63 thread.start(); | 76 thread.start(); |
| 64 mNetworkThread = new Handler(thread.getLooper()); | 77 mNetworkThread = new Handler(thread.getLooper()); |
| 65 } | 78 } |
| 79 mNetworkThread.post(runnable); | |
| 66 } | 80 } |
| 67 | 81 |
| 68 /** | 82 /** |
| 69 * Causes the host list to be fetched on a background thread. This should b e called on the | 83 * 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, | 84 * 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 | 85 * callback.onHostListReceived() will be called, otherwise callback.onError () will be called |
| 72 * with an error-code describing the failure. | 86 * with an error-code describing the failure. |
| 73 */ | 87 */ |
| 74 public void retrieveHostList(String authToken, Callback callback) { | 88 public void retrieveHostList(final String authToken, final Callback callback ) { |
| 75 initNetworkThread(); | 89 runOnNetworkThread(new Runnable() { |
| 76 final String authTokenFinal = authToken; | |
| 77 final Callback callbackFinal = callback; | |
| 78 mNetworkThread.post(new Runnable() { | |
| 79 @Override | 90 @Override |
| 80 public void run() { | 91 public void run() { |
| 81 doRetrieveHostList(authTokenFinal, callbackFinal); | 92 doRetrieveHostList(authToken, callback); |
| 82 } | 93 } |
| 83 }); | 94 }); |
| 84 } | 95 } |
| 85 | 96 |
| 86 private void doRetrieveHostList(String authToken, Callback callback) { | 97 private void doRetrieveHostList(String authToken, Callback callback) { |
| 87 HttpURLConnection link = null; | 98 Response response = sendRequest(authToken, HOST_LIST_PATH, "GET", null, null); |
| 88 String response = null; | 99 if (response.error != null || response.body == null) { |
| 89 try { | 100 postError(callback, response.error); |
|
Lambros
2016/05/03 22:11:03
If response.error is null, it feels wrong to pass
Yuwei
2016/05/03 22:39:02
Good catch:) I will let the JSON parser fail in th
Yuwei
2016/05/03 22:47:57
Wait... Is it possible that a request succeeds wit
| |
| 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; | 101 return; |
| 123 } finally { | |
| 124 if (link != null) { | |
| 125 link.disconnect(); | |
| 126 } | |
| 127 } | 102 } |
| 128 | 103 |
| 129 // Parse directory response. | 104 // Parse directory response. |
| 130 ArrayList<HostInfo> hostList = new ArrayList<HostInfo>(); | 105 ArrayList<HostInfo> hostList = new ArrayList<HostInfo>(); |
| 131 try { | 106 try { |
| 132 JSONObject data = new JSONObject(response).getJSONObject("data"); | 107 JSONObject data = new JSONObject(response.body).getJSONObject("data" ); |
| 133 if (data.has("items")) { | 108 if (data.has("items")) { |
| 134 JSONArray hostsJson = data.getJSONArray("items"); | 109 JSONArray hostsJson = data.getJSONArray("items"); |
| 135 | 110 |
| 136 int index = 0; | 111 int index = 0; |
| 137 while (!hostsJson.isNull(index)) { | 112 while (!hostsJson.isNull(index)) { |
| 138 JSONObject hostJson = hostsJson.getJSONObject(index); | 113 JSONObject hostJson = hostsJson.getJSONObject(index); |
| 139 // If a host is only recently registered, it may be missing some of the keys | 114 // 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 | 115 // 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 | 116 // 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 | 117 // trigger reloading of the host-list, by which time the key s will hopefully be |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 158 final Callback callbackFinal = callback; | 133 final Callback callbackFinal = callback; |
| 159 final HostInfo[] hosts = hostList.toArray(new HostInfo[hostList.size()]) ; | 134 final HostInfo[] hosts = hostList.toArray(new HostInfo[hostList.size()]) ; |
| 160 mMainThread.post(new Runnable() { | 135 mMainThread.post(new Runnable() { |
| 161 @Override | 136 @Override |
| 162 public void run() { | 137 public void run() { |
| 163 callbackFinal.onHostListReceived(hosts); | 138 callbackFinal.onHostListReceived(hosts); |
| 164 } | 139 } |
| 165 }); | 140 }); |
| 166 } | 141 } |
| 167 | 142 |
| 143 /** | |
| 144 * Updates a host on the background thread. On success, callback.onHostUpdat ed() will be called, | |
| 145 * otherwise callback.onError() will be called with an error-code describing the failure. | |
| 146 */ | |
| 147 public void putHost(final String authToken, final String hostId, final Strin g hostName, | |
| 148 final String publicKey, final Callback callback) { | |
| 149 runOnNetworkThread(new Runnable() { | |
| 150 @Override | |
| 151 public void run() { | |
| 152 doPutHost(authToken, hostId, hostName, publicKey, callback); | |
| 153 } | |
| 154 }); | |
| 155 } | |
| 156 | |
| 157 private void doPutHost(String authToken, String hostId, String hostName, Str ing publicKey, | |
| 158 final Callback callback) { | |
| 159 String requestJson; | |
| 160 try { | |
| 161 JSONObject data = new JSONObject(); | |
| 162 data.put("hostId", hostId); | |
| 163 data.put("hostName", hostName); | |
| 164 data.put("publicKey", publicKey); | |
| 165 JSONObject request = new JSONObject(); | |
| 166 request.put("data", data); | |
| 167 requestJson = request.toString(); | |
| 168 } catch (JSONException ex) { | |
| 169 Log.e(TAG, "Error creating put host JSON string: %s", ex.getMessage( )); | |
| 170 postError(callback, Error.UNKNOWN); | |
| 171 return; | |
| 172 } | |
| 173 Response response = sendRequest(authToken, HOST_LIST_PATH + '/' + hostId , "PUT", | |
| 174 "application/json", requestJson); | |
| 175 if (response.error != null) { | |
| 176 postError(callback, response.error); | |
| 177 } else { | |
| 178 mMainThread.post(new Runnable() { | |
| 179 @Override | |
| 180 public void run() { | |
| 181 callback.onHostUpdated(); | |
| 182 } | |
| 183 }); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 /** | |
| 188 * Deletes a host on the background thread. On success, callback.onHostUpdat ed() will be called, | |
| 189 * otherwise callback.onError() will be called with an error-code describing the failure. | |
| 190 */ | |
| 191 public void deleteHost(final String authToken, final String hostId, | |
| 192 final Callback callback) { | |
| 193 runOnNetworkThread(new Runnable() { | |
| 194 @Override | |
| 195 public void run() { | |
| 196 doDeleteHost(authToken, hostId, callback); | |
| 197 } | |
| 198 }); | |
| 199 } | |
| 200 | |
| 201 private void doDeleteHost(String authToken, String hostId, final Callback ca llback) { | |
| 202 Response response = sendRequest(authToken, HOST_LIST_PATH + '/' + hostId , "DELETE", | |
| 203 null, null); | |
| 204 if (response.error != null) { | |
| 205 postError(callback, response.error); | |
| 206 } else { | |
| 207 mMainThread.post(new Runnable() { | |
| 208 @Override | |
| 209 public void run() { | |
| 210 callback.onHostDeleted(); | |
| 211 } | |
| 212 }); | |
| 213 } | |
| 214 } | |
| 215 | |
| 168 /** Posts error to callback on main thread. */ | 216 /** Posts error to callback on main thread. */ |
| 169 private void postError(Callback callback, Error error) { | 217 private void postError(Callback callback, Error error) { |
| 170 final Callback callbackFinal = callback; | 218 final Callback callbackFinal = callback; |
| 171 final Error errorFinal = error; | 219 final Error errorFinal = error; |
| 172 mMainThread.post(new Runnable() { | 220 mMainThread.post(new Runnable() { |
| 173 @Override | 221 @Override |
| 174 public void run() { | 222 public void run() { |
| 175 callbackFinal.onError(errorFinal); | 223 callbackFinal.onError(errorFinal); |
| 176 } | 224 } |
| 177 }); | 225 }); |
| 178 } | 226 } |
| 179 | 227 |
| 180 private static void sortHosts(ArrayList<HostInfo> hosts) { | 228 private static void sortHosts(ArrayList<HostInfo> hosts) { |
| 181 Comparator<HostInfo> hostComparator = new Comparator<HostInfo>() { | 229 Comparator<HostInfo> hostComparator = new Comparator<HostInfo>() { |
| 182 @Override | 230 @Override |
| 183 public int compare(HostInfo a, HostInfo b) { | 231 public int compare(HostInfo a, HostInfo b) { |
| 184 if (a.isOnline != b.isOnline) { | 232 if (a.isOnline != b.isOnline) { |
| 185 return a.isOnline ? -1 : 1; | 233 return a.isOnline ? -1 : 1; |
| 186 } | 234 } |
| 187 String aName = a.name.toUpperCase(Locale.getDefault()); | 235 String aName = a.name.toUpperCase(Locale.getDefault()); |
| 188 String bName = b.name.toUpperCase(Locale.getDefault()); | 236 String bName = b.name.toUpperCase(Locale.getDefault()); |
| 189 return aName.compareTo(bName); | 237 return aName.compareTo(bName); |
| 190 } | 238 } |
| 191 }; | 239 }; |
| 192 Collections.sort(hosts, hostComparator); | 240 Collections.sort(hosts, hostComparator); |
| 193 } | 241 } |
| 242 | |
| 243 /** | |
| 244 * Sends request to the url and returns the response. | |
| 245 * @param authToken auth token | |
| 246 * @param url the URL to send the request | |
| 247 * @param method /GET/POST/PUT/DELETE/etc. | |
| 248 * @param requestContentType The content type of the request body. This can be null. | |
| 249 * @param requestBody This can be null. | |
| 250 * @return The response. | |
| 251 */ | |
| 252 private static Response sendRequest(String authToken, String url, String met hod, | |
| 253 String requestContentType, String reques tBody) { | |
| 254 HttpURLConnection link = null; | |
| 255 Error error = null; | |
| 256 String response = null; | |
| 257 try { | |
| 258 link = (HttpURLConnection) new URL(url).openConnection(); | |
| 259 link.setRequestMethod(method); | |
| 260 link.setRequestProperty("Authorization", "OAuth " + authToken); | |
| 261 if (requestContentType != null) { | |
| 262 link.setRequestProperty("Content-Type", requestContentType); | |
| 263 } | |
| 264 if (requestBody != null) { | |
| 265 byte[] requestBytes = requestBody.getBytes("UTF-8"); | |
| 266 OutputStream outStream = link.getOutputStream(); | |
| 267 outStream.write(requestBytes); | |
| 268 outStream.close(); | |
| 269 } | |
| 270 | |
| 271 // Listen for the server to respond. | |
| 272 int status = link.getResponseCode(); | |
| 273 switch (status) { | |
| 274 case HttpURLConnection.HTTP_OK: // 200 | |
| 275 break; | |
| 276 case HttpURLConnection.HTTP_UNAUTHORIZED: // 401 | |
| 277 error = Error.AUTH_FAILED; | |
| 278 break; | |
| 279 case HttpURLConnection.HTTP_BAD_GATEWAY: // 502 | |
| 280 case HttpURLConnection.HTTP_UNAVAILABLE: // 503 | |
| 281 error = Error.SERVICE_UNAVAILABLE; | |
| 282 break; | |
| 283 default: | |
| 284 error = Error.UNKNOWN; | |
| 285 } | |
| 286 | |
| 287 StringBuilder responseBuilder = new StringBuilder(); | |
| 288 Scanner incoming = new Scanner(link.getInputStream()); | |
| 289 while (incoming.hasNext()) { | |
| 290 responseBuilder.append(incoming.nextLine()); | |
| 291 } | |
| 292 response = String.valueOf(responseBuilder); | |
| 293 incoming.close(); | |
| 294 } catch (MalformedURLException ex) { | |
| 295 // This should never happen. | |
| 296 throw new RuntimeException("Unexpected error while fetching host lis t: ", ex); | |
| 297 } catch (IOException ex) { | |
| 298 error = Error.NETWORK_ERROR; | |
| 299 } finally { | |
| 300 if (link != null) { | |
| 301 link.disconnect(); | |
| 302 } | |
| 303 } | |
| 304 return new Response(error, response); | |
| 305 } | |
| 194 } | 306 } |
| OLD | NEW |