OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Bitmap; | |
9 | 8 |
10 import org.chromium.base.ContextUtils; | 9 import org.chromium.base.ContextUtils; |
11 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
12 import org.chromium.base.annotations.CalledByNative; | 11 import org.chromium.base.annotations.CalledByNative; |
13 import org.chromium.base.annotations.JNINamespace; | 12 import org.chromium.base.annotations.JNINamespace; |
14 import org.chromium.chromoting.OAuthTokenConsumer; | 13 import org.chromium.chromoting.OAuthTokenConsumer; |
15 import org.chromium.chromoting.base.OAuthTokenFetcher; | 14 import org.chromium.chromoting.base.OAuthTokenFetcher; |
16 | 15 |
17 import java.nio.ByteBuffer; | |
18 | |
19 /** | 16 /** |
20 * Initializes the Chromium remoting library, and provides JNI calls into it. | 17 * Initializes the Chromium remoting library, and provides JNI calls into it. |
21 * All interaction with the native code is centralized in this class. | 18 * All interaction with the native code is centralized in this class. |
22 */ | 19 */ |
23 @JNINamespace("remoting") | 20 @JNINamespace("remoting") |
24 public class JniInterface { | 21 public class JniInterface { |
25 private static final String TAG = "Chromoting"; | 22 private static final String TAG = "Chromoting"; |
26 | 23 |
27 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com
/auth/chromoting"; | 24 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com
/auth/chromoting"; |
28 | 25 |
29 // Used to fetch auth token for native client. | 26 // Used to fetch auth token for native client. |
30 private static OAuthTokenConsumer sLoggerTokenConsumer; | 27 private static OAuthTokenConsumer sLoggerTokenConsumer; |
31 | 28 |
32 private static String sAccount; | 29 private static String sAccount; |
33 | 30 |
34 /** | 31 /** |
35 * To be called once from the Application context singleton. Loads and initi
alizes the native | 32 * To be called once from the Application context singleton. Loads and initi
alizes the native |
36 * code. Called on the UI thread. | 33 * code. Called on the UI thread. |
37 * @param context The Application context. | 34 * @param context The Application context. |
38 */ | 35 */ |
39 public static void loadLibrary(Context context) { | 36 public static void loadLibrary(Context context) { |
40 ContextUtils.initApplicationContext(context.getApplicationContext()); | 37 ContextUtils.initApplicationContext(context.getApplicationContext()); |
41 sLoggerTokenConsumer = new OAuthTokenConsumer(context.getApplicationCont
ext(), TOKEN_SCOPE); | 38 sLoggerTokenConsumer = new OAuthTokenConsumer(context.getApplicationCont
ext(), TOKEN_SCOPE); |
42 System.loadLibrary("remoting_client_jni"); | 39 System.loadLibrary("remoting_client_jni"); |
43 ContextUtils.initApplicationContextForNative(); | 40 ContextUtils.initApplicationContextForNative(); |
44 nativeLoadNative(); | 41 nativeLoadNative(); |
45 } | 42 } |
46 | 43 |
47 /** Performs the native portion of the initialization. */ | |
48 private static native void nativeLoadNative(); | |
49 | |
50 /** Performs the native portion of the connection. */ | |
51 static native void nativeConnect(String username, String authToken, String h
ostJid, | |
52 String hostId, String hostPubkey, String pairId, String pairSecret, | |
53 String capabilities, String flags); | |
54 | |
55 /** Performs the native portion of the cleanup. */ | |
56 static native void nativeDisconnect(); | |
57 | |
58 /** Called by native code whenever the connection status changes. Called on
the UI thread. */ | |
59 @CalledByNative | |
60 private static void onConnectionState(int stateCode, int errorCode) { | |
61 if (Client.getInstance() != null) { | |
62 Client.getInstance().onConnectionState(stateCode, errorCode); | |
63 } | |
64 } | |
65 | |
66 /** Prompts the user to enter a PIN. Called on the UI thread. */ | |
67 @CalledByNative | |
68 private static void displayAuthenticationPrompt(boolean pairingSupported) { | |
69 if (Client.getInstance() != null) { | |
70 Client.getInstance().displayAuthenticationPrompt(pairingSupported); | |
71 } | |
72 } | |
73 | |
74 /** Native implementation of Client.handleAuthenticationResponse(). */ | |
75 static native void nativeAuthenticationResponse( | |
76 String pin, boolean createPair, String deviceName); | |
77 | |
78 /** Saves newly-received pairing credentials to permanent storage. Called on
the UI thread. */ | |
79 @CalledByNative | |
80 private static void commitPairingCredentials(String host, String id, String
secret) { | |
81 if (Client.getInstance() != null) { | |
82 Client.getInstance().commitPairingCredentials(host, id, secret); | |
83 } | |
84 } | |
85 | |
86 /** Passes mouse information to the native handling code. */ | |
87 static native void nativeSendMouseEvent( | |
88 int x, int y, int whichButton, boolean buttonDown); | |
89 | |
90 /** Passes mouse-wheel information to the native handling code. */ | |
91 static native void nativeSendMouseWheelEvent(int deltaX, int deltaY); | |
92 | |
93 /** | |
94 * Passes key press information to the native handling code. | |
95 */ | |
96 static native boolean nativeSendKeyEvent(int scanCode, int keyCode, boolean
keyDown); | |
97 | |
98 /** Passes text event information to the native handling code. */ | |
99 static native void nativeSendTextEvent(String text); | |
100 | |
101 /** Passes touch event information to the native handling code. */ | |
102 static native void nativeSendTouchEvent(int eventType, TouchEventData[] data
); | |
103 | |
104 /** Native implementation of Client.enableVideoChannel() */ | |
105 static native void nativeEnableVideoChannel(boolean enable); | |
106 | |
107 /** Schedules a redraw on the native graphics thread. */ | |
108 static native void nativeScheduleRedraw(); | |
109 | |
110 /** | |
111 * Performs the redrawing callback. This is a no-op if the window isn't visi
ble. Called on the | |
112 * graphics thread. | |
113 */ | |
114 @CalledByNative | |
115 private static void redrawGraphicsInternal() { | |
116 Client client = Client.getInstance(); | |
117 if (client != null) { | |
118 client.redrawGraphicsInternal(); | |
119 } | |
120 } | |
121 | |
122 /** | |
123 * Sets a new video frame. Called on the native graphics thread when a new f
rame is allocated. | |
124 */ | |
125 @CalledByNative | |
126 private static void setVideoFrame(Bitmap bitmap) { | |
127 Client client = Client.getInstance(); | |
128 if (client != null) { | |
129 client.setVideoFrame(bitmap); | |
130 } | |
131 } | |
132 | |
133 /** | |
134 * Creates a new Bitmap to hold video frame pixels. Called by native code wh
ich stores a global | |
135 * reference to the Bitmap and writes the decoded frame pixels to it. | |
136 */ | |
137 @CalledByNative | |
138 private static Bitmap newBitmap(int width, int height) { | |
139 return Client.newBitmap(width, height); | |
140 } | |
141 | |
142 /** | |
143 * Updates the cursor shape. This is called on the graphics thread when rece
iving a new cursor | |
144 * shape from the host. | |
145 */ | |
146 @CalledByNative | |
147 private static void updateCursorShape( | |
148 int width, int height, int hotspotX, int hotspotY, ByteBuffer buffer
) { | |
149 Client client = Client.getInstance(); | |
150 if (client != null) { | |
151 client.updateCursorShape(width, height, hotspotX, hotspotY, buffer); | |
152 } | |
153 } | |
154 | |
155 // | |
156 // Third Party Authentication | |
157 // | |
158 | |
159 /** Pops up a third party login page to fetch the token required for authent
ication. */ | |
160 @CalledByNative | |
161 private static void fetchThirdPartyToken(String tokenUrl, String clientId, S
tring scope) { | |
162 if (Client.getInstance() != null) { | |
163 Client.getInstance().fetchThirdPartyToken(tokenUrl, clientId, scope)
; | |
164 } | |
165 } | |
166 | |
167 /** Passes authentication data to the native handling code. */ | |
168 static native void nativeOnThirdPartyTokenFetched(String token, String share
dSecret); | |
169 | |
170 // | |
171 // Host and Client Capabilities | |
172 // | |
173 | |
174 /** Set the list of negotiated capabilities between host and client. Called
on the UI thread. */ | |
175 @CalledByNative | |
176 private static void setCapabilities(String capabilities) { | |
177 if (Client.getInstance() != null) { | |
178 Client.getInstance().setCapabilities(capabilities); | |
179 } | |
180 } | |
181 | |
182 // | |
183 // Extension Message Handling | |
184 // | |
185 | |
186 /** Passes on the deconstructed ExtensionMessage to the app. Called on the U
I thread. */ | |
187 @CalledByNative | |
188 private static void handleExtensionMessage(String type, String data) { | |
189 if (Client.getInstance() != null) { | |
190 Client.getInstance().handleExtensionMessage(type, data); | |
191 } | |
192 } | |
193 | |
194 /** Passes extension message to the native code. */ | |
195 static native void nativeSendExtensionMessage(String type, String data); | |
196 | |
197 public static void setAccountForLogging(String account) { | 44 public static void setAccountForLogging(String account) { |
198 sAccount = account; | 45 sAccount = account; |
199 } | 46 } |
200 | 47 |
201 @CalledByNative | 48 @CalledByNative |
202 private static void fetchAuthToken() { | 49 private static void fetchAuthToken() { |
203 if (sAccount == null) { | 50 if (sAccount == null) { |
204 throw new IllegalStateException("Account is not set before fetching
the auth token."); | 51 throw new IllegalStateException("Account is not set before fetching
the auth token."); |
205 } | 52 } |
206 sLoggerTokenConsumer.consume(sAccount, new OAuthTokenFetcher.Callback()
{ | 53 sLoggerTokenConsumer.consume(sAccount, new OAuthTokenFetcher.Callback()
{ |
207 @Override | 54 @Override |
208 public void onTokenFetched(String token) { | 55 public void onTokenFetched(String token) { |
209 nativeOnAuthTokenFetched(token); | 56 nativeOnAuthTokenFetched(token); |
210 } | 57 } |
211 | 58 |
212 @Override | 59 @Override |
213 public void onError(OAuthTokenFetcher.Error error) { | 60 public void onError(OAuthTokenFetcher.Error error) { |
214 Log.e(TAG, "Failed to fetch auth token for native client."); | 61 Log.e(TAG, "Failed to fetch auth token for native client."); |
215 } | 62 } |
216 }); | 63 }); |
217 } | 64 } |
218 | 65 |
219 static native void nativeOnAuthTokenFetched(String token); | 66 /** Performs the native portion of the initialization. */ |
| 67 private static native void nativeLoadNative(); |
| 68 |
| 69 /** Notifies the native client with the new auth token */ |
| 70 private static native void nativeOnAuthTokenFetched(String token); |
220 } | 71 } |
OLD | NEW |