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
3 // found in the LICENSE file.
5 package org.chromium.chromoting;
7 import android.os.Handler;
8 import android.os.HandlerThread;
9 import android.os.Looper;
10 import android.util.Log;
12 import org.chromium.chromoting.jni.JniInterface;
13 import org.json.JSONArray;
14 import org.json.JSONException;
15 import org.json.JSONObject;
17 import java.io.IOException;
18 import java.net.HttpURLConnection;
19 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Comparator;
24 import java.util.Scanner;
26 /** Helper for fetching the host list. */
27 public class HostListLoader {
36 /** Callback for receiving the host list, or getting notified of an error. */
37 public interface Callback {
38 void onHostListReceived(HostInfo[] hosts);
39 void onError(Error error);
42 /** Path from which to download a user's host list JSON object. */
43 private static final String HOST_LIST_PATH =
44 "https://www.googleapis.com/chromoting/v1/@me/hosts?key=";
46 /** Callback handler to be used for network operations. */
47 private Handler mNetworkThread;
49 /** Handler for main thread. */
50 private Handler mMainThread;
52 public HostListLoader() {
53 // Thread responsible for downloading the host list.
55 mMainThread = new Handler(Looper.getMainLooper());
58 private void initNetworkThread() {
59 if (mNetworkThread == null) {
60 HandlerThread thread = new HandlerThread("network");
62 mNetworkThread = new Handler(thread.getLooper());
67 * Causes the host list to be fetched on a background thread. This should be called on the
68 * main thread, and callbacks will also be invoked on the main thread. On success,
69 * callback.onHostListReceived() will be called, otherwise callback.onError() will be called
70 * with an error-code describing the failure.
72 public void retrieveHostList(String authToken, Callback callback) {
74 final String authTokenFinal = authToken;
75 final Callback callbackFinal = callback;
76 mNetworkThread.post(new Runnable() {
79 doRetrieveHostList(authTokenFinal, callbackFinal);
84 private void doRetrieveHostList(String authToken, Callback callback) {
85 HttpURLConnection link = null;
86 String response = null;
88 link = (HttpURLConnection)
89 new URL(HOST_LIST_PATH + JniInterface.nativeGetApiKey()).openConnection();
90 link.addRequestProperty("client_id", JniInterface.nativeGetClientId());
91 link.addRequestProperty("client_secret", JniInterface.nativeGetClientSecret());
92 link.setRequestProperty("Authorization", "OAuth " + authToken);
94 // Listen for the server to respond.
95 int status = link.getResponseCode();
97 case HttpURLConnection.HTTP_OK: // 200
99 case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
100 postError(callback, Error.AUTH_FAILED);
102 case HttpURLConnection.HTTP_BAD_GATEWAY: // 502
103 case HttpURLConnection.HTTP_UNAVAILABLE: // 503
104 postError(callback, Error.SERVICE_UNAVAILABLE);
107 postError(callback, Error.UNKNOWN);
111 StringBuilder responseBuilder = new StringBuilder();
112 Scanner incoming = new Scanner(link.getInputStream());
113 Log.i("auth", "Successfully authenticated to directory server");
114 while (incoming.hasNext()) {
115 responseBuilder.append(incoming.nextLine());
117 response = String.valueOf(responseBuilder);
119 } catch (MalformedURLException ex) {
120 // This should never happen.
121 throw new RuntimeException("Unexpected error while fetching host list: " + ex);
122 } catch (IOException ex) {
123 postError(callback, Error.NETWORK_ERROR);
131 // Parse directory response.
132 ArrayList<HostInfo> hostList = new ArrayList<HostInfo>();
134 JSONObject data = new JSONObject(response).getJSONObject("data");
135 JSONArray hostsJson = data.getJSONArray("items");
136 Log.i("hostlist", "Received host listing from directory server");
139 while (!hostsJson.isNull(index)) {
140 JSONObject hostJson = hostsJson.getJSONObject(index);
141 // If a host is only recently registered, it may be missing some of the keys below.
142 // It should still be visible in the list, even though a connection attempt will
143 // fail because of the missing keys. The failed attempt will trigger reloading of
144 // the host-list (once crbug.com/304719 is fixed), by which time the keys will
145 // hopefully be present, and the retried connection can succeed.
146 HostInfo host = new HostInfo(
147 hostJson.getString("hostName"),
148 hostJson.getString("hostId"),
149 hostJson.optString("jabberId"),
150 hostJson.optString("publicKey"),
151 hostJson.optString("status").equals("ONLINE"));
155 } catch (JSONException ex) {
156 postError(callback, Error.UNEXPECTED_RESPONSE);
162 final Callback callbackFinal = callback;
163 final HostInfo[] hosts = hostList.toArray(new HostInfo[hostList.size()]);
164 mMainThread.post(new Runnable() {
167 callbackFinal.onHostListReceived(hosts);
172 /** Posts error to callback on main thread. */
173 private void postError(Callback callback, Error error) {
174 final Callback callbackFinal = callback;
175 final Error errorFinal = error;
176 mMainThread.post(new Runnable() {
179 callbackFinal.onError(errorFinal);
184 private static void sortHosts(ArrayList<HostInfo> hosts) {
185 Comparator<HostInfo> hostComparator = new Comparator<HostInfo>() {
186 public int compare(HostInfo a, HostInfo b) {
187 if (a.isOnline != b.isOnline) {
188 return a.isOnline ? -1 : 1;
190 String aName = a.name.toUpperCase();
191 String bName = b.name.toUpperCase();
192 return aName.compareTo(bName);
195 Collections.sort(hosts, hostComparator);