2015-08-07 10:10:19 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_
|
|
|
|
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_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"
|
|
|
|
#include "base/callback.h"
|
2015-11-06 12:23:41 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2017-03-10 08:33:27 +00:00
|
|
|
#include "base/message_loop/message_loop.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "native_mate/function_template.h"
|
|
|
|
#include "native_mate/scoped_persistent.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/api/locker.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
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) {
|
2015-08-07 10:10:19 +00:00
|
|
|
Locker locker(isolate);
|
|
|
|
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);
|
2016-06-24 05:45:31 +00:00
|
|
|
v8::MicrotasksScope script_scope(isolate,
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> holder = function.NewHandle(isolate);
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Local<v8::Context> context = holder->CreationContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-25 00:40:27 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args{
|
|
|
|
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) {
|
2015-08-07 10:10:19 +00:00
|
|
|
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;
|
2016-06-24 05:45:31 +00:00
|
|
|
v8::MicrotasksScope script_scope(isolate,
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> holder = function.NewHandle(isolate);
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Local<v8::Context> context = holder->CreationContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-25 00:40:27 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args{
|
|
|
|
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) {
|
2015-08-07 10:10:19 +00:00
|
|
|
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;
|
2016-06-24 05:45:31 +00:00
|
|
|
v8::MicrotasksScope script_scope(isolate,
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
2015-12-03 03:24:33 +00:00
|
|
|
v8::Local<v8::Function> holder = function.NewHandle(isolate);
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Local<v8::Context> context = holder->CreationContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-25 00:40:27 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args{
|
|
|
|
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))
|
|
|
|
Converter<ReturnType>::FromV8(isolate, result, &ret);
|
2015-08-07 10:10:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-28 09:36:01 +00:00
|
|
|
// Helper to pass a C++ funtion to JavaScript.
|
|
|
|
using Translater = base::Callback<void(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...)> {
|
|
|
|
static void Go(base::Callback<ReturnType(ArgTypes...)> val, Arguments* args) {
|
|
|
|
using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type;
|
|
|
|
Invoker<Indices, ArgTypes...> invoker(args, 0);
|
|
|
|
if (invoker.IsOK())
|
|
|
|
invoker.DispatchToCallback(val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-07 10:10:19 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
2018-04-17 22:47:12 +00:00
|
|
|
template <typename Sig>
|
|
|
|
struct Converter<base::RepeatingCallback<Sig>> {
|
2015-08-07 10:10:19 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2018-04-17 22:47:12 +00:00
|
|
|
const base::RepeatingCallback<Sig>& val) {
|
2015-10-28 09:36:01 +00:00
|
|
|
// We don't use CreateFunctionTemplate here because it creates a new
|
|
|
|
// FunctionTemplate everytime, which is cached by V8 and causes leaks.
|
2018-10-05 15:38:27 +00:00
|
|
|
internal::Translater translater =
|
|
|
|
base::Bind(&internal::NativeFunctionInvoker<Sig>::Go, val);
|
2018-10-04 14:13:09 +00:00
|
|
|
// To avoid memory leak, we ensure that the callback can only be called
|
|
|
|
// for once.
|
|
|
|
return internal::CreateFunctionFromTranslater(isolate, translater, true);
|
2015-08-07 10:10:19 +00:00
|
|
|
}
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
2018-04-17 22:47:12 +00:00
|
|
|
base::RepeatingCallback<Sig>* out) {
|
2015-08-07 10:10:19 +00:00
|
|
|
if (!val->IsFunction())
|
|
|
|
return false;
|
|
|
|
|
2018-04-17 22:47:12 +00:00
|
|
|
*out = base::BindRepeating(&internal::V8FunctionInvoker<Sig>::Go, isolate,
|
|
|
|
internal::SafeV8Function(isolate, val));
|
2015-08-07 10:10:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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>
|
2018-10-04 14:13:09 +00:00
|
|
|
v8::Local<v8::Value> CallbackToV8(v8::Isolate* isolate,
|
|
|
|
const base::Callback<Sig>& val) {
|
2018-10-05 15:38:27 +00:00
|
|
|
internal::Translater translater =
|
|
|
|
base::Bind(&internal::NativeFunctionInvoker<Sig>::Go, val);
|
2018-10-04 14:13:09 +00:00
|
|
|
return internal::CreateFunctionFromTranslater(isolate, translater, false);
|
|
|
|
}
|
|
|
|
|
2015-08-07 10:10:19 +00:00
|
|
|
} // namespace mate
|
2015-08-07 11:34:00 +00:00
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_
|