| 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 org.chromium.chromoting.jni.Client; |
| 7 import org.chromium.chromoting.jni.ConnectionListener; | 8 import org.chromium.chromoting.jni.ConnectionListener; |
| 8 import org.chromium.chromoting.jni.JniInterface; | |
| 9 | 9 |
| 10 /** | 10 /** |
| 11 * This class manages making a connection to a host, with logic for reloading th
e host list and | 11 * This class manages making a connection to a host, with logic for reloading th
e host list and |
| 12 * retrying the connection in the case of a stale host JID. | 12 * retrying the connection in the case of a stale host JID. |
| 13 */ | 13 */ |
| 14 public class SessionConnector implements ConnectionListener, | 14 public class SessionConnector implements ConnectionListener, HostListLoader.Call
back { |
| 15 HostListLoader.Callback { | 15 private Client mClient; |
| 16 private ConnectionListener mConnectionListener; | 16 private ConnectionListener mConnectionListener; |
| 17 private HostListLoader.Callback mHostListCallback; | 17 private HostListLoader.Callback mHostListCallback; |
| 18 private HostListLoader mHostListLoader; | 18 private HostListLoader mHostListLoader; |
| 19 private SessionAuthenticator mAuthenticator; | 19 private SessionAuthenticator mAuthenticator; |
| 20 | 20 |
| 21 private String mAccountName; | 21 private String mAccountName; |
| 22 private String mAuthToken; | 22 private String mAuthToken; |
| 23 | 23 |
| 24 /* HostInfo for the host we are connecting to. */ | 24 /* HostInfo for the host we are connecting to. */ |
| 25 private HostInfo mHost; | 25 private HostInfo mHost; |
| 26 | 26 |
| 27 private String mFlags; | 27 private String mFlags; |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Tracks whether the connection has been established. Auto-reloading and re
connecting should | 30 * Tracks whether the connection has been established. Auto-reloading and re
connecting should |
| 31 * only happen if connection has not yet occurred. | 31 * only happen if connection has not yet occurred. |
| 32 */ | 32 */ |
| 33 private boolean mWasConnected; | 33 private boolean mWasConnected; |
| 34 private boolean mTriedReloadingHostList; | 34 private boolean mTriedReloadingHostList; |
| 35 | 35 |
| 36 /** | 36 /** |
| 37 * @param connectionListener Object to be notified on connection success/fai
lure. | 37 * @param connectionListener Object to be notified on connection success/fai
lure. |
| 38 * @param hostListCallback Object to be notified whenever the host list is r
eloaded. | 38 * @param hostListCallback Object to be notified whenever the host list is r
eloaded. |
| 39 * @param hostListLoader The object used for reloading the host list. | 39 * @param hostListLoader The object used for reloading the host list. |
| 40 */ | 40 */ |
| 41 public SessionConnector(ConnectionListener connectionListener, | 41 public SessionConnector(Client client, ConnectionListener connectionListener
, |
| 42 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { | 42 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { |
| 43 mClient = client; |
| 43 mConnectionListener = connectionListener; | 44 mConnectionListener = connectionListener; |
| 44 mHostListCallback = hostListCallback; | 45 mHostListCallback = hostListCallback; |
| 45 mHostListLoader = hostListLoader; | 46 mHostListLoader = hostListLoader; |
| 46 } | 47 } |
| 47 | 48 |
| 48 /** Initiates a connection to the host. */ | 49 /** Initiates a connection to the host. */ |
| 49 public void connectToHost(String accountName, String authToken, HostInfo hos
t, | 50 public void connectToHost(String accountName, String authToken, HostInfo hos
t, |
| 50 SessionAuthenticator authenticator, String flags) { | 51 SessionAuthenticator authenticator, String flags) { |
| 51 mAccountName = accountName; | 52 mAccountName = accountName; |
| 52 mAuthToken = authToken; | 53 mAuthToken = authToken; |
| 53 mHost = host; | 54 mHost = host; |
| 54 mAuthenticator = authenticator; | 55 mAuthenticator = authenticator; |
| 55 mFlags = flags; | 56 mFlags = flags; |
| 56 | 57 |
| 57 if (hostIncomplete(host)) { | 58 if (hostIncomplete(host)) { |
| 58 // These keys might not be present in a newly-registered host, so tr
eat this as a | 59 // These keys might not be present in a newly-registered host, so tr
eat this as a |
| 59 // connection failure and reload the host list. | 60 // connection failure and reload the host list. |
| 60 reloadHostListAndConnect(); | 61 reloadHostListAndConnect(); |
| 61 return; | 62 return; |
| 62 } | 63 } |
| 63 | 64 |
| 64 doConnect(); | 65 doConnect(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 private void doConnect() { | 68 private void doConnect() { |
| 68 JniInterface.connectToHost(mAccountName, mAuthToken, mHost.jabberId, mHo
st.id, | 69 mClient.connectToHost(mAccountName, mAuthToken, mHost.jabberId, mHost.id
, |
| 69 mHost.publicKey, mAuthenticator, mFlags, this); | 70 mHost.publicKey, mAuthenticator, mFlags, this); |
| 70 } | 71 } |
| 71 | 72 |
| 72 private static boolean hostIncomplete(HostInfo host) { | 73 private static boolean hostIncomplete(HostInfo host) { |
| 73 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); | 74 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 private void reloadHostListAndConnect() { | 77 private void reloadHostListAndConnect() { |
| 77 mTriedReloadingHostList = true; | 78 mTriedReloadingHostList = true; |
| 78 mHostListLoader.retrieveHostList(mAuthToken, this); | 79 mHostListLoader.retrieveHostList(mAuthToken, this); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // Connection failed and reloading the host list also failed, so report
the connection | 130 // Connection failed and reloading the host list also failed, so report
the connection |
| 130 // error. | 131 // error. |
| 131 mConnectionListener.onConnectionState(ConnectionListener.State.FAILED, | 132 mConnectionListener.onConnectionState(ConnectionListener.State.FAILED, |
| 132 ConnectionListener.Error.PEER_IS_OFFLINE); | 133 ConnectionListener.Error.PEER_IS_OFFLINE); |
| 133 | 134 |
| 134 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. | 135 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. |
| 135 // The currently-displayed host list is not likely to be valid any more. | 136 // The currently-displayed host list is not likely to be valid any more. |
| 136 mHostListCallback.onError(error); | 137 mHostListCallback.onError(error); |
| 137 } | 138 } |
| 138 } | 139 } |
| OLD | NEW |