| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/host/android/jni_host.h" | |
| 6 | |
| 7 #include "base/android/jni_string.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/strings/string_util.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; | |
| 21 using base::android::ConvertUTF8ToJavaString; | |
| 22 using base::android::JavaParamRef; | |
| 23 | |
| 24 namespace remoting { | |
| 25 | |
| 26 JniHost::JniHost(JNIEnv* env, jobject java_host) : weak_factory_(this) { | |
| 27 java_host_.Reset(env, java_host); | |
| 28 weak_ptr_ = weak_factory_.GetWeakPtr(); | |
| 29 | |
| 30 base::CommandLine::Init(0, nullptr); | |
| 31 | |
| 32 // Enable support for SSL server sockets, which must be done while still | |
| 33 // single-threaded. | |
| 34 net::EnableSSLServerSockets(); | |
| 35 | |
| 36 if (!base::MessageLoop::current()) { | |
| 37 HOST_LOG << "Starting main message loop"; | |
| 38 | |
| 39 // On Android, the UI thread is managed by Java, so we need to attach and | |
| 40 // start a special type of message loop to allow Chromium code to run tasks. | |
| 41 ui_loop_.reset(new base::MessageLoopForUI()); | |
| 42 ui_loop_->Start(); | |
| 43 } else { | |
| 44 HOST_LOG << "Using existing main message loop"; | |
| 45 ui_loop_.reset(base::MessageLoopForUI::current()); | |
| 46 } | |
| 47 | |
| 48 factory_.reset(new It2MeHostFactory()); | |
| 49 host_context_ = | |
| 50 ChromotingHostContext::Create(new remoting::AutoThreadTaskRunner( | |
| 51 ui_loop_->task_runner(), base::Bind(&base::DoNothing))); | |
| 52 | |
| 53 const ServiceUrls* service_urls = ServiceUrls::GetInstance(); | |
| 54 const bool xmpp_server_valid = net::ParseHostAndPort( | |
| 55 service_urls->xmpp_server_address(), &xmpp_server_config_.host, | |
| 56 &xmpp_server_config_.port); | |
| 57 DCHECK(xmpp_server_valid); | |
| 58 | |
| 59 xmpp_server_config_.use_tls = service_urls->xmpp_server_use_tls(); | |
| 60 } | |
| 61 | |
| 62 JniHost::~JniHost() {} | |
| 63 | |
| 64 // static | |
| 65 bool JniHost::RegisterJni(JNIEnv* env) { | |
| 66 return RegisterNativesImpl(env); | |
| 67 } | |
| 68 | |
| 69 void JniHost::Destroy(JNIEnv* env, const JavaParamRef<jobject>& caller) { | |
| 70 delete this; | |
| 71 } | |
| 72 | |
| 73 void JniHost::Connect(JNIEnv* env, | |
| 74 const base::android::JavaParamRef<jobject>& caller, | |
| 75 const JavaParamRef<jstring>& user_name, | |
| 76 const JavaParamRef<jstring>& auth_token) { | |
| 77 if (it2me_host_) { | |
| 78 LOG(ERROR) << "Connect: already connected."; | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 XmppSignalStrategy::XmppServerConfig xmpp_config = xmpp_server_config_; | |
| 83 xmpp_config.username = ConvertJavaStringToUTF8(user_name); | |
| 84 xmpp_config.auth_token = ConvertJavaStringToUTF8(auth_token); | |
| 85 | |
| 86 std::string directory_bot_jid = | |
| 87 ServiceUrls::GetInstance()->directory_bot_jid(); | |
| 88 | |
| 89 // Create the It2Me host and start connecting. | |
| 90 it2me_host_ = factory_->CreateIt2MeHost(host_context_->Copy(), | |
| 91 /*policy_service=*/nullptr, weak_ptr_, | |
| 92 xmpp_config, directory_bot_jid); | |
| 93 it2me_host_->Connect(); | |
| 94 } | |
| 95 | |
| 96 void JniHost::Disconnect(JNIEnv* env, | |
| 97 const base::android::JavaParamRef<jobject>& caller) { | |
| 98 if (it2me_host_) { | |
| 99 it2me_host_->Disconnect(); | |
| 100 it2me_host_ = nullptr; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void JniHost::OnClientAuthenticated(const std::string& client_username) { | |
| 105 HOST_LOG << "OnClientAuthenticated: " << client_username; | |
| 106 } | |
| 107 | |
| 108 void JniHost::OnStoreAccessCode(const std::string& access_code, | |
| 109 base::TimeDelta access_code_lifetime) { | |
| 110 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 111 Java_Host_onAccessCodeReceived( | |
| 112 env, java_host_, ConvertUTF8ToJavaString(env, access_code), | |
| 113 static_cast<int>(access_code_lifetime.InSeconds())); | |
| 114 } | |
| 115 | |
| 116 void JniHost::OnNatPolicyChanged(bool nat_traversal_enabled) { | |
| 117 HOST_LOG << "OnNatPolicyChanged: " << nat_traversal_enabled; | |
| 118 } | |
| 119 | |
| 120 void JniHost::OnStateChanged(It2MeHostState state, | |
| 121 const std::string& error_message) { | |
| 122 if (state == kDisconnected) { | |
| 123 it2me_host_ = nullptr; | |
| 124 } | |
| 125 | |
| 126 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 127 Java_Host_onStateChanged(env, java_host_, state, | |
| 128 ConvertUTF8ToJavaString(env, error_message)); | |
| 129 } | |
| 130 | |
| 131 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& caller) { | |
| 132 return reinterpret_cast<intptr_t>(new JniHost(env, caller)); | |
| 133 } | |
| 134 | |
| 135 } // namespace remoting | |
| OLD | NEW |