OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/android/jni_host.h" | 5 #include "remoting/host/android/jni_host.h" |
6 | 6 |
| 7 #include "base/android/jni_string.h" |
| 8 #include "base/command_line.h" |
| 9 #include "base/strings/string_util.h" |
7 #include "jni/Host_jni.h" | 10 #include "jni/Host_jni.h" |
| 11 #include "net/base/url_util.h" |
| 12 #include "net/socket/ssl_server_socket.h" |
| 13 #include "remoting/base/auto_thread_task_runner.h" |
| 14 #include "remoting/base/logging.h" |
| 15 #include "remoting/host/chromoting_host_context.h" |
| 16 #include "remoting/host/it2me/it2me_host.h" |
| 17 #include "remoting/host/service_urls.h" |
| 18 #include "remoting/signaling/xmpp_signal_strategy.h" |
| 19 |
| 20 using base::android::ConvertJavaStringToUTF8; |
8 | 21 |
9 namespace remoting { | 22 namespace remoting { |
10 | 23 |
11 JniHost::JniHost() {} | 24 JniHost::JniHost() : weak_factory_(this) { |
| 25 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 26 |
| 27 base::CommandLine::Init(0, nullptr); |
| 28 |
| 29 // Enable support for SSL server sockets, which must be done while still |
| 30 // single-threaded. |
| 31 net::EnableSSLServerSockets(); |
| 32 |
| 33 if (!base::MessageLoop::current()) { |
| 34 HOST_LOG << "Starting main message loop"; |
| 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 ui_loop_.reset(new base::MessageLoopForUI()); |
| 39 ui_loop_->Start(); |
| 40 } else { |
| 41 HOST_LOG << "Using existing main message loop"; |
| 42 ui_loop_.reset(base::MessageLoopForUI::current()); |
| 43 } |
| 44 |
| 45 factory_.reset(new It2MeHostFactory()); |
| 46 host_context_ = |
| 47 ChromotingHostContext::Create(new remoting::AutoThreadTaskRunner( |
| 48 ui_loop_->task_runner(), base::Bind(&base::DoNothing))); |
| 49 |
| 50 const ServiceUrls* service_urls = ServiceUrls::GetInstance(); |
| 51 const bool xmpp_server_valid = net::ParseHostAndPort( |
| 52 service_urls->xmpp_server_address(), &xmpp_server_config_.host, |
| 53 &xmpp_server_config_.port); |
| 54 DCHECK(xmpp_server_valid); |
| 55 |
| 56 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); |
| 57 } |
12 | 58 |
13 JniHost::~JniHost() {} | 59 JniHost::~JniHost() {} |
14 | 60 |
15 // static | 61 // static |
16 bool JniHost::RegisterJni(JNIEnv* env) { | 62 bool JniHost::RegisterJni(JNIEnv* env) { |
17 return RegisterNativesImpl(env); | 63 return RegisterNativesImpl(env); |
18 } | 64 } |
19 | 65 |
20 void JniHost::Destroy(JNIEnv* env, const JavaParamRef<jobject>& caller) { | 66 void JniHost::Destroy(JNIEnv* env, const JavaParamRef<jobject>& caller) { |
21 delete this; | 67 delete this; |
22 } | 68 } |
23 | 69 |
| 70 void JniHost::Connect(JNIEnv* env, |
| 71 const base::android::JavaParamRef<jobject>& caller, |
| 72 const JavaParamRef<jstring>& user_name, |
| 73 const JavaParamRef<jstring>& auth_token) { |
| 74 if (it2me_host_) { |
| 75 LOG(ERROR) << "Connect: already connected."; |
| 76 return; |
| 77 } |
| 78 |
| 79 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; |
| 80 xmpp_config.username = ConvertJavaStringToUTF8(user_name); |
| 81 xmpp_config.auth_token = ConvertJavaStringToUTF8(auth_token); |
| 82 |
| 83 std::string directory_bot_jid = |
| 84 ServiceUrls::GetInstance()->directory_bot_jid(); |
| 85 |
| 86 // Create the It2Me host and start connecting. |
| 87 it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(), weak_ptr_, |
| 88 xmpp_config, directory_bot_jid); |
| 89 it2me_host_->Connect(); |
| 90 } |
| 91 |
| 92 void JniHost::Disconnect(JNIEnv* env, |
| 93 const base::android::JavaParamRef<jobject>& caller) {} |
| 94 |
| 95 void JniHost::OnClientAuthenticated(const std::string& client_username) { |
| 96 HOST_LOG << "OnClientAuthenticated: " << client_username; |
| 97 } |
| 98 |
| 99 void JniHost::OnStoreAccessCode(const std::string& access_code, |
| 100 base::TimeDelta access_code_lifetime) { |
| 101 HOST_LOG << "OnStoreAccessCode: " << access_code << ", " |
| 102 << access_code_lifetime.InSeconds(); |
| 103 } |
| 104 |
| 105 void JniHost::OnNatPolicyChanged(bool nat_traversal_enabled) { |
| 106 HOST_LOG << "OnNatPolicyChanged: " << nat_traversal_enabled; |
| 107 } |
| 108 |
| 109 void JniHost::OnStateChanged(It2MeHostState state, |
| 110 const std::string& error_message) { |
| 111 HOST_LOG << "OnStateChanged: " << state; |
| 112 } |
| 113 |
24 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& caller) { | 114 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& caller) { |
25 return reinterpret_cast<intptr_t>(new JniHost()); | 115 return reinterpret_cast<intptr_t>(new JniHost()); |
26 } | 116 } |
27 | 117 |
28 } // namespace remoting | 118 } // namespace remoting |
OLD | NEW |