| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/transport_context.h" | 5 #include "remoting/protocol/transport_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "remoting/protocol/port_allocator_base.h" | |
| 14 #include "remoting/protocol/port_allocator_factory.h" | 13 #include "remoting/protocol/port_allocator_factory.h" |
| 14 #include "third_party/webrtc/base/socketaddress.h" |
| 15 | 15 |
| 16 #if !defined(OS_NACL) | 16 #if !defined(OS_NACL) |
| 17 #include "jingle/glue/thread_wrapper.h" | 17 #include "jingle/glue/thread_wrapper.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "remoting/protocol/chromium_port_allocator.h" | 19 #include "remoting/protocol/chromium_port_allocator.h" |
| 20 #endif // !defined(OS_NACL) | 20 #endif // !defined(OS_NACL) |
| 21 | 21 |
| 22 namespace remoting { | 22 namespace remoting { |
| 23 namespace protocol { | 23 namespace protocol { |
| 24 | 24 |
| 25 // Get fresh STUN/Relay configuration every hour. | 25 // Get fresh STUN/Relay configuration every hour. |
| 26 static const int kJingleInfoUpdatePeriodSeconds = 3600; | 26 static const int kJingleInfoUpdatePeriodSeconds = 3600; |
| 27 | 27 |
| 28 #if !defined(OS_NACL) | 28 #if !defined(OS_NACL) |
| 29 // static | 29 // static |
| 30 scoped_refptr<TransportContext> TransportContext::ForTests(TransportRole role) { | 30 scoped_refptr<TransportContext> TransportContext::ForTests(TransportRole role) { |
| 31 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 31 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 32 return new protocol::TransportContext( | 32 return new protocol::TransportContext( |
| 33 nullptr, make_scoped_ptr( | 33 nullptr, |
| 34 new protocol::ChromiumPortAllocatorFactory(nullptr)), | 34 make_scoped_ptr(new protocol::ChromiumPortAllocatorFactory(nullptr)), |
| 35 protocol::NetworkSettings( | 35 protocol::NetworkSettings( |
| 36 protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING), | 36 protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING), |
| 37 role); | 37 role); |
| 38 } | 38 } |
| 39 #endif // !defined(OS_NACL) | 39 #endif // !defined(OS_NACL) |
| 40 | 40 |
| 41 TransportContext::TransportContext( | 41 TransportContext::TransportContext( |
| 42 SignalStrategy* signal_strategy, | 42 SignalStrategy* signal_strategy, |
| 43 scoped_ptr<PortAllocatorFactory> port_allocator_factory, | 43 scoped_ptr<PortAllocatorFactory> port_allocator_factory, |
| 44 const NetworkSettings& network_settings, | 44 const NetworkSettings& network_settings, |
| 45 TransportRole role) | 45 TransportRole role) |
| 46 : signal_strategy_(signal_strategy), | 46 : signal_strategy_(signal_strategy), |
| 47 port_allocator_factory_(std::move(port_allocator_factory)), | 47 port_allocator_factory_(std::move(port_allocator_factory)), |
| 48 network_settings_(network_settings), | 48 network_settings_(network_settings), |
| 49 role_(role) {} | 49 role_(role) {} |
| 50 | 50 |
| 51 TransportContext::~TransportContext() {} | 51 TransportContext::~TransportContext() {} |
| 52 | 52 |
| 53 void TransportContext::Prepare() { | 53 void TransportContext::Prepare() { |
| 54 EnsureFreshJingleInfo(); | 54 EnsureFreshJingleInfo(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void TransportContext::CreatePortAllocator( | 57 void TransportContext::GetJingleInfo(const GetJingleInfoCallback& callback) { |
| 58 const CreatePortAllocatorCallback& callback) { | |
| 59 EnsureFreshJingleInfo(); | 58 EnsureFreshJingleInfo(); |
| 60 | 59 |
| 61 // If there is a pending |jingle_info_request_| delay starting the new | 60 // If there is a pending |jingle_info_request_| delay the callback until the |
| 62 // transport until the request is finished. | 61 // request is finished. |
| 63 if (jingle_info_request_) { | 62 if (jingle_info_request_) { |
| 64 pending_port_allocator_requests_.push_back(callback); | 63 pending_jingle_info_callbacks_.push_back(callback); |
| 65 } else { | 64 } else { |
| 66 callback.Run(CreatePortAllocatorInternal()); | 65 callback.Run(stun_hosts_, relay_hosts_, relay_token_); |
| 67 } | 66 } |
| 68 } | 67 } |
| 69 | 68 |
| 70 void TransportContext::EnsureFreshJingleInfo() { | 69 void TransportContext::EnsureFreshJingleInfo() { |
| 71 // Check if request is already pending. | 70 // Check if request is already pending. |
| 72 if (jingle_info_request_) | 71 if (jingle_info_request_) |
| 73 return; | 72 return; |
| 74 | 73 |
| 75 // Don't need to make jingleinfo request if both STUN and Relay are disabled. | 74 // Don't need to make jingleinfo request if both STUN and Relay are disabled. |
| 76 if ((network_settings_.flags & (NetworkSettings::NAT_TRAVERSAL_STUN | | 75 if ((network_settings_.flags & (NetworkSettings::NAT_TRAVERSAL_STUN | |
| (...skipping 15 matching lines...) Expand all Loading... |
| 92 const std::vector<std::string>& relay_hosts, | 91 const std::vector<std::string>& relay_hosts, |
| 93 const std::vector<rtc::SocketAddress>& stun_hosts) { | 92 const std::vector<rtc::SocketAddress>& stun_hosts) { |
| 94 relay_token_ = relay_token; | 93 relay_token_ = relay_token; |
| 95 relay_hosts_ = relay_hosts; | 94 relay_hosts_ = relay_hosts; |
| 96 stun_hosts_ = stun_hosts; | 95 stun_hosts_ = stun_hosts; |
| 97 | 96 |
| 98 jingle_info_request_.reset(); | 97 jingle_info_request_.reset(); |
| 99 if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) | 98 if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) |
| 100 last_jingle_info_update_time_ = base::TimeTicks::Now(); | 99 last_jingle_info_update_time_ = base::TimeTicks::Now(); |
| 101 | 100 |
| 102 while (!pending_port_allocator_requests_.empty()) { | 101 while (!pending_jingle_info_callbacks_.empty()) { |
| 103 pending_port_allocator_requests_.begin()->Run( | 102 pending_jingle_info_callbacks_.begin()->Run(stun_hosts_, relay_hosts_, |
| 104 CreatePortAllocatorInternal()); | 103 relay_token_); |
| 105 pending_port_allocator_requests_.pop_front(); | 104 pending_jingle_info_callbacks_.pop_front(); |
| 106 } | 105 } |
| 107 } | 106 } |
| 108 | 107 |
| 109 scoped_ptr<cricket::PortAllocator> | |
| 110 TransportContext::CreatePortAllocatorInternal() { | |
| 111 scoped_ptr<PortAllocatorBase> result = | |
| 112 port_allocator_factory_->CreatePortAllocator(); | |
| 113 | |
| 114 if (!relay_token_.empty() && !relay_hosts_.empty()) { | |
| 115 result->SetRelayHosts(relay_hosts_); | |
| 116 result->SetRelayToken(relay_token_); | |
| 117 } | |
| 118 if (!stun_hosts_.empty()) | |
| 119 result->SetStunHosts(stun_hosts_); | |
| 120 | |
| 121 // We always use PseudoTcp to provide a reliable channel. It provides poor | |
| 122 // performance when combined with TCP-based transport, so we have to disable | |
| 123 // TCP ports. ENABLE_SHARED_UFRAG flag is specified so that the same username | |
| 124 // fragment is shared between all candidates. | |
| 125 int flags = cricket::PORTALLOCATOR_DISABLE_TCP | | |
| 126 cricket::PORTALLOCATOR_ENABLE_SHARED_UFRAG | | |
| 127 cricket::PORTALLOCATOR_ENABLE_IPV6; | |
| 128 | |
| 129 if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_STUN)) | |
| 130 flags |= cricket::PORTALLOCATOR_DISABLE_STUN; | |
| 131 | |
| 132 if (!(network_settings_.flags & NetworkSettings::NAT_TRAVERSAL_RELAY)) | |
| 133 flags |= cricket::PORTALLOCATOR_DISABLE_RELAY; | |
| 134 | |
| 135 result->set_flags(flags); | |
| 136 result->SetPortRange(network_settings_.port_range.min_port, | |
| 137 network_settings_.port_range.max_port); | |
| 138 | |
| 139 return std::move(result); | |
| 140 } | |
| 141 | |
| 142 } // namespace protocol | 108 } // namespace protocol |
| 143 } // namespace remoting | 109 } // namespace remoting |
| OLD | NEW |