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"
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
typedef scoped_refptr<RefCountedPersistent<v8::Function> > SafeV8Function;
|
|
|
|
|
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...)> {
|
|
|
|
static v8::Local<v8::Value> Go(v8::Isolate* isolate, SafeV8Function function,
|
|
|
|
ArgTypes... raw) {
|
2015-08-07 10:10:19 +00:00
|
|
|
Locker locker(isolate);
|
|
|
|
v8::EscapableHandleScope handle_scope(isolate);
|
2015-08-07 11:34:00 +00:00
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
|
|
|
Locker::IsBrowserProcess() ?
|
|
|
|
nullptr : new blink::WebScopedRunV8Script(isolate));
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Local<v8::Function> holder = function->NewHandle();
|
|
|
|
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...)> {
|
|
|
|
static void Go(v8::Isolate* isolate, SafeV8Function function,
|
|
|
|
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-08-07 11:34:00 +00:00
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
|
|
|
Locker::IsBrowserProcess() ?
|
|
|
|
nullptr : new blink::WebScopedRunV8Script(isolate));
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Local<v8::Function> holder = function->NewHandle();
|
|
|
|
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...)> {
|
|
|
|
static ReturnType Go(v8::Isolate* isolate, SafeV8Function function,
|
|
|
|
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-08-07 11:34:00 +00:00
|
|
|
scoped_ptr<blink::WebScopedRunV8Script> script_scope(
|
|
|
|
Locker::IsBrowserProcess() ?
|
|
|
|
nullptr : new blink::WebScopedRunV8Script(isolate));
|
2015-08-07 10:10:19 +00:00
|
|
|
v8::Local<v8::Function> holder = function->NewHandle();
|
|
|
|
v8::Local<v8::Context> context = holder->CreationContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2015-08-07 11:15:36 +00:00
|
|
|
ReturnType ret;
|
|
|
|
std::vector<v8::Local<v8::Value>> args = { ConvertToV8(isolate, raw)... };
|
|
|
|
v8::Local<v8::Value> val(holder->Call(holder, args.size(), &args.front()));
|
|
|
|
Converter<ReturnType>::FromV8(isolate, val, &ret);
|
2015-08-07 10:10:19 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
template<typename Sig>
|
|
|
|
struct Converter<base::Callback<Sig> > {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const base::Callback<Sig>& val) {
|
|
|
|
return CreateFunctionTemplate(isolate, val)->GetFunction();
|
|
|
|
}
|
|
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Value> val,
|
|
|
|
base::Callback<Sig>* out) {
|
|
|
|
if (!val->IsFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
internal::SafeV8Function function(
|
|
|
|
new RefCountedPersistent<v8::Function>(isolate, val));
|
|
|
|
*out = base::Bind(&internal::V8FunctionInvoker<Sig>::Go, isolate, function);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
2015-08-07 11:34:00 +00:00
|
|
|
|
|
|
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_CALLBACK_H_
|