| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/client/jni/chromoting_jni_instance.h" | 5 #include "remoting/client/jni/chromoting_jni_instance.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "remoting/client/audio_player.h" | 9 #include "remoting/client/audio_player.h" |
| 10 #include "remoting/client/jni/chromoting_jni.h" | 10 #include "remoting/client/jni/chromoting_jni_runtime.h" |
| 11 #include "remoting/protocol/libjingle_transport_factory.h" | 11 #include "remoting/protocol/libjingle_transport_factory.h" |
| 12 | 12 |
| 13 // TODO(solb) Move into location shared with client plugin. | 13 // TODO(solb) Move into location shared with client plugin. |
| 14 const char* const CHAT_SERVER = "talk.google.com"; | 14 const char* const CHAT_SERVER = "talk.google.com"; |
| 15 const int CHAT_PORT = 5222; | 15 const int CHAT_PORT = 5222; |
| 16 const bool CHAT_USE_TLS = true; | 16 const bool CHAT_USE_TLS = true; |
| 17 | 17 |
| 18 namespace remoting { | 18 namespace remoting { |
| 19 | 19 |
| 20 ChromotingJniInstance::ChromotingJniInstance(const char* username, | 20 ChromotingJniInstance::ChromotingJniInstance(ChromotingJniRuntime* jni_runtime, |
| 21 const char* username, |
| 21 const char* auth_token, | 22 const char* auth_token, |
| 22 const char* host_jid, | 23 const char* host_jid, |
| 23 const char* host_id, | 24 const char* host_id, |
| 24 const char* host_pubkey) { | 25 const char* host_pubkey) |
| 25 DCHECK(ChromotingJni::GetInstance()-> | 26 : jni_runtime_(jni_runtime), |
| 26 ui_task_runner()->BelongsToCurrentThread()); | 27 username_(username), |
| 28 auth_token_(auth_token), |
| 29 host_jid_(host_jid), |
| 30 host_id_(host_id), |
| 31 host_pubkey_(host_pubkey) { |
| 32 DCHECK(jni_runtime_->ui_task_runner()->BelongsToCurrentThread()); |
| 27 | 33 |
| 28 username_ = username; | 34 jni_runtime_->display_task_runner()->PostTask( |
| 29 auth_token_ = auth_token; | |
| 30 host_jid_ = host_jid; | |
| 31 host_id_ = host_id; | |
| 32 host_pubkey_ = host_pubkey; | |
| 33 | |
| 34 ChromotingJni::GetInstance()->display_task_runner()->PostTask( | |
| 35 FROM_HERE, | 35 FROM_HERE, |
| 36 base::Bind(&ChromotingJniInstance::ConnectToHostOnDisplayThread, | 36 base::Bind(&ChromotingJniInstance::ConnectToHostOnDisplayThread, |
| 37 this)); | 37 this)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 ChromotingJniInstance::~ChromotingJniInstance() {} | 40 ChromotingJniInstance::~ChromotingJniInstance() {} |
| 41 | 41 |
| 42 void ChromotingJniInstance::Cleanup() { | 42 void ChromotingJniInstance::Cleanup() { |
| 43 if (!ChromotingJni::GetInstance()-> | 43 if (!jni_runtime_->display_task_runner()->BelongsToCurrentThread()) { |
| 44 display_task_runner()->BelongsToCurrentThread()) { | 44 jni_runtime_->display_task_runner()->PostTask( |
| 45 ChromotingJni::GetInstance()->display_task_runner()->PostTask( | |
| 46 FROM_HERE, | 45 FROM_HERE, |
| 47 base::Bind(&ChromotingJniInstance::Cleanup, this)); | 46 base::Bind(&ChromotingJniInstance::Cleanup, this)); |
| 48 return; | 47 return; |
| 49 } | 48 } |
| 50 | 49 |
| 51 // This must be destroyed on the display thread before the producer is gone. | 50 // This must be destroyed on the display thread before the producer is gone. |
| 52 view_.reset(); | 51 view_.reset(); |
| 53 | 52 |
| 54 // The weak pointers must be invalidated on the same thread they were used. | 53 // The weak pointers must be invalidated on the same thread they were used. |
| 55 view_weak_factory_->InvalidateWeakPtrs(); | 54 view_weak_factory_->InvalidateWeakPtrs(); |
| 56 | 55 |
| 57 ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE, | 56 jni_runtime_->network_task_runner()->PostTask( |
| 57 FROM_HERE, |
| 58 base::Bind(&ChromotingJniInstance::DisconnectFromHostOnNetworkThread, | 58 base::Bind(&ChromotingJniInstance::DisconnectFromHostOnNetworkThread, |
| 59 this)); | 59 this)); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void ChromotingJniInstance::ProvideSecret(const char* pin) { | 62 void ChromotingJniInstance::ProvideSecret(const char* pin) { |
| 63 DCHECK(ChromotingJni::GetInstance()-> | 63 DCHECK(jni_runtime_->ui_task_runner()->BelongsToCurrentThread()); |
| 64 ui_task_runner()->BelongsToCurrentThread()); | |
| 65 DCHECK(!pin_callback_.is_null()); | 64 DCHECK(!pin_callback_.is_null()); |
| 66 | 65 |
| 67 // We invoke the string constructor to ensure |pin| gets copied *before* the | 66 // We invoke the string constructor to ensure |pin| gets copied *before* the |
| 68 // asynchronous run, since Java might want it back as soon as we return. | 67 // asynchronous run, since Java might want it back as soon as we return. |
| 69 ChromotingJni::GetInstance()->network_task_runner()->PostTask(FROM_HERE, | 68 jni_runtime_->network_task_runner()->PostTask(FROM_HERE, |
| 70 base::Bind(pin_callback_, pin)); | 69 base::Bind(pin_callback_, pin)); |
| 71 } | 70 } |
| 72 | 71 |
| 73 void ChromotingJniInstance::RedrawDesktop() { | 72 void ChromotingJniInstance::RedrawDesktop() { |
| 74 if (!ChromotingJni::GetInstance()-> | 73 if (!jni_runtime_->display_task_runner()->BelongsToCurrentThread()) { |
| 75 display_task_runner()->BelongsToCurrentThread()) { | 74 jni_runtime_->display_task_runner()->PostTask( |
| 76 ChromotingJni::GetInstance()->display_task_runner()->PostTask( | |
| 77 FROM_HERE, | 75 FROM_HERE, |
| 78 base::Bind(&ChromotingJniInstance::RedrawDesktop, this)); | 76 base::Bind(&ChromotingJniInstance::RedrawDesktop, this)); |
| 79 return; | 77 return; |
| 80 } | 78 } |
| 81 | 79 |
| 82 ChromotingJni::GetInstance()->RedrawCanvas(); | 80 jni_runtime_->RedrawCanvas(); |
| 83 } | 81 } |
| 84 | 82 |
| 85 void ChromotingJniInstance::PerformMouseAction( | 83 void ChromotingJniInstance::PerformMouseAction( |
| 86 int x, | 84 int x, |
| 87 int y, | 85 int y, |
| 88 protocol::MouseEvent_MouseButton button, | 86 protocol::MouseEvent_MouseButton button, |
| 89 bool buttonDown) { | 87 bool buttonDown) { |
| 90 if(!ChromotingJni::GetInstance()-> | 88 if (!jni_runtime_->network_task_runner()->BelongsToCurrentThread()) { |
| 91 network_task_runner()->BelongsToCurrentThread()) { | 89 jni_runtime_->network_task_runner()->PostTask( |
| 92 ChromotingJni::GetInstance()->network_task_runner()->PostTask( | |
| 93 FROM_HERE, | 90 FROM_HERE, |
| 94 base::Bind(&ChromotingJniInstance::PerformMouseAction, | 91 base::Bind(&ChromotingJniInstance::PerformMouseAction, |
| 95 this, | 92 this, |
| 96 x, | 93 x, |
| 97 y, | 94 y, |
| 98 button, | 95 button, |
| 99 buttonDown)); | 96 buttonDown)); |
| 100 return; | 97 return; |
| 101 } | 98 } |
| 102 | 99 |
| 103 protocol::MouseEvent action; | 100 protocol::MouseEvent action; |
| 104 action.set_x(x); | 101 action.set_x(x); |
| 105 action.set_y(y); | 102 action.set_y(y); |
| 106 action.set_button(button); | 103 action.set_button(button); |
| 107 if (button != protocol::MouseEvent::BUTTON_UNDEFINED) | 104 if (button != protocol::MouseEvent::BUTTON_UNDEFINED) |
| 108 action.set_button_down(buttonDown); | 105 action.set_button_down(buttonDown); |
| 109 | 106 |
| 110 connection_->input_stub()->InjectMouseEvent(action); | 107 connection_->input_stub()->InjectMouseEvent(action); |
| 111 } | 108 } |
| 112 | 109 |
| 113 void ChromotingJniInstance::OnConnectionState( | 110 void ChromotingJniInstance::OnConnectionState( |
| 114 protocol::ConnectionToHost::State state, | 111 protocol::ConnectionToHost::State state, |
| 115 protocol::ErrorCode error) { | 112 protocol::ErrorCode error) { |
| 116 if (!ChromotingJni::GetInstance()-> | 113 if (!jni_runtime_->ui_task_runner()->BelongsToCurrentThread()) { |
| 117 ui_task_runner()->BelongsToCurrentThread()) { | 114 jni_runtime_->ui_task_runner()->PostTask( |
| 118 ChromotingJni::GetInstance()-> | |
| 119 ui_task_runner()->PostTask( | |
| 120 FROM_HERE, | 115 FROM_HERE, |
| 121 base::Bind(&ChromotingJniInstance::OnConnectionState, | 116 base::Bind(&ChromotingJniInstance::OnConnectionState, |
| 122 this, | 117 this, |
| 123 state, | 118 state, |
| 124 error)); | 119 error)); |
| 125 return; | 120 return; |
| 126 } | 121 } |
| 127 | 122 |
| 128 ChromotingJni::GetInstance()->ReportConnectionStatus(state, error); | 123 jni_runtime_->ReportConnectionStatus(state, error); |
| 129 } | 124 } |
| 130 | 125 |
| 131 void ChromotingJniInstance::OnConnectionReady(bool ready) { | 126 void ChromotingJniInstance::OnConnectionReady(bool ready) { |
| 132 // We ignore this message, since OnConnectionState() tells us the same thing. | 127 // We ignore this message, since OnConnectionState() tells us the same thing. |
| 133 } | 128 } |
| 134 | 129 |
| 135 void ChromotingJniInstance::SetCapabilities(const std::string& capabilities) {} | 130 void ChromotingJniInstance::SetCapabilities(const std::string& capabilities) {} |
| 136 | 131 |
| 137 void ChromotingJniInstance::SetPairingResponse( | 132 void ChromotingJniInstance::SetPairingResponse( |
| 138 const protocol::PairingResponse& response) { | 133 const protocol::PairingResponse& response) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 157 const protocol::ClipboardEvent& event) { | 152 const protocol::ClipboardEvent& event) { |
| 158 NOTIMPLEMENTED(); | 153 NOTIMPLEMENTED(); |
| 159 } | 154 } |
| 160 | 155 |
| 161 void ChromotingJniInstance::SetCursorShape( | 156 void ChromotingJniInstance::SetCursorShape( |
| 162 const protocol::CursorShapeInfo& shape) { | 157 const protocol::CursorShapeInfo& shape) { |
| 163 NOTIMPLEMENTED(); | 158 NOTIMPLEMENTED(); |
| 164 } | 159 } |
| 165 | 160 |
| 166 void ChromotingJniInstance::ConnectToHostOnDisplayThread() { | 161 void ChromotingJniInstance::ConnectToHostOnDisplayThread() { |
| 167 DCHECK(ChromotingJni::GetInstance()-> | 162 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); |
| 168 display_task_runner()->BelongsToCurrentThread()); | |
| 169 | 163 |
| 170 frame_consumer_ = new FrameConsumerProxy( | 164 frame_consumer_ = new FrameConsumerProxy(jni_runtime_->display_task_runner()); |
| 171 ChromotingJni::GetInstance()->display_task_runner()); | 165 view_.reset(new JniFrameConsumer(jni_runtime_)); |
| 172 view_.reset(new JniFrameConsumer()); | |
| 173 view_weak_factory_.reset(new base::WeakPtrFactory<JniFrameConsumer>( | 166 view_weak_factory_.reset(new base::WeakPtrFactory<JniFrameConsumer>( |
| 174 view_.get())); | 167 view_.get())); |
| 175 frame_consumer_->Attach(view_weak_factory_->GetWeakPtr()); | 168 frame_consumer_->Attach(view_weak_factory_->GetWeakPtr()); |
| 176 | 169 |
| 177 ChromotingJni::GetInstance()->network_task_runner()->PostTask( | 170 jni_runtime_->network_task_runner()->PostTask( |
| 178 FROM_HERE, | 171 FROM_HERE, |
| 179 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread, | 172 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread, |
| 180 this)); | 173 this)); |
| 181 } | 174 } |
| 182 | 175 |
| 183 void ChromotingJniInstance::ConnectToHostOnNetworkThread() { | 176 void ChromotingJniInstance::ConnectToHostOnNetworkThread() { |
| 184 DCHECK(ChromotingJni::GetInstance()-> | 177 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); |
| 185 network_task_runner()->BelongsToCurrentThread()); | |
| 186 | 178 |
| 187 client_config_.reset(new ClientConfig()); | 179 client_config_.reset(new ClientConfig()); |
| 188 client_config_->host_jid = host_jid_; | 180 client_config_->host_jid = host_jid_; |
| 189 client_config_->host_public_key = host_pubkey_; | 181 client_config_->host_public_key = host_pubkey_; |
| 190 | 182 |
| 191 client_config_->fetch_secret_callback = base::Bind( | 183 client_config_->fetch_secret_callback = base::Bind( |
| 192 &ChromotingJniInstance::FetchSecret, | 184 &ChromotingJniInstance::FetchSecret, |
| 193 this); | 185 this); |
| 194 client_config_->authentication_tag = host_id_; | 186 client_config_->authentication_tag = host_id_; |
| 195 | 187 |
| 196 client_config_->authentication_methods.push_back( | 188 client_config_->authentication_methods.push_back( |
| 197 protocol::AuthenticationMethod::FromString("spake2_hmac")); | 189 protocol::AuthenticationMethod::FromString("spake2_hmac")); |
| 198 client_config_->authentication_methods.push_back( | 190 client_config_->authentication_methods.push_back( |
| 199 protocol::AuthenticationMethod::FromString("spake2_plain")); | 191 protocol::AuthenticationMethod::FromString("spake2_plain")); |
| 200 | 192 |
| 201 client_context_.reset(new ClientContext( | 193 client_context_.reset(new ClientContext( |
| 202 ChromotingJni::GetInstance()->network_task_runner().get())); | 194 jni_runtime_->network_task_runner().get())); |
| 203 client_context_->Start(); | 195 client_context_->Start(); |
| 204 | 196 |
| 205 connection_.reset(new protocol::ConnectionToHost(true)); | 197 connection_.reset(new protocol::ConnectionToHost(true)); |
| 206 | 198 |
| 207 client_.reset(new ChromotingClient(*client_config_, | 199 client_.reset(new ChromotingClient(*client_config_, |
| 208 client_context_.get(), | 200 client_context_.get(), |
| 209 connection_.get(), | 201 connection_.get(), |
| 210 this, | 202 this, |
| 211 frame_consumer_, | 203 frame_consumer_, |
| 212 scoped_ptr<AudioPlayer>())); | 204 scoped_ptr<AudioPlayer>())); |
| 213 | 205 |
| 214 view_->set_frame_producer(client_->GetFrameProducer()); | 206 view_->set_frame_producer(client_->GetFrameProducer()); |
| 215 | 207 |
| 216 signaling_config_.reset(new XmppSignalStrategy::XmppServerConfig()); | 208 signaling_config_.reset(new XmppSignalStrategy::XmppServerConfig()); |
| 217 signaling_config_->host = CHAT_SERVER; | 209 signaling_config_->host = CHAT_SERVER; |
| 218 signaling_config_->port = CHAT_PORT; | 210 signaling_config_->port = CHAT_PORT; |
| 219 signaling_config_->use_tls = CHAT_USE_TLS; | 211 signaling_config_->use_tls = CHAT_USE_TLS; |
| 220 | 212 |
| 221 signaling_.reset(new XmppSignalStrategy( | 213 signaling_.reset(new XmppSignalStrategy(jni_runtime_->url_requester(), |
| 222 ChromotingJni::GetInstance()->url_requester(), | 214 username_, |
| 223 username_, | 215 auth_token_, |
| 224 auth_token_, | 216 "oauth2", |
| 225 "oauth2", | 217 *signaling_config_)); |
| 226 *signaling_config_)); | |
| 227 | 218 |
| 228 network_settings_.reset(new NetworkSettings( | 219 network_settings_.reset(new NetworkSettings( |
| 229 NetworkSettings::NAT_TRAVERSAL_OUTGOING)); | 220 NetworkSettings::NAT_TRAVERSAL_OUTGOING)); |
| 230 scoped_ptr<protocol::TransportFactory> fact( | 221 scoped_ptr<protocol::TransportFactory> fact( |
| 231 protocol::LibjingleTransportFactory::Create( | 222 protocol::LibjingleTransportFactory::Create( |
| 232 *network_settings_, | 223 *network_settings_, |
| 233 ChromotingJni::GetInstance()->url_requester())); | 224 jni_runtime_->url_requester())); |
| 234 | 225 |
| 235 client_->Start(signaling_.get(), fact.Pass()); | 226 client_->Start(signaling_.get(), fact.Pass()); |
| 236 } | 227 } |
| 237 | 228 |
| 238 void ChromotingJniInstance::DisconnectFromHostOnNetworkThread() { | 229 void ChromotingJniInstance::DisconnectFromHostOnNetworkThread() { |
| 239 DCHECK(ChromotingJni::GetInstance()-> | 230 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); |
| 240 network_task_runner()->BelongsToCurrentThread()); | |
| 241 | 231 |
| 242 username_ = ""; | 232 username_ = ""; |
| 243 auth_token_ = ""; | 233 auth_token_ = ""; |
| 244 host_jid_ = ""; | 234 host_jid_ = ""; |
| 245 host_id_ = ""; | 235 host_id_ = ""; |
| 246 host_pubkey_ = ""; | 236 host_pubkey_ = ""; |
| 247 | 237 |
| 248 // |client_| must be torn down before |signaling_|. | 238 // |client_| must be torn down before |signaling_|. |
| 249 connection_.reset(); | 239 connection_.reset(); |
| 250 client_.reset(); | 240 client_.reset(); |
| 251 } | 241 } |
| 252 | 242 |
| 253 void ChromotingJniInstance::FetchSecret( | 243 void ChromotingJniInstance::FetchSecret( |
| 254 bool pairable, | 244 bool pairable, |
| 255 const protocol::SecretFetchedCallback& callback) { | 245 const protocol::SecretFetchedCallback& callback) { |
| 256 if (!ChromotingJni::GetInstance()-> | 246 if (!jni_runtime_->ui_task_runner()->BelongsToCurrentThread()) { |
| 257 ui_task_runner()->BelongsToCurrentThread()) { | 247 jni_runtime_->ui_task_runner()->PostTask( |
| 258 ChromotingJni::GetInstance()->ui_task_runner()->PostTask( | |
| 259 FROM_HERE, | 248 FROM_HERE, |
| 260 base::Bind(&ChromotingJniInstance::FetchSecret, | 249 base::Bind(&ChromotingJniInstance::FetchSecret, |
| 261 this, | 250 this, |
| 262 pairable, | 251 pairable, |
| 263 callback)); | 252 callback)); |
| 264 return; | 253 return; |
| 265 } | 254 } |
| 266 | 255 |
| 267 pin_callback_ = callback; | 256 pin_callback_ = callback; |
| 268 ChromotingJni::GetInstance()->DisplayAuthenticationPrompt(); | 257 jni_runtime_->DisplayAuthenticationPrompt(); |
| 269 } | 258 } |
| 270 | 259 |
| 271 } // namespace remoting | 260 } // namespace remoting |
| OLD | NEW |