blob: 84f9ba6b920b76da1716e21a62af58fb489902dd [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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/constants.h"
12#include "webrtc/libjingle/xmpp/xmppclient.h"
13#include "webrtc/libjingle/xmpp/xmppengine.h"
14#include "webrtc/libjingle/xmpp/xmpptask.h"
15
16namespace buzz {
17
18XmppClientInterface::XmppClientInterface() {
19}
20
21XmppClientInterface::~XmppClientInterface() {
22}
23
24XmppTask::XmppTask(XmppTaskParentInterface* parent,
25 XmppEngine::HandlerLevel level)
26 : XmppTaskBase(parent), stopped_(false) {
tfarinaa41ab932015-10-30 16:08:48 -070027#if !defined(NDEBUG)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000028 debug_force_timeout_ = false;
29#endif
30
31 id_ = GetClient()->NextId();
32 GetClient()->AddXmppTask(this, level);
33 GetClient()->SignalDisconnected.connect(this, &XmppTask::OnDisconnect);
34}
35
36XmppTask::~XmppTask() {
37 StopImpl();
38}
39
40void XmppTask::StopImpl() {
41 while (NextStanza() != NULL) {}
42 if (!stopped_) {
43 GetClient()->RemoveXmppTask(this);
44 GetClient()->SignalDisconnected.disconnect(this);
45 stopped_ = true;
46 }
47}
48
49XmppReturnStatus XmppTask::SendStanza(const XmlElement* stanza) {
50 if (stopped_)
51 return XMPP_RETURN_BADSTATE;
52 return GetClient()->SendStanza(stanza);
53}
54
55XmppReturnStatus XmppTask::SendStanzaError(const XmlElement* element_original,
56 XmppStanzaError code,
57 const std::string& text) {
58 if (stopped_)
59 return XMPP_RETURN_BADSTATE;
60 return GetClient()->SendStanzaError(element_original, code, text);
61}
62
63void XmppTask::Stop() {
64 StopImpl();
65 Task::Stop();
66}
67
68void XmppTask::OnDisconnect() {
69 Error();
70}
71
72void XmppTask::QueueStanza(const XmlElement* stanza) {
tfarinaa41ab932015-10-30 16:08:48 -070073#if !defined(NDEBUG)
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000074 if (debug_force_timeout_)
75 return;
76#endif
77
78 stanza_queue_.push_back(new XmlElement(*stanza));
79 Wake();
80}
81
82const XmlElement* XmppTask::NextStanza() {
83 XmlElement* result = NULL;
84 if (!stanza_queue_.empty()) {
85 result = stanza_queue_.front();
86 stanza_queue_.pop_front();
87 }
88 next_stanza_.reset(result);
89 return result;
90}
91
92XmlElement* XmppTask::MakeIq(const std::string& type,
93 const buzz::Jid& to,
94 const std::string& id) {
95 XmlElement* result = new XmlElement(QN_IQ);
96 if (!type.empty())
97 result->AddAttr(QN_TYPE, type);
98 if (!to.IsEmpty())
99 result->AddAttr(QN_TO, to.Str());
100 if (!id.empty())
101 result->AddAttr(QN_ID, id);
102 return result;
103}
104
105XmlElement* XmppTask::MakeIqResult(const XmlElement * query) {
106 XmlElement* result = new XmlElement(QN_IQ);
107 result->AddAttr(QN_TYPE, STR_RESULT);
108 if (query->HasAttr(QN_FROM)) {
109 result->AddAttr(QN_TO, query->Attr(QN_FROM));
110 }
111 result->AddAttr(QN_ID, query->Attr(QN_ID));
112 return result;
113}
114
115bool XmppTask::MatchResponseIq(const XmlElement* stanza,
116 const Jid& to,
117 const std::string& id) {
118 if (stanza->Name() != QN_IQ)
119 return false;
120
121 if (stanza->Attr(QN_ID) != id)
122 return false;
123
124 return MatchStanzaFrom(stanza, to);
125}
126
127bool XmppTask::MatchStanzaFrom(const XmlElement* stanza,
128 const Jid& to) {
129 Jid from(stanza->Attr(QN_FROM));
130 if (from == to)
131 return true;
132
133 // We address the server as "", check if we are doing so here.
134 if (!to.IsEmpty())
135 return false;
136
137 // It is legal for the server to identify itself with "domain" or
138 // "myself@domain"
139 Jid me = GetClient()->jid();
140 return (from == Jid(me.domain())) || (from == me.BareJid());
141}
142
143bool XmppTask::MatchRequestIq(const XmlElement* stanza,
144 const std::string& type,
145 const QName& qn) {
146 if (stanza->Name() != QN_IQ)
147 return false;
148
149 if (stanza->Attr(QN_TYPE) != type)
150 return false;
151
152 if (stanza->FirstNamed(qn) == NULL)
153 return false;
154
155 return true;
156}
157
158}