Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/libjingle_xmpp/xmpp/pubsub_task.cc

Issue 2443903004: Add xmllite and xmpp sources to third_party/ (Closed)
Patch Set: Fix GN and sort includes Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2004 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 "third_party/libjingle_xmpp/xmpp/pubsub_task.h"
6
7 #include <map>
8 #include <memory>
9 #include <string>
10
11 #include "third_party/libjingle_xmpp/xmpp/constants.h"
12 #include "third_party/libjingle_xmpp/xmpp/xmppengine.h"
13 #include "webrtc/base/common.h"
14
15 namespace buzz {
16
17 PubsubTask::PubsubTask(XmppTaskParentInterface* parent,
18 const buzz::Jid& pubsub_node_jid)
19 : buzz::XmppTask(parent, buzz::XmppEngine::HL_SENDER),
20 pubsub_node_jid_(pubsub_node_jid) {
21 }
22
23 PubsubTask::~PubsubTask() {
24 }
25
26 // Checks for pubsub publish events as well as responses to get IQs.
27 bool PubsubTask::HandleStanza(const buzz::XmlElement* stanza) {
28 const buzz::QName& stanza_name(stanza->Name());
29 if (stanza_name == buzz::QN_MESSAGE) {
30 if (MatchStanzaFrom(stanza, pubsub_node_jid_)) {
31 const buzz::XmlElement* pubsub_event_item =
32 stanza->FirstNamed(QN_PUBSUB_EVENT);
33 if (pubsub_event_item != NULL) {
34 QueueStanza(pubsub_event_item);
35 return true;
36 }
37 }
38 } else if (stanza_name == buzz::QN_IQ) {
39 if (MatchResponseIq(stanza, pubsub_node_jid_, task_id())) {
40 const buzz::XmlElement* pubsub_item = stanza->FirstNamed(QN_PUBSUB);
41 if (pubsub_item != NULL) {
42 QueueStanza(pubsub_item);
43 return true;
44 }
45 }
46 }
47 return false;
48 }
49
50 int PubsubTask::ProcessResponse() {
51 const buzz::XmlElement* stanza = NextStanza();
52 if (stanza == NULL) {
53 return STATE_BLOCKED;
54 }
55
56 if (stanza->Attr(buzz::QN_TYPE) == buzz::STR_ERROR) {
57 OnPubsubError(stanza->FirstNamed(buzz::QN_ERROR));
58 return STATE_RESPONSE;
59 }
60
61 const buzz::QName& stanza_name(stanza->Name());
62 if (stanza_name == QN_PUBSUB_EVENT) {
63 HandlePubsubEventMessage(stanza);
64 } else if (stanza_name == QN_PUBSUB) {
65 HandlePubsubIqGetResponse(stanza);
66 }
67
68 return STATE_RESPONSE;
69 }
70
71 // Registers a function pointer to be called when the value of the pubsub
72 // node changes.
73 // Note that this does not actually change the XMPP pubsub
74 // subscription. All publish events are always received by everyone in the
75 // MUC. This function just controls whether the handle function will get
76 // called when the event is received.
77 bool PubsubTask::SubscribeToNode(const std::string& pubsub_node,
78 NodeHandler handler) {
79 subscribed_nodes_[pubsub_node] = handler;
80 std::unique_ptr<buzz::XmlElement> get_iq_request(
81 MakeIq(buzz::STR_GET, pubsub_node_jid_, task_id()));
82 if (!get_iq_request) {
83 return false;
84 }
85 buzz::XmlElement* pubsub_element = new buzz::XmlElement(QN_PUBSUB, true);
86 buzz::XmlElement* items_element = new buzz::XmlElement(QN_PUBSUB_ITEMS, true);
87
88 items_element->AddAttr(buzz::QN_NODE, pubsub_node);
89 pubsub_element->AddElement(items_element);
90 get_iq_request->AddElement(pubsub_element);
91
92 if (SendStanza(get_iq_request.get()) != buzz::XMPP_RETURN_OK) {
93 return false;
94 }
95
96 return true;
97 }
98
99 void PubsubTask::UnsubscribeFromNode(const std::string& pubsub_node) {
100 subscribed_nodes_.erase(pubsub_node);
101 }
102
103 void PubsubTask::OnPubsubError(const buzz::XmlElement* error_stanza) {
104 }
105
106 // Checks for a pubsub event message like the following:
107 //
108 // <message from="muvc-private-chat-some-id@groupchat.google.com"
109 // to="john@site.com/gcomm582B14C9">
110 // <event xmlns:"http://jabber.org/protocol/pubsub#event">
111 // <items node="node-name">
112 // <item id="some-id">
113 // <payload/>
114 // </item>
115 // </items>
116 // </event>
117 // </message>
118 //
119 // It also checks for retraction event messages like the following:
120 //
121 // <message from="muvc-private-chat-some-id@groupchat.google.com"
122 // to="john@site.com/gcomm582B14C9">
123 // <event xmlns:"http://jabber.org/protocol/pubsub#event">
124 // <items node="node-name">
125 // <retract id="some-id"/>
126 // </items>
127 // </event>
128 // </message>
129 void PubsubTask::HandlePubsubEventMessage(
130 const buzz::XmlElement* pubsub_event) {
131 ASSERT(pubsub_event->Name() == QN_PUBSUB_EVENT);
132 for (const buzz::XmlChild* child = pubsub_event->FirstChild();
133 child != NULL;
134 child = child->NextChild()) {
135 const buzz::XmlElement* child_element = child->AsElement();
136 const buzz::QName& child_name(child_element->Name());
137 if (child_name == QN_PUBSUB_EVENT_ITEMS) {
138 HandlePubsubItems(child_element);
139 }
140 }
141 }
142
143 // Checks for a response to an pubsub IQ get like the following:
144 //
145 // <iq from="muvc-private-chat-some-id@groupchat.google.com"
146 // to="john@site.com/gcomm582B14C9"
147 // type="result">
148 // <pubsub xmlns:"http://jabber.org/protocol/pubsub">
149 // <items node="node-name">
150 // <item id="some-id">
151 // <payload/>
152 // </item>
153 // </items>
154 // </event>
155 // </message>
156 void PubsubTask::HandlePubsubIqGetResponse(
157 const buzz::XmlElement* pubsub_iq_response) {
158 ASSERT(pubsub_iq_response->Name() == QN_PUBSUB);
159 for (const buzz::XmlChild* child = pubsub_iq_response->FirstChild();
160 child != NULL;
161 child = child->NextChild()) {
162 const buzz::XmlElement* child_element = child->AsElement();
163 const buzz::QName& child_name(child_element->Name());
164 if (child_name == QN_PUBSUB_ITEMS) {
165 HandlePubsubItems(child_element);
166 }
167 }
168 }
169
170 // Calls registered handlers in response to pubsub event or response to
171 // IQ pubsub get.
172 // 'items' is the child of a pubsub#event:event node or pubsub:pubsub node.
173 void PubsubTask::HandlePubsubItems(const buzz::XmlElement* items) {
174 ASSERT(items->HasAttr(QN_NODE));
175 const std::string& node_name(items->Attr(QN_NODE));
176 NodeSubscriptions::iterator iter = subscribed_nodes_.find(node_name);
177 if (iter != subscribed_nodes_.end()) {
178 NodeHandler handler = iter->second;
179 const buzz::XmlElement* item = items->FirstElement();
180 while (item != NULL) {
181 const buzz::QName& item_name(item->Name());
182 if (item_name != QN_PUBSUB_EVENT_ITEM &&
183 item_name != QN_PUBSUB_EVENT_RETRACT &&
184 item_name != QN_PUBSUB_ITEM) {
185 continue;
186 }
187
188 (this->*handler)(item);
189 item = item->NextElement();
190 }
191 return;
192 }
193 }
194
195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698