From 93fb1e26e2412cbfbdc2136e0c1737339c7f868b Mon Sep 17 00:00:00 2001 From: JW Wang Date: Fri, 28 Apr 2017 14:18:38 +0800 Subject: [PATCH] Bug 1361259. P1 - let ListenerBase inherit RevocableToken. r=gerald This is needed by P2 where |Listener| must be ref-counted so we can use NewRunnableMethod() to pass event data to the listener function. MozReview-Commit-ID: CpAgOmxcijc --HG-- extra : rebase_source : f80a302788f6312a912d710262322b30b2bce4c0 extra : source : 4ce544aacca4897bc3d2bb25fd415df93dc940b4 --- dom/media/MediaEventSource.h | 45 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/dom/media/MediaEventSource.h b/dom/media/MediaEventSource.h index 3b5957752b84..9063adf40371 100644 --- a/dom/media/MediaEventSource.h +++ b/dom/media/MediaEventSource.h @@ -13,7 +13,6 @@ #include "mozilla/Mutex.h" #include "mozilla/Tuple.h" #include "mozilla/TypeTraits.h" -#include "mozilla/UniquePtr.h" #include "nsISupportsImpl.h" #include "nsTArray.h" @@ -44,8 +43,12 @@ public: return mRevoked; } +protected: + // Virtual destructor is required since we might delete a Listener object + // through its base type pointer. + virtual ~RevocableToken() { } + private: - ~RevocableToken() {} Atomic mRevoked; }; @@ -213,17 +216,13 @@ enum class EventPassMode : int8_t { Move }; -class ListenerBase { -public: - ListenerBase() : mToken(new RevocableToken()) {} - ~ListenerBase() { - MOZ_ASSERT(Token()->IsRevoked(), "Must disconnect the listener."); +class ListenerBase : public RevocableToken +{ +protected: + virtual ~ListenerBase() + { + MOZ_ASSERT(IsRevoked(), "Must disconnect the listener."); } - RevocableToken* Token() const { - return mToken; - } -private: - const RefPtr mToken; }; /** @@ -232,16 +231,16 @@ private: * to provide different Dispatch() overloads depending on EventPassMode. */ template -class Listener : public ListenerBase { +class Listener : public ListenerBase +{ public: - virtual ~Listener() {} virtual void Dispatch(const As&... aEvents) = 0; }; template -class Listener : public ListenerBase { +class Listener : public ListenerBase +{ public: - virtual ~Listener() {} virtual void Dispatch(As... aEvents) = 0; }; @@ -253,7 +252,7 @@ template class ListenerImpl : public Listener { public: ListenerImpl(Target* aTarget, const Function& aFunction) - : mHelper(ListenerBase::Token(), aTarget, aFunction) {} + : mHelper(this, aTarget, aFunction) {} void Dispatch(const As&... aEvents) override { mHelper.Dispatch(aEvents...); } @@ -266,7 +265,7 @@ class ListenerImpl : public Listener { public: ListenerImpl(Target* aTarget, const Function& aFunction) - : mHelper(ListenerBase::Token(), aTarget, aFunction) {} + : mHelper(this, aTarget, aFunction) {} void Dispatch(As... aEvents) override { mHelper.Dispatch(Move(aEvents)...); } @@ -378,7 +377,7 @@ class MediaEventSourceImpl { void PruneListeners() { int32_t last = static_cast(mListeners.Length()) - 1; for (int32_t i = last; i >= 0; --i) { - if (mListeners[i]->Token()->IsRevoked()) { + if (mListeners[i]->IsRevoked()) { mListeners.RemoveElementAt(i); } } @@ -391,8 +390,8 @@ class MediaEventSourceImpl { PruneListeners(); MOZ_ASSERT(Lp == ListenerPolicy::NonExclusive || mListeners.IsEmpty()); auto l = mListeners.AppendElement(); - l->reset(new ListenerImpl(aTarget, aFunction)); - return MediaEventListener((*l)->Token()); + *l = new ListenerImpl(aTarget, aFunction); + return MediaEventListener(*l); } // |Method| takes one or more arguments. @@ -472,7 +471,7 @@ protected: auto&& l = mListeners[i]; // Remove disconnected listeners. // It is not optimal but is simple and works well. - if (l->Token()->IsRevoked()) { + if (l->IsRevoked()) { mListeners.RemoveElementAt(i); continue; } @@ -482,7 +481,7 @@ protected: private: Mutex mMutex; - nsTArray> mListeners; + nsTArray> mListeners; }; template