blob: 3827e20a384c2fd6a63f6c6a12cee8a6b0dcf005 [file] [log] [blame]
{% 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(
{%- if is_treat_non_object_as_null %}v8::Local<v8::Object>
{%- else %}v8::Local<v8::Function>{% endif %} 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;
{# Web IDL does not distinguish callback constructors from callback functions.
If the return type is 'any', then it\'s likely to be used as a callback
constructor. #}
{% if idl_type == 'any' %}
// Performs "construct".
// https://heycam.github.io/webidl/#construct-a-callback-function
v8::Maybe<{{return_cpp_type}}> Construct({{argument_declarations[1:] | join(', ')}}) WARN_UNUSED_RESULT;
{% endif %}
{# Type Function is often used as a sort of wild cards, and its return value is
often discarded. So, this provides some convenience. #}
{% if idl_type == 'void' or callback_function_name == 'Function' %}
// 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}}(
{%- if is_treat_non_object_as_null %}v8::Local<v8::Object>
{%- else %}v8::Local<v8::Function>{% endif %} 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' or callback_function_name == 'Function' %}
{{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 #}