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_XMPPENGINE_H_ |
| 6 #define WEBRTC_LIBJINGLE_XMPP_XMPPENGINE_H_ |
| 7 |
| 8 // also part of the API |
| 9 #include "third_party/libjingle_xmpp/xmllite/qname.h" |
| 10 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
| 11 #include "third_party/libjingle_xmpp/xmpp/jid.h" |
| 12 |
| 13 |
| 14 namespace buzz { |
| 15 |
| 16 class XmppEngine; |
| 17 class SaslHandler; |
| 18 typedef void * XmppIqCookie; |
| 19 |
| 20 //! XMPP stanza error codes. |
| 21 //! Used in XmppEngine.SendStanzaError(). |
| 22 enum XmppStanzaError { |
| 23 XSE_BAD_REQUEST, |
| 24 XSE_CONFLICT, |
| 25 XSE_FEATURE_NOT_IMPLEMENTED, |
| 26 XSE_FORBIDDEN, |
| 27 XSE_GONE, |
| 28 XSE_INTERNAL_SERVER_ERROR, |
| 29 XSE_ITEM_NOT_FOUND, |
| 30 XSE_JID_MALFORMED, |
| 31 XSE_NOT_ACCEPTABLE, |
| 32 XSE_NOT_ALLOWED, |
| 33 XSE_PAYMENT_REQUIRED, |
| 34 XSE_RECIPIENT_UNAVAILABLE, |
| 35 XSE_REDIRECT, |
| 36 XSE_REGISTRATION_REQUIRED, |
| 37 XSE_SERVER_NOT_FOUND, |
| 38 XSE_SERVER_TIMEOUT, |
| 39 XSE_RESOURCE_CONSTRAINT, |
| 40 XSE_SERVICE_UNAVAILABLE, |
| 41 XSE_SUBSCRIPTION_REQUIRED, |
| 42 XSE_UNDEFINED_CONDITION, |
| 43 XSE_UNEXPECTED_REQUEST, |
| 44 }; |
| 45 |
| 46 // XmppReturnStatus |
| 47 // This is used by API functions to synchronously return status. |
| 48 enum XmppReturnStatus { |
| 49 XMPP_RETURN_OK, |
| 50 XMPP_RETURN_BADARGUMENT, |
| 51 XMPP_RETURN_BADSTATE, |
| 52 XMPP_RETURN_PENDING, |
| 53 XMPP_RETURN_UNEXPECTED, |
| 54 XMPP_RETURN_NOTYETIMPLEMENTED, |
| 55 }; |
| 56 |
| 57 // TlsOptions |
| 58 // This is used by API to identify TLS setting. |
| 59 enum TlsOptions { |
| 60 TLS_DISABLED, |
| 61 TLS_ENABLED, |
| 62 TLS_REQUIRED |
| 63 }; |
| 64 |
| 65 //! Callback for socket output for an XmppEngine connection. |
| 66 //! Register via XmppEngine.SetOutputHandler. An XmppEngine |
| 67 //! can call back to this handler while it is processing |
| 68 //! Connect, SendStanza, SendIq, Disconnect, or HandleInput. |
| 69 class XmppOutputHandler { |
| 70 public: |
| 71 virtual ~XmppOutputHandler() {} |
| 72 |
| 73 //! Deliver the specified bytes to the XMPP socket. |
| 74 virtual void WriteOutput(const char * bytes, size_t len) = 0; |
| 75 |
| 76 //! Initiate TLS encryption on the socket. |
| 77 //! The implementation must verify that the SSL |
| 78 //! certificate matches the given domainname. |
| 79 virtual void StartTls(const std::string & domainname) = 0; |
| 80 |
| 81 //! Called when engine wants the connecton closed. |
| 82 virtual void CloseConnection() = 0; |
| 83 }; |
| 84 |
| 85 //! Callback to deliver engine state change notifications |
| 86 //! to the object managing the engine. |
| 87 class XmppSessionHandler { |
| 88 public: |
| 89 virtual ~XmppSessionHandler() {} |
| 90 //! Called when engine changes state. Argument is new state. |
| 91 virtual void OnStateChange(int state) = 0; |
| 92 }; |
| 93 |
| 94 //! Callback to deliver stanzas to an Xmpp application module. |
| 95 //! Register via XmppEngine.SetDefaultSessionHandler or via |
| 96 //! XmppEngine.AddSessionHAndler. |
| 97 class XmppStanzaHandler { |
| 98 public: |
| 99 virtual ~XmppStanzaHandler() {} |
| 100 //! Process the given stanza. |
| 101 //! The handler must return true if it has handled the stanza. |
| 102 //! A false return value causes the stanza to be passed on to |
| 103 //! the next registered handler. |
| 104 virtual bool HandleStanza(const XmlElement * stanza) = 0; |
| 105 }; |
| 106 |
| 107 //! Callback to deliver iq responses (results and errors). |
| 108 //! Register while sending an iq via XmppEngine.SendIq. |
| 109 //! Iq responses are routed to matching XmppIqHandlers in preference |
| 110 //! to sending to any registered SessionHandlers. |
| 111 class XmppIqHandler { |
| 112 public: |
| 113 virtual ~XmppIqHandler() {} |
| 114 //! Called to handle the iq response. |
| 115 //! The response may be either a result or an error, and will have |
| 116 //! an 'id' that matches the request and a 'from' that matches the |
| 117 //! 'to' of the request. Called no more than once; once this is |
| 118 //! called, the handler is automatically unregistered. |
| 119 virtual void IqResponse(XmppIqCookie cookie, const XmlElement * pelStanza) = 0
; |
| 120 }; |
| 121 |
| 122 //! The XMPP connection engine. |
| 123 //! This engine implements the client side of the 'core' XMPP protocol. |
| 124 //! To use it, register an XmppOutputHandler to handle socket output |
| 125 //! and pass socket input to HandleInput. Then application code can |
| 126 //! set up the connection with a user, password, and other settings, |
| 127 //! and then call Connect() to initiate the connection. |
| 128 //! An application can listen for events and receive stanzas by |
| 129 //! registering an XmppStanzaHandler via AddStanzaHandler(). |
| 130 class XmppEngine { |
| 131 public: |
| 132 static XmppEngine * Create(); |
| 133 virtual ~XmppEngine() {} |
| 134 |
| 135 //! Error codes. See GetError(). |
| 136 enum Error { |
| 137 ERROR_NONE = 0, //!< No error |
| 138 ERROR_XML, //!< Malformed XML or encoding error |
| 139 ERROR_STREAM, //!< XMPP stream error - see GetStreamError() |
| 140 ERROR_VERSION, //!< XMPP version error |
| 141 ERROR_UNAUTHORIZED, //!< User is not authorized (rejected credentials) |
| 142 ERROR_TLS, //!< TLS could not be negotiated |
| 143 ERROR_AUTH, //!< Authentication could not be negotiated |
| 144 ERROR_BIND, //!< Resource or session binding could not be negoti
ated |
| 145 ERROR_CONNECTION_CLOSED,//!< Connection closed by output handler. |
| 146 ERROR_DOCUMENT_CLOSED, //!< Closed by </stream:stream> |
| 147 ERROR_SOCKET, //!< Socket error |
| 148 ERROR_NETWORK_TIMEOUT, //!< Some sort of timeout (eg., we never got the ros
ter) |
| 149 ERROR_MISSING_USERNAME //!< User has a Google Account but no nickname |
| 150 }; |
| 151 |
| 152 //! States. See GetState(). |
| 153 enum State { |
| 154 STATE_NONE = 0, //!< Nonexistent state |
| 155 STATE_START, //!< Initial state. |
| 156 STATE_OPENING, //!< Exchanging stream headers, authenticating and so
on. |
| 157 STATE_OPEN, //!< Authenticated and bound. |
| 158 STATE_CLOSED, //!< Session closed, possibly due to error. |
| 159 }; |
| 160 |
| 161 // SOCKET INPUT AND OUTPUT ------------------------------------------------ |
| 162 |
| 163 //! Registers the handler for socket output |
| 164 virtual XmppReturnStatus SetOutputHandler(XmppOutputHandler *pxoh) = 0; |
| 165 |
| 166 //! Provides socket input to the engine |
| 167 virtual XmppReturnStatus HandleInput(const char * bytes, size_t len) = 0; |
| 168 |
| 169 //! Advises the engine that the socket has closed |
| 170 virtual XmppReturnStatus ConnectionClosed(int subcode) = 0; |
| 171 |
| 172 // SESSION SETUP --------------------------------------------------------- |
| 173 |
| 174 //! Indicates the (bare) JID for the user to use. |
| 175 virtual XmppReturnStatus SetUser(const Jid & jid)= 0; |
| 176 |
| 177 //! Get the login (bare) JID. |
| 178 virtual const Jid & GetUser() = 0; |
| 179 |
| 180 //! Provides different methods for credentials for login. |
| 181 //! Takes ownership of this object; deletes when login is done |
| 182 virtual XmppReturnStatus SetSaslHandler(SaslHandler * h) = 0; |
| 183 |
| 184 //! Sets whether TLS will be used within the connection (default true). |
| 185 virtual XmppReturnStatus SetTls(TlsOptions useTls) = 0; |
| 186 |
| 187 //! Sets an alternate domain from which we allows TLS certificates. |
| 188 //! This is for use in the case where a we want to allow a proxy to |
| 189 //! serve up its own certificate rather than one owned by the underlying |
| 190 //! domain. |
| 191 virtual XmppReturnStatus SetTlsServer(const std::string & proxy_hostname, |
| 192 const std::string & proxy_domain) = 0; |
| 193 |
| 194 //! Gets whether TLS will be used within the connection. |
| 195 virtual TlsOptions GetTls() = 0; |
| 196 |
| 197 //! Sets the request resource name, if any (optional). |
| 198 //! Note that the resource name may be overridden by the server; after |
| 199 //! binding, the actual resource name is available as part of FullJid(). |
| 200 virtual XmppReturnStatus SetRequestedResource(const std::string& resource) = 0
; |
| 201 |
| 202 //! Gets the request resource name. |
| 203 virtual const std::string & GetRequestedResource() = 0; |
| 204 |
| 205 //! Sets language |
| 206 virtual void SetLanguage(const std::string & lang) = 0; |
| 207 |
| 208 // SESSION MANAGEMENT --------------------------------------------------- |
| 209 |
| 210 //! Set callback for state changes. |
| 211 virtual XmppReturnStatus SetSessionHandler(XmppSessionHandler* handler) = 0; |
| 212 |
| 213 //! Initiates the XMPP connection. |
| 214 //! After supplying connection settings, call this once to initiate, |
| 215 //! (optionally) encrypt, authenticate, and bind the connection. |
| 216 virtual XmppReturnStatus Connect() = 0; |
| 217 |
| 218 //! The current engine state. |
| 219 virtual State GetState() = 0; |
| 220 |
| 221 //! Returns true if the connection is encrypted (under TLS) |
| 222 virtual bool IsEncrypted() = 0; |
| 223 |
| 224 //! The error code. |
| 225 //! Consult this after XmppOutputHandler.OnClose(). |
| 226 virtual Error GetError(int *subcode) = 0; |
| 227 |
| 228 //! The stream:error stanza, when the error is XmppEngine::ERROR_STREAM. |
| 229 //! Notice the stanza returned is owned by the XmppEngine and |
| 230 //! is deleted when the engine is destroyed. |
| 231 virtual const XmlElement * GetStreamError() = 0; |
| 232 |
| 233 //! Closes down the connection. |
| 234 //! Sends CloseConnection to output, and disconnects and registered |
| 235 //! session handlers. After Disconnect completes, it is guaranteed |
| 236 //! that no further callbacks will be made. |
| 237 virtual XmppReturnStatus Disconnect() = 0; |
| 238 |
| 239 // APPLICATION USE ------------------------------------------------------- |
| 240 |
| 241 enum HandlerLevel { |
| 242 HL_NONE = 0, |
| 243 HL_PEEK, //!< Sees messages before all other processing; cannot abort |
| 244 HL_SINGLE, //!< Watches for a single message, e.g., by id and sender |
| 245 HL_SENDER, //!< Watches for a type of message from a specific sender |
| 246 HL_TYPE, //!< Watches a type of message, e.g., all groupchat msgs |
| 247 HL_ALL, //!< Watches all messages - gets last shot |
| 248 HL_COUNT, //!< Count of handler levels |
| 249 }; |
| 250 |
| 251 //! Adds a listener for session events. |
| 252 //! Stanza delivery is chained to session handlers; the first to |
| 253 //! return 'true' is the last to get each stanza. |
| 254 virtual XmppReturnStatus AddStanzaHandler(XmppStanzaHandler* handler, HandlerL
evel level = HL_PEEK) = 0; |
| 255 |
| 256 //! Removes a listener for session events. |
| 257 virtual XmppReturnStatus RemoveStanzaHandler(XmppStanzaHandler* handler) = 0; |
| 258 |
| 259 //! Sends a stanza to the server. |
| 260 virtual XmppReturnStatus SendStanza(const XmlElement * pelStanza) = 0; |
| 261 |
| 262 //! Sends raw text to the server |
| 263 virtual XmppReturnStatus SendRaw(const std::string & text) = 0; |
| 264 |
| 265 //! Sends an iq to the server, and registers a callback for the result. |
| 266 //! Returns the cookie passed to the result handler. |
| 267 virtual XmppReturnStatus SendIq(const XmlElement* pelStanza, |
| 268 XmppIqHandler* iq_handler, |
| 269 XmppIqCookie* cookie) = 0; |
| 270 |
| 271 //! Unregisters an iq callback handler given its cookie. |
| 272 //! No callback will come to this handler after it's unregistered. |
| 273 virtual XmppReturnStatus RemoveIqHandler(XmppIqCookie cookie, |
| 274 XmppIqHandler** iq_handler) = 0; |
| 275 |
| 276 |
| 277 //! Forms and sends an error in response to the given stanza. |
| 278 //! Swaps to and from, sets type to "error", and adds error information |
| 279 //! based on the passed code. Text is optional and may be STR_EMPTY. |
| 280 virtual XmppReturnStatus SendStanzaError(const XmlElement * pelOriginal, |
| 281 XmppStanzaError code, |
| 282 const std::string & text) = 0; |
| 283 |
| 284 //! The fullly bound JID. |
| 285 //! This JID is only valid after binding has succeeded. If the value |
| 286 //! is JID_NULL, the binding has not succeeded. |
| 287 virtual const Jid & FullJid() = 0; |
| 288 |
| 289 //! The next unused iq id for this connection. |
| 290 //! Call this when building iq stanzas, to ensure that each iq |
| 291 //! gets its own unique id. |
| 292 virtual std::string NextId() = 0; |
| 293 |
| 294 }; |
| 295 |
| 296 } |
| 297 |
| 298 |
| 299 // Move these to a better location |
| 300 |
| 301 #define XMPP_FAILED(x) \ |
| 302 ( (x) == buzz::XMPP_RETURN_OK ? false : true) \ |
| 303 |
| 304 |
| 305 #define XMPP_SUCCEEDED(x) \ |
| 306 ( (x) == buzz::XMPP_RETURN_OK ? true : false) \ |
| 307 |
| 308 #define IFR(x) \ |
| 309 do { \ |
| 310 xmpp_status = (x); \ |
| 311 if (XMPP_FAILED(xmpp_status)) { \ |
| 312 return xmpp_status; \ |
| 313 } \ |
| 314 } while (false) \ |
| 315 |
| 316 |
| 317 #define IFC(x) \ |
| 318 do { \ |
| 319 xmpp_status = (x); \ |
| 320 if (XMPP_FAILED(xmpp_status)) { \ |
| 321 goto Cleanup; \ |
| 322 } \ |
| 323 } while (false) \ |
| 324 |
| 325 |
| 326 #endif // WEBRTC_LIBJINGLE_XMPP_XMPPENGINE_H_ |
OLD | NEW |