![electron-roller[bot]](/assets/img/avatar_default.png)
* chore: bump chromium in DEPS to 134.0.6948.0 * chore: update can_create_window.patch6151982
no patch code changes, but had to manually apply due to upstream context shear * chore: update proxy_config_monitor.patch no manual changes; patch applied with fuzz 2 Xref:6126219
* chore: update build_add_electron_tracing_category.patch Xref:6149256
* chore: update adjust_accessibility_ui_for_electron.patch6105650
no patch code changes, but had to manually apply due to upstream context shear * chore: e patches all * chore: use fully-qualified path for all.gn Xref:6154997
* chore: do not use a variable when assigning rtc_use_h264 Xref:6154997
* Move GlobalShortcutListenerLinux to //ui/base Xref:6097375
* [MPArch Guest View] Make WebPreferences queried per frame tree root Xref:6096390
* [Status Icons] Allow vector resources6139403
* [Extensions] Move MatchOriginAsFallbackBehavior to Mojom Xref:6141793
* Remove StrongAlias::Hasher Xref:6132291
* Rename text-change and select-change methods and related stuff Xref:6148816
* [Code Health] Remove stale feature EnableWebUsbOnExtensionServiceWorker6115161
* [Extensions Cleanup] Move creation of tab-based ports to factory method6143725
* refactor: add StatusIconGtk::SetIcon() Xref:6139403
copied from chrome/browser/status_icons/status_icon.cc * refactor: add TrayIconLinux::GetIcon() Xref:6139403
* chore: update feat_allow_usage_of_sccontentsharingpicker_on_supported_platforms.patch remove unused filter_ field * chore: bump chromium in DEPS to 134.0.6950.0 * chore: bump chromium in DEPS to 134.0.6952.0 * chore: bump chromium in DEPS to 134.0.6954.0 * chore: bump chromium in DEPS to 134.0.6956.0 * chore: update Chromium patches * 6165749: Check scanout support in RenderableGpuMemoryBufferPool |6165749
* 6106730: [Win] Use DXGI swapchains and DCOMP visuals in software mode |6106730
* chore: update patches * chore: bump chromium in DEPS to 134.0.6958.0 * chore: bump chromium in DEPS to 134.0.6960.0 * chore: update chromium patches * 6168371: Remove extensions GlobalShortcutListener wrapper |6168371
* chore: update patches * 6161637: WebUI: Leverage build_webui() in chrome://translate-internals |6161637
* chore: bump chromium in DEPS to 134.0.6962.0 * 6177329: Remove policy.used_policy_certificates pref on ChromeOS |6177329
* 6180524: Simplify logic in components/os_crypt/sync/BUILD.gn |6180524
* 6144831: Enforce --disallow-v8-feature-flag-overrides in the renderer |6144831
* chore: update patches * chore: bump chromium in DEPS to 134.0.6964.0 * 6181010: Ensure busy cursor does not show via LaunchWithoutSandbox |6181010
* chore: update patches * chore: bump chromium in DEPS to 134.0.6966.0 * 6180598: [api] Remove Reallocate |6180598
* 6170781: [Refactor] Move UninstallExtension to ExtensionRegistrar. |6170781
* chore: update filenames.libcxx.gni * 6168207: cdm: Remove widevine_cdm_version.h |6168207
* chore: bump chromium in DEPS to 134.0.6968.0 * 6030552: [macOS] Allow using vibrancy with NativeWidgetNSWindowBridge |6030552
* fix: use explicit copy to replace realloc impl6180598
https://issues.chromium.org/issues/331326406 As per recommendation, "File an issue with Node to explicitly copy,because they copy under the hood anyway" * fixup! 6106730: [Win] Use DXGI swapchains and DCOMP visuals in software mode |6106730
* fix: undefine win32 StrCat6172292
* fix: //device/vr:directx_helpers breaking the build6064548
Upstreamed in6186102
* fix: avoid calling ui::Layer::SetFillsBoundsOpaquely6175787
The layer opacity is determined by the background color's alpha value * fix: build with proprietary_codecs The explicit setting of rtc_use_h264 is no longer needed since https://webrtc-review.googlesource.com/c/src/+/62380 * fix: increase empty trace file size threshold6176642
Traces now contain a net-constants property to allow them to be converted to a net log. These contain ~1240 new properties with formatted JSON data. * fix: node tests missing resource management globals6174695
* fixup! fix: use explicit copy to replace realloc impl * chore: disable focus handling test due to win32/ia32 regression --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: Keeley Hammond <khammond@slack-corp.com> Co-authored-by: VerteDinde <vertedinde@electronjs.org> Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> Co-authored-by: Samuel Maddock <samuelmaddock@electronjs.org>
253 lines
8.7 KiB
C++
253 lines
8.7 KiB
C++
// Copyright (c) 2014 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "shell/browser/api/electron_api_global_shortcut.h"
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "base/containers/contains.h"
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "base/uuid.h"
|
|
#include "components/prefs/pref_service.h"
|
|
#include "electron/shell/browser/electron_browser_context.h"
|
|
#include "electron/shell/common/electron_constants.h"
|
|
#include "extensions/common/command.h"
|
|
#include "gin/dictionary.h"
|
|
#include "gin/handle.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"
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
|
#include "base/mac/mac_util.h"
|
|
#endif
|
|
|
|
using extensions::Command;
|
|
using ui::GlobalAcceleratorListener;
|
|
|
|
namespace {
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
|
bool RegisteringMediaKeyForUntrustedClient(const ui::Accelerator& accelerator) {
|
|
return accelerator.IsMediaKey() &&
|
|
!electron::api::SystemPreferences::IsTrustedAccessibilityClient(false);
|
|
}
|
|
|
|
bool MapHasMediaKeys(
|
|
const std::map<ui::Accelerator, base::RepeatingClosure>& accelerator_map) {
|
|
return std::ranges::any_of(
|
|
accelerator_map, [](const auto& ac) { return ac.first.IsMediaKey(); });
|
|
}
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
namespace electron::api {
|
|
|
|
gin::WrapperInfo GlobalShortcut::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
GlobalShortcut::GlobalShortcut(v8::Isolate* isolate) {}
|
|
|
|
GlobalShortcut::~GlobalShortcut() {
|
|
UnregisterAll();
|
|
}
|
|
|
|
void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
|
|
if (!base::Contains(accelerator_callback_map_, accelerator)) {
|
|
// This should never occur, because if it does,
|
|
// ui::GlobalAcceleratorListener notifies us with wrong accelerator.
|
|
NOTREACHED();
|
|
}
|
|
accelerator_callback_map_[accelerator].Run();
|
|
}
|
|
|
|
void GlobalShortcut::ExecuteCommand(const extensions::ExtensionId& extension_id,
|
|
const std::string& command_id) {
|
|
if (!base::Contains(command_callback_map_, command_id)) {
|
|
// This should never occur, because if it does, GlobalAcceleratorListener
|
|
// notifies us with wrong command.
|
|
NOTREACHED();
|
|
}
|
|
command_callback_map_[command_id].Run();
|
|
}
|
|
|
|
bool GlobalShortcut::RegisterAll(
|
|
const std::vector<ui::Accelerator>& accelerators,
|
|
const base::RepeatingClosure& 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) {
|
|
if (!Register(accelerator, callback)) {
|
|
// Unregister all shortcuts if any failed.
|
|
UnregisterSome(registered);
|
|
return false;
|
|
}
|
|
|
|
registered.push_back(accelerator);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
|
|
const base::RepeatingClosure& 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 BUILDFLAG(IS_MAC)
|
|
if (accelerator.IsMediaKey()) {
|
|
if (RegisteringMediaKeyForUntrustedClient(accelerator))
|
|
return false;
|
|
|
|
ui::GlobalAcceleratorListener::SetShouldUseInternalMediaKeyHandling(false);
|
|
}
|
|
#endif
|
|
|
|
auto* instance = ui::GlobalAcceleratorListener::GetInstance();
|
|
if (!instance) {
|
|
return false;
|
|
}
|
|
|
|
if (instance->IsRegistrationHandledExternally()) {
|
|
auto* context = ElectronBrowserContext::From("", false);
|
|
PrefService* prefs = context->prefs();
|
|
|
|
// Need a unique profile id. Set one if not generated yet, otherwise re-use
|
|
// the same so that the session for the globalShortcuts is able to get
|
|
// already registered shortcuts from the previous session. This will be used
|
|
// by GlobalAcceleratorListenerLinux as a session key.
|
|
std::string profile_id = prefs->GetString(kElectronGlobalShortcutsUuid);
|
|
if (profile_id.empty()) {
|
|
profile_id = base::Uuid::GenerateRandomV4().AsLowercaseString();
|
|
prefs->SetString(kElectronGlobalShortcutsUuid, profile_id);
|
|
}
|
|
|
|
// There is no way to get command id for the accelerator as it's extensions'
|
|
// thing. Instead, we can convert it to string in a following example form
|
|
// - std::string("Alt+Shift+K"). That must be sufficient enough for us to
|
|
// map this accelerator with registered commands.
|
|
const std::string command_str =
|
|
extensions::Command::AcceleratorToString(accelerator);
|
|
ui::CommandMap commands;
|
|
extensions::Command command(
|
|
command_str, base::UTF8ToUTF16("Electron shortcut " + command_str),
|
|
/*accelerator=*/std::string(), /*global=*/true);
|
|
command.set_accelerator(accelerator);
|
|
commands[command_str] = command;
|
|
|
|
// In order to distinguish the shortcuts, we must register multiple commands
|
|
// as different extensions. Otherwise, each shortcut will be an alternative
|
|
// for the very first registered and we'll not be able to distinguish them.
|
|
// For example, if Alt+Shift+K is registered first, registering and pressing
|
|
// Alt+Shift+M will trigger global shortcuts, but the command id that is
|
|
// received by GlobalShortcut will correspond to Alt+Shift+K as our command
|
|
// id is basically a stringified accelerator.
|
|
const std::string fake_extension_id = command_str + "+" + profile_id;
|
|
instance->OnCommandsChanged(fake_extension_id, profile_id, commands, this);
|
|
command_callback_map_[command_str] = callback;
|
|
return true;
|
|
} else {
|
|
if (instance->RegisterAccelerator(accelerator, this)) {
|
|
accelerator_callback_map_[accelerator] = callback;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
|
|
#if BUILDFLAG(IS_MAC)
|
|
if (accelerator.IsMediaKey() && !MapHasMediaKeys(accelerator_callback_map_)) {
|
|
ui::GlobalAcceleratorListener::SetShouldUseInternalMediaKeyHandling(true);
|
|
}
|
|
#endif
|
|
|
|
if (ui::GlobalAcceleratorListener::GetInstance()) {
|
|
ui::GlobalAcceleratorListener::GetInstance()->UnregisterAccelerator(
|
|
accelerator, this);
|
|
}
|
|
}
|
|
|
|
void GlobalShortcut::UnregisterSome(
|
|
const std::vector<ui::Accelerator>& accelerators) {
|
|
for (auto& accelerator : accelerators) {
|
|
Unregister(accelerator);
|
|
}
|
|
}
|
|
|
|
bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
|
|
if (base::Contains(accelerator_callback_map_, accelerator)) {
|
|
return true;
|
|
}
|
|
const std::string command_str =
|
|
extensions::Command::AcceleratorToString(accelerator);
|
|
return base::Contains(command_callback_map_, command_str);
|
|
}
|
|
|
|
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();
|
|
if (ui::GlobalAcceleratorListener::GetInstance()) {
|
|
ui::GlobalAcceleratorListener::GetInstance()->UnregisterAccelerators(this);
|
|
}
|
|
}
|
|
|
|
// static
|
|
gin::Handle<GlobalShortcut> GlobalShortcut::Create(v8::Isolate* isolate) {
|
|
return gin::CreateHandle(isolate, new GlobalShortcut(isolate));
|
|
}
|
|
|
|
// static
|
|
gin::ObjectTemplateBuilder GlobalShortcut::GetObjectTemplateBuilder(
|
|
v8::Isolate* isolate) {
|
|
return gin::Wrappable<GlobalShortcut>::GetObjectTemplateBuilder(isolate)
|
|
.SetMethod("registerAll", &GlobalShortcut::RegisterAll)
|
|
.SetMethod("register", &GlobalShortcut::Register)
|
|
.SetMethod("isRegistered", &GlobalShortcut::IsRegistered)
|
|
.SetMethod("unregister", &GlobalShortcut::Unregister)
|
|
.SetMethod("unregisterAll", &GlobalShortcut::UnregisterAll);
|
|
}
|
|
|
|
const char* GlobalShortcut::GetTypeName() {
|
|
return "GlobalShortcut";
|
|
}
|
|
|
|
} // namespace electron::api
|
|
|
|
namespace {
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
v8::Local<v8::Value> unused,
|
|
v8::Local<v8::Context> context,
|
|
void* priv) {
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
gin::Dictionary dict(isolate, exports);
|
|
dict.Set("globalShortcut", electron::api::GlobalShortcut::Create(isolate));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_global_shortcut, Initialize)
|