Files
electron/shell/browser/api/electron_api_global_shortcut.cc
trop[bot] e3cabb6119 chore: bump chromium to 144.0.7547.0 (40-x-y) (#49146)
* chore: bump chromium in DEPS to 144.0.7543.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* Pass PipScreenCaptureCoordinatorProxy to ScreenCaptureKitDeviceMac

https://chromium-review.googlesource.com/c/chromium/src/+/7157590

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: update patches

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: update filenames.libcxx.gni

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7142359: Spanification of process_singleton_posix.cc

https: //chromium-review.googlesource.com/c/chromium/src/+/7142359
Co-Authored-By: Charles Kerr <70381+ckerr@users.noreply.github.com>

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Move logging::LoggingSettings to base/logging/logging_settings.h

https://chromium-review.googlesource.com/c/chromium/src/+/7173024

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 144.0.7545.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* 7159368: update PluginService API for sync GetPlugins

Upstream removed async PluginService APIs:
- GetPluginsAsync() removed, use synchronous GetPlugins()
- RegisterInternalPlugin() now takes single argument (remove add_at_beginning)
- RefreshPlugins() removed entirely

Updated ElectronPluginInfoHostImpl to use synchronous plugin loading and
simplified ElectronBrowserMainParts internal plugin registration.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159368
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159328
Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159056
(cherry picked from commit 88cdf50b0a)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7159184: add missing base/files/file_path.h include

Add explicit include for base/files/file_path.h in electron_browser_context.h.
After removal of superfluous Mojo includes from content headers, base::FilePath
is no longer transitively included via content/public/browser/browser_context.h.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7159184
(cherry picked from commit 6ca8ea03ec)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7126479: add ShouldForceRefreshTextCheckService parameter to SpellCheckClient

Upstream added a force-refresh parameter to WebTextCheckClient::RequestCheckingOfText
to bypass spell check cache. Add the new ShouldForceRefreshTextCheckService
parameter to SpellCheckClient's override (currently unused in Electron).

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7126479
(cherry picked from commit 879c0401c4)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7083663: remove fingerprinting_protection_ruleset_service override

Upstream deleted external references to Fingerprinting Protection Filter (FPF)
component. Remove the fingerprinting_protection_ruleset_service() override
from BrowserProcessImpl as the method no longer exists in the base class.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7083663
(cherry picked from commit 8350d152f9)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7155287: implement WebContentsView::GetSize and Resize

Upstream delegated WebContents::GetSize() and Resize() to WebContentsView,
making them pure virtual. Add const qualifier to GetSize() and implement
the Resize() override in OffScreenWebContentsView (no-op for offscreen).

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7155287
(cherry picked from commit 084eaa568e)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7184238: add OnUnconfirmedTapConvertedToTap override

Upstream added OnUnconfirmedTapConvertedToTap as a pure virtual method
to RenderWidgetHostViewBase to inform root view when child frame converts
an unconfirmed tap. Add empty override for offscreen rendering.

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/7184238
(cherry picked from commit ef03400d9a)

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* 7143586: add widget parameter to OnCommandsChanged

GlobalAcceleratorListener::OnCommandsChanged gained a gfx::AcceleratedWidget
parameter for window association in the GlobalShortcutListenerLinux
implementation. Pass gfx::kNullAcceleratedWidget for Electron's usage.

Ref: : Pass parent handle to GlobalAcceleratorListenerLinux::BindShortcuts | https://chromium-review.googlesource.com/c/chromium/src/+/7143586
(cherry picked from commit 38306e2bfc)
Co-Authored-By: Samuel Attard <MarshallOfSound@users.noreply.github.com>

Co-authored-by: Samuel Attard <sattard@anthropic.com>

* chore: update patches

Co-Authored-By: Samuel Attard <MarshallOfSound@users.noreply.github.com>

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 144.0.7547.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: update patches

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7189232: Add support for UnownedUserData in GlobalFeatures

https: //chromium-review.googlesource.com/c/chromium/src/+/7189232

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Refactor: Use std::u16string for extension load error messages

https://chromium-review.googlesource.com/c/chromium/src/+/7185844

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup Add support for UnownedUserData in GlobalFeatures

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7165650: Remove ResourceContext

https://chromium-review.googlesource.com/c/chromium/src/+/7165650

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup BUILD.gn for lint

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* 7202164: Reland "Reland "Remove GenericScopedHandle:IsValid""

https://chromium-review.googlesource.com/c/chromium/src/+/7202164

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* advance deprecation of v8::ReturnValue<void>::Set(Local<S>).

7168624: [runtime][api] Relax requirements for setter/definer/deleter callbacks | https://chromium-review.googlesource.com/c/v8/v8/+/7168624

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup advance deprecation of v8::ReturnValue<void>::Set(Local<S>)

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: skip setting LPAC ACLs

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Revert "chore: skip setting LPAC ACLs"

This reverts commit e187aec488.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* chore: revert Convert to UNSAFE_TODO in sandbox

revert https://chromium-review.googlesource.com/c/chromium/src/+/7131661 to see if it fixes the Windows sandbox issue.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* Revert "chore: revert Convert to UNSAFE_TODO in sandbox"

This reverts commit 57afbfefe5.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* chore: Revert "Enable network sandbox by default on Windows"

see if this fixes the Windows sandbox issue

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* Enable network sandbox by default on Windows

https://chromium-review.googlesource.com/c/chromium/src/+/7204292

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* Revert "chore: Revert "Enable network sandbox by default on Windows""

This reverts commit 530ab6af82.

Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>

* fixup! Enable network sandbox by default on Windows | https://chromium-review.googlesource.com/c/chromium/src/+/7204292

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* fixup!: Correct flag name, add kLocalNetworkAccessChecks to all platforms

Co-authored-by: Keeley Hammond <khammond@slack-corp.com>

* chore: remove patch that hasn't been backported

See https://github.com/electron/electron/pull/48675

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Samuel Attard <sattard@anthropic.com>
Co-authored-by: John Kleinschmidt <kleinschmidtorama@gmail.com>
Co-authored-by: Keeley Hammond <khammond@slack-corp.com>
Co-authored-by: Niklas Wenzel <dev@nikwen.de>
2025-12-05 12:18:13 -05:00

260 lines
8.8 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/map_util.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/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/gin_helper/handle.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::DeprecatedWrapperInfo GlobalShortcut::kWrapperInfo = {
gin::kEmbedderNativeGin};
GlobalShortcut::GlobalShortcut() {}
GlobalShortcut::~GlobalShortcut() {
UnregisterAll();
}
void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
if (auto* cb = base::FindOrNull(accelerator_callback_map_, accelerator)) {
cb->Run();
} else {
// This should never occur, because if it does,
// ui::GlobalAcceleratorListener notifies us with wrong accelerator.
NOTREACHED();
}
}
void GlobalShortcut::ExecuteCommand(const extensions::ExtensionId& extension_id,
const std::string& command_id) {
if (auto* cb = base::FindOrNull(command_callback_map_, command_id)) {
cb->Run();
} else {
// This should never occur, because if it does, GlobalAcceleratorListener
// notifies us with wrong command.
NOTREACHED();
}
}
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::GetDefaultBrowserContext();
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,
gfx::kNullAcceleratedWidget, 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 (accelerator_callback_map_.contains(accelerator)) {
return true;
}
const std::string command_str =
extensions::Command::AcceleratorToString(accelerator);
return command_callback_map_.contains(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_helper::Handle<GlobalShortcut> GlobalShortcut::Create(
v8::Isolate* isolate) {
return gin_helper::CreateHandle(isolate, new GlobalShortcut());
}
// static
gin::ObjectTemplateBuilder GlobalShortcut::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::DeprecatedWrappable<
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* const isolate = electron::JavascriptEnvironment::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)