diff options
author | sergeyu <sergeyu@chromium.org> | 2015-12-16 14:45:00 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-16 22:46:17 +0000 |
commit | 86030f38b4a7be9418c50da7e83ff55749438dba (patch) | |
tree | a885f9e90179ee8ae6ad27c651bd2ddb704124da | |
parent | 3b7c3e37ec089dc5179359a52a7b2311285c7f21 (diff) | |
download | chromium_src-86030f38b4a7be9418c50da7e83ff55749438dba.zip chromium_src-86030f38b4a7be9418c50da7e83ff55749438dba.tar.gz chromium_src-86030f38b4a7be9418c50da7e83ff55749438dba.tar.bz2 |
Add TransportContext class.
The new TransportContext is now used to store all parameters required
to initialize Transport objects and is applicable both to IceTransport
and WebrtcTransport. It also allowed to reduce amount of boilerplate
code when passing around these parameters.
BUG=547158
Review URL: https://codereview.chromium.org/1521883006
Cr-Commit-Position: refs/heads/master@{#365649}
40 files changed, 577 insertions, 661 deletions
diff --git a/remoting/client/chromoting_client.cc b/remoting/client/chromoting_client.cc index d9e0f37..303150f 100644 --- a/remoting/client/chromoting_client.cc +++ b/remoting/client/chromoting_client.cc @@ -18,7 +18,7 @@ #include "remoting/protocol/host_stub.h" #include "remoting/protocol/negotiating_client_authenticator.h" #include "remoting/protocol/session_config.h" -#include "remoting/protocol/transport.h" +#include "remoting/protocol/transport_context.h" namespace remoting { @@ -56,7 +56,7 @@ void ChromotingClient::SetConnectionToHostForTests( void ChromotingClient::Start( SignalStrategy* signal_strategy, scoped_ptr<protocol::Authenticator> authenticator, - scoped_ptr<protocol::TransportFactory> transport_factory, + scoped_refptr<protocol::TransportContext> transport_context, const std::string& host_jid, const std::string& capabilities) { DCHECK(task_runner_->BelongsToCurrentThread()); @@ -68,8 +68,8 @@ void ChromotingClient::Start( connection_->set_video_stub(video_renderer_->GetVideoStub()); connection_->set_audio_stub(audio_decode_scheduler_.get()); - connection_->Connect(signal_strategy, transport_factory.Pass(), - authenticator.Pass(), host_jid, this); + connection_->Connect(signal_strategy, transport_context, authenticator.Pass(), + host_jid, this); } void ChromotingClient::SetCapabilities( diff --git a/remoting/client/chromoting_client.h b/remoting/client/chromoting_client.h index ce7fbcf..93daab2 100644 --- a/remoting/client/chromoting_client.h +++ b/remoting/client/chromoting_client.h @@ -27,7 +27,7 @@ namespace remoting { namespace protocol { class CandidateSessionConfig; -class TransportFactory; +class TransportContext; } // namespace protocol class AudioDecodeScheduler; @@ -62,7 +62,7 @@ class ChromotingClient : public protocol::ConnectionToHost::HostEventCallback, // must outlive the client. void Start(SignalStrategy* signal_strategy, scoped_ptr<protocol::Authenticator> authenticator, - scoped_ptr<protocol::TransportFactory> transport_factory, + scoped_refptr<protocol::TransportContext> transport_context, const std::string& host_jid, const std::string& capabilities); diff --git a/remoting/client/jni/chromoting_jni_instance.cc b/remoting/client/jni/chromoting_jni_instance.cc index 0d5314e..535ca0f 100644 --- a/remoting/client/jni/chromoting_jni_instance.cc +++ b/remoting/client/jni/chromoting_jni_instance.cc @@ -21,10 +21,10 @@ #include "remoting/protocol/chromium_port_allocator.h" #include "remoting/protocol/chromium_socket_factory.h" #include "remoting/protocol/host_stub.h" -#include "remoting/protocol/ice_transport_factory.h" #include "remoting/protocol/negotiating_client_authenticator.h" #include "remoting/protocol/network_settings.h" #include "remoting/protocol/performance_tracker.h" +#include "remoting/protocol/transport_context.h" #include "remoting/signaling/server_log_entry.h" #include "ui/events/keycodes/dom/keycode_converter.h" @@ -423,17 +423,17 @@ void ChromotingJniInstance::ConnectToHostOnNetworkThread() { protocol::NetworkSettings::NAT_TRAVERSAL_FULL); // Use Chrome's network stack to allocate ports for peer-to-peer channels. - scoped_ptr<protocol::ChromiumPortAllocator> port_allocator( - protocol::ChromiumPortAllocator::Create(jni_runtime_->url_requester(), - network_settings)); + scoped_ptr<protocol::ChromiumPortAllocatorFactory> port_allocator_factory( + new protocol::ChromiumPortAllocatorFactory( + jni_runtime_->url_requester())); - scoped_ptr<protocol::TransportFactory> transport_factory( - new protocol::IceTransportFactory( - signaling_.get(), port_allocator.Pass(), network_settings, - protocol::TransportRole::CLIENT)); + scoped_refptr<protocol::TransportContext> transport_context = + new protocol::TransportContext( + signaling_.get(), port_allocator_factory.Pass(), network_settings, + protocol::TransportRole::CLIENT); - client_->Start(signaling_.get(), authenticator_.Pass(), - transport_factory.Pass(), host_jid_, capabilities_); + client_->Start(signaling_.get(), authenticator_.Pass(), transport_context, + host_jid_, capabilities_); } void ChromotingJniInstance::FetchSecret( diff --git a/remoting/client/plugin/chromoting_instance.cc b/remoting/client/plugin/chromoting_instance.cc index 13fe342..93918de 100644 --- a/remoting/client/plugin/chromoting_instance.cc +++ b/remoting/client/plugin/chromoting_instance.cc @@ -52,7 +52,7 @@ #include "remoting/client/token_fetcher_proxy.h" #include "remoting/protocol/connection_to_host.h" #include "remoting/protocol/host_stub.h" -#include "remoting/protocol/ice_transport_factory.h" +#include "remoting/protocol/transport_context.h" #include "url/gurl.h" namespace remoting { @@ -693,10 +693,11 @@ void ChromotingInstance::HandleConnect(const base::DictionaryValue& data) { local_jid, base::Bind(&ChromotingInstance::SendOutgoingIq, weak_factory_.GetWeakPtr()))); - // Create TransportFactory. - scoped_ptr<protocol::TransportFactory> transport_factory( - new protocol::IceTransportFactory( - signal_strategy_.get(), PepperPortAllocator::Create(this).Pass(), + // Create TransportContext. + scoped_refptr<protocol::TransportContext> transport_context( + new protocol::TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new PepperPortAllocatorFactory(this)), protocol::NetworkSettings( protocol::NetworkSettings::NAT_TRAVERSAL_FULL), protocol::TransportRole::CLIENT)); @@ -731,7 +732,7 @@ void ChromotingInstance::HandleConnect(const base::DictionaryValue& data) { // Kick off the connection. client_->Start(signal_strategy_.get(), authenticator.Pass(), - transport_factory.Pass(), host_jid, capabilities); + transport_context, host_jid, capabilities); // Start timer that periodically sends perf stats. stats_update_timer_.Start( diff --git a/remoting/client/plugin/pepper_port_allocator.cc b/remoting/client/plugin/pepper_port_allocator.cc index a601480..11fee075 100644 --- a/remoting/client/plugin/pepper_port_allocator.cc +++ b/remoting/client/plugin/pepper_port_allocator.cc @@ -225,17 +225,9 @@ PepperPortAllocator::PepperPortAllocator( instance_(instance), network_manager_(network_manager.Pass()), socket_factory_(socket_factory.Pass()) { - // TCP transport is disabled becase PseudoTCP works poorly over - // it. ENABLE_SHARED_UFRAG flag is specified so that the same - // username fragment is shared between all candidates for this - // channel. - set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | - cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG| - cricket::PORTALLOCATOR_ENABLE_IPV6); } -PepperPortAllocator::~PepperPortAllocator() { -} +PepperPortAllocator::~PepperPortAllocator() {} cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( const std::string& content_name, @@ -247,4 +239,15 @@ cricket::PortAllocatorSession* PepperPortAllocator::CreateSessionInternal( stun_hosts(), relay_hosts(), relay_token(), instance_); } +PepperPortAllocatorFactory::PepperPortAllocatorFactory( + const pp::InstanceHandle& instance) + : instance_(instance) {} + +PepperPortAllocatorFactory::~PepperPortAllocatorFactory() {} + +scoped_ptr<cricket::HttpPortAllocatorBase> +PepperPortAllocatorFactory::CreatePortAllocator() { + return PepperPortAllocator::Create(instance_); +} + } // namespace remoting diff --git a/remoting/client/plugin/pepper_port_allocator.h b/remoting/client/plugin/pepper_port_allocator.h index c98c242..76d447b 100644 --- a/remoting/client/plugin/pepper_port_allocator.h +++ b/remoting/client/plugin/pepper_port_allocator.h @@ -8,6 +8,7 @@ #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "ppapi/cpp/instance_handle.h" +#include "remoting/protocol/port_allocator_factory.h" #include "third_party/webrtc/p2p/client/httpportallocator.h" namespace remoting { @@ -47,6 +48,20 @@ class PepperPortAllocator : public cricket::HttpPortAllocatorBase { DISALLOW_COPY_AND_ASSIGN(PepperPortAllocator); }; +class PepperPortAllocatorFactory : public protocol::PortAllocatorFactory { + public: + PepperPortAllocatorFactory(const pp::InstanceHandle& instance); + ~PepperPortAllocatorFactory() override; + + // PortAllocatorFactory interface. + scoped_ptr<cricket::HttpPortAllocatorBase> CreatePortAllocator() override; + + private: + pp::InstanceHandle instance_; + + DISALLOW_COPY_AND_ASSIGN(PepperPortAllocatorFactory); +}; + } // namespace remoting #endif // REMOTING_CLIENT_PLUGIN_PEPPER_PORT_ALLOCATOR_H_ diff --git a/remoting/host/cast_extension_session.cc b/remoting/host/cast_extension_session.cc index 9e4b12d..31087c3 100644 --- a/remoting/host/cast_extension_session.cc +++ b/remoting/host/cast_extension_session.cc @@ -49,11 +49,6 @@ const char kWebRtcSDPMLineIndex[] = "sdpMLineIndex"; const char kVideoLabel[] = "cast_video_label"; const char kStreamLabel[] = "stream_label"; -// Default STUN server used to construct -// ChromiumPortAllocator for the PeerConnection. -const char kDefaultStunHost[] = "stun.l.google.com"; -const int kDefaultStunPort = 19302; - const char kWorkerThreadName[] = "CastExtensionSessionWorkerThread"; // Interval between each call to PollPeerConnectionStats(). @@ -491,17 +486,14 @@ bool CastExtensionSession::InitializePeerConnection() { constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, webrtc::MediaConstraintsInterface::kValueTrue); - rtc::scoped_ptr<protocol::ChromiumPortAllocator> port_allocator( - protocol::ChromiumPortAllocator::Create(url_request_context_getter_, - network_settings_) - .release()); - std::vector<rtc::SocketAddress> stun_hosts; - stun_hosts.push_back(rtc::SocketAddress(kDefaultStunHost, kDefaultStunPort)); - port_allocator->SetStunHosts(stun_hosts); + scoped_ptr<cricket::PortAllocator> port_allocator = + protocol::ChromiumPortAllocator::Create(url_request_context_getter_); webrtc::PeerConnectionInterface::RTCConfiguration rtc_config; peer_connection_ = peer_conn_factory_->CreatePeerConnection( - rtc_config, &constraints, port_allocator.Pass(), nullptr, this); + rtc_config, &constraints, + rtc::scoped_ptr<cricket::PortAllocator>(port_allocator.release()), + nullptr, this); if (!peer_connection_.get()) { CleanupPeerConnection(); diff --git a/remoting/host/it2me/it2me_host.cc b/remoting/host/it2me/it2me_host.cc index ed1ce1c..0ecec5b 100644 --- a/remoting/host/it2me/it2me_host.cc +++ b/remoting/host/it2me/it2me_host.cc @@ -9,6 +9,7 @@ #include "base/strings/string_util.h" #include "base/threading/platform_thread.h" #include "net/socket/client_socket_factory.h" +#include "net/url_request/url_request_context_getter.h" #include "policy/policy_constants.h" #include "remoting/base/auto_thread.h" #include "remoting/base/logging.h" @@ -22,9 +23,12 @@ #include "remoting/host/it2me_desktop_environment.h" #include "remoting/host/policy_watcher.h" #include "remoting/host/register_support_host_request.h" -#include "remoting/host/session_manager_factory.h" +#include "remoting/protocol/chromium_port_allocator.h" +#include "remoting/protocol/ice_transport.h" #include "remoting/protocol/it2me_host_authenticator_factory.h" +#include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/network_settings.h" +#include "remoting/protocol/transport_context.h" #include "remoting/signaling/server_log_entry.h" namespace remoting { @@ -227,9 +231,16 @@ void It2MeHost::FinishConnect() { protocol::NetworkSettings::kDefaultMaxPort; } - scoped_ptr<protocol::SessionManager> session_manager = - CreateHostSessionManager(signal_strategy_.get(), network_settings, - host_context_->url_request_context_getter()); + scoped_ptr<protocol::TransportFactory> transport_factory( + new protocol::IceTransportFactory(new protocol::TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new protocol::ChromiumPortAllocatorFactory( + host_context_->url_request_context_getter())), + network_settings, protocol::TransportRole::SERVER))); + + scoped_ptr<protocol::SessionManager> session_manager( + new protocol::JingleSessionManager(transport_factory.Pass())); + scoped_ptr<protocol::CandidateSessionConfig> protocol_config = protocol::CandidateSessionConfig::CreateDefault(); // Disable audio by default. diff --git a/remoting/host/remoting_me2me_host.cc b/remoting/host/remoting_me2me_host.cc index 124c768..7e668d4 100644 --- a/remoting/host/remoting_me2me_host.cc +++ b/remoting/host/remoting_me2me_host.cc @@ -63,7 +63,6 @@ #include "remoting/host/oauth_token_getter_impl.h" #include "remoting/host/pairing_registry_delegate.h" #include "remoting/host/policy_watcher.h" -#include "remoting/host/session_manager_factory.h" #include "remoting/host/shutdown_watchdog.h" #include "remoting/host/signaling_connector.h" #include "remoting/host/single_window_desktop_environment.h" @@ -75,13 +74,15 @@ #include "remoting/host/video_frame_recorder_host_extension.h" #include "remoting/protocol/authenticator.h" #include "remoting/protocol/channel_authenticator.h" -#include "remoting/protocol/chromium_port_allocator_factory.h" +#include "remoting/protocol/chromium_port_allocator.h" +#include "remoting/protocol/ice_transport.h" #include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/me2me_host_authenticator_factory.h" #include "remoting/protocol/network_settings.h" #include "remoting/protocol/pairing_registry.h" #include "remoting/protocol/port_range.h" #include "remoting/protocol/token_validator.h" +#include "remoting/protocol/transport_context.h" #include "remoting/protocol/webrtc_transport.h" #include "remoting/signaling/push_notification_subscriber.h" #include "remoting/signaling/xmpp_signal_strategy.h" @@ -1496,39 +1497,34 @@ void HostProcess::StartHost() { network_settings.port_range.max_port = NetworkSettings::kDefaultMaxPort; } - scoped_ptr<protocol::SessionManager> session_manager; + scoped_refptr<protocol::TransportContext> transport_context = + new protocol::TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new protocol::ChromiumPortAllocatorFactory( + context_->url_request_context_getter())), + network_settings, protocol::TransportRole::SERVER); + scoped_ptr<protocol::TransportFactory> transport_factory; if (base::CommandLine::ForCurrentProcess()->HasSwitch(kEnableWebrtc)) { #if !defined(NDEBUG) - network_settings.flags = protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING; - - scoped_ptr<protocol::PortAllocatorFactory> port_allocator_factory = - protocol::ChromiumPortAllocatorFactory::Create( - network_settings, context_->url_request_context_getter()); - jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); // The network thread is also used as worker thread for webrtc. // // TODO(sergeyu): Figure out if we would benefit from using a separate // thread as a worker thread. - scoped_ptr<protocol::TransportFactory> transport_factory( - new protocol::WebrtcTransportFactory( - jingle_glue::JingleThreadWrapper::current(), signal_strategy_.get(), - std::move(port_allocator_factory), - protocol::TransportRole::SERVER)); - - session_manager.reset( - new protocol::JingleSessionManager(transport_factory.Pass())); + transport_factory.reset(new protocol::WebrtcTransportFactory( + jingle_glue::JingleThreadWrapper::current(), transport_context)); #else // !defined(NDEBUG) LOG(ERROR) << "WebRTC is enabled only in debug builds."; ShutdownHost(kUsageExitCode); return; #endif // defined(NDEBUG) } else { - session_manager = - CreateHostSessionManager(signal_strategy_.get(), network_settings, - context_->url_request_context_getter()); + transport_factory.reset( + new protocol::IceTransportFactory(transport_context)); } + scoped_ptr<protocol::SessionManager> session_manager( + new protocol::JingleSessionManager(transport_factory.Pass())); scoped_ptr<protocol::CandidateSessionConfig> protocol_config = protocol::CandidateSessionConfig::CreateDefault(); diff --git a/remoting/host/session_manager_factory.cc b/remoting/host/session_manager_factory.cc deleted file mode 100644 index 58a6752..0000000 --- a/remoting/host/session_manager_factory.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "remoting/host/session_manager_factory.h" - -#include "remoting/protocol/chromium_port_allocator.h" -#include "remoting/protocol/chromium_socket_factory.h" -#include "remoting/protocol/ice_transport_factory.h" -#include "remoting/protocol/jingle_session_manager.h" -#include "remoting/protocol/network_settings.h" -#include "remoting/protocol/session_manager.h" - -namespace remoting { - -scoped_ptr<protocol::SessionManager> CreateHostSessionManager( - SignalStrategy* signal_strategy, - const protocol::NetworkSettings& network_settings, - const scoped_refptr<net::URLRequestContextGetter>& - url_request_context_getter) { - // Use Chrome's network stack to allocate ports for peer-to-peer channels. - scoped_ptr<protocol::ChromiumPortAllocator> port_allocator( - protocol::ChromiumPortAllocator::Create(url_request_context_getter, - network_settings)); - - scoped_ptr<protocol::TransportFactory> transport_factory( - new protocol::IceTransportFactory( - signal_strategy, port_allocator.Pass(), network_settings, - protocol::TransportRole::SERVER)); - - scoped_ptr<protocol::JingleSessionManager> session_manager( - new protocol::JingleSessionManager(transport_factory.Pass())); - return session_manager.Pass(); -} - -} // namespace remoting diff --git a/remoting/host/session_manager_factory.h b/remoting/host/session_manager_factory.h deleted file mode 100644 index 4bce1f6..0000000 --- a/remoting/host/session_manager_factory.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef REMOTING_HOST_SESSION_MANAGER_FACTORY_H_ -#define REMOTING_HOST_SESSION_MANAGER_FACTORY_H_ - -#include "base/memory/scoped_ptr.h" -#include "net/url_request/url_request_context_getter.h" - -namespace net { -class URLRequestContextGetter; -} // namespace net - -namespace remoting { - -class SignalStrategy; - -namespace protocol { -struct NetworkSettings; -class SessionManager; -} // namespace protocol - -scoped_ptr<protocol::SessionManager> CreateHostSessionManager( - SignalStrategy* signal_strategy, - const protocol::NetworkSettings& network_settings, - const scoped_refptr<net::URLRequestContextGetter>& - url_request_context_getter); - -} // namespace remoting - -#endif // REMOTING_HOST_SESSION_MANAGER_FACTORY_H_ diff --git a/remoting/protocol/chromium_port_allocator.cc b/remoting/protocol/chromium_port_allocator.cc index ffe6756..5a5c103 100644 --- a/remoting/protocol/chromium_port_allocator.cc +++ b/remoting/protocol/chromium_port_allocator.cc @@ -12,7 +12,6 @@ #include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_request_context_getter.h" #include "remoting/protocol/chromium_socket_factory.h" -#include "remoting/protocol/network_settings.h" #include "url/gurl.h" namespace remoting { @@ -131,35 +130,13 @@ void ChromiumPortAllocatorSession::OnURLFetchComplete( // static scoped_ptr<ChromiumPortAllocator> ChromiumPortAllocator::Create( - const scoped_refptr<net::URLRequestContextGetter>& url_context, - const NetworkSettings& network_settings) { + const scoped_refptr<net::URLRequestContextGetter>& url_context) { scoped_ptr<rtc::NetworkManager> network_manager( new rtc::BasicNetworkManager()); scoped_ptr<rtc::PacketSocketFactory> socket_factory( new ChromiumPacketSocketFactory()); - scoped_ptr<ChromiumPortAllocator> result( - new ChromiumPortAllocator(url_context, network_manager.Pass(), - socket_factory.Pass())); - - // We always use PseudoTcp to provide a reliable channel. It provides poor - // performance when combined with TCP-based transport, so we have to disable - // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username - // fragment is shared between all candidates for this channel. - int flags = cricket::PORTALLOCATOR_DISABLE_TCP | - cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | - cricket::PORTALLOCATOR_ENABLE_IPV6; - - if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_STUN)) - flags |= cricket::PORTALLOCATOR_DISABLE_STUN; - - if (!(network_settings.flags & NetworkSettings::NAT_TRAVERSAL_RELAY)) - flags |= cricket::PORTALLOCATOR_DISABLE_RELAY; - - result->set_flags(flags); - result->SetPortRange(network_settings.port_range.min_port, - network_settings.port_range.max_port); - - return result.Pass(); + return make_scoped_ptr(new ChromiumPortAllocator( + url_context, network_manager.Pass(), socket_factory.Pass())); } ChromiumPortAllocator::ChromiumPortAllocator( @@ -173,8 +150,7 @@ ChromiumPortAllocator::ChromiumPortAllocator( network_manager_(network_manager.Pass()), socket_factory_(socket_factory.Pass()) {} -ChromiumPortAllocator::~ChromiumPortAllocator() { -} +ChromiumPortAllocator::~ChromiumPortAllocator() {} cricket::PortAllocatorSession* ChromiumPortAllocator::CreateSessionInternal( const std::string& content_name, @@ -186,5 +162,16 @@ cricket::PortAllocatorSession* ChromiumPortAllocator::CreateSessionInternal( stun_hosts(), relay_hosts(), relay_token(), url_context_); } +ChromiumPortAllocatorFactory::ChromiumPortAllocatorFactory( + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) + : url_request_context_getter_(url_request_context_getter) {} + +ChromiumPortAllocatorFactory::~ChromiumPortAllocatorFactory() {} + +scoped_ptr<cricket::HttpPortAllocatorBase> +ChromiumPortAllocatorFactory::CreatePortAllocator() { + return ChromiumPortAllocator::Create(url_request_context_getter_); +} + } // namespace protocol } // namespace remoting diff --git a/remoting/protocol/chromium_port_allocator.h b/remoting/protocol/chromium_port_allocator.h index 021000e..65d5799 100644 --- a/remoting/protocol/chromium_port_allocator.h +++ b/remoting/protocol/chromium_port_allocator.h @@ -9,22 +9,24 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "net/url_request/url_request_context_getter.h" +#include "remoting/protocol/port_allocator_factory.h" #include "third_party/webrtc/p2p/client/httpportallocator.h" +namespace net { +class URLRequestContextGetter; +} // namespace net + namespace remoting { namespace protocol { struct NetworkSettings; -// An implementation of cricket::PortAllocator for libjingle that -// uses Chromium's network stack and configures itself according -// to the specified NetworkSettings. +// An implementation of cricket::PortAllocator that uses Chromium's network +// stack. class ChromiumPortAllocator : public cricket::HttpPortAllocatorBase { public: static scoped_ptr<ChromiumPortAllocator> Create( - const scoped_refptr<net::URLRequestContextGetter>& url_context, - const NetworkSettings& network_settings); + const scoped_refptr<net::URLRequestContextGetter>& url_context); ~ChromiumPortAllocator() override; @@ -48,6 +50,21 @@ class ChromiumPortAllocator : public cricket::HttpPortAllocatorBase { DISALLOW_COPY_AND_ASSIGN(ChromiumPortAllocator); }; +class ChromiumPortAllocatorFactory : public PortAllocatorFactory { + public: + ChromiumPortAllocatorFactory( + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); + ~ChromiumPortAllocatorFactory() override; + + // PortAllocatorFactory interface. + scoped_ptr<cricket::HttpPortAllocatorBase> CreatePortAllocator() override; + + private: + scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; + + DISALLOW_COPY_AND_ASSIGN(ChromiumPortAllocatorFactory); +}; + } // namespace protocol } // namespace remoting diff --git a/remoting/protocol/chromium_port_allocator_factory.cc b/remoting/protocol/chromium_port_allocator_factory.cc deleted file mode 100644 index 32c980b..0000000 --- a/remoting/protocol/chromium_port_allocator_factory.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "remoting/protocol/chromium_port_allocator_factory.h" - -#include "base/logging.h" -#include "net/url_request/url_request_context_getter.h" -#include "remoting/protocol/chromium_port_allocator.h" -#include "remoting/protocol/network_settings.h" - -namespace remoting { -namespace protocol { - -ChromiumPortAllocatorFactory::ChromiumPortAllocatorFactory( - const NetworkSettings& network_settings, - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) - : network_settings_(network_settings), - url_request_context_getter_(url_request_context_getter) {} - -ChromiumPortAllocatorFactory::~ChromiumPortAllocatorFactory() {} - -scoped_ptr<PortAllocatorFactory> ChromiumPortAllocatorFactory::Create( - const NetworkSettings& network_settings, - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter) { - return scoped_ptr<PortAllocatorFactory>(new ChromiumPortAllocatorFactory( - network_settings, url_request_context_getter)); -} - -cricket::PortAllocator* ChromiumPortAllocatorFactory::CreatePortAllocator() { - scoped_ptr<ChromiumPortAllocator> port_allocator( - ChromiumPortAllocator::Create(url_request_context_getter_, - network_settings_)); - return port_allocator.release(); -} - -} // namespace protocol -} // namespace remoting - diff --git a/remoting/protocol/chromium_port_allocator_factory.h b/remoting/protocol/chromium_port_allocator_factory.h deleted file mode 100644 index 4c7db57..0000000 --- a/remoting/protocol/chromium_port_allocator_factory.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef REMOTING_PROTOCOL_CHROMIUM_PORT_ALLOCATOR_FACTORY_H_ -#define REMOTING_PROTOCOL_CHROMIUM_PORT_ALLOCATOR_FACTORY_H_ - -#include "base/macros.h" -#include "base/memory/ref_counted.h" -#include "base/memory/scoped_ptr.h" -#include "remoting/protocol/network_settings.h" -#include "remoting/protocol/port_allocator_factory.h" - -namespace net { -class URLRequestContextGetter; -} // namespace net - -namespace remoting { -namespace protocol { - -class ChromiumPortAllocatorFactory : public PortAllocatorFactory { - public: - static scoped_ptr<PortAllocatorFactory> Create( - const NetworkSettings& network_settings, - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); - - // PortAllocatorFactory implementation. - cricket::PortAllocator* CreatePortAllocator() override; - - protected: - ChromiumPortAllocatorFactory( - const NetworkSettings& network_settings, - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter); - ~ChromiumPortAllocatorFactory() override; - - private: - NetworkSettings network_settings_; - scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; - - DISALLOW_COPY_AND_ASSIGN(ChromiumPortAllocatorFactory); -}; - -} // namespace protocol -} // namespace remoting - -#endif // REMOTING_PROTOCOL_CHROMIUM_PORT_ALLOCATOR_FACTORY_H_ - diff --git a/remoting/protocol/connection_to_host.h b/remoting/protocol/connection_to_host.h index ad9c8ba..f5c8c82 100644 --- a/remoting/protocol/connection_to_host.h +++ b/remoting/protocol/connection_to_host.h @@ -27,7 +27,7 @@ class ExtensionMessage; class HostStub; class InputStub; class SessionConfig; -class TransportFactory; +class TransportContext; struct TransportRoute; class VideoStub; @@ -92,7 +92,7 @@ class ConnectionToHost { // and must outlive the ConnectionToHost. // Caller must set stubs (see below) before calling Connect. virtual void Connect(SignalStrategy* signal_strategy, - scoped_ptr<TransportFactory> transport_factory, + scoped_refptr<TransportContext> transport_context, scoped_ptr<Authenticator> authenticator, const std::string& host_jid, HostEventCallback* event_callback) = 0; diff --git a/remoting/protocol/connection_to_host_impl.cc b/remoting/protocol/connection_to_host_impl.cc index fbfdb30..bcca1b0 100644 --- a/remoting/protocol/connection_to_host_impl.cc +++ b/remoting/protocol/connection_to_host_impl.cc @@ -18,8 +18,10 @@ #include "remoting/protocol/client_video_dispatcher.h" #include "remoting/protocol/clipboard_stub.h" #include "remoting/protocol/errors.h" +#include "remoting/protocol/ice_transport.h" #include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/transport.h" +#include "remoting/protocol/transport_context.h" #include "remoting/protocol/video_stub.h" namespace remoting { @@ -66,7 +68,7 @@ const char* ConnectionToHost::StateToString(State state) { void ConnectionToHostImpl::Connect( SignalStrategy* signal_strategy, - scoped_ptr<TransportFactory> transport_factory, + scoped_refptr<TransportContext> transport_context, scoped_ptr<Authenticator> authenticator, const std::string& host_jid, HostEventCallback* event_callback) { @@ -91,7 +93,8 @@ void ConnectionToHostImpl::Connect( signal_strategy_->AddListener(this); - session_manager_.reset(new JingleSessionManager(transport_factory.Pass())); + session_manager_.reset(new JingleSessionManager( + make_scoped_ptr(new IceTransportFactory(transport_context)))); session_manager_->set_protocol_config(candidate_config_->Clone()); session_manager_->Init(signal_strategy_, this); diff --git a/remoting/protocol/connection_to_host_impl.h b/remoting/protocol/connection_to_host_impl.h index 936d583..9ee9687 100644 --- a/remoting/protocol/connection_to_host_impl.h +++ b/remoting/protocol/connection_to_host_impl.h @@ -54,7 +54,7 @@ class ConnectionToHostImpl : public ConnectionToHost, void set_video_stub(VideoStub* video_stub) override; void set_audio_stub(AudioStub* audio_stub) override; void Connect(SignalStrategy* signal_strategy, - scoped_ptr<TransportFactory> transport_factory, + scoped_refptr<TransportContext> transport_context, scoped_ptr<Authenticator> authenticator, const std::string& host_jid, HostEventCallback* event_callback) override; diff --git a/remoting/protocol/fake_connection_to_host.cc b/remoting/protocol/fake_connection_to_host.cc index 2983596..865b61c 100644 --- a/remoting/protocol/fake_connection_to_host.cc +++ b/remoting/protocol/fake_connection_to_host.cc @@ -5,6 +5,7 @@ #include "remoting/protocol/fake_connection_to_host.h" #include "remoting/protocol/authenticator.h" +#include "remoting/protocol/transport_context.h" namespace remoting { namespace test { @@ -36,7 +37,7 @@ void FakeConnectionToHost::set_audio_stub(protocol::AudioStub* audio_stub) { void FakeConnectionToHost::Connect( SignalStrategy* signal_strategy, - scoped_ptr<protocol::TransportFactory> transport_factory, + scoped_refptr<protocol::TransportContext> transport_context, scoped_ptr<protocol::Authenticator> authenticator, const std::string& host_jid, HostEventCallback* event_callback) { diff --git a/remoting/protocol/fake_connection_to_host.h b/remoting/protocol/fake_connection_to_host.h index 1c3fe40..85ea24a 100644 --- a/remoting/protocol/fake_connection_to_host.h +++ b/remoting/protocol/fake_connection_to_host.h @@ -26,7 +26,7 @@ class FakeConnectionToHost : public protocol::ConnectionToHost { void set_video_stub(protocol::VideoStub* video_stub) override; void set_audio_stub(protocol::AudioStub* audio_stub) override; void Connect(SignalStrategy* signal_strategy, - scoped_ptr<protocol::TransportFactory> transport_factory, + scoped_refptr<protocol::TransportContext> transport_context, scoped_ptr<protocol::Authenticator> authenticator, const std::string& host_jid, HostEventCallback* event_callback) override; diff --git a/remoting/protocol/ice_transport.cc b/remoting/protocol/ice_transport.cc index 8984dd2..08e95d3 100644 --- a/remoting/protocol/ice_transport.cc +++ b/remoting/protocol/ice_transport.cc @@ -10,6 +10,7 @@ #include "remoting/protocol/pseudotcp_channel_factory.h" #include "remoting/protocol/secure_channel_factory.h" #include "remoting/protocol/stream_channel_factory.h" +#include "remoting/protocol/transport_context.h" namespace remoting { namespace protocol { @@ -22,24 +23,16 @@ const int kTransportInfoSendDelayMs = 20; // Name of the multiplexed channel. static const char kMuxChannelName[] = "mux"; -IceTransport::IceTransport(cricket::PortAllocator* port_allocator, - const NetworkSettings& network_settings, - TransportRole role) - : port_allocator_(port_allocator), - network_settings_(network_settings), - role_(role), - weak_factory_(this) {} +IceTransport::IceTransport(scoped_refptr<TransportContext> transport_context) + : transport_context_(transport_context), weak_factory_(this) { + transport_context->Prepare(); +} IceTransport::~IceTransport() { channel_multiplexer_.reset(); DCHECK(channels_.empty()); } -base::Closure IceTransport::GetCanStartClosure() { - return base::Bind(&IceTransport::OnCanStart, - weak_factory_.GetWeakPtr()); -} - void IceTransport::Start(Transport::EventHandler* event_handler, Authenticator* authenticator) { DCHECK(event_handler); @@ -95,24 +88,12 @@ StreamChannelFactory* IceTransport::GetMultiplexedChannelFactory() { return channel_multiplexer_.get(); } -void IceTransport::OnCanStart() { - DCHECK(!can_start_); - - can_start_ = true; - for (ChannelsMap::iterator it = channels_.begin(); it != channels_.end(); - ++it) { - it->second->OnCanStart(); - } -} - void IceTransport::CreateChannel(const std::string& name, const ChannelCreatedCallback& callback) { DCHECK(!channels_[name]); scoped_ptr<IceTransportChannel> channel( - new IceTransportChannel(port_allocator_, network_settings_, role_)); - if (can_start_) - channel->OnCanStart(); + new IceTransportChannel(transport_context_)); channel->Connect(name, this, callback); AddPendingRemoteTransportInfo(channel.get()); channels_[name] = channel.release(); @@ -205,5 +186,15 @@ void IceTransport::SendTransportInfo() { pending_transport_info_message_.reset(); } +IceTransportFactory::IceTransportFactory( + scoped_refptr<TransportContext> transport_context) + : transport_context_(transport_context) {} + +IceTransportFactory::~IceTransportFactory() {} + +scoped_ptr<Transport> IceTransportFactory::CreateTransport() { + return make_scoped_ptr(new IceTransport(transport_context_.get())); +} + } // namespace protocol } // namespace remoting diff --git a/remoting/protocol/ice_transport.h b/remoting/protocol/ice_transport.h index 09f7da6..fc72b29 100644 --- a/remoting/protocol/ice_transport.h +++ b/remoting/protocol/ice_transport.h @@ -27,16 +27,10 @@ class IceTransport : public Transport, public IceTransportChannel::Delegate, public DatagramChannelFactory { public: - // |port_allocator| must outlive the session. - IceTransport(cricket::PortAllocator* port_allocator, - const NetworkSettings& network_settings, - TransportRole role); + // |transport_context| must outlive the session. + explicit IceTransport(scoped_refptr<TransportContext> transport_context); ~IceTransport() override; - // Returns a closure that must be called before transport channels start - // connecting . - base::Closure GetCanStartClosure(); - // Transport interface. void Start(EventHandler* event_handler, Authenticator* authenticator) override; @@ -47,8 +41,6 @@ class IceTransport : public Transport, private: typedef std::map<std::string, IceTransportChannel*> ChannelsMap; - void OnCanStart(); - // DatagramChannelFactory interface. void CreateChannel(const std::string& name, const ChannelCreatedCallback& callback) override; @@ -76,11 +68,7 @@ class IceTransport : public Transport, // Sends transport-info message with candidates from |pending_candidates_|. void SendTransportInfo(); - cricket::PortAllocator* port_allocator_; - NetworkSettings network_settings_; - TransportRole role_; - - bool can_start_ = false; + scoped_refptr<TransportContext> transport_context_; Transport::EventHandler* event_handler_ = nullptr; @@ -102,6 +90,20 @@ class IceTransport : public Transport, DISALLOW_COPY_AND_ASSIGN(IceTransport); }; +class IceTransportFactory : public TransportFactory { + public: + IceTransportFactory(scoped_refptr<TransportContext> transport_context); + ~IceTransportFactory() override; + + // TransportFactory interface. + scoped_ptr<Transport> CreateTransport() override; + + private: + scoped_refptr<TransportContext> transport_context_; + + DISALLOW_COPY_AND_ASSIGN(IceTransportFactory); +}; + } // namespace protocol } // namespace remoting diff --git a/remoting/protocol/ice_transport_channel.cc b/remoting/protocol/ice_transport_channel.cc index 7e47bce..1ef4d7d 100644 --- a/remoting/protocol/ice_transport_channel.cc +++ b/remoting/protocol/ice_transport_channel.cc @@ -13,6 +13,7 @@ #include "jingle/glue/utils.h" #include "net/base/net_errors.h" #include "remoting/protocol/channel_socket_adapter.h" +#include "remoting/protocol/transport_context.h" #include "third_party/webrtc/base/network.h" #include "third_party/webrtc/p2p/base/constants.h" #include "third_party/webrtc/p2p/base/p2ptransportchannel.h" @@ -46,16 +47,11 @@ TransportRoute::RouteType CandidateTypeToTransportRouteType( } // namespace -IceTransportChannel::IceTransportChannel(cricket::PortAllocator* port_allocator, - const NetworkSettings& network_settings, - TransportRole role) - : port_allocator_(port_allocator), - network_settings_(network_settings), - role_(role), - delegate_(nullptr), +IceTransportChannel::IceTransportChannel( + scoped_refptr<TransportContext> transport_context) + : transport_context_(transport_context), ice_username_fragment_( rtc::CreateRandomString(cricket::ICE_UFRAG_LENGTH)), - can_start_(false), connect_attempts_left_(kMaxReconnectAttempts), weak_factory_(this) { DCHECK(!ice_username_fragment_.empty()); @@ -66,32 +62,11 @@ IceTransportChannel::~IceTransportChannel() { delegate_->OnTransportDeleted(this); - if (channel_.get()) { - base::ThreadTaskRunnerHandle::Get()->DeleteSoon( - FROM_HERE, channel_.release()); - } -} - -void IceTransportChannel::OnCanStart() { - DCHECK(thread_checker_.CalledOnValidThread()); - - DCHECK(!can_start_); - can_start_ = true; - - // If Connect() has been called then start connection. - if (!callback_.is_null()) - DoStart(); - - // Pass pending ICE credentials and candidates to the channel. - if (!remote_ice_username_fragment_.empty()) { - channel_->SetRemoteIceCredentials(remote_ice_username_fragment_, - remote_ice_password_); - } - - while (!pending_candidates_.empty()) { - channel_->AddRemoteCandidate(pending_candidates_.front()); - pending_candidates_.pop_front(); - } + auto task_runner = base::ThreadTaskRunnerHandle::Get(); + if (channel_) + task_runner->DeleteSoon(FROM_HERE, channel_.release()); + if (port_allocator_) + task_runner->DeleteSoon(FROM_HERE, port_allocator_.release()); } void IceTransportChannel::Connect(const std::string& name, @@ -107,20 +82,24 @@ void IceTransportChannel::Connect(const std::string& name, delegate_ = delegate; callback_ = callback; - if (can_start_) - DoStart(); + transport_context_->CreatePortAllocator( + base::Bind(&IceTransportChannel::OnPortAllocatorCreated, + weak_factory_.GetWeakPtr())); } -void IceTransportChannel::DoStart() { +void IceTransportChannel::OnPortAllocatorCreated( + scoped_ptr<cricket::PortAllocator> port_allocator){ DCHECK(!channel_.get()); + port_allocator_ = port_allocator.Pass(); + // Create P2PTransportChannel, attach signal handlers and connect it. // TODO(sergeyu): Specify correct component ID for the channel. channel_.reset(new cricket::P2PTransportChannel( - std::string(), 0, nullptr, port_allocator_)); + std::string(), 0, nullptr, port_allocator_.get())); std::string ice_password = rtc::CreateRandomString(cricket::ICE_PWD_LENGTH); channel_->SetIceProtocolType(cricket::ICEPROTO_RFC5245); - channel_->SetIceRole((role_ == TransportRole::CLIENT) + channel_->SetIceRole((transport_context_->role() == TransportRole::CLIENT) ? cricket::ICEROLE_CONTROLLING : cricket::ICEROLE_CONTROLLED); delegate_->OnTransportIceCredentials(this, ice_username_fragment_, @@ -134,12 +113,23 @@ void IceTransportChannel::DoStart() { this, &IceTransportChannel::OnReceivingState); channel_->SignalWritableState.connect( this, &IceTransportChannel::OnWritableState); - channel_->set_incoming_only( - !(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_OUTGOING)); + channel_->set_incoming_only(!(transport_context_->network_settings().flags & + NetworkSettings::NAT_TRAVERSAL_OUTGOING)); channel_->Connect(); channel_->MaybeStartGathering(); + // Pass pending ICE credentials and candidates to the channel. + if (!remote_ice_username_fragment_.empty()) { + channel_->SetRemoteIceCredentials(remote_ice_username_fragment_, + remote_ice_password_); + } + + while (!pending_candidates_.empty()) { + channel_->AddRemoteCandidate(pending_candidates_.front()); + pending_candidates_.pop_front(); + } + --connect_attempts_left_; // Start reconnection timer. @@ -158,7 +148,7 @@ void IceTransportChannel::NotifyConnected() { } void IceTransportChannel::SetRemoteCredentials(const std::string& ufrag, - const std::string& password) { + const std::string& password) { DCHECK(thread_checker_.CalledOnValidThread()); remote_ice_username_fragment_ = ufrag; @@ -174,7 +164,7 @@ void IceTransportChannel::AddRemoteCandidate( // To enforce the no-relay setting, it's not enough to not produce relay // candidates. It's also necessary to discard remote relay candidates. - bool relay_allowed = (network_settings_.flags & + bool relay_allowed = (transport_context_->network_settings().flags & NetworkSettings::NAT_TRAVERSAL_RELAY) != 0; if (!relay_allowed && candidate.type() == cricket::RELAY_PORT_TYPE) return; diff --git a/remoting/protocol/ice_transport_channel.h b/remoting/protocol/ice_transport_channel.h index a0dd6e7..7bde3e5 100644 --- a/remoting/protocol/ice_transport_channel.h +++ b/remoting/protocol/ice_transport_channel.h @@ -26,6 +26,8 @@ class TransportChannelImpl; namespace remoting { namespace protocol { +class TransportContext; + class IceTransportChannel : public sigslot::has_slots<> { public: class Delegate { @@ -59,14 +61,10 @@ class IceTransportChannel : public sigslot::has_slots<> { typedef base::Callback<void(scoped_ptr<P2PDatagramSocket>)> ConnectedCallback; - IceTransportChannel(cricket::PortAllocator* port_allocator, - const NetworkSettings& network_settings, - TransportRole role); + explicit IceTransportChannel( + scoped_refptr<TransportContext> transport_context); ~IceTransportChannel() override; - // Called by IceTransport when it has fresh Jingle info. - void OnCanStart(); - // Connects the channel and calls the |callback| after that. void Connect(const std::string& name, Delegate* delegate, @@ -87,7 +85,9 @@ class IceTransportChannel : public sigslot::has_slots<> { bool is_connected() const; private: - void DoStart(); + void OnPortAllocatorCreated( + scoped_ptr<cricket::PortAllocator> port_allocator); + void NotifyConnected(); // Signal handlers for cricket::TransportChannel. @@ -107,16 +107,14 @@ class IceTransportChannel : public sigslot::has_slots<> { // Tries to connect by restarting ICE. Called by |reconnect_timer_|. void TryReconnect(); - cricket::PortAllocator* port_allocator_; - NetworkSettings network_settings_; - TransportRole role_; + scoped_refptr<TransportContext> transport_context_; std::string name_; - Delegate* delegate_; + Delegate* delegate_ = nullptr; ConnectedCallback callback_; std::string ice_username_fragment_; - bool can_start_; + scoped_ptr<cricket::PortAllocator> port_allocator_; std::string remote_ice_username_fragment_; std::string remote_ice_password_; diff --git a/remoting/protocol/ice_transport_factory.cc b/remoting/protocol/ice_transport_factory.cc deleted file mode 100644 index 19b14cb..0000000 --- a/remoting/protocol/ice_transport_factory.cc +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "remoting/protocol/ice_transport_factory.h" - -#include "base/single_thread_task_runner.h" -#include "base/thread_task_runner_handle.h" -#include "remoting/protocol/ice_transport.h" -#include "third_party/webrtc/p2p/client/httpportallocator.h" - -namespace remoting { -namespace protocol { - -// Get fresh STUN/Relay configuration every hour. -static const int kJingleInfoUpdatePeriodSeconds = 3600; - -IceTransportFactory::IceTransportFactory( - SignalStrategy* signal_strategy, - scoped_ptr<cricket::HttpPortAllocatorBase> port_allocator, - const NetworkSettings& network_settings, - TransportRole role) - : signal_strategy_(signal_strategy), - port_allocator_(port_allocator.Pass()), - network_settings_(network_settings), - role_(role) {} - -IceTransportFactory::~IceTransportFactory() { - // This method may be called in response to a libjingle signal, so - // libjingle objects must be deleted asynchronously. - scoped_refptr<base::SingleThreadTaskRunner> task_runner = - base::ThreadTaskRunnerHandle::Get(); - task_runner->DeleteSoon(FROM_HERE, port_allocator_.release()); -} - -scoped_ptr<Transport> IceTransportFactory::CreateTransport() { - scoped_ptr<IceTransport> result( - new IceTransport(port_allocator_.get(), network_settings_, role_)); - - EnsureFreshJingleInfo(); - - // If there is a pending |jingle_info_request_| delay starting the new - // transport until the request is finished. - if (jingle_info_request_) { - on_jingle_info_callbacks_.push_back(result->GetCanStartClosure()); - } else { - result->GetCanStartClosure().Run(); - } - - return result.Pass(); -} - -void IceTransportFactory::EnsureFreshJingleInfo() { - uint32 stun_or_relay_flags = NetworkSettings::NAT_TRAVERSAL_STUN | - NetworkSettings::NAT_TRAVERSAL_RELAY; - if (!(network_settings_.flags & stun_or_relay_flags) || - jingle_info_request_) { - return; - } - - if (last_jingle_info_update_time_.is_null() || - base::TimeTicks::Now() - last_jingle_info_update_time_ > - base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds)) { - jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); - jingle_info_request_->Send(base::Bind( - &IceTransportFactory::OnJingleInfo, base::Unretained(this))); - } -} - -void IceTransportFactory::OnJingleInfo( - const std::string& relay_token, - const std::vector<std::string>& relay_hosts, - const std::vector<rtc::SocketAddress>& stun_hosts) { - if (!relay_token.empty() && !relay_hosts.empty()) { - port_allocator_->SetRelayHosts(relay_hosts); - port_allocator_->SetRelayToken(relay_token); - } - if (!stun_hosts.empty()) { - port_allocator_->SetStunHosts(stun_hosts); - } - - jingle_info_request_.reset(); - if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) - last_jingle_info_update_time_ = base::TimeTicks::Now(); - - while (!on_jingle_info_callbacks_.empty()) { - on_jingle_info_callbacks_.begin()->Run(); - on_jingle_info_callbacks_.pop_front(); - } -} - -} // namespace protocol -} // namespace remoting diff --git a/remoting/protocol/ice_transport_factory.h b/remoting/protocol/ice_transport_factory.h deleted file mode 100644 index deaf72e..0000000 --- a/remoting/protocol/ice_transport_factory.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef REMOTING_PROTOCOL_ICE_TRANSPORT_FACTORY_H_ -#define REMOTING_PROTOCOL_ICE_TRANSPORT_FACTORY_H_ - -#include <list> - -#include "base/macros.h" -#include "remoting/protocol/network_settings.h" -#include "remoting/protocol/transport.h" -#include "remoting/signaling/jingle_info_request.h" - -namespace cricket { -class HttpPortAllocatorBase; -} // namespace cricket - -namespace remoting { - -class SignalStrategy; - -namespace protocol { - -class IceTransportFactory : public TransportFactory { - public: - IceTransportFactory( - SignalStrategy* signal_strategy, - scoped_ptr<cricket::HttpPortAllocatorBase> port_allocator, - const NetworkSettings& network_settings, - TransportRole role); - ~IceTransportFactory() override; - - // TransportFactory interface. - scoped_ptr<Transport> CreateTransport() override; - - private: - void EnsureFreshJingleInfo(); - void OnJingleInfo(const std::string& relay_token, - const std::vector<std::string>& relay_hosts, - const std::vector<rtc::SocketAddress>& stun_hosts); - - SignalStrategy* signal_strategy_; - scoped_ptr<cricket::HttpPortAllocatorBase> port_allocator_; - NetworkSettings network_settings_; - TransportRole role_; - - base::TimeTicks last_jingle_info_update_time_; - scoped_ptr<JingleInfoRequest> jingle_info_request_; - - // When there is an active |jingle_info_request_| stores list of callbacks to - // be called once the |jingle_info_request_| is finished. - std::list<base::Closure> on_jingle_info_callbacks_; - - DISALLOW_COPY_AND_ASSIGN(IceTransportFactory); -}; - -} // namespace protocol -} // namespace remoting - -#endif // REMOTING_PROTOCOL_ICE_TRANSPORT_FACTORY_H_ diff --git a/remoting/protocol/ice_transport_unittest.cc b/remoting/protocol/ice_transport_unittest.cc index 110a6d7..1f881a6 100644 --- a/remoting/protocol/ice_transport_unittest.cc +++ b/remoting/protocol/ice_transport_unittest.cc @@ -17,6 +17,7 @@ #include "remoting/protocol/fake_authenticator.h" #include "remoting/protocol/p2p_stream_socket.h" #include "remoting/protocol/stream_channel_factory.h" +#include "remoting/protocol/transport_context.h" #include "remoting/signaling/fake_signal_strategy.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -128,20 +129,19 @@ class IceTransportTest : public testing::Test { } void InitializeConnection() { - port_allocator_ = ChromiumPortAllocator::Create(nullptr, network_settings_); - - host_transport_.reset(new IceTransport( - port_allocator_.get(), network_settings_, TransportRole::SERVER)); - host_transport_->GetCanStartClosure().Run(); - + host_transport_.reset(new IceTransport(new TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), + network_settings_, TransportRole::SERVER))); if (!host_authenticator_) { host_authenticator_.reset(new FakeAuthenticator( FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, true)); } - client_transport_.reset(new IceTransport( - port_allocator_.get(), network_settings_, TransportRole::CLIENT)); - client_transport_->GetCanStartClosure().Run(); + client_transport_.reset(new IceTransport(new TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), + network_settings_, TransportRole::CLIENT))); if (!client_authenticator_) { client_authenticator_.reset(new FakeAuthenticator( FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, true)); @@ -208,14 +208,10 @@ class IceTransportTest : public testing::Test { base::TimeDelta transport_info_delay_; - scoped_ptr<cricket::PortAllocator> port_allocator_; - - // scoped_ptr<IceTransportFactory> host_transport_factory_; scoped_ptr<IceTransport> host_transport_; TestTransportEventHandler host_event_handler_; scoped_ptr<FakeAuthenticator> host_authenticator_; - // scoped_ptr<IceTransportFactory> client_transport_factory_; scoped_ptr<IceTransport> client_transport_; TestTransportEventHandler client_event_handler_; scoped_ptr<FakeAuthenticator> client_authenticator_; diff --git a/remoting/protocol/jingle_session_unittest.cc b/remoting/protocol/jingle_session_unittest.cc index c1338951..03cb533 100644 --- a/remoting/protocol/jingle_session_unittest.cc +++ b/remoting/protocol/jingle_session_unittest.cc @@ -19,9 +19,10 @@ #include "remoting/protocol/chromium_port_allocator.h" #include "remoting/protocol/connection_tester.h" #include "remoting/protocol/fake_authenticator.h" -#include "remoting/protocol/ice_transport_factory.h" +#include "remoting/protocol/ice_transport.h" #include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/network_settings.h" +#include "remoting/protocol/transport_context.h" #include "remoting/signaling/fake_signal_strategy.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -102,11 +103,10 @@ class JingleSessionTest : public testing::Test { FakeSignalStrategy::Connect(host_signal_strategy_.get(), client_signal_strategy_.get()); - scoped_ptr<TransportFactory> host_transport(new IceTransportFactory( - nullptr, - ChromiumPortAllocator::Create(nullptr, network_settings_).Pass(), - network_settings_, TransportRole::SERVER)); - host_server_.reset(new JingleSessionManager(host_transport.Pass())); + host_server_.reset(new JingleSessionManager( + make_scoped_ptr(new IceTransportFactory(new TransportContext( + nullptr, make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), + network_settings_, TransportRole::SERVER))))); host_server_->Init(host_signal_strategy_.get(), &host_server_listener_); scoped_ptr<AuthenticatorFactory> factory( @@ -114,12 +114,10 @@ class JingleSessionTest : public testing::Test { messages_till_start, auth_action, true)); host_server_->set_authenticator_factory(factory.Pass()); - scoped_ptr<TransportFactory> client_transport(new IceTransportFactory( - nullptr, - ChromiumPortAllocator::Create(nullptr, network_settings_).Pass(), - network_settings_, TransportRole::CLIENT)); - client_server_.reset( - new JingleSessionManager(client_transport.Pass())); + client_server_.reset(new JingleSessionManager( + make_scoped_ptr(new IceTransportFactory(new TransportContext( + nullptr, make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), + network_settings_, TransportRole::CLIENT))))); client_server_->Init(client_signal_strategy_.get(), &client_server_listener_); } diff --git a/remoting/protocol/port_allocator_factory.h b/remoting/protocol/port_allocator_factory.h index 741ccca..b08df71 100644 --- a/remoting/protocol/port_allocator_factory.h +++ b/remoting/protocol/port_allocator_factory.h @@ -5,7 +5,11 @@ #ifndef REMOTING_PROTOCOL_PORT_ALLOCATOR_FACTORY_H_ #define REMOTING_PROTOCOL_PORT_ALLOCATOR_FACTORY_H_ -#include "third_party/webrtc/p2p/base/portallocator.h" +#include "base/memory/scoped_ptr.h" + +namespace cricket { +class HttpPortAllocatorBase; +} // namespace cricket namespace remoting { namespace protocol { @@ -14,7 +18,7 @@ namespace protocol { // for ICE negotiation. class PortAllocatorFactory { public: - virtual cricket::PortAllocator* CreatePortAllocator() = 0; + virtual scoped_ptr<cricket::HttpPortAllocatorBase> CreatePortAllocator() = 0; virtual ~PortAllocatorFactory() {} }; diff --git a/remoting/protocol/transport_context.cc b/remoting/protocol/transport_context.cc new file mode 100644 index 0000000..251a028 --- /dev/null +++ b/remoting/protocol/transport_context.cc @@ -0,0 +1,122 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/protocol/transport_context.h" + +#include "base/bind.h" +#include "base/location.h" +#include "base/single_thread_task_runner.h" +#include "base/thread_task_runner_handle.h" +#include "remoting/protocol/port_allocator_factory.h" +#include "third_party/webrtc/p2p/client/httpportallocator.h" + +namespace remoting { +namespace protocol { + +// Get fresh STUN/Relay configuration every hour. +static const int kJingleInfoUpdatePeriodSeconds = 3600; + +TransportContext::TransportContext( + SignalStrategy* signal_strategy, + scoped_ptr<PortAllocatorFactory> port_allocator_factory, + const NetworkSettings& network_settings, + TransportRole role) + : signal_strategy_(signal_strategy), + port_allocator_factory_(port_allocator_factory.Pass()), + network_settings_(network_settings), + role_(role) {} + +TransportContext::~TransportContext() {} + +void TransportContext::Prepare() { + EnsureFreshJingleInfo(); +} + +void TransportContext::CreatePortAllocator( + const CreatePortAllocatorCallback& callback) { + EnsureFreshJingleInfo(); + + // If there is a pending |jingle_info_request_| delay starting the new + // transport until the request is finished. + if (jingle_info_request_) { + pending_port_allocator_requests_.push_back(callback); + } else { + callback.Run(CreatePortAllocatorInternal()); + } +} + +void TransportContext::EnsureFreshJingleInfo() { + // Check if request is already pending. + if (jingle_info_request_) + return; + + // Don't need to make jingleinfo request if both STUN and Relay are disabled. + if ((network_settings_.flags & (NetworkSettings::NAT_TRAVERSAL_STUN | + NetworkSettings::NAT_TRAVERSAL_RELAY)) == 0) { + return; + } + + if (last_jingle_info_update_time_.is_null() || + base::TimeTicks::Now() - last_jingle_info_update_time_ > + base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds)) { + jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); + jingle_info_request_->Send(base::Bind( + &TransportContext::OnJingleInfo, base::Unretained(this))); + } +} + +void TransportContext::OnJingleInfo( + const std::string& relay_token, + const std::vector<std::string>& relay_hosts, + const std::vector<rtc::SocketAddress>& stun_hosts) { + relay_token_ = relay_token; + relay_hosts_ = relay_hosts; + stun_hosts_ = stun_hosts; + + jingle_info_request_.reset(); + if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) + last_jingle_info_update_time_ = base::TimeTicks::Now(); + + while (!pending_port_allocator_requests_.empty()) { + pending_port_allocator_requests_.begin()->Run( + CreatePortAllocatorInternal()); + pending_port_allocator_requests_.pop_front(); + } +} + +scoped_ptr<cricket::PortAllocator> +TransportContext::CreatePortAllocatorInternal() { + scoped_ptr<cricket::HttpPortAllocatorBase> result = + port_allocator_factory_->CreatePortAllocator(); + + if (!relay_token_.empty() && !relay_hosts_.empty()) { + result->SetRelayHosts(relay_hosts_); + result->SetRelayToken(relay_token_); + } + if (!stun_hosts_.empty()) + result->SetStunHosts(stun_hosts_); + + // We always use PseudoTcp to provide a reliable channel. It provides poor + // performance when combined with TCP-based transport, so we have to disable + // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username + // fragment is shared between all candidates. + int flags = cricket::PORTALLOCATOR_DISABLE_TCP | + cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | + cricket::PORTALLOCATOR_ENABLE_IPV6; + + if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_STUN)) + flags |= cricket::PORTALLOCATOR_DISABLE_STUN; + + if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_RELAY)) + flags |= cricket::PORTALLOCATOR_DISABLE_RELAY; + + result->set_flags(flags); + result->SetPortRange(network_settings_.port_range.min_port, + network_settings_.port_range.max_port); + + return result.Pass(); +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/transport_context.h b/remoting/protocol/transport_context.h new file mode 100644 index 0000000..faac2d2 --- /dev/null +++ b/remoting/protocol/transport_context.h @@ -0,0 +1,92 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_PROTOCOL_TRANSPORT_CONTEXT_H_ +#define REMOTING_PROTOCOL_TRANSPORT_CONTEXT_H_ + +#include <list> +#include <string> +#include <vector> + +#include "base/macros.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "remoting/protocol/network_settings.h" +#include "remoting/protocol/transport.h" +#include "remoting/signaling/jingle_info_request.h" + +namespace cricket { +class PortAllocator; +} // namespace cricket + +namespace remoting { + +class SignalStrategy; + +namespace protocol { + +class PortAllocatorFactory; + +// TransportContext is responsible for storing all parameters required for +// P2P transport initialization. It's also responsible for making JingleInfo +// request and creation of PortAllocators configured according to the JingleInfo +// result. +class TransportContext : public base::RefCountedThreadSafe<TransportContext> { + public: + typedef base::Callback<void(scoped_ptr<cricket::PortAllocator> + port_allocator)> CreatePortAllocatorCallback; + + TransportContext( + SignalStrategy* signal_strategy, + scoped_ptr<PortAllocatorFactory> port_allocator_factory, + const NetworkSettings& network_settings, + TransportRole role); + + // Prepares to create new PortAllocator instances. It may be called while + // connection is being negotiated to minimize the chance that the following + // CreatePortAllocator() will be blocking. + void Prepare(); + + // Creates new PortAllocator making sure that it has correct STUN and TURN + // information. + void CreatePortAllocator(const CreatePortAllocatorCallback& callback); + + const NetworkSettings& network_settings() { return network_settings_; } + TransportRole role() { return role_; } + + private: + friend class base::RefCountedThreadSafe<TransportContext>; + + ~TransportContext(); + + void EnsureFreshJingleInfo(); + void OnJingleInfo(const std::string& relay_token, + const std::vector<std::string>& relay_hosts, + const std::vector<rtc::SocketAddress>& stun_hosts); + + scoped_ptr<cricket::PortAllocator> CreatePortAllocatorInternal(); + + SignalStrategy* signal_strategy_; + scoped_ptr<PortAllocatorFactory> port_allocator_factory_; + NetworkSettings network_settings_; + TransportRole role_; + + base::TimeTicks last_jingle_info_update_time_; + scoped_ptr<JingleInfoRequest> jingle_info_request_; + + std::string relay_token_; + std::vector<std::string> relay_hosts_; + std::vector<rtc::SocketAddress> stun_hosts_; + + // When there is an active |jingle_info_request_| stores list of callbacks to + // be called once the |jingle_info_request_| is finished. + std::list<CreatePortAllocatorCallback> pending_port_allocator_requests_; + + DISALLOW_COPY_AND_ASSIGN(TransportContext); +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_TRANSPORT_CONTEXT_H_ diff --git a/remoting/protocol/webrtc_transport.cc b/remoting/protocol/webrtc_transport.cc index 2c82aab..e4fd5c8f 100644 --- a/remoting/protocol/webrtc_transport.cc +++ b/remoting/protocol/webrtc_transport.cc @@ -10,6 +10,7 @@ #include "base/task_runner_util.h" #include "base/thread_task_runner_handle.h" #include "jingle/glue/thread_wrapper.h" +#include "remoting/protocol/transport_context.h" #include "third_party/libjingle/source/talk/app/webrtc/test/fakeconstraints.h" #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" #include "third_party/webrtc/modules/audio_device/include/fake_audio_device.h" @@ -99,11 +100,10 @@ class SetSessionDescriptionObserver } // namespace -WebrtcTransport::WebrtcTransport(rtc::Thread* worker_thread, - PortAllocatorFactory* port_allocator_factory, - TransportRole role) - : port_allocator_factory_(port_allocator_factory), - role_(role), +WebrtcTransport::WebrtcTransport( + rtc::Thread* worker_thread, + scoped_refptr<TransportContext> transport_context) + : transport_context_(transport_context), worker_thread_(worker_thread), weak_factory_(this) {} @@ -114,8 +114,14 @@ void WebrtcTransport::Start(EventHandler* event_handler, DCHECK(thread_checker_.CalledOnValidThread()); event_handler_ = event_handler; - // TODO(sergeyu): Use the |authenticator| to authenticate PeerConnection. + + transport_context_->CreatePortAllocator(base::Bind( + &WebrtcTransport::OnPortAllocatorCreated, weak_factory_.GetWeakPtr())); +} + +void WebrtcTransport::OnPortAllocatorCreated( + scoped_ptr<cricket::PortAllocator> port_allocator) { jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); // TODO(sergeyu): Investigate if it's possible to avoid Send(). @@ -136,16 +142,15 @@ void WebrtcTransport::Start(EventHandler* event_handler, constraints.AddMandatory(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp, webrtc::MediaConstraintsInterface::kValueTrue); - rtc::scoped_ptr<cricket::PortAllocator> port_allocator( - port_allocator_factory_->CreatePortAllocator()); - peer_connection_ = peer_connection_factory_->CreatePeerConnection( - rtc_config, &constraints, std::move(port_allocator), nullptr, this); + rtc_config, &constraints, + rtc::scoped_ptr<cricket::PortAllocator>(port_allocator.release()), + nullptr, this); - data_stream_adapter_.Initialize(peer_connection_, - role_ == TransportRole::SERVER); + data_stream_adapter_.Initialize( + peer_connection_, transport_context_->role() == TransportRole::SERVER); - if (role_ == TransportRole::SERVER) + if (transport_context_->role() == TransportRole::SERVER) RequestNegotiation(); } @@ -162,7 +167,7 @@ bool WebrtcTransport::ProcessTransportInfo(XmlElement* transport_info) { QName(kTransportNamespace, "session-description")); if (session_description) { webrtc::PeerConnectionInterface::SignalingState expected_state = - role_ == TransportRole::CLIENT + transport_context_->role() == TransportRole::CLIENT ? webrtc::PeerConnectionInterface::kStable : webrtc::PeerConnectionInterface::kHaveLocalOffer; if (peer_connection_->signaling_state() != expected_state) { @@ -365,7 +370,7 @@ void WebrtcTransport::OnDataChannel( void WebrtcTransport::OnRenegotiationNeeded() { DCHECK(thread_checker_.CalledOnValidThread()); - if (role_ == TransportRole::SERVER) { + if (transport_context_->role() == TransportRole::SERVER) { RequestNegotiation(); } else { // TODO(sergeyu): Is it necessary to support renegotiation initiated by the @@ -375,7 +380,7 @@ void WebrtcTransport::OnRenegotiationNeeded() { } void WebrtcTransport::RequestNegotiation() { - DCHECK(role_ == TransportRole::SERVER); + DCHECK(transport_context_->role() == TransportRole::SERVER); if (!negotiation_pending_) { negotiation_pending_ = true; @@ -440,7 +445,7 @@ void WebrtcTransport::EnsurePendingTransportInfoMessage() { } void WebrtcTransport::SendOffer() { - DCHECK(role_ == TransportRole::SERVER); + DCHECK(transport_context_->role() == TransportRole::SERVER); DCHECK(negotiation_pending_); negotiation_pending_ = false; @@ -488,19 +493,15 @@ void WebrtcTransport::AddPendingCandidatesIfPossible() { WebrtcTransportFactory::WebrtcTransportFactory( rtc::Thread* worker_thread, - SignalStrategy* signal_strategy, - scoped_ptr<PortAllocatorFactory> port_allocator_factory, - TransportRole role) + scoped_refptr<TransportContext> transport_context) : worker_thread_(worker_thread), - signal_strategy_(signal_strategy), - port_allocator_factory_(std::move(port_allocator_factory)), - role_(role) {} + transport_context_(transport_context) {} WebrtcTransportFactory::~WebrtcTransportFactory() {} scoped_ptr<Transport> WebrtcTransportFactory::CreateTransport() { - return make_scoped_ptr(new WebrtcTransport( - worker_thread_, port_allocator_factory_.get(), role_)); + return make_scoped_ptr( + new WebrtcTransport(worker_thread_, transport_context_.get())); } } // namespace protocol diff --git a/remoting/protocol/webrtc_transport.h b/remoting/protocol/webrtc_transport.h index 6fc79d7..827d2ef 100644 --- a/remoting/protocol/webrtc_transport.h +++ b/remoting/protocol/webrtc_transport.h @@ -25,12 +25,13 @@ class FakeAudioDeviceModule; namespace remoting { namespace protocol { +class TransportContext; + class WebrtcTransport : public Transport, public webrtc::PeerConnectionObserver { public: WebrtcTransport(rtc::Thread* worker_thread, - PortAllocatorFactory* port_allocator_factory, - TransportRole role); + scoped_refptr<TransportContext> transport_context); ~WebrtcTransport() override; webrtc::PeerConnectionInterface* peer_connection() { @@ -49,6 +50,9 @@ class WebrtcTransport : public Transport, WebrtcTransport* AsWebrtcTransport() override; private: + void OnPortAllocatorCreated( + scoped_ptr<cricket::PortAllocator> port_allocator); + void OnLocalSessionDescriptionCreated( scoped_ptr<webrtc::SessionDescriptionInterface> description, const std::string& error); @@ -80,8 +84,7 @@ class WebrtcTransport : public Transport, base::ThreadChecker thread_checker_; - PortAllocatorFactory* port_allocator_factory_; - TransportRole role_; + scoped_refptr<TransportContext> transport_context_; EventHandler* event_handler_ = nullptr; rtc::Thread* worker_thread_; @@ -110,11 +113,8 @@ class WebrtcTransport : public Transport, class WebrtcTransportFactory : public TransportFactory { public: - WebrtcTransportFactory( - rtc::Thread* worker_thread, - SignalStrategy* signal_strategy, - scoped_ptr<PortAllocatorFactory> port_allocator_factory, - TransportRole role); + WebrtcTransportFactory(rtc::Thread* worker_thread, + scoped_refptr<TransportContext> transport_context); ~WebrtcTransportFactory() override; // TransportFactory interface. @@ -122,9 +122,7 @@ class WebrtcTransportFactory : public TransportFactory { private: rtc::Thread* worker_thread_; - SignalStrategy* signal_strategy_; - scoped_ptr<PortAllocatorFactory> port_allocator_factory_; - TransportRole role_; + scoped_refptr<TransportContext> transport_context_; DISALLOW_COPY_AND_ASSIGN(WebrtcTransportFactory); }; diff --git a/remoting/protocol/webrtc_transport_unittest.cc b/remoting/protocol/webrtc_transport_unittest.cc index 1797a7e..82fb6d3 100644 --- a/remoting/protocol/webrtc_transport_unittest.cc +++ b/remoting/protocol/webrtc_transport_unittest.cc @@ -9,11 +9,12 @@ #include "jingle/glue/thread_wrapper.h" #include "net/base/io_buffer.h" #include "net/url_request/url_request_context_getter.h" -#include "remoting/protocol/chromium_port_allocator_factory.h" +#include "remoting/protocol/chromium_port_allocator.h" #include "remoting/protocol/connection_tester.h" #include "remoting/protocol/fake_authenticator.h" #include "remoting/protocol/network_settings.h" #include "remoting/protocol/p2p_stream_socket.h" +#include "remoting/protocol/transport_context.h" #include "remoting/signaling/fake_signal_strategy.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" @@ -90,17 +91,21 @@ class WebrtcTransportTest : public testing::Test { signal_strategy_.reset(new FakeSignalStrategy(kTestJid)); host_transport_factory_.reset(new WebrtcTransportFactory( - jingle_glue::JingleThreadWrapper::current(), signal_strategy_.get(), - ChromiumPortAllocatorFactory::Create(network_settings_, nullptr), - TransportRole::SERVER)); + jingle_glue::JingleThreadWrapper::current(), + new TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), + network_settings_, TransportRole::SERVER))); host_transport_ = host_transport_factory_->CreateTransport(); host_authenticator_.reset(new FakeAuthenticator( FakeAuthenticator::HOST, 0, FakeAuthenticator::ACCEPT, false)); client_transport_factory_.reset(new WebrtcTransportFactory( - jingle_glue::JingleThreadWrapper::current(), signal_strategy_.get(), - ChromiumPortAllocatorFactory::Create(network_settings_, nullptr), - TransportRole::CLIENT)); + jingle_glue::JingleThreadWrapper::current(), + new TransportContext( + signal_strategy_.get(), + make_scoped_ptr(new ChromiumPortAllocatorFactory(nullptr)), + network_settings_, TransportRole::CLIENT))); client_transport_ = client_transport_factory_->CreateTransport(); host_authenticator_.reset(new FakeAuthenticator( FakeAuthenticator::CLIENT, 0, FakeAuthenticator::ACCEPT, false)); diff --git a/remoting/remoting_host_srcs.gypi b/remoting/remoting_host_srcs.gypi index 6df087a..766d556 100644 --- a/remoting/remoting_host_srcs.gypi +++ b/remoting/remoting_host_srcs.gypi @@ -218,8 +218,6 @@ 'host/screen_resolution.h', 'host/server_log_entry_host.cc', 'host/server_log_entry_host.h', - 'host/session_manager_factory.cc', - 'host/session_manager_factory.h', 'host/shaped_desktop_capturer.cc', 'host/shaped_desktop_capturer.h', 'host/shutdown_watchdog.cc', diff --git a/remoting/remoting_srcs.gypi b/remoting/remoting_srcs.gypi index b5e2153..aac6cc7 100644 --- a/remoting/remoting_srcs.gypi +++ b/remoting/remoting_srcs.gypi @@ -118,8 +118,6 @@ 'protocol/host_video_dispatcher.h', 'protocol/ice_transport_channel.cc', 'protocol/ice_transport_channel.h', - 'protocol/ice_transport_factory.cc', - 'protocol/ice_transport_factory.h', 'protocol/ice_transport.cc', 'protocol/ice_transport.h', 'protocol/input_event_tracker.cc', @@ -193,6 +191,8 @@ 'protocol/token_validator.h', 'protocol/transport.cc', 'protocol/transport.h', + 'protocol/transport_context.cc', + 'protocol/transport_context.h', 'protocol/v2_authenticator.cc', 'protocol/v2_authenticator.h', 'protocol/video_stub.h', @@ -204,8 +204,6 @@ 'protocol/capture_scheduler.h', 'protocol/chromium_port_allocator.cc', 'protocol/chromium_port_allocator.h', - 'protocol/chromium_port_allocator_factory.cc', - 'protocol/chromium_port_allocator_factory.h', 'protocol/chromium_socket_factory.cc', 'protocol/chromium_socket_factory.h', 'protocol/ice_connection_to_client.cc', diff --git a/remoting/test/fake_port_allocator.cc b/remoting/test/fake_port_allocator.cc index d63243f..972ded5 100644 --- a/remoting/test/fake_port_allocator.cc +++ b/remoting/test/fake_port_allocator.cc @@ -7,6 +7,7 @@ #include "remoting/test/fake_network_dispatcher.h" #include "remoting/test/fake_network_manager.h" #include "remoting/test/fake_socket_factory.h" +#include "third_party/webrtc/p2p/client/basicportallocator.h" namespace remoting { @@ -82,35 +83,10 @@ void FakePortAllocatorSession::SendSessionRequest( } // namespace -// static -scoped_ptr<FakePortAllocator> FakePortAllocator::Create( - scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher) { - scoped_ptr<FakePacketSocketFactory> socket_factory( - new FakePacketSocketFactory(fake_network_dispatcher.get())); - scoped_ptr<rtc::NetworkManager> network_manager( - new FakeNetworkManager(socket_factory->GetAddress())); - - return make_scoped_ptr( - new FakePortAllocator(network_manager.Pass(), socket_factory.Pass())); -} - -FakePortAllocator::FakePortAllocator( - scoped_ptr<rtc::NetworkManager> network_manager, - scoped_ptr<FakePacketSocketFactory> socket_factory) - : HttpPortAllocatorBase(network_manager.get(), - socket_factory.get(), - std::string()), - network_manager_(network_manager.Pass()), - socket_factory_(socket_factory.Pass()) { - set_flags(cricket::PORTALLOCATOR_DISABLE_TCP | - cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | - cricket::PORTALLOCATOR_ENABLE_IPV6 | - cricket::PORTALLOCATOR_DISABLE_STUN | - cricket::PORTALLOCATOR_DISABLE_RELAY); -} - -FakePortAllocator::~FakePortAllocator() { -} +FakePortAllocator::FakePortAllocator(rtc::NetworkManager* network_manager, + FakePacketSocketFactory* socket_factory) + : HttpPortAllocatorBase(network_manager, socket_factory, std::string()) {} +FakePortAllocator::~FakePortAllocator() {} cricket::PortAllocatorSession* FakePortAllocator::CreateSessionInternal( const std::string& content_name, @@ -122,4 +98,19 @@ cricket::PortAllocatorSession* FakePortAllocator::CreateSessionInternal( stun_hosts(), relay_hosts(), relay_token()); } +FakePortAllocatorFactory::FakePortAllocatorFactory( + scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher) { + socket_factory_.reset( + new FakePacketSocketFactory(fake_network_dispatcher.get())); + network_manager_.reset(new FakeNetworkManager(socket_factory_->GetAddress())); +} + +FakePortAllocatorFactory::~FakePortAllocatorFactory() {} + +scoped_ptr<cricket::HttpPortAllocatorBase> +FakePortAllocatorFactory::CreatePortAllocator() { + return make_scoped_ptr( + new FakePortAllocator(network_manager_.get(), socket_factory_.get())); +} + } // namespace remoting diff --git a/remoting/test/fake_port_allocator.h b/remoting/test/fake_port_allocator.h index af6ec3f8..d848dfd 100644 --- a/remoting/test/fake_port_allocator.h +++ b/remoting/test/fake_port_allocator.h @@ -9,8 +9,13 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" +#include "remoting/protocol/port_allocator_factory.h" #include "third_party/webrtc/p2p/client/httpportallocator.h" +namespace rtc { +class NetworkManager; +} // namespace rtc + namespace remoting { class FakeNetworkDispatcher; @@ -18,13 +23,10 @@ class FakePacketSocketFactory; class FakePortAllocator : public cricket::HttpPortAllocatorBase { public: - static scoped_ptr<FakePortAllocator> Create( - scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher); - + FakePortAllocator(rtc::NetworkManager* network_manager, + FakePacketSocketFactory* socket_factory); ~FakePortAllocator() override; - FakePacketSocketFactory* socket_factory() { return socket_factory_.get(); } - // cricket::BasicPortAllocator overrides. cricket::PortAllocatorSession* CreateSessionInternal( const std::string& content_name, @@ -33,13 +35,25 @@ class FakePortAllocator : public cricket::HttpPortAllocatorBase { const std::string& ice_password) override; private: - FakePortAllocator(scoped_ptr<rtc::NetworkManager> network_manager, - scoped_ptr<FakePacketSocketFactory> socket_factory); + DISALLOW_COPY_AND_ASSIGN(FakePortAllocator); +}; +class FakePortAllocatorFactory : public protocol::PortAllocatorFactory { + public: + FakePortAllocatorFactory( + scoped_refptr<FakeNetworkDispatcher> fake_network_dispatcher); + ~FakePortAllocatorFactory() override; + + FakePacketSocketFactory* socket_factory() { return socket_factory_.get(); } + + // PortAllocatorFactory interface. + scoped_ptr<cricket::HttpPortAllocatorBase> CreatePortAllocator() override; + + private: scoped_ptr<rtc::NetworkManager> network_manager_; scoped_ptr<FakePacketSocketFactory> socket_factory_; - DISALLOW_COPY_AND_ASSIGN(FakePortAllocator); + DISALLOW_COPY_AND_ASSIGN(FakePortAllocatorFactory); }; } // namespace remoting diff --git a/remoting/test/protocol_perftest.cc b/remoting/test/protocol_perftest.cc index 61294da..92cc8fe 100644 --- a/remoting/test/protocol_perftest.cc +++ b/remoting/test/protocol_perftest.cc @@ -22,11 +22,12 @@ #include "remoting/host/chromoting_host.h" #include "remoting/host/chromoting_host_context.h" #include "remoting/host/fake_desktop_environment.h" -#include "remoting/protocol/ice_transport_factory.h" +#include "remoting/protocol/ice_transport.h" #include "remoting/protocol/jingle_session_manager.h" #include "remoting/protocol/me2me_host_authenticator_factory.h" #include "remoting/protocol/negotiating_client_authenticator.h" #include "remoting/protocol/session_config.h" +#include "remoting/protocol/transport_context.h" #include "remoting/protocol/video_frame_pump.h" #include "remoting/signaling/fake_signal_strategy.h" #include "remoting/test/fake_network_dispatcher.h" @@ -224,21 +225,22 @@ class ProtocolPerfTest protocol::NetworkSettings network_settings( protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING); - scoped_ptr<FakePortAllocator> port_allocator( - FakePortAllocator::Create(fake_network_dispatcher_)); - port_allocator->socket_factory()->SetBandwidth(GetParam().bandwidth, - GetParam().max_buffers); - port_allocator->socket_factory()->SetLatency(GetParam().latency_average, - GetParam().latency_stddev); - port_allocator->socket_factory()->set_out_of_order_rate( + scoped_ptr<FakePortAllocatorFactory> port_allocator_factory( + new FakePortAllocatorFactory(fake_network_dispatcher_)); + port_allocator_factory->socket_factory()->SetBandwidth( + GetParam().bandwidth, GetParam().max_buffers); + port_allocator_factory->socket_factory()->SetLatency( + GetParam().latency_average, GetParam().latency_stddev); + port_allocator_factory->socket_factory()->set_out_of_order_rate( GetParam().out_of_order_rate); - scoped_ptr<protocol::TransportFactory> host_transport_factory( - new protocol::IceTransportFactory( - host_signaling_.get(), port_allocator.Pass(), network_settings, - protocol::TransportRole::SERVER)); + scoped_refptr<protocol::TransportContext> transport_context( + new protocol::TransportContext( + host_signaling_.get(), port_allocator_factory.Pass(), + network_settings, protocol::TransportRole::SERVER)); scoped_ptr<protocol::SessionManager> session_manager( - new protocol::JingleSessionManager(host_transport_factory.Pass())); + new protocol::JingleSessionManager(make_scoped_ptr( + new protocol::IceTransportFactory(transport_context)))); session_manager->set_protocol_config(protocol_config_->Clone()); // Encoder runs on a separate thread, main thread is used for everything @@ -267,7 +269,6 @@ class ProtocolPerfTest scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(key_base64); ASSERT_TRUE(key_pair.get()); - protocol::SharedSecretHash host_secret; host_secret.hash_function = protocol::AuthenticationMethod::NONE; host_secret.value = "123456"; @@ -294,18 +295,18 @@ class ProtocolPerfTest client_context_.reset( new ClientContext(base::ThreadTaskRunnerHandle::Get())); - scoped_ptr<FakePortAllocator> port_allocator( - FakePortAllocator::Create(fake_network_dispatcher_)); - port_allocator->socket_factory()->SetBandwidth(GetParam().bandwidth, - GetParam().max_buffers); - port_allocator->socket_factory()->SetLatency(GetParam().latency_average, - GetParam().latency_stddev); - port_allocator->socket_factory()->set_out_of_order_rate( + scoped_ptr<FakePortAllocatorFactory> port_allocator_factory( + new FakePortAllocatorFactory(fake_network_dispatcher_)); + port_allocator_factory->socket_factory()->SetBandwidth( + GetParam().bandwidth, GetParam().max_buffers); + port_allocator_factory->socket_factory()->SetLatency( + GetParam().latency_average, GetParam().latency_stddev); + port_allocator_factory->socket_factory()->set_out_of_order_rate( GetParam().out_of_order_rate); - scoped_ptr<protocol::TransportFactory> client_transport_factory( - new protocol::IceTransportFactory( - client_signaling_.get(), port_allocator.Pass(), network_settings, - protocol::TransportRole::CLIENT)); + scoped_refptr<protocol::TransportContext> transport_context( + new protocol::TransportContext( + host_signaling_.get(), port_allocator_factory.Pass(), + network_settings, protocol::TransportRole::SERVER)); std::vector<protocol::AuthenticationMethod> auth_methods; auth_methods.push_back(protocol::AuthenticationMethod::Spake2( @@ -322,7 +323,7 @@ class ProtocolPerfTest new ChromotingClient(client_context_.get(), this, this, nullptr)); client_->set_protocol_config(protocol_config_->Clone()); client_->Start(client_signaling_.get(), client_authenticator.Pass(), - client_transport_factory.Pass(), kHostJid, std::string()); + transport_context, kHostJid, std::string()); } void FetchPin( diff --git a/remoting/test/test_chromoting_client.cc b/remoting/test/test_chromoting_client.cc index e1beedf..da19a6d 100644 --- a/remoting/test/test_chromoting_client.cc +++ b/remoting/test/test_chromoting_client.cc @@ -19,11 +19,11 @@ #include "remoting/client/token_fetcher_proxy.h" #include "remoting/protocol/chromium_port_allocator.h" #include "remoting/protocol/host_stub.h" -#include "remoting/protocol/ice_transport_factory.h" #include "remoting/protocol/negotiating_client_authenticator.h" #include "remoting/protocol/network_settings.h" #include "remoting/protocol/session_config.h" #include "remoting/protocol/third_party_client_authenticator.h" +#include "remoting/protocol/transport_context.h" #include "remoting/signaling/xmpp_signal_strategy.h" #include "remoting/test/connection_setup_info.h" #include "remoting/test/test_video_renderer.h" @@ -122,14 +122,13 @@ void TestChromotingClient::StartConnection( protocol::NetworkSettings network_settings( protocol::NetworkSettings::NAT_TRAVERSAL_FULL); - scoped_ptr<protocol::ChromiumPortAllocator> port_allocator( - protocol::ChromiumPortAllocator::Create(request_context_getter, - network_settings)); + scoped_ptr<protocol::ChromiumPortAllocatorFactory> port_allocator_factory( + new protocol::ChromiumPortAllocatorFactory(request_context_getter)); - scoped_ptr<protocol::TransportFactory> transport_factory( - new protocol::IceTransportFactory( - signal_strategy_.get(), port_allocator.Pass(), network_settings, - protocol::TransportRole::CLIENT)); + scoped_refptr<protocol::TransportContext> transport_context( + new protocol::TransportContext( + signal_strategy_.get(), port_allocator_factory.Pass(), + network_settings, protocol::TransportRole::CLIENT)); scoped_ptr<protocol::ThirdPartyClientAuthenticator::TokenFetcher> token_fetcher(new TokenFetcherProxy( @@ -153,7 +152,7 @@ void TestChromotingClient::StartConnection( connection_setup_info.auth_methods)); chromoting_client_->Start( - signal_strategy_.get(), authenticator.Pass(), transport_factory.Pass(), + signal_strategy_.get(), authenticator.Pass(), transport_context, connection_setup_info.host_jid, connection_setup_info.capabilities); } |