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 8412aae commit 8cd87f1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,83 @@ 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/content/browser/media/media_keys_listener_manager_impl.cc b/content/browser/media/media_keys_listener_manager_impl.cc
index 22fd524ae2996108fefdc3bb55a0f2b366310aa9..2a1fa620993aecde593de7c58c8a4688eb6181e0 100644
--- a/content/browser/media/media_keys_listener_manager_impl.cc
+++ b/content/browser/media/media_keys_listener_manager_impl.cc
@@ -15,6 +15,12 @@
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/idle/idle.h"

+namespace {
+ // False if media key handling has been explicitly disabled by a call to
+ // |DisableInternalMediaKeyHandling()|.
+ bool media_key_handling_enabled_ = true;
+}
+
namespace content {

MediaKeysListenerManagerImpl::ListeningData::ListeningData()
@@ -33,7 +39,6 @@ MediaKeysListenerManager* MediaKeysListenerManager::GetInstance() {
MediaKeysListenerManagerImpl::MediaKeysListenerManagerImpl()
: hardware_key_media_controller_(
std::make_unique<HardwareKeyMediaController>()),
- media_key_handling_enabled_(true),
auxiliary_services_started_(false) {
DCHECK(!MediaKeysListenerManager::GetInstance());
}
@@ -110,6 +115,11 @@ void MediaKeysListenerManagerImpl::EnableInternalMediaKeyHandling() {
UpdateKeyListening();
}

+// static
+void MediaKeysListenerManagerImpl::SetShouldUseInternalMediaKeyHandling(bool should_use) {
+ media_key_handling_enabled_ = should_use;
+}
+
void MediaKeysListenerManagerImpl::OnMediaKeysAccelerator(
const ui::Accelerator& accelerator) {
// We should never receive an accelerator that was never registered.
@@ -177,8 +187,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/content/browser/media/media_keys_listener_manager_impl.h b/content/browser/media/media_keys_listener_manager_impl.h
index dd122caa0e3e16ad74609d5aebaa38a1fee8e6c6..e999caf5829d580de13c50d6d0b95833096be63c 100644
--- a/content/browser/media/media_keys_listener_manager_impl.h
+++ b/content/browser/media/media_keys_listener_manager_impl.h
@@ -44,6 +44,8 @@ class CONTENT_EXPORT MediaKeysListenerManagerImpl
// ui::MediaKeysListener::Delegate:
void OnMediaKeysAccelerator(const ui::Accelerator& accelerator) override;

+ static void SetShouldUseInternalMediaKeyHandling(bool should_use);
+
// Informs the MediaKeysListener whether or not media is playing.
// TODO(https://crbug.com/974035): Once the MediaKeysListenerManager has been
// refactored to work with system media controls this should no longer be
@@ -101,10 +103,6 @@ class CONTENT_EXPORT MediaKeysListenerManagerImpl
std::unique_ptr<HardwareKeyMediaController> hardware_key_media_controller_;
std::unique_ptr<SystemMediaControlsNotifier> system_media_controls_notifier_;

- // False if media key handling has been explicitly disabled by a call to
- // |DisableInternalMediaKeyHandling()|.
- bool media_key_handling_enabled_;
-
// True if auxiliary services have already been started.
bool auxiliary_services_started_;

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 Down
15 changes: 13 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,8 @@

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

using content::MediaKeysListenerManagerImpl;
using extensions::GlobalShortcutListener;

namespace {
Expand Down Expand Up @@ -89,11 +92,15 @@ bool GlobalShortcut::RegisterAll(

bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
const base::Closure& callback) {
if (extensions::Command::IsMediaKey(accelerator)) {
#if defined(OS_MACOSX)
if (RegisteringMediaKeyForUntrustedClient(accelerator))
return false;
if (RegisteringMediaKeyForUntrustedClient(accelerator))
return false;
#endif

MediaKeysListenerManagerImpl::SetShouldUseInternalMediaKeyHandling(false);
}

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

if (extensions::Command::IsMediaKey(accelerator)) {
MediaKeysListenerManagerImpl::SetShouldUseInternalMediaKeyHandling(true);
}

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

0 comments on commit 8cd87f1

Please sign in to comment.