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; | |
8 import org.chromium.chromoting.jni.ConnectionListener; | 7 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, HostListLoader.Call
back { | 14 public class SessionConnector implements ConnectionListener, |
15 private Client mClient; | 15 HostListLoader.Callback { |
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(Client client, ConnectionListener connectionListener
, | 41 public SessionConnector(ConnectionListener connectionListener, |
42 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { | 42 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { |
43 mClient = client; | |
44 mConnectionListener = connectionListener; | 43 mConnectionListener = connectionListener; |
45 mHostListCallback = hostListCallback; | 44 mHostListCallback = hostListCallback; |
46 mHostListLoader = hostListLoader; | 45 mHostListLoader = hostListLoader; |
47 } | 46 } |
48 | 47 |
49 /** Initiates a connection to the host. */ | 48 /** Initiates a connection to the host. */ |
50 public void connectToHost(String accountName, String authToken, HostInfo hos
t, | 49 public void connectToHost(String accountName, String authToken, HostInfo hos
t, |
51 SessionAuthenticator authenticator, String flags) { | 50 SessionAuthenticator authenticator, String flags) { |
52 mAccountName = accountName; | 51 mAccountName = accountName; |
53 mAuthToken = authToken; | 52 mAuthToken = authToken; |
54 mHost = host; | 53 mHost = host; |
55 mAuthenticator = authenticator; | 54 mAuthenticator = authenticator; |
56 mFlags = flags; | 55 mFlags = flags; |
57 | 56 |
58 if (hostIncomplete(host)) { | 57 if (hostIncomplete(host)) { |
59 // 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 |
60 // connection failure and reload the host list. | 59 // connection failure and reload the host list. |
61 reloadHostListAndConnect(); | 60 reloadHostListAndConnect(); |
62 return; | 61 return; |
63 } | 62 } |
64 | 63 |
65 doConnect(); | 64 doConnect(); |
66 } | 65 } |
67 | 66 |
68 private void doConnect() { | 67 private void doConnect() { |
69 mClient.connectToHost(mAccountName, mAuthToken, mHost.jabberId, mHost.id
, | 68 JniInterface.connectToHost(mAccountName, mAuthToken, mHost.jabberId, mHo
st.id, |
70 mHost.publicKey, mAuthenticator, mFlags, this); | 69 mHost.publicKey, mAuthenticator, mFlags, this); |
71 } | 70 } |
72 | 71 |
73 private static boolean hostIncomplete(HostInfo host) { | 72 private static boolean hostIncomplete(HostInfo host) { |
74 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); | 73 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); |
75 } | 74 } |
76 | 75 |
77 private void reloadHostListAndConnect() { | 76 private void reloadHostListAndConnect() { |
78 mTriedReloadingHostList = true; | 77 mTriedReloadingHostList = true; |
79 mHostListLoader.retrieveHostList(mAuthToken, this); | 78 mHostListLoader.retrieveHostList(mAuthToken, this); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 // Connection failed and reloading the host list also failed, so report
the connection | 129 // Connection failed and reloading the host list also failed, so report
the connection |
131 // error. | 130 // error. |
132 mConnectionListener.onConnectionState(ConnectionListener.State.FAILED, | 131 mConnectionListener.onConnectionState(ConnectionListener.State.FAILED, |
133 ConnectionListener.Error.PEER_IS_OFFLINE); | 132 ConnectionListener.Error.PEER_IS_OFFLINE); |
134 | 133 |
135 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. | 134 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. |
136 // The currently-displayed host list is not likely to be valid any more. | 135 // The currently-displayed host list is not likely to be valid any more. |
137 mHostListCallback.onError(error); | 136 mHostListCallback.onError(error); |
138 } | 137 } |
139 } | 138 } |
OLD | NEW |