Skip to content

Commit

Permalink
fix: global shortcut media keys
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jun 16, 2020
1 parent 7a216a8 commit ff45ef9
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,54 @@ The new kGlobalRequiresAccessibility Scope type should be upstreamed
and then we can use that and minimize this patch to just the change in
global_shortcut_listener_mac.mm

diff --git a/chrome/browser/extensions/global_shortcut_listener.cc b/chrome/browser/extensions/global_shortcut_listener.cc
index bc009606d01469125052e68a9cdc82aaa697c764..ff18043cb07d748a49adea9874517fb29e3e7f9f 100644
--- a/chrome/browser/extensions/global_shortcut_listener.cc
+++ b/chrome/browser/extensions/global_shortcut_listener.cc
@@ -7,6 +7,7 @@
#include "base/check.h"
#include "base/notreached.h"
#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/media_keys_listener_manager.h"
#include "ui/base/accelerators/accelerator.h"

using content::BrowserThread;
@@ -66,6 +67,22 @@ void GlobalShortcutListener::UnregisterAccelerator(
StopListening();
}

+// static
+void GlobalShortcutListener::SetShouldUseInternalMediaKeyHandling(bool should_use) {
+ if (content::MediaKeysListenerManager::
+ IsMediaKeysListenerManagerEnabled()) {
+ content::MediaKeysListenerManager* media_keys_listener_manager =
+ content::MediaKeysListenerManager::GetInstance();
+ DCHECK(media_keys_listener_manager);
+
+ if (should_use) {
+ media_keys_listener_manager->EnableInternalMediaKeyHandling();
+ } else {
+ media_keys_listener_manager->DisableInternalMediaKeyHandling();
+ }
+ }
+}
+
void GlobalShortcutListener::UnregisterAccelerators(Observer* observer) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (IsShortcutHandlingSuspended())
diff --git a/chrome/browser/extensions/global_shortcut_listener.h b/chrome/browser/extensions/global_shortcut_listener.h
index 9aec54a3263d24491d24013a80b719dfc834ecd4..001a6cb2a5eb701351fa924109b43fab6f30748d 100644
--- a/chrome/browser/extensions/global_shortcut_listener.h
+++ b/chrome/browser/extensions/global_shortcut_listener.h
@@ -31,6 +31,8 @@ class GlobalShortcutListener {

static GlobalShortcutListener* GetInstance();

+ static void SetShouldUseInternalMediaKeyHandling(bool should_use);
+
// Register an observer for when a certain |accelerator| is struck. Returns
// true if register successfully, or false if 1) the specificied |accelerator|
// has been registered by another caller or other native applications, or
diff --git a/chrome/browser/extensions/global_shortcut_listener_mac.mm b/chrome/browser/extensions/global_shortcut_listener_mac.mm
index befe726af9c10b1563a7fc0bb77cc55f65943d5c..bac51f33f35f96fe4ecc764cf5ca887176642f74 100644
--- a/chrome/browser/extensions/global_shortcut_listener_mac.mm
Expand All @@ -21,6 +69,26 @@ index befe726af9c10b1563a7fc0bb77cc55f65943d5c..bac51f33f35f96fe4ecc764cf5ca8871
DCHECK(media_keys_listener_);
}
}
diff --git a/content/browser/media/media_keys_listener_manager_impl.cc b/content/browser/media/media_keys_listener_manager_impl.cc
index 22fd524ae2996108fefdc3bb55a0f2b366310aa9..047fdce0bb57f1d78963200f907df147e174b978 100644
--- a/content/browser/media/media_keys_listener_manager_impl.cc
+++ b/content/browser/media/media_keys_listener_manager_impl.cc
@@ -177,8 +177,14 @@ void MediaKeysListenerManagerImpl::EnsureMediaKeysListener() {

EnsureAuxiliaryServices();

- media_keys_listener_ = ui::MediaKeysListener::Create(
+ if (!media_key_handling_enabled_) {
+ media_keys_listener_ = ui::MediaKeysListener::Create(
+ this, ui::MediaKeysListener::Scope::kGlobalRequiresAccessibility);
+ } else {
+ media_keys_listener_ = ui::MediaKeysListener::Create(
this, ui::MediaKeysListener::Scope::kGlobal);
+ }
+
DCHECK(media_keys_listener_);

media_keys_listener_->SetIsMediaPlaying(is_media_playing_);
diff --git a/ui/base/accelerators/media_keys_listener.h b/ui/base/accelerators/media_keys_listener.h
index 6787fa39da18ec26c215e4cbe0b3f69093323f8c..ec10c46cde437a935edfdf79e5ba9622c6ba1d67 100644
--- a/ui/base/accelerators/media_keys_listener.h
Expand Down
26 changes: 24 additions & 2 deletions shell/browser/api/electron_api_global_shortcut.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/extensions/command.h"
#include "gin/dictionary.h"
#include "gin/object_template_builder.h"
#include "shell/browser/api/electron_api_system_preferences.h"
Expand All @@ -20,6 +21,7 @@
#include "base/mac/mac_util.h"
#endif

using extensions::Command;
using extensions::GlobalShortcutListener;

namespace {
Expand All @@ -43,6 +45,15 @@ bool RegisteringMediaKeyForUntrustedClient(const ui::Accelerator& accelerator) {
}
return false;
}

bool MapHasMediaKeys(
const std::map<ui::Accelerator, base::Closure>& 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();
}
#endif

} // namespace
Expand Down Expand Up @@ -90,9 +101,13 @@ bool GlobalShortcut::RegisterAll(
bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
const base::Closure& callback) {
#if defined(OS_MACOSX)
if (RegisteringMediaKeyForUntrustedClient(accelerator))
return false;
if (Command::IsMediaKey(accelerator)) {
if (RegisteringMediaKeyForUntrustedClient(accelerator))
return false;

GlobalShortcutListener::SetShouldUseInternalMediaKeyHandling(false);
#endif
}

if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(accelerator,
this)) {
Expand All @@ -107,6 +122,13 @@ void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
if (accelerator_callback_map_.erase(accelerator) == 0)
return;

#if defined(OS_MACOSX)
if (Command::IsMediaKey(accelerator) &&
!MapHasMediaKeys(accelerator_callback_map_)) {
GlobalShortcutListener::SetShouldUseInternalMediaKeyHandling(true);
}
#endif

GlobalShortcutListener::GetInstance()->UnregisterAccelerator(accelerator,
this);
}
Expand Down

0 comments on commit ff45ef9

Please sign in to comment.