Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This file defines functions that implement the static methods declared in a |
| 6 | // closely-related Java class in the platform-specific user interface |
| 7 | // implementation. In effect, it is the entry point for all JNI calls *into* |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 8 | // the C++ codebase from Java. The separate ChromotingJniRuntime class serves |
| 9 | // as the corresponding exit point, and is responsible for making all JNI calls |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 10 | // *out of* the C++ codebase into Java. |
| 11 | |
| 12 | #include <jni.h> |
| 13 | |
| 14 | #include "base/android/jni_android.h" |
| 15 | #include "base/command_line.h" |
| 16 | #include "base/memory/ref_counted.h" |
| 17 | #include "google_apis/google_api_keys.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 18 | #include "remoting/client/jni/chromoting_jni_instance.h" |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 19 | #include "remoting/client/jni/chromoting_jni_runtime.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 20 | |
| 21 | // Class and package name of the Java class that declares this file's functions. |
| 22 | #define JNI_IMPLEMENTATION(method) \ |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 23 | Java_org_chromium_chromoting_jni_JniInterface_##method |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 24 | |
| 25 | extern "C" { |
| 26 | |
| 27 | JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 28 | base::android::InitVM(vm); |
| 29 | return JNI_VERSION_1_2; |
| 30 | } |
| 31 | |
| 32 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(loadNative)(JNIEnv* env, |
| 33 | jobject that, |
| 34 | jobject context) { |
| 35 | base::android::ScopedJavaLocalRef<jobject> context_activity(env, context); |
| 36 | base::android::InitApplicationContext(context_activity); |
| 37 | |
| 38 | // The google_apis functions check the command-line arguments to make sure no |
| 39 | // runtime API keys have been specified by the environment. Unfortunately, we |
| 40 | // neither launch Chromium nor have a command line, so we need to prevent |
| 41 | // them from DCHECKing out when they go looking. |
| 42 | CommandLine::Init(0, NULL); |
| 43 | |
| 44 | // Create the singleton now so that the Chromoting threads will be set up. |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 45 | remoting::ChromotingJniRuntime::GetInstance(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getApiKey)(JNIEnv* env, |
| 49 | jobject that) { |
| 50 | return env->NewStringUTF(google_apis::GetAPIKey().c_str()); |
| 51 | } |
| 52 | |
| 53 | JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getClientId)(JNIEnv* env, |
| 54 | jobject that) { |
| 55 | return env->NewStringUTF( |
| 56 | google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING).c_str()); |
| 57 | } |
| 58 | |
| 59 | JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getClientSecret)(JNIEnv* env, |
| 60 | jobject that) { |
| 61 | return env->NewStringUTF( |
| 62 | google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING).c_str()); |
| 63 | } |
| 64 | |
| 65 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(connectNative)( |
| 66 | JNIEnv* env, |
| 67 | jobject that, |
| 68 | jstring username_jstr, |
| 69 | jstring auth_token_jstr, |
| 70 | jstring host_jid_jstr, |
| 71 | jstring host_id_jstr, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 72 | jstring host_pubkey_jstr, |
| 73 | jstring pair_id_jstr, |
| 74 | jstring pair_secret_jstr) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 75 | const char* username_cstr = env->GetStringUTFChars(username_jstr, NULL); |
| 76 | const char* auth_token_cstr = env->GetStringUTFChars(auth_token_jstr, NULL); |
| 77 | const char* host_jid_cstr = env->GetStringUTFChars(host_jid_jstr, NULL); |
| 78 | const char* host_id_cstr = env->GetStringUTFChars(host_id_jstr, NULL); |
| 79 | const char* host_pubkey_cstr = env->GetStringUTFChars(host_pubkey_jstr, NULL); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 80 | const char* pair_id_cstr = env->GetStringUTFChars(pair_id_jstr, NULL); |
| 81 | const char* pair_secret_cstr = env->GetStringUTFChars(pair_secret_jstr, NULL); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 82 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 83 | remoting::ChromotingJniRuntime::GetInstance()->ConnectToHost( |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 84 | username_cstr, |
| 85 | auth_token_cstr, |
| 86 | host_jid_cstr, |
| 87 | host_id_cstr, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 88 | host_pubkey_cstr, |
| 89 | pair_id_cstr, |
| 90 | pair_secret_cstr); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 91 | |
| 92 | env->ReleaseStringUTFChars(username_jstr, username_cstr); |
| 93 | env->ReleaseStringUTFChars(auth_token_jstr, auth_token_cstr); |
| 94 | env->ReleaseStringUTFChars(host_jid_jstr, host_jid_cstr); |
| 95 | env->ReleaseStringUTFChars(host_id_jstr, host_id_cstr); |
| 96 | env->ReleaseStringUTFChars(host_pubkey_jstr, host_pubkey_cstr); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 97 | env->ReleaseStringUTFChars(pair_id_jstr, pair_id_cstr); |
| 98 | env->ReleaseStringUTFChars(pair_secret_jstr, pair_secret_cstr); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(disconnectNative)(JNIEnv* env, |
| 102 | jobject that) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 103 | remoting::ChromotingJniRuntime::GetInstance()->DisconnectFromHost(); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(authenticationResponse)( |
| 107 | JNIEnv* env, |
| 108 | jobject that, |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 109 | jstring pin_jstr, |
| 110 | jboolean create_pair) { |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 111 | const char* pin_cstr = env->GetStringUTFChars(pin_jstr, NULL); |
| 112 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 113 | remoting::ChromotingJniRuntime::GetInstance()-> |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 114 | session()->ProvideSecret(pin_cstr, create_pair); |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 115 | |
| 116 | env->ReleaseStringUTFChars(pin_jstr, pin_cstr); |
| 117 | } |
| 118 | |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 119 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(scheduleRedrawNative)( |
| 120 | JNIEnv* env, |
| 121 | jobject that) { |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 122 | remoting::ChromotingJniRuntime::GetInstance()->session()->RedrawDesktop(); |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 125 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(mouseActionNative)( |
| 126 | JNIEnv* env, |
| 127 | jobject that, |
| 128 | jint x, |
| 129 | jint y, |
| 130 | jint which_button, |
| 131 | jboolean button_down) { |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 132 | // Button must be within the bounds of the MouseEvent_MouseButton enum. |
| 133 | DCHECK(which_button >= 0 && which_button < 5); |
| 134 | |
Ben Murdoch | bbcdd45 | 2013-07-25 10:06:34 +0100 | [diff] [blame] | 135 | remoting::ChromotingJniRuntime::GetInstance()->session()->PerformMouseAction( |
Ben Murdoch | a3f7b4e | 2013-07-24 10:36:34 +0100 | [diff] [blame] | 136 | x, |
| 137 | y, |
| 138 | static_cast<remoting::protocol::MouseEvent_MouseButton>(which_button), |
| 139 | button_down); |
| 140 | } |
| 141 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 142 | JNIEXPORT void JNICALL JNI_IMPLEMENTATION(keyboardActionNative)( |
| 143 | JNIEnv* env, |
| 144 | jobject that, |
| 145 | jint key_code, |
| 146 | jboolean key_down) { |
| 147 | remoting::ChromotingJniRuntime::GetInstance()->session()-> |
| 148 | PerformKeyboardAction(key_code, key_down); |
| 149 | } |
| 150 | |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 151 | } // extern "C" |