mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-26 14:22:01 +00:00
Bug 1321903 - Refactor the timeout/interval management code out of nsGlobalWindow; r=bkelly
This code now lives in TimeoutManager. Note that this is a transition state, the Timeout list management code also needs to be refactored out later. In order to simplify the lifetime management of the new class, its lifetime is equal to the lifetime of its containing nsGlobalWindow. In a few places where we need to dispatch runnables to do asynchronous work on this object, we hold the containing window alive to guarantee safety. This patch also removes a bit of dead code that was left over from the code removed in bug 1281793. See: https://hg.mozilla.org/mozilla-central/rev/0ac748f4d677#l1.63
This commit is contained in:
parent
51f7b58749
commit
443e426d7c
@ -10,6 +10,7 @@
|
||||
#include "mozilla/dom/IdleDeadline.h"
|
||||
#include "mozilla/dom/Performance.h"
|
||||
#include "mozilla/dom/PerformanceTiming.h"
|
||||
#include "mozilla/dom/TimeoutManager.h"
|
||||
#include "mozilla/dom/WindowBinding.h"
|
||||
#include "nsComponentManagerUtils.h"
|
||||
#include "nsGlobalWindow.h"
|
||||
@ -63,7 +64,7 @@ nsresult
|
||||
IdleRequest::SetTimeout(uint32_t aTimeout)
|
||||
{
|
||||
int32_t handle;
|
||||
nsresult rv = nsGlobalWindow::Cast(mWindow)->SetTimeoutOrInterval(
|
||||
nsresult rv = mWindow->TimeoutManager().SetTimeout(
|
||||
this, aTimeout, false, Timeout::Reason::eIdleCallbackTimeout, &handle);
|
||||
mTimeoutHandle = Some(handle);
|
||||
|
||||
@ -125,7 +126,7 @@ void
|
||||
IdleRequest::CancelTimeout()
|
||||
{
|
||||
if (mTimeoutHandle.isSome()) {
|
||||
nsGlobalWindow::Cast(mWindow)->ClearTimeoutOrInterval(
|
||||
mWindow->TimeoutManager().ClearTimeout(
|
||||
mTimeoutHandle.value(), Timeout::Reason::eIdleCallbackTimeout);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include "nsGlobalWindow.h"
|
||||
#include "nsITimeoutHandler.h"
|
||||
#include "nsITimer.h"
|
||||
#include "mozilla/dom/TimeoutManager.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
@ -60,7 +61,7 @@ void
|
||||
TimerCallback(nsITimer*, void* aClosure)
|
||||
{
|
||||
RefPtr<Timeout> timeout = (Timeout*)aClosure;
|
||||
timeout->mWindow->RunTimeout(timeout);
|
||||
timeout->mWindow->AsInner()->TimeoutManager().RunTimeout(timeout);
|
||||
}
|
||||
|
||||
void
|
||||
|
897
dom/base/TimeoutManager.cpp
Normal file
897
dom/base/TimeoutManager.cpp
Normal file
@ -0,0 +1,897 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "TimeoutManager.h"
|
||||
#include "nsGlobalWindow.h"
|
||||
#include "mozilla/ThrottledEventQueue.h"
|
||||
#include "mozilla/TimeStamp.h"
|
||||
#include "nsITimeoutHandler.h"
|
||||
#include "mozilla/dom/TabGroup.h"
|
||||
|
||||
using namespace mozilla::dom;
|
||||
|
||||
static int32_t gRunningTimeoutDepth = 0;
|
||||
|
||||
// The default shortest interval/timeout we permit
|
||||
#define DEFAULT_MIN_TIMEOUT_VALUE 4 // 4ms
|
||||
#define DEFAULT_MIN_BACKGROUND_TIMEOUT_VALUE 1000 // 1000ms
|
||||
static int32_t gMinTimeoutValue;
|
||||
static int32_t gMinBackgroundTimeoutValue;
|
||||
int32_t
|
||||
TimeoutManager::DOMMinTimeoutValue() const {
|
||||
// First apply any back pressure delay that might be in effect.
|
||||
int32_t value = std::max(mBackPressureDelayMS, 0);
|
||||
// Don't use the background timeout value when there are audio contexts
|
||||
// present, so that background audio can keep running smoothly. (bug 1181073)
|
||||
bool isBackground = !mWindow.AsInner()->HasAudioContexts() &&
|
||||
mWindow.IsBackgroundInternal();
|
||||
return
|
||||
std::max(isBackground ? gMinBackgroundTimeoutValue : gMinTimeoutValue, value);
|
||||
}
|
||||
|
||||
// The number of nested timeouts before we start clamping. HTML5 says 1, WebKit
|
||||
// uses 5.
|
||||
#define DOM_CLAMP_TIMEOUT_NESTING_LEVEL 5
|
||||
|
||||
// The longest interval (as PRIntervalTime) we permit, or that our
|
||||
// timer code can handle, really. See DELAY_INTERVAL_LIMIT in
|
||||
// nsTimerImpl.h for details.
|
||||
#define DOM_MAX_TIMEOUT_VALUE DELAY_INTERVAL_LIMIT
|
||||
|
||||
uint32_t TimeoutManager::sNestingLevel = 0;
|
||||
|
||||
namespace {
|
||||
|
||||
// The number of queued runnables within the TabGroup ThrottledEventQueue
|
||||
// at which to begin applying back pressure to the window.
|
||||
const uint32_t kThrottledEventQueueBackPressure = 5000;
|
||||
|
||||
// The amount of delay to apply to timers when back pressure is triggered.
|
||||
// As the length of the ThrottledEventQueue grows delay is increased. The
|
||||
// delay is scaled such that every kThrottledEventQueueBackPressure runnables
|
||||
// in the queue equates to an additional kBackPressureDelayMS.
|
||||
const double kBackPressureDelayMS = 500;
|
||||
|
||||
// Convert a ThrottledEventQueue length to a timer delay in milliseconds.
|
||||
// This will return a value between kBackPressureDelayMS and INT32_MAX.
|
||||
int32_t
|
||||
CalculateNewBackPressureDelayMS(uint32_t aBacklogDepth)
|
||||
{
|
||||
// The calculations here assume we are only operating while in back
|
||||
// pressure conditions.
|
||||
MOZ_ASSERT(aBacklogDepth >= kThrottledEventQueueBackPressure);
|
||||
double multiplier = static_cast<double>(aBacklogDepth) /
|
||||
static_cast<double>(kThrottledEventQueueBackPressure);
|
||||
double value = kBackPressureDelayMS * multiplier;
|
||||
if (value > INT32_MAX) {
|
||||
value = INT32_MAX;
|
||||
}
|
||||
return static_cast<int32_t>(value);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
TimeoutManager::TimeoutManager(nsGlobalWindow& aWindow)
|
||||
: mWindow(aWindow),
|
||||
mTimeoutInsertionPoint(nullptr),
|
||||
mTimeoutIdCounter(1),
|
||||
mTimeoutFiringDepth(0),
|
||||
mRunningTimeout(nullptr),
|
||||
mIdleCallbackTimeoutCounter(1),
|
||||
mBackPressureDelayMS(0)
|
||||
{
|
||||
MOZ_DIAGNOSTIC_ASSERT(aWindow.IsInnerWindow());
|
||||
}
|
||||
|
||||
/* static */
|
||||
void
|
||||
TimeoutManager::Initialize()
|
||||
{
|
||||
Preferences::AddIntVarCache(&gMinTimeoutValue,
|
||||
"dom.min_timeout_value",
|
||||
DEFAULT_MIN_TIMEOUT_VALUE);
|
||||
Preferences::AddIntVarCache(&gMinBackgroundTimeoutValue,
|
||||
"dom.min_background_timeout_value",
|
||||
DEFAULT_MIN_BACKGROUND_TIMEOUT_VALUE);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
TimeoutManager::GetTimeoutId(Timeout::Reason aReason)
|
||||
{
|
||||
switch (aReason) {
|
||||
case Timeout::Reason::eIdleCallbackTimeout:
|
||||
return ++mIdleCallbackTimeoutCounter;
|
||||
case Timeout::Reason::eTimeoutOrInterval:
|
||||
default:
|
||||
return ++mTimeoutIdCounter;
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimeoutManager::SetTimeout(nsITimeoutHandler* aHandler,
|
||||
int32_t interval, bool aIsInterval,
|
||||
Timeout::Reason aReason, int32_t* aReturn)
|
||||
{
|
||||
// If we don't have a document (we could have been unloaded since
|
||||
// the call to setTimeout was made), do nothing.
|
||||
if (!mWindow.GetExtantDoc()) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Disallow negative intervals. If aIsInterval also disallow 0,
|
||||
// because we use that as a "don't repeat" flag.
|
||||
interval = std::max(aIsInterval ? 1 : 0, interval);
|
||||
|
||||
// Make sure we don't proceed with an interval larger than our timer
|
||||
// code can handle. (Note: we already forced |interval| to be non-negative,
|
||||
// so the uint32_t cast (to avoid compiler warnings) is ok.)
|
||||
uint32_t maxTimeoutMs = PR_IntervalToMilliseconds(DOM_MAX_TIMEOUT_VALUE);
|
||||
if (static_cast<uint32_t>(interval) > maxTimeoutMs) {
|
||||
interval = maxTimeoutMs;
|
||||
}
|
||||
|
||||
RefPtr<Timeout> timeout = new Timeout();
|
||||
timeout->mIsInterval = aIsInterval;
|
||||
timeout->mInterval = interval;
|
||||
timeout->mScriptHandler = aHandler;
|
||||
timeout->mReason = aReason;
|
||||
|
||||
// Now clamp the actual interval we will use for the timer based on
|
||||
uint32_t nestingLevel = sNestingLevel + 1;
|
||||
uint32_t realInterval = interval;
|
||||
if (aIsInterval || nestingLevel >= DOM_CLAMP_TIMEOUT_NESTING_LEVEL ||
|
||||
mBackPressureDelayMS > 0 || mWindow.IsBackgroundInternal()) {
|
||||
// Don't allow timeouts less than DOMMinTimeoutValue() from
|
||||
// now...
|
||||
realInterval = std::max(realInterval, uint32_t(DOMMinTimeoutValue()));
|
||||
}
|
||||
|
||||
TimeDuration delta = TimeDuration::FromMilliseconds(realInterval);
|
||||
|
||||
if (mWindow.IsFrozen()) {
|
||||
// If we are frozen simply set timeout->mTimeRemaining to be the
|
||||
// "time remaining" in the timeout (i.e., the interval itself). This
|
||||
// will be used to create a new mWhen time when the window is thawed.
|
||||
// The end effect is that time does not appear to pass for frozen windows.
|
||||
timeout->mTimeRemaining = delta;
|
||||
} else {
|
||||
// Since we are not frozen we must set a precise mWhen target wakeup
|
||||
// time. Even if we are suspended we want to use this target time so
|
||||
// that it appears time passes while suspended.
|
||||
timeout->mWhen = TimeStamp::Now() + delta;
|
||||
}
|
||||
|
||||
// If we're not suspended, then set the timer.
|
||||
if (!mWindow.IsSuspended()) {
|
||||
MOZ_ASSERT(!timeout->mWhen.IsNull());
|
||||
|
||||
nsresult rv;
|
||||
timeout->mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
RefPtr<Timeout> copy = timeout;
|
||||
|
||||
rv = timeout->InitTimer(mWindow.GetThrottledEventQueue(), realInterval);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// The timeout is now also held in the timer's closure.
|
||||
Unused << copy.forget();
|
||||
}
|
||||
|
||||
timeout->mWindow = &mWindow;
|
||||
|
||||
if (!aIsInterval) {
|
||||
timeout->mNestingLevel = nestingLevel;
|
||||
}
|
||||
|
||||
// No popups from timeouts by default
|
||||
timeout->mPopupState = openAbused;
|
||||
|
||||
if (gRunningTimeoutDepth == 0 &&
|
||||
mWindow.GetPopupControlState() < openAbused) {
|
||||
// This timeout is *not* set from another timeout and it's set
|
||||
// while popups are enabled. Propagate the state to the timeout if
|
||||
// its delay (interval) is equal to or less than what
|
||||
// "dom.disable_open_click_delay" is set to (in ms).
|
||||
|
||||
int32_t delay =
|
||||
Preferences::GetInt("dom.disable_open_click_delay");
|
||||
|
||||
// This is checking |interval|, not realInterval, on purpose,
|
||||
// because our lower bound for |realInterval| could be pretty high
|
||||
// in some cases.
|
||||
if (interval <= delay) {
|
||||
timeout->mPopupState = mWindow.GetPopupControlState();
|
||||
}
|
||||
}
|
||||
|
||||
InsertTimeoutIntoList(timeout);
|
||||
|
||||
timeout->mTimeoutId = GetTimeoutId(aReason);
|
||||
*aReturn = timeout->mTimeoutId;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::ClearTimeout(int32_t aTimerId, Timeout::Reason aReason)
|
||||
{
|
||||
uint32_t timerId = (uint32_t)aTimerId;
|
||||
Timeout* timeout;
|
||||
|
||||
for (timeout = mTimeouts.getFirst(); timeout; timeout = timeout->getNext()) {
|
||||
if (timeout->mTimeoutId == timerId && timeout->mReason == aReason) {
|
||||
if (timeout->mRunning) {
|
||||
/* We're running from inside the timeout. Mark this
|
||||
timeout for deferred deletion by the code in
|
||||
RunTimeout() */
|
||||
timeout->mIsInterval = false;
|
||||
}
|
||||
else {
|
||||
/* Delete the timeout from the pending timeout list */
|
||||
timeout->remove();
|
||||
|
||||
if (timeout->mTimer) {
|
||||
timeout->mTimer->Cancel();
|
||||
timeout->mTimer = nullptr;
|
||||
timeout->Release();
|
||||
}
|
||||
timeout->Release();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::RunTimeout(Timeout* aTimeout)
|
||||
{
|
||||
if (mWindow.IsSuspended()) {
|
||||
return;
|
||||
}
|
||||
|
||||
NS_ASSERTION(!mWindow.IsFrozen(), "Timeout running on a window in the bfcache!");
|
||||
|
||||
Timeout* nextTimeout;
|
||||
Timeout* last_expired_timeout;
|
||||
Timeout* last_insertion_point;
|
||||
uint32_t firingDepth = mTimeoutFiringDepth + 1;
|
||||
|
||||
// Make sure that the window and the script context don't go away as
|
||||
// a result of running timeouts
|
||||
nsCOMPtr<nsIScriptGlobalObject> windowKungFuDeathGrip(&mWindow);
|
||||
// Silence the static analysis error about windowKungFuDeathGrip. Accessing
|
||||
// members of mWindow here is safe, because the lifetime of TimeoutManager is
|
||||
// the same as the lifetime of the containing nsGlobalWindow.
|
||||
Unused << windowKungFuDeathGrip;
|
||||
|
||||
// A native timer has gone off. See which of our timeouts need
|
||||
// servicing
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
TimeStamp deadline;
|
||||
|
||||
if (aTimeout && aTimeout->mWhen > now) {
|
||||
// The OS timer fired early (which can happen due to the timers
|
||||
// having lower precision than TimeStamp does). Set |deadline| to
|
||||
// be the time when the OS timer *should* have fired so that any
|
||||
// timers that *should* have fired before aTimeout *will* be fired
|
||||
// now.
|
||||
|
||||
deadline = aTimeout->mWhen;
|
||||
} else {
|
||||
deadline = now;
|
||||
}
|
||||
|
||||
// The timeout list is kept in deadline order. Discover the latest timeout
|
||||
// whose deadline has expired. On some platforms, native timeout events fire
|
||||
// "early", but we handled that above by setting deadline to aTimeout->mWhen
|
||||
// if the timer fired early. So we can stop walking if we get to timeouts
|
||||
// whose mWhen is greater than deadline, since once that happens we know
|
||||
// nothing past that point is expired.
|
||||
last_expired_timeout = nullptr;
|
||||
for (Timeout* timeout = mTimeouts.getFirst();
|
||||
timeout && timeout->mWhen <= deadline;
|
||||
timeout = timeout->getNext()) {
|
||||
if (timeout->mFiringDepth == 0) {
|
||||
// Mark any timeouts that are on the list to be fired with the
|
||||
// firing depth so that we can reentrantly run timeouts
|
||||
timeout->mFiringDepth = firingDepth;
|
||||
last_expired_timeout = timeout;
|
||||
|
||||
// Run available timers until we see our target timer. After
|
||||
// that, however, stop coalescing timers so we can yield the
|
||||
// main thread. Further timers that are ready will get picked
|
||||
// up by their own nsITimer runnables when they execute.
|
||||
//
|
||||
// For chrome windows, however, we do coalesce all timers and
|
||||
// do not yield the main thread. This is partly because we
|
||||
// trust chrome windows not to misbehave and partly because a
|
||||
// number of browser chrome tests have races that depend on this
|
||||
// coalescing.
|
||||
if (timeout == aTimeout && !mWindow.IsChromeWindow()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Maybe the timeout that the event was fired for has been deleted
|
||||
// and there are no others timeouts with deadlines that make them
|
||||
// eligible for execution yet. Go away.
|
||||
if (!last_expired_timeout) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert a dummy timeout into the list of timeouts between the
|
||||
// portion of the list that we are about to process now and those
|
||||
// timeouts that will be processed in a future call to
|
||||
// win_run_timeout(). This dummy timeout serves as the head of the
|
||||
// list for any timeouts inserted as a result of running a timeout.
|
||||
RefPtr<Timeout> dummy_timeout = new Timeout();
|
||||
dummy_timeout->mFiringDepth = firingDepth;
|
||||
dummy_timeout->mWhen = now;
|
||||
last_expired_timeout->setNext(dummy_timeout);
|
||||
RefPtr<Timeout> timeoutExtraRef(dummy_timeout);
|
||||
|
||||
last_insertion_point = mTimeoutInsertionPoint;
|
||||
// If we ever start setting mTimeoutInsertionPoint to a non-dummy timeout,
|
||||
// the logic in ResetTimersForThrottleReduction will need to change.
|
||||
mTimeoutInsertionPoint = dummy_timeout;
|
||||
|
||||
for (Timeout* timeout = mTimeouts.getFirst();
|
||||
timeout != dummy_timeout && !mWindow.IsFrozen();
|
||||
timeout = nextTimeout) {
|
||||
nextTimeout = timeout->getNext();
|
||||
|
||||
if (timeout->mFiringDepth != firingDepth) {
|
||||
// We skip the timeout since it's on the list to run at another
|
||||
// depth.
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mWindow.IsSuspended()) {
|
||||
// Some timer did suspend us. Make sure the
|
||||
// rest of the timers get executed later.
|
||||
timeout->mFiringDepth = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// The timeout is on the list to run at this depth, go ahead and
|
||||
// process it.
|
||||
|
||||
// Get the script context (a strong ref to prevent it going away)
|
||||
// for this timeout and ensure the script language is enabled.
|
||||
nsCOMPtr<nsIScriptContext> scx = mWindow.GetContextInternal();
|
||||
|
||||
if (!scx) {
|
||||
// No context means this window was closed or never properly
|
||||
// initialized for this language.
|
||||
continue;
|
||||
}
|
||||
|
||||
// This timeout is good to run
|
||||
bool timeout_was_cleared = mWindow.RunTimeoutHandler(timeout, scx);
|
||||
|
||||
if (timeout_was_cleared) {
|
||||
// The running timeout's window was cleared, this means that
|
||||
// ClearAllTimeouts() was called from a *nested* call, possibly
|
||||
// through a timeout that fired while a modal (to this window)
|
||||
// dialog was open or through other non-obvious paths.
|
||||
MOZ_ASSERT(dummy_timeout->HasRefCntOne(), "dummy_timeout may leak");
|
||||
Unused << timeoutExtraRef.forget().take();
|
||||
|
||||
mTimeoutInsertionPoint = last_insertion_point;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// If we have a regular interval timer, we re-schedule the
|
||||
// timeout, accounting for clock drift.
|
||||
bool needsReinsertion = RescheduleTimeout(timeout, now, !aTimeout);
|
||||
|
||||
// Running a timeout can cause another timeout to be deleted, so
|
||||
// we need to reset the pointer to the following timeout.
|
||||
nextTimeout = timeout->getNext();
|
||||
|
||||
timeout->remove();
|
||||
|
||||
if (needsReinsertion) {
|
||||
// Insert interval timeout onto list sorted in deadline order.
|
||||
// AddRefs timeout.
|
||||
InsertTimeoutIntoList(timeout);
|
||||
}
|
||||
|
||||
// Release the timeout struct since it's possibly out of the list
|
||||
timeout->Release();
|
||||
}
|
||||
|
||||
// Take the dummy timeout off the head of the list
|
||||
dummy_timeout->remove();
|
||||
timeoutExtraRef = nullptr;
|
||||
MOZ_ASSERT(dummy_timeout->HasRefCntOne(), "dummy_timeout may leak");
|
||||
|
||||
mTimeoutInsertionPoint = last_insertion_point;
|
||||
|
||||
MaybeApplyBackPressure();
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::MaybeApplyBackPressure()
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
// If we are already in back pressure then we don't need to apply back
|
||||
// pressure again. We also shouldn't need to apply back pressure while
|
||||
// the window is suspended.
|
||||
if (mBackPressureDelayMS > 0 || mWindow.IsSuspended()) {
|
||||
return;
|
||||
}
|
||||
|
||||
RefPtr<ThrottledEventQueue> queue = mWindow.TabGroup()->GetThrottledEventQueue();
|
||||
if (!queue) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Only begin back pressure if the window has greatly fallen behind the main
|
||||
// thread. This is a somewhat arbitrary threshold chosen such that it should
|
||||
// rarely fire under normaly circumstances. Its low enough, though,
|
||||
// that we should have time to slow new runnables from being added before an
|
||||
// OOM occurs.
|
||||
if (queue->Length() < kThrottledEventQueueBackPressure) {
|
||||
return;
|
||||
}
|
||||
|
||||
// First attempt to dispatch a runnable to update our back pressure state. We
|
||||
// do this first in order to verify we can dispatch successfully before
|
||||
// entering the back pressure state.
|
||||
nsCOMPtr<nsIRunnable> r =
|
||||
NewNonOwningRunnableMethod<StorensRefPtrPassByPtr<nsGlobalWindow>>(this,
|
||||
&TimeoutManager::CancelOrUpdateBackPressure, &mWindow);
|
||||
nsresult rv = queue->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
||||
NS_ENSURE_SUCCESS_VOID(rv);
|
||||
|
||||
// Since the callback was scheduled successfully we can now persist the
|
||||
// backpressure value.
|
||||
mBackPressureDelayMS = CalculateNewBackPressureDelayMS(queue->Length());
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::CancelOrUpdateBackPressure(nsGlobalWindow* aWindow)
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
MOZ_ASSERT(aWindow == &mWindow);
|
||||
MOZ_ASSERT(mBackPressureDelayMS > 0);
|
||||
|
||||
// First, check to see if we are still in back pressure. If we've dropped
|
||||
// below the threshold we can simply drop our back pressure delay. We
|
||||
// must also reset timers to remove the old back pressure delay in order to
|
||||
// avoid out-of-order timer execution.
|
||||
RefPtr<ThrottledEventQueue> queue = mWindow.TabGroup()->GetThrottledEventQueue();
|
||||
if (!queue || queue->Length() < kThrottledEventQueueBackPressure) {
|
||||
int32_t oldBackPressureDelayMS = mBackPressureDelayMS;
|
||||
mBackPressureDelayMS = 0;
|
||||
ResetTimersForThrottleReduction(oldBackPressureDelayMS);
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise we are still in back pressure mode.
|
||||
|
||||
// Re-calculate the back pressure delay.
|
||||
int32_t oldBackPressureDelayMS = mBackPressureDelayMS;
|
||||
mBackPressureDelayMS = CalculateNewBackPressureDelayMS(queue->Length());
|
||||
|
||||
// If the back pressure delay has gone down we must reset any existing
|
||||
// timers to use the new value. Otherwise we run the risk of executing
|
||||
// timer callbacks out-of-order.
|
||||
if (mBackPressureDelayMS < oldBackPressureDelayMS) {
|
||||
ResetTimersForThrottleReduction(oldBackPressureDelayMS);
|
||||
}
|
||||
|
||||
// Dispatch another runnable to update the back pressure state again.
|
||||
nsCOMPtr<nsIRunnable> r =
|
||||
NewNonOwningRunnableMethod<StorensRefPtrPassByPtr<nsGlobalWindow>>(this,
|
||||
&TimeoutManager::CancelOrUpdateBackPressure, &mWindow);
|
||||
MOZ_ALWAYS_SUCCEEDS(queue->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
|
||||
}
|
||||
|
||||
bool
|
||||
TimeoutManager::RescheduleTimeout(Timeout* aTimeout, const TimeStamp& now,
|
||||
bool aRunningPendingTimeouts)
|
||||
{
|
||||
if (!aTimeout->mIsInterval) {
|
||||
if (aTimeout->mTimer) {
|
||||
// The timeout still has an OS timer, and it's not an interval,
|
||||
// that means that the OS timer could still fire; cancel the OS
|
||||
// timer and release its reference to the timeout.
|
||||
aTimeout->mTimer->Cancel();
|
||||
aTimeout->mTimer = nullptr;
|
||||
aTimeout->Release();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Compute time to next timeout for interval timer.
|
||||
// Make sure nextInterval is at least DOMMinTimeoutValue().
|
||||
TimeDuration nextInterval =
|
||||
TimeDuration::FromMilliseconds(std::max(aTimeout->mInterval,
|
||||
uint32_t(DOMMinTimeoutValue())));
|
||||
|
||||
// If we're running pending timeouts, set the next interval to be
|
||||
// relative to "now", and not to when the timeout that was pending
|
||||
// should have fired.
|
||||
TimeStamp firingTime;
|
||||
if (aRunningPendingTimeouts) {
|
||||
firingTime = now + nextInterval;
|
||||
} else {
|
||||
firingTime = aTimeout->mWhen + nextInterval;
|
||||
}
|
||||
|
||||
TimeStamp currentNow = TimeStamp::Now();
|
||||
TimeDuration delay = firingTime - currentNow;
|
||||
|
||||
// And make sure delay is nonnegative; that might happen if the timer
|
||||
// thread is firing our timers somewhat early or if they're taking a long
|
||||
// time to run the callback.
|
||||
if (delay < TimeDuration(0)) {
|
||||
delay = TimeDuration(0);
|
||||
}
|
||||
|
||||
if (!aTimeout->mTimer) {
|
||||
NS_ASSERTION(mWindow.IsFrozen() || mWindow.IsSuspended(),
|
||||
"How'd our timer end up null if we're not frozen or "
|
||||
"suspended?");
|
||||
|
||||
aTimeout->mTimeRemaining = delay;
|
||||
return true;
|
||||
}
|
||||
|
||||
aTimeout->mWhen = currentNow + delay;
|
||||
|
||||
// Reschedule the OS timer. Don't bother returning any error codes if
|
||||
// this fails since the callers of this method don't care about them.
|
||||
nsresult rv = aTimeout->InitTimer(mWindow.GetThrottledEventQueue(),
|
||||
delay.ToMilliseconds());
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
NS_ERROR("Error initializing timer for DOM timeout!");
|
||||
|
||||
// We failed to initialize the new OS timer, this timer does
|
||||
// us no good here so we just cancel it (just in case) and
|
||||
// null out the pointer to the OS timer, this will release the
|
||||
// OS timer. As we continue executing the code below we'll end
|
||||
// up deleting the timeout since it's not an interval timeout
|
||||
// any more (since timeout->mTimer == nullptr).
|
||||
aTimeout->mTimer->Cancel();
|
||||
aTimeout->mTimer = nullptr;
|
||||
|
||||
// Now that the OS timer no longer has a reference to the
|
||||
// timeout we need to drop that reference.
|
||||
aTimeout->Release();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimeoutManager::ResetTimersForThrottleReduction()
|
||||
{
|
||||
return ResetTimersForThrottleReduction(gMinBackgroundTimeoutValue);
|
||||
}
|
||||
|
||||
nsresult
|
||||
TimeoutManager::ResetTimersForThrottleReduction(int32_t aPreviousThrottleDelayMS)
|
||||
{
|
||||
MOZ_ASSERT(aPreviousThrottleDelayMS > 0);
|
||||
|
||||
if (mWindow.IsFrozen() || mWindow.IsSuspended()) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
|
||||
// If mTimeoutInsertionPoint is non-null, we're in the middle of firing
|
||||
// timers and the timers we're planning to fire all come before
|
||||
// mTimeoutInsertionPoint; mTimeoutInsertionPoint itself is a dummy timeout
|
||||
// with an mWhen that may be semi-bogus. In that case, we don't need to do
|
||||
// anything with mTimeoutInsertionPoint or anything before it, so should
|
||||
// start at the timer after mTimeoutInsertionPoint, if there is one.
|
||||
// Otherwise, start at the beginning of the list.
|
||||
for (Timeout* timeout = mTimeoutInsertionPoint ?
|
||||
mTimeoutInsertionPoint->getNext() : mTimeouts.getFirst();
|
||||
timeout; ) {
|
||||
// It's important that this check be <= so that we guarantee that
|
||||
// taking std::max with |now| won't make a quantity equal to
|
||||
// timeout->mWhen below.
|
||||
if (timeout->mWhen <= now) {
|
||||
timeout = timeout->getNext();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (timeout->mWhen - now >
|
||||
TimeDuration::FromMilliseconds(aPreviousThrottleDelayMS)) {
|
||||
// No need to loop further. Timeouts are sorted in mWhen order
|
||||
// and the ones after this point were all set up for at least
|
||||
// gMinBackgroundTimeoutValue ms and hence were not clamped.
|
||||
break;
|
||||
}
|
||||
|
||||
// We reduced our throttled delay. Re-init the timer appropriately.
|
||||
// Compute the interval the timer should have had if it had not been set in a
|
||||
// background window
|
||||
TimeDuration interval =
|
||||
TimeDuration::FromMilliseconds(std::max(timeout->mInterval,
|
||||
uint32_t(DOMMinTimeoutValue())));
|
||||
uint32_t oldIntervalMillisecs = 0;
|
||||
timeout->mTimer->GetDelay(&oldIntervalMillisecs);
|
||||
TimeDuration oldInterval = TimeDuration::FromMilliseconds(oldIntervalMillisecs);
|
||||
if (oldInterval > interval) {
|
||||
// unclamp
|
||||
TimeStamp firingTime =
|
||||
std::max(timeout->mWhen - oldInterval + interval, now);
|
||||
|
||||
NS_ASSERTION(firingTime < timeout->mWhen,
|
||||
"Our firing time should strictly decrease!");
|
||||
|
||||
TimeDuration delay = firingTime - now;
|
||||
timeout->mWhen = firingTime;
|
||||
|
||||
// Since we reset mWhen we need to move |timeout| to the right
|
||||
// place in the list so that it remains sorted by mWhen.
|
||||
|
||||
// Get the pointer to the next timeout now, before we move the
|
||||
// current timeout in the list.
|
||||
Timeout* nextTimeout = timeout->getNext();
|
||||
|
||||
// It is safe to remove and re-insert because mWhen is now
|
||||
// strictly smaller than it used to be, so we know we'll insert
|
||||
// |timeout| before nextTimeout.
|
||||
NS_ASSERTION(!nextTimeout ||
|
||||
timeout->mWhen < nextTimeout->mWhen, "How did that happen?");
|
||||
timeout->remove();
|
||||
// InsertTimeoutIntoList will addref |timeout| and reset
|
||||
// mFiringDepth. Make sure to undo that after calling it.
|
||||
uint32_t firingDepth = timeout->mFiringDepth;
|
||||
InsertTimeoutIntoList(timeout);
|
||||
timeout->mFiringDepth = firingDepth;
|
||||
timeout->Release();
|
||||
|
||||
nsresult rv = timeout->InitTimer(mWindow.GetThrottledEventQueue(),
|
||||
delay.ToMilliseconds());
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
NS_WARNING("Error resetting non background timer for DOM timeout!");
|
||||
return rv;
|
||||
}
|
||||
|
||||
timeout = nextTimeout;
|
||||
} else {
|
||||
timeout = timeout->getNext();
|
||||
}
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::ClearAllTimeouts()
|
||||
{
|
||||
Timeout* timeout;
|
||||
Timeout* nextTimeout;
|
||||
|
||||
for (timeout = mTimeouts.getFirst(); timeout; timeout = nextTimeout) {
|
||||
/* If RunTimeout() is higher up on the stack for this
|
||||
window, e.g. as a result of document.write from a timeout,
|
||||
then we need to reset the list insertion point for
|
||||
newly-created timeouts in case the user adds a timeout,
|
||||
before we pop the stack back to RunTimeout. */
|
||||
if (mRunningTimeout == timeout)
|
||||
mTimeoutInsertionPoint = nullptr;
|
||||
|
||||
nextTimeout = timeout->getNext();
|
||||
|
||||
if (timeout->mTimer) {
|
||||
timeout->mTimer->Cancel();
|
||||
timeout->mTimer = nullptr;
|
||||
|
||||
// Drop the count since the timer isn't going to hold on
|
||||
// anymore.
|
||||
timeout->Release();
|
||||
}
|
||||
|
||||
// Set timeout->mCleared to true to indicate that the timeout was
|
||||
// cleared and taken out of the list of timeouts
|
||||
timeout->mCleared = true;
|
||||
|
||||
// Drop the count since we're removing it from the list.
|
||||
timeout->Release();
|
||||
}
|
||||
|
||||
// Clear out our list
|
||||
mTimeouts.clear();
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::InsertTimeoutIntoList(Timeout* aTimeout)
|
||||
{
|
||||
// Start at mLastTimeout and go backwards. Don't go further than
|
||||
// mTimeoutInsertionPoint, though. This optimizes for the common case of
|
||||
// insertion at the end.
|
||||
Timeout* prevSibling;
|
||||
for (prevSibling = mTimeouts.getLast();
|
||||
prevSibling && prevSibling != mTimeoutInsertionPoint &&
|
||||
// This condition needs to match the one in SetTimeoutOrInterval that
|
||||
// determines whether to set mWhen or mTimeRemaining.
|
||||
(mWindow.IsFrozen() ?
|
||||
prevSibling->mTimeRemaining > aTimeout->mTimeRemaining :
|
||||
prevSibling->mWhen > aTimeout->mWhen);
|
||||
prevSibling = prevSibling->getPrevious()) {
|
||||
/* Do nothing; just searching */
|
||||
}
|
||||
|
||||
// Now link in aTimeout after prevSibling.
|
||||
if (prevSibling) {
|
||||
prevSibling->setNext(aTimeout);
|
||||
} else {
|
||||
mTimeouts.insertFront(aTimeout);
|
||||
}
|
||||
|
||||
aTimeout->mFiringDepth = 0;
|
||||
|
||||
// Increment the timeout's reference count since it's now held on to
|
||||
// by the list
|
||||
aTimeout->AddRef();
|
||||
}
|
||||
|
||||
Timeout*
|
||||
TimeoutManager::BeginRunningTimeout(Timeout* aTimeout)
|
||||
{
|
||||
Timeout* currentTimeout = mRunningTimeout;
|
||||
mRunningTimeout = aTimeout;
|
||||
|
||||
++gRunningTimeoutDepth;
|
||||
++mTimeoutFiringDepth;
|
||||
|
||||
return currentTimeout;
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::EndRunningTimeout(Timeout* aTimeout)
|
||||
{
|
||||
--mTimeoutFiringDepth;
|
||||
--gRunningTimeoutDepth;
|
||||
|
||||
mRunningTimeout = aTimeout;
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::UnmarkGrayTimers()
|
||||
{
|
||||
for (Timeout* timeout = mTimeouts.getFirst();
|
||||
timeout;
|
||||
timeout = timeout->getNext()) {
|
||||
if (timeout->mScriptHandler) {
|
||||
timeout->mScriptHandler->MarkForCC();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::Suspend()
|
||||
{
|
||||
for (Timeout* t = mTimeouts.getFirst(); t; t = t->getNext()) {
|
||||
// Leave the timers with the current time remaining. This will
|
||||
// cause the timers to potentially fire when the window is
|
||||
// Resume()'d. Time effectively passes while suspended.
|
||||
|
||||
// Drop the XPCOM timer; we'll reschedule when restoring the state.
|
||||
if (t->mTimer) {
|
||||
t->mTimer->Cancel();
|
||||
t->mTimer = nullptr;
|
||||
|
||||
// Drop the reference that the timer's closure had on this timeout, we'll
|
||||
// add it back in Resume().
|
||||
t->Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::Resume()
|
||||
{
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
DebugOnly<bool> _seenDummyTimeout = false;
|
||||
|
||||
for (Timeout* t = mTimeouts.getFirst(); t; t = t->getNext()) {
|
||||
// There's a chance we're being called with RunTimeout on the stack in which
|
||||
// case we have a dummy timeout in the list that *must not* be resumed. It
|
||||
// can be identified by a null mWindow.
|
||||
if (!t->mWindow) {
|
||||
NS_ASSERTION(!_seenDummyTimeout, "More than one dummy timeout?!");
|
||||
_seenDummyTimeout = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(!t->mTimer);
|
||||
|
||||
// The timeout mWhen is set to the absolute time when the timer should
|
||||
// fire. Recalculate the delay from now until that deadline. If the
|
||||
// the deadline has already passed or falls within our minimum delay
|
||||
// deadline, then clamp the resulting value to the minimum delay. The
|
||||
// mWhen will remain at its absolute time, but we won't fire the OS
|
||||
// timer until our calculated delay has passed.
|
||||
int32_t remaining = 0;
|
||||
if (t->mWhen > now) {
|
||||
remaining = static_cast<int32_t>((t->mWhen - now).ToMilliseconds());
|
||||
}
|
||||
uint32_t delay = std::max(remaining, DOMMinTimeoutValue());
|
||||
|
||||
t->mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
||||
if (!t->mTimer) {
|
||||
t->remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
nsresult rv = t->InitTimer(mWindow.GetThrottledEventQueue(), delay);
|
||||
if (NS_FAILED(rv)) {
|
||||
t->mTimer = nullptr;
|
||||
t->remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
// Add a reference for the new timer's closure.
|
||||
t->AddRef();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::Freeze()
|
||||
{
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
for (Timeout *t = mTimeouts.getFirst(); t; t = t->getNext()) {
|
||||
// Save the current remaining time for this timeout. We will
|
||||
// re-apply it when the window is Thaw()'d. This effectively
|
||||
// shifts timers to the right as if time does not pass while
|
||||
// the window is frozen.
|
||||
if (t->mWhen > now) {
|
||||
t->mTimeRemaining = t->mWhen - now;
|
||||
} else {
|
||||
t->mTimeRemaining = TimeDuration(0);
|
||||
}
|
||||
|
||||
// Since we are suspended there should be no OS timer set for
|
||||
// this timeout entry.
|
||||
MOZ_ASSERT(!t->mTimer);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
TimeoutManager::Thaw()
|
||||
{
|
||||
TimeStamp now = TimeStamp::Now();
|
||||
DebugOnly<bool> _seenDummyTimeout = false;
|
||||
|
||||
for (Timeout *t = mTimeouts.getFirst(); t; t = t->getNext()) {
|
||||
// There's a chance we're being called with RunTimeout on the stack in which
|
||||
// case we have a dummy timeout in the list that *must not* be resumed. It
|
||||
// can be identified by a null mWindow.
|
||||
if (!t->mWindow) {
|
||||
NS_ASSERTION(!_seenDummyTimeout, "More than one dummy timeout?!");
|
||||
_seenDummyTimeout = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set mWhen back to the time when the timer is supposed to fire.
|
||||
t->mWhen = now + t->mTimeRemaining;
|
||||
|
||||
MOZ_ASSERT(!t->mTimer);
|
||||
}
|
||||
}
|
128
dom/base/TimeoutManager.h
Normal file
128
dom/base/TimeoutManager.h
Normal file
@ -0,0 +1,128 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef mozilla_dom_TimeoutManager_h__
|
||||
#define mozilla_dom_TimeoutManager_h__
|
||||
|
||||
#include "mozilla/dom/Timeout.h"
|
||||
|
||||
class nsITimeoutHandler;
|
||||
class nsGlobalWindow;
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
// This class manages the timeouts in a Window's setTimeout/setInterval pool.
|
||||
class TimeoutManager final
|
||||
{
|
||||
public:
|
||||
explicit TimeoutManager(nsGlobalWindow& aWindow);
|
||||
TimeoutManager(const TimeoutManager& rhs) = delete;
|
||||
void operator=(const TimeoutManager& rhs) = delete;
|
||||
|
||||
bool IsRunningTimeout() const { return mTimeoutFiringDepth > 0; }
|
||||
|
||||
static uint32_t GetNestingLevel() { return sNestingLevel; }
|
||||
static void SetNestingLevel(uint32_t aLevel) { sNestingLevel = aLevel; }
|
||||
|
||||
bool HasTimeouts() const { return !mTimeouts.isEmpty(); }
|
||||
|
||||
nsresult SetTimeout(nsITimeoutHandler* aHandler,
|
||||
int32_t interval, bool aIsInterval,
|
||||
mozilla::dom::Timeout::Reason aReason,
|
||||
int32_t* aReturn);
|
||||
void ClearTimeout(int32_t aTimerId,
|
||||
mozilla::dom::Timeout::Reason aReason);
|
||||
|
||||
// The timeout implementation functions.
|
||||
void RunTimeout(mozilla::dom::Timeout* aTimeout);
|
||||
// Return true if |aTimeout| needs to be reinserted into the timeout list.
|
||||
bool RescheduleTimeout(mozilla::dom::Timeout* aTimeout, const TimeStamp& now,
|
||||
bool aRunningPendingTimeouts);
|
||||
|
||||
void ClearAllTimeouts();
|
||||
// Insert aTimeout into the list, before all timeouts that would
|
||||
// fire after it, but no earlier than mTimeoutInsertionPoint, if any.
|
||||
void InsertTimeoutIntoList(mozilla::dom::Timeout* aTimeout);
|
||||
uint32_t GetTimeoutId(mozilla::dom::Timeout::Reason aReason);
|
||||
|
||||
// Apply back pressure to the window if the TabGroup ThrottledEventQueue
|
||||
// exists and has too many runnables waiting to run. For example, increase
|
||||
// the minimum timer delay, etc.
|
||||
void MaybeApplyBackPressure();
|
||||
|
||||
// Check the current ThrottledEventQueue depth and update the back pressure
|
||||
// state. If the queue has drained back pressure may be canceled.
|
||||
void CancelOrUpdateBackPressure(nsGlobalWindow* aWindow);
|
||||
|
||||
// When timers are being throttled and we reduce the thottle delay we must
|
||||
// reschedule. The amount of the old throttle delay must be provided in
|
||||
// order to bound how many timers must be examined.
|
||||
nsresult ResetTimersForThrottleReduction();
|
||||
|
||||
int32_t DOMMinTimeoutValue() const;
|
||||
|
||||
// aTimeout is the timeout that we're about to start running. This function
|
||||
// returns the current timeout.
|
||||
mozilla::dom::Timeout* BeginRunningTimeout(mozilla::dom::Timeout* aTimeout);
|
||||
// aTimeout is the last running timeout.
|
||||
void EndRunningTimeout(mozilla::dom::Timeout* aTimeout);
|
||||
|
||||
void UnmarkGrayTimers();
|
||||
|
||||
// These four methods are intended to be called from the corresponding methods
|
||||
// on nsGlobalWindow.
|
||||
void Suspend();
|
||||
void Resume();
|
||||
void Freeze();
|
||||
void Thaw();
|
||||
|
||||
// Initialize TimeoutManager before the first time it is accessed.
|
||||
static void Initialize();
|
||||
|
||||
// Run some code for each Timeout in our list.
|
||||
template <class Callable>
|
||||
void ForEachTimeout(Callable c)
|
||||
{
|
||||
for (Timeout* timeout = mTimeouts.getFirst();
|
||||
timeout;
|
||||
timeout = timeout->getNext()) {
|
||||
c(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
nsresult ResetTimersForThrottleReduction(int32_t aPreviousThrottleDelayMS);
|
||||
|
||||
private:
|
||||
// Each nsGlobalWindow object has a TimeoutManager member. This reference
|
||||
// points to that holder object.
|
||||
nsGlobalWindow& mWindow;
|
||||
// mTimeouts is generally sorted by mWhen, unless mTimeoutInsertionPoint is
|
||||
// non-null. In that case, the dummy timeout pointed to by
|
||||
// mTimeoutInsertionPoint may have a later mWhen than some of the timeouts
|
||||
// that come after it.
|
||||
mozilla::LinkedList<mozilla::dom::Timeout> mTimeouts;
|
||||
// If mTimeoutInsertionPoint is non-null, insertions should happen after it.
|
||||
// This is a dummy timeout at the moment; if that ever changes, the logic in
|
||||
// ResetTimersForThrottleReduction needs to change.
|
||||
mozilla::dom::Timeout* mTimeoutInsertionPoint;
|
||||
uint32_t mTimeoutIdCounter;
|
||||
uint32_t mTimeoutFiringDepth;
|
||||
mozilla::dom::Timeout* mRunningTimeout;
|
||||
|
||||
// The current idle request callback timeout handle
|
||||
uint32_t mIdleCallbackTimeoutCounter;
|
||||
|
||||
int32_t mBackPressureDelayMS;
|
||||
|
||||
static uint32_t sNestingLevel;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -205,6 +205,7 @@ EXPORTS.mozilla.dom += [
|
||||
'TabGroup.h',
|
||||
'Text.h',
|
||||
'Timeout.h',
|
||||
'TimeoutManager.h',
|
||||
'TreeWalker.h',
|
||||
'WebKitCSSMatrix.h',
|
||||
'WebSocket.h',
|
||||
@ -344,6 +345,7 @@ UNIFIED_SOURCES += [
|
||||
'TextInputProcessor.cpp',
|
||||
'ThirdPartyUtil.cpp',
|
||||
'Timeout.cpp',
|
||||
'TimeoutManager.cpp',
|
||||
'TreeWalker.cpp',
|
||||
'WebKitCSSMatrix.cpp',
|
||||
'WebSocket.cpp',
|
||||
|
@ -32,6 +32,7 @@
|
||||
#include "mozilla/EventListenerManager.h"
|
||||
#include "mozilla/dom/Element.h"
|
||||
#include "mozilla/dom/ProcessGlobal.h"
|
||||
#include "mozilla/dom/TimeoutManager.h"
|
||||
#include "xpcpublic.h"
|
||||
#include "nsObserverService.h"
|
||||
#include "nsFocusManager.h"
|
||||
@ -210,7 +211,8 @@ MarkContentViewer(nsIContentViewer* aViewer, bool aCleanupJS,
|
||||
if (elm) {
|
||||
elm->MarkForCC();
|
||||
}
|
||||
static_cast<nsGlobalWindow*>(win.get())->UnmarkGrayTimers();
|
||||
static_cast<nsGlobalWindow*>(win.get())->AsInner()->
|
||||
TimeoutManager().UnmarkGrayTimers();
|
||||
}
|
||||
} else if (aPrepareForCC) {
|
||||
// Unfortunately we need to still mark user data just before running CC so
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -56,7 +56,7 @@
|
||||
#include "nsSize.h"
|
||||
#include "nsCheapSets.h"
|
||||
#include "mozilla/dom/ImageBitmapSource.h"
|
||||
#include "mozilla/dom/Timeout.h"
|
||||
#include "mozilla/UniquePtr.h"
|
||||
|
||||
#define DEFAULT_HOME_PAGE "www.mozilla.org"
|
||||
#define PREF_BROWSER_STARTUP_HOMEPAGE "browser.startup.homepage"
|
||||
@ -395,7 +395,7 @@ public:
|
||||
virtual void SyncStateFromParentWindow();
|
||||
|
||||
virtual nsresult FireDelayedDOMEvents() override;
|
||||
virtual bool IsRunningTimeout() override { return mTimeoutFiringDepth > 0; }
|
||||
virtual bool IsRunningTimeout() override;
|
||||
|
||||
// Outer windows only.
|
||||
virtual bool WouldReuseInnerWindow(nsIDocument* aNewDocument) override;
|
||||
@ -707,8 +707,6 @@ public:
|
||||
|
||||
void AddSizeOfIncludingThis(nsWindowSizes* aWindowSizes) const;
|
||||
|
||||
void UnmarkGrayTimers();
|
||||
|
||||
// Inner windows only.
|
||||
void AddEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
|
||||
void RemoveEventTargetObject(mozilla::DOMEventTargetHelper* aObject);
|
||||
@ -1257,6 +1255,7 @@ public:
|
||||
already_AddRefed<nsWindowRoot> GetWindowRoot(mozilla::ErrorResult& aError);
|
||||
|
||||
mozilla::dom::Performance* GetPerformance();
|
||||
|
||||
protected:
|
||||
// Web IDL helpers
|
||||
|
||||
@ -1465,12 +1464,7 @@ private:
|
||||
|
||||
public:
|
||||
// Timeout Functions
|
||||
// Language agnostic timeout function (all args passed).
|
||||
// |interval| is in milliseconds.
|
||||
nsresult SetTimeoutOrInterval(nsITimeoutHandler* aHandler,
|
||||
int32_t interval, bool aIsInterval,
|
||||
mozilla::dom::Timeout::Reason aReason,
|
||||
int32_t* aReturn);
|
||||
int32_t SetTimeoutOrInterval(JSContext* aCx,
|
||||
mozilla::dom::Function& aFunction,
|
||||
int32_t aTimeout,
|
||||
@ -1479,23 +1473,9 @@ public:
|
||||
int32_t SetTimeoutOrInterval(JSContext* aCx, const nsAString& aHandler,
|
||||
int32_t aTimeout, bool aIsInterval,
|
||||
mozilla::ErrorResult& aError);
|
||||
void ClearTimeoutOrInterval(int32_t aTimerId,
|
||||
mozilla::dom::Timeout::Reason aReason);
|
||||
|
||||
// The timeout implementation functions.
|
||||
void RunTimeout(mozilla::dom::Timeout* aTimeout);
|
||||
void RunTimeout() { RunTimeout(nullptr); }
|
||||
// Return true if |aTimeout| was cleared while its handler ran.
|
||||
bool RunTimeoutHandler(mozilla::dom::Timeout* aTimeout, nsIScriptContext* aScx);
|
||||
// Return true if |aTimeout| needs to be reinserted into the timeout list.
|
||||
bool RescheduleTimeout(mozilla::dom::Timeout* aTimeout, const TimeStamp& now,
|
||||
bool aRunningPendingTimeouts);
|
||||
|
||||
void ClearAllTimeouts();
|
||||
// Insert aTimeout into the list, before all timeouts that would
|
||||
// fire after it, but no earlier than mTimeoutInsertionPoint, if any.
|
||||
void InsertTimeoutIntoList(mozilla::dom::Timeout* aTimeout);
|
||||
uint32_t GetTimeoutId(mozilla::dom::Timeout::Reason aReason);
|
||||
|
||||
// Helper Functions
|
||||
already_AddRefed<nsIDocShellTreeOwner> GetTreeOwner();
|
||||
@ -1617,8 +1597,6 @@ protected:
|
||||
|
||||
virtual void UpdateParentTarget() override;
|
||||
|
||||
inline int32_t DOMMinTimeoutValue() const;
|
||||
|
||||
void InitializeShowFocusRings();
|
||||
|
||||
// Clear the document-dependent slots on our JS wrapper. Inner windows only.
|
||||
@ -1705,22 +1683,6 @@ private:
|
||||
friend class nsPIDOMWindow<mozIDOMWindow>;
|
||||
friend class nsPIDOMWindow<nsISupports>;
|
||||
|
||||
// Apply back pressure to the window if the TabGroup ThrottledEventQueue
|
||||
// exists and has too many runnables waiting to run. For example, increase
|
||||
// the minimum timer delay, etc.
|
||||
void
|
||||
MaybeApplyBackPressure();
|
||||
|
||||
// Check the current ThrottledEventQueue depth and update the back pressure
|
||||
// state. If the queue has drained back pressure may be canceled.
|
||||
void
|
||||
CancelOrUpdateBackPressure();
|
||||
|
||||
// When timers are being throttled and we reduce the thottle delay we must
|
||||
// reschedule. The amount of the old throttle delay must be provided in
|
||||
// order to bound how many timers must be examined.
|
||||
nsresult ResetTimersForThrottleReduction(int32_t aPreviousThrottleDelayMS);
|
||||
|
||||
mozilla::dom::TabGroup* TabGroupInner();
|
||||
mozilla::dom::TabGroup* TabGroupOuter();
|
||||
|
||||
@ -1853,17 +1815,6 @@ protected:
|
||||
|
||||
// These member variable are used only on inner windows.
|
||||
RefPtr<mozilla::EventListenerManager> mListenerManager;
|
||||
// mTimeouts is generally sorted by mWhen, unless mTimeoutInsertionPoint is
|
||||
// non-null. In that case, the dummy timeout pointed to by
|
||||
// mTimeoutInsertionPoint may have a later mWhen than some of the timeouts
|
||||
// that come after it.
|
||||
mozilla::LinkedList<mozilla::dom::Timeout> mTimeouts;
|
||||
// If mTimeoutInsertionPoint is non-null, insertions should happen after it.
|
||||
// This is a dummy timeout at the moment; if that ever changes, the logic in
|
||||
// ResetTimersForThrottleReduction needs to change.
|
||||
mozilla::dom::Timeout* mTimeoutInsertionPoint;
|
||||
uint32_t mTimeoutIdCounter;
|
||||
uint32_t mTimeoutFiringDepth;
|
||||
RefPtr<mozilla::dom::Location> mLocation;
|
||||
RefPtr<nsHistory> mHistory;
|
||||
RefPtr<mozilla::dom::CustomElementRegistry> mCustomElements;
|
||||
@ -1874,11 +1825,10 @@ protected:
|
||||
typedef nsTArray<RefPtr<mozilla::dom::StorageEvent>> nsDOMStorageEventArray;
|
||||
nsDOMStorageEventArray mPendingStorageEvents;
|
||||
|
||||
|
||||
uint32_t mSuspendDepth;
|
||||
uint32_t mFreezeDepth;
|
||||
|
||||
int32_t mBackPressureDelayMS;
|
||||
|
||||
// the method that was used to focus mFocusedNode
|
||||
uint32_t mFocusMethod;
|
||||
|
||||
@ -1893,8 +1843,6 @@ protected:
|
||||
static void InsertIdleCallbackIntoList(mozilla::dom::IdleRequest* aRequest,
|
||||
IdleRequests& aList);
|
||||
|
||||
// The current idle request callback timeout handle
|
||||
uint32_t mIdleCallbackTimeoutCounter;
|
||||
// The current idle request callback handle
|
||||
uint32_t mIdleRequestCallbackCounter;
|
||||
IdleRequests mIdleRequestCallbacks;
|
||||
@ -1974,6 +1922,7 @@ protected:
|
||||
friend class nsDOMWindowUtils;
|
||||
friend class mozilla::dom::PostMessageEvent;
|
||||
friend class DesktopNotification;
|
||||
friend class mozilla::dom::TimeoutManager;
|
||||
|
||||
static WindowByIdTable* sWindowsById;
|
||||
static bool sWarnedAboutWindowInternal;
|
||||
|
@ -49,6 +49,7 @@ class Element;
|
||||
class Performance;
|
||||
class ServiceWorkerRegistration;
|
||||
class Timeout;
|
||||
class TimeoutManager;
|
||||
class CustomElementRegistry;
|
||||
} // namespace dom
|
||||
} // namespace mozilla
|
||||
@ -619,6 +620,8 @@ protected:
|
||||
|
||||
// mPerformance is only used on inner windows.
|
||||
RefPtr<mozilla::dom::Performance> mPerformance;
|
||||
// mTimeoutManager is only useed on inner windows.
|
||||
mozilla::UniquePtr<mozilla::dom::TimeoutManager> mTimeoutManager;
|
||||
|
||||
typedef nsRefPtrHashtable<nsStringHashKey,
|
||||
mozilla::dom::ServiceWorkerRegistration>
|
||||
@ -628,8 +631,6 @@ protected:
|
||||
uint32_t mModalStateDepth;
|
||||
|
||||
// These variables are only used on inner windows.
|
||||
mozilla::dom::Timeout *mRunningTimeout;
|
||||
|
||||
uint32_t mMutationBits;
|
||||
|
||||
bool mIsDocumentLoaded;
|
||||
@ -850,6 +851,10 @@ public:
|
||||
// window.
|
||||
void SyncStateFromParentWindow();
|
||||
|
||||
bool HasAudioContexts() const;
|
||||
|
||||
mozilla::dom::TimeoutManager& TimeoutManager();
|
||||
|
||||
protected:
|
||||
void CreatePerformanceObjectIfNeeded();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user