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