From 624ba4f642d3ef33bde23ff311f1e5ef03a69a28 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 19 Sep 2019 11:09:15 -0400 Subject: [PATCH] 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 --- filenames.gni | 3 + native_mate/BUILD.gn | 2 +- native_mate/README.md | 2 +- native_mate/native_mate/dictionary.h | 2 +- .../native_mate/object_template_builder.cc | 2 +- ...h => object_template_builder_deprecated.h} | 6 +- native_mate/native_mate/wrappable.cc | 2 +- patches/chromium/.patches | 1 + patches/chromium/gin_with_namespace.patch | 83 +++++++++++++++++++ shell/browser/api/atom_api_app.cc | 2 +- shell/browser/api/atom_api_auto_updater.cc | 2 +- shell/browser/api/atom_api_menu.cc | 18 ++-- shell/browser/api/atom_api_menu.h | 14 ++++ shell/browser/api/atom_api_native_theme.cc | 2 +- shell/browser/api/atom_api_notification.cc | 2 +- shell/browser/api/atom_api_screen.cc | 20 ++--- shell/browser/api/atom_api_screen.h | 4 +- shell/browser/api/atom_api_session.cc | 2 +- shell/browser/api/atom_api_session.h | 18 ++++ .../api/atom_api_system_preferences_mac.mm | 2 +- shell/browser/api/atom_api_top_level_window.h | 18 ++++ shell/browser/api/atom_api_url_request_ns.cc | 15 ++-- shell/browser/api/atom_api_url_request_ns.h | 23 ++++- shell/browser/api/atom_api_web_contents.cc | 2 +- shell/browser/api/event.cc | 2 +- shell/browser/api/event_emitter.cc | 2 +- shell/browser/api/trackable_object.h | 2 +- shell/common/api/atom_api_asar.cc | 2 +- shell/common/api/atom_api_key_weak_map.h | 25 +++++- shell/common/api/atom_api_native_image.cc | 2 +- shell/common/api/atom_api_v8_util.cc | 23 +++-- .../native_mate_handle_converter.h | 30 +++++++ shell/common/gin_converters/std_converter.h | 16 ++++ .../value_converter_gin_adapter.h | 13 +++ shell/common/gin_helper/dictionary.h | 38 --------- shell/common/gin_helper/function_template.h | 39 +++++++++ .../gin_helper/object_template_builder.cc | 32 +++++++ .../gin_helper/object_template_builder.h | 75 +++++++++++++++++ shell/renderer/api/atom_api_renderer_ipc.cc | 47 ++++++----- 39 files changed, 474 insertions(+), 121 deletions(-) rename native_mate/native_mate/{object_template_builder.h => object_template_builder_deprecated.h} (95%) create mode 100644 patches/chromium/gin_with_namespace.patch create mode 100644 shell/common/gin_converters/native_mate_handle_converter.h create mode 100644 shell/common/gin_helper/object_template_builder.cc create mode 100644 shell/common/gin_helper/object_template_builder.h diff --git a/filenames.gni b/filenames.gni index 29f345e0a53..4435343b47a 100644 --- a/filenames.gni +++ b/filenames.gni @@ -476,6 +476,7 @@ filenames = { "shell/common/gin_converters/gurl_converter.h", "shell/common/gin_converters/image_converter.cc", "shell/common/gin_converters/image_converter.h", + "shell/common/gin_converters/native_mate_handle_converter.h", "shell/common/gin_converters/message_box_converter.cc", "shell/common/gin_converters/message_box_converter.h", "shell/common/gin_converters/native_window_converter.h", @@ -494,6 +495,8 @@ filenames = { "shell/common/gin_helper/event_emitter_caller.h", "shell/common/gin_helper/function_template.cc", "shell/common/gin_helper/function_template.h", + "shell/common/gin_helper/object_template_builder.cc", + "shell/common/gin_helper/object_template_builder.h", "shell/common/heap_snapshot.cc", "shell/common/heap_snapshot.h", "shell/common/key_weak_map.h", diff --git a/native_mate/BUILD.gn b/native_mate/BUILD.gn index f6f234cef8f..c89ce890000 100644 --- a/native_mate/BUILD.gn +++ b/native_mate/BUILD.gn @@ -23,7 +23,7 @@ source_set("native_mate") { "native_mate/function_template.h", "native_mate/handle.h", "native_mate/object_template_builder.cc", - "native_mate/object_template_builder.h", + "native_mate/object_template_builder_deprecated.h", "native_mate/persistent_dictionary.cc", "native_mate/persistent_dictionary.h", "native_mate/scoped_persistent.h", diff --git a/native_mate/README.md b/native_mate/README.md index 655e8f7f123..24f2dc16920 100644 --- a/native_mate/README.md +++ b/native_mate/README.md @@ -45,7 +45,7 @@ void Initialize(v8::Handle exports) { types. You can define your own by specializing `Converter`. * `function_template.h` - Create JavaScript functions that dispatch to any C++ function, member function pointer, or `base::Callback`. -* `object_template_builder.h` - A handy utility for creation of `v8::ObjectTemplate`. +* `object_template_builder_deprecated.h` - A handy utility for creation of `v8::ObjectTemplate`. * `wrappable.h` - Base class for C++ classes that want to be owned by the V8 GC. Wrappable objects are automatically deleted when GC discovers that nothing in the V8 heap refers to them. This is also an easy way to expose C++ objects to diff --git a/native_mate/native_mate/dictionary.h b/native_mate/native_mate/dictionary.h index d6a430d9200..9bfc0d55b06 100644 --- a/native_mate/native_mate/dictionary.h +++ b/native_mate/native_mate/dictionary.h @@ -6,7 +6,7 @@ #define NATIVE_MATE_NATIVE_MATE_DICTIONARY_H_ #include "native_mate/converter.h" -#include "native_mate/object_template_builder.h" +#include "native_mate/object_template_builder_deprecated.h" namespace mate { diff --git a/native_mate/native_mate/object_template_builder.cc b/native_mate/native_mate/object_template_builder.cc index 833dca24aa8..4adb90e6fa8 100644 --- a/native_mate/native_mate/object_template_builder.cc +++ b/native_mate/native_mate/object_template_builder.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE.chromium file. -#include "native_mate/object_template_builder.h" +#include "native_mate/object_template_builder_deprecated.h" namespace mate { diff --git a/native_mate/native_mate/object_template_builder.h b/native_mate/native_mate/object_template_builder_deprecated.h similarity index 95% rename from native_mate/native_mate/object_template_builder.h rename to native_mate/native_mate/object_template_builder_deprecated.h index a576f4edfc8..efe4985e912 100644 --- a/native_mate/native_mate/object_template_builder.h +++ b/native_mate/native_mate/object_template_builder_deprecated.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE.chromium file. -#ifndef NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_H_ -#define NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_H_ +#ifndef NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_DEPRECATED_H_ +#define NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_DEPRECATED_H_ #include "base/bind.h" #include "base/callback.h" @@ -117,4 +117,4 @@ class ObjectTemplateBuilder { } // namespace mate -#endif // NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_H_ +#endif // NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_DEPRECATED_H_ diff --git a/native_mate/native_mate/wrappable.cc b/native_mate/native_mate/wrappable.cc index 8ee35e32b7a..086751dfc3d 100644 --- a/native_mate/native_mate/wrappable.cc +++ b/native_mate/native_mate/wrappable.cc @@ -6,7 +6,7 @@ #include "base/logging.h" #include "native_mate/dictionary.h" -#include "native_mate/object_template_builder.h" +#include "native_mate/object_template_builder_deprecated.h" namespace mate { diff --git a/patches/chromium/.patches b/patches/chromium/.patches index 8b9702a031e..438dcfbcb38 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -17,6 +17,7 @@ web_contents.patch webview_cross_drag.patch disable_user_gesture_requirement_for_beforeunload_dialogs.patch gin_enable_disable_v8_platform.patch +gin_with_namespace.patch blink-worker-enable-csp-in-file-scheme.patch disable-redraw-lock.patch v8_context_snapshot_generator.patch diff --git a/patches/chromium/gin_with_namespace.patch b/patches/chromium/gin_with_namespace.patch new file mode 100644 index 00000000000..9a7101b0f33 --- /dev/null +++ b/patches/chromium/gin_with_namespace.patch @@ -0,0 +1,83 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Cheng Zhao +Date: Thu, 20 Sep 2018 17:47:44 -0700 +Subject: gin_with_namespace.patch + +When using gin with native_mate together we may have C++ confused with +finding the correct ConvertFromV8. We add gin:: namespace explicitly in +those calls to work around the ambiguous compilation error. + +Note that this is only a work around to make it easier to remove +native_mate, and we should remove this patch once native_mate is erased +from Electron. + +diff --git a/gin/arguments.h b/gin/arguments.h +index eaded13e2991..03e1495566d1 100644 +--- a/gin/arguments.h ++++ b/gin/arguments.h +@@ -28,14 +28,14 @@ class GIN_EXPORT Arguments { + v8::Local holder = is_for_property_ + ? info_for_property_->Holder() + : info_for_function_->Holder(); +- return ConvertFromV8(isolate_, holder, out); ++ return gin::ConvertFromV8(isolate_, holder, out); + } + + template + bool GetData(T* out) { + v8::Local data = is_for_property_ ? info_for_property_->Data() + : info_for_function_->Data(); +- return ConvertFromV8(isolate_, data, out); ++ return gin::ConvertFromV8(isolate_, data, out); + } + + template +@@ -45,7 +45,7 @@ class GIN_EXPORT Arguments { + return false; + } + v8::Local val = (*info_for_function_)[next_++]; +- return ConvertFromV8(isolate_, val, out); ++ return gin::ConvertFromV8(isolate_, val, out); + } + + template +@@ -58,7 +58,7 @@ class GIN_EXPORT Arguments { + out->resize(remaining); + for (int i = 0; i < remaining; ++i) { + v8::Local val = (*info_for_function_)[next_++]; +- if (!ConvertFromV8(isolate_, val, &out->at(i))) ++ if (!gin::ConvertFromV8(isolate_, val, &out->at(i))) + return false; + } + return true; +@@ -80,7 +80,7 @@ class GIN_EXPORT Arguments { + template + void Return(T val) { + v8::Local v8_value; +- if (!TryConvertToV8(isolate_, val, &v8_value)) ++ if (!gin::TryConvertToV8(isolate_, val, &v8_value)) + return; + (is_for_property_ ? info_for_property_->GetReturnValue() + : info_for_function_->GetReturnValue()) +diff --git a/gin/converter.h b/gin/converter.h +index 27b4d0acd016..b19209a8534a 100644 +--- a/gin/converter.h ++++ b/gin/converter.h +@@ -250,7 +250,7 @@ std::enable_if_t::value, bool> TryConvertToV8( + v8::Isolate* isolate, + const T& input, + v8::Local* output) { +- return ConvertToV8(isolate, input).ToLocal(output); ++ return gin::ConvertToV8(isolate, input).ToLocal(output); + } + + template +@@ -258,7 +258,7 @@ std::enable_if_t::value, bool> TryConvertToV8( + v8::Isolate* isolate, + const T& input, + v8::Local* output) { +- *output = ConvertToV8(isolate, input); ++ *output = gin::ConvertToV8(isolate, input); + return true; + } + diff --git a/shell/browser/api/atom_api_app.cc b/shell/browser/api/atom_api_app.cc index 617e03216ac..94d4e205a2c 100644 --- a/shell/browser/api/atom_api_app.cc +++ b/shell/browser/api/atom_api_app.cc @@ -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" diff --git a/shell/browser/api/atom_api_auto_updater.cc b/shell/browser/api/atom_api_auto_updater.cc index ae57e286d8d..2b2342c3759 100644 --- a/shell/browser/api/atom_api_auto_updater.cc +++ b/shell/browser/api/atom_api_auto_updater.cc @@ -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" diff --git a/shell/browser/api/atom_api_menu.cc b/shell/browser/api/atom_api_menu.cc index 76fab2c74c4..e9dbd27ba0b 100644 --- a/shell/browser/api/atom_api_menu.cc +++ b/shell/browser/api/atom_api_menu.cc @@ -6,24 +6,22 @@ #include -#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> g_menus; + } // unnamed namespace namespace electron { @@ -224,9 +222,9 @@ void Menu::OnMenuWillShow() { // static void Menu::BuildPrototype(v8::Isolate* isolate, v8::Local 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 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()); diff --git a/shell/browser/api/atom_api_menu.h b/shell/browser/api/atom_api_menu.h index eabd695ea6b..23846d0dce2 100644 --- a/shell/browser/api/atom_api_menu.h +++ b/shell/browser/api/atom_api_menu.h @@ -155,6 +155,20 @@ struct Converter { } }; +// TODO(zcbenz): Remove this after converting Menu to gin::Wrapper. +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + electron::api::Menu** out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local ToV8(v8::Isolate* isolate, + electron::api::Menu* in) { + return mate::ConvertToV8(isolate, in); + } +}; + } // namespace gin #endif // SHELL_BROWSER_API_ATOM_API_MENU_H_ diff --git a/shell/browser/api/atom_api_native_theme.cc b/shell/browser/api/atom_api_native_theme.cc index 195e56f0206..bef30948603 100644 --- a/shell/browser/api/atom_api_native_theme.cc +++ b/shell/browser/api/atom_api_native_theme.cc @@ -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" diff --git a/shell/browser/api/atom_api_notification.cc b/shell/browser/api/atom_api_notification.cc index e1e48f1046d..bc104d71128 100644 --- a/shell/browser/api/atom_api_notification.cc +++ b/shell/browser/api/atom_api_notification.cc @@ -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" diff --git a/shell/browser/api/atom_api_screen.cc b/shell/browser/api/atom_api_screen.cc index 5d51ebd1f62..ad0dccb3c67 100644 --- a/shell/browser/api/atom_api_screen.cc +++ b/shell/browser/api/atom_api_screen.cc @@ -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 Screen::Create(v8::Isolate* isolate) { +v8::Local 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 diff --git a/shell/browser/api/atom_api_screen.h b/shell/browser/api/atom_api_screen.h index ff09a841938..d6bc81bb300 100644 --- a/shell/browser/api/atom_api_screen.h +++ b/shell/browser/api/atom_api_screen.h @@ -7,8 +7,8 @@ #include -#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, public display::DisplayObserver { public: - static v8::Local Create(v8::Isolate* isolate); + static v8::Local Create(gin_helper::ErrorThrower error_thrower); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); diff --git a/shell/browser/api/atom_api_session.cc b/shell/browser/api/atom_api_session.cc index 78eb4bc6287..598462819b0 100644 --- a/shell/browser/api/atom_api_session.cc +++ b/shell/browser/api/atom_api_session.cc @@ -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" diff --git a/shell/browser/api/atom_api_session.h b/shell/browser/api/atom_api_session.h index 1e18ba2733a..815168e100f 100644 --- a/shell/browser/api/atom_api_session.h +++ b/shell/browser/api/atom_api_session.h @@ -121,4 +121,22 @@ class Session : public mate::TrackableObject, } // namespace electron +namespace gin { + +// TODO(zcbenz): Remove this after converting Session to gin::Wrapper. +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + electron::api::Session** out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local ToV8(v8::Isolate* isolate, + electron::api::Session* in) { + return mate::ConvertToV8(isolate, in); + } +}; + +} // namespace gin + #endif // SHELL_BROWSER_API_ATOM_API_SESSION_H_ diff --git a/shell/browser/api/atom_api_system_preferences_mac.mm b/shell/browser/api/atom_api_system_preferences_mac.mm index 643ee54a572..079db554760 100644 --- a/shell/browser/api/atom_api_system_preferences_mac.mm +++ b/shell/browser/api/atom_api_system_preferences_mac.mm @@ -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" diff --git a/shell/browser/api/atom_api_top_level_window.h b/shell/browser/api/atom_api_top_level_window.h index 3958924d7f6..6401fe94553 100644 --- a/shell/browser/api/atom_api_top_level_window.h +++ b/shell/browser/api/atom_api_top_level_window.h @@ -266,4 +266,22 @@ class TopLevelWindow : public mate::TrackableObject, } // namespace electron +namespace gin { + +// TODO(zcbenz): Remove this after converting TopLevelWindow to gin::Wrapper. +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + electron::api::TopLevelWindow** out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local 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_ diff --git a/shell/browser/api/atom_api_url_request_ns.cc b/shell/browser/api/atom_api_url_request_ns.cc index 9dcb987ea96..1779e9c9c3b 100644 --- a/shell/browser/api/atom_api_url_request_ns.cc +++ b/shell/browser/api/atom_api_url_request_ns.cc @@ -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 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) diff --git a/shell/browser/api/atom_api_url_request_ns.h b/shell/browser/api/atom_api_url_request_ns.h index 3b856c29750..6c7e5c11d75 100644 --- a/shell/browser/api/atom_api_url_request_ns.h +++ b/shell/browser/api/atom_api_url_request_ns.h @@ -10,8 +10,9 @@ #include #include +#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, 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, } // namespace electron +namespace gin { + +// TODO(zcbenz): Remove this after converting URLRequestNS to gin::Wrapper. +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + electron::api::URLRequestNS** out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local 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_ diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 98425aaa6cf..013975ca778 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -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" diff --git a/shell/browser/api/event.cc b/shell/browser/api/event.cc index a5622f6f56e..a4917ca1d25 100644 --- a/shell/browser/api/event.cc +++ b/shell/browser/api/event.cc @@ -6,7 +6,7 @@ #include -#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 { diff --git a/shell/browser/api/event_emitter.cc b/shell/browser/api/event_emitter.cc index e304143bdac..c7d97191f47 100644 --- a/shell/browser/api/event_emitter.cc +++ b/shell/browser/api/event_emitter.cc @@ -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" diff --git a/shell/browser/api/trackable_object.h b/shell/browser/api/trackable_object.h index ad99237cdf4..3df918e2de5 100644 --- a/shell/browser/api/trackable_object.h +++ b/shell/browser/api/trackable_object.h @@ -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" diff --git a/shell/common/api/atom_api_asar.cc b/shell/common/api/atom_api_asar.cc index fdd5c9cea9f..8ca6a196aa0 100644 --- a/shell/common/api/atom_api_asar.cc +++ b/shell/common/api/atom_api_asar.cc @@ -7,7 +7,7 @@ #include #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" diff --git a/shell/common/api/atom_api_key_weak_map.h b/shell/common/api/atom_api_key_weak_map.h index ec7c157a98d..37d20aa34f7 100644 --- a/shell/common/api/atom_api_key_weak_map.h +++ b/shell/common/api/atom_api_key_weak_map.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> { static void BuildPrototype(v8::Isolate* isolate, v8::Local 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::Set) .SetMethod("get", &KeyWeakMap::Get) .SetMethod("has", &KeyWeakMap::Has) @@ -60,4 +61,22 @@ class KeyWeakMap : public mate::Wrappable> { } // namespace electron +namespace gin { + +// TODO(zcbenz): Remove this after converting KeyWeakMap to gin::Wrapper. +template +struct Converter*> { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + electron::api::KeyWeakMap** out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local ToV8(v8::Isolate* isolate, + electron::api::KeyWeakMap* in) { + return mate::ConvertToV8(isolate, in); + } +}; + +} // namespace gin + #endif // SHELL_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_ diff --git a/shell/common/api/atom_api_native_image.cc b/shell/common/api/atom_api_native_image.cc index 2f7ee324a7a..3d6d395cbc1 100644 --- a/shell/common/api/atom_api_native_image.cc +++ b/shell/common/api/atom_api_native_image.cc @@ -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" diff --git a/shell/common/api/atom_api_v8_util.cc b/shell/common/api/atom_api_v8_util.cc index 6de686f420d..786d20d7e6b 100644 --- a/shell/common/api/atom_api_v8_util.cc +++ b/shell/common/api/atom_api_v8_util.cc @@ -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> { } // namespace std -namespace mate { +namespace gin { template struct Converter> { @@ -54,7 +58,7 @@ struct Converter> { } }; -} // namespace mate +} // namespace gin namespace { @@ -115,15 +119,18 @@ void Initialize(v8::Local exports, v8::Local unused, v8::Local 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", diff --git a/shell/common/gin_converters/native_mate_handle_converter.h b/shell/common/gin_converters/native_mate_handle_converter.h new file mode 100644 index 00000000000..66f30b745ba --- /dev/null +++ b/shell/common/gin_converters/native_mate_handle_converter.h @@ -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 +struct Converter> { + static v8::Local ToV8(v8::Isolate* isolate, + const mate::Handle& in) { + return mate::ConvertToV8(isolate, in); + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + mate::Handle* out) { + return mate::ConvertFromV8(isolate, val, out); + } +}; + +} // namespace gin + +#endif // SHELL_COMMON_GIN_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_ diff --git a/shell/common/gin_converters/std_converter.h b/shell/common/gin_converters/std_converter.h index 50e6f8a9617..8c1efd273ea 100644 --- a/shell/common/gin_converters/std_converter.h +++ b/shell/common/gin_converters/std_converter.h @@ -63,6 +63,22 @@ struct Converter> { } }; +template <> +struct Converter> { + static v8::Local ToV8(v8::Isolate* isolate, + v8::Local val) { + return val; + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + v8::Local* out) { + if (!val->IsString()) + return false; + *out = v8::Local::Cast(val); + return true; + } +}; + template struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, diff --git a/shell/common/gin_converters/value_converter_gin_adapter.h b/shell/common/gin_converters/value_converter_gin_adapter.h index 04d1e96912d..e0baecd61ef 100644 --- a/shell/common/gin_converters/value_converter_gin_adapter.h +++ b/shell/common/gin_converters/value_converter_gin_adapter.h @@ -25,6 +25,19 @@ struct Converter { } }; +template <> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + base::ListValue* out) { + return mate::ConvertFromV8(isolate, val, out); + } + static v8::Local ToV8(v8::Isolate* isolate, + const base::ListValue& val) { + return mate::ConvertToV8(isolate, val); + } +}; + template <> struct Converter { static bool FromV8(v8::Isolate* isolate, diff --git a/shell/common/gin_helper/dictionary.h b/shell/common/gin_helper/dictionary.h index 8a48a093fd7..cdba33d89e0 100644 --- a/shell/common/gin_helper/dictionary.h +++ b/shell/common/gin_helper/dictionary.h @@ -7,49 +7,11 @@ #include -#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 -struct CallbackTraits { - static v8::Local CreateTemplate(v8::Isolate* isolate, - T callback) { - return CreateFunctionTemplate(isolate, base::BindRepeating(callback)); - } -}; - -// Specialization for base::Callback. -template -struct CallbackTraits> { - static v8::Local CreateTemplate( - v8::Isolate* isolate, - const base::RepeatingCallback& 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 -struct CallbackTraits< - T, - typename std::enable_if::value>::type> { - static v8::Local 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 diff --git a/shell/common/gin_helper/function_template.h b/shell/common/gin_helper/function_template.h index 15288e6add3..67b6f5edf6d 100644 --- a/shell/common/gin_helper/function_template.h +++ b/shell/common/gin_helper/function_template.h @@ -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 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 +struct CallbackTraits { + static v8::Local CreateTemplate(v8::Isolate* isolate, + T callback) { + return gin_helper::CreateFunctionTemplate(isolate, + base::BindRepeating(callback)); + } +}; + +// Specialization for base::Callback. +template +struct CallbackTraits> { + static v8::Local CreateTemplate( + v8::Isolate* isolate, + const base::RepeatingCallback& 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 +struct CallbackTraits< + T, + typename std::enable_if::value>::type> { + static v8::Local 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_ diff --git a/shell/common/gin_helper/object_template_builder.cc b/shell/common/gin_helper/object_template_builder.cc new file mode 100644 index 00000000000..7b21f691fe9 --- /dev/null +++ b/shell/common/gin_helper/object_template_builder.cc @@ -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 templ) + : isolate_(isolate), template_(templ) {} + +ObjectTemplateBuilder::~ObjectTemplateBuilder() = default; + +ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl( + const base::StringPiece& name, + v8::Local val) { + template_->Set(gin::StringToSymbol(isolate_, name), val); + return *this; +} + +ObjectTemplateBuilder& ObjectTemplateBuilder::SetPropertyImpl( + const base::StringPiece& name, + v8::Local getter, + v8::Local setter) { + template_->SetAccessorProperty(gin::StringToSymbol(isolate_, name), getter, + setter); + return *this; +} + +} // namespace gin_helper diff --git a/shell/common/gin_helper/object_template_builder.h b/shell/common/gin_helper/object_template_builder.h new file mode 100644 index 00000000000..5b683464d45 --- /dev/null +++ b/shell/common/gin_helper/object_template_builder.h @@ -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 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 + 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 + ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, + const T& callback) { + return SetImpl(name, CallbackTraits::CreateTemplate(isolate_, callback)); + } + template + ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, + const T& getter) { + return SetPropertyImpl(name, + CallbackTraits::CreateTemplate(isolate_, getter), + v8::Local()); + } + template + ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, + const T& getter, + const U& setter) { + return SetPropertyImpl(name, + CallbackTraits::CreateTemplate(isolate_, getter), + CallbackTraits::CreateTemplate(isolate_, setter)); + } + + private: + ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, + v8::Local val); + ObjectTemplateBuilder& SetPropertyImpl( + const base::StringPiece& name, + v8::Local getter, + v8::Local setter); + + v8::Isolate* isolate_; + + // ObjectTemplateBuilder should only be used on the stack. + v8::Local template_; +}; + +} // namespace gin_helper + +#endif // SHELL_COMMON_GIN_HELPER_OBJECT_TEMPLATE_BUILDER_H_ diff --git a/shell/renderer/api/atom_api_renderer_ipc.cc b/shell/renderer/api/atom_api_renderer_ipc.cc index f212ae34479..4577be16ff2 100644 --- a/shell/renderer/api/atom_api_renderer_ipc.cc +++ b/shell/renderer/api/atom_api_renderer_ipc.cc @@ -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 { +class IPCRenderer : public gin::Wrappable { public: + static gin::WrapperInfo kWrapperInfo; + + static gin::Handle 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 { task_runner_); } - static void BuildPrototype(v8::Isolate* isolate, - v8::Local prototype) { - prototype->SetClassName(mate::StringToV8(isolate, "IPCRenderer")); - mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) + // gin::Wrappable: + gin::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override { + return gin::Wrappable::GetObjectTemplateBuilder(isolate) .SetMethod("send", &IPCRenderer::Send) .SetMethod("sendSync", &IPCRenderer::SendSync) .SetMethod("sendTo", &IPCRenderer::SendTo) @@ -62,27 +66,26 @@ class IPCRenderer : public mate::Wrappable { .SetMethod("invoke", &IPCRenderer::Invoke); } - static mate::Handle 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 Invoke(mate::Arguments* args, + v8::Local Invoke(v8::Isolate* isolate, bool internal, const std::string& channel, const base::Value& arguments) { - electron::util::Promise p(args->isolate()); + electron::util::Promise p(isolate); auto handle = p.GetHandle(); electron_browser_ptr_->get()->Invoke( internal, channel, arguments.Clone(), base::BindOnce([](electron::util::Promise 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 { return result; } - private: void SendMessageSyncOnWorkerThread(base::WaitableEvent* event, base::Value* result, bool internal, @@ -180,6 +182,7 @@ class IPCRenderer : public mate::Wrappable { 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 { electron_browser_ptr_; }; +gin::WrapperInfo IPCRenderer::kWrapperInfo = {gin::kEmbedderNativeGin}; + void Initialize(v8::Local exports, v8::Local unused, v8::Local context, void* priv) { - mate::Dictionary dict(context->GetIsolate(), exports); + gin::Dictionary dict(context->GetIsolate(), exports); dict.Set("ipc", IPCRenderer::Create(context->GetIsolate())); }