chore: remove native_mate (Part 5) (#20264)
* deprecate native_mate/native_mate/object_template_builder.h * add gin_helper/object_template_builder.h * add patch to avoid ambiguous error * remove usage of object_template_builder_deprecated.h in a few files * add note we should remove gin_helper/object_template_builder.h in future
This commit is contained in:
parent
63f08fcdb0
commit
624ba4f642
39 changed files with 474 additions and 121 deletions
|
@ -7,7 +7,7 @@
|
|||
#include <vector>
|
||||
|
||||
#include "native_mate/arguments.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "native_mate/object_template_builder_deprecated.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
#include "shell/common/asar/archive.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
|
|
|
@ -6,8 +6,9 @@
|
|||
#define SHELL_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_
|
||||
|
||||
#include "native_mate/handle.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
#include "shell/common/gin_converters/std_converter.h"
|
||||
#include "shell/common/gin_helper/object_template_builder.h"
|
||||
#include "shell/common/key_weak_map.h"
|
||||
|
||||
namespace electron {
|
||||
|
@ -23,8 +24,8 @@ class KeyWeakMap : public mate::Wrappable<KeyWeakMap<K>> {
|
|||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Local<v8::FunctionTemplate> prototype) {
|
||||
prototype->SetClassName(mate::StringToV8(isolate, "KeyWeakMap"));
|
||||
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||
prototype->SetClassName(gin::StringToV8(isolate, "KeyWeakMap"));
|
||||
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
||||
.SetMethod("set", &KeyWeakMap<K>::Set)
|
||||
.SetMethod("get", &KeyWeakMap<K>::Get)
|
||||
.SetMethod("has", &KeyWeakMap<K>::Has)
|
||||
|
@ -60,4 +61,22 @@ class KeyWeakMap : public mate::Wrappable<KeyWeakMap<K>> {
|
|||
|
||||
} // namespace electron
|
||||
|
||||
namespace gin {
|
||||
|
||||
// TODO(zcbenz): Remove this after converting KeyWeakMap to gin::Wrapper.
|
||||
template <typename T>
|
||||
struct Converter<electron::api::KeyWeakMap<T>*> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
electron::api::KeyWeakMap<T>** out) {
|
||||
return mate::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
electron::api::KeyWeakMap<T>* in) {
|
||||
return mate::ConvertToV8(isolate, in);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#include "base/strings/pattern.h"
|
||||
#include "base/strings/string_util.h"
|
||||
#include "base/threading/thread_restrictions.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "native_mate/object_template_builder_deprecated.h"
|
||||
#include "net/base/data_url.h"
|
||||
#include "shell/common/asar/asar_util.h"
|
||||
#include "shell/common/native_mate_converters/file_path_converter.h"
|
||||
|
|
|
@ -7,9 +7,9 @@
|
|||
|
||||
#include "base/hash/hash.h"
|
||||
#include "electron/buildflags/buildflags.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/common/native_mate_converters/content_converter.h"
|
||||
#include "shell/common/native_mate_converters/gurl_converter.h"
|
||||
#include "shell/common/gin_converters/gurl_converter.h"
|
||||
#include "shell/common/gin_converters/native_mate_handle_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "url/origin.h"
|
||||
#include "v8/include/v8-profiler.h"
|
||||
|
@ -20,6 +20,10 @@
|
|||
#include "shell/common/api/remote/remote_object_freer.h"
|
||||
#endif
|
||||
|
||||
// TODO(zcbenz): Remove the includes after removing native_mate.
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/common/native_mate_converters/content_converter.h"
|
||||
|
||||
namespace std {
|
||||
|
||||
// The hash function used by DoubleIDWeakMap.
|
||||
|
@ -32,7 +36,7 @@ struct hash<std::pair<Type1, Type2>> {
|
|||
|
||||
} // namespace std
|
||||
|
||||
namespace mate {
|
||||
namespace gin {
|
||||
|
||||
template <typename Type1, typename Type2>
|
||||
struct Converter<std::pair<Type1, Type2>> {
|
||||
|
@ -54,7 +58,7 @@ struct Converter<std::pair<Type1, Type2>> {
|
|||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
} // namespace gin
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -115,15 +119,18 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
v8::Local<v8::Value> unused,
|
||||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
||||
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
||||
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
||||
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
|
||||
dict.SetMethod("getObjectHash", &GetObjectHash);
|
||||
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
||||
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
|
||||
dict.SetMethod("setRemoteCallbackFreer",
|
||||
&electron::RemoteCallbackFreer::BindTo);
|
||||
// TODO(zcbenz): Use gin_helper::Dictionary when content_converter.h is moved
|
||||
// to gin.
|
||||
mate::Dictionary mdict(context->GetIsolate(), exports);
|
||||
mdict.SetMethod("setRemoteCallbackFreer",
|
||||
&electron::RemoteCallbackFreer::BindTo);
|
||||
dict.SetMethod("setRemoteObjectFreer", &electron::RemoteObjectFreer::BindTo);
|
||||
dict.SetMethod("addRemoteObjectRef", &electron::RemoteObjectFreer::AddRef);
|
||||
dict.SetMethod("createIDWeakMap",
|
||||
|
|
30
shell/common/gin_converters/native_mate_handle_converter.h
Normal file
30
shell/common/gin_converters/native_mate_handle_converter.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
// 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_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_
|
||||
#define SHELL_COMMON_GIN_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_
|
||||
|
||||
#include "gin/converter.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace gin {
|
||||
|
||||
// TODO(zcbenz): Remove this converter after native_mate is removed.
|
||||
|
||||
template <typename T>
|
||||
struct Converter<mate::Handle<T>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const mate::Handle<T>& in) {
|
||||
return mate::ConvertToV8(isolate, in);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
mate::Handle<T>* out) {
|
||||
return mate::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_GIN_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_
|
|
@ -63,6 +63,22 @@ struct Converter<v8::Local<v8::Array>> {
|
|||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<v8::Local<v8::String>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::String> val) {
|
||||
return val;
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
v8::Local<v8::String>* out) {
|
||||
if (!val->IsString())
|
||||
return false;
|
||||
*out = v8::Local<v8::String>::Cast(val);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct Converter<std::set<T>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
|
|
|
@ -25,6 +25,19 @@ struct Converter<base::DictionaryValue> {
|
|||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<base::ListValue> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
base::ListValue* out) {
|
||||
return mate::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::ListValue& val) {
|
||||
return mate::ConvertToV8(isolate, val);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<base::Value> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
|
|
|
@ -7,49 +7,11 @@
|
|||
|
||||
#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
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#ifndef SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_H_
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "gin/arguments.h"
|
||||
#include "shell/common/gin_helper/destroyable.h"
|
||||
|
@ -258,6 +259,44 @@ v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
|
|||
isolate, holder->GetHandle(isolate)));
|
||||
}
|
||||
|
||||
// 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 gin_helper::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 gin_helper::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 gin_helper::CreateFunctionTemplate(
|
||||
isolate, base::BindRepeating(callback), flags);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_H_
|
||||
|
|
32
shell/common/gin_helper/object_template_builder.cc
Normal file
32
shell/common/gin_helper/object_template_builder.cc
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Copyright (c) 2019 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/gin_helper/object_template_builder.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
ObjectTemplateBuilder::ObjectTemplateBuilder(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::ObjectTemplate> templ)
|
||||
: isolate_(isolate), template_(templ) {}
|
||||
|
||||
ObjectTemplateBuilder::~ObjectTemplateBuilder() = default;
|
||||
|
||||
ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl(
|
||||
const base::StringPiece& name,
|
||||
v8::Local<v8::Data> val) {
|
||||
template_->Set(gin::StringToSymbol(isolate_, name), val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ObjectTemplateBuilder& ObjectTemplateBuilder::SetPropertyImpl(
|
||||
const base::StringPiece& name,
|
||||
v8::Local<v8::FunctionTemplate> getter,
|
||||
v8::Local<v8::FunctionTemplate> setter) {
|
||||
template_->SetAccessorProperty(gin::StringToSymbol(isolate_, name), getter,
|
||||
setter);
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
75
shell/common/gin_helper/object_template_builder.h
Normal file
75
shell/common/gin_helper/object_template_builder.h
Normal file
|
@ -0,0 +1,75 @@
|
|||
// Copyright (c) 2019 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
|
||||
|
||||
#include "shell/common/gin_helper/function_template.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
// This class works like gin::ObjectTemplateBuilder, but operates on existing
|
||||
// prototype template instead of creating a new one.
|
||||
//
|
||||
// It also uses gin_helper::CreateFunctionTemplate for function templates to
|
||||
// support gin_helper types.
|
||||
//
|
||||
// TODO(zcbenz): We should patch gin::ObjectTemplateBuilder to provide the same
|
||||
// functionality after removing gin_helper/function_template.h.
|
||||
class ObjectTemplateBuilder {
|
||||
public:
|
||||
ObjectTemplateBuilder(v8::Isolate* isolate,
|
||||
v8::Local<v8::ObjectTemplate> templ);
|
||||
~ObjectTemplateBuilder();
|
||||
|
||||
// It's against Google C++ style to return a non-const ref, but we take some
|
||||
// poetic license here in order that all calls to Set() can be via the '.'
|
||||
// operator and line up nicely.
|
||||
template <typename T>
|
||||
ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) {
|
||||
return SetImpl(name, ConvertToV8(isolate_, val));
|
||||
}
|
||||
|
||||
// In the following methods, T and U can be function pointer, member function
|
||||
// pointer, base::Callback, or v8::FunctionTemplate. Most clients will want to
|
||||
// use one of the first two options. Also see gin::CreateFunctionTemplate()
|
||||
// for creating raw function templates.
|
||||
template <typename T>
|
||||
ObjectTemplateBuilder& SetMethod(const base::StringPiece& name,
|
||||
const T& callback) {
|
||||
return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback));
|
||||
}
|
||||
template <typename T>
|
||||
ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
|
||||
const T& getter) {
|
||||
return SetPropertyImpl(name,
|
||||
CallbackTraits<T>::CreateTemplate(isolate_, getter),
|
||||
v8::Local<v8::FunctionTemplate>());
|
||||
}
|
||||
template <typename T, typename U>
|
||||
ObjectTemplateBuilder& SetProperty(const base::StringPiece& name,
|
||||
const T& getter,
|
||||
const U& setter) {
|
||||
return SetPropertyImpl(name,
|
||||
CallbackTraits<T>::CreateTemplate(isolate_, getter),
|
||||
CallbackTraits<T>::CreateTemplate(isolate_, setter));
|
||||
}
|
||||
|
||||
private:
|
||||
ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
|
||||
v8::Local<v8::Data> val);
|
||||
ObjectTemplateBuilder& SetPropertyImpl(
|
||||
const base::StringPiece& name,
|
||||
v8::Local<v8::FunctionTemplate> getter,
|
||||
v8::Local<v8::FunctionTemplate> setter);
|
||||
|
||||
v8::Isolate* isolate_;
|
||||
|
||||
// ObjectTemplateBuilder should only be used on the stack.
|
||||
v8::Local<v8::ObjectTemplate> template_;
|
||||
};
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_
|
Loading…
Add table
Add a link
Reference in a new issue