messaging_client.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 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_MESSAGING_CLIENT_H_
  5. #define REMOTING_SIGNALING_MESSAGING_CLIENT_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/callback_list.h"
  9. #include "remoting/proto/ftl/v1/chromoting_message.pb.h"
  10. #include "remoting/proto/ftl/v1/ftl_messages.pb.h"
  11. namespace remoting {
  12. class ProtobufHttpStatus;
  13. // An interface to send messages and receive messages from FTL messaging
  14. // service.
  15. class MessagingClient {
  16. public:
  17. using MessageCallback =
  18. base::RepeatingCallback<void(const ftl::Id& sender_id,
  19. const std::string& sender_registration_id,
  20. const ftl::ChromotingMessage& message)>;
  21. using MessageCallbackList = base::RepeatingCallbackList<
  22. void(const ftl::Id&, const std::string&, const ftl::ChromotingMessage&)>;
  23. using DoneCallback =
  24. base::OnceCallback<void(const ProtobufHttpStatus& status)>;
  25. virtual ~MessagingClient() = default;
  26. // Registers a callback which is run for each new message received. Simply
  27. // delete the returned subscription object to unregister. The subscription
  28. // object must be deleted before |this| is deleted.
  29. virtual base::CallbackListSubscription RegisterMessageCallback(
  30. const MessageCallback& callback) = 0;
  31. virtual void SendMessage(const std::string& destination,
  32. const std::string& destination_registration_id,
  33. const ftl::ChromotingMessage& message,
  34. DoneCallback on_done) = 0;
  35. // Opens a stream to continuously receive new messages from the server and
  36. // calls the registered MessageCallback once a new message is received.
  37. // |on_ready| is called once the stream is successfully started.
  38. // |on_closed| is called if the stream fails to start, in which case
  39. // |on_ready| will not be called, or when the stream is closed or dropped,
  40. // in which case it is called after |on_ready| is called.
  41. virtual void StartReceivingMessages(base::OnceClosure on_ready,
  42. DoneCallback on_closed) = 0;
  43. // Stops the stream for continuously receiving new messages. Note that
  44. // |on_closed| callback will be silently dropped.
  45. virtual void StopReceivingMessages() = 0;
  46. // Returns true if the streaming channel is open.
  47. virtual bool IsReceivingMessages() const = 0;
  48. protected:
  49. MessagingClient() = default;
  50. };
  51. } // namespace remoting
  52. #endif // REMOTING_SIGNALING_MESSAGING_CLIENT_H_