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