xmppclient.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef THIRD_PARTY_LIBJINGLE_XMPP_XMPP_XMPPCLIENT_H_
  11. #define THIRD_PARTY_LIBJINGLE_XMPP_XMPP_XMPPCLIENT_H_
  12. #include <memory>
  13. #include <string>
  14. #include "third_party/libjingle_xmpp/task_runner/task.h"
  15. #include "third_party/libjingle_xmpp/xmpp/asyncsocket.h"
  16. #include "third_party/libjingle_xmpp/xmpp/xmppclientsettings.h"
  17. #include "third_party/libjingle_xmpp/xmpp/xmppengine.h"
  18. #include "third_party/libjingle_xmpp/xmpp/xmpptask.h"
  19. #include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h"
  20. namespace jingle_xmpp {
  21. class PreXmppAuth;
  22. class CaptchaChallenge;
  23. // Just some non-colliding number. Could have picked "1".
  24. #define XMPP_CLIENT_TASK_CODE 0x366c1e47
  25. /////////////////////////////////////////////////////////////////////
  26. //
  27. // XMPPCLIENT
  28. //
  29. /////////////////////////////////////////////////////////////////////
  30. //
  31. // See Task first. XmppClient is a parent task for XmppTasks.
  32. //
  33. // XmppClient is a task which is designed to be the parent task for
  34. // all tasks that depend on a single Xmpp connection. If you want to,
  35. // for example, listen for subscription requests forever, then your
  36. // listener should be a task that is a child of the XmppClient that owns
  37. // the connection you are using. XmppClient has all the utility methods
  38. // that basically drill through to XmppEngine.
  39. //
  40. // XmppClient is just a wrapper for XmppEngine, and if I were writing it
  41. // all over again, I would make XmppClient == XmppEngine. Why?
  42. // XmppEngine needs tasks too, for example it has an XmppLoginTask which
  43. // should just be the same kind of Task instead of an XmppEngine specific
  44. // thing. It would help do certain things like GAIA auth cleaner.
  45. //
  46. /////////////////////////////////////////////////////////////////////
  47. class XmppClient : public XmppTaskParentInterface,
  48. public XmppClientInterface,
  49. public sigslot::has_slots<>
  50. {
  51. public:
  52. explicit XmppClient(jingle_xmpp::TaskParent * parent);
  53. virtual ~XmppClient();
  54. XmppReturnStatus Connect(const XmppClientSettings & settings,
  55. const std::string & lang,
  56. AsyncSocket * socket,
  57. PreXmppAuth * preauth);
  58. virtual int ProcessStart();
  59. virtual int ProcessResponse();
  60. XmppReturnStatus Disconnect();
  61. sigslot::signal1<XmppEngine::State> SignalStateChange;
  62. XmppEngine::Error GetError(int *subcode);
  63. // When there is a <stream:error> stanza, return the stanza
  64. // so that they can be handled.
  65. const XmlElement *GetStreamError();
  66. // When there is an authentication error, we may have captcha info
  67. // that the user can use to unlock their account
  68. CaptchaChallenge GetCaptchaChallenge();
  69. // When authentication is successful, this returns the service token
  70. // (if we used GAIA authentication)
  71. std::string GetAuthMechanism();
  72. std::string GetAuthToken();
  73. XmppReturnStatus SendRaw(const std::string & text);
  74. XmppEngine* engine();
  75. sigslot::signal2<const char *, int> SignalLogInput;
  76. sigslot::signal2<const char *, int> SignalLogOutput;
  77. // As XmppTaskParentIntreface
  78. virtual XmppClientInterface* GetClient() { return this; }
  79. // As XmppClientInterface
  80. virtual XmppEngine::State GetState() const;
  81. virtual const Jid& jid() const;
  82. virtual std::string NextId();
  83. virtual XmppReturnStatus SendStanza(const XmlElement *stanza);
  84. virtual XmppReturnStatus SendStanzaError(const XmlElement * pelOriginal,
  85. XmppStanzaError code,
  86. const std::string & text);
  87. virtual void AddXmppTask(XmppTask *, XmppEngine::HandlerLevel);
  88. virtual void RemoveXmppTask(XmppTask *);
  89. private:
  90. friend class XmppTask;
  91. void OnAuthDone();
  92. // Internal state management
  93. enum {
  94. STATE_PRE_XMPP_LOGIN = STATE_NEXT,
  95. STATE_START_XMPP_LOGIN = STATE_NEXT + 1,
  96. };
  97. int Process(int state) {
  98. switch (state) {
  99. case STATE_PRE_XMPP_LOGIN: return ProcessTokenLogin();
  100. case STATE_START_XMPP_LOGIN: return ProcessStartXmppLogin();
  101. default: return Task::Process(state);
  102. }
  103. }
  104. std::string GetStateName(int state) const {
  105. switch (state) {
  106. case STATE_PRE_XMPP_LOGIN: return "PRE_XMPP_LOGIN";
  107. case STATE_START_XMPP_LOGIN: return "START_XMPP_LOGIN";
  108. default: return Task::GetStateName(state);
  109. }
  110. }
  111. int ProcessTokenLogin();
  112. int ProcessStartXmppLogin();
  113. void EnsureClosed();
  114. class Private;
  115. friend class Private;
  116. std::unique_ptr<Private> d_;
  117. bool delivering_signal_;
  118. bool valid_;
  119. };
  120. }
  121. #endif // THIRD_PARTY_LIBJINGLE_XMPP_XMPP_XMPPCLIENT_H_