From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Samuel Attard <sattard@slack-corp.com>
Date: Wed, 29 Jan 2020 12:28:48 -0800
Subject: fix: use the new MediaPlayPause key listener for internal chrome
 logic

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
+++ b/chrome/browser/extensions/global_shortcut_listener_mac.mm
@@ -39,7 +39,7 @@
   // global MediaKeysListener to receive media keys.
   if (!content::MediaKeysListenerManager::IsMediaKeysListenerManagerEnabled()) {
     media_keys_listener_ = ui::MediaKeysListener::Create(
-        this, ui::MediaKeysListener::Scope::kGlobal);
+        this, ui::MediaKeysListener::Scope::kGlobalRequiresAccessibility);
     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 28dc8c8451940b18e74384aad10d4d9e886b31a3..f9e1854ed72cc5a9f2431df066eb72f5242569f1 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
+++ b/ui/base/accelerators/media_keys_listener.h
@@ -20,8 +20,9 @@ class Accelerator;
 class COMPONENT_EXPORT(UI_BASE) MediaKeysListener {
  public:
   enum class Scope {
-    kGlobal,   // Listener works whenever application in focus or not.
-    kFocused,  // Listener only works whan application has focus.
+    kGlobalRequiresAccessibility, // Listener works whenever application in focus or not but requires accessibility permissions on macOS
+    kGlobal,                      // Listener works whenever application in focus or not but requires media to be playnig.
+    kFocused,                     // Listener only works whan application has focus.
   };
 
   // Media keys accelerators receiver.
diff --git a/ui/base/accelerators/media_keys_listener_linux.cc b/ui/base/accelerators/media_keys_listener_linux.cc
index c74807dfae799851bb2e40996e634d8513e590a0..48f459941cae385e49af09410bb1812db5e6d971 100644
--- a/ui/base/accelerators/media_keys_listener_linux.cc
+++ b/ui/base/accelerators/media_keys_listener_linux.cc
@@ -13,7 +13,7 @@ std::unique_ptr<MediaKeysListener> MediaKeysListener::Create(
     MediaKeysListener::Scope scope) {
   DCHECK(delegate);
 
-  if (scope == Scope::kGlobal) {
+  if (scope == Scope::kGlobal || scope == Scope::kGlobalRequiresAccessibility) {
     if (!SystemMediaControlsMediaKeysListener::has_instance()) {
       auto listener =
           std::make_unique<SystemMediaControlsMediaKeysListener>(delegate);
diff --git a/ui/base/accelerators/media_keys_listener_win.cc b/ui/base/accelerators/media_keys_listener_win.cc
index c50ea0ca2b8d612b96c0c822f5d36e9eb4ff861d..8b89fea7c09be007d8a020eb4d75f783c887f1a7 100644
--- a/ui/base/accelerators/media_keys_listener_win.cc
+++ b/ui/base/accelerators/media_keys_listener_win.cc
@@ -14,7 +14,7 @@ std::unique_ptr<MediaKeysListener> MediaKeysListener::Create(
     MediaKeysListener::Scope scope) {
   DCHECK(delegate);
 
-  if (scope == Scope::kGlobal) {
+  if (scope == Scope::kGlobal || scope == Scope::kGlobalRequiresAccessibility) {
     // We should never have more than one global media keys listener.
     if (!SystemMediaControlsMediaKeysListener::has_instance() &&
         !GlobalMediaKeysListenerWin::has_instance()) {
