| {% filter format_blink_cpp_source_code %} |
| |
| {% include 'copyright_block.txt' %} |
| #ifndef {{header_guard}} |
| #define {{header_guard}} |
| |
| {% 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 MakeGarbageCollected<{{cpp_class}}>(callback_function); |
| } |
| |
| 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) {} |
| ~{{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 %} |
| |
| {% if callback_function_name == 'EventHandlerNonNull' %} |
| // Returns true if the callback is runnable, otherwise returns false and |
| // throws an exception. 'beforeunload' event need to have priority over pause |
| // of execution contexts. |
| enum class IgnorePause { kDontIgnore, kIgnore }; |
| bool IsRunnableOrThrowException(IgnorePause); |
| |
| // Performs "invoke" without checking the runnability check, which must be |
| // done prior to this call by |IsRunnableOrThrowException|. |
| // https://heycam.github.io/webidl/#es-invoking-callback-functions |
| // This function may throw unlike InvokeAndReportException. |
| v8::Maybe<{{return_cpp_type}}> InvokeWithoutRunnabilityCheck({{argument_declarations | join(', ')}}) WARN_UNUSED_RESULT; |
| {% endif %} |
| }; |
| |
| template <> |
| class V8PersistentCallbackFunction<{{cpp_class}}> final : public V8PersistentCallbackFunctionBase { |
| using V8CallbackFunction = {{cpp_class}}; |
| |
| public: |
| explicit V8PersistentCallbackFunction(V8CallbackFunction* callback_function) |
| : V8PersistentCallbackFunctionBase(callback_function) {} |
| ~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: |
| 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 // {{header_guard}} |
| |
| {% endfilter %}{# format_blink_cpp_source_code #} |