Bug 1348738 - (Part 2) Make nsRepeatService use InitWithFuncCallback instead of InitWithCallback. r=dholbert

This patch makes nsRepeatService stop inheriting from nsITimerCallback.
We needed that inheritance for InitWithCallback, but we do not need it
for InitWithFuncCallback.

This patch also makes nsRepeatService singly-owned instead of being
refcounted, since we're left with only a single owning pointer.

MozReview-Commit-ID: Fl8beVC8kGH

--HG--
extra : rebase_source : 3b6223c8e4754a90d2fef460940fda4510310f95
This commit is contained in:
KuoE0 2017-03-29 15:54:49 +08:00
Родитель 809fa786fe
Коммит 6187dc2f18
2 изменённых файлов: 24 добавлений и 23 удалений

Просмотреть файл

@ -11,11 +11,12 @@
//
#include "nsRepeatService.h"
#include "mozilla/StaticPtr.h"
#include "nsIServiceManager.h"
using namespace mozilla;
static StaticRefPtr<nsRepeatService> gRepeatService;
static StaticAutoPtr<nsRepeatService> gRepeatService;
nsRepeatService::nsRepeatService()
: mCallback(nullptr), mCallbackData(nullptr)
@ -71,23 +72,27 @@ void nsRepeatService::Stop(Callback aCallback, void* aCallbackData)
mCallbackData = nullptr;
}
NS_IMETHODIMP nsRepeatService::Notify(nsITimer *timer)
{
// do callback
if (mCallback)
mCallback(mCallbackData);
// start timer again.
InitTimerCallback(REPEAT_DELAY);
return NS_OK;
}
void
nsRepeatService::InitTimerCallback(uint32_t aInitialDelay)
{
if (mRepeatTimer) {
mRepeatTimer->InitWithCallback(this, aInitialDelay, nsITimer::TYPE_ONE_SHOT);
if (!mRepeatTimer) {
return;
}
}
NS_IMPL_ISUPPORTS(nsRepeatService, nsITimerCallback)
mRepeatTimer->InitWithFuncCallback([](nsITimer* aTimer, void* aClosure) {
// Use gRepeatService instead of nsRepeatService::GetInstance() (because
// we don't want nsRepeatService::GetInstance() to re-create a new instance
// for us, if we happen to get invoked after nsRepeatService::Shutdown() has
// nulled out gRepeatService).
nsRepeatService* rs = gRepeatService;
if (!rs) {
return;
}
if (rs->mCallback) {
rs->mCallback(rs->mCallbackData);
}
rs->InitTimerCallback(REPEAT_DELAY);
}, nullptr, aInitialDelay, nsITimer::TYPE_ONE_SHOT);
}

Просмотреть файл

@ -22,13 +22,12 @@
class nsITimer;
class nsRepeatService final : public nsITimerCallback
class nsRepeatService final
{
public:
typedef void (* Callback)(void* aData);
NS_DECL_NSITIMERCALLBACK
~nsRepeatService();
// Start dispatching timer events to the callback. There is no memory
// management of aData here; it is the caller's responsibility to call
@ -43,11 +42,8 @@ public:
static nsRepeatService* GetInstance();
static void Shutdown();
NS_DECL_ISUPPORTS
protected:
nsRepeatService();
virtual ~nsRepeatService();
private:
// helper function to initialize callback function to mRepeatTimer