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