message_dispatcher.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 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 COMPONENTS_MIRRORING_SERVICE_MESSAGE_DISPATCHER_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_MESSAGE_DISPATCHER_H_
  6. #include "base/callback.h"
  7. #include "base/component_export.h"
  8. #include "base/containers/flat_map.h"
  9. #include "components/mirroring/mojom/cast_message_channel.mojom.h"
  10. #include "components/mirroring/service/receiver_response.h"
  11. #include "mojo/public/cpp/bindings/pending_receiver.h"
  12. #include "mojo/public/cpp/bindings/pending_remote.h"
  13. #include "mojo/public/cpp/bindings/receiver.h"
  14. #include "mojo/public/cpp/bindings/remote.h"
  15. namespace mirroring {
  16. // Dispatches inbound/outbound messages. The outbound messages are sent out
  17. // through |outbound_channel|, and the inbound messages are handled by this
  18. // class.
  19. class COMPONENT_EXPORT(MIRRORING_SERVICE) MessageDispatcher final
  20. : public mojom::CastMessageChannel {
  21. public:
  22. using ErrorCallback = base::RepeatingCallback<void(const std::string&)>;
  23. MessageDispatcher(
  24. mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel,
  25. mojo::PendingReceiver<mojom::CastMessageChannel> inbound_channel,
  26. ErrorCallback error_callback);
  27. MessageDispatcher(const MessageDispatcher&) = delete;
  28. MessageDispatcher& operator=(const MessageDispatcher&) = delete;
  29. ~MessageDispatcher() override;
  30. using ResponseCallback =
  31. base::RepeatingCallback<void(const ReceiverResponse& response)>;
  32. // Registers/Unregisters callback for a certain type of responses.
  33. void Subscribe(ResponseType type, ResponseCallback callback);
  34. void Unsubscribe(ResponseType type);
  35. using OnceResponseCallback =
  36. base::OnceCallback<void(const ReceiverResponse& response)>;
  37. // Sends the given message and subscribes to replies until an acceptable one
  38. // is received or a timeout elapses. Message of the given response type is
  39. // delivered to the supplied callback if the sequence number of the response
  40. // matches |sequence_number|. If the timeout period elapses, the callback will
  41. // be run once with an unknown type of |response|.
  42. // Note: Calling RequestReply() before a previous reply was made will cancel
  43. // the previous request and not run its response callback.
  44. void RequestReply(mojom::CastMessagePtr message,
  45. ResponseType response_type,
  46. int32_t sequence_number,
  47. const base::TimeDelta& timeout,
  48. OnceResponseCallback callback);
  49. // Get the sequence number for the next outbound message. Never returns 0.
  50. int32_t GetNextSeqNumber();
  51. // Requests to send outbound |message|.
  52. void SendOutboundMessage(mojom::CastMessagePtr message);
  53. private:
  54. class RequestHolder;
  55. // mojom::CastMessageChannel implementation. Handles inbound messages.
  56. void Send(mojom::CastMessagePtr message) override;
  57. // Takes care of sending outbound messages.
  58. const mojo::Remote<mojom::CastMessageChannel> outbound_channel_;
  59. const mojo::Receiver<mojom::CastMessageChannel> receiver_;
  60. const ErrorCallback error_callback_;
  61. int32_t last_sequence_number_;
  62. // Holds callbacks for different types of responses.
  63. base::flat_map<ResponseType, ResponseCallback> callback_map_;
  64. };
  65. } // namespace mirroring
  66. #endif // COMPONENTS_MIRRORING_SERVICE_MESSAGE_DISPATCHER_H_