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"
|
2015-08-07 10:10:19 +00:00
|
|
|
#include "native_mate/function_template.h"
|
|
|
|
#include "native_mate/scoped_persistent.h"
|
2015-08-07 11:34:00 +00:00
|
|
|
#include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
|
2015-08-07 10:10:19 +00:00
|
|
|
|
|
|
|
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);
|
2015-08-07 11:34:00 +00:00
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
|
|
|
Locker::IsBrowserProcess() ?
|
2015-12-07 11:56:23 +00:00
|
|
|
nullptr : new blink::WebScopedRunV8Script);
|
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);
|
2015-08-07 11:15:36 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args = { ConvertToV8(isolate, raw)... };
|
|
|
|
v8::Local<v8::Value> ret(holder->Call(holder, args.size(), &args.front()));
|
|
|
|
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;
|
2015-08-07 11:34:00 +00:00
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
|
|
|
Locker::IsBrowserProcess() ?
|
2015-12-07 11:56:23 +00:00
|
|
|
nullptr : new blink::WebScopedRunV8Script);
|
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);
|
2015-08-07 11:15:36 +00:00
|
|
|
std::vector<v8::Local<v8::Value>> args = { ConvertToV8(isolate, raw)... };
|
|
|
|
holder->Call(holder, args.size(), &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;
|
2015-08-07 11:34:00 +00:00
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
|
|
|
Locker::IsBrowserProcess() ?
|
2015-12-07 11:56:23 +00:00
|
|
|
nullptr : new blink::WebScopedRunV8Script);
|
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);
|
2015-08-07 11:15:36 +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;
|
|
|
|
auto maybe_result =
|
|
|
|
holder->Call(context, holder, args.size(), &args.front());
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
template<typename Sig>
|
2015-10-28 10:32:21 +00:00
|
|
|
struct Converter<base::Callback<Sig>> {
|
2015-08-07 10:10:19 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2015-10-28 09:36:01 +00:00
|
|
|
const base::Callback<Sig>& val) {
|
|
|
|
// We don't use CreateFunctionTemplate here because it creates a new
|
|
|
|
// FunctionTemplate everytime, which is cached by V8 and causes leaks.
|
2015-10-28 10:32:21 +00:00
|
|
|
internal::Translater translater = base::Bind(
|
|
|
|
&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,
|
|
|
|
base::Callback<Sig>* out) {
|
|
|
|
if (!val->IsFunction())
|
|
|
|
return false;
|
|
|
|
|
2015-11-06 12:23:41 +00:00
|
|
|
*out = base::Bind(&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_
|