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/. */
6
7 //
8 // Eric Vaughan
9 // Netscape Communications
10 //
11 // See documentation in associated header file
12 //
13
14 #include "nsRepeatService.h"
15 #include "mozilla/StaticPtr.h"
16 #include "nsIDocument.h"
17 #include "nsIServiceManager.h"
18
19 using namespace mozilla;
20
21 static StaticAutoPtr<nsRepeatService> gRepeatService;
22
nsRepeatService()23 nsRepeatService::nsRepeatService()
24 : mCallback(nullptr), mCallbackData(nullptr) {}
25
~nsRepeatService()26 nsRepeatService::~nsRepeatService() {
27 NS_ASSERTION(!mCallback && !mCallbackData,
28 "Callback was not removed before shutdown");
29 }
30
GetInstance()31 /* static */ nsRepeatService* nsRepeatService::GetInstance() {
32 if (!gRepeatService) {
33 gRepeatService = new nsRepeatService();
34 }
35 return gRepeatService;
36 }
37
Shutdown()38 /*static*/ void nsRepeatService::Shutdown() { gRepeatService = nullptr; }
39
Start(Callback aCallback,void * aCallbackData,nsIDocument * aDocument,const nsACString & aCallbackName,uint32_t aInitialDelay)40 void nsRepeatService::Start(Callback aCallback, void* aCallbackData,
41 nsIDocument* aDocument,
42 const nsACString& aCallbackName,
43 uint32_t aInitialDelay) {
44 NS_PRECONDITION(aCallback != nullptr, "null ptr");
45
46 mCallback = aCallback;
47 mCallbackData = aCallbackData;
48 mCallbackName = aCallbackName;
49
50 mRepeatTimer = NS_NewTimer(aDocument->EventTargetFor(TaskCategory::Other));
51
52 if (mRepeatTimer) {
53 InitTimerCallback(aInitialDelay);
54 }
55 }
56
Stop(Callback aCallback,void * aCallbackData)57 void nsRepeatService::Stop(Callback aCallback, void* aCallbackData) {
58 if (mCallback != aCallback || mCallbackData != aCallbackData) return;
59
60 // printf("Stopping repeat timer\n");
61 if (mRepeatTimer) {
62 mRepeatTimer->Cancel();
63 mRepeatTimer = nullptr;
64 }
65 mCallback = nullptr;
66 mCallbackData = nullptr;
67 }
68
InitTimerCallback(uint32_t aInitialDelay)69 void nsRepeatService::InitTimerCallback(uint32_t aInitialDelay) {
70 if (!mRepeatTimer) {
71 return;
72 }
73
74 mRepeatTimer->InitWithNamedFuncCallback(
75 [](nsITimer* aTimer, void* aClosure) {
76 // Use gRepeatService instead of nsRepeatService::GetInstance() (because
77 // we don't want nsRepeatService::GetInstance() to re-create a new
78 // instance for us, if we happen to get invoked after
79 // nsRepeatService::Shutdown() has nulled out gRepeatService).
80 nsRepeatService* rs = gRepeatService;
81 if (!rs) {
82 return;
83 }
84
85 if (rs->mCallback) {
86 rs->mCallback(rs->mCallbackData);
87 }
88
89 rs->InitTimerCallback(REPEAT_DELAY);
90 },
91 nullptr, aInitialDelay, nsITimer::TYPE_ONE_SHOT, mCallbackName.Data());
92 }
93