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.
|
|
|
|
|
2015-08-07 11:34:00 +00:00
|
|
|
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_
|
|
|
|
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_
|
|
|
|
|
2015-08-07 11:15:36 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-08-07 11:34:00 +00:00
|
|
|
#include "atom/common/api/locker.h"
|
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"
|
2017-03-10 02:01:17 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "native_mate/function_template.h"
|
|
|
|
#include "native_mate/scoped_persistent.h"
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2015-12-03 03:24:33 +00:00
|
|
|
template<typename T>
|
|
|
|
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);
|
2017-06-30 09:40:35 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args { ConvertToV8(isolate, raw)... };
|
2017-07-31 07:47:14 +00:00
|
|
|
v8::Local<v8::Value> ret(holder->Call(
|
|
|
|
holder, args.size(), args.empty() ? nullptr : &args.front()));
|
2015-08-07 11:15:36 +00:00
|
|
|
return handle_scope.Escape(ret);
|
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);
|
2017-06-30 09:40:35 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args { ConvertToV8(isolate, raw)... };
|
2017-07-31 07:47:14 +00:00
|
|
|
holder->Call(
|
|
|
|
holder, args.size(), args.empty() ? nullptr : &args.front());
|
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);
|
2017-06-30 09:40:35 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args { ConvertToV8(isolate, raw)... };
|
2015-10-24 16:40:38 +00:00
|
|
|
v8::Local<v8::Value> result;
|
2017-07-31 07:47:14 +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)>;
|
|
|
|
v8::Local<v8::Value> CreateFunctionFromTranslater(
|
|
|
|
v8::Isolate* isolate, const Translater& translater);
|
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::OnceCallback<Sig>> {
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
base::OnceCallback<Sig>* out) {
|
|
|
|
if (!val->IsFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*out = base::BindOnce(&internal::V8FunctionInvoker<Sig>::Go, isolate,
|
|
|
|
internal::SafeV8Function(isolate, val));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-04-17 22:47:12 +00:00
|
|
|
internal::Translater translater =
|
|
|
|
base::BindRepeating(&internal::NativeFunctionInvoker<Sig>::Go, val);
|
2015-10-28 09:36:01 +00:00
|
|
|
return internal::CreateFunctionFromTranslater(isolate, translater);
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
2015-08-07 11:34:00 +00:00
|
|
|
|
|
|
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_
|