File: scoped_api_binding.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,341 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CAST_API_BINDINGS_SCOPED_API_BINDING_H_
#define COMPONENTS_CAST_API_BINDINGS_SCOPED_API_BINDING_H_

#include <memory>
#include <string_view>

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "components/cast/cast_component_export.h"
#include "components/cast/message_port/message_port.h"

namespace cast_api_bindings {

class Manager;

// Manages the registration of bindings Javascript and establishment of
// communication channels, as well as unregistration on object teardown, using
// RAII semantics.
class CAST_COMPONENT_EXPORT ScopedApiBinding final
    : public cast_api_bindings::MessagePort::Receiver {
 public:
  // Methods for handling message I/O with bindings scripts.
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Expresses the name for which MessagePort connection requests should be
    // routed to the Delegate implementation.
    virtual std::string_view GetPortName() const = 0;

    // Invoked when |message_port_| is connected. This allows the Delegate to do
    // work when the client first connects, e.g. sending it a message conveying
    // some initial state.
    virtual void OnConnected() {}

    // Invoked for messages received over |message_port_|.
    virtual bool OnMessage(std::string_view message) = 0;

    // Invoked when |message_port_| is disconnected.
    // Allows the delegate to cleanup if the client unexpectedly disconnects.
    virtual void OnDisconnected() {}
  };

  // |bindings_manager|: Specifies the Manager to which the binding will be
  //     published.
  // |delegate|: If set, provides the necessary identifier and
  //     method implementations for connecting script message I/O with the
  //     bindings backend.
  //     Must outlive |this|.
  //     Can be nullptr if the bindings do not require communication.
  // |js_bindings_id|: Id used for management of the |js_bindings| script.
  //     Must be unique.
  // |js_bindings|: script to inject.
  ScopedApiBinding(Manager* bindings_manager,
                   Delegate* delegate,
                   std::string_view js_bindings_id,
                   std::string_view js_bindings);
  ~ScopedApiBinding() override;

  ScopedApiBinding(const ScopedApiBinding&) = delete;
  ScopedApiBinding& operator=(const ScopedApiBinding&) = delete;

  // Sends a |message| to |message_port_|.
  // Returns true if the message was sent.
  bool SendMessage(std::string_view message);

 private:
  // Called when a port is received from the page.
  void OnPortConnected(std::unique_ptr<cast_api_bindings::MessagePort> port);

  // cast_api_bindings::MessagePort::Receiver implementation:
  bool OnMessage(std::string_view message,
                 std::vector<std::unique_ptr<cast_api_bindings::MessagePort>>
                     ports) override;
  void OnPipeError() override;

  const raw_ptr<Manager> bindings_manager_;
  const raw_ptr<Delegate> delegate_;
  const std::string js_bindings_id_;

  // The MessagePort used to receive messages from the receiver JS.
  std::unique_ptr<cast_api_bindings::MessagePort> message_port_;
};

}  // namespace cast_api_bindings

#endif  // COMPONENTS_CAST_API_BINDINGS_SCOPED_API_BINDING_H_