fix_media_key_usage_with_globalshortcuts.patch 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Shelley Vohr <[email protected]>
  3. Date: Mon, 16 Aug 2021 17:55:32 +0200
  4. Subject: fix: media key usage with globalShortcuts
  5. This patch enables media keys to work properly with Electron's globalShortcut
  6. module. Chromium's default usage of RemoteCommandCenterDelegate on macOS falls
  7. down into MPRemoteCommandCenter, which makes it such that an app will not
  8. receive remote control events until it begins playing audio. This runs
  9. counter to the design of globalShortcuts, and so we need to instead
  10. use `ui::MediaKeysListener`.
  11. diff --git a/chrome/browser/extensions/global_shortcut_listener.cc b/chrome/browser/extensions/global_shortcut_listener.cc
  12. index fa5be7e4dbddd8333263c412b84a1d93b2d5046a..1b3c65359bff41a96ff27aef6e979ac7e6e8518d 100644
  13. --- a/chrome/browser/extensions/global_shortcut_listener.cc
  14. +++ b/chrome/browser/extensions/global_shortcut_listener.cc
  15. @@ -7,6 +7,7 @@
  16. #include "base/check.h"
  17. #include "base/notreached.h"
  18. #include "content/public/browser/browser_thread.h"
  19. +#include "content/public/browser/media_keys_listener_manager.h"
  20. #include "ui/base/accelerators/accelerator.h"
  21. using content::BrowserThread;
  22. @@ -66,6 +67,22 @@ void GlobalShortcutListener::UnregisterAccelerator(
  23. StopListening();
  24. }
  25. +// static
  26. +void GlobalShortcutListener::SetShouldUseInternalMediaKeyHandling(bool should_use) {
  27. + if (content::MediaKeysListenerManager::
  28. + IsMediaKeysListenerManagerEnabled()) {
  29. + content::MediaKeysListenerManager* media_keys_listener_manager =
  30. + content::MediaKeysListenerManager::GetInstance();
  31. + DCHECK(media_keys_listener_manager);
  32. +
  33. + if (should_use) {
  34. + media_keys_listener_manager->EnableInternalMediaKeyHandling();
  35. + } else {
  36. + media_keys_listener_manager->DisableInternalMediaKeyHandling();
  37. + }
  38. + }
  39. +}
  40. +
  41. void GlobalShortcutListener::UnregisterAccelerators(Observer* observer) {
  42. CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  43. if (IsShortcutHandlingSuspended())
  44. diff --git a/chrome/browser/extensions/global_shortcut_listener.h b/chrome/browser/extensions/global_shortcut_listener.h
  45. index f96af14cb915c8ab92b314ac15b6dffcdd6ec607..2556abe433493251ebd48d4aeddaa6d4f553208f 100644
  46. --- a/chrome/browser/extensions/global_shortcut_listener.h
  47. +++ b/chrome/browser/extensions/global_shortcut_listener.h
  48. @@ -33,6 +33,8 @@ class GlobalShortcutListener {
  49. static GlobalShortcutListener* GetInstance();
  50. + static void SetShouldUseInternalMediaKeyHandling(bool should_use);
  51. +
  52. // Register an observer for when a certain |accelerator| is struck. Returns
  53. // true if register successfully, or false if 1) the specificied |accelerator|
  54. // has been registered by another caller or other native applications, or
  55. diff --git a/content/browser/media/media_keys_listener_manager_impl.cc b/content/browser/media/media_keys_listener_manager_impl.cc
  56. index 8bc58dd31b5822682fe13ad7ded1dc40892b296d..1811361f25fd8276c317e4fcd883e998af8aeab8 100644
  57. --- a/content/browser/media/media_keys_listener_manager_impl.cc
  58. +++ b/content/browser/media/media_keys_listener_manager_impl.cc
  59. @@ -86,7 +86,11 @@ bool MediaKeysListenerManagerImpl::StartWatchingMediaKey(
  60. CanActiveMediaSessionControllerReceiveEvents();
  61. // Tell the underlying MediaKeysListener to listen for the key.
  62. - if (should_start_watching && media_keys_listener_ &&
  63. + if (
  64. +#if BUILDFLAG(IS_MAC)
  65. + !media_key_handling_enabled_ &&
  66. +#endif // BUILDFLAG(IS_MAC)
  67. + should_start_watching && media_keys_listener_ &&
  68. !media_keys_listener_->StartWatchingMediaKey(key_code)) {
  69. return false;
  70. }
  71. @@ -320,6 +324,20 @@ void MediaKeysListenerManagerImpl::StartListeningForMediaKeysIfNecessary() {
  72. this, ui::MediaKeysListener::Scope::kGlobal);
  73. DCHECK(media_keys_listener_);
  74. }
  75. +
  76. +#if BUILDFLAG(IS_MAC)
  77. + // Chromium's implementation of SystemMediaControls falls
  78. + // down into MPRemoteCommandCenter, which makes it such that an app will not
  79. + // will not receive remote control events until it begins playing audio.
  80. + // If there's not already a MediaKeysListener instance, create one so
  81. + // that globalShortcuts work correctly.
  82. + if (!media_keys_listener_) {
  83. + media_keys_listener_ = ui::MediaKeysListener::Create(
  84. + this, ui::MediaKeysListener::Scope::kGlobal);
  85. + DCHECK(media_keys_listener_);
  86. + }
  87. +#endif
  88. +
  89. EnsureAuxiliaryServices();
  90. }