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