3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_XMPP_XMPPCLIENT_H_
29 #define TALK_XMPP_XMPPCLIENT_H_
32 #include "webrtc/libjingle/xmpp/asyncsocket.h"
33 #include "webrtc/libjingle/xmpp/xmppclientsettings.h"
34 #include "webrtc/libjingle/xmpp/xmppengine.h"
35 #include "webrtc/libjingle/xmpp/xmpptask.h"
36 #include "webrtc/base/basicdefs.h"
37 #include "webrtc/base/sigslot.h"
38 #include "webrtc/base/task.h"
43 class CaptchaChallenge;
45 // Just some non-colliding number. Could have picked "1".
46 #define XMPP_CLIENT_TASK_CODE 0x366c1e47
48 /////////////////////////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////////
54 // See Task first. XmppClient is a parent task for XmppTasks.
56 // XmppClient is a task which is designed to be the parent task for
57 // all tasks that depend on a single Xmpp connection. If you want to,
58 // for example, listen for subscription requests forever, then your
59 // listener should be a task that is a child of the XmppClient that owns
60 // the connection you are using. XmppClient has all the utility methods
61 // that basically drill through to XmppEngine.
63 // XmppClient is just a wrapper for XmppEngine, and if I were writing it
64 // all over again, I would make XmppClient == XmppEngine. Why?
65 // XmppEngine needs tasks too, for example it has an XmppLoginTask which
66 // should just be the same kind of Task instead of an XmppEngine specific
67 // thing. It would help do certain things like GAIA auth cleaner.
69 /////////////////////////////////////////////////////////////////////
71 class XmppClient : public XmppTaskParentInterface,
72 public XmppClientInterface,
73 public sigslot::has_slots<>
76 explicit XmppClient(rtc::TaskParent * parent);
77 virtual ~XmppClient();
79 XmppReturnStatus Connect(const XmppClientSettings & settings,
80 const std::string & lang,
82 PreXmppAuth * preauth);
84 virtual int ProcessStart();
85 virtual int ProcessResponse();
86 XmppReturnStatus Disconnect();
88 sigslot::signal1<XmppEngine::State> SignalStateChange;
89 XmppEngine::Error GetError(int *subcode);
91 // When there is a <stream:error> stanza, return the stanza
92 // so that they can be handled.
93 const XmlElement *GetStreamError();
95 // When there is an authentication error, we may have captcha info
96 // that the user can use to unlock their account
97 CaptchaChallenge GetCaptchaChallenge();
99 // When authentication is successful, this returns the service token
100 // (if we used GAIA authentication)
101 std::string GetAuthMechanism();
102 std::string GetAuthToken();
104 XmppReturnStatus SendRaw(const std::string & text);
106 XmppEngine* engine();
108 sigslot::signal2<const char *, int> SignalLogInput;
109 sigslot::signal2<const char *, int> SignalLogOutput;
111 // As XmppTaskParentIntreface
112 virtual XmppClientInterface* GetClient() { return this; }
114 // As XmppClientInterface
115 virtual XmppEngine::State GetState() const;
116 virtual const Jid& jid() const;
117 virtual std::string NextId();
118 virtual XmppReturnStatus SendStanza(const XmlElement *stanza);
119 virtual XmppReturnStatus SendStanzaError(const XmlElement * pelOriginal,
120 XmppStanzaError code,
121 const std::string & text);
122 virtual void AddXmppTask(XmppTask *, XmppEngine::HandlerLevel);
123 virtual void RemoveXmppTask(XmppTask *);
126 friend class XmppTask;
130 // Internal state management
132 STATE_PRE_XMPP_LOGIN = STATE_NEXT,
133 STATE_START_XMPP_LOGIN = STATE_NEXT + 1,
135 int Process(int state) {
137 case STATE_PRE_XMPP_LOGIN: return ProcessTokenLogin();
138 case STATE_START_XMPP_LOGIN: return ProcessStartXmppLogin();
139 default: return Task::Process(state);
143 std::string GetStateName(int state) const {
145 case STATE_PRE_XMPP_LOGIN: return "PRE_XMPP_LOGIN";
146 case STATE_START_XMPP_LOGIN: return "START_XMPP_LOGIN";
147 default: return Task::GetStateName(state);
151 int ProcessTokenLogin();
152 int ProcessStartXmppLogin();
156 friend class Private;
157 rtc::scoped_ptr<Private> d_;
159 bool delivering_signal_;
165 #endif // TALK_XMPP_XMPPCLIENT_H_