Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/xul/nsRepeatService.cpp
Line
Count
Source (jump to first uncovered line)
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
23
nsRepeatService::nsRepeatService()
24
: mCallback(nullptr), mCallbackData(nullptr)
25
0
{
26
0
}
27
28
nsRepeatService::~nsRepeatService()
29
0
{
30
0
  NS_ASSERTION(!mCallback && !mCallbackData, "Callback was not removed before shutdown");
31
0
}
32
33
/* static */ nsRepeatService*
34
nsRepeatService::GetInstance()
35
0
{
36
0
  if (!gRepeatService) {
37
0
    gRepeatService = new nsRepeatService();
38
0
  }
39
0
  return gRepeatService;
40
0
}
41
42
/*static*/ void
43
nsRepeatService::Shutdown()
44
0
{
45
0
  gRepeatService = nullptr;
46
0
}
47
48
void
49
nsRepeatService::Start(Callback aCallback, void* aCallbackData,
50
                       nsIDocument* aDocument, const nsACString& aCallbackName,
51
                       uint32_t aInitialDelay)
52
0
{
53
0
  MOZ_ASSERT(aCallback != nullptr, "null ptr");
54
0
55
0
  mCallback = aCallback;
56
0
  mCallbackData = aCallbackData;
57
0
  mCallbackName = aCallbackName;
58
0
59
0
  mRepeatTimer = NS_NewTimer(
60
0
    aDocument->EventTargetFor(TaskCategory::Other));
61
0
62
0
  if (mRepeatTimer)  {
63
0
    InitTimerCallback(aInitialDelay);
64
0
  }
65
0
}
66
67
void nsRepeatService::Stop(Callback aCallback, void* aCallbackData)
68
0
{
69
0
  if (mCallback != aCallback || mCallbackData != aCallbackData)
70
0
    return;
71
0
72
0
  //printf("Stopping repeat timer\n");
73
0
  if (mRepeatTimer) {
74
0
     mRepeatTimer->Cancel();
75
0
     mRepeatTimer = nullptr;
76
0
  }
77
0
  mCallback = nullptr;
78
0
  mCallbackData = nullptr;
79
0
}
80
81
void
82
nsRepeatService::InitTimerCallback(uint32_t aInitialDelay)
83
0
{
84
0
  if (!mRepeatTimer) {
85
0
    return;
86
0
  }
87
0
88
0
  mRepeatTimer->InitWithNamedFuncCallback([](nsITimer* aTimer, void* aClosure) {
89
0
    // Use gRepeatService instead of nsRepeatService::GetInstance() (because
90
0
    // we don't want nsRepeatService::GetInstance() to re-create a new instance
91
0
    // for us, if we happen to get invoked after nsRepeatService::Shutdown() has
92
0
    // nulled out gRepeatService).
93
0
    nsRepeatService* rs = gRepeatService;
94
0
    if (!rs) {
95
0
      return;
96
0
    }
97
0
98
0
    if (rs->mCallback) {
99
0
      rs->mCallback(rs->mCallbackData);
100
0
    }
101
0
102
0
    rs->InitTimerCallback(REPEAT_DELAY);
103
0
  }, nullptr, aInitialDelay, nsITimer::TYPE_ONE_SHOT, mCallbackName.Data());
104
0
}