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:
Cheng Zhao 2019-09-19 11:09:15 -04:00 committed by Shelley Vohr
parent 63f08fcdb0
commit 624ba4f642
39 changed files with 474 additions and 121 deletions

View file

@ -30,7 +30,7 @@
#include "content/public/common/content_switches.h"
#include "gin/arguments.h"
#include "media/audio/audio_manager.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "net/ssl/client_cert_identity.h"
#include "net/ssl/ssl_cert_request_info.h"
#include "services/service_manager/sandbox/switches.h"

View file

@ -6,7 +6,7 @@
#include "base/time/time.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/browser/browser.h"
#include "shell/browser/native_window.h"
#include "shell/browser/window_list.h"

View file

@ -6,24 +6,22 @@
#include <map>
#include "gin/dictionary.h"
#include "native_mate/constructor.h"
#include "native_mate/object_template_builder.h"
#include "shell/browser/native_window.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/image_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/native_mate_converters/accelerator_converter.h"
#include "shell/common/native_mate_converters/image_converter.h"
#include "shell/common/native_mate_converters/string16_converter.h"
#include "shell/common/node_includes.h"
// TODO(zcbenz): Remove this after removing mate::ObjectTemplateBuilder.
#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
namespace {
// We need this map to keep references to currently opened menus.
// Without this menus would be destroyed by js garbage collector
// even when they are still displayed.
std::map<uint32_t, v8::Global<v8::Object>> g_menus;
} // unnamed namespace
namespace electron {
@ -224,9 +222,9 @@ void Menu::OnMenuWillShow() {
// static
void Menu::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
prototype->SetClassName(gin::StringToV8(isolate, "Menu"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("insertItem", &Menu::InsertItemAt)
.SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
.SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
@ -267,7 +265,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate();
Menu::SetConstructor(isolate, base::BindRepeating(&Menu::New));
mate::Dictionary dict(isolate, exports);
gin_helper::Dictionary dict(isolate, exports);
dict.Set(
"Menu",
Menu::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());

View file

@ -155,6 +155,20 @@ struct Converter<electron::AtomMenuModel*> {
}
};
// TODO(zcbenz): Remove this after converting Menu to gin::Wrapper.
template <>
struct Converter<electron::api::Menu*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::Menu** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::Menu* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_MENU_H_

View file

@ -10,7 +10,7 @@
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/common/node_includes.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"

View file

@ -8,7 +8,7 @@
#include "base/strings/utf_string_conversions.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/browser/api/atom_api_menu.h"
#include "shell/browser/atom_browser_client.h"
#include "shell/browser/browser.h"

View file

@ -9,9 +9,9 @@
#include "base/bind.h"
#include "gin/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "shell/browser/browser.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/native_mate_converters/gfx_converter.h"
#include "shell/common/native_mate_converters/native_window_converter.h"
#include "shell/common/node_includes.h"
@ -134,22 +134,22 @@ void Screen::OnDisplayMetricsChanged(const display::Display& display,
}
// static
v8::Local<v8::Value> Screen::Create(v8::Isolate* isolate) {
v8::Local<v8::Value> Screen::Create(gin_helper::ErrorThrower error_thrower) {
if (!Browser::Get()->is_ready()) {
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
isolate,
"The 'screen' module can't be used before the app 'ready' event")));
return v8::Null(isolate);
error_thrower.ThrowError(
"The 'screen' module can't be used before the app 'ready' event");
return v8::Null(error_thrower.isolate());
}
display::Screen* screen = display::Screen::GetScreen();
if (!screen) {
isolate->ThrowException(v8::Exception::Error(
mate::StringToV8(isolate, "Failed to get screen information")));
return v8::Null(isolate);
error_thrower.ThrowError("Failed to get screen information");
return v8::Null(error_thrower.isolate());
}
return mate::CreateHandle(isolate, new Screen(isolate, screen)).ToV8();
return mate::CreateHandle(error_thrower.isolate(),
new Screen(error_thrower.isolate(), screen))
.ToV8();
}
// static

View file

@ -7,8 +7,8 @@
#include <vector>
#include "native_mate/handle.h"
#include "shell/browser/api/event_emitter.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "ui/display/display_observer.h"
#include "ui/display/screen.h"
@ -25,7 +25,7 @@ namespace api {
class Screen : public mate::EventEmitter<Screen>,
public display::DisplayObserver {
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate);
static v8::Local<v8::Value> Create(gin_helper::ErrorThrower error_thrower);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -33,7 +33,7 @@
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "net/base/completion_repeating_callback.h"
#include "net/base/load_flags.h"
#include "net/http/http_auth_handler_factory.h"

View file

@ -121,4 +121,22 @@ class Session : public mate::TrackableObject<Session>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting Session to gin::Wrapper.
template <>
struct Converter<electron::api::Session*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::Session** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::Session* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_SESSION_H_

View file

@ -21,7 +21,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/values.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "net/base/mac/url_conversions.h"
#include "shell/browser/mac/atom_application.h"
#include "shell/browser/mac/dict_util.h"

View file

@ -266,4 +266,22 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting TopLevelWindow to gin::Wrapper.
template <>
struct Converter<electron::api::TopLevelWindow*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::TopLevelWindow** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::TopLevelWindow* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_TOP_LEVEL_WINDOW_H_

View file

@ -9,11 +9,12 @@
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/system/string_data_source.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/http/http_util.h"
#include "services/network/public/mojom/chunked_data_pipe_getter.mojom.h"
#include "shell/browser/api/atom_api_session.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/native_mate_converters/gurl_converter.h"
#include "shell/common/native_mate_converters/net_converter.h"
@ -312,8 +313,8 @@ void URLRequestNS::SetChunkedUpload(bool is_chunked_upload) {
is_chunked_upload_ = is_chunked_upload;
}
mate::Dictionary URLRequestNS::GetUploadProgress() {
mate::Dictionary progress = mate::Dictionary::CreateEmpty(isolate());
gin::Dictionary URLRequestNS::GetUploadProgress() {
gin::Dictionary progress = gin::Dictionary::CreateEmpty(isolate());
if (loader_) {
if (request_)
progress.Set("started", false);
@ -504,7 +505,7 @@ void URLRequestNS::EmitError(EventType type, base::StringPiece message) {
else
response_state_ |= STATE_FAILED;
v8::HandleScope handle_scope(isolate());
auto error = v8::Exception::Error(mate::StringToV8(isolate(), message));
auto error = v8::Exception::Error(gin::StringToV8(isolate(), message));
EmitEvent(type, false, "error", error);
}
@ -513,7 +514,7 @@ void URLRequestNS::EmitEvent(EventType type, Args... args) {
const char* method =
type == EventType::kRequest ? "_emitRequestEvent" : "_emitResponseEvent";
v8::HandleScope handle_scope(isolate());
mate::CustomEmit(isolate(), GetWrapper(), method, args...);
gin_helper::CustomEmit(isolate(), GetWrapper(), method, args...);
}
// static
@ -524,9 +525,9 @@ mate::WrappableBase* URLRequestNS::New(mate::Arguments* args) {
// static
void URLRequestNS::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "URLRequest"));
prototype->SetClassName(gin::StringToV8(isolate, "URLRequest"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("write", &URLRequestNS::Write)
.SetMethod("cancel", &URLRequestNS::Cancel)
.SetMethod("setExtraHeader", &URLRequestNS::SetExtraHeader)

View file

@ -10,8 +10,9 @@
#include <string>
#include <vector>
#include "gin/arguments.h"
#include "gin/dictionary.h"
#include "mojo/public/cpp/system/data_pipe_producer.h"
#include "native_mate/dictionary.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/cpp/simple_url_loader_stream_consumer.h"
@ -47,7 +48,7 @@ class URLRequestNS : public mate::EventEmitter<URLRequestNS>,
bool SetExtraHeader(const std::string& name, const std::string& value);
void RemoveExtraHeader(const std::string& name);
void SetChunkedUpload(bool is_chunked_upload);
mate::Dictionary GetUploadProgress();
gin::Dictionary GetUploadProgress();
int StatusCode() const;
std::string StatusMessage() const;
net::HttpResponseHeaders* RawResponseHeaders() const;
@ -140,4 +141,22 @@ class URLRequestNS : public mate::EventEmitter<URLRequestNS>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting URLRequestNS to gin::Wrapper.
template <>
struct Converter<electron::api::URLRequestNS*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::URLRequestNS** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::URLRequestNS* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_URL_REQUEST_NS_H_

View file

@ -46,7 +46,7 @@
#include "mojo/public/cpp/system/platform_handle.h"
#include "native_mate/converter.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/browser/api/atom_api_browser_window.h"
#include "shell/browser/api/atom_api_debugger.h"
#include "shell/browser/api/atom_api_session.h"

View file

@ -6,7 +6,7 @@
#include <utility>
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/common/native_mate_converters/value_converter.h"
namespace mate {

View file

@ -9,7 +9,7 @@
#include "content/public/browser/render_frame_host.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/browser/api/event.h"
#include "shell/common/node_includes.h"
#include "ui/events/event_constants.h"

View file

@ -9,7 +9,7 @@
#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/browser/api/event_emitter.h"
#include "shell/common/key_weak_map.h"

View file

@ -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"

View file

@ -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_

View file

@ -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"

View file

@ -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",

View 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_

View file

@ -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,

View file

@ -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,

View file

@ -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

View file

@ -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_

View 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

View 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_

View file

@ -7,14 +7,13 @@
#include "base/task/post_task.h"
#include "base/values.h"
#include "content/public/renderer/render_frame.h"
#include "electron/shell/common/api/api.mojom.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "native_mate/object_template_builder.h"
#include "native_mate/wrappable.h"
#include "gin/dictionary.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "gin/wrappable.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "shell/common/native_mate_converters/value_converter.h"
#include "shell/common/api/api.mojom.h"
#include "shell/common/gin_converters/value_converter_gin_adapter.h"
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
#include "shell/common/promise_util.h"
@ -33,11 +32,16 @@ RenderFrame* GetCurrentRenderFrame() {
return RenderFrame::FromWebFrame(frame);
}
class IPCRenderer : public mate::Wrappable<IPCRenderer> {
class IPCRenderer : public gin::Wrappable<IPCRenderer> {
public:
static gin::WrapperInfo kWrapperInfo;
static gin::Handle<IPCRenderer> Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new IPCRenderer(isolate));
}
explicit IPCRenderer(v8::Isolate* isolate)
: task_runner_(base::CreateSingleThreadTaskRunner({base::ThreadPool()})) {
Init(isolate);
RenderFrame* render_frame = GetCurrentRenderFrame();
DCHECK(render_frame);
@ -51,10 +55,10 @@ class IPCRenderer : public mate::Wrappable<IPCRenderer> {
task_runner_);
}
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "IPCRenderer"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
// gin::Wrappable:
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override {
return gin::Wrappable<IPCRenderer>::GetObjectTemplateBuilder(isolate)
.SetMethod("send", &IPCRenderer::Send)
.SetMethod("sendSync", &IPCRenderer::SendSync)
.SetMethod("sendTo", &IPCRenderer::SendTo)
@ -62,27 +66,26 @@ class IPCRenderer : public mate::Wrappable<IPCRenderer> {
.SetMethod("invoke", &IPCRenderer::Invoke);
}
static mate::Handle<IPCRenderer> Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new IPCRenderer(isolate));
}
const char* GetTypeName() override { return "IPCRenderer"; }
private:
void Send(bool internal,
const std::string& channel,
const base::ListValue& arguments) {
electron_browser_ptr_->get()->Message(internal, channel, arguments.Clone());
}
v8::Local<v8::Promise> Invoke(mate::Arguments* args,
v8::Local<v8::Promise> Invoke(v8::Isolate* isolate,
bool internal,
const std::string& channel,
const base::Value& arguments) {
electron::util::Promise<base::Value> p(args->isolate());
electron::util::Promise<base::Value> p(isolate);
auto handle = p.GetHandle();
electron_browser_ptr_->get()->Invoke(
internal, channel, arguments.Clone(),
base::BindOnce([](electron::util::Promise<base::Value> p,
base::Value result) { p.Resolve(result); },
base::Value result) { p.ResolveWithGin(result); },
std::move(p)));
return handle;
@ -169,7 +172,6 @@ class IPCRenderer : public mate::Wrappable<IPCRenderer> {
return result;
}
private:
void SendMessageSyncOnWorkerThread(base::WaitableEvent* event,
base::Value* result,
bool internal,
@ -180,6 +182,7 @@ class IPCRenderer : public mate::Wrappable<IPCRenderer> {
base::BindOnce(&IPCRenderer::ReturnSyncResponseToMainThread,
base::Unretained(event), base::Unretained(result)));
}
static void ReturnSyncResponseToMainThread(base::WaitableEvent* event,
base::Value* result,
base::Value response) {
@ -192,11 +195,13 @@ class IPCRenderer : public mate::Wrappable<IPCRenderer> {
electron_browser_ptr_;
};
gin::WrapperInfo IPCRenderer::kWrapperInfo = {gin::kEmbedderNativeGin};
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::Dictionary dict(context->GetIsolate(), exports);
dict.Set("ipc", IPCRenderer::Create(context->GetIsolate()));
}