2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-07-31 04:22:29 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_global_shortcut.h"
|
2014-07-31 04:22:29 +00:00
|
|
|
|
|
|
|
#include <string>
|
2018-11-07 17:40:38 +00:00
|
|
|
#include <vector>
|
2014-07-31 04:22:29 +00:00
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
#include "base/stl_util.h"
|
2019-01-04 04:40:17 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2020-03-19 21:33:45 +00:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/object_template_builder.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_system_preferences.h"
|
2020-12-15 19:13:56 +00:00
|
|
|
#include "shell/browser/browser.h"
|
2019-10-25 13:03:28 +00:00
|
|
|
#include "shell/common/gin_converters/accelerator_converter.h"
|
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2014-07-31 04:22:29 +00:00
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2019-01-04 04:40:17 +00:00
|
|
|
#include "base/mac/mac_util.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
using extensions::GlobalShortcutListener;
|
|
|
|
|
2019-01-04 04:40:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2019-01-04 04:40:17 +00:00
|
|
|
bool RegisteringMediaKeyForUntrustedClient(const ui::Accelerator& accelerator) {
|
|
|
|
if (base::mac::IsAtLeastOS10_14()) {
|
|
|
|
constexpr ui::KeyboardCode mediaKeys[] = {
|
|
|
|
ui::VKEY_MEDIA_PLAY_PAUSE, ui::VKEY_MEDIA_NEXT_TRACK,
|
2020-05-28 12:56:48 +00:00
|
|
|
ui::VKEY_MEDIA_PREV_TRACK, ui::VKEY_MEDIA_STOP,
|
|
|
|
ui::VKEY_VOLUME_UP, ui::VKEY_VOLUME_DOWN,
|
|
|
|
ui::VKEY_VOLUME_MUTE};
|
2019-01-04 04:40:17 +00:00
|
|
|
|
|
|
|
if (std::find(std::begin(mediaKeys), std::end(mediaKeys),
|
|
|
|
accelerator.key_code()) != std::end(mediaKeys)) {
|
|
|
|
bool trusted =
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::api::SystemPreferences::IsTrustedAccessibilityClient(false);
|
2019-01-04 04:40:17 +00:00
|
|
|
if (!trusted)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2014-07-31 04:22:29 +00:00
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2020-03-19 21:33:45 +00:00
|
|
|
gin::WrapperInfo GlobalShortcut::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
|
|
GlobalShortcut::GlobalShortcut(v8::Isolate* isolate) {}
|
2014-07-31 04:22:29 +00:00
|
|
|
|
|
|
|
GlobalShortcut::~GlobalShortcut() {
|
|
|
|
UnregisterAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
|
|
|
|
if (accelerator_callback_map_.find(accelerator) ==
|
|
|
|
accelerator_callback_map_.end()) {
|
2020-05-28 12:56:48 +00:00
|
|
|
// This should never occur, because if it does, GlobalShortcutListener
|
2019-01-04 04:40:17 +00:00
|
|
|
// notifies us with wrong accelerator.
|
2014-07-31 04:22:29 +00:00
|
|
|
NOTREACHED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
accelerator_callback_map_[accelerator].Run();
|
|
|
|
}
|
|
|
|
|
2018-11-07 17:40:38 +00:00
|
|
|
bool GlobalShortcut::RegisterAll(
|
|
|
|
const std::vector<ui::Accelerator>& accelerators,
|
2021-05-06 22:01:04 +00:00
|
|
|
const base::RepeatingClosure& callback) {
|
2020-12-15 19:13:56 +00:00
|
|
|
if (!electron::Browser::Get()->is_ready()) {
|
|
|
|
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
|
|
|
|
.ThrowError("globalShortcut cannot be used before the app is ready");
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-07 17:40:38 +00:00
|
|
|
std::vector<ui::Accelerator> registered;
|
2019-01-04 04:40:17 +00:00
|
|
|
|
2018-11-07 17:40:38 +00:00
|
|
|
for (auto& accelerator : accelerators) {
|
2019-11-05 21:47:23 +00:00
|
|
|
if (!Register(accelerator, callback)) {
|
2018-11-07 17:40:38 +00:00
|
|
|
// unregister all shortcuts if any failed
|
|
|
|
UnregisterSome(registered);
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-05 21:47:23 +00:00
|
|
|
|
2018-11-07 17:40:38 +00:00
|
|
|
registered.push_back(accelerator);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
|
2021-05-06 22:01:04 +00:00
|
|
|
const base::RepeatingClosure& callback) {
|
2020-12-15 19:13:56 +00:00
|
|
|
if (!electron::Browser::Get()->is_ready()) {
|
|
|
|
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
|
|
|
|
.ThrowError("globalShortcut cannot be used before the app is ready");
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-12 18:33:58 +00:00
|
|
|
#if defined(OS_MAC)
|
2021-02-09 20:16:21 +00:00
|
|
|
if (RegisteringMediaKeyForUntrustedClient(accelerator))
|
|
|
|
return false;
|
2019-01-04 04:40:17 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(accelerator,
|
|
|
|
this)) {
|
2014-07-31 04:22:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 16:00:39 +00:00
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
accelerator_callback_map_[accelerator] = callback;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
|
2020-12-15 19:13:56 +00:00
|
|
|
if (!electron::Browser::Get()->is_ready()) {
|
|
|
|
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
|
|
|
|
.ThrowError("globalShortcut cannot be used before the app is ready");
|
|
|
|
return;
|
|
|
|
}
|
2019-08-28 14:39:21 +00:00
|
|
|
if (accelerator_callback_map_.erase(accelerator) == 0)
|
2014-07-31 04:22:29 +00:00
|
|
|
return;
|
2014-08-04 16:00:39 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
GlobalShortcutListener::GetInstance()->UnregisterAccelerator(accelerator,
|
|
|
|
this);
|
2014-07-31 04:22:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 17:40:38 +00:00
|
|
|
void GlobalShortcut::UnregisterSome(
|
|
|
|
const std::vector<ui::Accelerator>& accelerators) {
|
|
|
|
for (auto& accelerator : accelerators) {
|
|
|
|
Unregister(accelerator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
|
2019-07-03 01:22:09 +00:00
|
|
|
return base::Contains(accelerator_callback_map_, accelerator);
|
2014-08-04 16:00:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
void GlobalShortcut::UnregisterAll() {
|
2020-12-15 19:13:56 +00:00
|
|
|
if (!electron::Browser::Get()->is_ready()) {
|
|
|
|
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
|
|
|
|
.ThrowError("globalShortcut cannot be used before the app is ready");
|
|
|
|
return;
|
|
|
|
}
|
2014-07-31 04:22:29 +00:00
|
|
|
accelerator_callback_map_.clear();
|
|
|
|
GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this);
|
|
|
|
}
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// static
|
2019-10-25 13:03:28 +00:00
|
|
|
gin::Handle<GlobalShortcut> GlobalShortcut::Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new GlobalShortcut(isolate));
|
2016-04-25 01:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2020-03-19 21:33:45 +00:00
|
|
|
gin::ObjectTemplateBuilder GlobalShortcut::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin::Wrappable<GlobalShortcut>::GetObjectTemplateBuilder(isolate)
|
2018-11-07 17:40:38 +00:00
|
|
|
.SetMethod("registerAll", &GlobalShortcut::RegisterAll)
|
2014-08-22 02:53:37 +00:00
|
|
|
.SetMethod("register", &GlobalShortcut::Register)
|
|
|
|
.SetMethod("isRegistered", &GlobalShortcut::IsRegistered)
|
|
|
|
.SetMethod("unregister", &GlobalShortcut::Unregister)
|
|
|
|
.SetMethod("unregisterAll", &GlobalShortcut::UnregisterAll);
|
2014-07-31 04:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:33:45 +00:00
|
|
|
const char* GlobalShortcut::GetTypeName() {
|
|
|
|
return "GlobalShortcut";
|
|
|
|
}
|
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2014-07-31 04:22:29 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2014-07-31 04:22:29 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2020-03-19 21:33:45 +00:00
|
|
|
gin::Dictionary dict(isolate, exports);
|
2019-06-19 21:23:04 +00:00
|
|
|
dict.Set("globalShortcut", electron::api::GlobalShortcut::Create(isolate));
|
2014-07-31 04:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_global_shortcut, Initialize)
|