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; |
8 | 9 |
9 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
10 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
11 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
12 import org.chromium.base.annotations.JNINamespace; | 13 import org.chromium.base.annotations.JNINamespace; |
13 import org.chromium.chromoting.OAuthTokenConsumer; | 14 import org.chromium.chromoting.OAuthTokenConsumer; |
14 import org.chromium.chromoting.base.OAuthTokenFetcher; | 15 import org.chromium.chromoting.base.OAuthTokenFetcher; |
15 | 16 |
| 17 import java.nio.ByteBuffer; |
| 18 |
16 /** | 19 /** |
17 * Initializes the Chromium remoting library, and provides JNI calls into it. | 20 * Initializes the Chromium remoting library, and provides JNI calls into it. |
18 * All interaction with the native code is centralized in this class. | 21 * All interaction with the native code is centralized in this class. |
19 */ | 22 */ |
20 @JNINamespace("remoting") | 23 @JNINamespace("remoting") |
21 public class JniInterface { | 24 public class JniInterface { |
22 private static final String TAG = "Chromoting"; | 25 private static final String TAG = "Chromoting"; |
23 | 26 |
24 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com
/auth/chromoting"; | 27 private static final String TOKEN_SCOPE = "oauth2:https://www.googleapis.com
/auth/chromoting"; |
25 | 28 |
26 // Used to fetch auth token for native client. | 29 // Used to fetch auth token for native client. |
27 private static OAuthTokenConsumer sLoggerTokenConsumer; | 30 private static OAuthTokenConsumer sLoggerTokenConsumer; |
28 | 31 |
29 private static String sAccount; | 32 private static String sAccount; |
30 | 33 |
31 /** | 34 /** |
32 * To be called once from the Application context singleton. Loads and initi
alizes the native | 35 * To be called once from the Application context singleton. Loads and initi
alizes the native |
33 * code. Called on the UI thread. | 36 * code. Called on the UI thread. |
34 * @param context The Application context. | 37 * @param context The Application context. |
35 */ | 38 */ |
36 public static void loadLibrary(Context context) { | 39 public static void loadLibrary(Context context) { |
37 ContextUtils.initApplicationContext(context.getApplicationContext()); | 40 ContextUtils.initApplicationContext(context.getApplicationContext()); |
38 sLoggerTokenConsumer = new OAuthTokenConsumer(context.getApplicationCont
ext(), TOKEN_SCOPE); | 41 sLoggerTokenConsumer = new OAuthTokenConsumer(context.getApplicationCont
ext(), TOKEN_SCOPE); |
39 System.loadLibrary("remoting_client_jni"); | 42 System.loadLibrary("remoting_client_jni"); |
40 ContextUtils.initApplicationContextForNative(); | 43 ContextUtils.initApplicationContextForNative(); |
41 nativeLoadNative(); | 44 nativeLoadNative(); |
42 } | 45 } |
43 | 46 |
| 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 |
44 public static void setAccountForLogging(String account) { | 197 public static void setAccountForLogging(String account) { |
45 sAccount = account; | 198 sAccount = account; |
46 } | 199 } |
47 | 200 |
48 @CalledByNative | 201 @CalledByNative |
49 private static void fetchAuthToken() { | 202 private static void fetchAuthToken() { |
50 if (sAccount == null) { | 203 if (sAccount == null) { |
51 throw new IllegalStateException("Account is not set before fetching
the auth token."); | 204 throw new IllegalStateException("Account is not set before fetching
the auth token."); |
52 } | 205 } |
53 sLoggerTokenConsumer.consume(sAccount, new OAuthTokenFetcher.Callback()
{ | 206 sLoggerTokenConsumer.consume(sAccount, new OAuthTokenFetcher.Callback()
{ |
54 @Override | 207 @Override |
55 public void onTokenFetched(String token) { | 208 public void onTokenFetched(String token) { |
56 nativeOnAuthTokenFetched(token); | 209 nativeOnAuthTokenFetched(token); |
57 } | 210 } |
58 | 211 |
59 @Override | 212 @Override |
60 public void onError(OAuthTokenFetcher.Error error) { | 213 public void onError(OAuthTokenFetcher.Error error) { |
61 Log.e(TAG, "Failed to fetch auth token for native client."); | 214 Log.e(TAG, "Failed to fetch auth token for native client."); |
62 } | 215 } |
63 }); | 216 }); |
64 } | 217 } |
65 | 218 |
66 /** Performs the native portion of the initialization. */ | 219 static native void nativeOnAuthTokenFetched(String token); |
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); | |
71 } | 220 } |
OLD | NEW |