| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/protocol/ice_transport_factory.h" | |
| 6 | |
| 7 #include "base/single_thread_task_runner.h" | |
| 8 #include "base/thread_task_runner_handle.h" | |
| 9 #include "remoting/protocol/ice_transport.h" | |
| 10 #include "third_party/webrtc/p2p/client/httpportallocator.h" | |
| 11 | |
| 12 namespace remoting { | |
| 13 namespace protocol { | |
| 14 | |
| 15 // Get fresh STUN/Relay configuration every hour. | |
| 16 static const int kJingleInfoUpdatePeriodSeconds = 3600; | |
| 17 | |
| 18 IceTransportFactory::IceTransportFactory( | |
| 19 SignalStrategy* signal_strategy, | |
| 20 scoped_ptr<cricket::HttpPortAllocatorBase> port_allocator, | |
| 21 const NetworkSettings& network_settings, | |
| 22 TransportRole role) | |
| 23 : signal_strategy_(signal_strategy), | |
| 24 port_allocator_(port_allocator.Pass()), | |
| 25 network_settings_(network_settings), | |
| 26 role_(role) {} | |
| 27 | |
| 28 IceTransportFactory::~IceTransportFactory() { | |
| 29 // This method may be called in response to a libjingle signal, so | |
| 30 // libjingle objects must be deleted asynchronously. | |
| 31 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | |
| 32 base::ThreadTaskRunnerHandle::Get(); | |
| 33 task_runner->DeleteSoon(FROM_HERE, port_allocator_.release()); | |
| 34 } | |
| 35 | |
| 36 scoped_ptr<Transport> IceTransportFactory::CreateTransport() { | |
| 37 scoped_ptr<IceTransport> result( | |
| 38 new IceTransport(port_allocator_.get(), network_settings_, role_)); | |
| 39 | |
| 40 EnsureFreshJingleInfo(); | |
| 41 | |
| 42 // If there is a pending |jingle_info_request_| delay starting the new | |
| 43 // transport until the request is finished. | |
| 44 if (jingle_info_request_) { | |
| 45 on_jingle_info_callbacks_.push_back(result->GetCanStartClosure()); | |
| 46 } else { | |
| 47 result->GetCanStartClosure().Run(); | |
| 48 } | |
| 49 | |
| 50 return result.Pass(); | |
| 51 } | |
| 52 | |
| 53 void IceTransportFactory::EnsureFreshJingleInfo() { | |
| 54 uint32 stun_or_relay_flags = NetworkSettings::NAT_TRAVERSAL_STUN | | |
| 55 NetworkSettings::NAT_TRAVERSAL_RELAY; | |
| 56 if (!(network_settings_.flags & stun_or_relay_flags) || | |
| 57 jingle_info_request_) { | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 if (last_jingle_info_update_time_.is_null() || | |
| 62 base::TimeTicks::Now() - last_jingle_info_update_time_ > | |
| 63 base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds)) { | |
| 64 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); | |
| 65 jingle_info_request_->Send(base::Bind( | |
| 66 &IceTransportFactory::OnJingleInfo, base::Unretained(this))); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void IceTransportFactory::OnJingleInfo( | |
| 71 const std::string& relay_token, | |
| 72 const std::vector<std::string>& relay_hosts, | |
| 73 const std::vector<rtc::SocketAddress>& stun_hosts) { | |
| 74 if (!relay_token.empty() && !relay_hosts.empty()) { | |
| 75 port_allocator_->SetRelayHosts(relay_hosts); | |
| 76 port_allocator_->SetRelayToken(relay_token); | |
| 77 } | |
| 78 if (!stun_hosts.empty()) { | |
| 79 port_allocator_->SetStunHosts(stun_hosts); | |
| 80 } | |
| 81 | |
| 82 jingle_info_request_.reset(); | |
| 83 if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) | |
| 84 last_jingle_info_update_time_ = base::TimeTicks::Now(); | |
| 85 | |
| 86 while (!on_jingle_info_callbacks_.empty()) { | |
| 87 on_jingle_info_callbacks_.begin()->Run(); | |
| 88 on_jingle_info_callbacks_.pop_front(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 } // namespace protocol | |
| 93 } // namespace remoting | |
| OLD | NEW |