Bug 1593131 - part4 : make 'MediaControlKeysManager' being both an event source and listener. r=chunmin
authorAlastor Wu <alwu@mozilla.com>
Thu, 14 Nov 2019 21:14:29 +0000
changeset 502053 103ad9fe9feaba13a65fc544567f45aa37161504
parent 502052 9d1e133ed17c9525faafff30e632a136a536d807
child 502054 55fe1b2e71c4c12aee2f132db87a46e0bf5fa8f3
push id114172
push userdluca@mozilla.com
push dateTue, 19 Nov 2019 11:31:10 +0000
treeherdermozilla-inbound@b5c5ba07d3db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerschunmin
bugs1593131
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1593131 - part4 : make 'MediaControlKeysManager' being both an event source and listener. r=chunmin The event source in `MediaControlKeysManager` now is created dynamically, so that means sometime we don't have an event source to allow people add or remove listener Therefore, make `MediaControlKeysManager` inherit from `MediaControlKeysEventSource` to allow it to provide add/remove listener methods and inherit from `MediaControlKeysEventListener` to allow it to monitor the real media control keys event and dispatch event to its listeners. Differential Revision: https://phabricator.services.mozilla.com/D51766
dom/media/mediacontrol/MediaControlKeysManager.cpp
dom/media/mediacontrol/MediaControlKeysManager.h
dom/media/mediacontrol/MediaControlService.cpp
dom/media/mediacontrol/MediaControlService.h
--- a/dom/media/mediacontrol/MediaControlKeysManager.cpp
+++ b/dom/media/mediacontrol/MediaControlKeysManager.cpp
@@ -18,16 +18,19 @@ extern mozilla::LazyLogModule gMediaCont
 #undef LOG
 #define LOG(msg, ...)                        \
   MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
           ("MediaControlKeysManager=%p, " msg, this, ##__VA_ARGS__))
 
 namespace mozilla {
 namespace dom {
 
+NS_IMPL_ISUPPORTS_INHERITED0(MediaControlKeysManager,
+                             MediaControlKeysEventSource)
+
 void MediaControlKeysManager::Init() {
   mControllerAmountChangedListener =
       MediaControlService::GetService()
           ->MediaControllerAmountChangedEvent()
           .Connect(AbstractThread::MainThread(), this,
                    &MediaControlKeysManager::ControllerAmountChanged);
 }
 
@@ -43,16 +46,19 @@ void MediaControlKeysManager::StartMonit
   }
   CreateEventSource();
 }
 
 void MediaControlKeysManager::CreateEventSource() {
 #ifdef MOZ_APPLEMEDIA
   mEventSource = new MediaHardwareKeysEventSourceMac();
 #endif
+  if (mEventSource) {
+    mEventSource->AddListener(this);
+  }
 }
 
 void MediaControlKeysManager::StopMonitoringControlKeys() {
   LOG("StopMonitoringControlKeys");
   if (mEventSource) {
     mEventSource->Close();
     mEventSource = nullptr;
   }
@@ -63,30 +69,16 @@ void MediaControlKeysManager::Controller
   LOG("Controller amount changed=%" PRId64, aControllerAmount);
   if (aControllerAmount > 0 && !mEventSource) {
     StartMonitoringControlKeys();
   } else if (aControllerAmount == 0 && mEventSource) {
     StopMonitoringControlKeys();
   }
 }
 
-bool MediaControlKeysManager::AddListener(
-    MediaControlKeysEventListener* aListener) {
-  if (!mEventSource) {
-    LOG("No event source for adding a listener");
-    return false;
+void MediaControlKeysManager::OnKeyPressed(MediaControlKeysEvent aKeyEvent) {
+  for (auto listener : mListeners) {
+    listener->OnKeyPressed(aKeyEvent);
   }
-  mEventSource->AddListener(aListener);
-  return true;
-}
-
-bool MediaControlKeysManager::RemoveListener(
-    MediaControlKeysEventListener* aListener) {
-  if (!mEventSource) {
-    LOG("No event source for removing a listener");
-    return false;
-  }
-  mEventSource->RemoveListener(aListener);
-  return true;
 }
 
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/media/mediacontrol/MediaControlKeysManager.h
+++ b/dom/media/mediacontrol/MediaControlKeysManager.h
@@ -14,33 +14,32 @@ namespace dom {
 /**
  * MediaControlKeysManager is a wrapper of MediaControlKeysEventSource, which
  * is used to manage creating and destroying a real media keys event source.
  *
  * It monitors the amount of the media controller in MediaService, and would
  * create the event source when there is any existing controller and destroy it
  * when there is no controller.
  */
-class MediaControlKeysManager final {
+class MediaControlKeysManager final : public MediaControlKeysEventSource,
+                                      public MediaControlKeysEventListener {
  public:
+  NS_DECL_ISUPPORTS_INHERITED
   MediaControlKeysManager() = default;
-  ~MediaControlKeysManager();
 
-  // Return false when there is no event source created, so that we are not able
-  // to add or remove listener, otherwise, calling these methods should always
-  // succeed.
-  bool AddListener(MediaControlKeysEventListener* aListener);
-  bool RemoveListener(MediaControlKeysEventListener* aListener);
   void Init();
 
+  void OnKeyPressed(MediaControlKeysEvent aKeyEvent) override;
+
   // The callback function for monitoring the media controller amount changed
   // event.
   void ControllerAmountChanged(uint64_t aControllerAmount);
 
  private:
+  ~MediaControlKeysManager();
   void StartMonitoringControlKeys();
   void StopMonitoringControlKeys();
   void CreateEventSource();
   RefPtr<MediaControlKeysEventSource> mEventSource;
   MediaEventListener mControllerAmountChangedListener;
 };
 
 }  // namespace dom
--- a/dom/media/mediacontrol/MediaControlService.cpp
+++ b/dom/media/mediacontrol/MediaControlService.cpp
@@ -54,18 +54,19 @@ MediaControlService::MediaControlService
   RefPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
   if (obs) {
     obs->AddObserver(this, "xpcom-shutdown", false);
   }
 }
 
 void MediaControlService::Init() {
   mMediaKeysHandlder = new MediaControlKeysHandler();
-  GetMediaControlKeysManager().Init();
-  GetMediaControlKeysManager().AddListener(mMediaKeysHandlder.get());
+  mMediaControlKeysManager = new MediaControlKeysManager();
+  mMediaControlKeysManager->Init();
+  mMediaControlKeysManager->AddListener(mMediaKeysHandlder.get());
 }
 
 MediaControlService::~MediaControlService() {
   LOG("destroy media control service");
   Shutdown();
 }
 
 NS_IMETHODIMP
@@ -84,17 +85,17 @@ MediaControlService::Observe(nsISupports
   }
   return NS_OK;
 }
 
 void MediaControlService::Shutdown() {
   ShutdownAllControllers();
   mControllers.Clear();
   mAudioFocusManager.Shutdown();
-  GetMediaControlKeysManager().RemoveListener(mMediaKeysHandlder.get());
+  mMediaControlKeysManager->RemoveListener(mMediaKeysHandlder.get());
 }
 
 RefPtr<MediaController> MediaControlService::GetOrCreateControllerById(
     const uint64_t aId) const {
   RefPtr<MediaController> controller = mControllers.Get(aId);
   if (!controller) {
     controller = new TabMediaController(aId);
   }
--- a/dom/media/mediacontrol/MediaControlService.h
+++ b/dom/media/mediacontrol/MediaControlService.h
@@ -34,17 +34,17 @@ class MediaControlService final : public
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
 
   static RefPtr<MediaControlService> GetService();
 
   RefPtr<MediaController> GetOrCreateControllerById(const uint64_t aId) const;
   RefPtr<MediaController> GetControllerById(const uint64_t aId) const;
   AudioFocusManager& GetAudioFocusManager() { return mAudioFocusManager; }
-  MediaControlKeysManager& GetMediaControlKeysManager() {
+  MediaControlKeysEventSource* GetMediaControlKeysEventSource() {
     return mMediaControlKeysManager;
   }
 
   void AddMediaController(const RefPtr<MediaController>& aController);
   void RemoveMediaController(const RefPtr<MediaController>& aController);
   uint64_t GetControllersNum() const;
 
   already_AddRefed<MediaController> GetLastAddedController();
@@ -65,17 +65,17 @@ class MediaControlService final : public
   void PlayAllControllers() const;
   void PauseAllControllers() const;
   void StopAllControllers() const;
   void ShutdownAllControllers() const;
 
   nsDataHashtable<nsUint64HashKey, RefPtr<MediaController>> mControllers;
   nsTArray<uint64_t> mControllerHistory;
   AudioFocusManager mAudioFocusManager;
-  MediaControlKeysManager mMediaControlKeysManager;
+  RefPtr<MediaControlKeysManager> mMediaControlKeysManager;
   RefPtr<MediaControlKeysEventListener> mMediaKeysHandlder;
   MediaEventProducer<uint64_t> mMediaControllerAmountChangedEvent;
 };
 
 }  // namespace dom
 }  // namespace mozilla
 
 #endif