message_port.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2020 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_CAST_MESSAGE_PORT_MESSAGE_PORT_H_
  5. #define COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/strings/string_piece.h"
  9. #include "components/cast/cast_component_export.h"
  10. namespace cast_api_bindings {
  11. // HTML5 MessagePort abstraction; allows usage of the platform MessagePort type
  12. // without exposing details of the message format, paired port creation, or
  13. // transfer of ports.
  14. class CAST_COMPONENT_EXPORT MessagePort {
  15. public:
  16. // Implemented by receivers of messages from the MessagePort class.
  17. class Receiver {
  18. public:
  19. virtual ~Receiver();
  20. // Receives a |message| and ownership of |ports|.
  21. virtual bool OnMessage(base::StringPiece message,
  22. std::vector<std::unique_ptr<MessagePort>> ports) = 0;
  23. // Receives an error.
  24. virtual void OnPipeError() = 0;
  25. };
  26. virtual ~MessagePort();
  27. // Sends a |message| from the port.
  28. virtual bool PostMessage(base::StringPiece message) = 0;
  29. // Sends a |message| from the port along with transferable |ports|.
  30. virtual bool PostMessageWithTransferables(
  31. base::StringPiece message,
  32. std::vector<std::unique_ptr<MessagePort>> ports) = 0;
  33. // Sets the |receiver| for messages arriving to this port. May only be set
  34. // once.
  35. virtual void SetReceiver(
  36. cast_api_bindings::MessagePort::Receiver* receiver) = 0;
  37. // Closes the underlying port.
  38. virtual void Close() = 0;
  39. // Whether a message can be posted; may be used to check the state of the port
  40. // without posting a message.
  41. virtual bool CanPostMessage() const = 0;
  42. };
  43. } // namespace cast_api_bindings
  44. #endif // COMPONENTS_CAST_MESSAGE_PORT_MESSAGE_PORT_H_