michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // michael@0: // Eric Vaughan michael@0: // Netscape Communications michael@0: // michael@0: // See documentation in associated header file michael@0: // michael@0: michael@0: #include "nsRepeatService.h" michael@0: #include "nsIServiceManager.h" michael@0: michael@0: nsRepeatService* nsRepeatService::gInstance = nullptr; michael@0: michael@0: nsRepeatService::nsRepeatService() michael@0: : mCallback(nullptr), mCallbackData(nullptr) michael@0: { michael@0: } michael@0: michael@0: nsRepeatService::~nsRepeatService() michael@0: { michael@0: NS_ASSERTION(!mCallback && !mCallbackData, "Callback was not removed before shutdown"); michael@0: } michael@0: michael@0: nsRepeatService* michael@0: nsRepeatService::GetInstance() michael@0: { michael@0: if (!gInstance) { michael@0: gInstance = new nsRepeatService(); michael@0: NS_IF_ADDREF(gInstance); michael@0: } michael@0: return gInstance; michael@0: } michael@0: michael@0: /*static*/ void michael@0: nsRepeatService::Shutdown() michael@0: { michael@0: NS_IF_RELEASE(gInstance); michael@0: } michael@0: michael@0: void nsRepeatService::Start(Callback aCallback, void* aCallbackData, michael@0: uint32_t aInitialDelay) michael@0: { michael@0: NS_PRECONDITION(aCallback != nullptr, "null ptr"); michael@0: michael@0: mCallback = aCallback; michael@0: mCallbackData = aCallbackData; michael@0: nsresult rv; michael@0: mRepeatTimer = do_CreateInstance("@mozilla.org/timer;1", &rv); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: mRepeatTimer->InitWithCallback(this, aInitialDelay, nsITimer::TYPE_ONE_SHOT); michael@0: } michael@0: } michael@0: michael@0: void nsRepeatService::Stop(Callback aCallback, void* aCallbackData) michael@0: { michael@0: if (mCallback != aCallback || mCallbackData != aCallbackData) michael@0: return; michael@0: michael@0: //printf("Stopping repeat timer\n"); michael@0: if (mRepeatTimer) { michael@0: mRepeatTimer->Cancel(); michael@0: mRepeatTimer = nullptr; michael@0: } michael@0: mCallback = nullptr; michael@0: mCallbackData = nullptr; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsRepeatService::Notify(nsITimer *timer) michael@0: { michael@0: // do callback michael@0: if (mCallback) michael@0: mCallback(mCallbackData); michael@0: michael@0: // start timer again. michael@0: if (mRepeatTimer) { michael@0: mRepeatTimer->InitWithCallback(this, REPEAT_DELAY, nsITimer::TYPE_ONE_SHOT); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsRepeatService, nsITimerCallback)