Home | History | Annotate | Download | only in jni
      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*
      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
     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"
     18 #include "remoting/client/jni/chromoting_jni_instance.h"
     19 #include "remoting/client/jni/chromoting_jni_runtime.h"
     20 
     21 // Class and package name of the Java class that declares this file's functions.
     22 #define JNI_IMPLEMENTATION(method) \
     23         Java_org_chromium_chromoting_jni_JniInterface_##method
     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.
     45   remoting::ChromotingJniRuntime::GetInstance();
     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,
     72     jstring host_pubkey_jstr,
     73     jstring pair_id_jstr,
     74     jstring pair_secret_jstr) {
     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);
     80   const char* pair_id_cstr = env->GetStringUTFChars(pair_id_jstr, NULL);
     81   const char* pair_secret_cstr = env->GetStringUTFChars(pair_secret_jstr, NULL);
     82 
     83   remoting::ChromotingJniRuntime::GetInstance()->ConnectToHost(
     84       username_cstr,
     85       auth_token_cstr,
     86       host_jid_cstr,
     87       host_id_cstr,
     88       host_pubkey_cstr,
     89       pair_id_cstr,
     90       pair_secret_cstr);
     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);
     97   env->ReleaseStringUTFChars(pair_id_jstr, pair_id_cstr);
     98   env->ReleaseStringUTFChars(pair_secret_jstr, pair_secret_cstr);
     99 }
    100 
    101 JNIEXPORT void JNICALL JNI_IMPLEMENTATION(disconnectNative)(JNIEnv* env,
    102                                                             jobject that) {
    103   remoting::ChromotingJniRuntime::GetInstance()->DisconnectFromHost();
    104 }
    105 
    106 JNIEXPORT void JNICALL JNI_IMPLEMENTATION(authenticationResponse)(
    107     JNIEnv* env,
    108     jobject that,
    109     jstring pin_jstr,
    110     jboolean create_pair) {
    111   const char* pin_cstr = env->GetStringUTFChars(pin_jstr, NULL);
    112 
    113   remoting::ChromotingJniRuntime::GetInstance()->
    114       session()->ProvideSecret(pin_cstr, create_pair);
    115 
    116   env->ReleaseStringUTFChars(pin_jstr, pin_cstr);
    117 }
    118 
    119 JNIEXPORT void JNICALL JNI_IMPLEMENTATION(scheduleRedrawNative)(
    120     JNIEnv* env,
    121     jobject that) {
    122   remoting::ChromotingJniRuntime::GetInstance()->session()->RedrawDesktop();
    123 }
    124 
    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) {
    132   // Button must be within the bounds of the MouseEvent_MouseButton enum.
    133   DCHECK(which_button >= 0 && which_button < 5);
    134 
    135   remoting::ChromotingJniRuntime::GetInstance()->session()->PerformMouseAction(
    136       x,
    137       y,
    138       static_cast<remoting::protocol::MouseEvent_MouseButton>(which_button),
    139       button_down);
    140 }
    141 
    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 
    151 }  // extern "C"
    152