Index: remoting/host/gcd_notification_subscriber.cc |
diff --git a/remoting/host/gcd_notification_subscriber.cc b/remoting/host/gcd_notification_subscriber.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..181990000cabd5ac366edd3195aed2b7164bb720 |
--- /dev/null |
+++ b/remoting/host/gcd_notification_subscriber.cc |
@@ -0,0 +1,79 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
Sergey Ulanov
2015/05/07 18:22:38
remove (c)
John Williams
2015/05/15 00:51:12
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "remoting/host/gcd_notification_subscriber.h" |
+ |
+#include "base/bind.h" |
+#include "base/callback.h" |
+#include "remoting/base/logging.h" |
+#include "remoting/signaling/iq_sender.h" |
+#include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
+ |
+namespace remoting { |
+ |
+namespace { |
+ |
+const char kNsGooglePush[] = "google:push"; |
+ |
+} // namespace |
+ |
+GcdNotificationSubscriber::GcdNotificationSubscriber( |
+ SignalStrategy* signal_strategy) |
+ : signal_strategy_(signal_strategy) { |
+ signal_strategy_->AddListener(this); |
+} |
+ |
+GcdNotificationSubscriber::~GcdNotificationSubscriber() { |
+ signal_strategy_->RemoveListener(this); |
+} |
+ |
+void GcdNotificationSubscriber::OnSignalStrategyStateChange( |
+ SignalStrategy::State state) { |
+ if (state == SignalStrategy::CONNECTED) { |
+ DLOG(INFO) << "subscribing to GCD command notifications"; |
Sergey Ulanov
2015/05/07 18:22:38
nit: s/subscribing/Subscribing/
Add . at the end
John Williams
2015/05/15 00:51:12
Done.
|
+ |
+ // Convert full JID to bare JID. |
+ std::string bare_jid = signal_strategy_->GetLocalJid(); |
+ size_t slash_index = bare_jid.find('/'); |
Sergey Ulanov
2015/05/07 18:22:38
There are at least two other places where we need
John Williams
2015/05/15 00:51:12
Done.
|
+ if (slash_index != std::string::npos) { |
+ bare_jid.resize(slash_index); |
+ } |
+ |
+ // Build a subscription request. |
+ buzz::XmlElement* itemElement = |
Sergey Ulanov
2015/05/07 18:22:38
item_element
John Williams
2015/05/15 00:51:12
Done.
|
+ new buzz::XmlElement(buzz::QName(kNsGooglePush, "item")); |
+ itemElement->SetAttr(buzz::QName("", "channel"), "cloud_devices"); |
+ itemElement->SetAttr(buzz::QName("", "from"), ""); |
+ buzz::XmlElement* subscribeElement = |
Sergey Ulanov
2015/05/07 18:22:38
subscribe_element.
In other code that builds XmlEl
John Williams
2015/05/15 00:51:11
Done.
|
+ new buzz::XmlElement(buzz::QName(kNsGooglePush, "subscribe")); |
+ subscribeElement->AddElement(itemElement); |
+ |
+ // Send the request. |
+ iq_sender_.reset(new IqSender(signal_strategy_)); |
+ iq_request_ = iq_sender_->SendIq( |
+ "set", bare_jid, make_scoped_ptr(subscribeElement), |
+ base::Bind(&GcdNotificationSubscriber::OnSubscriptionResult, |
+ base::Unretained(this))); |
+ } |
+} |
+ |
+bool GcdNotificationSubscriber::OnSignalStrategyIncomingStanza( |
+ const buzz::XmlElement* stanza) { |
+ // Ignore all XMPP stanzas. |
+ return false; |
+} |
+ |
+void GcdNotificationSubscriber::OnSubscriptionResult( |
+ IqRequest* request, |
+ const buzz::XmlElement* response) { |
+ DCHECK_EQ(response->Attr(buzz::QName("", "type")), "result") |
Sergey Ulanov
2015/05/07 18:22:38
This shouldn't be DCHECK. Crashing in response to
John Williams
2015/05/15 00:51:12
Done.
|
+ << "invalid response type"; |
+ |
+ // The IqSender and IqRequest are no longer needed after receiving a |
+ // reply to the subscription request. |
+ iq_request_.reset(); |
+ iq_sender_.reset(); |
+} |
+ |
+} // namespace remoting |