Bug 1156407 - part 2 - make CALLBACK_TYPE enum a private implementation detail of nsTimerImpl; r=mccr8

The CALLBACK_TYPE enum doesn't need to be exposed outside of
nsTimerImpl, so move it inside the class definition.  While we're doing
this, let's C++-ify the enum definition and give the members
non-shouting names.
This commit is contained in:
Nathan Froyd 2015-04-20 14:08:57 -04:00
parent 880dd42a14
commit 8af565d0f7
2 changed files with 29 additions and 30 deletions

View File

@ -277,7 +277,7 @@ nsTimerImpl::Release(void)
nsTimerImpl::nsTimerImpl() :
mClosure(nullptr),
mCallbackType(CALLBACK_TYPE_UNKNOWN),
mCallbackType(CallbackType::Unknown),
mFiring(false),
mArmed(false),
mCanceled(false),
@ -398,7 +398,7 @@ nsTimerImpl::InitWithFuncCallback(nsTimerCallbackFunc aFunc,
}
ReleaseCallback();
mCallbackType = CALLBACK_TYPE_FUNC;
mCallbackType = CallbackType::Function;
mCallback.c = aFunc;
mClosure = aClosure;
@ -415,7 +415,7 @@ nsTimerImpl::InitWithCallback(nsITimerCallback* aCallback,
}
ReleaseCallback();
mCallbackType = CALLBACK_TYPE_INTERFACE;
mCallbackType = CallbackType::Interface;
mCallback.i = aCallback;
NS_ADDREF(mCallback.i);
@ -430,7 +430,7 @@ nsTimerImpl::Init(nsIObserver* aObserver, uint32_t aDelay, uint32_t aType)
}
ReleaseCallback();
mCallbackType = CALLBACK_TYPE_OBSERVER;
mCallbackType = CallbackType::Observer;
mCallback.o = aObserver;
NS_ADDREF(mCallback.o);
@ -454,7 +454,7 @@ nsTimerImpl::Cancel()
NS_IMETHODIMP
nsTimerImpl::SetDelay(uint32_t aDelay)
{
if (mCallbackType == CALLBACK_TYPE_UNKNOWN && mType == TYPE_ONE_SHOT) {
if (mCallbackType == CallbackType::Unknown && mType == TYPE_ONE_SHOT) {
// This may happen if someone tries to re-use a one-shot timer
// by re-setting delay instead of reinitializing the timer.
NS_ERROR("nsITimer->SetDelay() called when the "
@ -513,7 +513,7 @@ nsTimerImpl::GetClosure(void** aClosure)
NS_IMETHODIMP
nsTimerImpl::GetCallback(nsITimerCallback** aCallback)
{
if (mCallbackType == CALLBACK_TYPE_INTERFACE) {
if (mCallbackType == CallbackType::Interface) {
NS_IF_ADDREF(*aCallback = mCallback.i);
} else if (mTimerCallbackWhileFiring) {
NS_ADDREF(*aCallback = mTimerCallbackWhileFiring);
@ -536,7 +536,7 @@ nsTimerImpl::GetTarget(nsIEventTarget** aTarget)
NS_IMETHODIMP
nsTimerImpl::SetTarget(nsIEventTarget* aTarget)
{
if (NS_WARN_IF(mCallbackType != CALLBACK_TYPE_UNKNOWN)) {
if (NS_WARN_IF(mCallbackType != CallbackType::Unknown)) {
return NS_ERROR_ALREADY_INITIALIZED;
}
@ -609,7 +609,7 @@ nsTimerImpl::Fire()
timeout -= TimeDuration::FromMilliseconds(mDelay);
}
if (mCallbackType == CALLBACK_TYPE_INTERFACE) {
if (mCallbackType == CallbackType::Interface) {
mTimerCallbackWhileFiring = mCallback.i;
}
mFiring = true;
@ -617,22 +617,22 @@ nsTimerImpl::Fire()
// Handle callbacks that re-init the timer, but avoid leaking.
// See bug 330128.
CallbackUnion callback = mCallback;
unsigned callbackType = mCallbackType;
if (callbackType == CALLBACK_TYPE_INTERFACE) {
CallbackType callbackType = mCallbackType;
if (callbackType == CallbackType::Interface) {
NS_ADDREF(callback.i);
} else if (callbackType == CALLBACK_TYPE_OBSERVER) {
} else if (callbackType == CallbackType::Observer) {
NS_ADDREF(callback.o);
}
ReleaseCallback();
switch (callbackType) {
case CALLBACK_TYPE_FUNC:
case CallbackType::Function:
callback.c(this, mClosure);
break;
case CALLBACK_TYPE_INTERFACE:
case CallbackType::Interface:
callback.i->Notify(this);
break;
case CALLBACK_TYPE_OBSERVER:
case CallbackType::Observer:
callback.o->Observe(static_cast<nsITimer*>(this),
NS_TIMER_CALLBACK_TOPIC,
nullptr);
@ -643,15 +643,15 @@ nsTimerImpl::Fire()
// If the callback didn't re-init the timer, and it's not a one-shot timer,
// restore the callback state.
if (mCallbackType == CALLBACK_TYPE_UNKNOWN &&
if (mCallbackType == CallbackType::Unknown &&
mType != TYPE_ONE_SHOT && !mCanceled) {
mCallback = callback;
mCallbackType = callbackType;
} else {
// The timer was a one-shot, or the callback was reinitialized.
if (callbackType == CALLBACK_TYPE_INTERFACE) {
if (callbackType == CallbackType::Interface) {
NS_RELEASE(callback.i);
} else if (callbackType == CALLBACK_TYPE_OBSERVER) {
} else if (callbackType == CallbackType::Observer) {
NS_RELEASE(callback.o);
}
}

View File

@ -36,14 +36,6 @@ extern PRLogModuleInfo* GetTimerLog();
{0x84, 0x27, 0xfb, 0xab, 0x44, 0xf2, 0x9b, 0xc8} \
}
enum
{
CALLBACK_TYPE_UNKNOWN = 0,
CALLBACK_TYPE_INTERFACE = 1,
CALLBACK_TYPE_FUNC = 2,
CALLBACK_TYPE_OBSERVER = 3
};
class nsTimerImpl final : public nsITimer
{
public:
@ -81,6 +73,13 @@ public:
virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const override;
private:
enum class CallbackType : uint8_t {
Unknown = 0,
Interface = 1,
Function = 2,
Observer = 3,
};
~nsTimerImpl();
nsresult InitCommon(uint32_t aType, uint32_t aDelay);
@ -89,12 +88,12 @@ private:
// if we're the last owner of the callback object, make
// sure that we don't recurse into ReleaseCallback in case
// the callback's destructor calls Cancel() or similar.
uint8_t cbType = mCallbackType;
mCallbackType = CALLBACK_TYPE_UNKNOWN;
CallbackType cbType = mCallbackType;
mCallbackType = CallbackType::Unknown;
if (cbType == CALLBACK_TYPE_INTERFACE) {
if (cbType == CallbackType::Interface) {
NS_RELEASE(mCallback.i);
} else if (cbType == CALLBACK_TYPE_OBSERVER) {
} else if (cbType == CallbackType::Observer) {
NS_RELEASE(mCallback.o);
}
}
@ -131,7 +130,7 @@ private:
nsCOMPtr<nsITimerCallback> mTimerCallbackWhileFiring;
// These members are set by Init (called from NS_NewTimer) and never reset.
uint8_t mCallbackType;
CallbackType mCallbackType;
// These members are set by the initiating thread, when the timer's type is
// changed and during the period where it fires on that thread.