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 | 18 |
19 private String mAccountName; | 19 private String mAccountName; |
20 private String mAuthToken; | 20 private String mAuthToken; |
21 | 21 |
22 /** Used to find the HostInfo from the returned array after the host list is
reloaded. */ | 22 /** Used to find the HostInfo from the returned array after the host list is
reloaded. */ |
23 private String mHostId; | 23 private String mHostId; |
24 | 24 |
25 private String mHostJabberId; | 25 private String mHostJabberId; |
26 | 26 |
27 /** | 27 /** |
| 28 * Tracks whether the connection has been established. Auto-reloading and re
connecting should |
| 29 * only happen if connection has not yet occurred. |
| 30 */ |
| 31 private boolean mConnected; |
| 32 |
| 33 /** |
28 * @param connectionCallback Object to be notified on connection success/fai
lure. | 34 * @param connectionCallback Object to be notified on connection success/fai
lure. |
29 * @param hostListCallback Object to be notified whenever the host list is r
eloaded. | 35 * @param hostListCallback Object to be notified whenever the host list is r
eloaded. |
30 * @param hostListLoader The object used for reloading the host list. | 36 * @param hostListLoader The object used for reloading the host list. |
31 */ | 37 */ |
32 public SessionConnector(JniInterface.ConnectionListener connectionCallback, | 38 public SessionConnector(JniInterface.ConnectionListener connectionCallback, |
33 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { | 39 HostListLoader.Callback hostListCallback, HostListLoader hostListLoa
der) { |
34 mConnectionCallback = connectionCallback; | 40 mConnectionCallback = connectionCallback; |
35 mHostListCallback = hostListCallback; | 41 mHostListCallback = hostListCallback; |
36 mHostListLoader = hostListLoader; | 42 mHostListLoader = hostListLoader; |
37 } | 43 } |
(...skipping 20 matching lines...) Expand all Loading... |
58 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); | 64 return host.jabberId.isEmpty() || host.publicKey.isEmpty(); |
59 } | 65 } |
60 | 66 |
61 private void reloadHostListAndConnect() { | 67 private void reloadHostListAndConnect() { |
62 mHostListLoader.retrieveHostList(mAuthToken, this); | 68 mHostListLoader.retrieveHostList(mAuthToken, this); |
63 } | 69 } |
64 | 70 |
65 @Override | 71 @Override |
66 public void onConnectionState(JniInterface.ConnectionListener.State state, | 72 public void onConnectionState(JniInterface.ConnectionListener.State state, |
67 JniInterface.ConnectionListener.Error error) { | 73 JniInterface.ConnectionListener.Error error) { |
68 if (state == JniInterface.ConnectionListener.State.FAILED | 74 boolean connected = mConnected; |
| 75 mConnected = (state == JniInterface.ConnectionListener.State.CONNECTED); |
| 76 |
| 77 if (!connected && state == JniInterface.ConnectionListener.State.FAILED |
69 && error == JniInterface.ConnectionListener.Error.PEER_IS_OFFLIN
E) { | 78 && error == JniInterface.ConnectionListener.Error.PEER_IS_OFFLIN
E) { |
70 // The host is offline, which may mean the JID is out of date, so re
fresh the host list | 79 // The host is offline, which may mean the JID is out of date, so re
fresh the host list |
71 // and try to connect again. | 80 // and try to connect again. |
72 reloadHostListAndConnect(); | 81 reloadHostListAndConnect(); |
73 } else { | 82 } else { |
74 // Pass the state/error back to the caller. | 83 // Pass the state/error back to the caller. |
75 mConnectionCallback.onConnectionState(state, error); | 84 mConnectionCallback.onConnectionState(state, error); |
76 } | 85 } |
77 } | 86 } |
78 | 87 |
(...skipping 29 matching lines...) Expand all Loading... |
108 // Connection failed and reloading the host list also failed, so report
the connection | 117 // Connection failed and reloading the host list also failed, so report
the connection |
109 // error. | 118 // error. |
110 mConnectionCallback.onConnectionState(JniInterface.ConnectionListener.St
ate.FAILED, | 119 mConnectionCallback.onConnectionState(JniInterface.ConnectionListener.St
ate.FAILED, |
111 JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE); | 120 JniInterface.ConnectionListener.Error.PEER_IS_OFFLINE); |
112 | 121 |
113 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. | 122 // Notify the caller that the host list failed to load, so the UI is upd
ated accordingly. |
114 // The currently-displayed host list is not likely to be valid any more. | 123 // The currently-displayed host list is not likely to be valid any more. |
115 mHostListCallback.onError(error); | 124 mHostListCallback.onError(error); |
116 } | 125 } |
117 } | 126 } |
OLD | NEW |