OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.jni; | 5 package org.chromium.chromoting.jni; |
6 | 6 |
7 import org.chromium.base.annotations.CalledByNative; | 7 import org.chromium.base.annotations.CalledByNative; |
8 import org.chromium.base.annotations.JNINamespace; | 8 import org.chromium.base.annotations.JNINamespace; |
9 import org.chromium.base.annotations.SuppressFBWarnings; | 9 import org.chromium.base.annotations.SuppressFBWarnings; |
10 import org.chromium.chromoting.CapabilityManager; | 10 import org.chromium.chromoting.CapabilityManager; |
11 import org.chromium.chromoting.InputStub; | 11 import org.chromium.chromoting.InputStub; |
12 import org.chromium.chromoting.SessionAuthenticator; | 12 import org.chromium.chromoting.SessionAuthenticator; |
13 | 13 |
14 /** | 14 /** |
15 * Class to manage a client connection to the host. This class controls the life
time of the | 15 * Class to manage a client connection to the host. This class controls the life
time of the |
16 * corresponding C++ object which implements the connection. A new object should
be created for | 16 * corresponding C++ object which implements the connection. A new object should
be created for |
17 * each connection to the host, so that notifications from a connection are alwa
ys sent to the | 17 * each connection to the host, so that notifications from a connection are alwa
ys sent to the |
18 * right object. | 18 * right object. |
19 * This class is used entirely on the UI thread. | 19 * This class is used entirely on the UI thread. |
20 */ | 20 */ |
21 @JNINamespace("remoting") | 21 @JNINamespace("remoting") |
22 public class Client implements InputStub { | 22 public class Client implements InputStub { |
23 // Pointer to the C++ object, cast to a |long|. | 23 // Pointer to the C++ object, cast to a |long|. |
24 private long mNativeJniClient; | 24 private long mNativeJniClient; |
25 | 25 |
26 // Reference has to be kept until the lifecycle of Client ends. Code are cur
rently using | 26 // Implementation-dependent display object used by the desktop view. |
27 // getDisplay() without doing a null check. | 27 private Object mDisplay; |
28 private Display mDisplay; | |
29 | 28 |
30 // The global Client instance (may be null). This needs to be a global singl
eton so that the | 29 // The global Client instance (may be null). This needs to be a global singl
eton so that the |
31 // Client can be passed between Activities. | 30 // Client can be passed between Activities. |
32 private static Client sClient; | 31 private static Client sClient; |
33 | 32 |
34 public Client() { | 33 public Client() { |
35 if (sClient != null) { | 34 if (sClient != null) { |
36 throw new RuntimeException("Client instance already created."); | 35 throw new RuntimeException("Client instance already created."); |
37 } | 36 } |
38 | 37 |
39 sClient = this; | 38 sClient = this; |
40 mNativeJniClient = nativeInit(); | 39 mNativeJniClient = nativeInit(); |
41 } | 40 } |
42 | 41 |
43 /** | 42 /** |
44 * Returns the display object. It will be null before calling connectToHost(
) but won't be null | 43 * Sets the display object. Called by the native code when the connection st
arts. |
45 * after calling disconnectFromHost(). | 44 * @param display the implementation-dependent object used by the desktop vi
ew. |
| 45 */ |
| 46 @CalledByNative |
| 47 private void setDisplay(Object display) { |
| 48 mDisplay = display; |
| 49 } |
| 50 |
| 51 /** |
| 52 * Returns the display object. It will be null before calling connectToHost(
) or after calling |
| 53 * disconnectFromHost(). |
46 * @return the display object. | 54 * @return the display object. |
47 */ | 55 */ |
48 public Display getDisplay() { | 56 public Object getDisplay() { |
49 return mDisplay; | 57 return mDisplay; |
50 } | 58 } |
51 | 59 |
52 // Suppress FindBugs warning, since |sClient| is only used on the UI thread. | 60 // Suppress FindBugs warning, since |sClient| is only used on the UI thread. |
53 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") | 61 @SuppressFBWarnings("LI_LAZY_INIT_STATIC") |
54 public void destroy() { | 62 public void destroy() { |
55 if (sClient != null) { | 63 if (sClient != null) { |
56 disconnectFromHost(); | 64 disconnectFromHost(); |
57 nativeDestroy(mNativeJniClient); | 65 nativeDestroy(mNativeJniClient); |
58 sClient = null; | 66 sClient = null; |
(...skipping 27 matching lines...) Expand all Loading... |
86 } | 94 } |
87 | 95 |
88 /** Attempts to form a connection to the user-selected host. */ | 96 /** Attempts to form a connection to the user-selected host. */ |
89 public void connectToHost(String username, String authToken, String hostJid, | 97 public void connectToHost(String username, String authToken, String hostJid, |
90 String hostId, String hostPubkey, SessionAuthenticator authenticator
, String flags, | 98 String hostId, String hostPubkey, SessionAuthenticator authenticator
, String flags, |
91 ConnectionListener listener) { | 99 ConnectionListener listener) { |
92 disconnectFromHost(); | 100 disconnectFromHost(); |
93 | 101 |
94 mConnectionListener = listener; | 102 mConnectionListener = listener; |
95 mAuthenticator = authenticator; | 103 mAuthenticator = authenticator; |
96 mDisplay = new Display(); | 104 nativeConnect(mNativeJniClient, username, authToken, hostJid, |
97 nativeConnect(mNativeJniClient, mDisplay.getNativePointer(), username, a
uthToken, hostJid, | |
98 hostId, hostPubkey, mAuthenticator.getPairingId(hostId), | 105 hostId, hostPubkey, mAuthenticator.getPairingId(hostId), |
99 mAuthenticator.getPairingSecret(hostId), mCapabilityManager.getL
ocalCapabilities(), | 106 mAuthenticator.getPairingSecret(hostId), mCapabilityManager.getL
ocalCapabilities(), |
100 flags); | 107 flags); |
101 mConnected = true; | 108 mConnected = true; |
102 } | 109 } |
103 | 110 |
104 /** Severs the connection and cleans up. */ | 111 /** Severs the connection and cleans up. */ |
105 public void disconnectFromHost() { | 112 public void disconnectFromHost() { |
106 if (!mConnected) { | 113 if (!mConnected) { |
107 return; | 114 return; |
108 } | 115 } |
109 | 116 |
110 mConnectionListener.onConnectionState( | 117 mConnectionListener.onConnectionState( |
111 ConnectionListener.State.CLOSED, ConnectionListener.Error.OK); | 118 ConnectionListener.State.CLOSED, ConnectionListener.Error.OK); |
112 | 119 |
113 disconnectFromHostWithoutNotification(); | 120 disconnectFromHostWithoutNotification(); |
114 } | 121 } |
115 | 122 |
116 /** Same as disconnectFromHost() but without notifying the ConnectionListene
r. */ | 123 /** Same as disconnectFromHost() but without notifying the ConnectionListene
r. */ |
117 private void disconnectFromHostWithoutNotification() { | 124 private void disconnectFromHostWithoutNotification() { |
118 if (!mConnected) { | 125 if (!mConnected) { |
119 return; | 126 return; |
120 } | 127 } |
121 | 128 |
122 nativeDisconnect(mNativeJniClient); | 129 nativeDisconnect(mNativeJniClient); |
123 mConnectionListener = null; | 130 mConnectionListener = null; |
124 mConnected = false; | 131 mConnected = false; |
125 mCapabilityManager.onHostDisconnect(); | 132 mCapabilityManager.onHostDisconnect(); |
126 | 133 |
127 mDisplay.destroy(); | 134 mDisplay = null; |
128 } | 135 } |
129 | 136 |
130 /** Called whenever the connection status changes. */ | 137 /** Called whenever the connection status changes. */ |
131 @CalledByNative | 138 @CalledByNative |
132 void onConnectionState(int stateCode, int errorCode) { | 139 void onConnectionState(int stateCode, int errorCode) { |
133 ConnectionListener.State state = ConnectionListener.State.fromValue(stat
eCode); | 140 ConnectionListener.State state = ConnectionListener.State.fromValue(stat
eCode); |
134 ConnectionListener.Error error = ConnectionListener.Error.fromValue(erro
rCode); | 141 ConnectionListener.Error error = ConnectionListener.Error.fromValue(erro
rCode); |
135 mConnectionListener.onConnectionState(state, error); | 142 mConnectionListener.onConnectionState(state, error); |
136 if (state == ConnectionListener.State.FAILED || state == ConnectionListe
ner.State.CLOSED) { | 143 if (state == ConnectionListener.State.FAILED || state == ConnectionListe
ner.State.CLOSED) { |
137 // Disconnect from the host here, otherwise the next time connectToH
ost() is called, | 144 // Disconnect from the host here, otherwise the next time connectToH
ost() is called, |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 } | 296 } |
290 | 297 |
291 nativeSendExtensionMessage(mNativeJniClient, type, data); | 298 nativeSendExtensionMessage(mNativeJniClient, type, data); |
292 } | 299 } |
293 | 300 |
294 private native long nativeInit(); | 301 private native long nativeInit(); |
295 | 302 |
296 private native void nativeDestroy(long nativeJniClient); | 303 private native void nativeDestroy(long nativeJniClient); |
297 | 304 |
298 /** Performs the native portion of the connection. */ | 305 /** Performs the native portion of the connection. */ |
299 private native void nativeConnect(long nativeJniClient, long nativeJniDispla
yHandler, | 306 private native void nativeConnect(long nativeJniClient, |
300 String username, String authToken, String hostJid, String hostId, St
ring hostPubkey, | 307 String username, String authToken, String hostJid, String hostId, St
ring hostPubkey, |
301 String pairId, String pairSecret, String capabilities, String flags)
; | 308 String pairId, String pairSecret, String capabilities, String flags)
; |
302 | 309 |
303 /** Native implementation of Client.handleAuthenticationResponse(). */ | 310 /** Native implementation of Client.handleAuthenticationResponse(). */ |
304 private native void nativeAuthenticationResponse( | 311 private native void nativeAuthenticationResponse( |
305 long nativeJniClient, String pin, boolean createPair, String deviceN
ame); | 312 long nativeJniClient, String pin, boolean createPair, String deviceN
ame); |
306 | 313 |
307 /** Performs the native portion of the cleanup. */ | 314 /** Performs the native portion of the cleanup. */ |
308 private native void nativeDisconnect(long nativeJniClient); | 315 private native void nativeDisconnect(long nativeJniClient); |
309 | 316 |
(...skipping 18 matching lines...) Expand all Loading... |
328 /** Passes touch event information to the native handling code. */ | 335 /** Passes touch event information to the native handling code. */ |
329 private native void nativeSendTouchEvent( | 336 private native void nativeSendTouchEvent( |
330 long nativeJniClient, int eventType, TouchEventData[] data); | 337 long nativeJniClient, int eventType, TouchEventData[] data); |
331 | 338 |
332 /** Native implementation of Client.enableVideoChannel() */ | 339 /** Native implementation of Client.enableVideoChannel() */ |
333 private native void nativeEnableVideoChannel(long nativeJniClient, boolean e
nable); | 340 private native void nativeEnableVideoChannel(long nativeJniClient, boolean e
nable); |
334 | 341 |
335 /** Passes extension message to the native code. */ | 342 /** Passes extension message to the native code. */ |
336 private native void nativeSendExtensionMessage(long nativeJniClient, String
type, String data); | 343 private native void nativeSendExtensionMessage(long nativeJniClient, String
type, String data); |
337 } | 344 } |
OLD | NEW |