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