Bug 1593131 - part3 : use nsISupport for ref counting r=chunmin
authoralwu <alwu@mozilla.com>
Thu, 14 Nov 2019 20:59:05 +0000
changeset 502052 9d1e133ed17c9525faafff30e632a136a536d807
parent 502051 222533f6446ed6f8f544c6e25e10cf344557f62c
child 502053 103ad9fe9feaba13a65fc544567f45aa37161504
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 - part3 : use nsISupport for ref counting r=chunmin Use` nsISupport` for ref counting, instead of using `NS_INLINE_DECL_THREADSAFE_REFCOUNTING`, then we can allow an usage such like inheritting from both `MediaControlKeysEventListener` and `MediaControlKeysEventSource` at the same time, which doesn't be allowed in old implementation because we can only have one parent class owning `mRefCnt`. Differential Revision: https://phabricator.services.mozilla.com/D52703
dom/media/mediacontrol/MediaControlKeysEvent.cpp
dom/media/mediacontrol/MediaControlKeysEvent.h
dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm
--- a/dom/media/mediacontrol/MediaControlKeysEvent.cpp
+++ b/dom/media/mediacontrol/MediaControlKeysEvent.cpp
@@ -38,16 +38,18 @@ static const char* ToMediaControlKeysEve
 #undef LOG_KEY
 #define LOG_KEY(msg, key, ...)                                    \
   if (MOZ_LOG_TEST(gMediaControlLog, mozilla::LogLevel::Debug)) { \
     MOZ_LOG(gMediaControlLog, LogLevel::Debug,                    \
             ("MediaControlKeysEventListener=%p, " msg, this,      \
              ToMediaControlKeysEventStr(key), ##__VA_ARGS__));    \
   }
 
+NS_IMPL_ISUPPORTS0(MediaControlKeysHandler)
+
 void MediaControlKeysHandler::OnKeyPressed(MediaControlKeysEvent aKeyEvent) {
   LOG_KEY("OnKeyPressed '%s'", aKeyEvent);
   switch (aKeyEvent) {
     case MediaControlKeysEvent::ePlayPause: {
       RefPtr<MediaControlService> service = MediaControlService::GetService();
       MOZ_ASSERT(service);
       RefPtr<MediaController> controller = service->GetLastAddedController();
       if (!controller) {
@@ -66,16 +68,18 @@ void MediaControlKeysHandler::OnKeyPress
       // TODO : implement related controller functions.
       return;
     default:
       LOG("Error : undefined event!");
       return;
   }
 }
 
+NS_IMPL_ISUPPORTS0(MediaControlKeysEventSource)
+
 void MediaControlKeysEventSource::AddListener(
     MediaControlKeysEventListener* aListener) {
   MOZ_ASSERT(aListener);
   LOG("Add listener %p", aListener);
   mListeners.AppendElement(aListener);
 }
 
 void MediaControlKeysEventSource::RemoveListener(
--- a/dom/media/mediacontrol/MediaControlKeysEvent.h
+++ b/dom/media/mediacontrol/MediaControlKeysEvent.h
@@ -14,44 +14,49 @@ namespace dom {
 enum class MediaControlKeysEvent { ePlayPause, ePrev, eNext, eNone };
 
 /**
  * MediaControlKeysEventListener is a pure interface, which is used to monitor
  * MediaControlKeysEvent, we can add it onto the MediaControlKeysEventSource,
  * and then everytime when the media key events occur, `OnKeyPressed` will be
  * called so that we can do related handling.
  */
-class MediaControlKeysEventListener {
+class MediaControlKeysEventListener : public nsISupports {
  public:
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaControlKeysEventListener);
   MediaControlKeysEventListener() = default;
 
   virtual void OnKeyPressed(MediaControlKeysEvent aKeyEvent) = 0;
 
  protected:
   virtual ~MediaControlKeysEventListener() = default;
 };
 
 /**
  * MediaControlKeysHandler is used to operate media controller by corresponding
  * received media control key events.
  */
 class MediaControlKeysHandler final : public MediaControlKeysEventListener {
  public:
+  NS_DECL_ISUPPORTS
+
   void OnKeyPressed(MediaControlKeysEvent aKeyEvent) override;
+
+ private:
+  virtual ~MediaControlKeysHandler() = default;
 };
 
 /**
  * MediaControlKeysEventSource is a base class which is used to implement
  * transporting media control keys event to all its listeners when media keys
  * event happens.
  */
-class MediaControlKeysEventSource {
+class MediaControlKeysEventSource : public nsISupports {
  public:
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaControlKeysEventSource);
+  NS_DECL_ISUPPORTS
+
   MediaControlKeysEventSource() = default;
 
   virtual void AddListener(MediaControlKeysEventListener* aListener);
   virtual void RemoveListener(MediaControlKeysEventListener* aListener);
   size_t GetListenersNum() const;
   void Close();
 
  protected:
--- a/dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm
+++ b/dom/media/mediacontrol/tests/gtest/TestMediaKeysEventMac.mm
@@ -12,23 +12,28 @@
 #include "MediaHardwareKeysEventSourceMac.h"
 
 using namespace mozilla::dom;
 
 static const int kSystemDefinedEventMediaKeysSubtype = 8;
 
 class MediaHardwareKeysEventListenerTest : public MediaControlKeysEventListener {
  public:
+  NS_DECL_ISUPPORTS
+
   void OnKeyPressed(MediaControlKeysEvent aKeyEvent) override { mReceivedEvent = aKeyEvent; }
   MediaControlKeysEvent GetResult() const { return mReceivedEvent; }
 
  private:
+  ~MediaHardwareKeysEventListenerTest() = default;
   MediaControlKeysEvent mReceivedEvent = MediaControlKeysEvent::eNone;
 };
 
+NS_IMPL_ISUPPORTS0(MediaHardwareKeysEventListenerTest)
+
 static void NotifyFakeMediaKeysEvent(RefPtr<MediaHardwareKeysEventSourceMac>& aSource,
                                      MediaControlKeysEvent aEvent, bool aIsKeyPressed) {
   int keyData = 0;
   if (aEvent == MediaControlKeysEvent::ePlayPause) {
     keyData = NX_KEYTYPE_PLAY << 16;
   } else if (aEvent == MediaControlKeysEvent::eNext) {
     keyData = NX_KEYTYPE_NEXT << 16;
   } else if (aEvent == MediaControlKeysEvent::ePrev) {