Bug 1602617 - part2 : reuse same event source all the time. r=MeFisto94
authorAlastor Wu <alwu@mozilla.com>
Tue, 10 Dec 2019 18:30:36 +0000
changeset 506291 1fb1ca98f23b29d1a8a287ad2db326c84df04c57
parent 506290 f9eb0ac18a3ba43954e69ea5fe01c3473d6a531a
child 506292 3ea980df55487393cf671034dbd53bfe5cc26503
push id36901
push useraciure@mozilla.com
push dateTue, 10 Dec 2019 21:29:05 +0000
treeherdermozilla-central@1fb1ca98f23b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMeFisto94
bugs1602617
milestone73.0a1
first release with
nightly linux32
1fb1ca98f23b / 73.0a1 / 20191210212905 / files
nightly linux64
1fb1ca98f23b / 73.0a1 / 20191210212905 / files
nightly mac
1fb1ca98f23b / 73.0a1 / 20191210212905 / files
nightly win32
1fb1ca98f23b / 73.0a1 / 20191210212905 / files
nightly win64
1fb1ca98f23b / 73.0a1 / 20191210212905 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1602617 - part2 : reuse same event source all the time. r=MeFisto94 Because we won't change platform dynamically, so we should be able to keep same the event source all the time and use `Open()` and `Close()` to control if we need to monitor media keys event or not. Differential Revision: https://phabricator.services.mozilla.com/D56499
dom/media/mediacontrol/MediaControlKeysManager.cpp
dom/media/mediacontrol/MediaControlKeysManager.h
--- a/dom/media/mediacontrol/MediaControlKeysManager.cpp
+++ b/dom/media/mediacontrol/MediaControlKeysManager.cpp
@@ -38,50 +38,53 @@ bool MediaControlKeysManager::Open() {
           ->MediaControllerAmountChangedEvent()
           .Connect(AbstractThread::MainThread(), this,
                    &MediaControlKeysManager::ControllerAmountChanged);
   return true;
 }
 
 MediaControlKeysManager::~MediaControlKeysManager() {
   StopMonitoringControlKeys();
+  mEventSource = nullptr;
   mControllerAmountChangedListener.DisconnectIfExists();
 }
 
 void MediaControlKeysManager::StartMonitoringControlKeys() {
-  LOG("StartMonitoringControlKeys");
   if (!StaticPrefs::media_hardwaremediakeys_enabled()) {
     return;
   }
-  CreateEventSource();
-}
+
+  if (!mEventSource) {
+    mEventSource = widget::CreateMediaControlKeysEventSource();
+  }
 
-void MediaControlKeysManager::CreateEventSource() {
-  mEventSource = widget::CreateMediaControlKeysEventSource();
-  if (mEventSource && mEventSource->Open()) {
+  // TODO : now we only have implemented the event source on OSX, so we won't
+  // get the event source on other platforms. Once we finish implementation on
+  // all platforms, remove this `if` checks and use `assertion` to make sure the
+  // source alway exists.
+  if (mEventSource && !mEventSource->IsOpened()) {
+    LOG("StartMonitoringControlKeys");
+    mEventSource->Open();
     mEventSource->AddListener(this);
-  } else {
-    mEventSource = nullptr;
   }
 }
 
 void MediaControlKeysManager::StopMonitoringControlKeys() {
-  LOG("StopMonitoringControlKeys");
-  if (mEventSource) {
+  if (mEventSource && mEventSource->IsOpened()) {
+    LOG("StopMonitoringControlKeys");
     mEventSource->Close();
-    mEventSource = nullptr;
   }
 }
 
 void MediaControlKeysManager::ControllerAmountChanged(
     uint64_t aControllerAmount) {
   LOG("Controller amount changed=%" PRId64, aControllerAmount);
-  if (aControllerAmount > 0 && !mEventSource) {
+  if (aControllerAmount > 0) {
     StartMonitoringControlKeys();
-  } else if (aControllerAmount == 0 && mEventSource) {
+  } else if (aControllerAmount == 0) {
     StopMonitoringControlKeys();
   }
 }
 
 void MediaControlKeysManager::OnKeyPressed(MediaControlKeysEvent aKeyEvent) {
   for (auto listener : mListeners) {
     listener->OnKeyPressed(aKeyEvent);
   }
--- a/dom/media/mediacontrol/MediaControlKeysManager.h
+++ b/dom/media/mediacontrol/MediaControlKeysManager.h
@@ -35,17 +35,16 @@ class MediaControlKeysManager final : pu
   // 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
 }  // namespace mozilla
 
 #endif