Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / Timeout.cpp
blobac1bbca24c0caac724b35e9640833d7e6c01901d
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 #include "Timeout.h"
9 #include "mozilla/dom/TimeoutManager.h"
10 #include "GeckoProfiler.h"
12 namespace mozilla::dom {
14 Timeout::Timeout()
15 : mTimeoutId(0),
16 mFiringId(TimeoutManager::InvalidFiringId),
17 #ifdef DEBUG
18 mFiringIndex(-1),
19 #endif
20 mPopupState(PopupBlocker::openAllowed),
21 mReason(Reason::eTimeoutOrInterval),
22 mNestingLevel(0),
23 mCleared(false),
24 mRunning(false),
25 mIsInterval(false) {
28 NS_IMPL_CYCLE_COLLECTION_CLASS(Timeout)
30 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Timeout)
31 NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
32 NS_IMPL_CYCLE_COLLECTION_UNLINK(mScriptHandler)
33 if (tmp->isInList()) {
34 tmp->remove();
36 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Timeout)
39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
40 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mScriptHandler)
41 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
43 void Timeout::SetWhenOrTimeRemaining(const TimeStamp& aBaseTime,
44 const TimeDuration& aDelay) {
45 MOZ_DIAGNOSTIC_ASSERT(mWindow);
46 mSubmitTime = aBaseTime;
48 mSubmitTime = aBaseTime;
49 if (profiler_is_active()) {
50 mCause = profiler_capture_backtrace();
53 // If we are frozen simply set mTimeRemaining to be the "time remaining" in
54 // the timeout (i.e., the interval itself). This will be used to create a
55 // new mWhen time when the window is thawed. The end effect is that time does
56 // not appear to pass for frozen windows.
57 if (mWindow->IsFrozen()) {
58 mWhen = TimeStamp();
59 mTimeRemaining = aDelay;
60 return;
63 // Since we are not frozen we must set a precise mWhen target wakeup
64 // time. Even if we are suspended we want to use this target time so
65 // that it appears time passes while suspended.
66 mWhen = aBaseTime + aDelay;
67 mTimeRemaining = TimeDuration(0);
70 const TimeStamp& Timeout::When() const {
71 MOZ_DIAGNOSTIC_ASSERT(!mWhen.IsNull());
72 // Note, mWindow->IsFrozen() can be true here. The Freeze() method calls
73 // When() to calculate the delay to populate mTimeRemaining.
74 return mWhen;
77 const TimeStamp& Timeout::SubmitTime() const { return mSubmitTime; }
79 const TimeDuration& Timeout::TimeRemaining() const {
80 MOZ_DIAGNOSTIC_ASSERT(mWhen.IsNull());
81 // Note, mWindow->IsFrozen() can be false here. The Thaw() method calls
82 // TimeRemaining() to calculate the new When() value.
83 return mTimeRemaining;
86 } // namespace mozilla::dom