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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/api/electron_api_global_shortcut.h"
|
2014-07-31 04:22:29 +00:00
|
|
|
|
2018-11-07 17:40:38 +00:00
|
|
|
#include <vector>
|
2014-07-31 04:22:29 +00:00
|
|
|
|
2021-06-03 08:05:04 +00:00
|
|
|
#include "base/containers/contains.h"
|
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"
|
2022-06-01 06:12:47 +00:00
|
|
|
#include "extensions/common/command.h"
|
2020-03-19 21:33:45 +00:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/object_template_builder.h"
|
2019-06-19 20:46:59 +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
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-01-04 04:40:17 +00:00
|
|
|
#include "base/mac/mac_util.h"
|
|
|
|
#endif
|
|
|
|
|
2021-10-21 19:39:38 +00:00
|
|
|
using extensions::Command;
|
2014-07-31 04:22:29 +00:00
|
|
|
using extensions::GlobalShortcutListener;
|
|
|
|
|
2019-01-04 04:40:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2019-01-04 04:40:17 +00:00
|
|
|
bool RegisteringMediaKeyForUntrustedClient(const ui::Accelerator& accelerator) {
|
2023-07-01 20:22:55 +00:00
|
|
|
return Command::IsMediaKey(accelerator) &&
|
|
|
|
!electron::api::SystemPreferences::IsTrustedAccessibilityClient(false);
|
2019-01-04 04:40:17 +00:00
|
|
|
}
|
2021-10-21 19:39:38 +00:00
|
|
|
|
|
|
|
bool MapHasMediaKeys(
|
|
|
|
const std::map<ui::Accelerator, base::RepeatingClosure>& accelerator_map) {
|
|
|
|
auto media_key = std::find_if(
|
|
|
|
accelerator_map.begin(), accelerator_map.end(),
|
|
|
|
[](const auto& ac) { return Command::IsMediaKey(ac.first); });
|
|
|
|
|
|
|
|
return media_key != accelerator_map.end();
|
|
|
|
}
|
2019-01-04 04:40:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2014-07-31 04:22:29 +00:00
|
|
|
|
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) {
|
2023-05-30 08:28:43 +00:00
|
|
|
if (!base::Contains(accelerator_callback_map_, accelerator)) {
|
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)) {
|
2021-10-21 19:39:38 +00:00
|
|
|
// Unregister all shortcuts if any failed.
|
2018-11-07 17:40:38 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2021-10-21 19:39:38 +00:00
|
|
|
if (Command::IsMediaKey(accelerator)) {
|
|
|
|
if (RegisteringMediaKeyForUntrustedClient(accelerator))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
GlobalShortcutListener::SetShouldUseInternalMediaKeyHandling(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
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2021-10-21 19:39:38 +00:00
|
|
|
if (Command::IsMediaKey(accelerator) &&
|
|
|
|
!MapHasMediaKeys(accelerator_callback_map_)) {
|
|
|
|
GlobalShortcutListener::SetShouldUseInternalMediaKeyHandling(true);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|
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);
|
2014-07-31 04:22:29 +00:00
|
|
|
dict.Set("globalShortcut", electron::api::GlobalShortcut::Create(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-02-09 01:31:38 +00:00
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_global_shortcut, Initialize)
|