fix: throw when using globalShortcut before ready (#27002)

This commit is contained in:
Jeremy Rose 2020-12-15 11:13:56 -08:00 committed by GitHub
parent ef49fea5eb
commit 833baa4c54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View file

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

View file

@ -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,6 +85,11 @@ void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
bool GlobalShortcut::RegisterAll(
const std::vector<ui::Accelerator>& accelerators,
const base::Closure& callback) {
if (!electron::Browser::Get()->is_ready()) {
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
.ThrowError("globalShortcut cannot be used before the app is ready");
return false;
}
std::vector<ui::Accelerator> registered;
for (auto& accelerator : accelerators) {
@ -100,6 +106,11 @@ bool GlobalShortcut::RegisterAll(
bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
const base::Closure& callback) {
if (!electron::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 +130,11 @@ bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
}
void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
if (!electron::Browser::Get()->is_ready()) {
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
.ThrowError("globalShortcut cannot be used before the app is ready");
return;
}
if (accelerator_callback_map_.erase(accelerator) == 0)
return;
@ -145,6 +161,11 @@ bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
}
void GlobalShortcut::UnregisterAll() {
if (!electron::Browser::Get()->is_ready()) {
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
.ThrowError("globalShortcut cannot be used before the app is ready");
return;
}
accelerator_callback_map_.clear();
GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this);
}