3 * Copyright 2004--2006, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "webrtc/libjingle/xmpp/constants.h"
29 #include "webrtc/libjingle/xmpp/xmppclient.h"
30 #include "webrtc/libjingle/xmpp/xmppengine.h"
31 #include "webrtc/libjingle/xmpp/xmpptask.h"
35 XmppClientInterface::XmppClientInterface() {
38 XmppClientInterface::~XmppClientInterface() {
41 XmppTask::XmppTask(XmppTaskParentInterface* parent,
42 XmppEngine::HandlerLevel level)
43 : XmppTaskBase(parent), stopped_(false) {
45 debug_force_timeout_ = false;
48 id_ = GetClient()->NextId();
49 GetClient()->AddXmppTask(this, level);
50 GetClient()->SignalDisconnected.connect(this, &XmppTask::OnDisconnect);
53 XmppTask::~XmppTask() {
57 void XmppTask::StopImpl() {
58 while (NextStanza() != NULL) {}
60 GetClient()->RemoveXmppTask(this);
61 GetClient()->SignalDisconnected.disconnect(this);
66 XmppReturnStatus XmppTask::SendStanza(const XmlElement* stanza) {
68 return XMPP_RETURN_BADSTATE;
69 return GetClient()->SendStanza(stanza);
72 XmppReturnStatus XmppTask::SendStanzaError(const XmlElement* element_original,
74 const std::string& text) {
76 return XMPP_RETURN_BADSTATE;
77 return GetClient()->SendStanzaError(element_original, code, text);
80 void XmppTask::Stop() {
85 void XmppTask::OnDisconnect() {
89 void XmppTask::QueueStanza(const XmlElement* stanza) {
91 if (debug_force_timeout_)
95 stanza_queue_.push_back(new XmlElement(*stanza));
99 const XmlElement* XmppTask::NextStanza() {
100 XmlElement* result = NULL;
101 if (!stanza_queue_.empty()) {
102 result = stanza_queue_.front();
103 stanza_queue_.pop_front();
105 next_stanza_.reset(result);
109 XmlElement* XmppTask::MakeIq(const std::string& type,
111 const std::string& id) {
112 XmlElement* result = new XmlElement(QN_IQ);
114 result->AddAttr(QN_TYPE, type);
116 result->AddAttr(QN_TO, to.Str());
118 result->AddAttr(QN_ID, id);
122 XmlElement* XmppTask::MakeIqResult(const XmlElement * query) {
123 XmlElement* result = new XmlElement(QN_IQ);
124 result->AddAttr(QN_TYPE, STR_RESULT);
125 if (query->HasAttr(QN_FROM)) {
126 result->AddAttr(QN_TO, query->Attr(QN_FROM));
128 result->AddAttr(QN_ID, query->Attr(QN_ID));
132 bool XmppTask::MatchResponseIq(const XmlElement* stanza,
134 const std::string& id) {
135 if (stanza->Name() != QN_IQ)
138 if (stanza->Attr(QN_ID) != id)
141 return MatchStanzaFrom(stanza, to);
144 bool XmppTask::MatchStanzaFrom(const XmlElement* stanza,
146 Jid from(stanza->Attr(QN_FROM));
150 // We address the server as "", check if we are doing so here.
154 // It is legal for the server to identify itself with "domain" or
156 Jid me = GetClient()->jid();
157 return (from == Jid(me.domain())) || (from == me.BareJid());
160 bool XmppTask::MatchRequestIq(const XmlElement* stanza,
161 const std::string& type,
163 if (stanza->Name() != QN_IQ)
166 if (stanza->Attr(QN_TYPE) != type)
169 if (stanza->FirstNamed(qn) == NULL)