| 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/base/url_request.h" | 13 #include "remoting/base/url_request.h" |
| 14 #include "remoting/protocol/jingle_info_request.h" |
| 14 #include "remoting/protocol/port_allocator_factory.h" | 15 #include "remoting/protocol/port_allocator_factory.h" |
| 15 #include "third_party/webrtc/base/socketaddress.h" | 16 #include "third_party/webrtc/base/socketaddress.h" |
| 16 | 17 |
| 17 #if !defined(OS_NACL) | 18 #if !defined(OS_NACL) |
| 18 #include "jingle/glue/thread_wrapper.h" | 19 #include "jingle/glue/thread_wrapper.h" |
| 19 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 20 #include "remoting/protocol/chromium_port_allocator_factory.h" | 21 #include "remoting/protocol/chromium_port_allocator_factory.h" |
| 21 #endif // !defined(OS_NACL) | 22 #endif // !defined(OS_NACL) |
| 22 | 23 |
| 23 namespace remoting { | 24 namespace remoting { |
| 24 namespace protocol { | 25 namespace protocol { |
| 25 | 26 |
| 26 // Get fresh STUN/Relay configuration every hour. | |
| 27 static const int kJingleInfoUpdatePeriodSeconds = 3600; | |
| 28 | |
| 29 #if !defined(OS_NACL) | 27 #if !defined(OS_NACL) |
| 30 // static | 28 // static |
| 31 scoped_refptr<TransportContext> TransportContext::ForTests(TransportRole role) { | 29 scoped_refptr<TransportContext> TransportContext::ForTests(TransportRole role) { |
| 32 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); | 30 jingle_glue::JingleThreadWrapper::EnsureForCurrentMessageLoop(); |
| 33 return new protocol::TransportContext( | 31 return new protocol::TransportContext( |
| 34 nullptr, make_scoped_ptr(new protocol::ChromiumPortAllocatorFactory()), | 32 nullptr, make_scoped_ptr(new protocol::ChromiumPortAllocatorFactory()), |
| 35 nullptr, protocol::NetworkSettings( | 33 nullptr, protocol::NetworkSettings( |
| 36 protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING), | 34 protocol::NetworkSettings::NAT_TRAVERSAL_OUTGOING), |
| 37 role); | 35 role); |
| 38 } | 36 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 49 url_request_factory_(std::move(url_request_factory)), | 47 url_request_factory_(std::move(url_request_factory)), |
| 50 network_settings_(network_settings), | 48 network_settings_(network_settings), |
| 51 role_(role) {} | 49 role_(role) {} |
| 52 | 50 |
| 53 TransportContext::~TransportContext() {} | 51 TransportContext::~TransportContext() {} |
| 54 | 52 |
| 55 void TransportContext::Prepare() { | 53 void TransportContext::Prepare() { |
| 56 EnsureFreshJingleInfo(); | 54 EnsureFreshJingleInfo(); |
| 57 } | 55 } |
| 58 | 56 |
| 59 void TransportContext::GetJingleInfo(const GetJingleInfoCallback& callback) { | 57 void TransportContext::GetIceConfig(const GetIceConfigCallback& callback) { |
| 60 EnsureFreshJingleInfo(); | 58 EnsureFreshJingleInfo(); |
| 61 | 59 |
| 62 // If there is a pending |jingle_info_request_| delay the callback until the | 60 // If there is a pending |ice_config_request_| delay the callback until the |
| 63 // request is finished. | 61 // request is finished. |
| 64 if (jingle_info_request_) { | 62 if (ice_config_request_) { |
| 65 pending_jingle_info_callbacks_.push_back(callback); | 63 pending_ice_config_callbacks_.push_back(callback); |
| 66 } else { | 64 } else { |
| 67 callback.Run(stun_hosts_, relay_hosts_, relay_token_); | 65 callback.Run(ice_config_); |
| 68 } | 66 } |
| 69 } | 67 } |
| 70 | 68 |
| 71 void TransportContext::EnsureFreshJingleInfo() { | 69 void TransportContext::EnsureFreshJingleInfo() { |
| 72 // Check if request is already pending. | 70 // Check if request is already pending. |
| 73 if (jingle_info_request_) | 71 if (ice_config_request_) |
| 74 return; | 72 return; |
| 75 | 73 |
| 76 // 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. |
| 77 if ((network_settings_.flags & (NetworkSettings::NAT_TRAVERSAL_STUN | | 75 if ((network_settings_.flags & (NetworkSettings::NAT_TRAVERSAL_STUN | |
| 78 NetworkSettings::NAT_TRAVERSAL_RELAY)) == 0) { | 76 NetworkSettings::NAT_TRAVERSAL_RELAY)) == 0) { |
| 79 return; | 77 return; |
| 80 } | 78 } |
| 81 | 79 |
| 82 if (last_jingle_info_update_time_.is_null() || | 80 if (ice_config_.is_null() || |
| 83 base::TimeTicks::Now() - last_jingle_info_update_time_ > | 81 base::Time::Now() > ice_config_.expiration_time) { |
| 84 base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds)) { | 82 ice_config_request_.reset(new JingleInfoRequest(signal_strategy_)); |
| 85 jingle_info_request_.reset(new JingleInfoRequest(signal_strategy_)); | 83 ice_config_request_->Send(base::Bind( |
| 86 jingle_info_request_->Send(base::Bind( | 84 &TransportContext::OnIceConfig, base::Unretained(this))); |
| 87 &TransportContext::OnJingleInfo, base::Unretained(this))); | |
| 88 } | 85 } |
| 89 } | 86 } |
| 90 | 87 |
| 91 void TransportContext::OnJingleInfo( | 88 void TransportContext::OnIceConfig(const IceConfig& ice_config) { |
| 92 const std::string& relay_token, | 89 ice_config_ = ice_config; |
| 93 const std::vector<std::string>& relay_hosts, | 90 ice_config_request_.reset(); |
| 94 const std::vector<rtc::SocketAddress>& stun_hosts) { | |
| 95 relay_token_ = relay_token; | |
| 96 relay_hosts_ = relay_hosts; | |
| 97 stun_hosts_ = stun_hosts; | |
| 98 | 91 |
| 99 jingle_info_request_.reset(); | 92 while (!pending_ice_config_callbacks_.empty()) { |
| 100 if ((!relay_token.empty() && !relay_hosts.empty()) || !stun_hosts.empty()) | 93 pending_ice_config_callbacks_.begin()->Run(ice_config_); |
| 101 last_jingle_info_update_time_ = base::TimeTicks::Now(); | 94 pending_ice_config_callbacks_.pop_front(); |
| 102 | |
| 103 while (!pending_jingle_info_callbacks_.empty()) { | |
| 104 pending_jingle_info_callbacks_.begin()->Run(stun_hosts_, relay_hosts_, | |
| 105 relay_token_); | |
| 106 pending_jingle_info_callbacks_.pop_front(); | |
| 107 } | 95 } |
| 108 } | 96 } |
| 109 | 97 |
| 110 } // namespace protocol | 98 } // namespace protocol |
| 111 } // namespace remoting | 99 } // namespace remoting |
| OLD | NEW |