OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "remoting/signaling/push_notification_subscriber.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "remoting/base/logging.h" |
| 10 #include "remoting/signaling/iq_sender.h" |
| 11 #include "remoting/signaling/jid_util.h" |
| 12 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 13 |
| 14 namespace remoting { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kGooglePushNamespace[] = "google:push"; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 PushNotificationSubscriber::Subscription::Subscription() { |
| 23 } |
| 24 |
| 25 PushNotificationSubscriber::Subscription::~Subscription() { |
| 26 } |
| 27 |
| 28 PushNotificationSubscriber::PushNotificationSubscriber( |
| 29 SignalStrategy* signal_strategy, |
| 30 const SubscriptionList& subscriptions) |
| 31 : signal_strategy_(signal_strategy), subscriptions_(subscriptions) { |
| 32 signal_strategy_->AddListener(this); |
| 33 } |
| 34 |
| 35 PushNotificationSubscriber::~PushNotificationSubscriber() { |
| 36 signal_strategy_->RemoveListener(this); |
| 37 } |
| 38 |
| 39 void PushNotificationSubscriber::OnSignalStrategyStateChange( |
| 40 SignalStrategy::State state) { |
| 41 if (state == SignalStrategy::CONNECTED) { |
| 42 for (const Subscription& subscription : subscriptions_) { |
| 43 Subscribe(subscription); |
| 44 } |
| 45 subscriptions_.clear(); // no longer needed |
| 46 } |
| 47 } |
| 48 |
| 49 bool PushNotificationSubscriber::OnSignalStrategyIncomingStanza( |
| 50 const buzz::XmlElement* stanza) { |
| 51 // Ignore all XMPP stanzas. |
| 52 return false; |
| 53 } |
| 54 |
| 55 void PushNotificationSubscriber::Subscribe(const Subscription& subscription) { |
| 56 VLOG(0) << "Subscribing to push notifications on channel: " |
| 57 << subscription.channel << "."; |
| 58 |
| 59 std::string bare_jid; |
| 60 SplitJidResource(signal_strategy_->GetLocalJid(), &bare_jid, nullptr); |
| 61 |
| 62 // Build a subscription request. |
| 63 buzz::XmlElement* subscribe_element = |
| 64 new buzz::XmlElement(buzz::QName(kGooglePushNamespace, "subscribe")); |
| 65 buzz::XmlElement* item_element = |
| 66 new buzz::XmlElement(buzz::QName(kGooglePushNamespace, "item")); |
| 67 subscribe_element->AddElement(item_element); |
| 68 item_element->SetAttr(buzz::QName(std::string(), "channel"), |
| 69 subscription.channel); |
| 70 item_element->SetAttr(buzz::QName(std::string(), "from"), subscription.from); |
| 71 |
| 72 // Send the request. |
| 73 iq_sender_.reset(new IqSender(signal_strategy_)); |
| 74 iq_request_ = iq_sender_->SendIq( |
| 75 "set", bare_jid, make_scoped_ptr(subscribe_element), |
| 76 base::Bind(&PushNotificationSubscriber::OnSubscriptionResult, |
| 77 base::Unretained(this))); |
| 78 } |
| 79 |
| 80 void PushNotificationSubscriber::OnSubscriptionResult( |
| 81 IqRequest* request, |
| 82 const buzz::XmlElement* response) { |
| 83 std::string response_type = |
| 84 response->Attr(buzz::QName(std::string(), "type")); |
| 85 if (response_type != "result") { |
| 86 LOG(ERROR) << "Invalid response type for subscription: " << response_type; |
| 87 } |
| 88 |
| 89 // The IqSender and IqRequest are no longer needed after receiving a |
| 90 // reply to the subscription request. |
| 91 iq_request_.reset(); |
| 92 iq_sender_.reset(); |
| 93 } |
| 94 |
| 95 } // namespace remoting |
OLD | NEW |