2019-09-04 15:45:25 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc. All rights reserved.
|
2015-08-07 10:10:19 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_CALLBACK_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_CALLBACK_H_
|
2015-08-07 11:34:00 +00:00
|
|
|
|
2019-04-25 00:40:27 +00:00
|
|
|
#include <utility>
|
2015-08-07 11:15:36 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "base/bind.h"
|
2020-05-15 18:57:40 +00:00
|
|
|
#include "gin/dictionary.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/std_converter.h"
|
2019-09-04 15:45:25 +00:00
|
|
|
#include "shell/common/gin_helper/function_template.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_helper/locker.h"
|
2020-06-17 17:08:10 +00:00
|
|
|
#include "shell/common/gin_helper/microtasks_scope.h"
|
2021-06-01 01:42:38 +00:00
|
|
|
// Implements safe conversions between JS functions and base::RepeatingCallback.
|
2015-08-07 10:10:19 +00:00
|
|
|
|
2019-09-04 15:45:25 +00:00
|
|
|
namespace gin_helper {
|
2015-08-07 10:10:19 +00:00
|
|
|
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename T>
|
2015-12-03 03:24:33 +00:00
|
|
|
class RefCountedGlobal;
|
|
|
|
|
|
|
|
// Manages the V8 function with RAII.
|
2015-11-06 12:23:41 +00:00
|
|
|
class SafeV8Function {
|
|
|
|
public:
|
|
|
|
SafeV8Function(v8::Isolate* isolate, v8::Local<v8::Value> value);
|
|
|
|
SafeV8Function(const SafeV8Function& other);
|
2015-12-03 03:24:33 +00:00
|
|
|
~SafeV8Function();
|
2015-11-06 12:23:41 +00:00
|
|
|
|
2015-12-03 03:24:33 +00:00
|
|
|
bool IsAlive() const;
|
|
|
|
v8::Local<v8::Function> NewHandle(v8::Isolate* isolate) const;
|
2015-11-06 12:23:41 +00:00
|
|
|
|
|
|
|
private:
|
2015-12-03 03:24:33 +00:00
|
|
|
scoped_refptr<RefCountedGlobal<v8::Function>> v8_function_;
|
2015-11-06 12:23:41 +00:00
|
|
|
};
|
2015-08-07 10:10:19 +00:00
|
|
|
|
2015-10-28 09:36:01 +00:00
|
|
|
// Helper to invoke a V8 function with C++ parameters.
|
2015-08-07 11:15:36 +00:00
|
|
|
template <typename Sig>
|
|
|
|
struct V8FunctionInvoker {};
|
2015-08-07 10:10:19 +00:00
|
|
|
|
2015-08-07 11:15:36 +00:00
|
|
|
template <typename... ArgTypes>
|
|
|
|
struct V8FunctionInvoker<v8::Local<v8::Value>(ArgTypes...)> {
|
2015-11-06 12:23:41 +00:00
|
|
|
static v8::Local<v8::Value> Go(v8::Isolate* isolate,
|
|
|
|
const SafeV8Function& function,
|
2015-08-07 11:15:36 +00:00
|
|
|
ArgTypes... raw) {
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Locker locker(isolate);
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::EscapableHandleScope handle_scope(isolate);
|
2015-12-03 03:24:33 +00:00
|
|
|
if (!function.IsAlive())
|
2015-11-06 12:23:41 +00:00
|
|
|
return v8::Null(isolate);
|
2020-06-17 17:08:10 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope(isolate, true);
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> holder = function.NewHandle(isolate);
|
2022-02-10 02:58:52 +00:00
|
|
|
v8::Local<v8::Context> context = holder->GetCreationContextChecked();
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-25 00:40:27 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args{
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
|
2019-03-20 15:27:06 +00:00
|
|
|
v8::MaybeLocal<v8::Value> ret = holder->Call(
|
|
|
|
context, holder, args.size(), args.empty() ? nullptr : &args.front());
|
|
|
|
if (ret.IsEmpty())
|
|
|
|
return v8::Undefined(isolate);
|
|
|
|
else
|
|
|
|
return handle_scope.Escape(ret.ToLocalChecked());
|
2015-08-07 10:10:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-07 11:15:36 +00:00
|
|
|
template <typename... ArgTypes>
|
|
|
|
struct V8FunctionInvoker<void(ArgTypes...)> {
|
2015-11-06 12:23:41 +00:00
|
|
|
static void Go(v8::Isolate* isolate,
|
|
|
|
const SafeV8Function& function,
|
2015-08-07 11:15:36 +00:00
|
|
|
ArgTypes... raw) {
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Locker locker(isolate);
|
2015-08-07 11:15:36 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2015-12-03 03:24:33 +00:00
|
|
|
if (!function.IsAlive())
|
2015-11-06 12:23:41 +00:00
|
|
|
return;
|
2020-06-17 17:08:10 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope(isolate, true);
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> holder = function.NewHandle(isolate);
|
2022-02-10 02:58:52 +00:00
|
|
|
v8::Local<v8::Context> context = holder->GetCreationContextChecked();
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-25 00:40:27 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args{
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
|
2019-01-09 19:17:05 +00:00
|
|
|
holder
|
|
|
|
->Call(context, holder, args.size(),
|
|
|
|
args.empty() ? nullptr : &args.front())
|
2019-03-20 15:27:06 +00:00
|
|
|
.IsEmpty();
|
2015-08-07 10:10:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-07 11:15:36 +00:00
|
|
|
template <typename ReturnType, typename... ArgTypes>
|
|
|
|
struct V8FunctionInvoker<ReturnType(ArgTypes...)> {
|
2015-11-06 12:23:41 +00:00
|
|
|
static ReturnType Go(v8::Isolate* isolate,
|
|
|
|
const SafeV8Function& function,
|
2015-08-07 11:15:36 +00:00
|
|
|
ArgTypes... raw) {
|
2019-10-31 07:56:00 +00:00
|
|
|
gin_helper::Locker locker(isolate);
|
2015-08-07 11:15:36 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
2015-11-06 12:23:41 +00:00
|
|
|
ReturnType ret = ReturnType();
|
2015-12-03 03:24:33 +00:00
|
|
|
if (!function.IsAlive())
|
2015-11-06 12:23:41 +00:00
|
|
|
return ret;
|
2020-06-17 17:08:10 +00:00
|
|
|
gin_helper::MicrotasksScope microtasks_scope(isolate, true);
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> holder = function.NewHandle(isolate);
|
2022-02-10 02:58:52 +00:00
|
|
|
v8::Local<v8::Context> context = holder->GetCreationContextChecked();
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-25 00:40:27 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args{
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::ConvertToV8(isolate, std::forward<ArgTypes>(raw))...};
|
2015-10-24 16:40:38 +00:00
|
|
|
v8::Local<v8::Value> result;
|
2018-04-18 01:44:10 +00:00
|
|
|
auto maybe_result = holder->Call(context, holder, args.size(),
|
|
|
|
args.empty() ? nullptr : &args.front());
|
2015-10-24 16:40:38 +00:00
|
|
|
if (maybe_result.ToLocal(&result))
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::Converter<ReturnType>::FromV8(isolate, result, &ret);
|
2015-08-07 10:10:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-01 01:42:38 +00:00
|
|
|
// Helper to pass a C++ function to JavaScript.
|
2021-05-06 22:01:04 +00:00
|
|
|
using Translater = base::RepeatingCallback<void(gin::Arguments* args)>;
|
2018-10-05 15:38:27 +00:00
|
|
|
v8::Local<v8::Value> CreateFunctionFromTranslater(v8::Isolate* isolate,
|
|
|
|
const Translater& translater,
|
|
|
|
bool one_time);
|
2017-11-03 18:00:41 +00:00
|
|
|
v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Function> func,
|
|
|
|
v8::Local<v8::Value> arg1,
|
|
|
|
v8::Local<v8::Value> arg2);
|
2015-10-28 09:36:01 +00:00
|
|
|
|
|
|
|
// Calls callback with Arguments.
|
|
|
|
template <typename Sig>
|
|
|
|
struct NativeFunctionInvoker {};
|
|
|
|
|
|
|
|
template <typename ReturnType, typename... ArgTypes>
|
|
|
|
struct NativeFunctionInvoker<ReturnType(ArgTypes...)> {
|
2021-05-06 22:01:04 +00:00
|
|
|
static void Go(base::RepeatingCallback<ReturnType(ArgTypes...)> val,
|
2019-09-04 15:45:25 +00:00
|
|
|
gin::Arguments* args) {
|
2015-10-28 09:36:01 +00:00
|
|
|
using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type;
|
|
|
|
Invoker<Indices, ArgTypes...> invoker(args, 0);
|
|
|
|
if (invoker.IsOK())
|
|
|
|
invoker.DispatchToCallback(val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-04 14:13:09 +00:00
|
|
|
// Convert a callback to V8 without the call number limitation, this can easily
|
|
|
|
// cause memory leaks so use it with caution.
|
2018-10-05 15:38:27 +00:00
|
|
|
template <typename Sig>
|
2019-09-04 15:45:25 +00:00
|
|
|
v8::Local<v8::Value> CallbackToV8Leaked(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::RepeatingCallback<Sig>& val) {
|
2021-01-25 01:27:40 +00:00
|
|
|
Translater translater =
|
|
|
|
base::BindRepeating(&NativeFunctionInvoker<Sig>::Go, val);
|
2019-09-04 15:45:25 +00:00
|
|
|
return CreateFunctionFromTranslater(isolate, translater, false);
|
2018-10-04 14:13:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 15:45:25 +00:00
|
|
|
} // namespace gin_helper
|
2015-08-07 11:34:00 +00:00
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_CALLBACK_H_
|