no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / layout / xul / nsRepeatService.cpp
blob5495a0a10f314c02d0a4415a7bd7514a375b9d92
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 //
8 // Eric Vaughan
9 // Netscape Communications
11 // See documentation in associated header file
14 #include "nsRepeatService.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/dom/Document.h"
18 using namespace mozilla;
20 static StaticAutoPtr<nsRepeatService> gRepeatService;
22 nsRepeatService::nsRepeatService()
23 : mCallback(nullptr), mCallbackData(nullptr) {}
25 nsRepeatService::~nsRepeatService() {
26 NS_ASSERTION(!mCallback && !mCallbackData,
27 "Callback was not removed before shutdown");
30 /* static */
31 nsRepeatService* nsRepeatService::GetInstance() {
32 if (!gRepeatService) {
33 gRepeatService = new nsRepeatService();
35 return gRepeatService;
38 /*static*/
39 void nsRepeatService::Shutdown() { gRepeatService = nullptr; }
41 void nsRepeatService::Start(Callback aCallback, void* aCallbackData,
42 dom::Document* aDocument,
43 const nsACString& aCallbackName,
44 uint32_t aInitialDelay) {
45 MOZ_ASSERT(aCallback != nullptr, "null ptr");
47 mCallback = aCallback;
48 mCallbackData = aCallbackData;
49 mCallbackName = aCallbackName;
51 mRepeatTimer = NS_NewTimer(GetMainThreadSerialEventTarget());
53 if (mRepeatTimer) {
54 InitTimerCallback(aInitialDelay);
58 void nsRepeatService::Stop(Callback aCallback, void* aCallbackData) {
59 if (mCallback != aCallback || mCallbackData != aCallbackData) return;
61 // printf("Stopping repeat timer\n");
62 if (mRepeatTimer) {
63 mRepeatTimer->Cancel();
64 mRepeatTimer = nullptr;
66 mCallback = nullptr;
67 mCallbackData = nullptr;
70 void nsRepeatService::InitTimerCallback(uint32_t aInitialDelay) {
71 if (!mRepeatTimer) {
72 return;
75 mRepeatTimer->InitWithNamedFuncCallback(
76 [](nsITimer* aTimer, void* aClosure) {
77 // Use gRepeatService instead of nsRepeatService::GetInstance() (because
78 // we don't want nsRepeatService::GetInstance() to re-create a new
79 // instance for us, if we happen to get invoked after
80 // nsRepeatService::Shutdown() has nulled out gRepeatService).
81 nsRepeatService* rs = gRepeatService;
82 if (!rs) {
83 return;
86 if (rs->mCallback) {
87 rs->mCallback(rs->mCallbackData);
90 rs->InitTimerCallback(REPEAT_DELAY);
92 nullptr, aInitialDelay, nsITimer::TYPE_ONE_SHOT, mCallbackName.Data());