| 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 mConnectionCallback; | 16 private ConnectionListener mConnectionCallback; |
| 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 /** Used to find the HostInfo from the returned array after the host list is
reloaded. */ | 24 /** Used to find the HostInfo from the returned array after the host list is
reloaded. */ |
| 25 private String mHostId; | 25 private String mHostId; |
| 26 | 26 |
| 27 private String mHostJabberId; | 27 private String mHostJabberId; |
| 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 mConnected; | 33 private boolean mConnected; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * @param connectionCallback Object to be notified on connection success/fai
lure. | 36 * @param connectionCallback Object to be notified on connection success/fai
lure. |
| 37 * @param hostListCallback Object to be notified whenever the host list is r
eloaded. | 37 * @param hostListCallback Object to be notified whenever the host list is r
eloaded. |
| 38 * @param hostListLoader The object used for reloading the host list. | 38 * @param hostListLoader The object used for reloading the host list. |
| 39 */ | 39 */ |
| 40 public SessionConnector(ConnectionListener connectionCallback, | 40 public SessionConnector(Client client, ConnectionListener connectionCallback
, |
| 41 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { | 41 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { |
| 42 mClient = client; |
| 42 mConnectionCallback = connectionCallback; | 43 mConnectionCallback = connectionCallback; |
| 43 mHostListCallback = hostListCallback; | 44 mHostListCallback = hostListCallback; |
| 44 mHostListLoader = hostListLoader; | 45 mHostListLoader = hostListLoader; |
| 45 } | 46 } |
| 46 | 47 |
| 47 /** Initiates a connection to the host. */ | 48 /** Initiates a connection to the host. */ |
| 48 public void connectToHost(String accountName, String authToken, HostInfo hos
t, | 49 public void connectToHost(String accountName, String authToken, HostInfo hos
t, |
| 49 SessionAuthenticator authenticator) { | 50 SessionAuthenticator authenticator) { |
| 50 mAccountName = accountName; | 51 mAccountName = accountName; |
| 51 mAuthToken = authToken; | 52 mAuthToken = authToken; |
| 52 mHostId = host.id; | 53 mHostId = host.id; |
| 53 mHostJabberId = host.jabberId; | 54 mHostJabberId = host.jabberId; |
| 54 mAuthenticator = authenticator; | 55 mAuthenticator = authenticator; |
| 55 | 56 |
| 56 if (hostIncomplete(host)) { | 57 if (hostIncomplete(host)) { |
| 57 // These keys might not be present in a newly-registered host, so tr
eat this as a | 58 // These keys might not be present in a newly-registered host, so tr
eat this as a |
| 58 // connection failure and reload the host list. | 59 // connection failure and reload the host list. |
| 59 reloadHostListAndConnect(); | 60 reloadHostListAndConnect(); |
| 60 return; | 61 return; |
| 61 } | 62 } |
| 62 | 63 |
| 63 JniInterface.connectToHost(accountName, authToken, host.jabberId, host.i
d, host.publicKey, | 64 mClient.connectToHost(accountName, authToken, host.jabberId, host.id, ho
st.publicKey, |
| 64 this, mAuthenticator); | 65 this, mAuthenticator); |
| 65 } | 66 } |
| 66 | 67 |
| 67 private static boolean hostIncomplete(HostInfo host) { | 68 private static boolean hostIncomplete(HostInfo host) { |
| 68 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); | 69 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 private void reloadHostListAndConnect() { | 72 private void reloadHostListAndConnect() { |
| 72 mHostListLoader.retrieveHostList(mAuthToken, this); | 73 mHostListLoader.retrieveHostList(mAuthToken, this); |
| 73 } | 74 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 103 | 104 |
| 104 if (foundHost == null || foundHost.jabberId.equals(mHostJabberId) | 105 if (foundHost == null || foundHost.jabberId.equals(mHostJabberId) |
| 105 || hostIncomplete(foundHost)) { | 106 || hostIncomplete(foundHost)) { |
| 106 // Cannot reconnect to this host, or there's no point in trying beca
use the JID is | 107 // Cannot reconnect to this host, or there's no point in trying beca
use the JID is |
| 107 // unchanged, so report the original failure to the client. | 108 // unchanged, so report the original failure to the client. |
| 108 mConnectionCallback.onConnectionState(ConnectionListener.State.FAILE
D, | 109 mConnectionCallback.onConnectionState(ConnectionListener.State.FAILE
D, |
| 109 ConnectionListener.Error.PEER_IS_OFFLINE); | 110 ConnectionListener.Error.PEER_IS_OFFLINE); |
| 110 } else { | 111 } else { |
| 111 // Reconnect to the host, but use the original callback directly, in
stead of this | 112 // Reconnect to the host, but use the original callback directly, in
stead of this |
| 112 // wrapper object, so the host list is not loaded again. | 113 // wrapper object, so the host list is not loaded again. |
| 113 JniInterface.connectToHost(mAccountName, mAuthToken, foundHost.jabbe
rId, | 114 mClient.connectToHost(mAccountName, mAuthToken, foundHost.jabberId, |
| 114 foundHost.id, foundHost.publicKey, mConnectionCallback, mAut
henticator); | 115 foundHost.id, foundHost.publicKey, mConnectionCallback, mAut
henticator); |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 | 118 |
| 118 @Override | 119 @Override |
| 119 public void onError(HostListLoader.Error error) { | 120 public void onError(HostListLoader.Error error) { |
| 120 // Connection failed and reloading the host list also failed, so report
the connection | 121 // Connection failed and reloading the host list also failed, so report
the connection |
| 121 // error. | 122 // error. |
| 122 mConnectionCallback.onConnectionState(ConnectionListener.State.FAILED, | 123 mConnectionCallback.onConnectionState(ConnectionListener.State.FAILED, |
| 123 ConnectionListener.Error.PEER_IS_OFFLINE); | 124 ConnectionListener.Error.PEER_IS_OFFLINE); |
| 124 | 125 |
| 125 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. | 126 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. |
| 126 // The currently-displayed host list is not likely to be valid any more. | 127 // The currently-displayed host list is not likely to be valid any more. |
| 127 mHostListCallback.onError(error); | 128 mHostListCallback.onError(error); |
| 128 } | 129 } |
| 129 } | 130 } |
| OLD | NEW |