From cbc15fd7e1b3c9e8e12530d0f95f43fb065b4164 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Mon, 14 Dec 2020 10:49:41 -0800 Subject: [PATCH 1/2] fix: throw when using globalShortcut before ready --- docs/api/global-shortcut.md | 2 +- .../api/electron_api_global_shortcut.cc | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/api/global-shortcut.md b/docs/api/global-shortcut.md index 78263901761d3..bb9dd27fbd37b 100644 --- a/docs/api/global-shortcut.md +++ b/docs/api/global-shortcut.md @@ -9,7 +9,7 @@ with the operating system so that you can customize the operations for various shortcuts. **Note:** The shortcut is global; it will work even if the app does -not have the keyboard focus. You should not use this module until the `ready` +not have the keyboard focus. This module cannot be used before the `ready` event of the app module is emitted. ```javascript diff --git a/shell/browser/api/electron_api_global_shortcut.cc b/shell/browser/api/electron_api_global_shortcut.cc index 2ae8af1c811d2..0bd4482bc5da2 100644 --- a/shell/browser/api/electron_api_global_shortcut.cc +++ b/shell/browser/api/electron_api_global_shortcut.cc @@ -84,6 +84,11 @@ void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) { bool GlobalShortcut::RegisterAll( const std::vector& accelerators, const base::Closure& callback) { + if (!Browser::Get()->is_ready()) { + gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) + .ThrowError("globalShortcut cannot be used before the app is ready"); + return false; + } std::vector registered; for (auto& accelerator : accelerators) { @@ -100,6 +105,11 @@ bool GlobalShortcut::RegisterAll( bool GlobalShortcut::Register(const ui::Accelerator& accelerator, const base::Closure& callback) { + if (!Browser::Get()->is_ready()) { + gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) + .ThrowError("globalShortcut cannot be used before the app is ready"); + return false; + } #if defined(OS_MAC) if (Command::IsMediaKey(accelerator)) { if (RegisteringMediaKeyForUntrustedClient(accelerator)) @@ -119,6 +129,11 @@ bool GlobalShortcut::Register(const ui::Accelerator& accelerator, } void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) { + if (!Browser::Get()->is_ready()) { + gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) + .ThrowError("globalShortcut cannot be used before the app is ready"); + return false; + } if (accelerator_callback_map_.erase(accelerator) == 0) return; @@ -145,6 +160,11 @@ bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) { } void GlobalShortcut::UnregisterAll() { + if (!Browser::Get()->is_ready()) { + gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) + .ThrowError("globalShortcut cannot be used before the app is ready"); + return false; + } accelerator_callback_map_.clear(); GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this); } From 1c1bb714ff31c6a27171bed58ec457474ed6a447 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Mon, 14 Dec 2020 11:25:09 -0800 Subject: [PATCH 2/2] fix --- shell/browser/api/electron_api_global_shortcut.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/shell/browser/api/electron_api_global_shortcut.cc b/shell/browser/api/electron_api_global_shortcut.cc index 0bd4482bc5da2..597511be6e579 100644 --- a/shell/browser/api/electron_api_global_shortcut.cc +++ b/shell/browser/api/electron_api_global_shortcut.cc @@ -13,6 +13,7 @@ #include "gin/dictionary.h" #include "gin/object_template_builder.h" #include "shell/browser/api/electron_api_system_preferences.h" +#include "shell/browser/browser.h" #include "shell/common/gin_converters/accelerator_converter.h" #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/node_includes.h" @@ -84,7 +85,7 @@ void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) { bool GlobalShortcut::RegisterAll( const std::vector& accelerators, const base::Closure& callback) { - if (!Browser::Get()->is_ready()) { + if (!electron::Browser::Get()->is_ready()) { gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) .ThrowError("globalShortcut cannot be used before the app is ready"); return false; @@ -105,7 +106,7 @@ bool GlobalShortcut::RegisterAll( bool GlobalShortcut::Register(const ui::Accelerator& accelerator, const base::Closure& callback) { - if (!Browser::Get()->is_ready()) { + if (!electron::Browser::Get()->is_ready()) { gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) .ThrowError("globalShortcut cannot be used before the app is ready"); return false; @@ -129,10 +130,10 @@ bool GlobalShortcut::Register(const ui::Accelerator& accelerator, } void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) { - if (!Browser::Get()->is_ready()) { + if (!electron::Browser::Get()->is_ready()) { gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) .ThrowError("globalShortcut cannot be used before the app is ready"); - return false; + return; } if (accelerator_callback_map_.erase(accelerator) == 0) return; @@ -160,10 +161,10 @@ bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) { } void GlobalShortcut::UnregisterAll() { - if (!Browser::Get()->is_ready()) { + if (!electron::Browser::Get()->is_ready()) { gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate()) .ThrowError("globalShortcut cannot be used before the app is ready"); - return false; + return; } accelerator_callback_map_.clear(); GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this);