OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/jingle_glue/jingle_info_request.h" | 5 #include "remoting/jingle_glue/jingle_info_request.h" |
6 | 6 |
7 #include "base/task.h" | 7 #include "base/task.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util.h" |
8 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 11 #include "net/base/net_util.h" |
| 12 #include "remoting/jingle_glue/host_resolver.h" |
9 #include "remoting/jingle_glue/iq_request.h" | 13 #include "remoting/jingle_glue/iq_request.h" |
10 #include "third_party/libjingle/source/talk/base/socketaddress.h" | 14 #include "third_party/libjingle/source/talk/base/socketaddress.h" |
11 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 15 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
12 #include "third_party/libjingle/source/talk/xmpp/constants.h" | 16 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
13 | 17 |
14 namespace remoting { | 18 namespace remoting { |
15 | 19 |
16 JingleInfoRequest::JingleInfoRequest(IqRequest* request) | 20 |
17 : request_(request) { | 21 JingleInfoRequest::JingleInfoRequest(IqRequest* request, |
| 22 HostResolverFactory* host_resolver_factory) |
| 23 : host_resolver_factory_(host_resolver_factory), |
| 24 request_(request) { |
18 request_->set_callback(NewCallback(this, &JingleInfoRequest::OnResponse)); | 25 request_->set_callback(NewCallback(this, &JingleInfoRequest::OnResponse)); |
19 } | 26 } |
20 | 27 |
21 JingleInfoRequest::~JingleInfoRequest() { | 28 JingleInfoRequest::~JingleInfoRequest() { |
| 29 STLDeleteContainerPointers(stun_dns_requests_.begin(), |
| 30 stun_dns_requests_.end()); |
22 } | 31 } |
23 | 32 |
24 void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { | 33 void JingleInfoRequest::Send(const OnJingleInfoCallback& callback) { |
25 on_jingle_info_cb_ = callback; | 34 on_jingle_info_cb_ = callback; |
26 request_->SendIq(buzz::STR_GET, buzz::STR_EMPTY, | 35 request_->SendIq(buzz::STR_GET, buzz::STR_EMPTY, |
27 new buzz::XmlElement(buzz::QN_JINGLE_INFO_QUERY, true)); | 36 new buzz::XmlElement(buzz::QN_JINGLE_INFO_QUERY, true)); |
28 } | 37 } |
29 | 38 |
30 void JingleInfoRequest::OnResponse(const buzz::XmlElement* stanza) { | 39 void JingleInfoRequest::OnResponse(const buzz::XmlElement* stanza) { |
31 std::vector<std::string> relay_hosts; | |
32 std::vector<talk_base::SocketAddress> stun_hosts; | |
33 std::string relay_token; | |
34 | |
35 const buzz::XmlElement* query = | 40 const buzz::XmlElement* query = |
36 stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); | 41 stanza->FirstNamed(buzz::QN_JINGLE_INFO_QUERY); |
37 if (query == NULL) { | 42 if (query == NULL) { |
38 LOG(WARNING) << "No Jingle info found in Jingle Info query response." | 43 LOG(WARNING) << "No Jingle info found in Jingle Info query response." |
39 << stanza->Str(); | 44 << stanza->Str(); |
40 return; | 45 return; |
41 } | 46 } |
42 | 47 |
43 const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); | 48 const buzz::XmlElement* stun = query->FirstNamed(buzz::QN_JINGLE_INFO_STUN); |
44 if (stun) { | 49 if (stun) { |
45 for (const buzz::XmlElement* server = | 50 for (const buzz::XmlElement* server = |
46 stun->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); | 51 stun->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); |
47 server != NULL; | 52 server != NULL; |
48 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { | 53 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { |
49 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); | 54 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); |
50 std::string port_str = server->Attr(buzz::QN_JINGLE_INFO_UDP); | 55 std::string port_str = server->Attr(buzz::QN_JINGLE_INFO_UDP); |
51 if (host != buzz::STR_EMPTY && port_str != buzz::STR_EMPTY) { | 56 if (host != buzz::STR_EMPTY && port_str != buzz::STR_EMPTY) { |
52 int port; | 57 int port; |
53 if (!base::StringToInt(port_str, &port)) { | 58 if (!base::StringToInt(port_str, &port)) { |
54 LOG(WARNING) << "Unable to parse port in stanza" << stanza->Str(); | 59 LOG(WARNING) << "Unable to parse port in stanza" << stanza->Str(); |
55 } else { | 60 } else { |
56 stun_hosts.push_back(talk_base::SocketAddress(host, port)); | 61 net::IPAddressNumber ip_number; |
| 62 HostResolver* resolver = host_resolver_factory_->CreateHostResolver(); |
| 63 stun_dns_requests_.insert(resolver); |
| 64 resolver->SignalDone.connect( |
| 65 this, &JingleInfoRequest::OnStunAddressResponse); |
| 66 resolver->Resolve(talk_base::SocketAddress(host, port)); |
57 } | 67 } |
58 } | 68 } |
59 } | 69 } |
60 } | 70 } |
61 | 71 |
62 const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); | 72 const buzz::XmlElement* relay = query->FirstNamed(buzz::QN_JINGLE_INFO_RELAY); |
63 if (relay) { | 73 if (relay) { |
64 relay_token = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); | 74 relay_token_ = relay->TextNamed(buzz::QN_JINGLE_INFO_TOKEN); |
65 for (const buzz::XmlElement* server = | 75 for (const buzz::XmlElement* server = |
66 relay->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); | 76 relay->FirstNamed(buzz::QN_JINGLE_INFO_SERVER); |
67 server != NULL; | 77 server != NULL; |
68 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { | 78 server = server->NextNamed(buzz::QN_JINGLE_INFO_SERVER)) { |
69 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); | 79 std::string host = server->Attr(buzz::QN_JINGLE_INFO_HOST); |
70 if (host != buzz::STR_EMPTY) { | 80 if (host != buzz::STR_EMPTY) |
71 relay_hosts.push_back(host); | 81 relay_hosts_.push_back(host); |
72 } | |
73 } | 82 } |
74 } | 83 } |
75 | 84 |
76 on_jingle_info_cb_.Run(relay_token, relay_hosts, stun_hosts); | 85 if (stun_dns_requests_.empty()) |
| 86 on_jingle_info_cb_.Run(relay_token_, relay_hosts_, stun_hosts_); |
| 87 } |
| 88 |
| 89 void JingleInfoRequest::OnStunAddressResponse( |
| 90 HostResolver* resolver, const talk_base::SocketAddress& address) { |
| 91 if (!address.IsNil()) |
| 92 stun_hosts_.push_back(address); |
| 93 |
| 94 MessageLoop::current()->DeleteSoon(FROM_HERE, resolver); |
| 95 stun_dns_requests_.erase(resolver); |
| 96 |
| 97 if (stun_dns_requests_.empty()) |
| 98 on_jingle_info_cb_.Run(relay_token_, relay_hosts_, stun_hosts_); |
77 } | 99 } |
78 | 100 |
79 } // namespace remoting | 101 } // namespace remoting |
OLD | NEW |