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_XMPPENGINEIMPL_H_ |
| 6 #define WEBRTC_LIBJINGLE_XMPP_XMPPENGINEIMPL_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <sstream> |
| 10 #include <vector> |
| 11 |
| 12 #include "third_party/libjingle_xmpp/xmpp/xmppengine.h" |
| 13 #include "third_party/libjingle_xmpp/xmpp/xmppstanzaparser.h" |
| 14 |
| 15 namespace buzz { |
| 16 |
| 17 class XmppLoginTask; |
| 18 class XmppEngine; |
| 19 class XmppIqEntry; |
| 20 class SaslHandler; |
| 21 class SaslMechanism; |
| 22 |
| 23 //! The XMPP connection engine. |
| 24 //! This engine implements the client side of the 'core' XMPP protocol. |
| 25 //! To use it, register an XmppOutputHandler to handle socket output |
| 26 //! and pass socket input to HandleInput. Then application code can |
| 27 //! set up the connection with a user, password, and other settings, |
| 28 //! and then call Connect() to initiate the connection. |
| 29 //! An application can listen for events and receive stanzas by |
| 30 //! registering an XmppStanzaHandler via AddStanzaHandler(). |
| 31 class XmppEngineImpl : public XmppEngine { |
| 32 public: |
| 33 XmppEngineImpl(); |
| 34 virtual ~XmppEngineImpl(); |
| 35 |
| 36 // SOCKET INPUT AND OUTPUT ------------------------------------------------ |
| 37 |
| 38 //! Registers the handler for socket output |
| 39 virtual XmppReturnStatus SetOutputHandler(XmppOutputHandler *pxoh); |
| 40 |
| 41 //! Provides socket input to the engine |
| 42 virtual XmppReturnStatus HandleInput(const char* bytes, size_t len); |
| 43 |
| 44 //! Advises the engine that the socket has closed |
| 45 virtual XmppReturnStatus ConnectionClosed(int subcode); |
| 46 |
| 47 // SESSION SETUP --------------------------------------------------------- |
| 48 |
| 49 //! Indicates the (bare) JID for the user to use. |
| 50 virtual XmppReturnStatus SetUser(const Jid& jid); |
| 51 |
| 52 //! Get the login (bare) JID. |
| 53 virtual const Jid& GetUser(); |
| 54 |
| 55 //! Indicates the autentication to use. Takes ownership of the object. |
| 56 virtual XmppReturnStatus SetSaslHandler(SaslHandler* sasl_handler); |
| 57 |
| 58 //! Sets whether TLS will be used within the connection (default true). |
| 59 virtual XmppReturnStatus SetTls(TlsOptions use_tls); |
| 60 |
| 61 //! Sets an alternate domain from which we allows TLS certificates. |
| 62 //! This is for use in the case where a we want to allow a proxy to |
| 63 //! serve up its own certificate rather than one owned by the underlying |
| 64 //! domain. |
| 65 virtual XmppReturnStatus SetTlsServer(const std::string& proxy_hostname, |
| 66 const std::string& proxy_domain); |
| 67 |
| 68 //! Gets whether TLS will be used within the connection. |
| 69 virtual TlsOptions GetTls(); |
| 70 |
| 71 //! Sets the request resource name, if any (optional). |
| 72 //! Note that the resource name may be overridden by the server; after |
| 73 //! binding, the actual resource name is available as part of FullJid(). |
| 74 virtual XmppReturnStatus SetRequestedResource(const std::string& resource); |
| 75 |
| 76 //! Gets the request resource name. |
| 77 virtual const std::string& GetRequestedResource(); |
| 78 |
| 79 //! Sets language |
| 80 virtual void SetLanguage(const std::string& lang) { |
| 81 lang_ = lang; |
| 82 } |
| 83 |
| 84 // SESSION MANAGEMENT --------------------------------------------------- |
| 85 |
| 86 //! Set callback for state changes. |
| 87 virtual XmppReturnStatus SetSessionHandler(XmppSessionHandler* handler); |
| 88 |
| 89 //! Initiates the XMPP connection. |
| 90 //! After supplying connection settings, call this once to initiate, |
| 91 //! (optionally) encrypt, authenticate, and bind the connection. |
| 92 virtual XmppReturnStatus Connect(); |
| 93 |
| 94 //! The current engine state. |
| 95 virtual State GetState() { return state_; } |
| 96 |
| 97 //! Returns true if the connection is encrypted (under TLS) |
| 98 virtual bool IsEncrypted() { return encrypted_; } |
| 99 |
| 100 //! The error code. |
| 101 //! Consult this after XmppOutputHandler.OnClose(). |
| 102 virtual Error GetError(int *subcode) { |
| 103 if (subcode) { |
| 104 *subcode = subcode_; |
| 105 } |
| 106 return error_code_; |
| 107 } |
| 108 |
| 109 //! The stream:error stanza, when the error is XmppEngine::ERROR_STREAM. |
| 110 //! Notice the stanza returned is owned by the XmppEngine and |
| 111 //! is deleted when the engine is destroyed. |
| 112 virtual const XmlElement* GetStreamError() { return stream_error_.get(); } |
| 113 |
| 114 //! Closes down the connection. |
| 115 //! Sends CloseConnection to output, and disconnects and registered |
| 116 //! session handlers. After Disconnect completes, it is guaranteed |
| 117 //! that no further callbacks will be made. |
| 118 virtual XmppReturnStatus Disconnect(); |
| 119 |
| 120 // APPLICATION USE ------------------------------------------------------- |
| 121 |
| 122 //! Adds a listener for session events. |
| 123 //! Stanza delivery is chained to session handlers; the first to |
| 124 //! return 'true' is the last to get each stanza. |
| 125 virtual XmppReturnStatus AddStanzaHandler(XmppStanzaHandler* handler, |
| 126 XmppEngine::HandlerLevel level); |
| 127 |
| 128 //! Removes a listener for session events. |
| 129 virtual XmppReturnStatus RemoveStanzaHandler(XmppStanzaHandler* handler); |
| 130 |
| 131 //! Sends a stanza to the server. |
| 132 virtual XmppReturnStatus SendStanza(const XmlElement* stanza); |
| 133 |
| 134 //! Sends raw text to the server |
| 135 virtual XmppReturnStatus SendRaw(const std::string& text); |
| 136 |
| 137 //! Sends an iq to the server, and registers a callback for the result. |
| 138 //! Returns the cookie passed to the result handler. |
| 139 virtual XmppReturnStatus SendIq(const XmlElement* stanza, |
| 140 XmppIqHandler* iq_handler, |
| 141 XmppIqCookie* cookie); |
| 142 |
| 143 //! Unregisters an iq callback handler given its cookie. |
| 144 //! No callback will come to this handler after it's unregistered. |
| 145 virtual XmppReturnStatus RemoveIqHandler(XmppIqCookie cookie, |
| 146 XmppIqHandler** iq_handler); |
| 147 |
| 148 //! Forms and sends an error in response to the given stanza. |
| 149 //! Swaps to and from, sets type to "error", and adds error information |
| 150 //! based on the passed code. Text is optional and may be STR_EMPTY. |
| 151 virtual XmppReturnStatus SendStanzaError(const XmlElement* pelOriginal, |
| 152 XmppStanzaError code, |
| 153 const std::string& text); |
| 154 |
| 155 //! The fullly bound JID. |
| 156 //! This JID is only valid after binding has succeeded. If the value |
| 157 //! is JID_NULL, the binding has not succeeded. |
| 158 virtual const Jid& FullJid() { return bound_jid_; } |
| 159 |
| 160 //! The next unused iq id for this connection. |
| 161 //! Call this when building iq stanzas, to ensure that each iq |
| 162 //! gets its own unique id. |
| 163 virtual std::string NextId(); |
| 164 |
| 165 private: |
| 166 friend class XmppLoginTask; |
| 167 friend class XmppIqEntry; |
| 168 |
| 169 void IncomingStanza(const XmlElement *stanza); |
| 170 void IncomingStart(const XmlElement *stanza); |
| 171 void IncomingEnd(bool isError); |
| 172 |
| 173 void InternalSendStart(const std::string& domainName); |
| 174 void InternalSendStanza(const XmlElement* stanza); |
| 175 std::string ChooseBestSaslMechanism( |
| 176 const std::vector<std::string>& mechanisms, bool encrypted); |
| 177 SaslMechanism* GetSaslMechanism(const std::string& name); |
| 178 void SignalBound(const Jid& fullJid); |
| 179 void SignalStreamError(const XmlElement* streamError); |
| 180 void SignalError(Error errorCode, int subCode); |
| 181 bool HasError(); |
| 182 void DeleteIqCookies(); |
| 183 bool HandleIqResponse(const XmlElement* element); |
| 184 void StartTls(const std::string& domain); |
| 185 void RaiseReset() { raised_reset_ = true; } |
| 186 |
| 187 class StanzaParseHandler : public XmppStanzaParseHandler { |
| 188 public: |
| 189 StanzaParseHandler(XmppEngineImpl* outer) : outer_(outer) {} |
| 190 virtual ~StanzaParseHandler() {} |
| 191 |
| 192 virtual void StartStream(const XmlElement* stream) { |
| 193 outer_->IncomingStart(stream); |
| 194 } |
| 195 virtual void Stanza(const XmlElement* stanza) { |
| 196 outer_->IncomingStanza(stanza); |
| 197 } |
| 198 virtual void EndStream() { |
| 199 outer_->IncomingEnd(false); |
| 200 } |
| 201 virtual void XmlError() { |
| 202 outer_->IncomingEnd(true); |
| 203 } |
| 204 |
| 205 private: |
| 206 XmppEngineImpl* const outer_; |
| 207 }; |
| 208 |
| 209 class EnterExit { |
| 210 public: |
| 211 EnterExit(XmppEngineImpl* engine); |
| 212 ~EnterExit(); |
| 213 private: |
| 214 XmppEngineImpl* engine_; |
| 215 State state_; |
| 216 }; |
| 217 |
| 218 friend class StanzaParseHandler; |
| 219 friend class EnterExit; |
| 220 |
| 221 StanzaParseHandler stanza_parse_handler_; |
| 222 XmppStanzaParser stanza_parser_; |
| 223 |
| 224 // state |
| 225 int engine_entered_; |
| 226 Jid user_jid_; |
| 227 std::string password_; |
| 228 std::string requested_resource_; |
| 229 TlsOptions tls_option_; |
| 230 std::string tls_server_hostname_; |
| 231 std::string tls_server_domain_; |
| 232 std::unique_ptr<XmppLoginTask> login_task_; |
| 233 std::string lang_; |
| 234 |
| 235 int next_id_; |
| 236 Jid bound_jid_; |
| 237 State state_; |
| 238 bool encrypted_; |
| 239 Error error_code_; |
| 240 int subcode_; |
| 241 std::unique_ptr<XmlElement> stream_error_; |
| 242 bool raised_reset_; |
| 243 XmppOutputHandler* output_handler_; |
| 244 XmppSessionHandler* session_handler_; |
| 245 |
| 246 XmlnsStack xmlns_stack_; |
| 247 |
| 248 typedef std::vector<XmppStanzaHandler*> StanzaHandlerVector; |
| 249 std::unique_ptr<StanzaHandlerVector> stanza_handlers_[HL_COUNT]; |
| 250 |
| 251 typedef std::vector<XmppIqEntry*> IqEntryVector; |
| 252 std::unique_ptr<IqEntryVector> iq_entries_; |
| 253 |
| 254 std::unique_ptr<SaslHandler> sasl_handler_; |
| 255 |
| 256 std::unique_ptr<std::stringstream> output_; |
| 257 }; |
| 258 |
| 259 } // namespace buzz |
| 260 |
| 261 #endif // WEBRTC_LIBJINGLE_XMPP_XMPPENGINEIMPL_H_ |
OLD | NEW |