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