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
3 // found in the LICENSE file.
5 #include "remoting/host/register_support_host_request.h"
8 #include "base/callback_helpers.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/time/time.h"
13 #include "remoting/base/constants.h"
14 #include "remoting/host/host_config.h"
15 #include "remoting/signaling/iq_sender.h"
16 #include "remoting/signaling/jid_util.h"
17 #include "remoting/signaling/signal_strategy.h"
18 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
19 #include "third_party/webrtc/libjingle/xmpp/constants.h"
22 using buzz::XmlElement
;
27 // Strings used in the request message we send to the bot.
28 const char kRegisterQueryTag
[] = "register-support-host";
29 const char kPublicKeyTag
[] = "public-key";
30 const char kSignatureTag
[] = "signature";
31 const char kSignatureTimeAttr
[] = "time";
33 // Strings used to parse responses received from the bot.
34 const char kRegisterQueryResultTag
[] = "register-support-host-result";
35 const char kSupportIdTag
[] = "support-id";
36 const char kSupportIdLifetimeTag
[] = "support-id-lifetime";
39 RegisterSupportHostRequest::RegisterSupportHostRequest(
40 SignalStrategy
* signal_strategy
,
41 scoped_refptr
<RsaKeyPair
> key_pair
,
42 const std::string
& directory_bot_jid
,
43 const RegisterCallback
& callback
)
44 : signal_strategy_(signal_strategy
),
46 directory_bot_jid_(directory_bot_jid
),
48 DCHECK(signal_strategy_
);
49 DCHECK(key_pair_
.get());
50 signal_strategy_
->AddListener(this);
51 iq_sender_
.reset(new IqSender(signal_strategy_
));
54 RegisterSupportHostRequest::~RegisterSupportHostRequest() {
56 signal_strategy_
->RemoveListener(this);
59 void RegisterSupportHostRequest::OnSignalStrategyStateChange(
60 SignalStrategy::State state
) {
61 if (state
== SignalStrategy::CONNECTED
) {
62 DCHECK(!callback_
.is_null());
64 request_
= iq_sender_
->SendIq(
65 buzz::STR_SET
, directory_bot_jid_
,
66 CreateRegistrationRequest(signal_strategy_
->GetLocalJid()).Pass(),
67 base::Bind(&RegisterSupportHostRequest::ProcessResponse
,
68 base::Unretained(this)));
69 } else if (state
== SignalStrategy::DISCONNECTED
) {
70 // We will reach here if signaling fails to connect.
71 CallCallback(false, std::string(), base::TimeDelta());
75 bool RegisterSupportHostRequest::OnSignalStrategyIncomingStanza(
76 const buzz::XmlElement
* stanza
) {
80 scoped_ptr
<XmlElement
> RegisterSupportHostRequest::CreateRegistrationRequest(
81 const std::string
& jid
) {
82 scoped_ptr
<XmlElement
> query(new XmlElement(
83 QName(kChromotingXmlNamespace
, kRegisterQueryTag
)));
84 XmlElement
* public_key
= new XmlElement(
85 QName(kChromotingXmlNamespace
, kPublicKeyTag
));
86 public_key
->AddText(key_pair_
->GetPublicKey());
87 query
->AddElement(public_key
);
88 query
->AddElement(CreateSignature(jid
).release());
92 scoped_ptr
<XmlElement
> RegisterSupportHostRequest::CreateSignature(
93 const std::string
& jid
) {
94 scoped_ptr
<XmlElement
> signature_tag(new XmlElement(
95 QName(kChromotingXmlNamespace
, kSignatureTag
)));
97 int64 time
= static_cast<int64
>(base::Time::Now().ToDoubleT());
98 std::string
time_str(base::Int64ToString(time
));
99 signature_tag
->AddAttr(
100 QName(kChromotingXmlNamespace
, kSignatureTimeAttr
), time_str
);
102 std::string message
= NormalizeJid(jid
) + ' ' + time_str
;
103 std::string
signature(key_pair_
->SignMessage(message
));
104 signature_tag
->AddText(signature
);
106 return signature_tag
.Pass();
109 bool RegisterSupportHostRequest::ParseResponse(const XmlElement
* response
,
110 std::string
* support_id
,
111 base::TimeDelta
* lifetime
) {
112 std::string type
= response
->Attr(buzz::QN_TYPE
);
113 if (type
== buzz::STR_ERROR
) {
114 LOG(ERROR
) << "Received error in response to heartbeat: "
119 // This method must only be called for error or result stanzas.
120 if (type
!= buzz::STR_RESULT
) {
121 LOG(ERROR
) << "Received unexpect stanza of type \"" << type
<< "\"";
125 const XmlElement
* result_element
= response
->FirstNamed(QName(
126 kChromotingXmlNamespace
, kRegisterQueryResultTag
));
127 if (!result_element
) {
128 LOG(ERROR
) << "<" << kRegisterQueryResultTag
129 << "> is missing in the host registration response: "
134 const XmlElement
* support_id_element
=
135 result_element
->FirstNamed(QName(kChromotingXmlNamespace
, kSupportIdTag
));
136 if (!support_id_element
) {
137 LOG(ERROR
) << "<" << kSupportIdTag
138 << "> is missing in the host registration response: "
143 const XmlElement
* lifetime_element
=
144 result_element
->FirstNamed(QName(kChromotingXmlNamespace
,
145 kSupportIdLifetimeTag
));
146 if (!lifetime_element
) {
147 LOG(ERROR
) << "<" << kSupportIdLifetimeTag
148 << "> is missing in the host registration response: "
154 if (!base::StringToInt(lifetime_element
->BodyText().c_str(), &lifetime_int
) ||
156 LOG(ERROR
) << "<" << kSupportIdLifetimeTag
157 << "> is malformed in the host registration response: "
162 *support_id
= support_id_element
->BodyText();
163 *lifetime
= base::TimeDelta::FromSeconds(lifetime_int
);
167 void RegisterSupportHostRequest::ProcessResponse(IqRequest
* request
,
168 const XmlElement
* response
) {
169 std::string support_id
;
170 base::TimeDelta lifetime
;
171 bool success
= ParseResponse(response
, &support_id
, &lifetime
);
172 CallCallback(success
, support_id
, lifetime
);
175 void RegisterSupportHostRequest::CallCallback(
176 bool success
, const std::string
& support_id
, base::TimeDelta lifetime
) {
177 // Cleanup state before calling the callback.
180 signal_strategy_
->RemoveListener(this);
181 signal_strategy_
= nullptr;
183 base::ResetAndReturn(&callback_
).Run(success
, support_id
, lifetime
);
186 } // namespace remoting