From 3e0d61e2bb88eb80058b63ac6a8b9ebff64cc4eb Mon Sep 17 00:00:00 2001 From: "sergeyu@chromium.org" Date: Mon, 16 Dec 2013 18:23:01 +0000 Subject: Revert 240926 "Fix LibjingleTransportFactory to refresh STUN/Relay." > Fix LibjingleTransportFactory to refresh STUN/Relay. > > Previously chromoting host would get relay info only when started. Relay > token expires within several hours, which means relay session creation would > always fail from the host running for longer than several hours, and that > makes connection impossible in some configurations. Moved code that > gets Jingle info to LibjingleTransportFactory and it's responsible for > refreshing relay token every hour. > > BUG=318897 > R=rmsousa@chromium.org > > Review URL: https://codereview.chromium.org/98173006 TBR=sergeyu@chromium.org Review URL: https://codereview.chromium.org/116123003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@240936 0039d316-1c4b-4281-b951-d872f2087c98 --- remoting/client/jni/chromoting_jni_instance.cc | 3 +- remoting/client/plugin/chromoting_instance.cc | 4 +- remoting/host/it2me/it2me_host.cc | 2 +- remoting/host/remoting_me2me_host.cc | 2 +- remoting/host/session_manager_factory.cc | 15 ++- remoting/host/session_manager_factory.h | 4 +- remoting/jingle_glue/jingle_info_request.cc | 30 +---- remoting/jingle_glue/jingle_info_request.h | 18 +-- remoting/protocol/connection_to_host.cc | 3 +- remoting/protocol/jingle_session_manager.cc | 51 ++++++-- remoting/protocol/jingle_session_manager.h | 13 ++- remoting/protocol/jingle_session_unittest.cc | 12 +- remoting/protocol/libjingle_transport_factory.cc | 143 +++++------------------ remoting/protocol/libjingle_transport_factory.h | 35 ++---- remoting/protocol/session_manager.h | 1 + remoting/protocol/transport.h | 8 +- remoting/protocol/transport_config.cc | 17 +++ remoting/protocol/transport_config.h | 25 ++++ remoting/remoting.gyp | 2 + 19 files changed, 179 insertions(+), 209 deletions(-) create mode 100644 remoting/protocol/transport_config.cc create mode 100644 remoting/protocol/transport_config.h diff --git a/remoting/client/jni/chromoting_jni_instance.cc b/remoting/client/jni/chromoting_jni_instance.cc index 25398a8..339cea8 100644 --- a/remoting/client/jni/chromoting_jni_instance.cc +++ b/remoting/client/jni/chromoting_jni_instance.cc @@ -309,8 +309,7 @@ void ChromotingJniInstance::ConnectToHostOnNetworkThread() { scoped_ptr transport_factory( new protocol::LibjingleTransportFactory( - port_allocator.PassAs(), - network_settings)); + port_allocator.PassAs(), false)); client_->Start(signaling_.get(), transport_factory.Pass()); } diff --git a/remoting/client/plugin/chromoting_instance.cc b/remoting/client/plugin/chromoting_instance.cc index 46e30f5..ba69267 100644 --- a/remoting/client/plugin/chromoting_instance.cc +++ b/remoting/client/plugin/chromoting_instance.cc @@ -658,9 +658,7 @@ void ChromotingInstance::ConnectWithConfig(const ClientConfig& config, scoped_ptr port_allocator( PepperPortAllocator::Create(this)); scoped_ptr transport_factory( - new protocol::LibjingleTransportFactory( - signal_strategy_.get(), port_allocator.Pass(), - NetworkSettings(NetworkSettings::NAT_TRAVERSAL_ENABLED))); + new protocol::LibjingleTransportFactory(port_allocator.Pass(), false)); // Kick off the connection. client_->Start(signal_strategy_.get(), transport_factory.Pass()); diff --git a/remoting/host/it2me/it2me_host.cc b/remoting/host/it2me/it2me_host.cc index fec201f..9aa88fe 100644 --- a/remoting/host/it2me/it2me_host.cc +++ b/remoting/host/it2me/it2me_host.cc @@ -195,7 +195,7 @@ void It2MeHost::FinishConnect() { host_.reset(new ChromotingHost( signal_strategy_.get(), desktop_environment_factory_.get(), - CreateHostSessionManager(signal_strategy_.get(), network_settings, + CreateHostSessionManager(network_settings, host_context_->url_request_context_getter()), host_context_->audio_task_runner(), host_context_->input_task_runner(), diff --git a/remoting/host/remoting_me2me_host.cc b/remoting/host/remoting_me2me_host.cc index 7a0faba..bc7e8a7 100644 --- a/remoting/host/remoting_me2me_host.cc +++ b/remoting/host/remoting_me2me_host.cc @@ -998,7 +998,7 @@ void HostProcess::StartHost() { host_.reset(new ChromotingHost( signal_strategy_.get(), desktop_environment_factory_.get(), - CreateHostSessionManager(signal_strategy_.get(), network_settings, + CreateHostSessionManager(network_settings, context_->url_request_context_getter()), context_->audio_task_runner(), context_->input_task_runner(), diff --git a/remoting/host/session_manager_factory.cc b/remoting/host/session_manager_factory.cc index a055df2..cd5befc 100644 --- a/remoting/host/session_manager_factory.cc +++ b/remoting/host/session_manager_factory.cc @@ -14,7 +14,6 @@ namespace remoting { scoped_ptr CreateHostSessionManager( - SignalStrategy* signal_strategy, const NetworkSettings& network_settings, const scoped_refptr& url_request_context_getter) { @@ -23,14 +22,22 @@ scoped_ptr CreateHostSessionManager( ChromiumPortAllocator::Create(url_request_context_getter, network_settings)); + bool incoming_only = network_settings.nat_traversal_mode == + NetworkSettings::NAT_TRAVERSAL_DISABLED; + scoped_ptr transport_factory( new protocol::LibjingleTransportFactory( - signal_strategy, port_allocator.PassAs(), - network_settings)); + incoming_only)); + + // Use the Jingle protocol for channel-negotiation signalling between + // peer TransportFactories. + bool fetch_stun_relay_info = network_settings.nat_traversal_mode == + NetworkSettings::NAT_TRAVERSAL_ENABLED; scoped_ptr session_manager( - new protocol::JingleSessionManager(transport_factory.Pass())); + new protocol::JingleSessionManager( + transport_factory.Pass(), fetch_stun_relay_info)); return session_manager.PassAs(); } diff --git a/remoting/host/session_manager_factory.h b/remoting/host/session_manager_factory.h index ce2a53d..2c0a1b6 100644 --- a/remoting/host/session_manager_factory.h +++ b/remoting/host/session_manager_factory.h @@ -15,14 +15,12 @@ class URLRequestContextGetter; namespace remoting { struct NetworkSettings; -class SignalStrategy; namespace protocol { -class SessionManager; + class SessionManager; } // namespace protocol scoped_ptr CreateHostSessionManager( - SignalStrategy* signal_strategy, const NetworkSettings& network_settings, const scoped_refptr& url_request_context_getter); diff --git a/remoting/jingle_glue/jingle_info_request.cc b/remoting/jingle_glue/jingle_info_request.cc index d41f18a..e75b89f 100644 --- a/remoting/jingle_glue/jingle_info_request.cc +++ b/remoting/jingle_glue/jingle_info_request.cc @@ -8,7 +8,6 @@ #include "base/message_loop/message_loop.h" #include "base/stl_util.h" #include "base/strings/string_number_conversions.h" -#include "base/time/time.h" #include "net/base/net_util.h" #include "remoting/jingle_glue/iq_sender.h" #include "third_party/libjingle/source/talk/base/socketaddress.h" @@ -17,13 +16,12 @@ namespace remoting { -const int kRequestTimeoutSeconds = 5; - JingleInfoRequest::JingleInfoRequest(SignalStrategy* signal_strategy) : iq_sender_(signal_strategy) { } -JingleInfoRequest::~JingleInfoRequest() {} +JingleInfoRequest::~JingleInfoRequest() { +} void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { on_jingle_info_cb_ = callback; @@ -32,39 +30,19 @@ void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { request_ = iq_sender_.SendIq( buzz::STR_GET, buzz::STR_EMPTY, iq_body.Pass(), base::Bind(&JingleInfoRequest::OnResponse, base::Unretained(this))); - if (!request_) { - // If we failed to send IqRequest it means that SignalStrategy is - // disconnected. Notify the caller. - std::vector stun_hosts; - std::vector relay_hosts; - std::string relay_token; - on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); - return; - } - request_->SetTimeout(base::TimeDelta::FromSeconds(kRequestTimeoutSeconds)); } void JingleInfoRequest::OnResponse(IqRequest* request, const buzz::XmlElement* stanza) { - std::vector stun_hosts; - std::vector relay_hosts; - std::string relay_token; - - if (!stanza) { - LOG(WARNING) << "Jingle info request has timed out."; - on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); - return; - } - const buzz::XmlElement* query = stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); if (query == NULL) { LOG(WARNING) << "No Jingle info found in Jingle Info query response." << stanza->Str(); - on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); return; } + std::vector stun_hosts; const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); if (stun) { for (const buzz::XmlElement* server = @@ -85,6 +63,8 @@ void JingleInfoRequest::OnResponse(IqRequest* request, } } + std::vector relay_hosts; + std::string relay_token; const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); if (relay) { relay_token = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); diff --git a/remoting/jingle_glue/jingle_info_request.h b/remoting/jingle_glue/jingle_info_request.h index 118aa4a..a1b10de 100644 --- a/remoting/jingle_glue/jingle_info_request.h +++ b/remoting/jingle_glue/jingle_info_request.h @@ -13,6 +13,7 @@ #include "base/callback.h" #include "base/memory/scoped_ptr.h" #include "remoting/jingle_glue/iq_sender.h" +#include "third_party/libjingle/source/talk/base/sigslot.h" namespace buzz { class XmlElement; @@ -33,14 +34,17 @@ class SignalStrategy; // // This class is not threadsafe and should be used on the same thread it is // created on. -class JingleInfoRequest { +// +// TODO(ajwong): Add support for a timeout. +class JingleInfoRequest : public sigslot::has_slots<> { public: - // Callback to receive the Jingle configuration settings. All fields are empty - // if the request has timed out. - typedef base::Callback& relay_servers, - const std::vector& - stun_servers)> OnJingleInfoCallback; + // Callback to receive the Jingle configuration settings. The arguments are + // passed by pointer so the receive may call swap on them. The receiver does + // NOT own the arguments, which are guaranteed only to be alive for the + // duration of the callback. + typedef base::Callback&, + const std::vector&)> OnJingleInfoCallback; explicit JingleInfoRequest(SignalStrategy* signal_strategy); virtual ~JingleInfoRequest(); diff --git a/remoting/protocol/connection_to_host.cc b/remoting/protocol/connection_to_host.cc index e35ba22..bcd77e5 100644 --- a/remoting/protocol/connection_to_host.cc +++ b/remoting/protocol/connection_to_host.cc @@ -92,7 +92,8 @@ void ConnectionToHost::Connect(SignalStrategy* signal_strategy, signal_strategy_->AddListener(this); signal_strategy_->Connect(); - session_manager_.reset(new JingleSessionManager(transport_factory.Pass())); + session_manager_.reset(new JingleSessionManager( + transport_factory.Pass(), allow_nat_traversal_)); session_manager_->Init(signal_strategy_, this); SetState(CONNECTING, OK); diff --git a/remoting/protocol/jingle_session_manager.cc b/remoting/protocol/jingle_session_manager.cc index 58bcd4d..7cb1586 100644 --- a/remoting/protocol/jingle_session_manager.cc +++ b/remoting/protocol/jingle_session_manager.cc @@ -6,12 +6,14 @@ #include "base/bind.h" #include "remoting/jingle_glue/iq_sender.h" +#include "remoting/jingle_glue/jingle_info_request.h" #include "remoting/jingle_glue/signal_strategy.h" #include "remoting/protocol/authenticator.h" #include "remoting/protocol/content_description.h" #include "remoting/protocol/jingle_messages.h" #include "remoting/protocol/jingle_session.h" #include "remoting/protocol/transport.h" +#include "remoting/protocol/transport_config.h" #include "third_party/libjingle/source/talk/base/socketaddress.h" #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" @@ -21,8 +23,10 @@ namespace remoting { namespace protocol { JingleSessionManager::JingleSessionManager( - scoped_ptr transport_factory) + scoped_ptr transport_factory, + bool fetch_stun_relay_config) : transport_factory_(transport_factory.Pass()), + fetch_stun_relay_config_(fetch_stun_relay_config), signal_strategy_(NULL), listener_(NULL), ready_(false) { @@ -44,13 +48,35 @@ void JingleSessionManager::Init( OnSignalStrategyStateChange(signal_strategy_->GetState()); } +void JingleSessionManager::OnJingleInfo( + const std::string& relay_token, + const std::vector& relay_hosts, + const std::vector& stun_hosts) { + DCHECK(CalledOnValidThread()); + + // TODO(sergeyu): Add support for multiple STUN/relay servers when + // it's implemented in libjingle and P2P Transport API. + TransportConfig config; + config.stun_server = stun_hosts[0].ToString(); + config.relay_server = relay_hosts[0]; + config.relay_token = relay_token; + transport_factory_->SetTransportConfig(config); + + VLOG(1) << "STUN server: " << config.stun_server + << " Relay server: " << config.relay_server + << " Relay token: " << config.relay_token; + + + if (!ready_) { + ready_ = true; + listener_->OnSessionManagerReady(); + } +} + scoped_ptr JingleSessionManager::Connect( const std::string& host_jid, scoped_ptr authenticator, scoped_ptr config) { - // Notify |transport_factory_| that it may be used soon. - transport_factory_->PrepareTokens(); - scoped_ptr session(new JingleSession(this)); session->StartConnection(host_jid, authenticator.Pass(), config.Pass()); sessions_[session->session_id_] = session.get(); @@ -64,6 +90,7 @@ void JingleSessionManager::Close() { DCHECK(sessions_.empty()); listener_ = NULL; + jingle_info_request_.reset(); if (signal_strategy_) { signal_strategy_->RemoveListener(this); @@ -79,9 +106,16 @@ void JingleSessionManager::set_authenticator_factory( void JingleSessionManager::OnSignalStrategyStateChange( SignalStrategy::State state) { - if (state == SignalStrategy::CONNECTED && !ready_) { - ready_ = true; - listener_->OnSessionManagerReady(); + if (state == SignalStrategy::CONNECTED) { + // Request STUN/Relay info if necessary. + if (fetch_stun_relay_config_) { + jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); + jingle_info_request_->Send(base::Bind(&JingleSessionManager::OnJingleInfo, + base::Unretained(this))); + } else if (!ready_) { + ready_ = true; + listener_->OnSessionManagerReady(); + } } } @@ -103,9 +137,6 @@ bool JingleSessionManager::OnSignalStrategyIncomingStanza( SendReply(stanza, JingleMessageReply::NONE); - // Notify |transport_factory_| that it may be used soon. - transport_factory_->PrepareTokens(); - scoped_ptr authenticator = authenticator_factory_->CreateAuthenticator( signal_strategy_->GetLocalJid(), message.from, diff --git a/remoting/protocol/jingle_session_manager.h b/remoting/protocol/jingle_session_manager.h index e808ad2..7130f3f 100644 --- a/remoting/protocol/jingle_session_manager.h +++ b/remoting/protocol/jingle_session_manager.h @@ -30,6 +30,7 @@ class SocketAddress; namespace remoting { class IqSender; +class JingleInfoRequest; namespace protocol { @@ -43,7 +44,15 @@ class TransportFactory; class JingleSessionManager : public SessionManager, public SignalStrategy::Listener { public: - explicit JingleSessionManager(scoped_ptr transport_factory); + // When |fetch_stun_relay_config| is set to true then + // JingleSessionManager will also try to query configuration of STUN + // and Relay servers from the signaling server. + // + // TODO(sergeyu): Move NAT-traversal config fetching to a separate + // class. + explicit JingleSessionManager( + scoped_ptr transport_factory, + bool fetch_stun_relay_config); virtual ~JingleSessionManager(); // SessionManager interface. @@ -90,6 +99,8 @@ class JingleSessionManager : public SessionManager, bool ready_; + scoped_ptr jingle_info_request_; + SessionsMap sessions_; DISALLOW_COPY_AND_ASSIGN(JingleSessionManager); diff --git a/remoting/protocol/jingle_session_unittest.cc b/remoting/protocol/jingle_session_unittest.cc index 62e4d40..34e09d6 100644 --- a/remoting/protocol/jingle_session_unittest.cc +++ b/remoting/protocol/jingle_session_unittest.cc @@ -141,14 +141,13 @@ class JingleSessionTest : public testing::Test { EXPECT_CALL(host_server_listener_, OnSessionManagerReady()) .Times(1); - NetworkSettings network_settings(NetworkSettings::NAT_TRAVERSAL_OUTGOING); + NetworkSettings network_settings; scoped_ptr host_transport(new LibjingleTransportFactory( - NULL, ChromiumPortAllocator::Create(NULL, network_settings) .PassAs(), - network_settings)); - host_server_.reset(new JingleSessionManager(host_transport.Pass())); + false)); + host_server_.reset(new JingleSessionManager(host_transport.Pass(), false)); host_server_->Init(host_signal_strategy_.get(), &host_server_listener_); scoped_ptr factory( @@ -158,12 +157,11 @@ class JingleSessionTest : public testing::Test { EXPECT_CALL(client_server_listener_, OnSessionManagerReady()) .Times(1); scoped_ptr client_transport(new LibjingleTransportFactory( - NULL, ChromiumPortAllocator::Create(NULL, network_settings) .PassAs(), - network_settings)); + false)); client_server_.reset( - new JingleSessionManager(client_transport.Pass())); + new JingleSessionManager(client_transport.Pass(), false)); client_server_->Init(client_signal_strategy_.get(), &client_server_listener_); } diff --git a/remoting/protocol/libjingle_transport_factory.cc b/remoting/protocol/libjingle_transport_factory.cc index ef22f2a..76c3507 100644 --- a/remoting/protocol/libjingle_transport_factory.cc +++ b/remoting/protocol/libjingle_transport_factory.cc @@ -4,7 +4,6 @@ #include "remoting/protocol/libjingle_transport_factory.h" -#include "base/callback.h" #include "base/single_thread_task_runner.h" #include "base/thread_task_runner_handle.h" #include "base/timer/timer.h" @@ -14,9 +13,9 @@ #include "jingle/glue/utils.h" #include "net/base/net_errors.h" #include "remoting/base/constants.h" -#include "remoting/jingle_glue/jingle_info_request.h" #include "remoting/jingle_glue/network_settings.h" #include "remoting/protocol/channel_authenticator.h" +#include "remoting/protocol/transport_config.h" #include "third_party/libjingle/source/talk/base/network.h" #include "third_party/libjingle/source/talk/p2p/base/constants.h" #include "third_party/libjingle/source/talk/p2p/base/p2ptransportchannel.h" @@ -41,21 +40,13 @@ const int kTcpSendBufferSize = kTcpReceiveBufferSize + 30 * 1024; const int kMaxReconnectAttempts = 2; const int kReconnectDelaySeconds = 15; -// Get fresh STUN/Relay configuration every hour. -const int kJingleInfoUpdatePeriodSeconds = 3600; - -class LibjingleStreamTransport - : public StreamTransport, - public base::SupportsWeakPtr, - public sigslot::has_slots<> { +class LibjingleStreamTransport : public StreamTransport, + public sigslot::has_slots<> { public: LibjingleStreamTransport(cricket::PortAllocator* port_allocator, - const NetworkSettings& network_settings); + bool incoming_only); virtual ~LibjingleStreamTransport(); - // Called by JingleTransportFactory when it has fresh Jingle info. - void OnCanStart(); - // StreamTransport interface. virtual void Initialize( const std::string& name, @@ -68,8 +59,6 @@ class LibjingleStreamTransport virtual bool is_connected() const OVERRIDE; private: - void DoStart(); - // Signal handlers for cricket::TransportChannel. void OnRequestSignaling(cricket::TransportChannelImpl* channel); void OnCandidateReady(cricket::TransportChannelImpl* channel, @@ -97,7 +86,7 @@ class LibjingleStreamTransport void NotifyConnectFailed(); cricket::PortAllocator* port_allocator_; - NetworkSettings network_settings_; + bool incoming_only_; std::string name_; EventHandler* event_handler_; @@ -106,9 +95,6 @@ class LibjingleStreamTransport std::string ice_username_fragment_; std::string ice_password_; - bool can_start_; - - std::list pending_candidates_; scoped_ptr channel_; bool channel_was_writable_; int connect_attempts_left_; @@ -122,14 +108,13 @@ class LibjingleStreamTransport LibjingleStreamTransport::LibjingleStreamTransport( cricket::PortAllocator* port_allocator, - const NetworkSettings& network_settings) + bool incoming_only) : port_allocator_(port_allocator), - network_settings_(network_settings), + incoming_only_(incoming_only), event_handler_(NULL), ice_username_fragment_( talk_base::CreateRandomString(cricket::ICE_UFRAG_LENGTH)), ice_password_(talk_base::CreateRandomString(cricket::ICE_PWD_LENGTH)), - can_start_(false), channel_was_writable_(false), connect_attempts_left_(kMaxReconnectAttempts) { } @@ -146,22 +131,6 @@ LibjingleStreamTransport::~LibjingleStreamTransport() { } } -void LibjingleStreamTransport::OnCanStart() { - DCHECK(CalledOnValidThread()); - - DCHECK(!can_start_); - can_start_ = true; - - // If Connect() has been called then start connection. - if (!callback_.is_null()) - DoStart(); - - while (!pending_candidates_.empty()) { - channel_->OnCandidate(pending_candidates_.front()); - pending_candidates_.pop_front(); - } -} - void LibjingleStreamTransport::Initialize( const std::string& name, Transport::EventHandler* event_handler, @@ -182,13 +151,9 @@ void LibjingleStreamTransport::Initialize( void LibjingleStreamTransport::Connect( const StreamTransport::ConnectedCallback& callback) { DCHECK(CalledOnValidThread()); - callback_ = callback; - if (can_start_) - DoStart(); -} + callback_ = callback; -void LibjingleStreamTransport::DoStart() { DCHECK(!channel_.get()); // Create P2PTransportChannel, attach signal handlers and connect it. @@ -204,10 +169,7 @@ void LibjingleStreamTransport::DoStart() { this, &LibjingleStreamTransport::OnRouteChange); channel_->SignalWritableState.connect( this, &LibjingleStreamTransport::OnWritableState); - if (network_settings_.nat_traversal_mode == - NetworkSettings::NAT_TRAVERSAL_DISABLED) { - channel_->set_incoming_only(true); - } + channel_->set_incoming_only(incoming_only_); channel_->Connect(); @@ -249,11 +211,7 @@ void LibjingleStreamTransport::DoStart() { void LibjingleStreamTransport::AddRemoteCandidate( const cricket::Candidate& candidate) { DCHECK(CalledOnValidThread()); - if (channel_) { - channel_->OnCandidate(candidate); - } else { - pending_candidates_.push_back(candidate); - } + channel_->OnCandidate(candidate); } const std::string& LibjingleStreamTransport::name() const { @@ -404,12 +362,10 @@ void LibjingleStreamTransport::NotifyConnectFailed() { } // namespace LibjingleTransportFactory::LibjingleTransportFactory( - SignalStrategy* signal_strategy, scoped_ptr port_allocator, - const NetworkSettings& network_settings) - : signal_strategy_(signal_strategy), - port_allocator_(port_allocator.Pass()), - network_settings_(network_settings) { + bool incoming_only) + : port_allocator_(port_allocator.Pass()), + incoming_only_(incoming_only) { jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); } @@ -421,27 +377,27 @@ LibjingleTransportFactory::~LibjingleTransportFactory() { task_runner->DeleteSoon(FROM_HERE, port_allocator_.release()); } -void LibjingleTransportFactory::PrepareTokens() { - EnsureFreshJingleInfo(); -} - -scoped_ptr LibjingleTransportFactory::CreateStreamTransport() { - scoped_ptr result( - new LibjingleStreamTransport(port_allocator_.get(), network_settings_)); - - 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( - base::Bind(&LibjingleStreamTransport::OnCanStart, - result->AsWeakPtr())); +void LibjingleTransportFactory::SetTransportConfig( + const TransportConfig& config) { + std::vector stun_hosts; + talk_base::SocketAddress stun_address; + if (stun_address.FromString(config.stun_server)) { + stun_hosts.push_back(stun_address); + port_allocator_->SetStunHosts(stun_hosts); } else { - result->OnCanStart(); + LOG(ERROR) << "Failed to parse stun server address: " + << config.stun_server; } - return result.PassAs(); + std::vector relay_hosts; + relay_hosts.push_back(config.relay_server); + port_allocator_->SetRelayHosts(relay_hosts); + port_allocator_->SetRelayToken(config.relay_token); +} + +scoped_ptr LibjingleTransportFactory::CreateStreamTransport() { + return scoped_ptr( + new LibjingleStreamTransport(port_allocator_.get(), incoming_only_)); } scoped_ptr @@ -450,42 +406,5 @@ LibjingleTransportFactory::CreateDatagramTransport() { return scoped_ptr(); } -void LibjingleTransportFactory::EnsureFreshJingleInfo() { - if (network_settings_.nat_traversal_mode != - NetworkSettings::NAT_TRAVERSAL_ENABLED || - jingle_info_request_) { - return; - } - - if (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( - &LibjingleTransportFactory::OnJingleInfo, base::Unretained(this))); - } -} - -void LibjingleTransportFactory::OnJingleInfo( - const std::string& relay_token, - const std::vector& relay_hosts, - const std::vector& 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/libjingle_transport_factory.h b/remoting/protocol/libjingle_transport_factory.h index 637410c..dfa0ab1 100644 --- a/remoting/protocol/libjingle_transport_factory.h +++ b/remoting/protocol/libjingle_transport_factory.h @@ -5,10 +5,6 @@ #ifndef REMOTING_PROTOCOL_LIBJINGLE_TRANSPORT_FACTORY_H_ #define REMOTING_PROTOCOL_LIBJINGLE_TRANSPORT_FACTORY_H_ -#include - -#include "base/callback_forward.h" -#include "remoting/jingle_glue/network_settings.h" #include "remoting/protocol/transport.h" namespace cricket { @@ -23,49 +19,34 @@ class URLRequestContextGetter; namespace talk_base { class NetworkManager; class PacketSocketFactory; -class SocketAddress; } // namespace talk_base namespace remoting { -class SignalStrategy; -class JingleInfoRequest; +struct NetworkSettings; namespace protocol { class LibjingleTransportFactory : public TransportFactory { public: - // |signal_strategy| must outlive LibjingleTransportFactory. Need to use - // cricket::HttpPortAllocatorBase pointer for the |port_allocator|, so that it - // is possible to configure |port_allocator| with STUN/Relay addresses. + // Need to use cricket::HttpPortAllocatorBase pointer for the + // |port_allocator|, so that it is possible to configure + // |port_allocator| with STUN/Relay addresses. + // TODO(sergeyu): Reconsider this design. LibjingleTransportFactory( - SignalStrategy* signal_strategy, scoped_ptr port_allocator, - const NetworkSettings& network_settings); + bool incoming_only); virtual ~LibjingleTransportFactory(); // TransportFactory interface. - virtual void PrepareTokens() OVERRIDE; + virtual void SetTransportConfig(const TransportConfig& config) OVERRIDE; virtual scoped_ptr CreateStreamTransport() OVERRIDE; virtual scoped_ptr CreateDatagramTransport() OVERRIDE; private: - void EnsureFreshJingleInfo(); - void OnJingleInfo(const std::string& relay_token, - const std::vector& relay_hosts, - const std::vector& stun_hosts); - - SignalStrategy* signal_strategy_; scoped_ptr port_allocator_; - NetworkSettings network_settings_; - - base::TimeTicks last_jingle_info_update_time_; - scoped_ptr 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 on_jingle_info_callbacks_; + bool incoming_only_; DISALLOW_COPY_AND_ASSIGN(LibjingleTransportFactory); }; diff --git a/remoting/protocol/session_manager.h b/remoting/protocol/session_manager.h index 8ac484d..37b9599 100644 --- a/remoting/protocol/session_manager.h +++ b/remoting/protocol/session_manager.h @@ -59,6 +59,7 @@ #include "base/memory/ref_counted.h" #include "base/threading/non_thread_safe.h" #include "remoting/protocol/session.h" +#include "remoting/protocol/transport_config.h" namespace remoting { diff --git a/remoting/protocol/transport.h b/remoting/protocol/transport.h index 196b9d2..fe2f4e1 100644 --- a/remoting/protocol/transport.h +++ b/remoting/protocol/transport.h @@ -43,6 +43,7 @@ namespace remoting { namespace protocol { class ChannelAuthenticator; +struct TransportConfig; struct TransportRoute { enum RouteType { @@ -150,11 +151,8 @@ class TransportFactory { TransportFactory() { } virtual ~TransportFactory() { } - // Called to notify transport factory that a new transport might be created - // soon, e.g. when a new session is being created. Implementation may use it - // to start asynchronous preparation, e.g. fetch a new relay token if - // necessary while the session is being authenticated. - virtual void PrepareTokens() = 0; + // Sets configuration for the transports created by this factory. + virtual void SetTransportConfig(const TransportConfig& config) = 0; virtual scoped_ptr CreateStreamTransport() = 0; virtual scoped_ptr CreateDatagramTransport() = 0; diff --git a/remoting/protocol/transport_config.cc b/remoting/protocol/transport_config.cc new file mode 100644 index 0000000..093e1b1 --- /dev/null +++ b/remoting/protocol/transport_config.cc @@ -0,0 +1,17 @@ +// 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/protocol/transport_config.h" + +namespace remoting { +namespace protocol { + +TransportConfig::TransportConfig() { +} + +TransportConfig::~TransportConfig() { +} + +} // namespace protocol +} // namespace remoting diff --git a/remoting/protocol/transport_config.h b/remoting/protocol/transport_config.h new file mode 100644 index 0000000..d996bd5 --- /dev/null +++ b/remoting/protocol/transport_config.h @@ -0,0 +1,25 @@ +// 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_PROTOCOL_TRANSPORT_CONFIG_H_ +#define REMOTING_PROTOCOL_TRANSPORT_CONFIG_H_ + +#include + +namespace remoting { +namespace protocol { + +struct TransportConfig { + TransportConfig(); + ~TransportConfig(); + + std::string stun_server; + std::string relay_server; + std::string relay_token; +}; + +} // namespace protocol +} // namespace remoting + +#endif // REMOTING_PROTOCOL_TRANSPORT_CONFIG_H_ diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index ed93d2b..c4f6fde 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -2767,6 +2767,8 @@ 'protocol/ssl_hmac_channel_authenticator.h', 'protocol/transport.cc', 'protocol/transport.h', + 'protocol/transport_config.cc', + 'protocol/transport_config.h', 'protocol/util.cc', 'protocol/util.h', 'protocol/third_party_authenticator_base.cc', -- cgit v1.1