blob: 518055831de7ff2df1785dbf6057a1c3af4b77d4 [file] [log] [blame]
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001// 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 Murdochbbcdd452013-07-25 10:06:34 +01008// 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 Murdoch7dbb3d52013-07-17 14:55:54 +010010// *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 Murdoch7dbb3d52013-07-17 14:55:54 +010018#include "remoting/client/jni/chromoting_jni_instance.h"
Ben Murdochbbcdd452013-07-25 10:06:34 +010019#include "remoting/client/jni/chromoting_jni_runtime.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010020
21// Class and package name of the Java class that declares this file's functions.
22#define JNI_IMPLEMENTATION(method) \
Ben Murdoch9ab55632013-07-18 11:57:30 +010023 Java_org_chromium_chromoting_jni_JniInterface_##method
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010024
25extern "C" {
26
27JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
28 base::android::InitVM(vm);
29 return JNI_VERSION_1_2;
30}
31
32JNIEXPORT 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 Murdochbbcdd452013-07-25 10:06:34 +010045 remoting::ChromotingJniRuntime::GetInstance();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010046}
47
48JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getApiKey)(JNIEnv* env,
49 jobject that) {
50 return env->NewStringUTF(google_apis::GetAPIKey().c_str());
51}
52
53JNIEXPORT 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
59JNIEXPORT 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
65JNIEXPORT 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 Murdochbb1529c2013-08-08 10:24:53 +010072 jstring host_pubkey_jstr,
73 jstring pair_id_jstr,
74 jstring pair_secret_jstr) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010075 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 Murdochbb1529c2013-08-08 10:24:53 +010080 const char* pair_id_cstr = env->GetStringUTFChars(pair_id_jstr, NULL);
81 const char* pair_secret_cstr = env->GetStringUTFChars(pair_secret_jstr, NULL);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010082
Ben Murdochbbcdd452013-07-25 10:06:34 +010083 remoting::ChromotingJniRuntime::GetInstance()->ConnectToHost(
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010084 username_cstr,
85 auth_token_cstr,
86 host_jid_cstr,
87 host_id_cstr,
Ben Murdochbb1529c2013-08-08 10:24:53 +010088 host_pubkey_cstr,
89 pair_id_cstr,
90 pair_secret_cstr);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010091
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 Murdochbb1529c2013-08-08 10:24:53 +010097 env->ReleaseStringUTFChars(pair_id_jstr, pair_id_cstr);
98 env->ReleaseStringUTFChars(pair_secret_jstr, pair_secret_cstr);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010099}
100
101JNIEXPORT void JNICALL JNI_IMPLEMENTATION(disconnectNative)(JNIEnv* env,
102 jobject that) {
Ben Murdochbbcdd452013-07-25 10:06:34 +0100103 remoting::ChromotingJniRuntime::GetInstance()->DisconnectFromHost();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100104}
105
106JNIEXPORT void JNICALL JNI_IMPLEMENTATION(authenticationResponse)(
107 JNIEnv* env,
108 jobject that,
Ben Murdochbb1529c2013-08-08 10:24:53 +0100109 jstring pin_jstr,
110 jboolean create_pair) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100111 const char* pin_cstr = env->GetStringUTFChars(pin_jstr, NULL);
112
Ben Murdochbbcdd452013-07-25 10:06:34 +0100113 remoting::ChromotingJniRuntime::GetInstance()->
Ben Murdochbb1529c2013-08-08 10:24:53 +0100114 session()->ProvideSecret(pin_cstr, create_pair);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100115
116 env->ReleaseStringUTFChars(pin_jstr, pin_cstr);
117}
118
Ben Murdoch9ab55632013-07-18 11:57:30 +0100119JNIEXPORT void JNICALL JNI_IMPLEMENTATION(scheduleRedrawNative)(
120 JNIEnv* env,
121 jobject that) {
Ben Murdochbbcdd452013-07-25 10:06:34 +0100122 remoting::ChromotingJniRuntime::GetInstance()->session()->RedrawDesktop();
Ben Murdoch9ab55632013-07-18 11:57:30 +0100123}
124
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100125JNIEXPORT 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)a36e5922013-08-05 13:57:33 +0100132 // Button must be within the bounds of the MouseEvent_MouseButton enum.
133 DCHECK(which_button >= 0 && which_button < 5);
134
Ben Murdochbbcdd452013-07-25 10:06:34 +0100135 remoting::ChromotingJniRuntime::GetInstance()->session()->PerformMouseAction(
Ben Murdocha3f7b4e2013-07-24 10:36:34 +0100136 x,
137 y,
138 static_cast<remoting::protocol::MouseEvent_MouseButton>(which_button),
139 button_down);
140}
141
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100142JNIEXPORT 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 Murdoch7dbb3d52013-07-17 14:55:54 +0100151} // extern "C"