diff options
author | lambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-29 17:56:56 +0000 |
---|---|---|
committer | lambroslambrou@chromium.org <lambroslambrou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-29 17:56:56 +0000 |
commit | 6856e2d374b954e0232078d4e30e3d6f38f63567 (patch) | |
tree | 4fa8b7f91c8dfbd941a3496aa19e98d1e272aae6 /remoting/client/jni | |
parent | 03f2c5e870a7e6ae30af86445ac5d421da4d434a (diff) | |
download | chromium_src-6856e2d374b954e0232078d4e30e3d6f38f63567.zip chromium_src-6856e2d374b954e0232078d4e30e3d6f38f63567.tar.gz chromium_src-6856e2d374b954e0232078d4e30e3d6f38f63567.tar.bz2 |
Use jni_generator in Chromoting Android client
This removes all manually-written JNI interface code and uses the
automatically generated JNI stubs instead.
BUG=304225
R=digit@chromium.org, solb@chromium.org
Review URL: https://codereview.chromium.org/29583003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231600 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/client/jni')
-rw-r--r-- | remoting/client/jni/DEPS | 1 | ||||
-rw-r--r-- | remoting/client/jni/chromoting_jni_onload.cc | 32 | ||||
-rw-r--r-- | remoting/client/jni/chromoting_jni_runtime.cc | 186 | ||||
-rw-r--r-- | remoting/client/jni/chromoting_jni_runtime.h | 5 | ||||
-rw-r--r-- | remoting/client/jni/jni_interface.cc | 151 |
5 files changed, 152 insertions, 223 deletions
diff --git a/remoting/client/jni/DEPS b/remoting/client/jni/DEPS index 26dcd15..85fd773 100644 --- a/remoting/client/jni/DEPS +++ b/remoting/client/jni/DEPS @@ -1,3 +1,4 @@ include_rules = [ + "+jni", "+ui/gfx/android", ] diff --git a/remoting/client/jni/chromoting_jni_onload.cc b/remoting/client/jni/chromoting_jni_onload.cc new file mode 100644 index 0000000..e82b757 --- /dev/null +++ b/remoting/client/jni/chromoting_jni_onload.cc @@ -0,0 +1,32 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/android/base_jni_registrar.h" +#include "base/android/jni_android.h" +#include "base/android/jni_registrar.h" +#include "net/android/net_jni_registrar.h" +#include "remoting/client/jni/chromoting_jni_runtime.h" +#include "ui/gfx/android/gfx_jni_registrar.h" + +extern "C" { + +JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { + base::android::InitVM(vm); + + JNIEnv* env = base::android::AttachCurrentThread(); + static base::android::RegistrationMethod kRemotingRegisteredMethods[] = { + {"base", base::android::RegisterJni}, + {"gfx", gfx::android::RegisterJni}, + {"net", net::android::RegisterJni}, + {"remoting", remoting::RegisterJni}, + }; + if (!base::android::RegisterNativeMethods( + env, kRemotingRegisteredMethods, arraysize(kRemotingRegisteredMethods))) { + return -1; + } + + return JNI_VERSION_1_4; +} + +} // extern "C" diff --git a/remoting/client/jni/chromoting_jni_runtime.cc b/remoting/client/jni/chromoting_jni_runtime.cc index 11ca730..6b531fa 100644 --- a/remoting/client/jni/chromoting_jni_runtime.cc +++ b/remoting/client/jni/chromoting_jni_runtime.cc @@ -4,21 +4,23 @@ #include "remoting/client/jni/chromoting_jni_runtime.h" -#include "base/android/base_jni_registrar.h" #include "base/android/jni_android.h" +#include "base/android/jni_string.h" #include "base/android/scoped_java_ref.h" +#include "base/basictypes.h" +#include "base/command_line.h" #include "base/memory/singleton.h" #include "base/stl_util.h" #include "base/synchronization/waitable_event.h" +#include "google_apis/google_api_keys.h" +#include "jni/JniInterface_jni.h" #include "media/base/yuv_convert.h" -#include "net/android/net_jni_registrar.h" #include "remoting/base/url_request_context.h" #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" -namespace { +using base::android::ConvertJavaStringToUTF8; -// Class and package name of the Java class supporting the methods we call. -const char* const kJavaClass = "org/chromium/chromoting/jni/JniInterface"; +namespace { const int kBytesPerPixel = 4; @@ -26,22 +28,110 @@ const int kBytesPerPixel = 4; namespace remoting { +bool RegisterJni(JNIEnv* env) { + return remoting::RegisterNativesImpl(env); +} + +// Implementation of stubs defined in JniInterface_jni.h. These are the entry +// points for JNI calls from Java into C++. + +static void LoadNative(JNIEnv* env, jclass clazz, jobject context) { + base::android::ScopedJavaLocalRef<jobject> context_activity(env, context); + base::android::InitApplicationContext(context_activity); + + // The google_apis functions check the command-line arguments to make sure no + // runtime API keys have been specified by the environment. Unfortunately, we + // neither launch Chromium nor have a command line, so we need to prevent + // them from DCHECKing out when they go looking. + CommandLine::Init(0, NULL); + + // Create the singleton now so that the Chromoting threads will be set up. + remoting::ChromotingJniRuntime::GetInstance(); +} + +static jstring GetApiKey(JNIEnv* env, jclass clazz) { + return env->NewStringUTF(google_apis::GetAPIKey().c_str()); +} + +static jstring GetClientId(JNIEnv* env, jclass clazz) { + return env->NewStringUTF( + google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING).c_str()); +} + +static jstring GetClientSecret(JNIEnv* env, jclass clazz) { + return env->NewStringUTF( + google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING).c_str()); +} + +static void Connect(JNIEnv* env, + jclass clazz, + jstring username, + jstring authToken, + jstring hostJid, + jstring hostId, + jstring hostPubkey, + jstring pairId, + jstring pairSecret) { + remoting::ChromotingJniRuntime::GetInstance()->ConnectToHost( + ConvertJavaStringToUTF8(env, username).c_str(), + ConvertJavaStringToUTF8(env, authToken).c_str(), + ConvertJavaStringToUTF8(env, hostJid).c_str(), + ConvertJavaStringToUTF8(env, hostId).c_str(), + ConvertJavaStringToUTF8(env, hostPubkey).c_str(), + ConvertJavaStringToUTF8(env, pairId).c_str(), + ConvertJavaStringToUTF8(env, pairSecret).c_str()); +} + +static void Disconnect(JNIEnv* env, jclass clazz) { + remoting::ChromotingJniRuntime::GetInstance()->DisconnectFromHost(); +} + +static void AuthenticationResponse(JNIEnv* env, + jclass clazz, + jstring pin, + jboolean createPair) { + remoting::ChromotingJniRuntime::GetInstance()->session()->ProvideSecret( + ConvertJavaStringToUTF8(env, pin).c_str(), createPair); +} + +static void ScheduleRedraw(JNIEnv* env, jclass clazz) { + remoting::ChromotingJniRuntime::GetInstance()->session()->RedrawDesktop(); +} + +static void MouseAction(JNIEnv* env, + jclass clazz, + jint x, + jint y, + jint whichButton, + jboolean buttonDown) { + // Button must be within the bounds of the MouseEvent_MouseButton enum. + DCHECK(whichButton >= 0 && whichButton < 5); + + remoting::ChromotingJniRuntime::GetInstance()->session()->PerformMouseAction( + x, + y, + static_cast<remoting::protocol::MouseEvent_MouseButton>(whichButton), + buttonDown); +} + +static void KeyboardAction(JNIEnv* env, + jclass clazz, + jint keyCode, + jboolean keyDown) { + remoting::ChromotingJniRuntime::GetInstance() + ->session() + ->PerformKeyboardAction(keyCode, keyDown); +} + +// ChromotingJniRuntime implementation. + // static ChromotingJniRuntime* ChromotingJniRuntime::GetInstance() { return Singleton<ChromotingJniRuntime>::get(); } ChromotingJniRuntime::ChromotingJniRuntime() { - // Obtain a reference to the Java environment. (Future calls to this function - // made from the same thread return the same stored reference instead of - // repeating the work of attaching to the JVM.) - JNIEnv* env = base::android::AttachCurrentThread(); - - // The base and networks stacks must be registered with JNI in order to work - // on Android. An AtExitManager cleans this up at process exit. at_exit_manager_.reset(new base::AtExitManager()); - base::android::RegisterJni(env); - net::android::RegisterJni(env); // On Android, the UI thread is managed by Java, so we need to attach and // start a special type of message loop to allow Chromium code to run tasks. @@ -63,8 +153,6 @@ ChromotingJniRuntime::ChromotingJniRuntime() { // Allows later decoding of video frames. media::InitializeCPUSpecificYUVConversions(); - - class_ = static_cast<jclass>(env->NewGlobalRef(env->FindClass(kJavaClass))); } ChromotingJniRuntime::~ChromotingJniRuntime() { @@ -75,9 +163,6 @@ ChromotingJniRuntime::~ChromotingJniRuntime() { // components' still being alive. DisconnectFromHost(); - JNIEnv* env = base::android::AttachCurrentThread(); - env->DeleteGlobalRef(class_); - base::WaitableEvent done_event(false, false); network_task_runner_->PostTask(FROM_HERE, base::Bind( &ChromotingJniRuntime::DetachFromVmAndSignal, @@ -125,21 +210,14 @@ void ChromotingJniRuntime::ReportConnectionStatus( DCHECK(ui_task_runner_->BelongsToCurrentThread()); JNIEnv* env = base::android::AttachCurrentThread(); - env->CallStaticVoidMethod( - class_, - env->GetStaticMethodID(class_, "reportConnectionStatus", "(II)V"), - state, - error); + Java_JniInterface_reportConnectionStatus(env, state, error); } void ChromotingJniRuntime::DisplayAuthenticationPrompt(bool pairing_supported) { DCHECK(ui_task_runner_->BelongsToCurrentThread()); JNIEnv* env = base::android::AttachCurrentThread(); - env->CallStaticVoidMethod( - class_, - env->GetStaticMethodID(class_, "displayAuthenticationPrompt", "(Z)V"), - pairing_supported); + Java_JniInterface_displayAuthenticationPrompt(env, pairing_supported); } void ChromotingJniRuntime::CommitPairingCredentials(const std::string& host, @@ -156,15 +234,8 @@ void ChromotingJniRuntime::CommitPairingCredentials(const std::string& host, env->SetByteArrayRegion(secret_arr, 0, secret.size(), reinterpret_cast<const jbyte*>(secret.c_str())); - env->CallStaticVoidMethod( - class_, - env->GetStaticMethodID( - class_, - "commitPairingCredentials", - "(Ljava/lang/String;[B[B)V"), - host_jstr, - id_arr, - secret_arr); + Java_JniInterface_commitPairingCredentials(env, host_jstr, id_arr, + secret_arr); // Because we passed them as arguments, their corresponding Java objects were // GCd as soon as the managed method returned, so we mustn't release it here. @@ -173,30 +244,14 @@ void ChromotingJniRuntime::CommitPairingCredentials(const std::string& host, base::android::ScopedJavaLocalRef<jobject> ChromotingJniRuntime::NewBitmap( webrtc::DesktopSize size) { JNIEnv* env = base::android::AttachCurrentThread(); - - jobject bitmap = env->CallStaticObjectMethod( - class_, - env->GetStaticMethodID( - class_, - "newBitmap", - "(II)Landroid/graphics/Bitmap;"), - size.width(), - size.height()); - return base::android::ScopedJavaLocalRef<jobject>(env, bitmap); + return Java_JniInterface_newBitmap(env, size.width(), size.height()); } void ChromotingJniRuntime::UpdateFrameBitmap(jobject bitmap) { DCHECK(display_task_runner_->BelongsToCurrentThread()); JNIEnv* env = base::android::AttachCurrentThread(); - - env->CallStaticVoidMethod( - class_, - env->GetStaticMethodID( - class_, - "setVideoFrame", - "(Landroid/graphics/Bitmap;)V"), - bitmap); + Java_JniInterface_setVideoFrame(env, bitmap); } void ChromotingJniRuntime::UpdateCursorShape( @@ -214,26 +269,19 @@ void ChromotingJniRuntime::UpdateCursorShape( JNIEnv* env = base::android::AttachCurrentThread(); base::android::ScopedJavaLocalRef<jobject> buffer(env, env->NewDirectByteBuffer(data, cursor_total_bytes)); - env->CallStaticVoidMethod( - class_, - env->GetStaticMethodID( - class_, - "updateCursorShape", - "(IIIILjava/nio/ByteBuffer;)V"), - cursor_shape.width(), - cursor_shape.height(), - cursor_shape.hotspot_x(), - cursor_shape.hotspot_y(), - buffer.obj()); + Java_JniInterface_updateCursorShape(env, + cursor_shape.width(), + cursor_shape.height(), + cursor_shape.hotspot_x(), + cursor_shape.hotspot_y(), + buffer.obj()); } void ChromotingJniRuntime::RedrawCanvas() { DCHECK(display_task_runner_->BelongsToCurrentThread()); JNIEnv* env = base::android::AttachCurrentThread(); - env->CallStaticVoidMethod( - class_, - env->GetStaticMethodID(class_, "redrawGraphicsInternal", "()V")); + Java_JniInterface_redrawGraphicsInternal(env); } void ChromotingJniRuntime::DetachFromVmAndSignal(base::WaitableEvent* waiter) { diff --git a/remoting/client/jni/chromoting_jni_runtime.h b/remoting/client/jni/chromoting_jni_runtime.h index 93d77bd..bb356a1 100644 --- a/remoting/client/jni/chromoting_jni_runtime.h +++ b/remoting/client/jni/chromoting_jni_runtime.h @@ -19,6 +19,8 @@ template<typename T> struct DefaultSingletonTraits; namespace remoting { +bool RegisterJni(JNIEnv* env); + // Houses the global resources on which the Chromoting components run // (e.g. message loops and task runners). Proxies outgoing JNI calls from its // ChromotingJniInstance member to Java. All its methods should be invoked @@ -107,9 +109,6 @@ class ChromotingJniRuntime { // Detaches JVM from the current thread, then signals. Doesn't own |waiter|. void DetachFromVmAndSignal(base::WaitableEvent* waiter); - // Reference to the Java class into which we make JNI calls. - jclass class_; - // Used by the Chromium libraries to clean up the base and net libraries' JNI // bindings. It must persist for the lifetime of the singleton. scoped_ptr<base::AtExitManager> at_exit_manager_; diff --git a/remoting/client/jni/jni_interface.cc b/remoting/client/jni/jni_interface.cc deleted file mode 100644 index 5180558..0000000 --- a/remoting/client/jni/jni_interface.cc +++ /dev/null @@ -1,151 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// This file defines functions that implement the static methods declared in a -// closely-related Java class in the platform-specific user interface -// implementation. In effect, it is the entry point for all JNI calls *into* -// the C++ codebase from Java. The separate ChromotingJniRuntime class serves -// as the corresponding exit point, and is responsible for making all JNI calls -// *out of* the C++ codebase into Java. - -#include <jni.h> - -#include "base/android/jni_android.h" -#include "base/command_line.h" -#include "base/memory/ref_counted.h" -#include "google_apis/google_api_keys.h" -#include "remoting/client/jni/chromoting_jni_instance.h" -#include "remoting/client/jni/chromoting_jni_runtime.h" - -// Class and package name of the Java class that declares this file's functions. -#define JNI_IMPLEMENTATION(method) \ - Java_org_chromium_chromoting_jni_JniInterface_##method - -extern "C" { - -JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { - base::android::InitVM(vm); - return JNI_VERSION_1_2; -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(loadNative)(JNIEnv* env, - jobject that, - jobject context) { - base::android::ScopedJavaLocalRef<jobject> context_activity(env, context); - base::android::InitApplicationContext(context_activity); - - // The google_apis functions check the command-line arguments to make sure no - // runtime API keys have been specified by the environment. Unfortunately, we - // neither launch Chromium nor have a command line, so we need to prevent - // them from DCHECKing out when they go looking. - CommandLine::Init(0, NULL); - - // Create the singleton now so that the Chromoting threads will be set up. - remoting::ChromotingJniRuntime::GetInstance(); -} - -JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getApiKey)(JNIEnv* env, - jobject that) { - return env->NewStringUTF(google_apis::GetAPIKey().c_str()); -} - -JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getClientId)(JNIEnv* env, - jobject that) { - return env->NewStringUTF( - google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING).c_str()); -} - -JNIEXPORT jstring JNICALL JNI_IMPLEMENTATION(getClientSecret)(JNIEnv* env, - jobject that) { - return env->NewStringUTF( - google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_REMOTING).c_str()); -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(connectNative)( - JNIEnv* env, - jobject that, - jstring username_jstr, - jstring auth_token_jstr, - jstring host_jid_jstr, - jstring host_id_jstr, - jstring host_pubkey_jstr, - jstring pair_id_jstr, - jstring pair_secret_jstr) { - const char* username_cstr = env->GetStringUTFChars(username_jstr, NULL); - const char* auth_token_cstr = env->GetStringUTFChars(auth_token_jstr, NULL); - const char* host_jid_cstr = env->GetStringUTFChars(host_jid_jstr, NULL); - const char* host_id_cstr = env->GetStringUTFChars(host_id_jstr, NULL); - const char* host_pubkey_cstr = env->GetStringUTFChars(host_pubkey_jstr, NULL); - const char* pair_id_cstr = env->GetStringUTFChars(pair_id_jstr, NULL); - const char* pair_secret_cstr = env->GetStringUTFChars(pair_secret_jstr, NULL); - - remoting::ChromotingJniRuntime::GetInstance()->ConnectToHost( - username_cstr, - auth_token_cstr, - host_jid_cstr, - host_id_cstr, - host_pubkey_cstr, - pair_id_cstr, - pair_secret_cstr); - - env->ReleaseStringUTFChars(username_jstr, username_cstr); - env->ReleaseStringUTFChars(auth_token_jstr, auth_token_cstr); - env->ReleaseStringUTFChars(host_jid_jstr, host_jid_cstr); - env->ReleaseStringUTFChars(host_id_jstr, host_id_cstr); - env->ReleaseStringUTFChars(host_pubkey_jstr, host_pubkey_cstr); - env->ReleaseStringUTFChars(pair_id_jstr, pair_id_cstr); - env->ReleaseStringUTFChars(pair_secret_jstr, pair_secret_cstr); -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(disconnectNative)(JNIEnv* env, - jobject that) { - remoting::ChromotingJniRuntime::GetInstance()->DisconnectFromHost(); -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(authenticationResponse)( - JNIEnv* env, - jobject that, - jstring pin_jstr, - jboolean create_pair) { - const char* pin_cstr = env->GetStringUTFChars(pin_jstr, NULL); - - remoting::ChromotingJniRuntime::GetInstance()-> - session()->ProvideSecret(pin_cstr, create_pair); - - env->ReleaseStringUTFChars(pin_jstr, pin_cstr); -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(scheduleRedrawNative)( - JNIEnv* env, - jobject that) { - remoting::ChromotingJniRuntime::GetInstance()->session()->RedrawDesktop(); -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(mouseActionNative)( - JNIEnv* env, - jobject that, - jint x, - jint y, - jint which_button, - jboolean button_down) { - // Button must be within the bounds of the MouseEvent_MouseButton enum. - DCHECK(which_button >= 0 && which_button < 5); - - remoting::ChromotingJniRuntime::GetInstance()->session()->PerformMouseAction( - x, - y, - static_cast<remoting::protocol::MouseEvent_MouseButton>(which_button), - button_down); -} - -JNIEXPORT void JNICALL JNI_IMPLEMENTATION(keyboardActionNative)( - JNIEnv* env, - jobject that, - jint key_code, - jboolean key_down) { - remoting::ChromotingJniRuntime::GetInstance()->session()-> - PerformKeyboardAction(key_code, key_down); -} - -} // extern "C" |