blob: 2c3c451c13b367422901895830d4a6a665cb8a79 [file] [log] [blame]
Kevin Marshall82fe04c02020-07-30 23:38:061// 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 Marshall82fe04c02020-07-30 23:38:0610#include "base/check.h"
Kevin Marshall82fe04c02020-07-30 23:38:0611#include "base/strings/string_piece.h"
12#include "components/cast/cast_component_export.h"
Shawn Quereshicf3da5f2020-10-06 19:35:1713#include "components/cast/message_port/message_port.h"
Anton Bikineev1156b5f2021-05-15 22:35:3614#include "third_party/abseil-cpp/absl/types/optional.h"
Kevin Marshall82fe04c02020-07-30 23:38:0615
16namespace cast_api_bindings {
17
18class 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.
23class CAST_COMPONENT_EXPORT ScopedApiBinding
Shawn Quereshicf3da5f2020-10-06 19:35:1724 : public cast_api_bindings::MessagePort::Receiver {
Kevin Marshall82fe04c02020-07-30 23:38:0625 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 Quereshicf3da5f2020-10-06 19:35:1773 void OnPortConnected(std::unique_ptr<cast_api_bindings::MessagePort> port);
Kevin Marshall82fe04c02020-07-30 23:38:0674
Shawn Quereshicf3da5f2020-10-06 19:35:1775 // 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 Marshall82fe04c02020-07-30 23:38:0679 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 Quereshicf3da5f2020-10-06 19:35:1786 std::unique_ptr<cast_api_bindings::MessagePort> message_port_;
Kevin Marshall82fe04c02020-07-30 23:38:0687};
88
89} // namespace cast_api_bindings
90
91#endif // COMPONENTS_CAST_API_BINDINGS_SCOPED_API_BINDING_H_