| 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 <android/log.h> | 7 #include <android/log.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "jingle/glue/thread_wrapper.h" | 11 #include "jingle/glue/thread_wrapper.h" |
| 12 #include "net/socket/client_socket_factory.h" | 12 #include "net/socket/client_socket_factory.h" |
| 13 #include "remoting/base/service_urls.h" | 13 #include "remoting/base/service_urls.h" |
| 14 #include "remoting/client/audio_player.h" | 14 #include "remoting/client/audio_player.h" |
| 15 #include "remoting/client/client_status_logger.h" | 15 #include "remoting/client/client_status_logger.h" |
| 16 #include "remoting/client/jni/android_keymap.h" | 16 #include "remoting/client/jni/android_keymap.h" |
| 17 #include "remoting/client/jni/chromoting_jni_runtime.h" | 17 #include "remoting/client/jni/chromoting_jni_runtime.h" |
| 18 #include "remoting/client/jni/jni_frame_consumer.h" |
| 18 #include "remoting/client/software_video_renderer.h" | 19 #include "remoting/client/software_video_renderer.h" |
| 19 #include "remoting/client/token_fetcher_proxy.h" | 20 #include "remoting/client/token_fetcher_proxy.h" |
| 20 #include "remoting/protocol/chromium_port_allocator.h" | 21 #include "remoting/protocol/chromium_port_allocator.h" |
| 21 #include "remoting/protocol/chromium_socket_factory.h" | 22 #include "remoting/protocol/chromium_socket_factory.h" |
| 22 #include "remoting/protocol/host_stub.h" | 23 #include "remoting/protocol/host_stub.h" |
| 23 #include "remoting/protocol/libjingle_transport_factory.h" | 24 #include "remoting/protocol/libjingle_transport_factory.h" |
| 24 #include "remoting/protocol/negotiating_client_authenticator.h" | 25 #include "remoting/protocol/negotiating_client_authenticator.h" |
| 25 #include "remoting/protocol/network_settings.h" | 26 #include "remoting/protocol/network_settings.h" |
| 26 #include "remoting/signaling/server_log_entry.h" | 27 #include "remoting/signaling/server_log_entry.h" |
| 27 | 28 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 auth_methods.push_back(protocol::AuthenticationMethod::Spake2( | 79 auth_methods.push_back(protocol::AuthenticationMethod::Spake2( |
| 79 protocol::AuthenticationMethod::NONE)); | 80 protocol::AuthenticationMethod::NONE)); |
| 80 auth_methods.push_back(protocol::AuthenticationMethod::ThirdParty()); | 81 auth_methods.push_back(protocol::AuthenticationMethod::ThirdParty()); |
| 81 | 82 |
| 82 authenticator_.reset(new protocol::NegotiatingClientAuthenticator( | 83 authenticator_.reset(new protocol::NegotiatingClientAuthenticator( |
| 83 pairing_id, pairing_secret, host_id_, | 84 pairing_id, pairing_secret, host_id_, |
| 84 base::Bind(&ChromotingJniInstance::FetchSecret, this), | 85 base::Bind(&ChromotingJniInstance::FetchSecret, this), |
| 85 token_fetcher.Pass(), auth_methods)); | 86 token_fetcher.Pass(), auth_methods)); |
| 86 | 87 |
| 87 // Post a task to start connection | 88 // Post a task to start connection |
| 88 jni_runtime_->display_task_runner()->PostTask( | 89 jni_runtime_->network_task_runner()->PostTask( |
| 89 FROM_HERE, | 90 FROM_HERE, |
| 90 base::Bind(&ChromotingJniInstance::ConnectToHostOnDisplayThread, | 91 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread, this)); |
| 91 this)); | |
| 92 } | 92 } |
| 93 | 93 |
| 94 ChromotingJniInstance::~ChromotingJniInstance() { | 94 ChromotingJniInstance::~ChromotingJniInstance() { |
| 95 // This object is ref-counted, so this dtor can execute on any thread. | 95 // This object is ref-counted, so this dtor can execute on any thread. |
| 96 // Ensure that all these objects have been freed already, so they are not | 96 // Ensure that all these objects have been freed already, so they are not |
| 97 // destroyed on some random thread. | 97 // destroyed on some random thread. |
| 98 DCHECK(!view_); | 98 DCHECK(!view_); |
| 99 DCHECK(!client_context_); | 99 DCHECK(!client_context_); |
| 100 DCHECK(!video_renderer_); | 100 DCHECK(!video_renderer_); |
| 101 DCHECK(!authenticator_); | 101 DCHECK(!authenticator_); |
| 102 DCHECK(!client_); | 102 DCHECK(!client_); |
| 103 DCHECK(!signaling_); | 103 DCHECK(!signaling_); |
| 104 DCHECK(!client_status_logger_); | 104 DCHECK(!client_status_logger_); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void ChromotingJniInstance::Disconnect() { | 107 void ChromotingJniInstance::Disconnect() { |
| 108 if (!jni_runtime_->display_task_runner()->BelongsToCurrentThread()) { | 108 if (!jni_runtime_->network_task_runner()->BelongsToCurrentThread()) { |
| 109 jni_runtime_->display_task_runner()->PostTask( | 109 jni_runtime_->network_task_runner()->PostTask( |
| 110 FROM_HERE, | 110 FROM_HERE, |
| 111 base::Bind(&ChromotingJniInstance::Disconnect, this)); | 111 base::Bind(&ChromotingJniInstance::Disconnect, this)); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 | 114 |
| 115 // This must be destroyed on the display thread before the producer is gone. | 115 host_id_.clear(); |
| 116 |
| 117 stats_logging_enabled_ = false; |
| 118 |
| 119 // |client_| must be torn down before |signaling_|. |
| 120 client_.reset(); |
| 121 client_status_logger_.reset(); |
| 122 video_renderer_.reset(); |
| 116 view_.reset(); | 123 view_.reset(); |
| 117 | 124 authenticator_.reset(); |
| 118 // The weak pointers must be invalidated on the same thread they were used. | 125 signaling_.reset(); |
| 119 view_weak_factory_->InvalidateWeakPtrs(); | 126 client_context_.reset(); |
| 120 | |
| 121 jni_runtime_->network_task_runner()->PostTask( | |
| 122 FROM_HERE, | |
| 123 base::Bind(&ChromotingJniInstance::DisconnectFromHostOnNetworkThread, | |
| 124 this)); | |
| 125 } | 127 } |
| 126 | 128 |
| 127 void ChromotingJniInstance::FetchThirdPartyToken( | 129 void ChromotingJniInstance::FetchThirdPartyToken( |
| 128 const GURL& token_url, | 130 const GURL& token_url, |
| 129 const std::string& client_id, | 131 const std::string& client_id, |
| 130 const std::string& scope, | 132 const std::string& scope, |
| 131 base::WeakPtr<TokenFetcherProxy> token_fetcher_proxy) { | 133 base::WeakPtr<TokenFetcherProxy> token_fetcher_proxy) { |
| 132 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); | 134 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); |
| 133 DCHECK(!token_fetcher_proxy_.get()); | 135 DCHECK(!token_fetcher_proxy_.get()); |
| 134 | 136 |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 if (!jni_runtime_->display_task_runner()->BelongsToCurrentThread()) { | 376 if (!jni_runtime_->display_task_runner()->BelongsToCurrentThread()) { |
| 375 jni_runtime_->display_task_runner()->PostTask( | 377 jni_runtime_->display_task_runner()->PostTask( |
| 376 FROM_HERE, | 378 FROM_HERE, |
| 377 base::Bind(&ChromotingJniInstance::SetCursorShape, this, shape)); | 379 base::Bind(&ChromotingJniInstance::SetCursorShape, this, shape)); |
| 378 return; | 380 return; |
| 379 } | 381 } |
| 380 | 382 |
| 381 jni_runtime_->UpdateCursorShape(shape); | 383 jni_runtime_->UpdateCursorShape(shape); |
| 382 } | 384 } |
| 383 | 385 |
| 384 void ChromotingJniInstance::ConnectToHostOnDisplayThread() { | 386 void ChromotingJniInstance::ConnectToHostOnNetworkThread() { |
| 385 DCHECK(jni_runtime_->display_task_runner()->BelongsToCurrentThread()); | |
| 386 | |
| 387 view_.reset(new JniFrameConsumer(jni_runtime_, this)); | |
| 388 view_weak_factory_.reset(new base::WeakPtrFactory<JniFrameConsumer>( | |
| 389 view_.get())); | |
| 390 scoped_ptr<FrameConsumerProxy> frame_consumer = | |
| 391 make_scoped_ptr(new FrameConsumerProxy(view_weak_factory_->GetWeakPtr())); | |
| 392 | |
| 393 jni_runtime_->network_task_runner()->PostTask( | |
| 394 FROM_HERE, | |
| 395 base::Bind(&ChromotingJniInstance::ConnectToHostOnNetworkThread, this, | |
| 396 base::Passed(&frame_consumer))); | |
| 397 } | |
| 398 | |
| 399 void ChromotingJniInstance::ConnectToHostOnNetworkThread( | |
| 400 scoped_ptr<FrameConsumerProxy> frame_consumer) { | |
| 401 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); | 387 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); |
| 402 DCHECK(frame_consumer); | |
| 403 | 388 |
| 404 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 389 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 405 | 390 |
| 406 client_context_.reset(new ClientContext(jni_runtime_->network_task_runner())); | 391 client_context_.reset(new ClientContext(jni_runtime_->network_task_runner())); |
| 407 client_context_->Start(); | 392 client_context_->Start(); |
| 408 | 393 |
| 409 SoftwareVideoRenderer* renderer = new SoftwareVideoRenderer( | 394 view_.reset(new JniFrameConsumer(jni_runtime_)); |
| 410 client_context_->main_task_runner(), | 395 video_renderer_.reset(new SoftwareVideoRenderer( |
| 411 client_context_->decode_task_runner(), frame_consumer.Pass()); | 396 client_context_->decode_task_runner(), view_.get())); |
| 412 view_->set_frame_producer(renderer); | |
| 413 video_renderer_.reset(renderer); | |
| 414 | 397 |
| 415 client_.reset(new ChromotingClient( | 398 client_.reset(new ChromotingClient( |
| 416 client_context_.get(), this, video_renderer_.get(), nullptr)); | 399 client_context_.get(), this, video_renderer_.get(), nullptr)); |
| 417 | 400 |
| 418 signaling_.reset(new XmppSignalStrategy( | 401 signaling_.reset(new XmppSignalStrategy( |
| 419 net::ClientSocketFactory::GetDefaultFactory(), | 402 net::ClientSocketFactory::GetDefaultFactory(), |
| 420 jni_runtime_->url_requester(), xmpp_config_)); | 403 jni_runtime_->url_requester(), xmpp_config_)); |
| 421 | 404 |
| 422 client_status_logger_.reset( | 405 client_status_logger_.reset( |
| 423 new ClientStatusLogger(ServerLogEntry::ME2ME, | 406 new ClientStatusLogger(ServerLogEntry::ME2ME, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 434 | 417 |
| 435 scoped_ptr<protocol::TransportFactory> transport_factory( | 418 scoped_ptr<protocol::TransportFactory> transport_factory( |
| 436 new protocol::LibjingleTransportFactory( | 419 new protocol::LibjingleTransportFactory( |
| 437 signaling_.get(), port_allocator.Pass(), network_settings, | 420 signaling_.get(), port_allocator.Pass(), network_settings, |
| 438 protocol::TransportRole::CLIENT)); | 421 protocol::TransportRole::CLIENT)); |
| 439 | 422 |
| 440 client_->Start(signaling_.get(), authenticator_.Pass(), | 423 client_->Start(signaling_.get(), authenticator_.Pass(), |
| 441 transport_factory.Pass(), host_jid_, capabilities_); | 424 transport_factory.Pass(), host_jid_, capabilities_); |
| 442 } | 425 } |
| 443 | 426 |
| 444 void ChromotingJniInstance::DisconnectFromHostOnNetworkThread() { | |
| 445 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); | |
| 446 | |
| 447 host_id_.clear(); | |
| 448 | |
| 449 stats_logging_enabled_ = false; | |
| 450 | |
| 451 // |client_| must be torn down before |signaling_|. | |
| 452 client_.reset(); | |
| 453 client_status_logger_.reset(); | |
| 454 video_renderer_.reset(); | |
| 455 authenticator_.reset(); | |
| 456 signaling_.reset(); | |
| 457 client_context_.reset(); | |
| 458 } | |
| 459 | |
| 460 void ChromotingJniInstance::FetchSecret( | 427 void ChromotingJniInstance::FetchSecret( |
| 461 bool pairable, | 428 bool pairable, |
| 462 const protocol::SecretFetchedCallback& callback) { | 429 const protocol::SecretFetchedCallback& callback) { |
| 463 if (!jni_runtime_->ui_task_runner()->BelongsToCurrentThread()) { | 430 if (!jni_runtime_->ui_task_runner()->BelongsToCurrentThread()) { |
| 464 jni_runtime_->ui_task_runner()->PostTask( | 431 jni_runtime_->ui_task_runner()->PostTask( |
| 465 FROM_HERE, base::Bind(&ChromotingJniInstance::FetchSecret, | 432 FROM_HERE, base::Bind(&ChromotingJniInstance::FetchSecret, |
| 466 this, pairable, callback)); | 433 this, pairable, callback)); |
| 467 return; | 434 return; |
| 468 } | 435 } |
| 469 | 436 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 487 | 454 |
| 488 void ChromotingJniInstance::SendKeyEventInternal(int usb_key_code, | 455 void ChromotingJniInstance::SendKeyEventInternal(int usb_key_code, |
| 489 bool key_down) { | 456 bool key_down) { |
| 490 if (!jni_runtime_->network_task_runner()->BelongsToCurrentThread()) { | 457 if (!jni_runtime_->network_task_runner()->BelongsToCurrentThread()) { |
| 491 jni_runtime_->network_task_runner()->PostTask( | 458 jni_runtime_->network_task_runner()->PostTask( |
| 492 FROM_HERE, base::Bind(&ChromotingJniInstance::SendKeyEventInternal, | 459 FROM_HERE, base::Bind(&ChromotingJniInstance::SendKeyEventInternal, |
| 493 this, usb_key_code, key_down)); | 460 this, usb_key_code, key_down)); |
| 494 return; | 461 return; |
| 495 } | 462 } |
| 496 | 463 |
| 497 | |
| 498 protocol::KeyEvent event; | 464 protocol::KeyEvent event; |
| 499 event.set_usb_keycode(usb_key_code); | 465 event.set_usb_keycode(usb_key_code); |
| 500 event.set_pressed(key_down); | 466 event.set_pressed(key_down); |
| 501 client_->input_stub()->InjectKeyEvent(event); | 467 client_->input_stub()->InjectKeyEvent(event); |
| 502 } | 468 } |
| 503 | 469 |
| 504 void ChromotingJniInstance::EnableStatsLogging(bool enabled) { | 470 void ChromotingJniInstance::EnableStatsLogging(bool enabled) { |
| 505 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); | 471 DCHECK(jni_runtime_->network_task_runner()->BelongsToCurrentThread()); |
| 506 | 472 |
| 507 if (enabled && !stats_logging_enabled_) { | 473 if (enabled && !stats_logging_enabled_) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 528 stats->round_trip_ms()); | 494 stats->round_trip_ms()); |
| 529 | 495 |
| 530 client_status_logger_->LogStatistics(stats); | 496 client_status_logger_->LogStatistics(stats); |
| 531 | 497 |
| 532 jni_runtime_->network_task_runner()->PostDelayedTask( | 498 jni_runtime_->network_task_runner()->PostDelayedTask( |
| 533 FROM_HERE, base::Bind(&ChromotingJniInstance::LogPerfStats, this), | 499 FROM_HERE, base::Bind(&ChromotingJniInstance::LogPerfStats, this), |
| 534 base::TimeDelta::FromMilliseconds(kPerfStatsIntervalMs)); | 500 base::TimeDelta::FromMilliseconds(kPerfStatsIntervalMs)); |
| 535 } | 501 } |
| 536 | 502 |
| 537 } // namespace remoting | 503 } // namespace remoting |
| OLD | NEW |