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 "mozilla/dom/Document.h"
17 
18 using namespace mozilla;
19 
20 static StaticAutoPtr<nsRepeatService> gRepeatService;
21 
nsRepeatService()22 nsRepeatService::nsRepeatService()
23     : mCallback(nullptr), mCallbackData(nullptr) {}
24 
~nsRepeatService()25 nsRepeatService::~nsRepeatService() {
26   NS_ASSERTION(!mCallback && !mCallbackData,
27                "Callback was not removed before shutdown");
28 }
29 
30 /* static */
GetInstance()31 nsRepeatService* nsRepeatService::GetInstance() {
32   if (!gRepeatService) {
33     gRepeatService = new nsRepeatService();
34   }
35   return gRepeatService;
36 }
37 
38 /*static*/
Shutdown()39 void nsRepeatService::Shutdown() { gRepeatService = nullptr; }
40 
Start(Callback aCallback,void * aCallbackData,dom::Document * aDocument,const nsACString & aCallbackName,uint32_t aInitialDelay)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");
46 
47   mCallback = aCallback;
48   mCallbackData = aCallbackData;
49   mCallbackName = aCallbackName;
50 
51   mRepeatTimer = NS_NewTimer(aDocument->EventTargetFor(TaskCategory::Other));
52 
53   if (mRepeatTimer) {
54     InitTimerCallback(aInitialDelay);
55   }
56 }
57 
Stop(Callback aCallback,void * aCallbackData)58 void nsRepeatService::Stop(Callback aCallback, void* aCallbackData) {
59   if (mCallback != aCallback || mCallbackData != aCallbackData) return;
60 
61   // printf("Stopping repeat timer\n");
62   if (mRepeatTimer) {
63     mRepeatTimer->Cancel();
64     mRepeatTimer = nullptr;
65   }
66   mCallback = nullptr;
67   mCallbackData = nullptr;
68 }
69 
InitTimerCallback(uint32_t aInitialDelay)70 void nsRepeatService::InitTimerCallback(uint32_t aInitialDelay) {
71   if (!mRepeatTimer) {
72     return;
73   }
74 
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;
84         }
85 
86         if (rs->mCallback) {
87           rs->mCallback(rs->mCallbackData);
88         }
89 
90         rs->InitTimerCallback(REPEAT_DELAY);
91       },
92       nullptr, aInitialDelay, nsITimer::TYPE_ONE_SHOT, mCallbackName.Data());
93 }
94