File: js_binding.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 3,803 bytes parent folder | download | duplicates (3)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2019 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_JS_INJECTION_RENDERER_JS_BINDING_H_
#define COMPONENTS_JS_INJECTION_RENDERER_JS_BINDING_H_

#include <algorithm>
#include <string>

#include "base/auto_reset.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/js_injection/common/interfaces.mojom.h"
#include "gin/arguments.h"
#include "gin/wrappable.h"
#include "mojo/public/cpp/bindings/associated_receiver.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "third_party/blink/public/common/messaging/string_message_codec.h"
#include "v8/include/v8.h"

namespace v8 {
template <typename T>
class Global;
class Function;
}  // namespace v8

namespace content {
class RenderFrame;
}  // namespace content

namespace js_injection {
class JsCommunication;

// A gin::Wrappable class used for providing JavaScript API. JsCommunication
// creates an instance of JsBinding for each unique name exposed to the page.
// JsBinding is owned by v8.
class JsBinding final : public gin::Wrappable<JsBinding>,
                        public mojom::BrowserToJsMessaging {
 public:
  static gin::WrapperInfo kWrapperInfo;

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

  static base::WeakPtr<JsBinding> Install(
      content::RenderFrame* render_frame,
      const std::u16string& js_object_name,
      base::WeakPtr<JsCommunication> js_communication,
      v8::Isolate* isolate,
      v8::Local<v8::Context> context);

  // mojom::BrowserToJsMessaging implementation.
  void OnPostMessage(blink::WebMessagePayload message) override;

  void ReleaseV8GlobalObjects();

  void Bind(
      mojo::PendingAssociatedReceiver<mojom::BrowserToJsMessaging> receiver);

 protected:
  ~JsBinding() override;

 private:
  explicit JsBinding(content::RenderFrame* render_frame,
                     const std::u16string& js_object_name,
                     base::WeakPtr<JsCommunication> js_java_configurator);

  // gin::Wrappable implementation
  gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
      v8::Isolate* isolate) override;

  auto find_listener(v8::Local<v8::Function> listener) {
    // Can't just use `find(listeners_, listener)` because `v8::Global<T>` and
    // `v8::Local<T>` do not have a common reference type and thus do not
    // satisfy `std::equality_comparable_with<>`. We could project using
    // `v8::Global<T>::Get()`, but that's less efficient.
    return std::ranges::find_if(listeners_,
                                [listener](const auto& global_listener) {
                                  return global_listener == listener;
                                });
  }

  // For jsObject.postMessage(message[, ports]) JavaScript API.
  void PostMessage(gin::Arguments* args);
  // For jsObject.addEventListener("message", listener) JavaScript API.
  void AddEventListener(gin::Arguments* args);
  // For jsObject.removeEventListener("message", listener) JavaScript API.
  void RemoveEventListener(gin::Arguments* args);
  // For get jsObject.onmessage.
  v8::Local<v8::Function> GetOnMessage(v8::Isolate* isolate);
  // For set jsObject.onmessage.
  void SetOnMessage(v8::Isolate* isolate, v8::Local<v8::Value> value);

  raw_ptr<content::RenderFrame> render_frame_;
  std::u16string js_object_name_;
  v8::Global<v8::Function> on_message_;
  std::vector<v8::Global<v8::Function>> listeners_;

  base::WeakPtr<JsCommunication> js_communication_;

  mojo::AssociatedReceiver<mojom::BrowserToJsMessaging> receiver_{this};

  base::WeakPtrFactory<JsBinding> weak_ptr_factory_{this};
};

}  // namespace js_injection

#endif  // COMPONENTS_JS_INJECTION_RENDERER_JS_BINDING_H_