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/host/register_support_host_request.h" | 5 #include "remoting/host/register_support_host_request.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 const char kSignatureTag[] = "signature"; | 29 const char kSignatureTag[] = "signature"; |
30 const char kSignatureTimeAttr[] = "time"; | 30 const char kSignatureTimeAttr[] = "time"; |
31 | 31 |
32 // Strings used to parse responses received from the bot. | 32 // Strings used to parse responses received from the bot. |
33 const char kRegisterQueryResultTag[] = "register-support-host-result"; | 33 const char kRegisterQueryResultTag[] = "register-support-host-result"; |
34 const char kSupportIdTag[] = "support-id"; | 34 const char kSupportIdTag[] = "support-id"; |
35 const char kSupportIdLifetimeTag[] = "support-id-lifetime"; | 35 const char kSupportIdLifetimeTag[] = "support-id-lifetime"; |
36 } | 36 } |
37 | 37 |
38 RegisterSupportHostRequest::RegisterSupportHostRequest() | 38 RegisterSupportHostRequest::RegisterSupportHostRequest() |
39 : message_loop_(NULL) { | 39 : signal_strategy_(NULL) { |
40 } | 40 } |
41 | 41 |
42 RegisterSupportHostRequest::~RegisterSupportHostRequest() { | 42 RegisterSupportHostRequest::~RegisterSupportHostRequest() { |
| 43 if (signal_strategy_) |
| 44 signal_strategy_->RemoveListener(this); |
43 } | 45 } |
44 | 46 |
45 bool RegisterSupportHostRequest::Init(HostConfig* config, | 47 bool RegisterSupportHostRequest::Init(SignalStrategy* signal_strategy, |
| 48 HostConfig* config, |
46 const RegisterCallback& callback) { | 49 const RegisterCallback& callback) { |
47 callback_ = callback; | |
48 | |
49 if (!key_pair_.Load(config)) { | 50 if (!key_pair_.Load(config)) { |
50 return false; | 51 return false; |
51 } | 52 } |
| 53 |
| 54 callback_ = callback; |
| 55 signal_strategy_ = signal_strategy; |
| 56 signal_strategy_->AddListener(this); |
| 57 iq_sender_.reset(new IqSender(signal_strategy_)); |
| 58 |
52 return true; | 59 return true; |
53 } | 60 } |
54 | 61 |
55 void RegisterSupportHostRequest::OnSignallingConnected( | 62 void RegisterSupportHostRequest::OnSignalStrategyStateChange( |
56 SignalStrategy* signal_strategy) { | 63 SignalStrategy::State state) { |
57 DCHECK(!callback_.is_null()); | 64 if (state == SignalStrategy::CONNECTED) { |
| 65 DCHECK(!callback_.is_null()); |
58 | 66 |
59 message_loop_ = MessageLoop::current(); | 67 request_.reset(iq_sender_->SendIq( |
60 | 68 buzz::STR_SET, kChromotingBotJid, |
61 iq_sender_.reset(new IqSender(signal_strategy)); | 69 CreateRegistrationRequest(signal_strategy_->GetLocalJid()), |
62 request_.reset(iq_sender_->SendIq( | 70 base::Bind(&RegisterSupportHostRequest::ProcessResponse, |
63 buzz::STR_SET, kChromotingBotJid, | 71 base::Unretained(this)))); |
64 CreateRegistrationRequest(signal_strategy->GetLocalJid()), | 72 } else if (state == SignalStrategy::DISCONNECTED) { |
65 base::Bind(&RegisterSupportHostRequest::ProcessResponse, | 73 // We will reach here if signaling fails to connect. |
66 base::Unretained(this)))); | 74 CallCallback(false, std::string(), base::TimeDelta()); |
| 75 } |
67 } | 76 } |
68 | 77 |
69 void RegisterSupportHostRequest::OnSignallingDisconnected() { | 78 bool RegisterSupportHostRequest::OnSignalStrategyIncomingStanza( |
70 if (!message_loop_) { | 79 const buzz::XmlElement* stanza) { |
71 // We will reach here with |message_loop_| NULL if the Host's | 80 return false; |
72 // XMPP connection attempt fails. | |
73 CHECK(!callback_.is_null()); | |
74 DCHECK(!request_.get()); | |
75 callback_.Run(false, std::string(), base::TimeDelta()); | |
76 return; | |
77 } | |
78 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
79 request_.reset(); | |
80 iq_sender_.reset(); | |
81 } | 81 } |
82 | 82 |
83 // Ignore any notifications other than signalling | |
84 // connected/disconnected events. | |
85 void RegisterSupportHostRequest::OnAccessDenied() { } | |
86 void RegisterSupportHostRequest::OnClientAuthenticated( | |
87 const std::string& jid) { } | |
88 void RegisterSupportHostRequest::OnClientDisconnected( | |
89 const std::string& jid) { } | |
90 void RegisterSupportHostRequest::OnShutdown() { } | |
91 | |
92 XmlElement* RegisterSupportHostRequest::CreateRegistrationRequest( | 83 XmlElement* RegisterSupportHostRequest::CreateRegistrationRequest( |
93 const std::string& jid) { | 84 const std::string& jid) { |
94 XmlElement* query = new XmlElement( | 85 XmlElement* query = new XmlElement( |
95 QName(kChromotingXmlNamespace, kRegisterQueryTag)); | 86 QName(kChromotingXmlNamespace, kRegisterQueryTag)); |
96 XmlElement* public_key = new XmlElement( | 87 XmlElement* public_key = new XmlElement( |
97 QName(kChromotingXmlNamespace, kPublicKeyTag)); | 88 QName(kChromotingXmlNamespace, kPublicKeyTag)); |
98 public_key->AddText(key_pair_.GetPublicKey()); | 89 public_key->AddText(key_pair_.GetPublicKey()); |
99 query->AddElement(public_key); | 90 query->AddElement(public_key); |
100 query->AddElement(CreateSignature(jid)); | 91 query->AddElement(CreateSignature(jid)); |
101 return query; | 92 return query; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 << "> is malformed in the host registration response: " | 160 << "> is malformed in the host registration response: " |
170 << response->Str(); | 161 << response->Str(); |
171 return false; | 162 return false; |
172 } | 163 } |
173 | 164 |
174 *support_id = support_id_element->BodyText(); | 165 *support_id = support_id_element->BodyText(); |
175 *lifetime = base::TimeDelta::FromSeconds(lifetime_int); | 166 *lifetime = base::TimeDelta::FromSeconds(lifetime_int); |
176 return true; | 167 return true; |
177 } | 168 } |
178 | 169 |
179 | |
180 void RegisterSupportHostRequest::ProcessResponse(const XmlElement* response) { | 170 void RegisterSupportHostRequest::ProcessResponse(const XmlElement* response) { |
181 DCHECK_EQ(message_loop_, MessageLoop::current()); | |
182 std::string support_id; | 171 std::string support_id; |
183 base::TimeDelta lifetime; | 172 base::TimeDelta lifetime; |
184 bool success = ParseResponse(response, &support_id, &lifetime); | 173 bool success = ParseResponse(response, &support_id, &lifetime); |
185 callback_.Run(success, support_id, lifetime); | 174 CallCallback(success, support_id, lifetime); |
| 175 } |
| 176 |
| 177 void RegisterSupportHostRequest::CallCallback( |
| 178 bool success, const std::string& support_id, base::TimeDelta lifetime) { |
| 179 // Cleanup state before calling the callback. |
| 180 request_.reset(); |
| 181 iq_sender_.reset(); |
| 182 signal_strategy_->RemoveListener(this); |
| 183 signal_strategy_ = NULL; |
| 184 |
| 185 RegisterCallback callback = callback_; |
| 186 callback_.Reset(); |
| 187 callback.Run(success, support_id, lifetime); |
186 } | 188 } |
187 | 189 |
188 } // namespace remoting | 190 } // namespace remoting |
OLD | NEW |