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 #ifndef WEBRTC_LIBJINGLE_XMPP_XMPPTASK_H_ |
| 6 #define WEBRTC_LIBJINGLE_XMPP_XMPPTASK_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "third_party/libjingle_xmpp/xmpp/xmppengine.h" |
| 13 #include "webrtc/base/constructormagic.h" |
| 14 #include "webrtc/base/sigslot.h" |
| 15 #include "webrtc/base/task.h" |
| 16 #include "webrtc/base/taskparent.h" |
| 17 |
| 18 namespace buzz { |
| 19 |
| 20 ///////////////////////////////////////////////////////////////////// |
| 21 // |
| 22 // XMPPTASK |
| 23 // |
| 24 ///////////////////////////////////////////////////////////////////// |
| 25 // |
| 26 // See Task and XmppClient first. |
| 27 // |
| 28 // XmppTask is a task that is designed to go underneath XmppClient and be |
| 29 // useful there. It has a way of finding its XmppClient parent so you |
| 30 // can have it nested arbitrarily deep under an XmppClient and it can |
| 31 // still find the XMPP services. |
| 32 // |
| 33 // Tasks register themselves to listen to particular kinds of stanzas |
| 34 // that are sent out by the client. Rather than processing stanzas |
| 35 // right away, they should decide if they own the sent stanza, |
| 36 // and if so, queue it and Wake() the task, or if a stanza does not belong |
| 37 // to you, return false right away so the next XmppTask can take a crack. |
| 38 // This technique (synchronous recognize, but asynchronous processing) |
| 39 // allows you to have arbitrary logic for recognizing stanzas yet still, |
| 40 // for example, disconnect a client while processing a stanza - |
| 41 // without reentrancy problems. |
| 42 // |
| 43 ///////////////////////////////////////////////////////////////////// |
| 44 |
| 45 class XmppTask; |
| 46 |
| 47 // XmppClientInterface is an abstract interface for sending and |
| 48 // handling stanzas. It can be implemented for unit tests or |
| 49 // different network environments. It will usually be implemented by |
| 50 // XmppClient. |
| 51 class XmppClientInterface { |
| 52 public: |
| 53 XmppClientInterface(); |
| 54 virtual ~XmppClientInterface(); |
| 55 |
| 56 virtual XmppEngine::State GetState() const = 0; |
| 57 virtual const Jid& jid() const = 0; |
| 58 virtual std::string NextId() = 0; |
| 59 virtual XmppReturnStatus SendStanza(const XmlElement* stanza) = 0; |
| 60 virtual XmppReturnStatus SendStanzaError(const XmlElement* original_stanza, |
| 61 XmppStanzaError error_code, |
| 62 const std::string& message) = 0; |
| 63 virtual void AddXmppTask(XmppTask* task, XmppEngine::HandlerLevel level) = 0; |
| 64 virtual void RemoveXmppTask(XmppTask* task) = 0; |
| 65 sigslot::signal0<> SignalDisconnected; |
| 66 |
| 67 RTC_DISALLOW_COPY_AND_ASSIGN(XmppClientInterface); |
| 68 }; |
| 69 |
| 70 // XmppTaskParentInterface is the interface require for any parent of |
| 71 // an XmppTask. It needs, for example, a way to get an |
| 72 // XmppClientInterface. |
| 73 |
| 74 // We really ought to inherit from a TaskParentInterface, but we tried |
| 75 // that and it's way too complicated to change |
| 76 // Task/TaskParent/TaskRunner. For now, this works. |
| 77 class XmppTaskParentInterface : public rtc::Task { |
| 78 public: |
| 79 explicit XmppTaskParentInterface(rtc::TaskParent* parent) |
| 80 : Task(parent) { |
| 81 } |
| 82 virtual ~XmppTaskParentInterface() {} |
| 83 |
| 84 virtual XmppClientInterface* GetClient() = 0; |
| 85 |
| 86 RTC_DISALLOW_COPY_AND_ASSIGN(XmppTaskParentInterface); |
| 87 }; |
| 88 |
| 89 class XmppTaskBase : public XmppTaskParentInterface { |
| 90 public: |
| 91 explicit XmppTaskBase(XmppTaskParentInterface* parent) |
| 92 : XmppTaskParentInterface(parent), |
| 93 parent_(parent) { |
| 94 } |
| 95 virtual ~XmppTaskBase() {} |
| 96 |
| 97 virtual XmppClientInterface* GetClient() { |
| 98 return parent_->GetClient(); |
| 99 } |
| 100 |
| 101 protected: |
| 102 XmppTaskParentInterface* parent_; |
| 103 |
| 104 RTC_DISALLOW_COPY_AND_ASSIGN(XmppTaskBase); |
| 105 }; |
| 106 |
| 107 class XmppTask : public XmppTaskBase, |
| 108 public XmppStanzaHandler, |
| 109 public sigslot::has_slots<> |
| 110 { |
| 111 public: |
| 112 XmppTask(XmppTaskParentInterface* parent, |
| 113 XmppEngine::HandlerLevel level = XmppEngine::HL_NONE); |
| 114 virtual ~XmppTask(); |
| 115 |
| 116 std::string task_id() const { return id_; } |
| 117 void set_task_id(std::string id) { id_ = id; } |
| 118 |
| 119 #if !defined(NDEBUG) |
| 120 void set_debug_force_timeout(const bool f) { debug_force_timeout_ = f; } |
| 121 #endif |
| 122 |
| 123 virtual bool HandleStanza(const XmlElement* stanza) { return false; } |
| 124 |
| 125 protected: |
| 126 XmppReturnStatus SendStanza(const XmlElement* stanza); |
| 127 XmppReturnStatus SetResult(const std::string& code); |
| 128 XmppReturnStatus SendStanzaError(const XmlElement* element_original, |
| 129 XmppStanzaError code, |
| 130 const std::string& text); |
| 131 |
| 132 virtual void Stop(); |
| 133 virtual void OnDisconnect(); |
| 134 |
| 135 virtual void QueueStanza(const XmlElement* stanza); |
| 136 const XmlElement* NextStanza(); |
| 137 |
| 138 bool MatchStanzaFrom(const XmlElement* stanza, const Jid& match_jid); |
| 139 |
| 140 bool MatchResponseIq(const XmlElement* stanza, const Jid& to, |
| 141 const std::string& task_id); |
| 142 |
| 143 static bool MatchRequestIq(const XmlElement* stanza, const std::string& type, |
| 144 const QName& qn); |
| 145 static XmlElement *MakeIqResult(const XmlElement* query); |
| 146 static XmlElement *MakeIq(const std::string& type, |
| 147 const Jid& to, const std::string& task_id); |
| 148 |
| 149 // Returns true if the task is under the specified rate limit and updates the |
| 150 // rate limit accordingly |
| 151 bool VerifyTaskRateLimit(const std::string task_name, int max_count, |
| 152 int per_x_seconds); |
| 153 |
| 154 private: |
| 155 void StopImpl(); |
| 156 |
| 157 bool stopped_; |
| 158 std::deque<XmlElement*> stanza_queue_; |
| 159 std::unique_ptr<XmlElement> next_stanza_; |
| 160 std::string id_; |
| 161 |
| 162 #if !defined(NDEBUG) |
| 163 bool debug_force_timeout_; |
| 164 #endif |
| 165 }; |
| 166 |
| 167 } // namespace buzz |
| 168 |
| 169 #endif // WEBRTC_LIBJINGLE_XMPP_XMPPTASK_H_ |
OLD | NEW |