delegating_signal_strategy.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2013 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_DELEGATING_SIGNAL_STRATEGY_H_
  5. #define REMOTING_SIGNALING_DELEGATING_SIGNAL_STRATEGY_H_
  6. #include "base/callback.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/observer_list.h"
  9. #include "remoting/signaling/signal_strategy.h"
  10. #include "remoting/signaling/signaling_address.h"
  11. namespace base {
  12. class SingleThreadTaskRunner;
  13. } // namespace base
  14. namespace remoting {
  15. // A signaling strategy class that delegates IQ sending and receiving.
  16. //
  17. // Notes on thread safety:
  18. // 1. This object can be created on any thread.
  19. // 2. |send_iq_callback| will always be called on the thread that it is created.
  20. // Note that |send_iq_callback| may be called after this object is destroyed.
  21. // 3. The caller should invoke all methods on the SignalStrategy interface on
  22. // the |client_task_runner|.
  23. // 4. All listeners will be called on |client_task_runner| as well.
  24. // 5. The destructor should always be called on the |client_task_runner|.
  25. // 6. As a result of (5), use MakeIncomingMessageCallback() to obtain a callback
  26. // when passing incoming signaling messages from the delegate. The callback
  27. // can then be invoked at any thread.
  28. class DelegatingSignalStrategy : public SignalStrategy {
  29. public:
  30. typedef base::RepeatingCallback<void(const std::string&)> IqCallback;
  31. DelegatingSignalStrategy(
  32. const SignalingAddress& local_address,
  33. scoped_refptr<base::SingleThreadTaskRunner> client_task_runner,
  34. const IqCallback& send_iq_callback);
  35. DelegatingSignalStrategy(const DelegatingSignalStrategy&) = delete;
  36. DelegatingSignalStrategy& operator=(const DelegatingSignalStrategy&) = delete;
  37. ~DelegatingSignalStrategy() override;
  38. IqCallback GetIncomingMessageCallback();
  39. // SignalStrategy interface.
  40. void Connect() override;
  41. void Disconnect() override;
  42. State GetState() const override;
  43. Error GetError() const override;
  44. const SignalingAddress& GetLocalAddress() const override;
  45. void AddListener(Listener* listener) override;
  46. void RemoveListener(Listener* listener) override;
  47. bool SendStanza(std::unique_ptr<jingle_xmpp::XmlElement> stanza) override;
  48. bool SendMessage(const SignalingAddress& destination_address,
  49. const ftl::ChromotingMessage& message) override;
  50. std::string GetNextId() override;
  51. private:
  52. static void OnIncomingMessageFromDelegate(
  53. base::WeakPtr<DelegatingSignalStrategy> weak_ptr,
  54. scoped_refptr<base::SingleThreadTaskRunner> client_task_runner,
  55. const std::string& message);
  56. void OnIncomingMessage(const std::string& message);
  57. SignalingAddress local_address_;
  58. scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_;
  59. scoped_refptr<base::SingleThreadTaskRunner> client_task_runner_;
  60. IqCallback incoming_iq_callback_;
  61. IqCallback send_iq_callback_;
  62. base::ObserverList<Listener> listeners_;
  63. base::WeakPtrFactory<DelegatingSignalStrategy> weak_factory_{this};
  64. };
  65. } // namespace remoting
  66. #endif // REMOTING_SIGNALING_DELEGATING_SIGNAL_STRATEGY_H_