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(); | |
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. | |
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, | |
garykac
2013/07/15 21:13:42
align args
solb
2013/07/15 21:49:42
Done.
| |
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 JNIEnv* env = base::android::AttachCurrentThread(); | |
garykac
2013/07/15 21:13:42
DCHECK ui thread?
solb
2013/07/15 21:49:42
Done.
| |
91 env->CallStaticVoidMethod( | |
92 class_, | |
93 env->GetStaticMethodID(class_, "reportConnectionStatus", "(II)V"), | |
94 state, | |
95 error); | |
96 } | |
97 | |
98 void ChromotingJNI::DisplayAuthenticationPrompt() { | |
99 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
100 | |
101 JNIEnv* env = base::android::AttachCurrentThread(); | |
102 env->CallStaticVoidMethod( | |
103 class_, | |
104 env->GetStaticMethodID(class_, "displayAuthenticationPrompt", "()V")); | |
105 } | |
106 | |
107 } // namespace remoting | |
OLD | NEW |