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