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

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

Issue 2443903004: Add xmllite and xmpp sources to third_party/ (Closed)
Patch Set: Fix GN and sort includes Created 4 years 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 <algorithm>
6 #include <vector>
7 #include "third_party/libjingle_xmpp/xmpp/constants.h"
8 #include "third_party/libjingle_xmpp/xmpp/xmppengineimpl.h"
9 #include "webrtc/base/common.h"
10
11 namespace buzz {
12
13 class XmppIqEntry {
14 XmppIqEntry(const std::string & id, const std::string & to,
15 XmppEngine * pxce, XmppIqHandler * iq_handler) :
16 id_(id),
17 to_(to),
18 engine_(pxce),
19 iq_handler_(iq_handler) {
20 }
21
22 private:
23 friend class XmppEngineImpl;
24
25 const std::string id_;
26 const std::string to_;
27 XmppEngine * const engine_;
28 XmppIqHandler * const iq_handler_;
29 };
30
31
32 XmppReturnStatus
33 XmppEngineImpl::SendIq(const XmlElement * element, XmppIqHandler * iq_handler,
34 XmppIqCookie* cookie) {
35 if (state_ == STATE_CLOSED)
36 return XMPP_RETURN_BADSTATE;
37 if (NULL == iq_handler)
38 return XMPP_RETURN_BADARGUMENT;
39 if (!element || element->Name() != QN_IQ)
40 return XMPP_RETURN_BADARGUMENT;
41
42 const std::string& type = element->Attr(QN_TYPE);
43 if (type != "get" && type != "set")
44 return XMPP_RETURN_BADARGUMENT;
45
46 if (!element->HasAttr(QN_ID))
47 return XMPP_RETURN_BADARGUMENT;
48 const std::string& id = element->Attr(QN_ID);
49
50 XmppIqEntry * iq_entry = new XmppIqEntry(id,
51 element->Attr(QN_TO),
52 this, iq_handler);
53 iq_entries_->push_back(iq_entry);
54 SendStanza(element);
55
56 if (cookie)
57 *cookie = iq_entry;
58
59 return XMPP_RETURN_OK;
60 }
61
62
63 XmppReturnStatus
64 XmppEngineImpl::RemoveIqHandler(XmppIqCookie cookie,
65 XmppIqHandler ** iq_handler) {
66
67 std::vector<XmppIqEntry*, std::allocator<XmppIqEntry*> >::iterator pos;
68
69 pos = std::find(iq_entries_->begin(),
70 iq_entries_->end(),
71 reinterpret_cast<XmppIqEntry*>(cookie));
72
73 if (pos == iq_entries_->end())
74 return XMPP_RETURN_BADARGUMENT;
75
76 XmppIqEntry* entry = *pos;
77 iq_entries_->erase(pos);
78 if (iq_handler)
79 *iq_handler = entry->iq_handler_;
80 delete entry;
81
82 return XMPP_RETURN_OK;
83 }
84
85 void
86 XmppEngineImpl::DeleteIqCookies() {
87 for (size_t i = 0; i < iq_entries_->size(); i += 1) {
88 XmppIqEntry * iq_entry_ = (*iq_entries_)[i];
89 (*iq_entries_)[i] = NULL;
90 delete iq_entry_;
91 }
92 iq_entries_->clear();
93 }
94
95 static void
96 AecImpl(XmlElement * error_element, const QName & name,
97 const char * type, const char * code) {
98 error_element->AddElement(new XmlElement(QN_ERROR));
99 error_element->AddAttr(QN_CODE, code, 1);
100 error_element->AddAttr(QN_TYPE, type, 1);
101 error_element->AddElement(new XmlElement(name, true), 1);
102 }
103
104
105 static void
106 AddErrorCode(XmlElement * error_element, XmppStanzaError code) {
107 switch (code) {
108 case XSE_BAD_REQUEST:
109 AecImpl(error_element, QN_STANZA_BAD_REQUEST, "modify", "400");
110 break;
111 case XSE_CONFLICT:
112 AecImpl(error_element, QN_STANZA_CONFLICT, "cancel", "409");
113 break;
114 case XSE_FEATURE_NOT_IMPLEMENTED:
115 AecImpl(error_element, QN_STANZA_FEATURE_NOT_IMPLEMENTED,
116 "cancel", "501");
117 break;
118 case XSE_FORBIDDEN:
119 AecImpl(error_element, QN_STANZA_FORBIDDEN, "auth", "403");
120 break;
121 case XSE_GONE:
122 AecImpl(error_element, QN_STANZA_GONE, "modify", "302");
123 break;
124 case XSE_INTERNAL_SERVER_ERROR:
125 AecImpl(error_element, QN_STANZA_INTERNAL_SERVER_ERROR, "wait", "500");
126 break;
127 case XSE_ITEM_NOT_FOUND:
128 AecImpl(error_element, QN_STANZA_ITEM_NOT_FOUND, "cancel", "404");
129 break;
130 case XSE_JID_MALFORMED:
131 AecImpl(error_element, QN_STANZA_JID_MALFORMED, "modify", "400");
132 break;
133 case XSE_NOT_ACCEPTABLE:
134 AecImpl(error_element, QN_STANZA_NOT_ACCEPTABLE, "cancel", "406");
135 break;
136 case XSE_NOT_ALLOWED:
137 AecImpl(error_element, QN_STANZA_NOT_ALLOWED, "cancel", "405");
138 break;
139 case XSE_PAYMENT_REQUIRED:
140 AecImpl(error_element, QN_STANZA_PAYMENT_REQUIRED, "auth", "402");
141 break;
142 case XSE_RECIPIENT_UNAVAILABLE:
143 AecImpl(error_element, QN_STANZA_RECIPIENT_UNAVAILABLE, "wait", "404");
144 break;
145 case XSE_REDIRECT:
146 AecImpl(error_element, QN_STANZA_REDIRECT, "modify", "302");
147 break;
148 case XSE_REGISTRATION_REQUIRED:
149 AecImpl(error_element, QN_STANZA_REGISTRATION_REQUIRED, "auth", "407");
150 break;
151 case XSE_SERVER_NOT_FOUND:
152 AecImpl(error_element, QN_STANZA_REMOTE_SERVER_NOT_FOUND,
153 "cancel", "404");
154 break;
155 case XSE_SERVER_TIMEOUT:
156 AecImpl(error_element, QN_STANZA_REMOTE_SERVER_TIMEOUT, "wait", "502");
157 break;
158 case XSE_RESOURCE_CONSTRAINT:
159 AecImpl(error_element, QN_STANZA_RESOURCE_CONSTRAINT, "wait", "500");
160 break;
161 case XSE_SERVICE_UNAVAILABLE:
162 AecImpl(error_element, QN_STANZA_SERVICE_UNAVAILABLE, "cancel", "503");
163 break;
164 case XSE_SUBSCRIPTION_REQUIRED:
165 AecImpl(error_element, QN_STANZA_SUBSCRIPTION_REQUIRED, "auth", "407");
166 break;
167 case XSE_UNDEFINED_CONDITION:
168 AecImpl(error_element, QN_STANZA_UNDEFINED_CONDITION, "wait", "500");
169 break;
170 case XSE_UNEXPECTED_REQUEST:
171 AecImpl(error_element, QN_STANZA_UNEXPECTED_REQUEST, "wait", "400");
172 break;
173 }
174 }
175
176
177 XmppReturnStatus
178 XmppEngineImpl::SendStanzaError(const XmlElement * element_original,
179 XmppStanzaError code,
180 const std::string & text) {
181
182 if (state_ == STATE_CLOSED)
183 return XMPP_RETURN_BADSTATE;
184
185 XmlElement error_element(element_original->Name());
186 error_element.AddAttr(QN_TYPE, "error");
187
188 // copy attrs, copy 'from' to 'to' and strip 'from'
189 for (const XmlAttr * attribute = element_original->FirstAttr();
190 attribute; attribute = attribute->NextAttr()) {
191 QName name = attribute->Name();
192 if (name == QN_TO)
193 continue; // no need to put a from attr. Server will stamp stanza
194 else if (name == QN_FROM)
195 name = QN_TO;
196 else if (name == QN_TYPE)
197 continue;
198 error_element.AddAttr(name, attribute->Value());
199 }
200
201 // copy children
202 for (const XmlChild * child = element_original->FirstChild();
203 child;
204 child = child->NextChild()) {
205 if (child->IsText()) {
206 error_element.AddText(child->AsText()->Text());
207 } else {
208 error_element.AddElement(new XmlElement(*(child->AsElement())));
209 }
210 }
211
212 // add error information
213 AddErrorCode(&error_element, code);
214 if (text != STR_EMPTY) {
215 XmlElement * text_element = new XmlElement(QN_STANZA_TEXT, true);
216 text_element->AddText(text);
217 error_element.AddElement(text_element);
218 }
219
220 SendStanza(&error_element);
221
222 return XMPP_RETURN_OK;
223 }
224
225
226 bool
227 XmppEngineImpl::HandleIqResponse(const XmlElement * element) {
228 if (iq_entries_->empty())
229 return false;
230 if (element->Name() != QN_IQ)
231 return false;
232 std::string type = element->Attr(QN_TYPE);
233 if (type != "result" && type != "error")
234 return false;
235 if (!element->HasAttr(QN_ID))
236 return false;
237 std::string id = element->Attr(QN_ID);
238 std::string from = element->Attr(QN_FROM);
239
240 for (std::vector<XmppIqEntry *>::iterator it = iq_entries_->begin();
241 it != iq_entries_->end(); it += 1) {
242 XmppIqEntry * iq_entry = *it;
243 if (iq_entry->id_ == id && iq_entry->to_ == from) {
244 iq_entries_->erase(it);
245 iq_entry->iq_handler_->IqResponse(iq_entry, element);
246 delete iq_entry;
247 return true;
248 }
249 }
250
251 return false;
252 }
253
254 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698