2c23e44ed9
* use gin converter in atom_api_menu * please only put necessary includes in header Having include in header means they have dependency relationship, putting arbitrary includes really really really really really makes refacoring much harder. * remove some simple uses of callback_converter_deprecated.h * use gin callback converter in file_dialog code * use gin in ErrorThrower * use gin in atom_bundle_mover * fix mistake in node stream * deprecate native_mate version of event_emitter_caller * use gin in node_bindings * remove usages of native_mate event_emitter_caller.h except for EventEmitter * fix compilation on Windows * gin::Arguments behaves differently on GetNext * just use StringToV8
101 lines
3.5 KiB
C++
101 lines
3.5 KiB
C++
// Copyright (c) 2019 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#ifndef SHELL_COMMON_GIN_HELPER_DICTIONARY_H_
|
|
#define SHELL_COMMON_GIN_HELPER_DICTIONARY_H_
|
|
|
|
#include <type_traits>
|
|
|
|
#include "base/bind.h"
|
|
#include "gin/dictionary.h"
|
|
#include "shell/common/gin_helper/function_template.h"
|
|
|
|
namespace gin_helper {
|
|
|
|
// Base template - used only for non-member function pointers. Other types
|
|
// either go to one of the below specializations, or go here and fail to compile
|
|
// because of base::Bind().
|
|
template <typename T, typename Enable = void>
|
|
struct CallbackTraits {
|
|
static v8::Local<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate,
|
|
T callback) {
|
|
return CreateFunctionTemplate(isolate, base::BindRepeating(callback));
|
|
}
|
|
};
|
|
|
|
// Specialization for base::Callback.
|
|
template <typename T>
|
|
struct CallbackTraits<base::Callback<T>> {
|
|
static v8::Local<v8::FunctionTemplate> CreateTemplate(
|
|
v8::Isolate* isolate,
|
|
const base::RepeatingCallback<T>& callback) {
|
|
return CreateFunctionTemplate(isolate, callback);
|
|
}
|
|
};
|
|
|
|
// Specialization for member function pointers. We need to handle this case
|
|
// specially because the first parameter for callbacks to MFP should typically
|
|
// come from the the JavaScript "this" object the function was called on, not
|
|
// from the first normal parameter.
|
|
template <typename T>
|
|
struct CallbackTraits<
|
|
T,
|
|
typename std::enable_if<std::is_member_function_pointer<T>::value>::type> {
|
|
static v8::Local<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate,
|
|
T callback) {
|
|
int flags = HolderIsFirstArgument;
|
|
return CreateFunctionTemplate(isolate, base::BindRepeating(callback),
|
|
flags);
|
|
}
|
|
};
|
|
|
|
// Adds a few more extends methods to gin::Dictionary.
|
|
//
|
|
// Note that as the destructor of gin::Dictionary is not virtual, and we want to
|
|
// convert between 2 types, we must not add any member.
|
|
class Dictionary : public gin::Dictionary {
|
|
public:
|
|
Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object)
|
|
: gin::Dictionary(isolate, object) {}
|
|
|
|
template <typename T>
|
|
bool SetMethod(base::StringPiece key, const T& callback) {
|
|
auto context = isolate()->GetCurrentContext();
|
|
auto templ = CallbackTraits<T>::CreateTemplate(isolate(), callback);
|
|
return GetHandle()
|
|
->Set(context, gin::StringToV8(isolate(), key),
|
|
templ->GetFunction(context).ToLocalChecked())
|
|
.ToChecked();
|
|
}
|
|
|
|
template <typename T>
|
|
bool SetReadOnly(base::StringPiece key, const T& val) {
|
|
v8::Local<v8::Value> v8_value;
|
|
if (!gin::TryConvertToV8(isolate(), val, &v8_value))
|
|
return false;
|
|
v8::Maybe<bool> result = GetHandle()->DefineOwnProperty(
|
|
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key),
|
|
v8_value, v8::ReadOnly);
|
|
return !result.IsNothing() && result.FromJust();
|
|
}
|
|
|
|
bool Delete(base::StringPiece key) {
|
|
v8::Maybe<bool> result = GetHandle()->Delete(
|
|
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key));
|
|
return !result.IsNothing() && result.FromJust();
|
|
}
|
|
|
|
v8::Local<v8::Object> GetHandle() const {
|
|
return gin::ConvertToV8(isolate(),
|
|
*static_cast<const gin::Dictionary*>(this))
|
|
.As<v8::Object>();
|
|
}
|
|
|
|
private:
|
|
// DO NOT ADD ANY DATA MEMBER.
|
|
};
|
|
|
|
} // namespace gin_helper
|
|
|
|
#endif // SHELL_COMMON_GIN_HELPER_DICTIONARY_H_
|