2015-07-24 04:03:11 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MediaEventSource_h_
|
|
|
|
#define MediaEventSource_h_
|
|
|
|
|
|
|
|
#include "mozilla/AbstractThread.h"
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2015-11-02 05:48:01 +03:00
|
|
|
#include "mozilla/Tuple.h"
|
2015-07-24 04:03:11 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2017-11-15 09:58:03 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-07-24 04:03:11 +03:00
|
|
|
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsTArray.h"
|
2015-08-07 06:11:18 +03:00
|
|
|
#include "nsThreadUtils.h"
|
2015-07-24 04:03:11 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A thread-safe tool to communicate "revocation" across threads. It is used to
|
|
|
|
* disconnect a listener from the event source to prevent future notifications
|
|
|
|
* from coming. Revoke() can be called on any thread. However, it is recommended
|
|
|
|
* to be called on the target thread to avoid race condition.
|
|
|
|
*
|
|
|
|
* RevocableToken is not exposed to the client code directly.
|
|
|
|
* Use MediaEventListener below to do the job.
|
|
|
|
*/
|
|
|
|
class RevocableToken {
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RevocableToken);
|
|
|
|
|
|
|
|
public:
|
|
|
|
RevocableToken() : mRevoked(false) {}
|
|
|
|
|
|
|
|
void Revoke() {
|
|
|
|
mRevoked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsRevoked() const {
|
|
|
|
return mRevoked;
|
|
|
|
}
|
|
|
|
|
2017-04-28 09:18:38 +03:00
|
|
|
protected:
|
|
|
|
// Virtual destructor is required since we might delete a Listener object
|
|
|
|
// through its base type pointer.
|
|
|
|
virtual ~RevocableToken() { }
|
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
private:
|
|
|
|
Atomic<bool> mRevoked;
|
|
|
|
};
|
|
|
|
|
2016-06-20 11:06:54 +03:00
|
|
|
enum class ListenerPolicy : int8_t {
|
2015-08-14 08:01:03 +03:00
|
|
|
// Allow at most one listener. Move will be used when possible
|
|
|
|
// to pass the event data to save copy.
|
|
|
|
Exclusive,
|
2016-06-20 11:06:54 +03:00
|
|
|
// Allow multiple listeners. Event data will always be copied when passed
|
2015-08-14 08:01:03 +03:00
|
|
|
// to the listeners.
|
|
|
|
NonExclusive
|
|
|
|
};
|
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define how an event type is passed internally in MediaEventSource and to the
|
|
|
|
* listeners. Specialized for the void type to pass a dummy bool instead.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
struct EventTypeTraits {
|
|
|
|
typedef T ArgType;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct EventTypeTraits<void> {
|
|
|
|
typedef bool ArgType;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if a method function or lambda accepts one or more arguments.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2015-08-24 05:38:24 +03:00
|
|
|
class TakeArgsHelper {
|
2015-07-24 04:03:11 +03:00
|
|
|
template <typename C> static FalseType test(void(C::*)(), int);
|
|
|
|
template <typename C> static FalseType test(void(C::*)() const, int);
|
|
|
|
template <typename C> static FalseType test(void(C::*)() volatile, int);
|
|
|
|
template <typename C> static FalseType test(void(C::*)() const volatile, int);
|
|
|
|
template <typename F> static FalseType test(F&&, decltype(DeclVal<F>()(), 0));
|
|
|
|
static TrueType test(...);
|
|
|
|
public:
|
|
|
|
typedef decltype(test(DeclVal<T>(), 0)) Type;
|
|
|
|
};
|
|
|
|
|
2015-08-24 05:38:24 +03:00
|
|
|
template <typename T>
|
|
|
|
struct TakeArgs : public TakeArgsHelper<T>::Type {};
|
|
|
|
|
2017-04-28 06:28:13 +03:00
|
|
|
template <typename T> struct EventTarget;
|
2015-07-24 04:03:11 +03:00
|
|
|
|
|
|
|
template <>
|
2017-04-28 06:28:13 +03:00
|
|
|
struct EventTarget<nsIEventTarget> {
|
2015-07-24 04:03:11 +03:00
|
|
|
static void
|
2016-06-08 10:26:28 +03:00
|
|
|
Dispatch(nsIEventTarget* aTarget, already_AddRefed<nsIRunnable> aTask) {
|
2018-05-30 22:15:35 +03:00
|
|
|
aTarget->Dispatch(std::move(aTask), NS_DISPATCH_NORMAL);
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2017-04-28 06:28:13 +03:00
|
|
|
struct EventTarget<AbstractThread> {
|
2015-07-24 04:03:11 +03:00
|
|
|
static void
|
2016-06-08 10:26:28 +03:00
|
|
|
Dispatch(AbstractThread* aTarget, already_AddRefed<nsIRunnable> aTask) {
|
2018-05-30 22:15:35 +03:00
|
|
|
Unused << aTarget->Dispatch(std::move(aTask));
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encapsulate a raw pointer to be captured by a lambda without causing
|
|
|
|
* static-analysis errors.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
class RawPtr {
|
|
|
|
public:
|
|
|
|
explicit RawPtr(T* aPtr) : mPtr(aPtr) {}
|
|
|
|
T* get() const { return mPtr; }
|
|
|
|
private:
|
|
|
|
T* const mPtr;
|
|
|
|
};
|
|
|
|
|
2017-04-28 12:18:50 +03:00
|
|
|
template <typename... As>
|
|
|
|
class Listener : public RevocableToken
|
2017-04-28 09:18:38 +03:00
|
|
|
{
|
2015-08-24 05:38:33 +03:00
|
|
|
public:
|
2017-04-28 11:28:47 +03:00
|
|
|
template <typename... Ts>
|
|
|
|
void Dispatch(Ts&&... aEvents)
|
|
|
|
{
|
2017-04-28 19:17:57 +03:00
|
|
|
if (CanTakeArgs()) {
|
|
|
|
DispatchTask(NewRunnableMethod<typename Decay<Ts>::Type&&...>(
|
2017-06-12 22:34:10 +03:00
|
|
|
"detail::Listener::ApplyWithArgs",
|
|
|
|
this,
|
|
|
|
&Listener::ApplyWithArgs,
|
2018-06-01 19:30:30 +03:00
|
|
|
std::forward<Ts>(aEvents)...));
|
2017-04-28 19:17:57 +03:00
|
|
|
} else {
|
2017-06-12 22:34:10 +03:00
|
|
|
DispatchTask(NewRunnableMethod(
|
|
|
|
"detail::Listener::ApplyWithNoArgs", this, &Listener::ApplyWithNoArgs));
|
2017-04-28 19:17:57 +03:00
|
|
|
}
|
2017-04-28 11:28:47 +03:00
|
|
|
}
|
2015-08-24 05:38:33 +03:00
|
|
|
|
2017-04-28 12:18:50 +03:00
|
|
|
protected:
|
|
|
|
virtual ~Listener()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsRevoked(), "Must disconnect the listener.");
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:28:47 +03:00
|
|
|
private:
|
|
|
|
virtual void DispatchTask(already_AddRefed<nsIRunnable> aTask) = 0;
|
2017-04-28 19:17:57 +03:00
|
|
|
|
|
|
|
// True if the underlying listener function takes non-zero arguments.
|
|
|
|
virtual bool CanTakeArgs() const = 0;
|
|
|
|
// Pass the event data to the underlying listener function. Should be called
|
|
|
|
// only when CanTakeArgs() returns true.
|
|
|
|
virtual void ApplyWithArgs(As&&... aEvents) = 0;
|
|
|
|
// Invoke the underlying listener function. Should be called only when
|
|
|
|
// CanTakeArgs() returns false.
|
|
|
|
virtual void ApplyWithNoArgs() = 0;
|
2015-08-24 05:38:33 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the registered target thread and function so it knows where and to
|
|
|
|
* whom to send the event data.
|
|
|
|
*/
|
2017-04-28 12:18:50 +03:00
|
|
|
template <typename Target, typename Function, typename... As>
|
|
|
|
class ListenerImpl : public Listener<As...>
|
2017-04-28 11:28:47 +03:00
|
|
|
{
|
2017-05-02 12:11:51 +03:00
|
|
|
// Strip CV and reference from Function.
|
|
|
|
using FunctionStorage = typename Decay<Function>::Type;
|
|
|
|
|
2015-08-24 05:38:33 +03:00
|
|
|
public:
|
2017-05-02 12:11:51 +03:00
|
|
|
template <typename F>
|
|
|
|
ListenerImpl(Target* aTarget, F&& aFunction)
|
2017-04-28 11:28:47 +03:00
|
|
|
: mTarget(aTarget)
|
2018-06-01 19:30:30 +03:00
|
|
|
, mFunction(std::forward<F>(aFunction))
|
2017-04-28 11:28:47 +03:00
|
|
|
{
|
2015-08-24 05:38:33 +03:00
|
|
|
}
|
2017-04-28 11:28:47 +03:00
|
|
|
|
2015-08-24 05:38:33 +03:00
|
|
|
private:
|
2017-04-28 11:28:47 +03:00
|
|
|
void DispatchTask(already_AddRefed<nsIRunnable> aTask) override
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
EventTarget<Target>::Dispatch(mTarget.get(), std::move(aTask));
|
2017-04-28 11:28:47 +03:00
|
|
|
}
|
2015-08-24 05:38:33 +03:00
|
|
|
|
2017-04-28 19:17:57 +03:00
|
|
|
bool CanTakeArgs() const override
|
|
|
|
{
|
2017-05-02 12:11:51 +03:00
|
|
|
return TakeArgs<FunctionStorage>::value;
|
2017-04-28 19:17:57 +03:00
|
|
|
}
|
|
|
|
|
2017-04-28 11:28:47 +03:00
|
|
|
// |F| takes one or more arguments.
|
|
|
|
template <typename F>
|
|
|
|
typename EnableIf<TakeArgs<F>::value, void>::Type
|
2017-04-28 19:17:57 +03:00
|
|
|
ApplyWithArgsImpl(const F& aFunc, As&&... aEvents)
|
2017-04-28 11:28:47 +03:00
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
aFunc(std::move(aEvents)...);
|
2015-08-24 05:38:33 +03:00
|
|
|
}
|
2017-04-28 11:28:47 +03:00
|
|
|
|
2017-04-28 19:17:57 +03:00
|
|
|
// |F| takes no arguments.
|
|
|
|
template <typename F>
|
|
|
|
typename EnableIf<!TakeArgs<F>::value, void>::Type
|
|
|
|
ApplyWithArgsImpl(const F& aFunc, As&&... aEvents)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Call ApplyWithNoArgs instead.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyWithArgs(As&&... aEvents) override
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(TakeArgs<Function>::value);
|
|
|
|
// Don't call the listener if it is disconnected.
|
|
|
|
if (!RevocableToken::IsRevoked()) {
|
2018-05-30 22:15:35 +03:00
|
|
|
ApplyWithArgsImpl(mFunction, std::move(aEvents)...);
|
2017-04-28 19:17:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// |F| takes one or more arguments.
|
|
|
|
template <typename F>
|
|
|
|
typename EnableIf<TakeArgs<F>::value, void>::Type
|
|
|
|
ApplyWithNoArgsImpl(const F& aFunc)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("Call ApplyWithArgs instead.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// |F| takes no arguments.
|
2017-04-28 11:28:47 +03:00
|
|
|
template <typename F>
|
|
|
|
typename EnableIf<!TakeArgs<F>::value, void>::Type
|
2017-04-28 19:17:57 +03:00
|
|
|
ApplyWithNoArgsImpl(const F& aFunc)
|
2017-04-28 11:28:47 +03:00
|
|
|
{
|
|
|
|
aFunc();
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:57 +03:00
|
|
|
virtual void ApplyWithNoArgs() override
|
2017-04-28 11:28:47 +03:00
|
|
|
{
|
2017-04-28 19:17:57 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!TakeArgs<Function>::value);
|
2017-04-28 11:28:47 +03:00
|
|
|
// Don't call the listener if it is disconnected.
|
|
|
|
if (!RevocableToken::IsRevoked()) {
|
2017-04-28 19:17:57 +03:00
|
|
|
ApplyWithNoArgsImpl(mFunction);
|
2017-04-28 11:28:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const RefPtr<Target> mTarget;
|
2017-05-02 12:11:51 +03:00
|
|
|
FunctionStorage mFunction;
|
2015-08-24 05:38:33 +03:00
|
|
|
};
|
|
|
|
|
2015-11-02 05:48:01 +03:00
|
|
|
/**
|
|
|
|
* Return true if any type is a reference type.
|
|
|
|
*/
|
|
|
|
template <typename Head, typename... Tails>
|
|
|
|
struct IsAnyReference {
|
|
|
|
static const bool value = IsReference<Head>::value ||
|
|
|
|
IsAnyReference<Tails...>::value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct IsAnyReference<T> {
|
|
|
|
static const bool value = IsReference<T>::value;
|
|
|
|
};
|
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
2017-04-28 06:28:13 +03:00
|
|
|
template <ListenerPolicy, typename... Ts> class MediaEventSourceImpl;
|
2015-07-24 04:03:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Not thread-safe since this is not meant to be shared and therefore only
|
|
|
|
* move constructor is provided. Used to hold the result of
|
|
|
|
* MediaEventSource<T>::Connect() and call Disconnect() to disconnect the
|
|
|
|
* listener from an event source.
|
|
|
|
*/
|
|
|
|
class MediaEventListener {
|
2017-04-28 06:28:13 +03:00
|
|
|
template <ListenerPolicy, typename... Ts>
|
2015-11-02 05:48:01 +03:00
|
|
|
friend class MediaEventSourceImpl;
|
2015-07-24 04:03:11 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
MediaEventListener() {}
|
|
|
|
|
|
|
|
MediaEventListener(MediaEventListener&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mToken(std::move(aOther.mToken)) {}
|
2015-07-24 04:03:11 +03:00
|
|
|
|
|
|
|
MediaEventListener& operator=(MediaEventListener&& aOther) {
|
|
|
|
MOZ_ASSERT(!mToken, "Must disconnect the listener.");
|
2018-05-30 22:15:35 +03:00
|
|
|
mToken = std::move(aOther.mToken);
|
2015-07-24 04:03:11 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~MediaEventListener() {
|
|
|
|
MOZ_ASSERT(!mToken, "Must disconnect the listener.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() {
|
|
|
|
mToken->Revoke();
|
|
|
|
mToken = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-30 04:13:10 +03:00
|
|
|
void DisconnectIfExists() {
|
|
|
|
if (mToken) {
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
private:
|
|
|
|
// Avoid exposing RevocableToken directly to the client code so that
|
|
|
|
// listeners can be disconnected in a controlled manner.
|
|
|
|
explicit MediaEventListener(RevocableToken* aToken) : mToken(aToken) {}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<RevocableToken> mToken;
|
2015-07-24 04:03:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A generic and thread-safe class to implement the observer pattern.
|
|
|
|
*/
|
2017-04-28 06:28:13 +03:00
|
|
|
template <ListenerPolicy Lp, typename... Es>
|
2015-11-02 05:48:01 +03:00
|
|
|
class MediaEventSourceImpl {
|
|
|
|
static_assert(!detail::IsAnyReference<Es...>::value,
|
|
|
|
"Ref-type not supported!");
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using ArgType = typename detail::EventTypeTraits<T>::ArgType;
|
|
|
|
|
2017-04-28 12:18:50 +03:00
|
|
|
typedef detail::Listener<ArgType<Es>...> Listener;
|
2015-08-24 05:38:33 +03:00
|
|
|
|
|
|
|
template<typename Target, typename Func>
|
2017-04-28 12:18:50 +03:00
|
|
|
using ListenerImpl = detail::ListenerImpl<Target, Func, ArgType<Es>...>;
|
2015-07-24 04:03:11 +03:00
|
|
|
|
2015-08-24 05:38:24 +03:00
|
|
|
template <typename Method>
|
|
|
|
using TakeArgs = detail::TakeArgs<Method>;
|
|
|
|
|
2016-06-21 11:31:03 +03:00
|
|
|
void PruneListeners() {
|
|
|
|
int32_t last = static_cast<int32_t>(mListeners.Length()) - 1;
|
|
|
|
for (int32_t i = last; i >= 0; --i) {
|
2017-04-28 09:18:38 +03:00
|
|
|
if (mListeners[i]->IsRevoked()) {
|
2016-06-21 11:31:03 +03:00
|
|
|
mListeners.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
template<typename Target, typename Function>
|
|
|
|
MediaEventListener
|
2017-05-02 12:11:51 +03:00
|
|
|
ConnectInternal(Target* aTarget, Function&& aFunction) {
|
2015-07-24 04:03:11 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2016-06-21 11:31:03 +03:00
|
|
|
PruneListeners();
|
2016-06-20 11:06:54 +03:00
|
|
|
MOZ_ASSERT(Lp == ListenerPolicy::NonExclusive || mListeners.IsEmpty());
|
2015-07-24 04:03:11 +03:00
|
|
|
auto l = mListeners.AppendElement();
|
2017-05-02 12:11:51 +03:00
|
|
|
*l = new ListenerImpl<Target, Function>(
|
2018-06-01 19:30:30 +03:00
|
|
|
aTarget, std::forward<Function>(aFunction));
|
2017-04-28 09:18:38 +03:00
|
|
|
return MediaEventListener(*l);
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
2015-11-02 05:48:01 +03:00
|
|
|
// |Method| takes one or more arguments.
|
2015-07-24 04:03:11 +03:00
|
|
|
template <typename Target, typename This, typename Method>
|
2015-08-24 05:38:24 +03:00
|
|
|
typename EnableIf<TakeArgs<Method>::value, MediaEventListener>::Type
|
|
|
|
ConnectInternal(Target* aTarget, This* aThis, Method aMethod) {
|
2015-07-24 04:03:11 +03:00
|
|
|
detail::RawPtr<This> thiz(aThis);
|
2017-05-02 12:11:51 +03:00
|
|
|
return ConnectInternal(aTarget,
|
|
|
|
[=](ArgType<Es>&&... aEvents) {
|
2018-05-30 22:15:35 +03:00
|
|
|
(thiz.get()->*aMethod)(std::move(aEvents)...);
|
2017-05-02 12:11:51 +03:00
|
|
|
});
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// |Method| takes no arguments. Don't bother passing the event data.
|
|
|
|
template <typename Target, typename This, typename Method>
|
2015-08-24 05:38:24 +03:00
|
|
|
typename EnableIf<!TakeArgs<Method>::value, MediaEventListener>::Type
|
|
|
|
ConnectInternal(Target* aTarget, This* aThis, Method aMethod) {
|
2015-07-24 04:03:11 +03:00
|
|
|
detail::RawPtr<This> thiz(aThis);
|
2017-05-02 12:11:51 +03:00
|
|
|
return ConnectInternal(aTarget,
|
|
|
|
[=]() {
|
|
|
|
(thiz.get()->*aMethod)();
|
|
|
|
});
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Register a function to receive notifications from the event source.
|
|
|
|
*
|
|
|
|
* @param aTarget The target thread on which the function will run.
|
|
|
|
* @param aFunction A function to be called on the target thread. The function
|
|
|
|
* parameter must be convertible from |EventType|.
|
|
|
|
* @return An object used to disconnect from the event source.
|
|
|
|
*/
|
|
|
|
template<typename Function>
|
|
|
|
MediaEventListener
|
2017-05-02 12:11:51 +03:00
|
|
|
Connect(AbstractThread* aTarget, Function&& aFunction) {
|
2018-06-01 19:30:30 +03:00
|
|
|
return ConnectInternal(aTarget, std::forward<Function>(aFunction));
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Function>
|
|
|
|
MediaEventListener
|
2017-05-02 12:11:51 +03:00
|
|
|
Connect(nsIEventTarget* aTarget, Function&& aFunction) {
|
2018-06-01 19:30:30 +03:00
|
|
|
return ConnectInternal(aTarget, std::forward<Function>(aFunction));
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* As above.
|
|
|
|
*
|
|
|
|
* Note we deliberately keep a weak reference to |aThis| in order not to
|
|
|
|
* change its lifetime. This is because notifications are dispatched
|
|
|
|
* asynchronously and removing a listener doesn't always break the reference
|
|
|
|
* cycle for the pending event could still hold a reference to |aThis|.
|
|
|
|
*
|
|
|
|
* The caller must call MediaEventListener::Disconnect() to avoid dangling
|
|
|
|
* pointers.
|
|
|
|
*/
|
|
|
|
template <typename This, typename Method>
|
|
|
|
MediaEventListener
|
|
|
|
Connect(AbstractThread* aTarget, This* aThis, Method aMethod) {
|
2015-08-24 05:38:24 +03:00
|
|
|
return ConnectInternal(aTarget, aThis, aMethod);
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename This, typename Method>
|
|
|
|
MediaEventListener
|
|
|
|
Connect(nsIEventTarget* aTarget, This* aThis, Method aMethod) {
|
2015-08-24 05:38:24 +03:00
|
|
|
return ConnectInternal(aTarget, aThis, aMethod);
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-11-02 05:48:01 +03:00
|
|
|
MediaEventSourceImpl() : mMutex("MediaEventSourceImpl::mMutex") {}
|
2015-07-24 04:03:11 +03:00
|
|
|
|
2017-04-28 06:28:13 +03:00
|
|
|
template <typename... Ts>
|
|
|
|
void NotifyInternal(Ts&&... aEvents) {
|
2015-07-24 04:03:11 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2016-06-21 11:31:03 +03:00
|
|
|
int32_t last = static_cast<int32_t>(mListeners.Length()) - 1;
|
|
|
|
for (int32_t i = last; i >= 0; --i) {
|
2015-07-24 04:03:11 +03:00
|
|
|
auto&& l = mListeners[i];
|
|
|
|
// Remove disconnected listeners.
|
|
|
|
// It is not optimal but is simple and works well.
|
2017-04-28 09:18:38 +03:00
|
|
|
if (l->IsRevoked()) {
|
2015-07-24 04:03:11 +03:00
|
|
|
mListeners.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
2018-06-01 19:30:30 +03:00
|
|
|
l->Dispatch(std::forward<Ts>(aEvents)...);
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex mMutex;
|
2017-04-28 09:18:38 +03:00
|
|
|
nsTArray<RefPtr<Listener>> mListeners;
|
2015-07-24 04:03:11 +03:00
|
|
|
};
|
|
|
|
|
2015-11-02 05:48:01 +03:00
|
|
|
template <typename... Es>
|
|
|
|
using MediaEventSource =
|
2017-04-28 06:28:13 +03:00
|
|
|
MediaEventSourceImpl<ListenerPolicy::NonExclusive, Es...>;
|
2015-11-02 05:48:01 +03:00
|
|
|
|
|
|
|
template <typename... Es>
|
|
|
|
using MediaEventSourceExc =
|
2017-04-28 06:28:13 +03:00
|
|
|
MediaEventSourceImpl<ListenerPolicy::Exclusive, Es...>;
|
2015-11-02 05:48:01 +03:00
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
/**
|
|
|
|
* A class to separate the interface of event subject (MediaEventSource)
|
|
|
|
* and event publisher. Mostly used as a member variable to publish events
|
|
|
|
* to the listeners.
|
|
|
|
*/
|
2015-11-02 05:48:01 +03:00
|
|
|
template <typename... Es>
|
|
|
|
class MediaEventProducer : public MediaEventSource<Es...> {
|
2015-07-24 04:03:11 +03:00
|
|
|
public:
|
2017-04-28 06:28:13 +03:00
|
|
|
template <typename... Ts>
|
|
|
|
void Notify(Ts&&... aEvents) {
|
2017-04-28 12:56:57 +03:00
|
|
|
// Pass lvalues to prevent move in NonExclusive mode.
|
|
|
|
this->NotifyInternal(aEvents...);
|
2017-04-28 06:28:13 +03:00
|
|
|
}
|
2015-07-24 04:03:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specialization for void type. A dummy bool is passed to NotifyInternal
|
|
|
|
* since there is no way to pass a void value.
|
|
|
|
*/
|
|
|
|
template <>
|
|
|
|
class MediaEventProducer<void> : public MediaEventSource<void> {
|
|
|
|
public:
|
|
|
|
void Notify() {
|
2017-04-28 06:28:13 +03:00
|
|
|
this->NotifyInternal(true /* dummy */);
|
2015-07-24 04:03:11 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-02 05:48:01 +03:00
|
|
|
/**
|
2016-06-20 11:06:54 +03:00
|
|
|
* A producer allowing at most one listener.
|
2015-11-02 05:48:01 +03:00
|
|
|
*/
|
|
|
|
template <typename... Es>
|
|
|
|
class MediaEventProducerExc : public MediaEventSourceExc<Es...> {
|
|
|
|
public:
|
2017-04-28 06:28:13 +03:00
|
|
|
template <typename... Ts>
|
|
|
|
void Notify(Ts&&... aEvents) {
|
2018-06-01 19:30:30 +03:00
|
|
|
this->NotifyInternal(std::forward<Ts>(aEvents)...);
|
2017-04-28 06:28:13 +03:00
|
|
|
}
|
2016-06-20 13:31:01 +03:00
|
|
|
};
|
|
|
|
|
2015-07-24 04:03:11 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif //MediaEventSource_h_
|