Skip to content

Commit

Permalink
#463: wip 1
Browse files Browse the repository at this point in the history
  • Loading branch information
classilla committed Feb 11, 2018
1 parent 33abf8f commit 91b9713
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions dom/base/nsGlobalWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ class nsIScriptTimeoutHandler;
#include <unistd.h> // for getpid()
#endif

#include "mozilla/dom/IdleDeadline.h" // issue 463

static const char kStorageEnabled[] = "dom.storage.enabled";

using namespace mozilla;
Expand Down Expand Up @@ -11538,11 +11540,21 @@ nsGlobalWindow::SetInterval(JSContext* aCx, const nsAString& aHandler,
}

// TenFourFox issue 463

static bool
SystemIsIdle()
{
// XXX: check Mach factor
return false;
}

nsresult
nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler,
int32_t interval,
bool aIsInterval, int32_t *aReturn)
{
// XXX: see below about interval versus deadline. we abuse interval for
// when to recheck if idle.
return SetTimeoutOrIntervalOrIdleCallback(aHandler, interval, aIsInterval, aReturn, nullptr);
}

Expand Down Expand Up @@ -11574,6 +11586,9 @@ nsGlobalWindow::SetTimeoutOrIntervalOrIdleCallback(nsIScriptTimeoutHandler *aHan

RefPtr<nsTimeout> timeout = new nsTimeout();
timeout->mIsInterval = aIsInterval;

// XXX: add to Interval a deadline field, and use the interval for when to check
// if idle again. TenFourFox issue 463
timeout->mInterval = interval;
if (aCallback)
timeout->mCallback = aCallback;
Expand Down Expand Up @@ -11768,11 +11783,19 @@ nsGlobalWindow::RunTimeoutHandler(nsTimeout* aTimeout,
}

if (!timeout->mScriptHandler) {
// Time to assess the conditions for requestIdleCallback (issue 463).
// Call the idle callback. TenFourFox issue 463.
MOZ_ASSERT(timeout->mCallback);
MOZ_CRASH("RunTimeoutHandler triggered on requestIdleCallback");
return false;
}
nsCOMPtr<nsISupports> me(static_cast<nsIDOMWindow *>(this));

ErrorResult error;
RefPtr<IdleDeadline> deadline =
new IdleDeadline(timeout->mWindow,
/* didTimeout */ true,
/* timeRemaining */ 0.0f);
timeout->mCallback->Call(*deadline, error, "requestIdleCallback handler");
timeout->mCallback = nullptr;
error.SuppressException();
} else {

nsCOMPtr<nsIScriptTimeoutHandler> handler(timeout->mScriptHandler);
RefPtr<Function> callback = handler->GetCallback();
Expand Down Expand Up @@ -11805,6 +11828,8 @@ nsGlobalWindow::RunTimeoutHandler(nsTimeout* aTimeout,
ignored.SuppressException();
}

}

// We ignore any failures from calling EvaluateString() on the context or
// Call() on a Function here since we're in a loop
// where we're likely to be running timeouts whose OS timers
Expand Down Expand Up @@ -11917,6 +11942,8 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
{
// If a modal dialog is open for this window, return early. Pending
// timeouts will run when the modal dialog is dismissed.
// (Similarly, do this behaviour for idleCallbacks, since it's too
// much bookwork otherwise. See issue 463.)
if (IsInModalState() || mTimeoutsSuspendDepth) {
return;
}
Expand Down Expand Up @@ -11974,6 +12001,7 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
return;
}

#if(0)
// Record telemetry information about timers set recently.
TimeDuration recordingInterval = TimeDuration::FromMilliseconds(STATISTICS_INTERVAL);
if (gLastRecordedRecentTimeouts.IsNull() ||
Expand All @@ -11983,6 +12011,7 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
Telemetry::Accumulate(Telemetry::DOM_TIMERS_RECENTLY_SET, count);
gLastRecordedRecentTimeouts = now;
}
#endif

// Insert a dummy timeout into the list of timeouts between the
// portion of the list that we are about to process now and those
Expand All @@ -12000,7 +12029,7 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
// the logic in ResetTimersForNonBackgroundWindow will need to change.
mTimeoutInsertionPoint = dummy_timeout;

Telemetry::AutoCounter<Telemetry::DOM_TIMERS_FIRED_PER_NATIVE_TIMEOUT> timeoutsRan;
//Telemetry::AutoCounter<Telemetry::DOM_TIMERS_FIRED_PER_NATIVE_TIMEOUT> timeoutsRan;

for (nsTimeout *timeout = mTimeouts.getFirst();
timeout != dummy_timeout && !IsFrozen();
Expand All @@ -12024,6 +12053,13 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
// The timeout is on the list to run at this depth, go ahead and
// process it.

// If this timeout is an IdleCallback (TenFourFox issue 463), check
// to see if we have hit the deadline. If so, run the timeout. If not,
// check to see if we're idle. If so, run the timeout. If not, reschedule
// to check again.
//
// -- NYI XXX --

// 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 = GetContextInternal();
Expand All @@ -12035,7 +12071,7 @@ nsGlobalWindow::RunTimeout(nsTimeout *aTimeout)
}

// This timeout is good to run
++timeoutsRan;
//++timeoutsRan;
bool timeout_was_cleared = RunTimeoutHandler(timeout, scx);

if (timeout_was_cleared) {
Expand Down Expand Up @@ -13894,6 +13930,11 @@ nsGlobalWindow::RequestIdleCallback(JSContext* aCx,
// uint32_t handle = ++mIdleRequestCallbackCounter;

fprintf(stderr, "::RequestIdleCallback() is not yet implemented\n");

// Plan:
// Check if idle now. If so, set a timeout of zero so it runs right away.
// Else set the deadline, if provided, and set an interval to recheck idle.
// See SystemIsIdle()
#if DEBUG
MOZ_ASSERT(0);
#endif
Expand All @@ -13906,6 +13947,10 @@ nsGlobalWindow::CancelIdleCallback(uint32_t aHandle)
MOZ_RELEASE_ASSERT(IsInnerWindow());

fprintf(stderr, "::CancelIdleCallback() is not yet implemented\n");

// Plan:
// Check if this is a timeout with a Callback. If not, fail.
// If so, hand this to RemoveTimeout.
#if DEBUG
MOZ_ASSERT(0);
#endif
Expand Down

0 comments on commit 91b9713

Please sign in to comment.