signal_strategy.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2014 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. #ifndef REMOTING_SIGNALING_SIGNAL_STRATEGY_H_
  5. #define REMOTING_SIGNALING_SIGNAL_STRATEGY_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/observer_list_types.h"
  9. namespace jingle_xmpp {
  10. class XmlElement;
  11. } // namespace jingle_xmpp
  12. namespace remoting {
  13. namespace ftl {
  14. class ChromotingMessage;
  15. class Id;
  16. } // namespace ftl
  17. class SignalingAddress;
  18. class SignalStrategy {
  19. public:
  20. enum State {
  21. // Connection is being established.
  22. CONNECTING,
  23. // Signalling is connected.
  24. CONNECTED,
  25. // Connection is closed due to an error or because Disconnect()
  26. // was called.
  27. DISCONNECTED,
  28. };
  29. enum Error {
  30. OK,
  31. AUTHENTICATION_FAILED,
  32. NETWORK_ERROR,
  33. PROTOCOL_ERROR,
  34. };
  35. // Callback interface for signaling event. Event handlers are not
  36. // allowed to destroy SignalStrategy, but may add or remove other
  37. // listeners.
  38. class Listener : public base::CheckedObserver {
  39. public:
  40. ~Listener() override {}
  41. // Called after state of the connection has changed. If the state
  42. // is DISCONNECTED, then GetError() can be used to get the reason
  43. // for the disconnection.
  44. virtual void OnSignalStrategyStateChange(State state) = 0;
  45. // Must return true if the stanza was handled, false
  46. // otherwise. The signal strategy must not be deleted from a
  47. // handler of this message.
  48. virtual bool OnSignalStrategyIncomingStanza(
  49. const jingle_xmpp::XmlElement* stanza) = 0;
  50. // This method is similar to OnSignalStrategyIncomingStanza(). It will be
  51. // called by signal strategy that supports ChromotingMessage (i.e.
  52. // FtlSignalStrategy) before OnSignalStrategyIncomingStanza() is called.
  53. //
  54. // Must return true if the message was handled, false
  55. // otherwise. The signal strategy must not be deleted from a
  56. // handler of this message.
  57. //
  58. // TODO(yuweih): Remove OnSignalStrategyIncomingStanza() and make this
  59. // method pure virtual.
  60. virtual bool OnSignalStrategyIncomingMessage(
  61. const ftl::Id& sender_id,
  62. const std::string& sender_registration_id,
  63. const ftl::ChromotingMessage& message);
  64. };
  65. SignalStrategy() {}
  66. SignalStrategy(const SignalStrategy&) = delete;
  67. SignalStrategy& operator=(const SignalStrategy&) = delete;
  68. virtual ~SignalStrategy() {}
  69. // Starts connection attempt. If connection is currently active
  70. // disconnects it and opens a new connection (implicit disconnect
  71. // triggers CLOSED notification). Connection is finished
  72. // asynchronously.
  73. virtual void Connect() = 0;
  74. // Disconnects current connection if connected. Triggers CLOSED
  75. // notification.
  76. virtual void Disconnect() = 0;
  77. // Returns current state.
  78. virtual State GetState() const = 0;
  79. // Returns the last error. Set when state changes to DISCONNECT.
  80. virtual Error GetError() const = 0;
  81. // Local address. An empty value is returned when not connected.
  82. virtual const SignalingAddress& GetLocalAddress() const = 0;
  83. // Add a |listener| that can listen to all incoming
  84. // messages. Doesn't take ownership of the |listener|. All listeners
  85. // must be removed before this object is destroyed.
  86. virtual void AddListener(Listener* listener) = 0;
  87. // Remove a |listener| previously added with AddListener().
  88. virtual void RemoveListener(Listener* listener) = 0;
  89. // Sends a raw XMPP stanza. Returns false if the stanza couldn't be sent.
  90. virtual bool SendStanza(std::unique_ptr<jingle_xmpp::XmlElement> stanza) = 0;
  91. // Sends a ChromotingMessage. Returns false if the message couldn't be sent.
  92. virtual bool SendMessage(const SignalingAddress& destination_address,
  93. const ftl::ChromotingMessage& message) = 0;
  94. // Returns new ID that should be used for the next outgoing IQ
  95. // request.
  96. virtual std::string GetNextId() = 0;
  97. // Returns true if the signal strategy gets into an error state when it tries
  98. // to sign in. You can get back the actual error by calling GetError().
  99. // The default implementation always returns false.
  100. virtual bool IsSignInError() const;
  101. };
  102. } // namespace remoting
  103. #endif // REMOTING_SIGNALING_SIGNAL_STRATEGY_H_