Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | // |
michael@0 | 7 | // Eric Vaughan |
michael@0 | 8 | // Netscape Communications |
michael@0 | 9 | // |
michael@0 | 10 | // See documentation in associated header file |
michael@0 | 11 | // |
michael@0 | 12 | |
michael@0 | 13 | #include "nsRepeatService.h" |
michael@0 | 14 | #include "nsIServiceManager.h" |
michael@0 | 15 | |
michael@0 | 16 | nsRepeatService* nsRepeatService::gInstance = nullptr; |
michael@0 | 17 | |
michael@0 | 18 | nsRepeatService::nsRepeatService() |
michael@0 | 19 | : mCallback(nullptr), mCallbackData(nullptr) |
michael@0 | 20 | { |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | nsRepeatService::~nsRepeatService() |
michael@0 | 24 | { |
michael@0 | 25 | NS_ASSERTION(!mCallback && !mCallbackData, "Callback was not removed before shutdown"); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | nsRepeatService* |
michael@0 | 29 | nsRepeatService::GetInstance() |
michael@0 | 30 | { |
michael@0 | 31 | if (!gInstance) { |
michael@0 | 32 | gInstance = new nsRepeatService(); |
michael@0 | 33 | NS_IF_ADDREF(gInstance); |
michael@0 | 34 | } |
michael@0 | 35 | return gInstance; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /*static*/ void |
michael@0 | 39 | nsRepeatService::Shutdown() |
michael@0 | 40 | { |
michael@0 | 41 | NS_IF_RELEASE(gInstance); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | void nsRepeatService::Start(Callback aCallback, void* aCallbackData, |
michael@0 | 45 | uint32_t aInitialDelay) |
michael@0 | 46 | { |
michael@0 | 47 | NS_PRECONDITION(aCallback != nullptr, "null ptr"); |
michael@0 | 48 | |
michael@0 | 49 | mCallback = aCallback; |
michael@0 | 50 | mCallbackData = aCallbackData; |
michael@0 | 51 | nsresult rv; |
michael@0 | 52 | mRepeatTimer = do_CreateInstance("@mozilla.org/timer;1", &rv); |
michael@0 | 53 | |
michael@0 | 54 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 55 | mRepeatTimer->InitWithCallback(this, aInitialDelay, nsITimer::TYPE_ONE_SHOT); |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | void nsRepeatService::Stop(Callback aCallback, void* aCallbackData) |
michael@0 | 60 | { |
michael@0 | 61 | if (mCallback != aCallback || mCallbackData != aCallbackData) |
michael@0 | 62 | return; |
michael@0 | 63 | |
michael@0 | 64 | //printf("Stopping repeat timer\n"); |
michael@0 | 65 | if (mRepeatTimer) { |
michael@0 | 66 | mRepeatTimer->Cancel(); |
michael@0 | 67 | mRepeatTimer = nullptr; |
michael@0 | 68 | } |
michael@0 | 69 | mCallback = nullptr; |
michael@0 | 70 | mCallbackData = nullptr; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | NS_IMETHODIMP nsRepeatService::Notify(nsITimer *timer) |
michael@0 | 74 | { |
michael@0 | 75 | // do callback |
michael@0 | 76 | if (mCallback) |
michael@0 | 77 | mCallback(mCallbackData); |
michael@0 | 78 | |
michael@0 | 79 | // start timer again. |
michael@0 | 80 | if (mRepeatTimer) { |
michael@0 | 81 | mRepeatTimer->InitWithCallback(this, REPEAT_DELAY, nsITimer::TYPE_ONE_SHOT); |
michael@0 | 82 | } |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | NS_IMPL_ISUPPORTS(nsRepeatService, nsITimerCallback) |