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 "jingle/notifier/listener/push_notifications_send_update_task.h"
9 #include "base/base64.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "jingle/notifier/listener/notification_constants.h"
13 #include "jingle/notifier/listener/xml_element_util.h"
14 #include "talk/xmpp/constants.h"
15 #include "talk/xmpp/jid.h"
16 #include "talk/xmpp/xmppclient.h"
17 #include "third_party/webrtc/libjingle/xmllite/qname.h"
18 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
22 PushNotificationsSendUpdateTask::PushNotificationsSendUpdateTask(
23 buzz::XmppTaskParentInterface* parent, const Notification& notification)
24 : XmppTask(parent), notification_(notification) {}
26 PushNotificationsSendUpdateTask::~PushNotificationsSendUpdateTask() {}
28 int PushNotificationsSendUpdateTask::ProcessStart() {
29 scoped_ptr<buzz::XmlElement> stanza(
30 MakeUpdateMessage(notification_,
31 GetClient()->jid().BareJid()));
32 DVLOG(1) << "Sending notification " << notification_.ToString()
33 << " as stanza " << XmlElementToString(*stanza);
34 if (SendStanza(stanza.get()) != buzz::XMPP_RETURN_OK) {
35 DLOG(WARNING) << "Could not send stanza " << XmlElementToString(*stanza);
40 buzz::XmlElement* PushNotificationsSendUpdateTask::MakeUpdateMessage(
41 const Notification& notification,
42 const buzz::Jid& to_jid_bare) {
43 DCHECK(to_jid_bare.IsBare());
44 const buzz::QName kQnPush(kPushNotificationsNamespace, "push");
45 const buzz::QName kQnChannel(buzz::STR_EMPTY, "channel");
46 const buzz::QName kQnData(kPushNotificationsNamespace, "data");
47 const buzz::QName kQnRecipient(kPushNotificationsNamespace, "recipient");
49 // Create our update stanza. The message is constructed as:
50 // <message from='{full jid}' to='{bare jid}' type='headline'>
51 // <push xmlns='google:push' channel='{channel}'>
52 // [<recipient to='{bare jid}'>{base-64 encoded data}</data>]*
53 // <data>{base-64 encoded data}</data>
57 buzz::XmlElement* message = new buzz::XmlElement(buzz::QN_MESSAGE);
58 message->AddAttr(buzz::QN_TO, to_jid_bare.Str());
59 message->AddAttr(buzz::QN_TYPE, "headline");
61 buzz::XmlElement* push = new buzz::XmlElement(kQnPush, true);
62 push->AddAttr(kQnChannel, notification.channel);
63 message->AddElement(push);
65 const RecipientList& recipients = notification.recipients;
66 for (size_t i = 0; i < recipients.size(); ++i) {
67 const Recipient& recipient = recipients[i];
68 buzz::XmlElement* recipient_element =
69 new buzz::XmlElement(kQnRecipient, true);
70 push->AddElement(recipient_element);
71 recipient_element->AddAttr(buzz::QN_TO, recipient.to);
72 if (!recipient.user_specific_data.empty()) {
73 std::string base64_data;
74 base::Base64Encode(recipient.user_specific_data, &base64_data);
75 recipient_element->SetBodyText(base64_data);
79 buzz::XmlElement* data = new buzz::XmlElement(kQnData, true);
80 std::string base64_data;
81 base::Base64Encode(notification.data, &base64_data);
82 data->SetBodyText(base64_data);
83 push->AddElement(data);
88 } // namespace notifier