diff --git a/filenames.gni b/filenames.gni index f244a471bcaf..fc03b63bb300 100644 --- a/filenames.gni +++ b/filenames.gni @@ -476,7 +476,6 @@ 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", diff --git a/native_mate/BUILD.gn b/native_mate/BUILD.gn index c89ce890000c..26dde6b20efb 100644 --- a/native_mate/BUILD.gn +++ b/native_mate/BUILD.gn @@ -10,6 +10,7 @@ source_set("native_mate") { "//v8:v8_headers", ] public_configs = [ ":native_mate_config" ] + include_dirs = [ ".." ] sources = [ "native_mate/arguments.cc", "native_mate/arguments.h", diff --git a/native_mate/native_mate/constructor.h b/native_mate/native_mate/constructor.h index 8152df152e88..438bbf500467 100644 --- a/native_mate/native_mate/constructor.h +++ b/native_mate/native_mate/constructor.h @@ -11,6 +11,7 @@ #include "base/bind.h" #include "native_mate/function_template.h" +#include "shell/common/gin_helper/function_template.h" namespace mate { @@ -30,7 +31,8 @@ inline WrappableBase* InvokeFactory( Arguments* args, const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; - if (!GetNextArgument(args, 0, true, &a1)) + gin::Arguments gin_args(args->info()); + if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1)) return nullptr; return callback.Run(a1); } @@ -41,8 +43,9 @@ inline WrappableBase* InvokeFactory( const base::Callback& callback) { typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; - if (!GetNextArgument(args, 0, true, &a1) || - !GetNextArgument(args, 0, false, &a2)) + gin::Arguments gin_args(args->info()); + if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a2)) return nullptr; return callback.Run(a1, a2); } @@ -54,9 +57,10 @@ inline WrappableBase* InvokeFactory( typename CallbackParamTraits::LocalType a1; typename CallbackParamTraits::LocalType a2; typename CallbackParamTraits::LocalType a3; - if (!GetNextArgument(args, 0, true, &a1) || - !GetNextArgument(args, 0, false, &a2) || - !GetNextArgument(args, 0, false, &a3)) + gin::Arguments gin_args(args->info()); + if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a2) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a3)) return nullptr; return callback.Run(a1, a2, a3); } @@ -69,10 +73,11 @@ inline WrappableBase* InvokeFactory( typename CallbackParamTraits::LocalType a2; typename CallbackParamTraits::LocalType a3; typename CallbackParamTraits::LocalType a4; - if (!GetNextArgument(args, 0, true, &a1) || - !GetNextArgument(args, 0, false, &a2) || - !GetNextArgument(args, 0, false, &a3) || - !GetNextArgument(args, 0, false, &a4)) + gin::Arguments gin_args(args->info()); + if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a2) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a3) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a4)) return nullptr; return callback.Run(a1, a2, a3, a4); } @@ -86,11 +91,12 @@ inline WrappableBase* InvokeFactory( typename CallbackParamTraits::LocalType a3; typename CallbackParamTraits::LocalType a4; typename CallbackParamTraits::LocalType a5; - if (!GetNextArgument(args, 0, true, &a1) || - !GetNextArgument(args, 0, false, &a2) || - !GetNextArgument(args, 0, false, &a3) || - !GetNextArgument(args, 0, false, &a4) || - !GetNextArgument(args, 0, false, &a5)) + gin::Arguments gin_args(args->info()); + if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a2) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a3) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a4) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a5)) return nullptr; return callback.Run(a1, a2, a3, a4, a5); } @@ -110,12 +116,13 @@ inline WrappableBase* InvokeFactory( typename CallbackParamTraits::LocalType a4; typename CallbackParamTraits::LocalType a5; typename CallbackParamTraits::LocalType a6; - if (!GetNextArgument(args, 0, true, &a1) || - !GetNextArgument(args, 0, false, &a2) || - !GetNextArgument(args, 0, false, &a3) || - !GetNextArgument(args, 0, false, &a4) || - !GetNextArgument(args, 0, false, &a5) || - !GetNextArgument(args, 0, false, &a6)) + gin::Arguments gin_args(args->info()); + if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a2) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a3) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a4) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a5) || + !gin_helper::GetNextArgument(&gin_args, 0, false, &a6)) return nullptr; return callback.Run(a1, a2, a3, a4, a5, a6); } diff --git a/native_mate/native_mate/dictionary.h b/native_mate/native_mate/dictionary.h index 9bfc0d55b067..412acd25a958 100644 --- a/native_mate/native_mate/dictionary.h +++ b/native_mate/native_mate/dictionary.h @@ -115,4 +115,22 @@ struct Converter { } // namespace mate +namespace gin { + +// Keep compatibility with gin. +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const mate::Dictionary& in) { + return mate::ConvertToV8(isolate, in); + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + mate::Dictionary* out) { + return mate::ConvertFromV8(isolate, val, out); + } +}; + +} // namespace gin + #endif // NATIVE_MATE_NATIVE_MATE_DICTIONARY_H_ diff --git a/native_mate/native_mate/function_template.h b/native_mate/native_mate/function_template.h index 47ebfd3d2b0c..babba9506308 100644 --- a/native_mate/native_mate/function_template.h +++ b/native_mate/native_mate/function_template.h @@ -5,11 +5,11 @@ #ifndef NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_ #define NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_ -#include "../shell/common/gin_helper/destroyable.h" -#include "../shell/common/gin_helper/error_thrower.h" #include "base/callback.h" #include "native_mate/arguments.h" #include "native_mate/wrappable_base.h" +#include "shell/common/gin_helper/destroyable.h" +#include "shell/common/gin_helper/error_thrower.h" // =============================== NOTICE =============================== // Do not add code here, native_mate is being removed. Any new code diff --git a/native_mate/native_mate/handle.h b/native_mate/native_mate/handle.h index 793d722234fd..12e411434a7e 100644 --- a/native_mate/native_mate/handle.h +++ b/native_mate/native_mate/handle.h @@ -69,4 +69,22 @@ mate::Handle CreateHandle(v8::Isolate* isolate, T* object) { } // namespace mate +namespace gin { + +// Keep compatibility with gin. +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 // NATIVE_MATE_NATIVE_MATE_HANDLE_H_ diff --git a/native_mate/native_mate/wrappable.cc b/native_mate/native_mate/wrappable.cc index 086751dfc3d3..73f0502bd13a 100644 --- a/native_mate/native_mate/wrappable.cc +++ b/native_mate/native_mate/wrappable.cc @@ -5,6 +5,7 @@ #include "native_mate/wrappable.h" #include "base/logging.h" +#include "gin/arguments.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder_deprecated.h" @@ -37,6 +38,12 @@ v8::MaybeLocal WrappableBase::GetWrapper( return v8::MaybeLocal(); } +void WrappableBase::InitWithArgs(gin::Arguments* args) { + v8::Local holder; + args->GetHolder(&holder); + InitWith(args->isolate(), holder); +} + void WrappableBase::InitWith(v8::Isolate* isolate, v8::Local wrapper) { CHECK(wrapper_.IsEmpty()); diff --git a/native_mate/native_mate/wrappable.h b/native_mate/native_mate/wrappable.h index 94fdff080439..0e4dcdd1a309 100644 --- a/native_mate/native_mate/wrappable.h +++ b/native_mate/native_mate/wrappable.h @@ -97,4 +97,28 @@ struct Converter +struct Converter< + T*, + typename std::enable_if< + std::is_convertible::value>::type> { + static v8::Local ToV8(v8::Isolate* isolate, T* val) { + if (val) + return val->GetWrapper(); + else + return v8::Null(isolate); + } + + static bool FromV8(v8::Isolate* isolate, v8::Local val, T** out) { + *out = static_cast(static_cast( + mate::internal::FromV8Impl(isolate, val))); + return *out != nullptr; + } +}; + +} // namespace gin + #endif // NATIVE_MATE_NATIVE_MATE_WRAPPABLE_H_ diff --git a/native_mate/native_mate/wrappable_base.h b/native_mate/native_mate/wrappable_base.h index 526bb1aea7c8..6df70b671dfe 100644 --- a/native_mate/native_mate/wrappable_base.h +++ b/native_mate/native_mate/wrappable_base.h @@ -6,8 +6,9 @@ #define NATIVE_MATE_NATIVE_MATE_WRAPPABLE_BASE_H_ namespace gin { +class Arguments; struct Destroyable; -} +} // namespace gin namespace mate { @@ -47,6 +48,9 @@ class WrappableBase { // This method should only be called by classes using Constructor. virtual void InitWith(v8::Isolate* isolate, v8::Local wrapper); + // Helper to migrate from native_mate to gin. + void InitWithArgs(gin::Arguments* args); + private: friend struct gin::Destroyable; diff --git a/shell/browser/api/atom_api_browser_view.cc b/shell/browser/api/atom_api_browser_view.cc index 18a122cb08bf..478f95bbd216 100644 --- a/shell/browser/api/atom_api_browser_view.cc +++ b/shell/browser/api/atom_api_browser_view.cc @@ -57,15 +57,9 @@ namespace electron { namespace api { -BrowserView::BrowserView(v8::Isolate* isolate, - v8::Local wrapper, +BrowserView::BrowserView(gin::Arguments* args, const mate::Dictionary& options) { - Init(isolate, wrapper, options); -} - -void BrowserView::Init(v8::Isolate* isolate, - v8::Local wrapper, - const mate::Dictionary& options) { + v8::Isolate* isolate = args->isolate(); mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate); options.Get(options::kWebPreferences, &web_preferences); web_preferences.Set("type", "browserView"); @@ -79,7 +73,7 @@ void BrowserView::Init(v8::Isolate* isolate, view_.reset( NativeBrowserView::Create(api_web_contents_->managed_web_contents())); - InitWith(isolate, wrapper); + InitWithArgs(args); } BrowserView::~BrowserView() { @@ -96,16 +90,17 @@ void BrowserView::WebContentsDestroyed() { } // static -mate::WrappableBase* BrowserView::New(mate::Arguments* args) { +mate::WrappableBase* BrowserView::New(gin_helper::ErrorThrower thrower, + gin::Arguments* args) { if (!Browser::Get()->is_ready()) { - args->ThrowError("Cannot create BrowserView before app is ready"); + thrower.ThrowError("Cannot create BrowserView before app is ready"); return nullptr; } mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate()); args->GetNext(&options); - return new BrowserView(args->isolate(), args->GetThis(), options); + return new BrowserView(args, options); } int32_t BrowserView::ID() const { diff --git a/shell/browser/api/atom_api_browser_view.h b/shell/browser/api/atom_api_browser_view.h index 5d02406608ab..59ce541f4a74 100644 --- a/shell/browser/api/atom_api_browser_view.h +++ b/shell/browser/api/atom_api_browser_view.h @@ -12,6 +12,7 @@ #include "native_mate/handle.h" #include "shell/browser/api/trackable_object.h" #include "shell/browser/native_browser_view.h" +#include "shell/common/gin_helper/error_thrower.h" namespace gfx { class Rect; @@ -33,7 +34,8 @@ class WebContents; class BrowserView : public mate::TrackableObject, public content::WebContentsObserver { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin_helper::ErrorThrower thrower, + gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -44,19 +46,13 @@ class BrowserView : public mate::TrackableObject, int32_t ID() const; protected: - BrowserView(v8::Isolate* isolate, - v8::Local wrapper, - const mate::Dictionary& options); + BrowserView(gin::Arguments* args, const mate::Dictionary& options); ~BrowserView() override; // content::WebContentsObserver: void WebContentsDestroyed() override; private: - void Init(v8::Isolate* isolate, - v8::Local wrapper, - const mate::Dictionary& options); - void SetAutoResize(AutoResizeFlags flags); void SetBounds(const gfx::Rect& bounds); gfx::Rect GetBounds(); diff --git a/shell/browser/api/atom_api_browser_window.cc b/shell/browser/api/atom_api_browser_window.cc index 172830252167..4e85062dc971 100644 --- a/shell/browser/api/atom_api_browser_window.cc +++ b/shell/browser/api/atom_api_browser_window.cc @@ -29,13 +29,13 @@ namespace electron { namespace api { -BrowserWindow::BrowserWindow(v8::Isolate* isolate, - v8::Local wrapper, +BrowserWindow::BrowserWindow(gin::Arguments* args, const mate::Dictionary& options) - : TopLevelWindow(isolate, options), weak_factory_(this) { + : TopLevelWindow(args->isolate(), options), weak_factory_(this) { mate::Handle web_contents; // Use options.webPreferences in WebContents. + v8::Isolate* isolate = args->isolate(); mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate); options.Get(options::kWebPreferences, &web_preferences); @@ -92,7 +92,7 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate, if (host) host->GetWidget()->AddInputEventObserver(this); - InitWith(isolate, wrapper); + InitWithArgs(args); #if defined(OS_MACOSX) OverrideNSWindowContentView(web_contents->managed_web_contents()); @@ -444,9 +444,10 @@ void BrowserWindow::OnWindowHide() { } // static -mate::WrappableBase* BrowserWindow::New(mate::Arguments* args) { +mate::WrappableBase* BrowserWindow::New(gin_helper::ErrorThrower thrower, + gin::Arguments* args) { if (!Browser::Get()->is_ready()) { - args->ThrowError("Cannot create BrowserWindow before app is ready"); + thrower.ThrowError("Cannot create BrowserWindow before app is ready"); return nullptr; } @@ -460,7 +461,7 @@ mate::WrappableBase* BrowserWindow::New(mate::Arguments* args) { options = mate::Dictionary::CreateEmpty(args->isolate()); } - return new BrowserWindow(args->isolate(), args->GetThis(), options); + return new BrowserWindow(args, options); } // static diff --git a/shell/browser/api/atom_api_browser_window.h b/shell/browser/api/atom_api_browser_window.h index a7f64aac7151..c0a9900b5973 100644 --- a/shell/browser/api/atom_api_browser_window.h +++ b/shell/browser/api/atom_api_browser_window.h @@ -12,6 +12,7 @@ #include "base/cancelable_callback.h" #include "shell/browser/api/atom_api_top_level_window.h" #include "shell/browser/api/atom_api_web_contents.h" +#include "shell/common/gin_helper/error_thrower.h" namespace electron { @@ -22,7 +23,8 @@ class BrowserWindow : public TopLevelWindow, public content::WebContentsObserver, public ExtendedWebContentsObserver { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin_helper::ErrorThrower thrower, + gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -36,9 +38,7 @@ class BrowserWindow : public TopLevelWindow, } protected: - BrowserWindow(v8::Isolate* isolate, - v8::Local wrapper, - const mate::Dictionary& options); + BrowserWindow(gin::Arguments* args, const mate::Dictionary& options); ~BrowserWindow() override; // content::RenderWidgetHost::InputEventObserver: diff --git a/shell/browser/api/atom_api_menu.cc b/shell/browser/api/atom_api_menu.cc index e9dbd27ba0b4..d28439eabcaa 100644 --- a/shell/browser/api/atom_api_menu.cc +++ b/shell/browser/api/atom_api_menu.cc @@ -28,9 +28,8 @@ namespace electron { namespace api { -Menu::Menu(v8::Isolate* isolate, v8::Local wrapper) - : model_(new AtomMenuModel(this)) { - InitWith(isolate, wrapper); +Menu::Menu(gin::Arguments* args) : model_(new AtomMenuModel(this)) { + InitWithArgs(args); model_->AddObserver(this); } diff --git a/shell/browser/api/atom_api_menu.h b/shell/browser/api/atom_api_menu.h index 23846d0dce22..b67eb925f19b 100644 --- a/shell/browser/api/atom_api_menu.h +++ b/shell/browser/api/atom_api_menu.h @@ -9,6 +9,7 @@ #include #include "base/callback.h" +#include "gin/arguments.h" #include "shell/browser/api/atom_api_top_level_window.h" #include "shell/browser/api/trackable_object.h" #include "shell/browser/ui/atom_menu_model.h" @@ -21,7 +22,7 @@ class Menu : public mate::TrackableObject, public AtomMenuModel::Delegate, public AtomMenuModel::Observer { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -37,7 +38,7 @@ class Menu : public mate::TrackableObject, AtomMenuModel* model() const { return model_.get(); } protected: - Menu(v8::Isolate* isolate, v8::Local wrapper); + explicit Menu(gin::Arguments* args); ~Menu() override; // mate::Wrappable: @@ -144,31 +145,4 @@ struct Converter { } // namespace mate -namespace gin { - -template <> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - electron::AtomMenuModel** out) { - return mate::ConvertFromV8(isolate, val, out); - } -}; - -// 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_menu_mac.h b/shell/browser/api/atom_api_menu_mac.h index a2885c4ef9b5..db7d9f4d2784 100644 --- a/shell/browser/api/atom_api_menu_mac.h +++ b/shell/browser/api/atom_api_menu_mac.h @@ -20,7 +20,7 @@ namespace api { class MenuMac : public Menu { protected: - MenuMac(v8::Isolate* isolate, v8::Local wrapper); + explicit MenuMac(gin::Arguments* args); ~MenuMac() override; void PopupAt(TopLevelWindow* window, diff --git a/shell/browser/api/atom_api_menu_mac.mm b/shell/browser/api/atom_api_menu_mac.mm index 3119f9453dd9..246918ab22dc 100644 --- a/shell/browser/api/atom_api_menu_mac.mm +++ b/shell/browser/api/atom_api_menu_mac.mm @@ -30,8 +30,7 @@ namespace electron { namespace api { -MenuMac::MenuMac(v8::Isolate* isolate, v8::Local wrapper) - : Menu(isolate, wrapper), weak_factory_(this) {} +MenuMac::MenuMac(gin::Arguments* args) : Menu(args), weak_factory_(this) {} MenuMac::~MenuMac() = default; @@ -173,8 +172,8 @@ void Menu::SendActionToFirstResponder(const std::string& action) { } // static -mate::WrappableBase* Menu::New(mate::Arguments* args) { - return new MenuMac(args->isolate(), args->GetThis()); +mate::WrappableBase* Menu::New(gin::Arguments* args) { + return new MenuMac(args); } } // namespace api diff --git a/shell/browser/api/atom_api_menu_views.cc b/shell/browser/api/atom_api_menu_views.cc index f93160dc47cc..fa51699dbd98 100644 --- a/shell/browser/api/atom_api_menu_views.cc +++ b/shell/browser/api/atom_api_menu_views.cc @@ -16,8 +16,7 @@ namespace electron { namespace api { -MenuViews::MenuViews(v8::Isolate* isolate, v8::Local wrapper) - : Menu(isolate, wrapper), weak_factory_(this) {} +MenuViews::MenuViews(gin::Arguments* args) : Menu(args), weak_factory_(this) {} MenuViews::~MenuViews() = default; @@ -75,8 +74,8 @@ void MenuViews::OnClosed(int32_t window_id, base::Closure callback) { } // static -mate::WrappableBase* Menu::New(mate::Arguments* args) { - return new MenuViews(args->isolate(), args->GetThis()); +mate::WrappableBase* Menu::New(gin::Arguments* args) { + return new MenuViews(args); } } // namespace api diff --git a/shell/browser/api/atom_api_menu_views.h b/shell/browser/api/atom_api_menu_views.h index 30744e2d8a41..f2e4c28103f5 100644 --- a/shell/browser/api/atom_api_menu_views.h +++ b/shell/browser/api/atom_api_menu_views.h @@ -19,7 +19,7 @@ namespace api { class MenuViews : public Menu { public: - MenuViews(v8::Isolate* isolate, v8::Local wrapper); + explicit MenuViews(gin::Arguments* args); ~MenuViews() override; protected: diff --git a/shell/browser/api/atom_api_native_theme.h b/shell/browser/api/atom_api_native_theme.h index b5b41eb66d74..c5e291efefb9 100644 --- a/shell/browser/api/atom_api_native_theme.h +++ b/shell/browser/api/atom_api_native_theme.h @@ -60,20 +60,6 @@ struct Converter { ui::NativeTheme::ThemeSource* out); }; -// TODO(zcbenz): Remove this after converting NativeTheme to gin::Wrapper. -template <> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - electron::api::NativeTheme** out) { - return mate::ConvertFromV8(isolate, val, out); - } - static v8::Local ToV8(v8::Isolate* isolate, - electron::api::NativeTheme* in) { - return mate::ConvertToV8(isolate, in); - } -}; - } // namespace gin #endif // SHELL_BROWSER_API_ATOM_API_NATIVE_THEME_H_ diff --git a/shell/browser/api/atom_api_notification.cc b/shell/browser/api/atom_api_notification.cc index 8a2f98943b7b..769f6f95cb9e 100644 --- a/shell/browser/api/atom_api_notification.cc +++ b/shell/browser/api/atom_api_notification.cc @@ -49,9 +49,8 @@ namespace electron { namespace api { -Notification::Notification(v8::Local wrapper, - gin::Arguments* args) { - InitWith(args->isolate(), wrapper); +Notification::Notification(gin::Arguments* args) { + InitWithArgs(args); presenter_ = static_cast(AtomBrowserClient::Get()) ->GetNotificationPresenter(); @@ -82,13 +81,13 @@ Notification::~Notification() { } // static -mate::WrappableBase* Notification::New(mate::Arguments* args) { +mate::WrappableBase* Notification::New(gin_helper::ErrorThrower thrower, + gin::Arguments* args) { if (!Browser::Get()->is_ready()) { - args->ThrowError("Cannot create Notification before app is ready"); + thrower.ThrowError("Cannot create Notification before app is ready"); return nullptr; } - gin::Arguments gin_args(args->info()); - return new Notification(args->GetThis(), &gin_args); + return new Notification(args); } // Getters diff --git a/shell/browser/api/atom_api_notification.h b/shell/browser/api/atom_api_notification.h index 9aa84f5b29cc..23f5a7b45875 100644 --- a/shell/browser/api/atom_api_notification.h +++ b/shell/browser/api/atom_api_notification.h @@ -14,6 +14,7 @@ #include "shell/browser/notifications/notification.h" #include "shell/browser/notifications/notification_delegate.h" #include "shell/browser/notifications/notification_presenter.h" +#include "shell/common/gin_helper/error_thrower.h" #include "ui/gfx/image/image.h" namespace gin { @@ -27,7 +28,8 @@ namespace api { class Notification : public mate::TrackableObject, public NotificationDelegate { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin_helper::ErrorThrower thrower, + gin::Arguments* args); static bool IsSupported(); static void BuildPrototype(v8::Isolate* isolate, @@ -42,7 +44,7 @@ class Notification : public mate::TrackableObject, void NotificationClosed() override; protected: - Notification(v8::Local wrapper, gin::Arguments* args); + explicit Notification(gin::Arguments* args); ~Notification() override; void Show(); @@ -101,22 +103,4 @@ class Notification : public mate::TrackableObject, } // namespace electron -namespace gin { - -// TODO(zcbenz): Remove this after converting Notification to gin::Wrapper. -template <> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - electron::api::Notification** out) { - return mate::ConvertFromV8(isolate, val, out); - } - static v8::Local ToV8(v8::Isolate* isolate, - electron::api::Notification* in) { - return mate::ConvertToV8(isolate, in); - } -}; - -} // namespace gin - #endif // SHELL_BROWSER_API_ATOM_API_NOTIFICATION_H_ diff --git a/shell/browser/api/atom_api_protocol_ns.cc b/shell/browser/api/atom_api_protocol_ns.cc index 7778303454b7..83963c24d870 100644 --- a/shell/browser/api/atom_api_protocol_ns.cc +++ b/shell/browser/api/atom_api_protocol_ns.cc @@ -13,8 +13,10 @@ #include "shell/browser/atom_browser_context.h" #include "shell/browser/browser.h" #include "shell/common/deprecate_util.h" -#include "shell/common/native_mate_converters/net_converter.h" -#include "shell/common/native_mate_converters/once_callback.h" +#include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_converters/net_converter.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/options_switches.h" #include "shell/common/promise_util.h" @@ -39,19 +41,19 @@ struct CustomScheme { } // namespace -namespace mate { +namespace gin { template <> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, CustomScheme* out) { - mate::Dictionary dict; + gin::Dictionary dict(isolate); if (!ConvertFromV8(isolate, val, &dict)) return false; if (!dict.Get("scheme", &(out->scheme))) return false; - mate::Dictionary opt; + gin::Dictionary opt(isolate); // options are optional. Default values specified in SchemeOptions are used if (dict.Get("privileges", &opt)) { opt.Get("standard", &(out->options.standard)); @@ -66,7 +68,7 @@ struct Converter { } }; -} // namespace mate +} // namespace gin namespace electron { namespace api { @@ -75,11 +77,11 @@ std::vector GetStandardSchemes() { return g_standard_schemes; } -void RegisterSchemesAsPrivileged(v8::Local val, - mate::Arguments* args) { +void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower, + v8::Local val) { std::vector custom_schemes; - if (!mate::ConvertFromV8(args->isolate(), val, &custom_schemes)) { - args->ThrowError("Argument must be an array of custom schemes."); + if (!gin::ConvertFromV8(thrower.isolate(), val, &custom_schemes)) { + thrower.ThrowError("Argument must be an array of custom schemes."); return; } @@ -181,7 +183,7 @@ ProtocolError ProtocolNS::RegisterProtocol(ProtocolType type, } void ProtocolNS::UnregisterProtocol(const std::string& scheme, - mate::Arguments* args) { + gin::Arguments* args) { const bool removed = handlers_.erase(scheme) != 0; const auto error = removed ? ProtocolError::OK : ProtocolError::NOT_REGISTERED; @@ -201,7 +203,7 @@ ProtocolError ProtocolNS::InterceptProtocol(ProtocolType type, } void ProtocolNS::UninterceptProtocol(const std::string& scheme, - mate::Arguments* args) { + gin::Arguments* args) { const bool removed = intercept_handlers_.erase(scheme) != 0; const auto error = removed ? ProtocolError::OK : ProtocolError::NOT_INTERCEPTED; @@ -213,7 +215,7 @@ bool ProtocolNS::IsProtocolIntercepted(const std::string& scheme) { } v8::Local ProtocolNS::IsProtocolHandled(const std::string& scheme, - mate::Arguments* args) { + gin::Arguments* args) { node::Environment* env = node::Environment::GetCurrent(args->isolate()); EmitDeprecationWarning( env, @@ -234,7 +236,7 @@ v8::Local ProtocolNS::IsProtocolHandled(const std::string& scheme, base::Contains(kBuiltinSchemes, scheme)); } -void ProtocolNS::HandleOptionalCallback(mate::Arguments* args, +void ProtocolNS::HandleOptionalCallback(gin::Arguments* args, ProtocolError error) { CompletionCallback callback; if (args->GetNext(&callback)) { @@ -247,22 +249,22 @@ void ProtocolNS::HandleOptionalCallback(mate::Arguments* args, callback.Run(v8::Null(args->isolate())); else callback.Run(v8::Exception::Error( - mate::StringToV8(isolate(), ErrorCodeToString(error)))); + gin::StringToV8(isolate(), ErrorCodeToString(error)))); } } // static -mate::Handle ProtocolNS::Create( +gin::Handle ProtocolNS::Create( v8::Isolate* isolate, AtomBrowserContext* browser_context) { - return mate::CreateHandle(isolate, new ProtocolNS(isolate, browser_context)); + return gin::CreateHandle(isolate, new ProtocolNS(isolate, browser_context)); } // static void ProtocolNS::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { - prototype->SetClassName(mate::StringToV8(isolate, "Protocol")); - mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) + prototype->SetClassName(gin::StringToV8(isolate, "Protocol")); + gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) .SetMethod("registerStringProtocol", &ProtocolNS::RegisterProtocolFor) .SetMethod("registerBufferProtocol", @@ -299,16 +301,16 @@ void ProtocolNS::BuildPrototype(v8::Isolate* isolate, namespace { -void RegisterSchemesAsPrivileged(v8::Local val, - mate::Arguments* args) { +void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower, + v8::Local val) { if (electron::Browser::Get()->is_ready()) { - args->ThrowError( + thrower.ThrowError( "protocol.registerSchemesAsPrivileged should be called before " "app is ready"); return; } - electron::api::RegisterSchemesAsPrivileged(val, args); + electron::api::RegisterSchemesAsPrivileged(thrower, val); } void Initialize(v8::Local exports, @@ -316,7 +318,7 @@ void Initialize(v8::Local exports, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); - mate::Dictionary dict(isolate, exports); + gin_helper::Dictionary dict(isolate, exports); dict.SetMethod("registerSchemesAsPrivileged", &RegisterSchemesAsPrivileged); dict.SetMethod("getStandardSchemes", &electron::api::GetStandardSchemes); } diff --git a/shell/browser/api/atom_api_protocol_ns.h b/shell/browser/api/atom_api_protocol_ns.h index 44fa89ea3af8..b8cd623c5739 100644 --- a/shell/browser/api/atom_api_protocol_ns.h +++ b/shell/browser/api/atom_api_protocol_ns.h @@ -9,10 +9,10 @@ #include #include "content/public/browser/content_browser_client.h" -#include "native_mate/dictionary.h" -#include "native_mate/handle.h" +#include "gin/handle.h" #include "shell/browser/api/trackable_object.h" #include "shell/browser/net/atom_url_loader_factory.h" +#include "shell/common/gin_helper/dictionary.h" namespace electron { @@ -22,8 +22,8 @@ namespace api { std::vector GetStandardSchemes(); -void RegisterSchemesAsPrivileged(v8::Local val, - mate::Arguments* args); +void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower, + v8::Local val); // Possible errors. enum class ProtocolError { @@ -37,8 +37,8 @@ enum class ProtocolError { // Protocol implementation based on network services. class ProtocolNS : public mate::TrackableObject { public: - static mate::Handle Create(v8::Isolate* isolate, - AtomBrowserContext* browser_context); + static gin::Handle Create(v8::Isolate* isolate, + AtomBrowserContext* browser_context); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -61,35 +61,35 @@ class ProtocolNS : public mate::TrackableObject { ProtocolError RegisterProtocol(ProtocolType type, const std::string& scheme, const ProtocolHandler& handler); - void UnregisterProtocol(const std::string& scheme, mate::Arguments* args); + void UnregisterProtocol(const std::string& scheme, gin::Arguments* args); bool IsProtocolRegistered(const std::string& scheme); ProtocolError InterceptProtocol(ProtocolType type, const std::string& scheme, const ProtocolHandler& handler); - void UninterceptProtocol(const std::string& scheme, mate::Arguments* args); + void UninterceptProtocol(const std::string& scheme, gin::Arguments* args); bool IsProtocolIntercepted(const std::string& scheme); // Old async version of IsProtocolRegistered. v8::Local IsProtocolHandled(const std::string& scheme, - mate::Arguments* args); + gin::Arguments* args); // Helper for converting old registration APIs to new RegisterProtocol API. template void RegisterProtocolFor(const std::string& scheme, const ProtocolHandler& handler, - mate::Arguments* args) { + gin::Arguments* args) { HandleOptionalCallback(args, RegisterProtocol(type, scheme, handler)); } template void InterceptProtocolFor(const std::string& scheme, const ProtocolHandler& handler, - mate::Arguments* args) { + gin::Arguments* args) { HandleOptionalCallback(args, InterceptProtocol(type, scheme, handler)); } // Be compatible with old interface, which accepts optional callback. - void HandleOptionalCallback(mate::Arguments* args, ProtocolError error); + void HandleOptionalCallback(gin::Arguments* args, ProtocolError error); HandlersMap handlers_; HandlersMap intercept_handlers_; diff --git a/shell/browser/api/atom_api_session.h b/shell/browser/api/atom_api_session.h index 815168e100fe..1e18ba2733a9 100644 --- a/shell/browser/api/atom_api_session.h +++ b/shell/browser/api/atom_api_session.h @@ -121,22 +121,4 @@ 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_top_level_window.cc b/shell/browser/api/atom_api_top_level_window.cc index f2df07158cb9..9db8efcf9980 100644 --- a/shell/browser/api/atom_api_top_level_window.cc +++ b/shell/browser/api/atom_api_top_level_window.cc @@ -108,11 +108,10 @@ TopLevelWindow::TopLevelWindow(v8::Isolate* isolate, #endif } -TopLevelWindow::TopLevelWindow(v8::Isolate* isolate, - v8::Local wrapper, +TopLevelWindow::TopLevelWindow(gin::Arguments* args, const mate::Dictionary& options) - : TopLevelWindow(isolate, options) { - InitWith(isolate, wrapper); + : TopLevelWindow(args->isolate(), options) { + InitWithArgs(args); // Init window after everything has been setup. window()->InitFromOptions(options); } @@ -1057,11 +1056,11 @@ void TopLevelWindow::RemoveFromParentChildWindows() { } // static -mate::WrappableBase* TopLevelWindow::New(mate::Arguments* args) { +mate::WrappableBase* TopLevelWindow::New(gin::Arguments* args) { mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate()); args->GetNext(&options); - return new TopLevelWindow(args->isolate(), args->GetThis(), options); + return new TopLevelWindow(args, options); } // static diff --git a/shell/browser/api/atom_api_top_level_window.h b/shell/browser/api/atom_api_top_level_window.h index 6401fe94553a..b4429fad00dd 100644 --- a/shell/browser/api/atom_api_top_level_window.h +++ b/shell/browser/api/atom_api_top_level_window.h @@ -28,7 +28,7 @@ class View; class TopLevelWindow : public mate::TrackableObject, public NativeWindowObserver { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -43,9 +43,7 @@ class TopLevelWindow : public mate::TrackableObject, // Common constructor. TopLevelWindow(v8::Isolate* isolate, const mate::Dictionary& options); // Creating independent TopLevelWindow instance. - TopLevelWindow(v8::Isolate* isolate, - v8::Local wrapper, - const mate::Dictionary& options); + TopLevelWindow(gin::Arguments* args, const mate::Dictionary& options); ~TopLevelWindow() override; // TrackableObject: @@ -266,22 +264,4 @@ 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_tray.cc b/shell/browser/api/atom_api_tray.cc index b0f800bd0eea..06653239329f 100644 --- a/shell/browser/api/atom_api_tray.cc +++ b/shell/browser/api/atom_api_tray.cc @@ -55,26 +55,25 @@ namespace electron { namespace api { -Tray::Tray(v8::Isolate* isolate, - v8::Local wrapper, - mate::Handle image) +Tray::Tray(mate::Handle image, gin::Arguments* args) : tray_icon_(TrayIcon::Create()) { - SetImage(isolate, image); + SetImage(args->isolate(), image); tray_icon_->AddObserver(this); - InitWith(isolate, wrapper); + InitWithArgs(args); } Tray::~Tray() = default; // static -mate::WrappableBase* Tray::New(mate::Handle image, - mate::Arguments* args) { +mate::WrappableBase* Tray::New(gin_helper::ErrorThrower thrower, + mate::Handle image, + gin::Arguments* args) { if (!Browser::Get()->is_ready()) { - args->ThrowError("Cannot create Tray before app is ready"); + thrower.ThrowError("Cannot create Tray before app is ready"); return nullptr; } - return new Tray(args->isolate(), args->GetThis(), image); + return new Tray(image, args); } void Tray::OnClicked(const gfx::Rect& bounds, diff --git a/shell/browser/api/atom_api_tray.h b/shell/browser/api/atom_api_tray.h index 719e265c7de4..a0a61ec47499 100644 --- a/shell/browser/api/atom_api_tray.h +++ b/shell/browser/api/atom_api_tray.h @@ -13,6 +13,7 @@ #include "shell/browser/api/trackable_object.h" #include "shell/browser/ui/tray_icon.h" #include "shell/browser/ui/tray_icon_observer.h" +#include "shell/common/gin_helper/error_thrower.h" namespace gfx { class Image; @@ -34,16 +35,15 @@ class NativeImage; class Tray : public mate::TrackableObject, public TrayIconObserver { public: - static mate::WrappableBase* New(mate::Handle image, - mate::Arguments* args); + static mate::WrappableBase* New(gin_helper::ErrorThrower thrower, + mate::Handle image, + gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); protected: - Tray(v8::Isolate* isolate, - v8::Local wrapper, - mate::Handle image); + Tray(mate::Handle image, gin::Arguments* args); ~Tray() override; // TrayIconObserver: diff --git a/shell/browser/api/atom_api_url_request_ns.cc b/shell/browser/api/atom_api_url_request_ns.cc index 2d260e79e128..523db8c9cd8a 100644 --- a/shell/browser/api/atom_api_url_request_ns.cc +++ b/shell/browser/api/atom_api_url_request_ns.cc @@ -6,21 +6,22 @@ #include +#include "gin/handle.h" #include "mojo/public/cpp/bindings/receiver_set.h" #include "mojo/public/cpp/system/string_data_source.h" -#include "native_mate/dictionary.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_converters/gurl_converter.h" +#include "shell/common/gin_converters/net_converter.h" +#include "shell/common/gin_helper/dictionary.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" #include "shell/common/node_includes.h" -namespace mate { +namespace gin { template <> struct Converter { @@ -42,7 +43,7 @@ struct Converter { } }; -} // namespace mate +} // namespace gin namespace electron { @@ -165,9 +166,9 @@ class ChunkedDataPipeGetter : public UploadDataPipeGetter, mojo::ReceiverSet receiver_set_; }; -URLRequestNS::URLRequestNS(mate::Arguments* args) : weak_factory_(this) { +URLRequestNS::URLRequestNS(gin::Arguments* args) : weak_factory_(this) { request_ = std::make_unique(); - mate::Dictionary dict; + gin_helper::Dictionary dict; if (args->GetNext(&dict)) { dict.Get("method", &request_->method); dict.Get("url", &request_->url); @@ -186,7 +187,7 @@ URLRequestNS::URLRequestNS(mate::Arguments* args) : weak_factory_(this) { url_loader_factory_ = session->browser_context()->GetURLLoaderFactory(); - InitWith(args->isolate(), args->GetThis()); + InitWithArgs(args); } URLRequestNS::~URLRequestNS() = default; @@ -518,7 +519,7 @@ void URLRequestNS::EmitEvent(EventType type, Args... args) { } // static -mate::WrappableBase* URLRequestNS::New(mate::Arguments* args) { +mate::WrappableBase* URLRequestNS::New(gin::Arguments* args) { return new URLRequestNS(args); } diff --git a/shell/browser/api/atom_api_url_request_ns.h b/shell/browser/api/atom_api_url_request_ns.h index da02b3a6ce84..54c3107de24d 100644 --- a/shell/browser/api/atom_api_url_request_ns.h +++ b/shell/browser/api/atom_api_url_request_ns.h @@ -28,13 +28,13 @@ class UploadDataPipeGetter; class URLRequestNS : public mate::EventEmitter, public network::SimpleURLLoaderStreamConsumer { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); protected: - explicit URLRequestNS(mate::Arguments* args); + explicit URLRequestNS(gin::Arguments* args); ~URLRequestNS() override; bool NotStarted() const; @@ -141,22 +141,4 @@ 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_view.cc b/shell/browser/api/atom_api_view.cc index f8778702fb98..78d5938f6f84 100644 --- a/shell/browser/api/atom_api_view.cc +++ b/shell/browser/api/atom_api_view.cc @@ -42,9 +42,9 @@ void View::AddChildViewAt(mate::Handle child, size_t index) { #endif // static -mate::WrappableBase* View::New(mate::Arguments* args) { +mate::WrappableBase* View::New(gin::Arguments* args) { auto* view = new View(); - view->InitWith(args->isolate(), args->GetThis()); + view->InitWithArgs(args); return view; } diff --git a/shell/browser/api/atom_api_view.h b/shell/browser/api/atom_api_view.h index a5424758620b..b0f4cd4f77eb 100644 --- a/shell/browser/api/atom_api_view.h +++ b/shell/browser/api/atom_api_view.h @@ -19,7 +19,7 @@ namespace api { class View : public mate::TrackableObject { public: - static mate::WrappableBase* New(mate::Arguments* args); + static mate::WrappableBase* New(gin::Arguments* args); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index db25c135cbac..87dd250bffcc 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -575,22 +575,4 @@ class WebContents : public mate::TrackableObject, } // namespace electron -namespace gin { - -// TODO(zcbenz): Remove this after converting WebContents to gin::Wrapper. -template <> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - electron::api::WebContents** out) { - return mate::ConvertFromV8(isolate, val, out); - } - static v8::Local ToV8(v8::Isolate* isolate, - electron::api::WebContents* in) { - return mate::ConvertToV8(isolate, in); - } -}; - -} // namespace gin - #endif // SHELL_BROWSER_API_ATOM_API_WEB_CONTENTS_H_ diff --git a/shell/browser/api/atom_api_web_contents_view.cc b/shell/browser/api/atom_api_web_contents_view.cc index 9307db55c099..28e84b579581 100644 --- a/shell/browser/api/atom_api_web_contents_view.cc +++ b/shell/browser/api/atom_api_web_contents_view.cc @@ -80,7 +80,7 @@ void WebContentsView::WebContentsDestroyed() { // static mate::WrappableBase* WebContentsView::New( - mate::Arguments* args, + gin::Arguments* args, mate::Handle web_contents) { // Currently we only support InspectableWebContents, e.g. the WebContents // created by users directly. To support devToolsWebContents we need to create @@ -101,7 +101,7 @@ mate::WrappableBase* WebContentsView::New( // Constructor call. auto* view = new WebContentsView(args->isolate(), web_contents, web_contents->managed_web_contents()); - view->InitWith(args->isolate(), args->GetThis()); + view->InitWithArgs(args); return view; } diff --git a/shell/browser/api/atom_api_web_contents_view.h b/shell/browser/api/atom_api_web_contents_view.h index f6a2bd81694e..6502cb7dd601 100644 --- a/shell/browser/api/atom_api_web_contents_view.h +++ b/shell/browser/api/atom_api_web_contents_view.h @@ -19,7 +19,7 @@ class WebContents; class WebContentsView : public View, public content::WebContentsObserver { public: - static mate::WrappableBase* New(mate::Arguments* args, + static mate::WrappableBase* New(gin::Arguments* args, mate::Handle web_contents); static void BuildPrototype(v8::Isolate* isolate, diff --git a/shell/browser/api/atom_api_web_request_ns.h b/shell/browser/api/atom_api_web_request_ns.h index c7bc8dc53f11..d4d3be8a3af5 100644 --- a/shell/browser/api/atom_api_web_request_ns.h +++ b/shell/browser/api/atom_api_web_request_ns.h @@ -13,8 +13,6 @@ #include "gin/arguments.h" #include "gin/handle.h" #include "gin/wrappable.h" -#include "native_mate/dictionary.h" -#include "native_mate/handle.h" #include "shell/browser/net/proxying_url_loader_factory.h" namespace content { diff --git a/shell/browser/net/atom_url_loader_factory.cc b/shell/browser/net/atom_url_loader_factory.cc index a864270044a8..7898c8ea5b55 100644 --- a/shell/browser/net/atom_url_loader_factory.cc +++ b/shell/browser/net/atom_url_loader_factory.cc @@ -23,16 +23,16 @@ #include "shell/browser/net/node_stream_loader.h" #include "shell/browser/net/url_pipe_loader.h" #include "shell/common/atom_constants.h" -#include "shell/common/native_mate_converters/file_path_converter.h" -#include "shell/common/native_mate_converters/gurl_converter.h" -#include "shell/common/native_mate_converters/net_converter.h" -#include "shell/common/native_mate_converters/value_converter.h" +#include "shell/common/gin_converters/file_path_converter.h" +#include "shell/common/gin_converters/gurl_converter.h" +#include "shell/common/gin_converters/net_converter.h" +#include "shell/common/gin_converters/value_converter_gin_adapter.h" #include "shell/common/node_includes.h" using content::BrowserThread; -namespace mate { +namespace gin { template <> struct Converter { @@ -58,7 +58,7 @@ struct Converter { } }; -} // namespace mate +} // namespace gin namespace electron { @@ -77,17 +77,18 @@ bool ResponseMustBeObject(ProtocolType type) { } // Helper to convert value to Dictionary. -mate::Dictionary ToDict(v8::Isolate* isolate, v8::Local value) { +gin::Dictionary ToDict(v8::Isolate* isolate, v8::Local value) { if (!value->IsFunction() && value->IsObject()) - return mate::Dictionary( + return gin::Dictionary( isolate, value->ToObject(isolate->GetCurrentContext()).ToLocalChecked()); else - return mate::Dictionary(); + return gin::Dictionary(isolate); } // Parse headers from response object. -network::ResourceResponseHead ToResponseHead(const mate::Dictionary& dict) { +network::ResourceResponseHead ToResponseHead( + const gin_helper::Dictionary& dict) { network::ResourceResponseHead head; head.mime_type = "text/html"; head.charset = "utf-8"; @@ -197,7 +198,7 @@ void AtomURLLoaderFactory::StartLoading( const net::MutableNetworkTrafficAnnotationTag& traffic_annotation, network::mojom::URLLoaderFactory* proxy_factory, ProtocolType type, - mate::Arguments* args) { + gin::Arguments* args) { // Send network error when there is no argument passed. // // Note that we should not throw JS error in the callback no matter what is @@ -210,7 +211,7 @@ void AtomURLLoaderFactory::StartLoading( } // Parse {error} object. - mate::Dictionary dict = ToDict(args->isolate(), response); + gin_helper::Dictionary dict = ToDict(args->isolate(), response); if (!dict.IsEmpty()) { int error_code; if (dict.Get("error", &error_code)) { @@ -251,7 +252,7 @@ void AtomURLLoaderFactory::StartLoading( } else { StartLoadingHttp(std::move(loader), new_request, std::move(client), traffic_annotation, - mate::Dictionary::CreateEmpty(args->isolate())); + gin::Dictionary::CreateEmpty(args->isolate())); } return; } @@ -285,7 +286,7 @@ void AtomURLLoaderFactory::StartLoading( break; case ProtocolType::kFree: ProtocolType type; - if (!mate::ConvertFromV8(args->isolate(), response, &type)) { + if (!gin::ConvertFromV8(args->isolate(), response, &type)) { client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED)); return; } @@ -300,7 +301,7 @@ void AtomURLLoaderFactory::StartLoading( void AtomURLLoaderFactory::StartLoadingBuffer( network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict) { + const gin_helper::Dictionary& dict) { v8::Local buffer = dict.GetHandle(); dict.Get("data", &buffer); if (!node::Buffer::HasInstance(buffer)) { @@ -317,7 +318,7 @@ void AtomURLLoaderFactory::StartLoadingBuffer( void AtomURLLoaderFactory::StartLoadingString( network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict, + const gin_helper::Dictionary& dict, v8::Isolate* isolate, v8::Local response) { std::string contents; @@ -339,11 +340,11 @@ void AtomURLLoaderFactory::StartLoadingFile( network::ResourceRequest request, network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict, + const gin_helper::Dictionary& dict, v8::Isolate* isolate, v8::Local response) { base::FilePath path; - if (mate::ConvertFromV8(isolate, response, &path)) { + if (gin::ConvertFromV8(isolate, response, &path)) { request.url = net::FilePathToFileURL(path); } else if (!dict.IsEmpty()) { dict.Get("referrer", &request.referrer); @@ -366,7 +367,7 @@ void AtomURLLoaderFactory::StartLoadingHttp( const network::ResourceRequest& original_request, network::mojom::URLLoaderClientPtr client, const net::MutableNetworkTrafficAnnotationTag& traffic_annotation, - const mate::Dictionary& dict) { + const gin_helper::Dictionary& dict) { auto request = std::make_unique(); request->headers = original_request.headers; request->cors_exempt_headers = original_request.cors_exempt_headers; @@ -407,7 +408,7 @@ void AtomURLLoaderFactory::StartLoadingStream( network::mojom::URLLoaderRequest loader, network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict) { + const gin_helper::Dictionary& dict) { v8::Local stream; if (!dict.Get("data", &stream)) { // Assume the opts is already a stream. @@ -435,7 +436,7 @@ void AtomURLLoaderFactory::StartLoadingStream( return; } - mate::Dictionary data = ToDict(dict.isolate(), stream); + gin_helper::Dictionary data = ToDict(dict.isolate(), stream); v8::Local method; if (!data.Get("on", &method) || !method->IsFunction() || !data.Get("removeListener", &method) || !method->IsFunction()) { diff --git a/shell/browser/net/atom_url_loader_factory.h b/shell/browser/net/atom_url_loader_factory.h index 875110c1e311..0038b2f3d174 100644 --- a/shell/browser/net/atom_url_loader_factory.h +++ b/shell/browser/net/atom_url_loader_factory.h @@ -10,10 +10,10 @@ #include #include "mojo/public/cpp/bindings/binding_set.h" -#include "native_mate/dictionary.h" #include "net/url_request/url_request_job_factory.h" #include "services/network/public/cpp/resource_response.h" #include "services/network/public/mojom/url_loader_factory.mojom.h" +#include "shell/common/gin_helper/dictionary.h" namespace electron { @@ -27,7 +27,7 @@ enum class ProtocolType { kFree, // special type for returning arbitrary type of response. }; -using StartLoadingCallback = base::OnceCallback; +using StartLoadingCallback = base::OnceCallback; using ProtocolHandler = base::Callback; @@ -62,22 +62,22 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory { const net::MutableNetworkTrafficAnnotationTag& traffic_annotation, network::mojom::URLLoaderFactory* proxy_factory, ProtocolType type, - mate::Arguments* args); + gin::Arguments* args); private: static void StartLoadingBuffer(network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict); + const gin_helper::Dictionary& dict); static void StartLoadingString(network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict, + const gin_helper::Dictionary& dict, v8::Isolate* isolate, v8::Local response); static void StartLoadingFile(network::mojom::URLLoaderRequest loader, network::ResourceRequest request, network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict, + const gin_helper::Dictionary& dict, v8::Isolate* isolate, v8::Local response); static void StartLoadingHttp( @@ -85,11 +85,11 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory { const network::ResourceRequest& original_request, network::mojom::URLLoaderClientPtr client, const net::MutableNetworkTrafficAnnotationTag& traffic_annotation, - const mate::Dictionary& dict); + const gin_helper::Dictionary& dict); static void StartLoadingStream(network::mojom::URLLoaderRequest loader, network::mojom::URLLoaderClientPtr client, network::ResourceResponseHead head, - const mate::Dictionary& dict); + const gin_helper::Dictionary& dict); // Helper to send string as response. static void SendContents(network::mojom::URLLoaderClientPtr client, diff --git a/shell/common/api/atom_api_key_weak_map.h b/shell/common/api/atom_api_key_weak_map.h index 37d20aa34f7b..159d0596a053 100644 --- a/shell/common/api/atom_api_key_weak_map.h +++ b/shell/common/api/atom_api_key_weak_map.h @@ -61,22 +61,4 @@ 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_v8_util.cc b/shell/common/api/atom_api_v8_util.cc index 786d20d7e6b9..67e4ad73be0f 100644 --- a/shell/common/api/atom_api_v8_util.cc +++ b/shell/common/api/atom_api_v8_util.cc @@ -8,7 +8,6 @@ #include "base/hash/hash.h" #include "electron/buildflags/buildflags.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" diff --git a/shell/common/gin_converters/native_mate_handle_converter.h b/shell/common/gin_converters/native_mate_handle_converter.h deleted file mode 100644 index 66f30b745baa..000000000000 --- a/shell/common/gin_converters/native_mate_handle_converter.h +++ /dev/null @@ -1,30 +0,0 @@ -// 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_helper/dictionary.h b/shell/common/gin_helper/dictionary.h index cdba33d89e0f..05fdb4e35c9f 100644 --- a/shell/common/gin_helper/dictionary.h +++ b/shell/common/gin_helper/dictionary.h @@ -27,6 +27,23 @@ class Dictionary : public gin::Dictionary { Dictionary(const gin::Dictionary& dict) // NOLINT(runtime/explicit) : gin::Dictionary(dict) {} + // Difference from the Get method in gin::Dictionary: + // 1. This is a const method; + // 2. It checks whether the key exists before reading. + template + bool Get(base::StringPiece key, T* out) const { + // Check for existence before getting, otherwise this method will always + // returns true when T == v8::Local. + v8::Local context = isolate()->GetCurrentContext(); + v8::Local v8_key = gin::StringToV8(isolate(), key); + v8::Local value; + v8::Maybe result = GetHandle()->Has(context, v8_key); + if (result.IsJust() && result.FromJust() && + GetHandle()->Get(context, v8_key).ToLocal(&value)) + return gin::ConvertFromV8(isolate(), value, out); + return false; + } + template bool GetHidden(base::StringPiece key, T* out) const { v8::Local context = isolate()->GetCurrentContext(); @@ -80,6 +97,8 @@ class Dictionary : public gin::Dictionary { return !result.IsNothing() && result.FromJust(); } + bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); } + v8::Local GetHandle() const { return gin::ConvertToV8(isolate(), *static_cast(this))