Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/TimeoutExecutor.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
#include "TimeoutExecutor.h"
8
9
#include "mozilla/dom/TimeoutManager.h"
10
#include "nsComponentManagerUtils.h"
11
#include "nsIEventTarget.h"
12
#include "nsString.h"
13
14
namespace mozilla {
15
namespace dom {
16
17
NS_IMPL_ISUPPORTS(TimeoutExecutor, nsIRunnable, nsITimerCallback, nsINamed)
18
19
TimeoutExecutor::~TimeoutExecutor()
20
0
{
21
0
  // The TimeoutManager should keep the Executor alive until its destroyed,
22
0
  // and then call Shutdown() explicitly.
23
0
  MOZ_DIAGNOSTIC_ASSERT(mMode == Mode::Shutdown);
24
0
  MOZ_DIAGNOSTIC_ASSERT(!mOwner);
25
0
  MOZ_DIAGNOSTIC_ASSERT(!mTimer);
26
0
}
27
28
nsresult
29
TimeoutExecutor::ScheduleImmediate(const TimeStamp& aDeadline,
30
                                   const TimeStamp& aNow)
31
0
{
32
0
  MOZ_DIAGNOSTIC_ASSERT(mDeadline.IsNull());
33
0
  MOZ_DIAGNOSTIC_ASSERT(mMode == Mode::None);
34
0
  MOZ_DIAGNOSTIC_ASSERT(aDeadline <= (aNow + mAllowedEarlyFiringTime));
35
0
36
0
  nsresult rv =
37
0
    mOwner->EventTarget()->Dispatch(this, nsIEventTarget::DISPATCH_NORMAL);
38
0
  NS_ENSURE_SUCCESS(rv, rv);
39
0
40
0
  mMode = Mode::Immediate;
41
0
  mDeadline = aDeadline;
42
0
43
0
  return NS_OK;
44
0
}
45
46
nsresult
47
TimeoutExecutor::ScheduleDelayed(const TimeStamp& aDeadline,
48
                                 const TimeStamp& aNow,
49
                                 const TimeDuration& aMinDelay)
50
0
{
51
0
  MOZ_DIAGNOSTIC_ASSERT(mDeadline.IsNull());
52
0
  MOZ_DIAGNOSTIC_ASSERT(mMode == Mode::None);
53
0
  MOZ_DIAGNOSTIC_ASSERT(!aMinDelay.IsZero() ||
54
0
                        aDeadline > (aNow + mAllowedEarlyFiringTime));
55
0
56
0
  nsresult rv = NS_OK;
57
0
58
0
  if (!mTimer) {
59
0
    mTimer = NS_NewTimer();
60
0
    NS_ENSURE_TRUE(mTimer, NS_ERROR_OUT_OF_MEMORY);
61
0
62
0
    uint32_t earlyMicros = 0;
63
0
    MOZ_ALWAYS_SUCCEEDS(mTimer->GetAllowedEarlyFiringMicroseconds(&earlyMicros));
64
0
    mAllowedEarlyFiringTime = TimeDuration::FromMicroseconds(earlyMicros);
65
0
  }
66
0
67
0
  // Always call Cancel() in case we are re-using a timer.  Otherwise
68
0
  // the subsequent SetTarget() may fail.
69
0
  rv = mTimer->Cancel();
70
0
  NS_ENSURE_SUCCESS(rv, rv);
71
0
72
0
  rv = mTimer->SetTarget(mOwner->EventTarget());
73
0
  NS_ENSURE_SUCCESS(rv, rv);
74
0
75
0
  // Calculate the delay based on the deadline and current time.  If we have
76
0
  // a minimum delay set then clamp to that value.
77
0
  //
78
0
  // Note, we don't actually adjust our mDeadline for the minimum delay, just
79
0
  // the nsITimer value.  This is necessary to avoid lots of needless
80
0
  // rescheduling if more deadlines come in between now and the minimum delay
81
0
  // firing time.
82
0
  TimeDuration delay = TimeDuration::Max(aMinDelay, aDeadline - aNow);
83
0
84
0
  // Note, we cannot use the normal nsITimer init methods that take
85
0
  // integer milliseconds.  We need higher precision.  Consider this
86
0
  // situation:
87
0
  //
88
0
  // 1. setTimeout(f, 1);
89
0
  // 2. do some work for 500us
90
0
  // 3. setTimeout(g, 1);
91
0
  //
92
0
  // This should fire f() and g() 500us apart.
93
0
  //
94
0
  // In the past worked because each setTimeout() got its own nsITimer.  The 1ms
95
0
  // was preserved and passed through to nsITimer which converted it to a
96
0
  // TimeStamp, etc.
97
0
  //
98
0
  // Now, however, there is only one nsITimer.  We fire f() and then try to
99
0
  // schedule a new nsITimer for g().  Its only 500us in the future, though.  We
100
0
  // must be able to pass this fractional value to nsITimer in order to get an
101
0
  // accurate wakeup time.
102
0
  rv = mTimer->InitHighResolutionWithCallback(this, delay,
103
0
                                              nsITimer::TYPE_ONE_SHOT);
104
0
  NS_ENSURE_SUCCESS(rv, rv);
105
0
106
0
  mMode = Mode::Delayed;
107
0
  mDeadline = aDeadline;
108
0
109
0
  return NS_OK;
110
0
}
111
112
nsresult
113
TimeoutExecutor::Schedule(const TimeStamp& aDeadline,
114
                          const TimeDuration& aMinDelay)
115
0
{
116
0
  TimeStamp now(TimeStamp::Now());
117
0
118
0
  // Schedule an immediate runnable if the desired deadline has passed
119
0
  // or is slightly in the future.  This is similar to how nsITimer will
120
0
  // fire timers early based on the interval resolution.
121
0
  if (aMinDelay.IsZero() && aDeadline <= (now + mAllowedEarlyFiringTime)) {
122
0
    return ScheduleImmediate(aDeadline, now);
123
0
  }
124
0
125
0
  return ScheduleDelayed(aDeadline, now, aMinDelay);
126
0
}
127
128
nsresult
129
TimeoutExecutor::MaybeReschedule(const TimeStamp& aDeadline,
130
                                 const TimeDuration& aMinDelay)
131
0
{
132
0
  MOZ_DIAGNOSTIC_ASSERT(!mDeadline.IsNull());
133
0
  MOZ_DIAGNOSTIC_ASSERT(mMode == Mode::Immediate ||
134
0
                        mMode == Mode::Delayed);
135
0
136
0
  if (aDeadline >= mDeadline) {
137
0
    return NS_OK;
138
0
  }
139
0
140
0
  if (mMode == Mode::Immediate) {
141
0
    // Don't reduce the deadline here as we want to execute the
142
0
    // timer we originally scheduled even if its a few microseconds
143
0
    // in the future.
144
0
    return NS_OK;
145
0
  }
146
0
147
0
  Cancel();
148
0
  return Schedule(aDeadline, aMinDelay);
149
0
}
150
151
void
152
TimeoutExecutor::MaybeExecute()
153
0
{
154
0
  MOZ_DIAGNOSTIC_ASSERT(mMode != Mode::Shutdown && mMode != Mode::None);
155
0
  MOZ_DIAGNOSTIC_ASSERT(mOwner);
156
0
  MOZ_DIAGNOSTIC_ASSERT(!mDeadline.IsNull());
157
0
158
0
  TimeStamp deadline(mDeadline);
159
0
160
0
  // Sometimes nsITimer or canceled timers will fire too early.  If this
161
0
  // happens then just cap our deadline to our maximum time in the future
162
0
  // and proceed.  If there are no timers ready we will get rescheduled
163
0
  // by TimeoutManager.
164
0
  TimeStamp now(TimeStamp::Now());
165
0
  TimeStamp limit = now + mAllowedEarlyFiringTime;
166
0
  if (deadline > limit) {
167
0
    deadline = limit;
168
0
  }
169
0
170
0
  Cancel();
171
0
172
0
  mOwner->RunTimeout(now, deadline);
173
0
}
174
175
TimeoutExecutor::TimeoutExecutor(TimeoutManager* aOwner)
176
  : mOwner(aOwner)
177
  , mMode(Mode::None)
178
0
{
179
0
  MOZ_DIAGNOSTIC_ASSERT(mOwner);
180
0
}
181
182
void
183
TimeoutExecutor::Shutdown()
184
0
{
185
0
  mOwner = nullptr;
186
0
187
0
  if (mTimer) {
188
0
    mTimer->Cancel();
189
0
    mTimer = nullptr;
190
0
  }
191
0
192
0
  mMode = Mode::Shutdown;
193
0
  mDeadline = TimeStamp();
194
0
}
195
196
nsresult
197
TimeoutExecutor::MaybeSchedule(const TimeStamp& aDeadline,
198
                               const TimeDuration& aMinDelay)
199
0
{
200
0
  MOZ_DIAGNOSTIC_ASSERT(!aDeadline.IsNull());
201
0
202
0
  if (mMode == Mode::Shutdown) {
203
0
    return NS_OK;
204
0
  }
205
0
206
0
  if (mMode == Mode::Immediate || mMode == Mode::Delayed) {
207
0
    return MaybeReschedule(aDeadline, aMinDelay);
208
0
  }
209
0
210
0
  return Schedule(aDeadline, aMinDelay);
211
0
}
212
213
void
214
TimeoutExecutor::Cancel()
215
0
{
216
0
  if (mTimer) {
217
0
    mTimer->Cancel();
218
0
  }
219
0
  mMode = Mode::None;
220
0
  mDeadline = TimeStamp();
221
0
}
222
223
NS_IMETHODIMP
224
TimeoutExecutor::Run()
225
0
{
226
0
  // If the executor is canceled and then rescheduled its possible to get
227
0
  // spurious executions here.  Ignore these unless our current mode matches.
228
0
  if (mMode == Mode::Immediate) {
229
0
    MaybeExecute();
230
0
  }
231
0
  return NS_OK;
232
0
}
233
234
NS_IMETHODIMP
235
TimeoutExecutor::Notify(nsITimer* aTimer)
236
0
{
237
0
  // If the executor is canceled and then rescheduled its possible to get
238
0
  // spurious executions here.  Ignore these unless our current mode matches.
239
0
  if (mMode == Mode::Delayed) {
240
0
    MaybeExecute();
241
0
  }
242
0
  return NS_OK;
243
0
}
244
245
NS_IMETHODIMP
246
TimeoutExecutor::GetName(nsACString& aNameOut)
247
0
{
248
0
  aNameOut.AssignLiteral("TimeoutExecutor Runnable");
249
0
  return NS_OK;
250
0
}
251
252
} // namespace dom
253
} // namespace mozilla