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