tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Timeout.cpp (2846B)


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