scoped_api_binding.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_API_BINDINGS_SCOPED_API_BINDING_H_
  5. #define COMPONENTS_CAST_API_BINDINGS_SCOPED_API_BINDING_H_
  6. #include <memory>
  7. #include "base/check.h"
  8. #include "base/strings/string_piece.h"
  9. #include "components/cast/cast_component_export.h"
  10. #include "components/cast/message_port/message_port.h"
  11. namespace cast_api_bindings {
  12. class Manager;
  13. // Manages the registration of bindings Javascript and establishment of
  14. // communication channels, as well as unregistration on object teardown, using
  15. // RAII semantics.
  16. class CAST_COMPONENT_EXPORT ScopedApiBinding final
  17. : public cast_api_bindings::MessagePort::Receiver {
  18. public:
  19. // Methods for handling message I/O with bindings scripts.
  20. class Delegate {
  21. public:
  22. virtual ~Delegate() = default;
  23. // Expresses the name for which MessagePort connection requests should be
  24. // routed to the Delegate implementation.
  25. virtual base::StringPiece GetPortName() const = 0;
  26. // Invoked when |message_port_| is connected. This allows the Delegate to do
  27. // work when the client first connects, e.g. sending it a message conveying
  28. // some initial state.
  29. virtual void OnConnected() {}
  30. // Invoked for messages received over |message_port_|.
  31. virtual bool OnMessage(base::StringPiece message) = 0;
  32. // Invoked when |message_port_| is disconnected.
  33. // Allows the delegate to cleanup if the client unexpectedly disconnects.
  34. virtual void OnDisconnected() {}
  35. };
  36. // |bindings_manager|: Specifies the Manager to which the binding will be
  37. // published.
  38. // |delegate|: If set, provides the necessary identifier and
  39. // method implementations for connecting script message I/O with the
  40. // bindings backend.
  41. // Must outlive |this|.
  42. // Can be nullptr if the bindings do not require communication.
  43. // |js_bindings_id|: Id used for management of the |js_bindings| script.
  44. // Must be unique.
  45. // |js_bindings|: script to inject.
  46. ScopedApiBinding(Manager* bindings_manager,
  47. Delegate* delegate,
  48. base::StringPiece js_bindings_id,
  49. base::StringPiece js_bindings);
  50. ~ScopedApiBinding() override;
  51. ScopedApiBinding(const ScopedApiBinding&) = delete;
  52. ScopedApiBinding& operator=(const ScopedApiBinding&) = delete;
  53. // Sends a |message| to |message_port_|.
  54. // Returns true if the message was sent.
  55. bool SendMessage(base::StringPiece message);
  56. private:
  57. // Called when a port is received from the page.
  58. void OnPortConnected(std::unique_ptr<cast_api_bindings::MessagePort> port);
  59. // cast_api_bindings::MessagePort::Receiver implementation:
  60. bool OnMessage(base::StringPiece message,
  61. std::vector<std::unique_ptr<cast_api_bindings::MessagePort>>
  62. ports) override;
  63. void OnPipeError() override;
  64. Manager* const bindings_manager_;
  65. Delegate* const delegate_;
  66. const std::string js_bindings_id_;
  67. // The MessagePort used to receive messages from the receiver JS.
  68. std::unique_ptr<cast_api_bindings::MessagePort> message_port_;
  69. };
  70. } // namespace cast_api_bindings
  71. #endif // COMPONENTS_CAST_API_BINDINGS_SCOPED_API_BINDING_H_