layout/xul/nsRepeatService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/xul/nsRepeatService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +//
    1.10 +// Eric Vaughan
    1.11 +// Netscape Communications
    1.12 +//
    1.13 +// See documentation in associated header file
    1.14 +//
    1.15 +
    1.16 +#include "nsRepeatService.h"
    1.17 +#include "nsIServiceManager.h"
    1.18 +
    1.19 +nsRepeatService* nsRepeatService::gInstance = nullptr;
    1.20 +
    1.21 +nsRepeatService::nsRepeatService()
    1.22 +: mCallback(nullptr), mCallbackData(nullptr)
    1.23 +{
    1.24 +}
    1.25 +
    1.26 +nsRepeatService::~nsRepeatService()
    1.27 +{
    1.28 +  NS_ASSERTION(!mCallback && !mCallbackData, "Callback was not removed before shutdown");
    1.29 +}
    1.30 +
    1.31 +nsRepeatService* 
    1.32 +nsRepeatService::GetInstance()
    1.33 +{
    1.34 +  if (!gInstance) {
    1.35 +    gInstance = new nsRepeatService();
    1.36 +    NS_IF_ADDREF(gInstance);
    1.37 +  }
    1.38 +  return gInstance;
    1.39 +}
    1.40 +
    1.41 +/*static*/ void
    1.42 +nsRepeatService::Shutdown()
    1.43 +{
    1.44 +  NS_IF_RELEASE(gInstance);
    1.45 +}
    1.46 +
    1.47 +void nsRepeatService::Start(Callback aCallback, void* aCallbackData,
    1.48 +                            uint32_t aInitialDelay)
    1.49 +{
    1.50 +  NS_PRECONDITION(aCallback != nullptr, "null ptr");
    1.51 +
    1.52 +  mCallback = aCallback;
    1.53 +  mCallbackData = aCallbackData;
    1.54 +  nsresult rv;
    1.55 +  mRepeatTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
    1.56 +
    1.57 +  if (NS_SUCCEEDED(rv))  {
    1.58 +    mRepeatTimer->InitWithCallback(this, aInitialDelay, nsITimer::TYPE_ONE_SHOT);
    1.59 +  }
    1.60 +}
    1.61 +
    1.62 +void nsRepeatService::Stop(Callback aCallback, void* aCallbackData)
    1.63 +{
    1.64 +  if (mCallback != aCallback || mCallbackData != aCallbackData)
    1.65 +    return;
    1.66 +
    1.67 +  //printf("Stopping repeat timer\n");
    1.68 +  if (mRepeatTimer) {
    1.69 +     mRepeatTimer->Cancel();
    1.70 +     mRepeatTimer = nullptr;
    1.71 +  }
    1.72 +  mCallback = nullptr;
    1.73 +  mCallbackData = nullptr;
    1.74 +}
    1.75 +
    1.76 +NS_IMETHODIMP nsRepeatService::Notify(nsITimer *timer)
    1.77 +{
    1.78 +  // do callback
    1.79 +  if (mCallback)
    1.80 +    mCallback(mCallbackData);
    1.81 +
    1.82 +  // start timer again.
    1.83 +  if (mRepeatTimer) {
    1.84 +    mRepeatTimer->InitWithCallback(this, REPEAT_DELAY, nsITimer::TYPE_ONE_SHOT);
    1.85 +  }
    1.86 +  return NS_OK;
    1.87 +}
    1.88 +
    1.89 +NS_IMPL_ISUPPORTS(nsRepeatService, nsITimerCallback)

mercurial