ftl_messaging_client.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_FTL_MESSAGING_CLIENT_H_
  5. #define REMOTING_SIGNALING_FTL_MESSAGING_CLIENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "remoting/signaling/message_tracker.h"
  11. #include "remoting/signaling/messaging_client.h"
  12. namespace google {
  13. namespace protobuf {
  14. class MessageLite;
  15. } // namespace protobuf
  16. } // namespace google
  17. namespace net {
  18. struct NetworkTrafficAnnotationTag;
  19. } // namespace net
  20. namespace network {
  21. class SharedURLLoaderFactory;
  22. } // namespace network
  23. namespace remoting {
  24. class ProtobufHttpClient;
  25. class MessageReceptionChannel;
  26. class OAuthTokenGetter;
  27. class RegistrationManager;
  28. class ScopedProtobufHttpRequest;
  29. class SignalingTracker;
  30. // A class for sending and receiving messages via the FTL API.
  31. class FtlMessagingClient final : public MessagingClient {
  32. public:
  33. // |signaling_tracker| is nullable.
  34. // Raw pointers must outlive |this|.
  35. FtlMessagingClient(
  36. OAuthTokenGetter* token_getter,
  37. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  38. RegistrationManager* registration_manager,
  39. SignalingTracker* signaling_tracker = nullptr);
  40. FtlMessagingClient(const FtlMessagingClient&) = delete;
  41. FtlMessagingClient& operator=(const FtlMessagingClient&) = delete;
  42. ~FtlMessagingClient() override;
  43. // MessagingClient implementations.
  44. base::CallbackListSubscription RegisterMessageCallback(
  45. const MessageCallback& callback) override;
  46. void SendMessage(const std::string& destination,
  47. const std::string& destination_registration_id,
  48. const ftl::ChromotingMessage& message,
  49. DoneCallback on_done) override;
  50. void StartReceivingMessages(base::OnceClosure on_ready,
  51. DoneCallback on_closed) override;
  52. void StopReceivingMessages() override;
  53. bool IsReceivingMessages() const override;
  54. private:
  55. friend class FtlMessagingClientTest;
  56. FtlMessagingClient(std::unique_ptr<ProtobufHttpClient> client,
  57. RegistrationManager* registration_manager,
  58. std::unique_ptr<MessageReceptionChannel> channel);
  59. template <typename CallbackFunctor>
  60. void ExecuteRequest(const net::NetworkTrafficAnnotationTag& tag,
  61. const std::string& path,
  62. std::unique_ptr<google::protobuf::MessageLite> request,
  63. CallbackFunctor callback_functor,
  64. DoneCallback on_done);
  65. void OnSendMessageResponse(DoneCallback on_done,
  66. const ProtobufHttpStatus& status,
  67. std::unique_ptr<ftl::InboxSendResponse> response);
  68. void BatchAckMessages(const ftl::BatchAckMessagesRequest& request,
  69. DoneCallback on_done);
  70. void OnBatchAckMessagesResponse(
  71. DoneCallback on_done,
  72. const ProtobufHttpStatus& status,
  73. std::unique_ptr<ftl::BatchAckMessagesResponse> response);
  74. std::unique_ptr<ScopedProtobufHttpRequest> OpenReceiveMessagesStream(
  75. base::OnceClosure on_channel_ready,
  76. const base::RepeatingCallback<
  77. void(std::unique_ptr<ftl::ReceiveMessagesResponse>)>& on_incoming_msg,
  78. base::OnceCallback<void(const ProtobufHttpStatus&)> on_channel_closed);
  79. void RunMessageCallbacks(const ftl::InboxMessage& message);
  80. void OnMessageReceived(const ftl::InboxMessage& message);
  81. std::unique_ptr<ProtobufHttpClient> client_;
  82. raw_ptr<RegistrationManager> registration_manager_;
  83. std::unique_ptr<MessageReceptionChannel> reception_channel_;
  84. MessageCallbackList callback_list_;
  85. MessageTracker message_tracker_;
  86. };
  87. } // namespace remoting
  88. #endif // REMOTING_SIGNALING_FTL_MESSAGING_CLIENT_H_