OLD | NEW |
(Empty) | |
| 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 #include "remoting/client/jni/chromoting_jni.h" |
| 6 |
| 7 #include "base/android/base_jni_registrar.h" |
| 8 #include "base/android/jni_android.h" |
| 9 #include "base/memory/singleton.h" |
| 10 #include "net/android/net_jni_registrar.h" |
| 11 #include "remoting/base/url_request_context.h" |
| 12 #include "remoting/client/jni/chromoting_jni_instance.h" |
| 13 |
| 14 namespace { |
| 15 // Class and package name of the Java class supporting the methods we call. |
| 16 const char* const JAVA_CLASS = "org/chromium/chromoting/jni/JNIInterface"; |
| 17 } // namespace |
| 18 |
| 19 namespace remoting { |
| 20 |
| 21 // static |
| 22 ChromotingJni* ChromotingJni::GetInstance() { |
| 23 return Singleton<ChromotingJni>::get(); |
| 24 } |
| 25 |
| 26 ChromotingJni::ChromotingJni() { |
| 27 // Obtain a reference to the Java environment. (Future calls to this function |
| 28 // made from the same thread return the same stored reference instead of |
| 29 // repeating the work of attaching to the JVM.) |
| 30 JNIEnv* env = base::android::AttachCurrentThread(); |
| 31 |
| 32 // The base and networks stacks must be registered with JNI in order to work |
| 33 // on Android. An AtExitManager cleans this up at process exit. |
| 34 at_exit_manager_.reset(new base::AtExitManager()); |
| 35 base::android::RegisterJni(env); |
| 36 net::android::RegisterJni(env); |
| 37 |
| 38 // On Android, the UI thread is managed by Java, so we need to attach and |
| 39 // start a special type of message loop to allow Chromium code to run tasks. |
| 40 LOG(INFO) << "Starting main message loop"; |
| 41 ui_loop_.reset(new base::MessageLoopForUI()); |
| 42 ui_loop_->Start(); |
| 43 |
| 44 LOG(INFO) << "Spawning additional threads"; |
| 45 // TODO(solb) Stop pretending to control the managed UI thread's lifetime. |
| 46 ui_task_runner_ = new AutoThreadTaskRunner(ui_loop_->message_loop_proxy(), |
| 47 base::MessageLoop::QuitClosure()); |
| 48 network_task_runner_ = AutoThread::CreateWithType("native_net", |
| 49 ui_task_runner_, |
| 50 base::MessageLoop::TYPE_IO); |
| 51 display_task_runner_ = AutoThread::Create("native_disp", |
| 52 ui_task_runner_); |
| 53 |
| 54 url_requester_ = new URLRequestContextGetter(ui_task_runner_, |
| 55 network_task_runner_); |
| 56 |
| 57 class_ = static_cast<jclass>(env->NewGlobalRef(env->FindClass(JAVA_CLASS))); |
| 58 } |
| 59 |
| 60 ChromotingJni::~ChromotingJni() { |
| 61 // The singleton should only ever be destroyed on the main thread. |
| 62 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 63 |
| 64 // The session must be shut down first, since it depends on our other |
| 65 // components' still being alive. |
| 66 DisconnectFromHost(); |
| 67 |
| 68 JNIEnv* env = base::android::AttachCurrentThread(); |
| 69 env->DeleteGlobalRef(class_); |
| 70 // TODO(solb): crbug.com/259594 Detach all threads from JVM here. |
| 71 } |
| 72 |
| 73 void ChromotingJni::ConnectToHost(const char* username, |
| 74 const char* auth_token, |
| 75 const char* host_jid, |
| 76 const char* host_id, |
| 77 const char* host_pubkey) { |
| 78 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 79 DCHECK(!session_); |
| 80 session_ = new ChromotingJniInstance(username, |
| 81 auth_token, |
| 82 host_jid, |
| 83 host_id, |
| 84 host_pubkey); |
| 85 } |
| 86 |
| 87 void ChromotingJni::DisconnectFromHost() { |
| 88 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 89 if (session_) { |
| 90 session_->Cleanup(); |
| 91 session_ = NULL; |
| 92 } |
| 93 } |
| 94 |
| 95 void ChromotingJni::ReportConnectionStatus( |
| 96 protocol::ConnectionToHost::State state, |
| 97 protocol::ErrorCode error) { |
| 98 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 99 |
| 100 JNIEnv* env = base::android::AttachCurrentThread(); |
| 101 env->CallStaticVoidMethod( |
| 102 class_, |
| 103 env->GetStaticMethodID(class_, "reportConnectionStatus", "(II)V"), |
| 104 state, |
| 105 error); |
| 106 } |
| 107 |
| 108 void ChromotingJni::DisplayAuthenticationPrompt() { |
| 109 DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
| 110 |
| 111 JNIEnv* env = base::android::AttachCurrentThread(); |
| 112 env->CallStaticVoidMethod( |
| 113 class_, |
| 114 env->GetStaticMethodID(class_, "displayAuthenticationPrompt", "()V")); |
| 115 } |
| 116 |
| 117 } // namespace remoting |
OLD | NEW |