OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/signaling/jingle_info_request.h" | 5 #include "remoting/protocol/jingle_info_request.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "net/base/net_util.h" | 14 #include "net/base/net_util.h" |
| 15 #include "remoting/protocol/ice_config.h" |
15 #include "remoting/signaling/iq_sender.h" | 16 #include "remoting/signaling/iq_sender.h" |
16 #include "third_party/webrtc/base/socketaddress.h" | 17 #include "third_party/webrtc/base/socketaddress.h" |
17 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 18 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
18 #include "third_party/webrtc/libjingle/xmpp/constants.h" | 19 #include "third_party/webrtc/libjingle/xmpp/constants.h" |
19 | 20 |
20 namespace remoting { | 21 namespace remoting { |
| 22 namespace protocol { |
21 | 23 |
22 const int kRequestTimeoutSeconds = 5; | 24 const int kRequestTimeoutSeconds = 5; |
23 | 25 |
| 26 // Get fresh STUN/Relay configuration every hour. |
| 27 static const int kJingleInfoUpdatePeriodSeconds = 3600; |
| 28 |
24 JingleInfoRequest::JingleInfoRequest(SignalStrategy* signal_strategy) | 29 JingleInfoRequest::JingleInfoRequest(SignalStrategy* signal_strategy) |
25 : iq_sender_(signal_strategy) {} | 30 : iq_sender_(signal_strategy) {} |
26 | 31 |
27 JingleInfoRequest::~JingleInfoRequest() {} | 32 JingleInfoRequest::~JingleInfoRequest() {} |
28 | 33 |
29 void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { | 34 void JingleInfoRequest::Send(const OnIceConfigCallback& callback) { |
30 on_jingle_info_cb_ = callback; | 35 on_ice_config_callback_ = callback; |
31 scoped_ptr<buzz::XmlElement> iq_body( | 36 scoped_ptr<buzz::XmlElement> iq_body( |
32 new buzz::XmlElement(buzz::QN_JINGLE_INFO_QUERY, true)); | 37 new buzz::XmlElement(buzz::QN_JINGLE_INFO_QUERY, true)); |
33 request_ = iq_sender_.SendIq( | 38 request_ = iq_sender_.SendIq( |
34 buzz::STR_GET, buzz::STR_EMPTY, std::move(iq_body), | 39 buzz::STR_GET, buzz::STR_EMPTY, std::move(iq_body), |
35 base::Bind(&JingleInfoRequest::OnResponse, base::Unretained(this))); | 40 base::Bind(&JingleInfoRequest::OnResponse, base::Unretained(this))); |
36 if (!request_) { | 41 if (!request_) { |
37 // If we failed to send IqRequest it means that SignalStrategy is | 42 // If we failed to send IqRequest it means that SignalStrategy is |
38 // disconnected. Notify the caller. | 43 // disconnected. Notify the caller. |
39 std::vector<rtc::SocketAddress> stun_hosts; | 44 IceConfig config; |
40 std::vector<std::string> relay_hosts; | 45 on_ice_config_callback_.Run(config); |
41 std::string relay_token; | |
42 on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); | |
43 return; | 46 return; |
44 } | 47 } |
45 request_->SetTimeout(base::TimeDelta::FromSeconds(kRequestTimeoutSeconds)); | 48 request_->SetTimeout(base::TimeDelta::FromSeconds(kRequestTimeoutSeconds)); |
46 } | 49 } |
47 | 50 |
48 void JingleInfoRequest::OnResponse(IqRequest* request, | 51 void JingleInfoRequest::OnResponse(IqRequest* request, |
49 const buzz::XmlElement* stanza) { | 52 const buzz::XmlElement* stanza) { |
50 std::vector<rtc::SocketAddress> stun_hosts; | 53 IceConfig result; |
51 std::vector<std::string> relay_hosts; | |
52 std::string relay_token; | |
53 | 54 |
54 if (!stanza) { | 55 if (!stanza) { |
55 LOG(WARNING) << "Jingle info request has timed out."; | 56 LOG(WARNING) << "Jingle info request has timed out."; |
56 on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); | 57 on_ice_config_callback_.Run(result); |
57 return; | 58 return; |
58 } | 59 } |
59 | 60 |
60 const buzz::XmlElement* query = | 61 const buzz::XmlElement* query = |
61 stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); | 62 stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); |
62 if (query == nullptr) { | 63 if (query == nullptr) { |
63 LOG(WARNING) << "No Jingle info found in Jingle Info query response." | 64 LOG(WARNING) << "No Jingle info found in Jingle Info query response." |
64 << stanza->Str(); | 65 << stanza->Str(); |
65 on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); | 66 on_ice_config_callback_.Run(result); |
66 return; | 67 return; |
67 } | 68 } |
68 | 69 |
69 const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); | 70 const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); |
70 if (stun) { | 71 if (stun) { |
71 for (const buzz::XmlElement* server = | 72 for (const buzz::XmlElement* server = |
72 stun->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); | 73 stun->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); |
73 server != nullptr; | 74 server != nullptr; |
74 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { | 75 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { |
75 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); | 76 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); |
76 std::string port_str = server->Attr(buzz::QN_JINGLE_INFO_UDP); | 77 std::string port_str = server->Attr(buzz::QN_JINGLE_INFO_UDP); |
77 if (host != buzz::STR_EMPTY && port_str != buzz::STR_EMPTY) { | 78 if (host != buzz::STR_EMPTY && port_str != buzz::STR_EMPTY) { |
78 int port; | 79 int port; |
79 if (!base::StringToInt(port_str, &port)) { | 80 if (!base::StringToInt(port_str, &port)) { |
80 LOG(WARNING) << "Unable to parse port in stanza" << stanza->Str(); | 81 LOG(WARNING) << "Unable to parse port in stanza" << stanza->Str(); |
81 continue; | 82 continue; |
82 } | 83 } |
83 | 84 |
84 stun_hosts.push_back(rtc::SocketAddress(host, port)); | 85 result.stun_servers.push_back(rtc::SocketAddress(host, port)); |
85 } | 86 } |
86 } | 87 } |
87 } | 88 } |
88 | 89 |
89 const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); | 90 const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); |
90 if (relay) { | 91 if (relay) { |
91 relay_token = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); | 92 result.relay_token = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); |
92 for (const buzz::XmlElement* server = | 93 for (const buzz::XmlElement* server = |
93 relay->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); | 94 relay->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); |
94 server != nullptr; | 95 server != nullptr; |
95 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { | 96 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { |
96 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); | 97 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); |
97 if (host != buzz::STR_EMPTY) | 98 if (host != buzz::STR_EMPTY) |
98 relay_hosts.push_back(host); | 99 result.relay_servers.push_back(host); |
99 } | 100 } |
100 } | 101 } |
101 | 102 |
102 on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); | 103 result.expiration_time = |
| 104 base::Time::Now() + |
| 105 base::TimeDelta::FromSeconds(kJingleInfoUpdatePeriodSeconds); |
| 106 |
| 107 on_ice_config_callback_.Run(result); |
103 } | 108 } |
104 | 109 |
| 110 } // namespace protocol |
105 } // namespace remoting | 111 } // namespace remoting |
OLD | NEW |