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