diff --git a/filenames.gni b/filenames.gni index a7e8f1427..2af8f7488 100644 --- a/filenames.gni +++ b/filenames.gni @@ -435,8 +435,8 @@ filenames = { "shell/common/api/electron_bindings.cc", "shell/common/api/electron_bindings.h", "shell/common/api/constructor.h", - "shell/common/api/event_emitter_caller.cc", - "shell/common/api/event_emitter_caller.h", + "shell/common/api/event_emitter_caller_deprecated.cc", + "shell/common/api/event_emitter_caller_deprecated.h", "shell/common/api/features.cc", "shell/common/api/locker.cc", "shell/common/api/locker.h", @@ -494,6 +494,10 @@ filenames = { "shell/common/gin_helper/destroyable.cc", "shell/common/gin_helper/destroyable.h", "shell/common/gin_helper/dictionary.h", + "shell/common/gin_helper/error_thrower.cc", + "shell/common/gin_helper/error_thrower.h", + "shell/common/gin_helper/event_emitter_caller.cc", + "shell/common/gin_helper/event_emitter_caller.h", "shell/common/gin_helper/function_template.cc", "shell/common/gin_helper/function_template.h", "shell/common/heap_snapshot.cc", @@ -503,8 +507,6 @@ filenames = { "shell/common/keyboard_util.h", "shell/common/deprecate_util.cc", "shell/common/deprecate_util.h", - "shell/common/error_util.cc", - "shell/common/error_util.h", "shell/common/mouse_util.cc", "shell/common/mouse_util.h", "shell/common/mac/main_application_bundle.h", diff --git a/native_mate/native_mate/arguments.h b/native_mate/native_mate/arguments.h index d646049b2..4caac83d9 100644 --- a/native_mate/native_mate/arguments.h +++ b/native_mate/native_mate/arguments.h @@ -97,6 +97,7 @@ class Arguments { v8::Local ThrowTypeError(const std::string& message) const; v8::Isolate* isolate() const { return isolate_; } + const v8::FunctionCallbackInfo& info() const { return *info_; } private: v8::Isolate* isolate_ = nullptr; diff --git a/native_mate/native_mate/function_template.h b/native_mate/native_mate/function_template.h index 1ee65f3a8..47ebfd3d2 100644 --- a/native_mate/native_mate/function_template.h +++ b/native_mate/native_mate/function_template.h @@ -5,13 +5,11 @@ #ifndef NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_ #define NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_ -#include "../shell/common/error_util.h" #include "../shell/common/gin_helper/destroyable.h" +#include "../shell/common/gin_helper/error_thrower.h" #include "base/callback.h" -#include "base/logging.h" #include "native_mate/arguments.h" #include "native_mate/wrappable_base.h" -#include "v8/include/v8.h" // =============================== NOTICE =============================== // Do not add code here, native_mate is being removed. Any new code @@ -122,8 +120,8 @@ inline bool GetNextArgument(Arguments* args, inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, - electron::util::ErrorThrower* result) { - *result = electron::util::ErrorThrower(args->isolate()); + gin_helper::ErrorThrower* result) { + *result = gin_helper::ErrorThrower(args->isolate()); return true; } diff --git a/patches/chromium/.patches b/patches/chromium/.patches index 5ad65be39..554942093 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_dictionary_default_constructor.patch blink-worker-enable-csp-in-file-scheme.patch disable-redraw-lock.patch v8_context_snapshot_generator.patch diff --git a/patches/chromium/gin_dictionary_default_constructor.patch b/patches/chromium/gin_dictionary_default_constructor.patch new file mode 100644 index 000000000..3a2be76d0 --- /dev/null +++ b/patches/chromium/gin_dictionary_default_constructor.patch @@ -0,0 +1,37 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Cheng Zhao +Date: Thu, 4 Oct 2018 14:57:02 -0700 +Subject: gin_dictionary_default_constructor.patch + +Add default constructor for gin::Dictionary. + +This is required for automatically converting arguments for functions that +take gin::Dictionary as parameter. + +diff --git a/gin/dictionary.cc b/gin/dictionary.cc +index 95e00072700c..7643347890a5 100644 +--- a/gin/dictionary.cc ++++ b/gin/dictionary.cc +@@ -6,6 +6,10 @@ + + namespace gin { + ++Dictionary::Dictionary() ++ : isolate_(nullptr) { ++} ++ + Dictionary::Dictionary(v8::Isolate* isolate) + : isolate_(isolate) { + } +diff --git a/gin/dictionary.h b/gin/dictionary.h +index 2645d328b4c1..43b227dd7e48 100644 +--- a/gin/dictionary.h ++++ b/gin/dictionary.h +@@ -24,6 +24,7 @@ namespace gin { + // + class GIN_EXPORT Dictionary { + public: ++ Dictionary(); + explicit Dictionary(v8::Isolate* isolate); + Dictionary(v8::Isolate* isolate, v8::Local object); + Dictionary(const Dictionary& other); diff --git a/script/lint.js b/script/lint.js index 9cb6619f1..e34a8e821 100755 --- a/script/lint.js +++ b/script/lint.js @@ -15,6 +15,7 @@ const BLACKLIST = new Set([ ['shell', 'browser', 'mac', 'atom_application_delegate.h'], ['shell', 'browser', 'resources', 'win', 'resource.h'], ['shell', 'browser', 'notifications', 'mac', 'notification_center_delegate.h'], + ['shell', 'browser', 'ui', 'cocoa', 'atom_bundle_mover.h'], ['shell', 'browser', 'ui', 'cocoa', 'atom_menu_controller.h'], ['shell', 'browser', 'ui', 'cocoa', 'atom_ns_window.h'], ['shell', 'browser', 'ui', 'cocoa', 'atom_ns_window_delegate.h'], diff --git a/shell/browser/api/atom_api_app.cc b/shell/browser/api/atom_api_app.cc index d9e898c48..0d190782a 100644 --- a/shell/browser/api/atom_api_app.cc +++ b/shell/browser/api/atom_api_app.cc @@ -26,6 +26,7 @@ #include "content/public/browser/gpu_data_manager.h" #include "content/public/browser/render_frame_host.h" #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 "net/ssl/client_cert_identity.h" @@ -846,7 +847,7 @@ void App::SetAppPath(const base::FilePath& app_path) { } #if !defined(OS_MACOSX) -void App::SetAppLogsPath(util::ErrorThrower thrower, +void App::SetAppLogsPath(gin_helper::ErrorThrower thrower, base::Optional custom_path) { if (custom_path.has_value()) { if (!custom_path->IsAbsolute()) { @@ -865,7 +866,7 @@ void App::SetAppLogsPath(util::ErrorThrower thrower, } #endif -base::FilePath App::GetPath(util::ErrorThrower thrower, +base::FilePath App::GetPath(gin_helper::ErrorThrower thrower, const std::string& name) { bool succeed = false; base::FilePath path; @@ -888,7 +889,7 @@ base::FilePath App::GetPath(util::ErrorThrower thrower, return path; } -void App::SetPath(util::ErrorThrower thrower, +void App::SetPath(gin_helper::ErrorThrower thrower, const std::string& name, const base::FilePath& path) { if (!path.IsAbsolute()) { @@ -1031,7 +1032,7 @@ bool App::Relaunch(mate::Arguments* js_args) { return relauncher::RelaunchApp(argv); } -void App::DisableHardwareAcceleration(util::ErrorThrower thrower) { +void App::DisableHardwareAcceleration(gin_helper::ErrorThrower thrower) { if (Browser::Get()->is_ready()) { thrower.ThrowError( "app.disableHardwareAcceleration() can only be called " @@ -1041,7 +1042,7 @@ void App::DisableHardwareAcceleration(util::ErrorThrower thrower) { content::GpuDataManager::GetInstance()->DisableHardwareAcceleration(); } -void App::DisableDomainBlockingFor3DAPIs(util::ErrorThrower thrower) { +void App::DisableDomainBlockingFor3DAPIs(gin_helper::ErrorThrower thrower) { if (Browser::Get()->is_ready()) { thrower.ThrowError( "app.disableDomainBlockingFor3DAPIs() can only be called " @@ -1057,7 +1058,7 @@ bool App::IsAccessibilitySupportEnabled() { return ax_state->IsAccessibleBrowser(); } -void App::SetAccessibilitySupportEnabled(util::ErrorThrower thrower, +void App::SetAccessibilitySupportEnabled(gin_helper::ErrorThrower thrower, bool enabled) { if (!Browser::Get()->is_ready()) { thrower.ThrowError( @@ -1314,7 +1315,7 @@ static void RemoveNoSandboxSwitch(base::CommandLine* command_line) { } } -void App::EnableSandbox(util::ErrorThrower thrower) { +void App::EnableSandbox(gin_helper::ErrorThrower thrower) { if (Browser::Get()->is_ready()) { thrower.ThrowError( "app.enableSandbox() can only be called " @@ -1343,12 +1344,14 @@ bool App::CanBrowserClientUseCustomSiteInstance() { } #if defined(OS_MACOSX) -bool App::MoveToApplicationsFolder(mate::Arguments* args) { - return ui::cocoa::AtomBundleMover::Move(args); +bool App::MoveToApplicationsFolder(gin_helper::ErrorThrower thrower, + mate::Arguments* args) { + gin::Arguments gin_args(args->info()); + return AtomBundleMover::Move(thrower, &gin_args); } bool App::IsInApplicationsFolder() { - return ui::cocoa::AtomBundleMover::IsCurrentAppInApplicationsFolder(); + return AtomBundleMover::IsCurrentAppInApplicationsFolder(); } int DockBounce(mate::Arguments* args) { diff --git a/shell/browser/api/atom_api_app.h b/shell/browser/api/atom_api_app.h index 1e0ce0a94..2a4325591 100644 --- a/shell/browser/api/atom_api_app.h +++ b/shell/browser/api/atom_api_app.h @@ -27,8 +27,7 @@ #include "shell/browser/atom_browser_client.h" #include "shell/browser/browser.h" #include "shell/browser/browser_observer.h" -#include "shell/common/error_util.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "shell/common/gin_helper/error_thrower.h" #include "shell/common/promise_util.h" #if defined(USE_NSS_CERTS) @@ -165,12 +164,13 @@ class App : public AtomBrowserClient::Delegate, void ChildProcessLaunched(int process_type, base::ProcessHandle handle); void ChildProcessDisconnected(base::ProcessId pid); - void SetAppLogsPath(util::ErrorThrower thrower, + void SetAppLogsPath(gin_helper::ErrorThrower thrower, base::Optional custom_path); // Get/Set the pre-defined path in PathService. - base::FilePath GetPath(util::ErrorThrower thrower, const std::string& name); - void SetPath(util::ErrorThrower thrower, + base::FilePath GetPath(gin_helper::ErrorThrower thrower, + const std::string& name); + void SetPath(gin_helper::ErrorThrower thrower, const std::string& name, const base::FilePath& path); @@ -183,10 +183,11 @@ class App : public AtomBrowserClient::Delegate, bool RequestSingleInstanceLock(); void ReleaseSingleInstanceLock(); bool Relaunch(mate::Arguments* args); - void DisableHardwareAcceleration(util::ErrorThrower thrower); - void DisableDomainBlockingFor3DAPIs(util::ErrorThrower thrower); + void DisableHardwareAcceleration(gin_helper::ErrorThrower thrower); + void DisableDomainBlockingFor3DAPIs(gin_helper::ErrorThrower thrower); bool IsAccessibilitySupportEnabled(); - void SetAccessibilitySupportEnabled(util::ErrorThrower thrower, bool enabled); + void SetAccessibilitySupportEnabled(gin_helper::ErrorThrower thrower, + bool enabled); Browser::LoginItemSettings GetLoginItemSettings(mate::Arguments* args); #if defined(USE_NSS_CERTS) void ImportCertificate(const base::DictionaryValue& options, @@ -199,14 +200,15 @@ class App : public AtomBrowserClient::Delegate, v8::Local GetGPUFeatureStatus(v8::Isolate* isolate); v8::Local GetGPUInfo(v8::Isolate* isolate, const std::string& info_type); - void EnableSandbox(util::ErrorThrower thrower); + void EnableSandbox(gin_helper::ErrorThrower thrower); void SetUserAgentFallback(const std::string& user_agent); std::string GetUserAgentFallback(); void SetBrowserClientCanUseCustomSiteInstance(bool should_disable); bool CanBrowserClientUseCustomSiteInstance(); #if defined(OS_MACOSX) - bool MoveToApplicationsFolder(mate::Arguments* args); + bool MoveToApplicationsFolder(gin_helper::ErrorThrower, + mate::Arguments* args); bool IsInApplicationsFolder(); v8::Local GetDockAPI(v8::Isolate* isolate); v8::Global dock_; diff --git a/shell/browser/api/atom_api_app_mac.mm b/shell/browser/api/atom_api_app_mac.mm index 48e490717..98415bc81 100644 --- a/shell/browser/api/atom_api_app_mac.mm +++ b/shell/browser/api/atom_api_app_mac.mm @@ -13,7 +13,7 @@ namespace electron { namespace api { -void App::SetAppLogsPath(util::ErrorThrower thrower, +void App::SetAppLogsPath(gin_helper::ErrorThrower thrower, base::Optional custom_path) { if (custom_path.has_value()) { if (!custom_path->IsAbsolute()) { diff --git a/shell/browser/api/atom_api_auto_updater.cc b/shell/browser/api/atom_api_auto_updater.cc index c9a9b6e91..ae57e286d 100644 --- a/shell/browser/api/atom_api_auto_updater.cc +++ b/shell/browser/api/atom_api_auto_updater.cc @@ -10,10 +10,12 @@ #include "shell/browser/browser.h" #include "shell/browser/native_window.h" #include "shell/browser/window_list.h" -#include "shell/common/api/event_emitter_caller.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "shell/common/gin_helper/event_emitter_caller.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 mate { template <> @@ -48,7 +50,7 @@ void AutoUpdater::OnError(const std::string& message) { v8::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); auto error = v8::Exception::Error(mate::StringToV8(isolate(), message)); - mate::EmitEvent( + gin_helper::EmitEvent( isolate(), GetWrapper(), "error", error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(), // Message is also emitted to keep compatibility with old code. @@ -76,7 +78,7 @@ void AutoUpdater::OnError(const std::string& message, mate::StringToV8(isolate(), domain)) .Check(); - mate::EmitEvent(isolate(), GetWrapper(), "error", errorObject, message); + gin_helper::EmitEvent(isolate(), GetWrapper(), "error", errorObject, message); } void AutoUpdater::OnCheckingForUpdate() { diff --git a/shell/browser/api/atom_api_browser_window.cc b/shell/browser/api/atom_api_browser_window.cc index 9a9a8b74b..85a4c77b3 100644 --- a/shell/browser/api/atom_api_browser_window.cc +++ b/shell/browser/api/atom_api_browser_window.cc @@ -20,7 +20,6 @@ #include "shell/browser/window_list.h" #include "shell/common/api/constructor.h" #include "shell/common/color_util.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/node_includes.h" #include "shell/common/options_switches.h" diff --git a/shell/browser/api/atom_api_cookies.cc b/shell/browser/api/atom_api_cookies.cc index 1108f479a..3ef55413b 100644 --- a/shell/browser/api/atom_api_cookies.cc +++ b/shell/browser/api/atom_api_cookies.cc @@ -20,7 +20,6 @@ #include "net/cookies/cookie_util.h" #include "shell/browser/atom_browser_context.h" #include "shell/browser/cookie_change_notifier.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/gurl_converter.h" #include "shell/common/native_mate_converters/value_converter.h" diff --git a/shell/browser/api/atom_api_debugger.cc b/shell/browser/api/atom_api_debugger.cc index e50b6a342..4693f54da 100644 --- a/shell/browser/api/atom_api_debugger.cc +++ b/shell/browser/api/atom_api_debugger.cc @@ -13,7 +13,6 @@ #include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/web_contents.h" #include "native_mate/dictionary.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/node_includes.h" diff --git a/shell/browser/api/atom_api_dialog.cc b/shell/browser/api/atom_api_dialog.cc index 9575b0353..996b9ee8f 100644 --- a/shell/browser/api/atom_api_dialog.cc +++ b/shell/browser/api/atom_api_dialog.cc @@ -11,6 +11,7 @@ #include "shell/browser/ui/message_box.h" #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/file_dialog_converter.h" +#include "shell/common/gin_converters/file_path_converter.h" #include "shell/common/gin_converters/message_box_converter.h" #include "shell/common/gin_converters/native_window_converter.h" #include "shell/common/gin_converters/net_converter.h" @@ -59,7 +60,7 @@ void ShowOpenDialogSync(const file_dialog::DialogSettings& settings, v8::Local ShowOpenDialog( const file_dialog::DialogSettings& settings, gin::Arguments* args) { - electron::util::Promise promise(args->isolate()); + electron::util::Promise promise(args->isolate()); v8::Local handle = promise.GetHandle(); file_dialog::ShowOpenDialog(settings, std::move(promise)); return handle; @@ -75,7 +76,7 @@ void ShowSaveDialogSync(const file_dialog::DialogSettings& settings, v8::Local ShowSaveDialog( const file_dialog::DialogSettings& settings, gin::Arguments* args) { - electron::util::Promise promise(args->isolate()); + electron::util::Promise promise(args->isolate()); v8::Local handle = promise.GetHandle(); file_dialog::ShowSaveDialog(settings, std::move(promise)); diff --git a/shell/browser/api/atom_api_download_item.cc b/shell/browser/api/atom_api_download_item.cc index 825927e7d..7a2649013 100644 --- a/shell/browser/api/atom_api_download_item.cc +++ b/shell/browser/api/atom_api_download_item.cc @@ -11,7 +11,6 @@ #include "native_mate/dictionary.h" #include "net/base/filename_util.h" #include "shell/browser/atom_browser_main_parts.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/file_dialog_converter.h" #include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/native_mate_converters/gurl_converter.h" diff --git a/shell/browser/api/atom_api_in_app_purchase.cc b/shell/browser/api/atom_api_in_app_purchase.cc index dba2ff1c7..e572dd486 100644 --- a/shell/browser/api/atom_api_in_app_purchase.cc +++ b/shell/browser/api/atom_api_in_app_purchase.cc @@ -9,7 +9,6 @@ #include #include "native_mate/dictionary.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/node_includes.h" namespace mate { diff --git a/shell/browser/api/atom_api_menu.cc b/shell/browser/api/atom_api_menu.cc index ed322a3df..76fab2c74 100644 --- a/shell/browser/api/atom_api_menu.cc +++ b/shell/browser/api/atom_api_menu.cc @@ -6,16 +6,19 @@ #include +#include "gin/dictionary.h" #include "native_mate/constructor.h" -#include "native_mate/dictionary.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/native_mate_converters/accelerator_converter.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.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 @@ -40,8 +43,8 @@ Menu::~Menu() { } void Menu::AfterInit(v8::Isolate* isolate) { - mate::Dictionary wrappable(isolate, GetWrapper()); - mate::Dictionary delegate; + gin::Dictionary wrappable(isolate, GetWrapper()); + gin::Dictionary delegate(nullptr); if (!wrappable.Get("delegate", &delegate)) return; diff --git a/shell/browser/api/atom_api_menu.h b/shell/browser/api/atom_api_menu.h index 6d5f1552c..eabd695ea 100644 --- a/shell/browser/api/atom_api_menu.h +++ b/shell/browser/api/atom_api_menu.h @@ -144,4 +144,17 @@ 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); + } +}; + +} // namespace gin + #endif // SHELL_BROWSER_API_ATOM_API_MENU_H_ diff --git a/shell/browser/api/atom_api_net_log.cc b/shell/browser/api/atom_api_net_log.cc index a81bc3ed9..a30c2f4eb 100644 --- a/shell/browser/api/atom_api_net_log.cc +++ b/shell/browser/api/atom_api_net_log.cc @@ -16,7 +16,6 @@ #include "native_mate/handle.h" #include "shell/browser/atom_browser_context.h" #include "shell/browser/net/system_network_context_manager.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/node_includes.h" diff --git a/shell/browser/api/atom_api_power_monitor.cc b/shell/browser/api/atom_api_power_monitor.cc index 0ffeb9d23..4240795ee 100644 --- a/shell/browser/api/atom_api_power_monitor.cc +++ b/shell/browser/api/atom_api_power_monitor.cc @@ -6,9 +6,9 @@ #include "base/power_monitor/power_monitor.h" #include "base/power_monitor/power_monitor_device_source.h" -#include "native_mate/dictionary.h" +#include "gin/dictionary.h" #include "shell/browser/browser.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "shell/common/gin_converters/callback_converter.h" #include "shell/common/node_includes.h" namespace mate { @@ -138,9 +138,8 @@ void Initialize(v8::Local exports, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); - mate::Dictionary dict(isolate, exports); - dict.Set("createPowerMonitor", - base::BindRepeating(&PowerMonitor::Create, isolate)); + gin::Dictionary dict(isolate, exports); + dict.Set("createPowerMonitor", base::BindRepeating(&PowerMonitor::Create)); dict.Set("PowerMonitor", PowerMonitor::GetConstructor(isolate) ->GetFunction(context) .ToLocalChecked()); diff --git a/shell/browser/api/atom_api_screen.cc b/shell/browser/api/atom_api_screen.cc index 5131b06fa..8650f2b95 100644 --- a/shell/browser/api/atom_api_screen.cc +++ b/shell/browser/api/atom_api_screen.cc @@ -8,9 +8,10 @@ #include #include "base/bind.h" -#include "native_mate/dictionary.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/native_mate_converters/gfx_converter.h" #include "shell/common/native_mate_converters/native_window_converter.h" #include "shell/common/node_includes.h" @@ -182,8 +183,8 @@ void Initialize(v8::Local exports, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); - mate::Dictionary dict(isolate, exports); - dict.Set("createScreen", base::BindRepeating(&Screen::Create, isolate)); + gin::Dictionary dict(isolate, exports); + dict.Set("createScreen", base::BindRepeating(&Screen::Create)); dict.Set( "Screen", Screen::GetConstructor(isolate)->GetFunction(context).ToLocalChecked()); diff --git a/shell/browser/api/atom_api_session.cc b/shell/browser/api/atom_api_session.cc index 55df63765..688d4a217 100644 --- a/shell/browser/api/atom_api_session.cc +++ b/shell/browser/api/atom_api_session.cc @@ -58,6 +58,7 @@ #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/once_callback.h" #include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/node_includes.h" #include "shell/common/options_switches.h" diff --git a/shell/browser/api/atom_api_system_preferences.h b/shell/browser/api/atom_api_system_preferences.h index 9682f8afb..22b576334 100644 --- a/shell/browser/api/atom_api_system_preferences.h +++ b/shell/browser/api/atom_api_system_preferences.h @@ -12,7 +12,7 @@ #include "base/values.h" #include "native_mate/handle.h" #include "shell/browser/api/event_emitter.h" -#include "shell/common/error_util.h" +#include "shell/common/gin_helper/error_thrower.h" #include "shell/common/promise_util.h" #if defined(OS_WIN) @@ -96,7 +96,7 @@ class SystemPreferences : public mate::EventEmitter void RemoveUserDefault(const std::string& name); bool IsSwipeTrackingFromScrollEventsEnabled(); - std::string GetSystemColor(util::ErrorThrower thrower, + std::string GetSystemColor(gin_helper::ErrorThrower thrower, const std::string& color); bool CanPromptTouchID(); diff --git a/shell/browser/api/atom_api_system_preferences_mac.mm b/shell/browser/api/atom_api_system_preferences_mac.mm index 38f050434..643ee54a5 100644 --- a/shell/browser/api/atom_api_system_preferences_mac.mm +++ b/shell/browser/api/atom_api_system_preferences_mac.mm @@ -405,7 +405,7 @@ std::string SystemPreferences::GetAccentColor() { return base::SysNSStringToUTF8([sysColor RGBAValue]); } -std::string SystemPreferences::GetSystemColor(util::ErrorThrower thrower, +std::string SystemPreferences::GetSystemColor(gin_helper::ErrorThrower thrower, const std::string& color) { NSColor* sysColor = nil; if (color == "blue") { diff --git a/shell/browser/api/atom_api_top_level_window.cc b/shell/browser/api/atom_api_top_level_window.cc index 3a5fe1ddf..074791624 100644 --- a/shell/browser/api/atom_api_top_level_window.cc +++ b/shell/browser/api/atom_api_top_level_window.cc @@ -8,7 +8,7 @@ #include #include "electron/buildflags/buildflags.h" -#include "gin/converter.h" +#include "gin/dictionary.h" #include "native_mate/handle.h" #include "native_mate/persistent_dictionary.h" #include "shell/browser/api/atom_api_browser_view.h" @@ -16,7 +16,8 @@ #include "shell/browser/api/atom_api_view.h" #include "shell/browser/api/atom_api_web_contents.h" #include "shell/common/color_util.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_converters/image_converter.h" #include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/native_mate_converters/gfx_converter.h" #include "shell/common/native_mate_converters/image_converter.h" @@ -35,6 +36,9 @@ #include "ui/base/win/shell.h" #endif +// TODO(zcbenz): Remove this after removing mate::ObjectTemplateBuilder. +#include "shell/common/native_mate_converters/callback_converter_deprecated.h" + #if defined(OS_WIN) namespace mate { @@ -43,8 +47,8 @@ struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Handle val, electron::TaskbarHost::ThumbarButton* out) { - mate::Dictionary dict; - if (!ConvertFromV8(isolate, val, &dict)) + gin::Dictionary dict; + if (!gin::ConvertFromV8(isolate, val, &dict)) return false; dict.Get("click", &(out->clicked_callback)); dict.Get("tooltip", &(out->tooltip)); diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index c5d55a61d..9fd812ad7 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -69,7 +69,6 @@ #include "shell/browser/web_contents_zoom_controller.h" #include "shell/browser/web_view_guest_delegate.h" #include "shell/common/api/atom_api_native_image.h" -#include "shell/common/api/event_emitter_caller.h" #include "shell/common/color_util.h" #include "shell/common/mouse_util.h" #include "shell/common/native_mate_converters/blink_converter.h" diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index dc220d834..4316384af 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -566,4 +566,22 @@ 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/event_emitter.h b/shell/browser/api/event_emitter.h index 4a00457f8..5951f555a 100644 --- a/shell/browser/api/event_emitter.h +++ b/shell/browser/api/event_emitter.h @@ -12,7 +12,7 @@ #include "content/public/browser/browser_thread.h" #include "electron/shell/common/api/api.mojom.h" #include "native_mate/wrappable.h" -#include "shell/common/api/event_emitter_caller.h" +#include "shell/common/api/event_emitter_caller_deprecated.h" namespace content { class RenderFrameHost; diff --git a/shell/browser/atom_download_manager_delegate.cc b/shell/browser/atom_download_manager_delegate.cc index 40fa8e19f..f9534d6d1 100644 --- a/shell/browser/atom_download_manager_delegate.cc +++ b/shell/browser/atom_download_manager_delegate.cc @@ -17,13 +17,15 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_item_utils.h" #include "content/public/browser/download_manager.h" +#include "gin/dictionary.h" #include "net/base/filename_util.h" #include "shell/browser/api/atom_api_download_item.h" #include "shell/browser/atom_browser_context.h" #include "shell/browser/native_window.h" #include "shell/browser/ui/file_dialog.h" #include "shell/browser/web_contents_preferences.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_converters/file_path_converter.h" #include "shell/common/options_switches.h" namespace electron { @@ -122,7 +124,7 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated( settings.force_detached = offscreen; v8::Isolate* isolate = v8::Isolate::GetCurrent(); - electron::util::Promise dialog_promise(isolate); + electron::util::Promise dialog_promise(isolate); auto dialog_callback = base::BindOnce(&AtomDownloadManagerDelegate::OnDownloadSaveDialogDone, base::Unretained(this), download_id, callback); @@ -139,7 +141,7 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated( void AtomDownloadManagerDelegate::OnDownloadSaveDialogDone( uint32_t download_id, const content::DownloadTargetCallback& download_callback, - mate::Dictionary result) { + gin::Dictionary result) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); auto* item = download_manager_->GetDownload(download_id); diff --git a/shell/browser/atom_download_manager_delegate.h b/shell/browser/atom_download_manager_delegate.h index 06d632b54..cdbf6a7fe 100644 --- a/shell/browser/atom_download_manager_delegate.h +++ b/shell/browser/atom_download_manager_delegate.h @@ -48,7 +48,7 @@ class AtomDownloadManagerDelegate : public content::DownloadManagerDelegate { void OnDownloadSaveDialogDone( uint32_t download_id, const content::DownloadTargetCallback& download_callback, - mate::Dictionary result); + gin::Dictionary result); content::DownloadManager* download_manager_; base::WeakPtrFactory weak_ptr_factory_; diff --git a/shell/browser/net/node_stream_loader.cc b/shell/browser/net/node_stream_loader.cc index d1816fb16..cc737b68c 100644 --- a/shell/browser/net/node_stream_loader.cc +++ b/shell/browser/net/node_stream_loader.cc @@ -7,9 +7,7 @@ #include #include "mojo/public/cpp/system/string_data_source.h" -#include "shell/common/api/event_emitter_caller.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" - +#include "shell/common/gin_converters/callback_converter.h" #include "shell/common/node_includes.h" namespace electron { @@ -41,7 +39,7 @@ NodeStreamLoader::~NodeStreamLoader() { // Unsubscribe all handlers. for (const auto& it : handlers_) { - v8::Local args[] = {mate::StringToV8(isolate_, it.first), + v8::Local args[] = {gin::StringToV8(isolate_, it.first), it.second.Get(isolate_)}; node::MakeCallback(isolate_, emitter_.Get(isolate_), "removeListener", node::arraysize(args), args, {0, 0}); @@ -142,8 +140,8 @@ void NodeStreamLoader::On(const char* event, EventCallback callback) { // emitter.on(event, callback) v8::Local args[] = { - mate::StringToV8(isolate_, event), - mate::CallbackToV8(isolate_, std::move(callback)), + gin::StringToV8(isolate_, event), + gin_helper::CallbackToV8Leaked(isolate_, std::move(callback)), }; handlers_[event].Reset(isolate_, args[1]); node::MakeCallback(isolate_, emitter_.Get(isolate_), "on", diff --git a/shell/browser/printing/print_preview_message_handler.cc b/shell/browser/printing/print_preview_message_handler.cc index 79e3cd3e8..90fdcf209 100644 --- a/shell/browser/printing/print_preview_message_handler.cc +++ b/shell/browser/printing/print_preview_message_handler.cc @@ -23,6 +23,8 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/web_contents.h" +#include "shell/common/api/locker.h" + #include "shell/common/node_includes.h" using content::BrowserThread; diff --git a/shell/browser/ui/cocoa/atom_bundle_mover.h b/shell/browser/ui/cocoa/atom_bundle_mover.h index 2ac39d289..8e952a1f2 100644 --- a/shell/browser/ui/cocoa/atom_bundle_mover.h +++ b/shell/browser/ui/cocoa/atom_bundle_mover.h @@ -5,27 +5,27 @@ #ifndef SHELL_BROWSER_UI_COCOA_ATOM_BUNDLE_MOVER_H_ #define SHELL_BROWSER_UI_COCOA_ATOM_BUNDLE_MOVER_H_ -#include +#include "base/mac/foundation_util.h" +#include "shell/common/gin_helper/error_thrower.h" -#include "native_mate/persistent_dictionary.h" +namespace gin { +class Arguments; +} namespace electron { // Possible bundle movement conflicts enum class BundlerMoverConflictType { EXISTS, EXISTS_AND_RUNNING }; -namespace ui { - -namespace cocoa { - class AtomBundleMover { public: - static bool Move(mate::Arguments* args); + static bool Move(gin_helper::ErrorThrower thrower, gin::Arguments* args); static bool IsCurrentAppInApplicationsFolder(); private: - static bool ShouldContinueMove(BundlerMoverConflictType type, - mate::Arguments* args); + static bool ShouldContinueMove(gin_helper::ErrorThrower thrower, + BundlerMoverConflictType type, + gin::Arguments* args); static bool IsInApplicationsFolder(NSString* bundlePath); static NSString* ContainingDiskImageDevice(NSString* bundlePath); static void Relaunch(NSString* destinationPath); @@ -39,10 +39,6 @@ class AtomBundleMover { static bool Trash(NSString* path); }; -} // namespace cocoa - -} // namespace ui - } // namespace electron #endif // SHELL_BROWSER_UI_COCOA_ATOM_BUNDLE_MOVER_H_ diff --git a/shell/browser/ui/cocoa/atom_bundle_mover.mm b/shell/browser/ui/cocoa/atom_bundle_mover.mm index ae322fa03..1c51b21e9 100644 --- a/shell/browser/ui/cocoa/atom_bundle_mover.mm +++ b/shell/browser/ui/cocoa/atom_bundle_mover.mm @@ -4,19 +4,17 @@ #import "shell/browser/ui/cocoa/atom_bundle_mover.h" -#include - #import -#import #import #import #import #import -#import "shell/browser/browser.h" -#include "shell/common/native_mate_converters/once_callback.h" +#include "gin/dictionary.h" +#include "shell/browser/browser.h" +#include "shell/common/gin_converters/callback_converter.h" -namespace mate { +namespace gin { template <> struct Converter { @@ -24,26 +22,23 @@ struct Converter { electron::BundlerMoverConflictType value) { switch (value) { case electron::BundlerMoverConflictType::EXISTS: - return mate::StringToV8(isolate, "exists"); + return gin::StringToV8(isolate, "exists"); case electron::BundlerMoverConflictType::EXISTS_AND_RUNNING: - return mate::StringToV8(isolate, "existsAndRunning"); + return gin::StringToV8(isolate, "existsAndRunning"); default: - return mate::StringToV8(isolate, ""); + return gin::StringToV8(isolate, ""); } } }; -} // namespace mate +} // namespace gin namespace electron { -namespace ui { - -namespace cocoa { - -bool AtomBundleMover::ShouldContinueMove(BundlerMoverConflictType type, - mate::Arguments* args) { - mate::Dictionary options; +bool AtomBundleMover::ShouldContinueMove(gin_helper::ErrorThrower thrower, + BundlerMoverConflictType type, + gin::Arguments* args) { + gin::Dictionary options; bool hasOptions = args->GetNext(&options); base::OnceCallback(BundlerMoverConflictType)> conflict_cb; @@ -57,13 +52,14 @@ bool AtomBundleMover::ShouldContinueMove(BundlerMoverConflictType type, // we only want to throw an error if a user has returned a non-boolean // value; this allows for client-side error handling should something in // the handler throw - args->ThrowError("Invalid conflict handler return type."); + thrower.ThrowError("Invalid conflict handler return type."); } } return true; } -bool AtomBundleMover::Move(mate::Arguments* args) { +bool AtomBundleMover::Move(gin_helper::ErrorThrower thrower, + gin::Arguments* args) { // Path of the current bundle NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; @@ -103,10 +99,10 @@ bool AtomBundleMover::Move(mate::Arguments* args) { &authorizationCanceled)) { if (authorizationCanceled) { // User rejected the authorization request - args->ThrowError("User rejected the authorization request"); + thrower.ThrowError("User rejected the authorization request"); return false; } else { - args->ThrowError( + thrower.ThrowError( "Failed to copy to applications directory even with authorization"); return false; } @@ -117,8 +113,8 @@ bool AtomBundleMover::Move(mate::Arguments* args) { // But first, make sure that it's not running if (IsApplicationAtPathRunning(destinationPath)) { // Check for callback handler and get user choice for open/quit - if (!ShouldContinueMove(BundlerMoverConflictType::EXISTS_AND_RUNNING, - args)) + if (!ShouldContinueMove( + thrower, BundlerMoverConflictType::EXISTS_AND_RUNNING, args)) return false; // Unless explicitly denied, give running app focus and terminate self @@ -131,20 +127,21 @@ bool AtomBundleMover::Move(mate::Arguments* args) { return true; } else { // Check callback handler and get user choice for app trashing - if (!ShouldContinueMove(BundlerMoverConflictType::EXISTS, args)) + if (!ShouldContinueMove(thrower, BundlerMoverConflictType::EXISTS, + args)) return false; // Unless explicitly denied, attempt to trash old app if (!Trash([applicationsDirectory stringByAppendingPathComponent:bundleName])) { - args->ThrowError("Failed to delete existing application"); + thrower.ThrowError("Failed to delete existing application"); return false; } } } if (!CopyBundle(bundlePath, destinationPath)) { - args->ThrowError( + thrower.ThrowError( "Failed to copy current bundle to the applications folder"); return false; } @@ -466,8 +463,4 @@ bool AtomBundleMover::DeleteOrTrash(NSString* path) { } } -} // namespace cocoa - -} // namespace ui - } // namespace electron diff --git a/shell/browser/ui/file_dialog.h b/shell/browser/ui/file_dialog.h index 106318301..7430e1b9a 100644 --- a/shell/browser/ui/file_dialog.h +++ b/shell/browser/ui/file_dialog.h @@ -10,8 +10,7 @@ #include #include "base/files/file_path.h" -#include "native_mate/dictionary.h" -#include "shell/common/native_mate_converters/file_path_converter.h" +#include "gin/dictionary.h" #include "shell/common/promise_util.h" namespace electron { @@ -66,12 +65,12 @@ bool ShowOpenDialogSync(const DialogSettings& settings, std::vector* paths); void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise); + electron::util::Promise promise); bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path); void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise); + electron::util::Promise promise); } // namespace file_dialog diff --git a/shell/browser/ui/file_dialog_gtk.cc b/shell/browser/ui/file_dialog_gtk.cc index 87d63846d..fda14d94e 100644 --- a/shell/browser/ui/file_dialog_gtk.cc +++ b/shell/browser/ui/file_dialog_gtk.cc @@ -11,6 +11,7 @@ #include "chrome/browser/ui/libgtkui/gtk_util.h" #include "shell/browser/native_window_views.h" #include "shell/browser/unresponsive_suppressor.h" +#include "shell/common/gin_converters/file_path_converter.h" #include "ui/base/glib/glib_signal.h" #include "ui/views/widget/desktop_aura/x11_desktop_handler.h" @@ -137,15 +138,15 @@ class FileChooserDialog { gtk_window_present_with_time(GTK_WINDOW(dialog_), time); } - void RunSaveAsynchronous(electron::util::Promise promise) { + void RunSaveAsynchronous(electron::util::Promise promise) { save_promise_.reset( - new electron::util::Promise(std::move(promise))); + new electron::util::Promise(std::move(promise))); RunAsynchronous(); } - void RunOpenAsynchronous(electron::util::Promise promise) { + void RunOpenAsynchronous(electron::util::Promise promise) { open_promise_.reset( - new electron::util::Promise(std::move(promise))); + new electron::util::Promise(std::move(promise))); RunAsynchronous(); } @@ -186,8 +187,8 @@ class FileChooserDialog { GtkWidget* preview_; Filters filters_; - std::unique_ptr> save_promise_; - std::unique_ptr> open_promise_; + std::unique_ptr> save_promise_; + std::unique_ptr> open_promise_; // Callback for when we update the preview for the selection. CHROMEG_CALLBACK_0(FileChooserDialog, void, OnUpdatePreview, GtkWidget*); @@ -198,8 +199,8 @@ class FileChooserDialog { void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) { gtk_widget_hide(dialog_); if (save_promise_) { - mate::Dictionary dict = - mate::Dictionary::CreateEmpty(save_promise_->isolate()); + gin::Dictionary dict = + gin::Dictionary::CreateEmpty(save_promise_->isolate()); if (response == GTK_RESPONSE_ACCEPT) { dict.Set("canceled", false); dict.Set("filePath", GetFileName()); @@ -207,10 +208,10 @@ void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) { dict.Set("canceled", true); dict.Set("filePath", base::FilePath()); } - save_promise_->Resolve(dict); + save_promise_->ResolveWithGin(dict); } else if (open_promise_) { - mate::Dictionary dict = - mate::Dictionary::CreateEmpty(open_promise_->isolate()); + gin::Dictionary dict = + gin::Dictionary::CreateEmpty(open_promise_->isolate()); if (response == GTK_RESPONSE_ACCEPT) { dict.Set("canceled", false); dict.Set("filePaths", GetFileNames()); @@ -218,7 +219,7 @@ void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) { dict.Set("canceled", true); dict.Set("filePaths", std::vector()); } - open_promise_->Resolve(dict); + open_promise_->ResolveWithGin(dict); } delete this; } @@ -294,7 +295,7 @@ bool ShowOpenDialogSync(const DialogSettings& settings, } void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; if (settings.properties & OPEN_DIALOG_OPEN_DIRECTORY) action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER; @@ -317,7 +318,7 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) { } void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { FileChooserDialog* save_dialog = new FileChooserDialog(GTK_FILE_CHOOSER_ACTION_SAVE, settings); save_dialog->RunSaveAsynchronous(std::move(promise)); diff --git a/shell/browser/ui/file_dialog_mac.mm b/shell/browser/ui/file_dialog_mac.mm index ce7ca65c5..eb3ccd5a5 100644 --- a/shell/browser/ui/file_dialog_mac.mm +++ b/shell/browser/ui/file_dialog_mac.mm @@ -17,6 +17,7 @@ #include "base/mac/scoped_cftyperef.h" #include "base/strings/sys_string_conversions.h" #include "shell/browser/native_window.h" +#include "shell/common/gin_converters/file_path_converter.h" @interface PopUpButtonHandler : NSObject @@ -300,15 +301,15 @@ bool ShowOpenDialogSync(const DialogSettings& settings, void OpenDialogCompletion(int chosen, NSOpenPanel* dialog, bool security_scoped_bookmarks, - electron::util::Promise promise) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); + electron::util::Promise promise) { + gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); if (chosen == NSFileHandlingPanelCancelButton) { dict.Set("canceled", true); dict.Set("filePaths", std::vector()); #if defined(MAS_BUILD) dict.Set("bookmarks", std::vector()); #endif - promise.Resolve(dict); + promise.ResolveWithGin(dict); } else { std::vector paths; dict.Set("canceled", false); @@ -324,12 +325,12 @@ void OpenDialogCompletion(int chosen, ReadDialogPaths(dialog, &paths); dict.Set("filePaths", paths); #endif - promise.Resolve(dict); + promise.ResolveWithGin(dict); } } void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { NSOpenPanel* dialog = [NSOpenPanel openPanel]; SetupDialog(dialog, settings); @@ -339,7 +340,7 @@ void ShowOpenDialog(const DialogSettings& settings, // and pass it to the completion handler. bool security_scoped_bookmarks = settings.security_scoped_bookmarks; - __block electron::util::Promise p = std::move(promise); + __block electron::util::Promise p = std::move(promise); if (!settings.parent_window || !settings.parent_window->GetNativeWindow() || settings.force_detached) { @@ -377,13 +378,13 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) { void SaveDialogCompletion(int chosen, NSSavePanel* dialog, bool security_scoped_bookmarks, - electron::util::Promise promise) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); + electron::util::Promise promise) { + gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); if (chosen == NSFileHandlingPanelCancelButton) { dict.Set("canceled", true); dict.Set("filePath", base::FilePath()); #if defined(MAS_BUILD) - dict.Set("bookmark", ""); + dict.Set("bookmark", base::StringPiece()); #endif } else { std::string path = base::SysNSStringToUTF8([[dialog URL] path]); @@ -397,11 +398,11 @@ void SaveDialogCompletion(int chosen, } #endif } - promise.Resolve(dict); + promise.ResolveWithGin(dict); } void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { NSSavePanel* dialog = [NSSavePanel savePanel]; SetupDialog(dialog, settings); @@ -412,7 +413,7 @@ void ShowSaveDialog(const DialogSettings& settings, // and pass it to the completion handler. bool security_scoped_bookmarks = settings.security_scoped_bookmarks; - __block electron::util::Promise p = std::move(promise); + __block electron::util::Promise p = std::move(promise); if (!settings.parent_window || !settings.parent_window->GetNativeWindow() || settings.force_detached) { diff --git a/shell/browser/ui/file_dialog_win.cc b/shell/browser/ui/file_dialog_win.cc index 1b009d0b1..b7d81312a 100644 --- a/shell/browser/ui/file_dialog_win.cc +++ b/shell/browser/ui/file_dialog_win.cc @@ -21,6 +21,7 @@ #include "base/win/registry.h" #include "shell/browser/native_window_views.h" #include "shell/browser/unresponsive_suppressor.h" +#include "shell/common/gin_converters/file_path_converter.h" namespace file_dialog { @@ -81,19 +82,19 @@ bool CreateDialogThread(RunState* run_state) { return true; } -void OnDialogOpened(electron::util::Promise promise, +void OnDialogOpened(electron::util::Promise promise, bool canceled, std::vector paths) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); + gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); dict.Set("canceled", canceled); dict.Set("filePaths", paths); - promise.Resolve(dict); + promise.ResolveWithGin(dict); } void RunOpenDialogInNewThread( const RunState& run_state, const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { std::vector paths; bool result = ShowOpenDialogSync(settings, &paths); run_state.ui_task_runner->PostTask( @@ -102,19 +103,19 @@ void RunOpenDialogInNewThread( run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread); } -void OnSaveDialogDone(electron::util::Promise promise, +void OnSaveDialogDone(electron::util::Promise promise, bool canceled, const base::FilePath path) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); + gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); dict.Set("canceled", canceled); dict.Set("filePath", path); - promise.Resolve(dict); + promise.ResolveWithGin(dict); } void RunSaveDialogInNewThread( const RunState& run_state, const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { base::FilePath path; bool result = ShowSaveDialogSync(settings, &path); run_state.ui_task_runner->PostTask( @@ -276,13 +277,13 @@ bool ShowOpenDialogSync(const DialogSettings& settings, } void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); + electron::util::Promise promise) { + gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); RunState run_state; if (!CreateDialogThread(&run_state)) { dict.Set("canceled", true); dict.Set("filePaths", std::vector()); - promise.Resolve(dict); + promise.ResolveWithGin(dict); } else { run_state.dialog_thread->task_runner()->PostTask( FROM_HERE, base::BindOnce(&RunOpenDialogInNewThread, run_state, @@ -326,13 +327,13 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) { } void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { RunState run_state; if (!CreateDialogThread(&run_state)) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(promise.isolate()); + gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); dict.Set("canceled", true); dict.Set("filePath", base::FilePath()); - promise.Resolve(dict); + promise.ResolveWithGin(dict); } else { run_state.dialog_thread->task_runner()->PostTask( FROM_HERE, base::BindOnce(&RunSaveDialogInNewThread, run_state, diff --git a/shell/browser/web_dialog_helper.cc b/shell/browser/web_dialog_helper.cc index 90db26e6f..0df6d0ed5 100644 --- a/shell/browser/web_dialog_helper.cc +++ b/shell/browser/web_dialog_helper.cc @@ -20,13 +20,14 @@ #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_observer.h" -#include "native_mate/dictionary.h" +#include "gin/dictionary.h" #include "net/base/directory_lister.h" #include "net/base/mime_util.h" #include "shell/browser/atom_browser_context.h" #include "shell/browser/native_window.h" #include "shell/browser/ui/file_dialog.h" -#include "shell/common/native_mate_converters/once_callback.h" +#include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_converters/file_path_converter.h" #include "ui/shell_dialogs/selected_file_info.h" using blink::mojom::FileChooserFileInfo; @@ -54,7 +55,7 @@ class FileSelectHelper : public base::RefCounted, void ShowOpenDialog(const file_dialog::DialogSettings& settings) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - electron::util::Promise promise(isolate); + electron::util::Promise promise(isolate); auto callback = base::BindOnce(&FileSelectHelper::OnOpenDialogDone, this); ignore_result(promise.Then(std::move(callback))); @@ -64,7 +65,7 @@ class FileSelectHelper : public base::RefCounted, void ShowSaveDialog(const file_dialog::DialogSettings& settings) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - electron::util::Promise promise(isolate); + electron::util::Promise promise(isolate); auto callback = base::BindOnce(&FileSelectHelper::OnSaveDialogDone, this); ignore_result(promise.Then(std::move(callback))); @@ -115,7 +116,7 @@ class FileSelectHelper : public base::RefCounted, AddRef(); } - void OnOpenDialogDone(mate::Dictionary result) { + void OnOpenDialogDone(gin::Dictionary result) { std::vector file_info; bool canceled = true; result.Get("canceled", &canceled); @@ -157,7 +158,7 @@ class FileSelectHelper : public base::RefCounted, } } - void OnSaveDialogDone(mate::Dictionary result) { + void OnSaveDialogDone(gin::Dictionary result) { std::vector file_info; bool canceled = true; result.Get("canceled", &canceled); diff --git a/shell/common/api/atom_api_asar.cc b/shell/common/api/atom_api_asar.cc index da3548079..fdd5c9cea 100644 --- a/shell/common/api/atom_api_asar.cc +++ b/shell/common/api/atom_api_asar.cc @@ -7,11 +7,11 @@ #include #include "native_mate/arguments.h" -#include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" #include "native_mate/wrappable.h" #include "shell/common/asar/archive.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/node_includes.h" #include "third_party/electron_node/src/node_native_module_env.h" @@ -56,7 +56,7 @@ class Archive : public mate::Wrappable { asar::Archive::FileInfo info; if (!archive_ || !archive_->GetFileInfo(path, &info)) return v8::False(isolate); - mate::Dictionary dict(isolate, v8::Object::New(isolate)); + gin_helper::Dictionary dict(isolate, v8::Object::New(isolate)); dict.Set("size", info.size); dict.Set("unpacked", info.unpacked); dict.Set("offset", info.offset); @@ -68,7 +68,7 @@ class Archive : public mate::Wrappable { asar::Archive::Stats stats; if (!archive_ || !archive_->Stat(path, &stats)) return v8::False(isolate); - mate::Dictionary dict(isolate, v8::Object::New(isolate)); + gin_helper::Dictionary dict(isolate, v8::Object::New(isolate)); dict.Set("size", stats.size); dict.Set("offset", stats.offset); dict.Set("isFile", stats.is_file); @@ -131,7 +131,7 @@ 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("createArchive", &Archive::Create); dict.SetMethod("initAsarSupport", &InitAsarSupport); } diff --git a/shell/common/api/atom_api_shell.cc b/shell/common/api/atom_api_shell.cc index 771341093..532f7272c 100644 --- a/shell/common/api/atom_api_shell.cc +++ b/shell/common/api/atom_api_shell.cc @@ -4,9 +4,10 @@ #include -#include "native_mate/dictionary.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" -#include "shell/common/native_mate_converters/file_path_converter.h" +#include "shell/common/gin_converters/callback_converter.h" +#include "shell/common/gin_converters/file_path_converter.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/error_thrower.h" #include "shell/common/native_mate_converters/gurl_converter.h" #include "shell/common/native_mate_converters/string16_converter.h" #include "shell/common/node_includes.h" @@ -17,7 +18,7 @@ #include "base/win/scoped_com_initializer.h" #include "base/win/shortcut.h" -namespace mate { +namespace gin { template <> struct Converter { @@ -39,7 +40,7 @@ struct Converter { } }; -} // namespace mate +} // namespace gin #endif namespace { @@ -52,13 +53,13 @@ void OnOpenExternalFinished(electron::util::Promise promise, promise.RejectWithErrorMessage(error.c_str()); } -v8::Local OpenExternal(const GURL& url, mate::Arguments* args) { +v8::Local OpenExternal(const GURL& url, gin::Arguments* args) { electron::util::Promise promise(args->isolate()); v8::Local handle = promise.GetHandle(); platform_util::OpenExternalOptions options; if (args->Length() >= 2) { - mate::Dictionary obj; + gin::Dictionary obj(nullptr); if (args->GetNext(&obj)) { obj.Get("activate", &options.activate); obj.Get("workingDirectory", &options.working_dir); @@ -71,7 +72,7 @@ v8::Local OpenExternal(const GURL& url, mate::Arguments* args) { return handle; } -bool MoveItemToTrash(mate::Arguments* args) { +bool MoveItemToTrash(gin::Arguments* args) { base::FilePath full_path; args->GetNext(&full_path); @@ -83,10 +84,11 @@ bool MoveItemToTrash(mate::Arguments* args) { #if defined(OS_WIN) bool WriteShortcutLink(const base::FilePath& shortcut_path, - mate::Arguments* args) { + gin::Arguments* args) { base::win::ShortcutOperation operation = base::win::SHORTCUT_CREATE_ALWAYS; - args->GetNext(&operation); - mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate()); + if (gin::ConvertFromV8(args->isolate(), args->PeekNext(), &operation)) + args->Skip(); + gin::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate()); if (!args->GetNext(&options)) { args->ThrowError(); return false; @@ -114,16 +116,16 @@ bool WriteShortcutLink(const base::FilePath& shortcut_path, operation); } -v8::Local ReadShortcutLink(mate::Arguments* args, +v8::Local ReadShortcutLink(gin_helper::ErrorThrower thrower, const base::FilePath& path) { using base::win::ShortcutProperties; - mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate()); + gin::Dictionary options = gin::Dictionary::CreateEmpty(thrower.isolate()); base::win::ScopedCOMInitializer com_initializer; base::win::ShortcutProperties properties; if (!base::win::ResolveShortcutProperties( path, ShortcutProperties::PROPERTIES_ALL, &properties)) { - args->ThrowError("Failed to read shortcut link"); - return v8::Null(args->isolate()); + thrower.ThrowError("Failed to read shortcut link"); + return v8::Null(thrower.isolate()); } options.Set("target", properties.target); options.Set("cwd", properties.working_dir); @@ -132,7 +134,7 @@ v8::Local ReadShortcutLink(mate::Arguments* args, options.Set("icon", properties.icon); options.Set("iconIndex", properties.icon_index); options.Set("appUserModelId", properties.app_id); - return options.GetHandle(); + return gin::ConvertToV8(thrower.isolate(), options); } #endif @@ -140,7 +142,7 @@ 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("showItemInFolder", &platform_util::ShowItemInFolder); dict.SetMethod("openItem", &platform_util::OpenItem); dict.SetMethod("openExternal", &OpenExternal); diff --git a/shell/common/api/event_emitter_caller_deprecated.cc b/shell/common/api/event_emitter_caller_deprecated.cc new file mode 100644 index 000000000..f73f21d88 --- /dev/null +++ b/shell/common/api/event_emitter_caller_deprecated.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "shell/common/api/event_emitter_caller_deprecated.h" + +#include "shell/common/api/locker.h" +#include "shell/common/node_includes.h" + +namespace mate { + +namespace internal { + +v8::Local CallMethodWithArgs(v8::Isolate* isolate, + v8::Local obj, + const char* method, + ValueVector* args) { + // Perform microtask checkpoint after running JavaScript. + v8::MicrotasksScope script_scope(isolate, + v8::MicrotasksScope::kRunMicrotasks); + // Use node::MakeCallback to call the callback, and it will also run pending + // tasks in Node.js. + v8::MaybeLocal ret = node::MakeCallback( + isolate, obj, method, args->size(), &args->front(), {0, 0}); + // If the JS function throws an exception (doesn't return a value) the result + // of MakeCallback will be empty and therefore ToLocal will be false, in this + // case we need to return "false" as that indicates that the event emitter did + // not handle the event + v8::Local localRet; + if (ret.ToLocal(&localRet)) { + return localRet; + } + return v8::Boolean::New(isolate, false); +} + +} // namespace internal + +} // namespace mate diff --git a/shell/common/api/event_emitter_caller_deprecated.h b/shell/common/api/event_emitter_caller_deprecated.h new file mode 100644 index 000000000..328514a09 --- /dev/null +++ b/shell/common/api/event_emitter_caller_deprecated.h @@ -0,0 +1,73 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_COMMON_API_EVENT_EMITTER_CALLER_DEPRECATED_H_ +#define SHELL_COMMON_API_EVENT_EMITTER_CALLER_DEPRECATED_H_ + +// =============================== NOTICE =============================== +// Do not add code here, native_mate is being removed. Any new code +// should use gin_helper version instead. + +#include +#include + +#include "native_mate/converter.h" +#include "shell/common/native_mate_converters/string16_converter.h" + +namespace mate { + +namespace internal { + +using ValueVector = std::vector>; + +v8::Local CallMethodWithArgs(v8::Isolate* isolate, + v8::Local obj, + const char* method, + ValueVector* args); + +} // namespace internal + +// obj.emit.apply(obj, name, args...); +// The caller is responsible of allocating a HandleScope. +template +v8::Local EmitEvent(v8::Isolate* isolate, + v8::Local obj, + const StringType& name, + const internal::ValueVector& args) { + internal::ValueVector concatenated_args = {StringToV8(isolate, name)}; + concatenated_args.reserve(1 + args.size()); + concatenated_args.insert(concatenated_args.end(), args.begin(), args.end()); + return internal::CallMethodWithArgs(isolate, obj, "emit", &concatenated_args); +} + +// obj.emit(name, args...); +// The caller is responsible of allocating a HandleScope. +template +v8::Local EmitEvent(v8::Isolate* isolate, + v8::Local obj, + const StringType& name, + Args&&... args) { + internal::ValueVector converted_args = { + StringToV8(isolate, name), + ConvertToV8(isolate, std::forward(args))..., + }; + return internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args); +} + +// obj.custom_emit(args...) +template +v8::Local CustomEmit(v8::Isolate* isolate, + v8::Local object, + const char* custom_emit, + Args&&... args) { + internal::ValueVector converted_args = { + ConvertToV8(isolate, std::forward(args))..., + }; + return internal::CallMethodWithArgs(isolate, object, custom_emit, + &converted_args); +} + +} // namespace mate + +#endif // SHELL_COMMON_API_EVENT_EMITTER_CALLER_DEPRECATED_H_ diff --git a/shell/common/deprecate_util.cc b/shell/common/deprecate_util.cc index 9edc511e0..6d20cf390 100644 --- a/shell/common/deprecate_util.cc +++ b/shell/common/deprecate_util.cc @@ -4,17 +4,15 @@ #include "shell/common/deprecate_util.h" -#include "base/callback.h" -#include "native_mate/converter.h" -#include "native_mate/dictionary.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" +#include "gin/dictionary.h" +#include "shell/common/gin_converters/callback_converter.h" namespace electron { void EmitDeprecationWarning(node::Environment* env, const std::string& warning_msg, const std::string& warning_type) { - mate::Dictionary process(env->isolate(), env->process_object()); + gin::Dictionary process(env->isolate(), env->process_object()); base::RepeatingCallback diff --git a/shell/common/error_util.h b/shell/common/error_util.h deleted file mode 100644 index eaf6cacdb..000000000 --- a/shell/common/error_util.h +++ /dev/null @@ -1,47 +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_ERROR_UTIL_H_ -#define SHELL_COMMON_ERROR_UTIL_H_ - -#include - -#include "native_mate/converter.h" - -namespace electron { - -namespace util { - -class ErrorThrower { - public: - explicit ErrorThrower(v8::Isolate* isolate); - ErrorThrower(); - - ~ErrorThrower(); - - void ThrowError(const std::string& err_msg); - void ThrowTypeError(const std::string& err_msg); - void ThrowRangeError(const std::string& err_msg); - void ThrowReferenceError(const std::string& err_msg); - void ThrowSyntaxError(const std::string& err_msg); - - private: - v8::Isolate* isolate() const { return isolate_; } - - using ErrorGenerator = - v8::Local (*)(v8::Local err_msg); - void Throw(ErrorGenerator gen, const std::string& err_msg) { - v8::Local exception = gen(mate::StringToV8(isolate_, err_msg)); - if (!isolate_->IsExecutionTerminating()) - isolate_->ThrowException(exception); - } - - v8::Isolate* isolate_; -}; - -} // namespace util - -} // namespace electron - -#endif // SHELL_COMMON_ERROR_UTIL_H_ diff --git a/shell/common/gin_converters/std_converter.h b/shell/common/gin_converters/std_converter.h index f9a66a0b9..eaa54f6cb 100644 --- a/shell/common/gin_converters/std_converter.h +++ b/shell/common/gin_converters/std_converter.h @@ -6,11 +6,30 @@ #define SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ #include +#include #include "gin/converter.h" namespace gin { +// Make it possible to convert move-only types. +template +v8::Local ConvertToV8(v8::Isolate* isolate, T&& input) { + return Converter::type>::ToV8( + isolate, std::move(input)); +} + +#if !defined(OS_LINUX) && !defined(OS_FREEBSD) +template <> +struct Converter { // NOLINT(runtime/int) + static v8::Local ToV8(v8::Isolate* isolate, + unsigned long val); // NOLINT(runtime/int) + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + unsigned long* out); // NOLINT(runtime/int) +}; +#endif + template <> struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, diff --git a/shell/common/gin_helper/callback.cc b/shell/common/gin_helper/callback.cc index 756dff91a..5f803f882 100644 --- a/shell/common/gin_helper/callback.cc +++ b/shell/common/gin_helper/callback.cc @@ -41,11 +41,11 @@ void CallTranslater(v8::Local external, v8::Isolate* isolate = args->isolate(); auto context = isolate->GetCurrentContext(); bool one_time = - state->Has(context, mate::StringToSymbol(isolate, "oneTime")).ToChecked(); + state->Has(context, gin::StringToSymbol(isolate, "oneTime")).ToChecked(); // Check if the callback has already been called. if (one_time) { - auto called_symbol = mate::StringToSymbol(isolate, "called"); + auto called_symbol = gin::StringToSymbol(isolate, "called"); if (state->Has(context, called_symbol).ToChecked()) { args->ThrowTypeError("callback can only be called for once"); return; @@ -145,7 +145,7 @@ v8::Local BindFunctionWith(v8::Isolate* isolate, v8::Local arg1, v8::Local arg2) { v8::MaybeLocal bind = - func->Get(context, mate::StringToV8(isolate, "bind")); + func->Get(context, gin::StringToV8(isolate, "bind")); CHECK(!bind.IsEmpty()); v8::Local bind_func = v8::Local::Cast(bind.ToLocalChecked()); diff --git a/shell/common/gin_helper/callback.h b/shell/common/gin_helper/callback.h index 2c8a1ecba..5175fd304 100644 --- a/shell/common/gin_helper/callback.h +++ b/shell/common/gin_helper/callback.h @@ -10,21 +10,11 @@ #include "base/bind.h" #include "shell/common/api/locker.h" +#include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_helper/function_template.h" // Implements safe convertions between JS functions and base::Callback. -namespace gin { - -// Make it possible to convert move-only types. -template -v8::Local ConvertToV8(v8::Isolate* isolate, T&& input) { - return Converter::type>::ToV8( - isolate, std::move(input)); -} - -} // namespace gin - namespace gin_helper { template diff --git a/shell/common/gin_helper/dictionary.h b/shell/common/gin_helper/dictionary.h index f3f61a098..ec637c5fb 100644 --- a/shell/common/gin_helper/dictionary.h +++ b/shell/common/gin_helper/dictionary.h @@ -69,6 +69,23 @@ class Dictionary : public gin::Dictionary { .ToChecked(); } + template + bool SetReadOnly(base::StringPiece key, const T& val) { + v8::Local v8_value; + if (!gin::TryConvertToV8(isolate(), val, &v8_value)) + return false; + v8::Maybe result = GetHandle()->DefineOwnProperty( + isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key), + v8_value, v8::ReadOnly); + return !result.IsNothing() && result.FromJust(); + } + + bool Delete(base::StringPiece key) { + v8::Maybe result = GetHandle()->Delete( + isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key)); + return !result.IsNothing() && result.FromJust(); + } + v8::Local GetHandle() const { return gin::ConvertToV8(isolate(), *static_cast(this)) diff --git a/shell/common/error_util.cc b/shell/common/gin_helper/error_thrower.cc similarity index 50% rename from shell/common/error_util.cc rename to shell/common/gin_helper/error_thrower.cc index 46327bfc2..c3af12372 100644 --- a/shell/common/error_util.cc +++ b/shell/common/gin_helper/error_thrower.cc @@ -2,14 +2,11 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include +#include "shell/common/gin_helper/error_thrower.h" -#include "native_mate/converter.h" -#include "shell/common/error_util.h" +#include "gin/converter.h" -namespace electron { - -namespace util { +namespace gin_helper { ErrorThrower::ErrorThrower(v8::Isolate* isolate) : isolate_(isolate) {} @@ -20,26 +17,30 @@ ErrorThrower::ErrorThrower() : isolate_(v8::Isolate::GetCurrent()) {} ErrorThrower::~ErrorThrower() = default; -void ErrorThrower::ThrowError(const std::string& err_msg) { +void ErrorThrower::ThrowError(base::StringPiece err_msg) { Throw(v8::Exception::Error, err_msg); } -void ErrorThrower::ThrowTypeError(const std::string& err_msg) { +void ErrorThrower::ThrowTypeError(base::StringPiece err_msg) { Throw(v8::Exception::TypeError, err_msg); } -void ErrorThrower::ThrowRangeError(const std::string& err_msg) { +void ErrorThrower::ThrowRangeError(base::StringPiece err_msg) { Throw(v8::Exception::RangeError, err_msg); } -void ErrorThrower::ThrowReferenceError(const std::string& err_msg) { +void ErrorThrower::ThrowReferenceError(base::StringPiece err_msg) { Throw(v8::Exception::ReferenceError, err_msg); } -void ErrorThrower::ThrowSyntaxError(const std::string& err_msg) { +void ErrorThrower::ThrowSyntaxError(base::StringPiece err_msg) { Throw(v8::Exception::SyntaxError, err_msg); } -} // namespace util +void ErrorThrower::Throw(ErrorGenerator gen, base::StringPiece err_msg) { + v8::Local exception = gen(gin::StringToV8(isolate_, err_msg)); + if (!isolate_->IsExecutionTerminating()) + isolate_->ThrowException(exception); +} -} // namespace electron +} // namespace gin_helper diff --git a/shell/common/gin_helper/error_thrower.h b/shell/common/gin_helper/error_thrower.h new file mode 100644 index 000000000..2a54021e7 --- /dev/null +++ b/shell/common/gin_helper/error_thrower.h @@ -0,0 +1,38 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_ +#define SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_ + +#include "base/strings/string_piece.h" +#include "v8/include/v8.h" + +namespace gin_helper { + +class ErrorThrower { + public: + explicit ErrorThrower(v8::Isolate* isolate); + ErrorThrower(); + + ~ErrorThrower(); + + void ThrowError(base::StringPiece err_msg); + void ThrowTypeError(base::StringPiece err_msg); + void ThrowRangeError(base::StringPiece err_msg); + void ThrowReferenceError(base::StringPiece err_msg); + void ThrowSyntaxError(base::StringPiece err_msg); + + v8::Isolate* isolate() const { return isolate_; } + + private: + using ErrorGenerator = + v8::Local (*)(v8::Local err_msg); + void Throw(ErrorGenerator gen, base::StringPiece err_msg); + + v8::Isolate* isolate_; +}; + +} // namespace gin_helper + +#endif // SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_ diff --git a/shell/common/api/event_emitter_caller.cc b/shell/common/gin_helper/event_emitter_caller.cc similarity index 89% rename from shell/common/api/event_emitter_caller.cc rename to shell/common/gin_helper/event_emitter_caller.cc index 6288da03e..f57048e45 100644 --- a/shell/common/api/event_emitter_caller.cc +++ b/shell/common/gin_helper/event_emitter_caller.cc @@ -1,13 +1,13 @@ -// Copyright (c) 2015 GitHub, Inc. +// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include "shell/common/api/event_emitter_caller.h" +#include "shell/common/gin_helper/event_emitter_caller.h" #include "shell/common/api/locker.h" #include "shell/common/node_includes.h" -namespace mate { +namespace gin_helper { namespace internal { @@ -35,4 +35,4 @@ v8::Local CallMethodWithArgs(v8::Isolate* isolate, } // namespace internal -} // namespace mate +} // namespace gin_helper diff --git a/shell/common/api/event_emitter_caller.h b/shell/common/gin_helper/event_emitter_caller.h similarity index 78% rename from shell/common/api/event_emitter_caller.h rename to shell/common/gin_helper/event_emitter_caller.h index fa51333ed..aeae06ad8 100644 --- a/shell/common/api/event_emitter_caller.h +++ b/shell/common/gin_helper/event_emitter_caller.h @@ -1,17 +1,16 @@ -// Copyright (c) 2015 GitHub, Inc. +// 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_API_EVENT_EMITTER_CALLER_H_ -#define SHELL_COMMON_API_EVENT_EMITTER_CALLER_H_ +#ifndef SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ +#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ #include #include -#include "native_mate/converter.h" -#include "shell/common/native_mate_converters/string16_converter.h" +#include "gin/converter.h" -namespace mate { +namespace gin_helper { namespace internal { @@ -31,7 +30,7 @@ v8::Local EmitEvent(v8::Isolate* isolate, v8::Local obj, const StringType& name, const internal::ValueVector& args) { - internal::ValueVector concatenated_args = {StringToV8(isolate, name)}; + internal::ValueVector concatenated_args = {gin::StringToV8(isolate, name)}; concatenated_args.reserve(1 + args.size()); concatenated_args.insert(concatenated_args.end(), args.begin(), args.end()); return internal::CallMethodWithArgs(isolate, obj, "emit", &concatenated_args); @@ -45,8 +44,8 @@ v8::Local EmitEvent(v8::Isolate* isolate, const StringType& name, Args&&... args) { internal::ValueVector converted_args = { - StringToV8(isolate, name), - ConvertToV8(isolate, std::forward(args))..., + gin::StringToV8(isolate, name), + gin::ConvertToV8(isolate, std::forward(args))..., }; return internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args); } @@ -58,12 +57,12 @@ v8::Local CustomEmit(v8::Isolate* isolate, const char* custom_emit, Args&&... args) { internal::ValueVector converted_args = { - ConvertToV8(isolate, std::forward(args))..., + gin::ConvertToV8(isolate, std::forward(args))..., }; return internal::CallMethodWithArgs(isolate, object, custom_emit, &converted_args); } -} // namespace mate +} // namespace gin_helper -#endif // SHELL_COMMON_API_EVENT_EMITTER_CALLER_H_ +#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ diff --git a/shell/common/gin_helper/function_template.h b/shell/common/gin_helper/function_template.h index a685eef76..15288e6ad 100644 --- a/shell/common/gin_helper/function_template.h +++ b/shell/common/gin_helper/function_template.h @@ -7,8 +7,8 @@ #include "base/callback.h" #include "gin/arguments.h" -#include "shell/common/error_util.h" #include "shell/common/gin_helper/destroyable.h" +#include "shell/common/gin_helper/error_thrower.h" // This file is forked from gin/function_template.h with 2 differences: // 1. Support for additional types of arguments. @@ -112,8 +112,8 @@ inline bool GetNextArgument(gin::Arguments* args, inline bool GetNextArgument(gin::Arguments* args, int create_flags, bool is_first, - electron::util::ErrorThrower* result) { - *result = electron::util::ErrorThrower(args->isolate()); + ErrorThrower* result) { + *result = ErrorThrower(args->isolate()); return true; } diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 655fac20e..61f8c0eb0 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -22,12 +22,12 @@ #include "content/public/browser/browser_thread.h" #include "content/public/common/content_paths.h" #include "electron/buildflags/buildflags.h" -#include "native_mate/dictionary.h" -#include "shell/common/api/event_emitter_caller.h" #include "shell/common/api/locker.h" #include "shell/common/atom_command_line.h" +#include "shell/common/gin_converters/file_path_converter.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/event_emitter_caller.h" #include "shell/common/mac/main_application_bundle.h" -#include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/node_includes.h" #define ELECTRON_BUILTIN_MODULES(V) \ @@ -314,7 +314,7 @@ node::Environment* NodeBindings::CreateEnvironment( break; } - mate::Dictionary global(context->GetIsolate(), context->Global()); + gin_helper::Dictionary global(context->GetIsolate(), context->Global()); // Do not set DOM globals for renderer process. // We must set this before the node bootstrapper which is run inside // CreateEnvironment @@ -351,7 +351,7 @@ node::Environment* NodeBindings::CreateEnvironment( context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped); } - mate::Dictionary process(context->GetIsolate(), env->process_object()); + gin_helper::Dictionary process(context->GetIsolate(), env->process_object()); process.SetReadOnly("type", process_type); process.Set("resourcesPath", resources_path); // The path to helper app. @@ -364,7 +364,7 @@ node::Environment* NodeBindings::CreateEnvironment( void NodeBindings::LoadEnvironment(node::Environment* env) { node::LoadEnvironment(env); - mate::EmitEvent(env->isolate(), env->process_object(), "loaded"); + gin_helper::EmitEvent(env->isolate(), env->process_object(), "loaded"); } void NodeBindings::PrepareMessageLoop() { diff --git a/shell/common/promise_util.h b/shell/common/promise_util.h index e7dee4691..7d12bc0eb 100644 --- a/shell/common/promise_util.h +++ b/shell/common/promise_util.h @@ -14,9 +14,7 @@ #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "native_mate/converter.h" -#include "shell/common/api/locker.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" -#include "shell/common/native_mate_converters/once_callback.h" +#include "shell/common/gin_converters/std_converter.h" namespace electron { @@ -142,7 +140,7 @@ class Promise { v8::Context::Scope context_scope( v8::Local::New(isolate(), GetContext())); - v8::Local value = mate::ConvertToV8(isolate(), cb); + v8::Local value = gin::ConvertToV8(isolate(), std::move(cb)); v8::Local handler = v8::Local::Cast(value); return GetHandle()->Then(GetContext(), handler); diff --git a/shell/renderer/api/atom_api_web_frame.cc b/shell/renderer/api/atom_api_web_frame.cc index e8f37aba8..5a28c6c0d 100644 --- a/shell/renderer/api/atom_api_web_frame.cc +++ b/shell/renderer/api/atom_api_web_frame.cc @@ -13,12 +13,9 @@ #include "content/public/renderer/render_frame_visitor.h" #include "content/public/renderer/render_view.h" #include "native_mate/dictionary.h" -#include "native_mate/object_template_builder.h" #include "services/service_manager/public/cpp/interface_provider.h" #include "shell/common/api/api.mojom.h" -#include "shell/common/api/event_emitter_caller.h" #include "shell/common/native_mate_converters/blink_converter.h" -#include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/gfx_converter.h" #include "shell/common/native_mate_converters/string16_converter.h" #include "shell/common/node_includes.h" diff --git a/shell/renderer/atom_render_frame_observer.cc b/shell/renderer/atom_render_frame_observer.cc index 2f32b9fda..2067ebe63 100644 --- a/shell/renderer/atom_render_frame_observer.cc +++ b/shell/renderer/atom_render_frame_observer.cc @@ -16,13 +16,9 @@ #include "electron/buildflags/buildflags.h" #include "electron/shell/common/api/api.mojom.h" #include "ipc/ipc_message_macros.h" -#include "native_mate/dictionary.h" #include "net/base/net_module.h" #include "net/grit/net_resources.h" #include "services/service_manager/public/cpp/interface_provider.h" -#include "shell/common/api/event_emitter_caller.h" -#include "shell/common/native_mate_converters/value_converter.h" -#include "shell/common/node_includes.h" #include "shell/common/options_switches.h" #include "third_party/blink/public/platform/web_isolated_world_info.h" #include "third_party/blink/public/web/blink.h" diff --git a/shell/renderer/atom_renderer_client.cc b/shell/renderer/atom_renderer_client.cc index a6d5684a3..031f06e73 100644 --- a/shell/renderer/atom_renderer_client.cc +++ b/shell/renderer/atom_renderer_client.cc @@ -10,10 +10,10 @@ #include "base/command_line.h" #include "content/public/renderer/render_frame.h" #include "electron/buildflags/buildflags.h" -#include "native_mate/dictionary.h" #include "shell/common/api/electron_bindings.h" -#include "shell/common/api/event_emitter_caller.h" #include "shell/common/asar/asar_util.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/event_emitter_caller.h" #include "shell/common/node_bindings.h" #include "shell/common/node_includes.h" #include "shell/common/options_switches.h" @@ -56,7 +56,8 @@ void AtomRendererClient::RunScriptsAtDocumentStart( v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); node::Environment* env = GetEnvironment(render_frame); if (env) - mate::EmitEvent(env->isolate(), env->process_object(), "document-start"); + gin_helper::EmitEvent(env->isolate(), env->process_object(), + "document-start"); } void AtomRendererClient::RunScriptsAtDocumentEnd( @@ -66,7 +67,8 @@ void AtomRendererClient::RunScriptsAtDocumentEnd( v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); node::Environment* env = GetEnvironment(render_frame); if (env) - mate::EmitEvent(env->isolate(), env->process_object(), "document-end"); + gin_helper::EmitEvent(env->isolate(), env->process_object(), + "document-end"); } void AtomRendererClient::DidCreateScriptContext( @@ -135,7 +137,7 @@ void AtomRendererClient::DidCreateScriptContext( // Add Electron extended APIs. electron_bindings_->BindTo(env->isolate(), env->process_object()); AddRenderBindings(env->isolate(), env->process_object()); - mate::Dictionary process_dict(env->isolate(), env->process_object()); + gin_helper::Dictionary process_dict(env->isolate(), env->process_object()); process_dict.SetReadOnly("isMainFrame", render_frame->IsMainFrame()); // Load everything. @@ -160,7 +162,7 @@ void AtomRendererClient::WillReleaseScriptContext( if (environments_.erase(env) == 0) return; - mate::EmitEvent(env->isolate(), env->process_object(), "exit"); + gin_helper::EmitEvent(env->isolate(), env->process_object(), "exit"); // The main frame may be replaced. if (env == node_bindings_->uv_env()) diff --git a/shell/renderer/electron_api_service_impl.cc b/shell/renderer/electron_api_service_impl.cc index 24b6f901b..ce7d572bf 100644 --- a/shell/renderer/electron_api_service_impl.cc +++ b/shell/renderer/electron_api_service_impl.cc @@ -11,16 +11,14 @@ #include "base/environment.h" #include "base/macros.h" #include "base/threading/thread_restrictions.h" -#include "electron/shell/common/api/event_emitter_caller.h" -#include "electron/shell/common/node_includes.h" -#include "electron/shell/common/options_switches.h" -#include "electron/shell/renderer/atom_render_frame_observer.h" -#include "electron/shell/renderer/renderer_client_base.h" #include "mojo/public/cpp/system/platform_handle.h" -#include "native_mate/dictionary.h" #include "shell/common/atom_constants.h" +#include "shell/common/gin_converters/value_converter_gin_adapter.h" #include "shell/common/heap_snapshot.h" -#include "shell/common/native_mate_converters/value_converter.h" +#include "shell/common/node_includes.h" +#include "shell/common/options_switches.h" +#include "shell/renderer/atom_render_frame_observer.h" +#include "shell/renderer/renderer_client_base.h" #include "third_party/blink/public/web/blink.h" #include "third_party/blink/public/web/web_local_frame.h" @@ -33,8 +31,7 @@ const char kIpcKey[] = "ipcNative"; // Gets the private object under kIpcKey v8::Local GetIpcObject(v8::Local context) { auto* isolate = context->GetIsolate(); - auto binding_key = - mate::ConvertToV8(isolate, kIpcKey)->ToString(context).ToLocalChecked(); + auto binding_key = gin::StringToV8(isolate, kIpcKey); auto private_binding_key = v8::Private::ForApi(isolate, binding_key); auto global_object = context->Global(); auto value = @@ -64,7 +61,7 @@ void InvokeIpcCallback(v8::Local context, callback_scope.reset(new node::CallbackScope(isolate, ipcNative, {0, 0})); } - auto callback_key = mate::ConvertToV8(isolate, callback_name) + auto callback_key = gin::ConvertToV8(isolate, callback_name) ->ToString(context) .ToLocalChecked(); auto callback_value = ipcNative->Get(context, callback_key).ToLocalChecked(); @@ -86,8 +83,8 @@ void EmitIPCEvent(v8::Local context, v8::MicrotasksScope::kRunMicrotasks); std::vector> argv = { - mate::ConvertToV8(isolate, internal), mate::ConvertToV8(isolate, channel), - mate::ConvertToV8(isolate, args), mate::ConvertToV8(isolate, sender_id)}; + gin::ConvertToV8(isolate, internal), gin::ConvertToV8(isolate, channel), + gin::ConvertToV8(isolate, args), gin::ConvertToV8(isolate, sender_id)}; InvokeIpcCallback(context, "onMessage", argv); } diff --git a/shell/renderer/web_worker_observer.cc b/shell/renderer/web_worker_observer.cc index f328b34d1..30e1bfcfa 100644 --- a/shell/renderer/web_worker_observer.cc +++ b/shell/renderer/web_worker_observer.cc @@ -7,8 +7,8 @@ #include "base/lazy_instance.h" #include "base/threading/thread_local.h" #include "shell/common/api/electron_bindings.h" -#include "shell/common/api/event_emitter_caller.h" #include "shell/common/asar/asar_util.h" +#include "shell/common/gin_helper/event_emitter_caller.h" #include "shell/common/node_bindings.h" #include "shell/common/node_includes.h" @@ -69,7 +69,7 @@ void WebWorkerObserver::ContextCreated(v8::Local worker_context) { void WebWorkerObserver::ContextWillDestroy(v8::Local context) { node::Environment* env = node::Environment::GetCurrent(context); if (env) - mate::EmitEvent(env->isolate(), env->process_object(), "exit"); + gin_helper::EmitEvent(env->isolate(), env->process_object(), "exit"); delete this; }