summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/templates/callback_function.h.tmpl
blob: 44b5414376d1353ec653c7380bf7d5578c68689f (plain)
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
{% filter format_blink_cpp_source_code %}

{% include 'copyright_block.txt' %}

#ifndef {{cpp_class}}_h
#define {{cpp_class}}_h

{% for filename in header_includes %}
#include "{{filename}}"
{% endfor %}

namespace blink {

{% for forward_declaration in forward_declarations %}
class {{forward_declaration}};
{% endfor %}

class {{exported}}{{cpp_class}} final : public CallbackFunctionBase {
 public:
  static {{cpp_class}}* Create(v8::Local<v8::Function> callback_function) {
    return new {{cpp_class}}(callback_function);
  }

  ~{{cpp_class}}() override = default;

  // NameClient overrides:
  const char* NameInHeapSnapshot() const override;

  // Performs "invoke".
  // https://heycam.github.io/webidl/#es-invoking-callback-functions
  v8::Maybe<{{return_cpp_type}}> Invoke({{argument_declarations | join(', ')}}) WARN_UNUSED_RESULT;

{% if idl_type == 'void' %}
  // Performs "invoke", and then reports an exception, if any, to the global
  // error handler such as DevTools' console.
  void InvokeAndReportException({{argument_declarations | join(', ')}});
{% endif %}

 private:
  explicit {{cpp_class}}(v8::Local<v8::Function> callback_function)
      : CallbackFunctionBase(callback_function) {}
};

template <>
class V8PersistentCallbackFunction<{{cpp_class}}> final : public V8PersistentCallbackFunctionBase {
  using V8CallbackFunction = {{cpp_class}};

 public:
  ~V8PersistentCallbackFunction() override = default;

  // Returns a wrapper-tracing version of this callback function.
  V8CallbackFunction* ToNonV8Persistent() { return Proxy(); }

  v8::Maybe<{{return_cpp_type}}> Invoke({{argument_declarations | join(', ')}}) WARN_UNUSED_RESULT;
{% if idl_type == 'void' %}
  {{exported}}void InvokeAndReportException({{argument_declarations | join(', ')}});
{% endif %}

 private:
  explicit V8PersistentCallbackFunction(V8CallbackFunction* callback_function)
      : V8PersistentCallbackFunctionBase(callback_function) {}

  V8CallbackFunction* Proxy() {
    return As<V8CallbackFunction>();
  }

  template <typename V8CallbackFunction>
  friend V8PersistentCallbackFunction<V8CallbackFunction>*
  ToV8PersistentCallbackFunction(V8CallbackFunction*);
};

// {{cpp_class}} is designed to be used with wrapper-tracing.
// As blink::Persistent does not perform wrapper-tracing, use of
// |WrapPersistent| for callback functions is likely (if not always) misuse.
// Thus, this code prohibits such a use case. The call sites should explicitly
// use WrapPersistent(V8PersistentCallbackFunction<T>*).
Persistent<{{cpp_class}}> WrapPersistent({{cpp_class}}*) = delete;

}  // namespace blink

#endif  // {{cpp_class}}_h

{% endfilter %}{# format_blink_cpp_source_code #}