Bug 803039 - Use TimeStamp instead of PRTime to store the idle time. r=jlebar

This commit is contained in:
Shelly Lin 2012-10-26 10:17:36 +08:00
Родитель d390a1808b
Коммит 0365b4fe28
2 изменённых файлов: 47 добавлений и 50 удалений

Просмотреть файл

@ -381,10 +381,10 @@ nsIdleService::GetInstance()
return instance.forget();
}
nsIdleService::nsIdleService() : mCurrentlySetToTimeoutAtInPR(0),
nsIdleService::nsIdleService() : mCurrentlySetToTimeoutAt(TimeStamp()),
mAnyObserverIdle(false),
mDeltaToNextIdleSwitchInS(UINT32_MAX),
mLastUserInteractionInPR(PR_Now())
mLastUserInteraction(TimeStamp::Now())
{
#ifdef PR_LOGGING
if (sLog == NULL)
@ -507,7 +507,8 @@ nsIdleService::ResetIdleTimeOut(uint32_t idleDeltaInMS)
idleDeltaInMS));
// Store the time
mLastUserInteractionInPR = PR_Now() - (idleDeltaInMS * PR_USEC_PER_MSEC);
mLastUserInteraction = TimeStamp::Now() -
TimeDuration::FromMilliseconds(idleDeltaInMS);
// If no one is idle, then we are done, any existing timers can keep running.
if (!mAnyObserverIdle) {
@ -594,8 +595,8 @@ nsIdleService::GetIdleTime(uint32_t* idleTime)
polledIdleTimeMS, polledIdleTimeIsValid));
// timeSinceReset is in milliseconds.
uint32_t timeSinceResetInMS = (PR_Now() - mLastUserInteractionInPR) /
PR_USEC_PER_MSEC;
TimeDuration timeSinceReset = TimeStamp::Now() - mLastUserInteraction;
uint32_t timeSinceResetInMS = timeSinceReset.ToMilliseconds();
PR_LOG(sLog, PR_LOG_DEBUG,
("idleService: Get idle time: time since reset %u msec",
@ -644,7 +645,7 @@ void
nsIdleService::IdleTimerCallback(void)
{
// Remember that we no longer have a timer running.
mCurrentlySetToTimeoutAtInPR = 0;
mCurrentlySetToTimeoutAt = TimeStamp();
// Get the current idle time.
uint32_t currentIdleTimeInMS;
@ -672,8 +673,8 @@ nsIdleService::IdleTimerCallback(void)
// we do the calculation in ms to lessen the chance for rounding errors to
// trigger wrong results, it is also very important that we call PR_Now AFTER
// the call to GetIdleTime().
if (((PR_Now() - mLastUserInteractionInPR) / PR_USEC_PER_MSEC) >
currentIdleTimeInMS)
TimeDuration aIdleTime = TimeStamp::Now() - mLastUserInteraction;
if (aIdleTime.ToMilliseconds() > currentIdleTimeInMS)
{
// We had user activity, so handle that part first (to ensure the listeners
// don't risk getting an non-idle after they get a new idle indication.
@ -759,15 +760,16 @@ nsIdleService::IdleTimerCallback(void)
}
void
nsIdleService::SetTimerExpiryIfBefore(PRTime aNextTimeoutInPR)
nsIdleService::SetTimerExpiryIfBefore(TimeStamp aNextTimeout)
{
TimeDuration nextTimeoutDuration = aNextTimeout - TimeStamp::Now();
PR_LOG(sLog, PR_LOG_DEBUG,
("idleService: SetTimerExpiryIfBefore: next timeout %lld usec",
aNextTimeoutInPR));
("idleService: SetTimerExpiryIfBefore: next timeout %0.f msec from now",
nextTimeoutDuration.ToMilliseconds()));
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService",
"SetTimerExpiryIfBefore: next timeout %lld usec",
aNextTimeoutInPR);
"SetTimerExpiryIfBefore: next timeout %0.f msec from now",
nextTimeoutDuration.ToMilliseconds());
#endif
// Bail if we don't have a timer service.
@ -777,41 +779,37 @@ nsIdleService::SetTimerExpiryIfBefore(PRTime aNextTimeoutInPR)
// If the new timeout is before the old one or we don't have a timer running,
// then restart the timer.
if (mCurrentlySetToTimeoutAtInPR > aNextTimeoutInPR ||
!mCurrentlySetToTimeoutAtInPR) {
if (mCurrentlySetToTimeoutAt.IsNull() ||
mCurrentlySetToTimeoutAt > aNextTimeout) {
#if defined(PR_LOGGING) || defined(ANDROID)
PRTime oldTimeout = mCurrentlySetToTimeoutAtInPR;
#endif
mCurrentlySetToTimeoutAtInPR = aNextTimeoutInPR ;
mCurrentlySetToTimeoutAt = aNextTimeout;
// Stop the current timer (it's ok to try'n stop it, even it isn't running).
mTimer->Cancel();
// Check that the timeout is actually in the future, otherwise make it so.
PRTime currentTimeInPR = PR_Now();
if (currentTimeInPR > mCurrentlySetToTimeoutAtInPR) {
mCurrentlySetToTimeoutAtInPR = currentTimeInPR;
TimeStamp currentTime = TimeStamp::Now();
if (currentTime > mCurrentlySetToTimeoutAt) {
mCurrentlySetToTimeoutAt = currentTime;
}
// Add 10 ms to ensure we don't undershoot, and never get a "0" timer.
mCurrentlySetToTimeoutAtInPR += 10 * PR_USEC_PER_MSEC;
mCurrentlySetToTimeoutAt += TimeDuration::FromMilliseconds(10);
TimeDuration deltaTime = mCurrentlySetToTimeoutAt - currentTime;
PR_LOG(sLog, PR_LOG_DEBUG,
("idleService: reset timer expiry from %lld usec to %lld usec",
oldTimeout, mCurrentlySetToTimeoutAtInPR));
("idleService: IdleService", "reset timer expiry to %0.f msec from now",
deltaTime.ToMilliseconds()));
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService",
"reset timer expiry from %lld usec to %lld usec",
oldTimeout, mCurrentlySetToTimeoutAtInPR);
__android_log_print(ANDROID_LOG_INFO, "IdleService",
"reset timer expiry to %0.f msec from now",
deltaTime.ToMilliseconds());
#endif
// Start the timer
mTimer->InitWithFuncCallback(StaticIdleTimerCallback,
this,
(mCurrentlySetToTimeoutAtInPR -
currentTimeInPR) / PR_USEC_PER_MSEC,
deltaTime.ToMilliseconds(),
nsITimer::TYPE_ONE_SHOT);
}
@ -838,28 +836,26 @@ nsIdleService::ReconfigureTimer(void)
// We need to store the current time, so we don't get artifacts from the time
// ticking while we are processing.
PRTime curTimeInPR = PR_Now();
TimeStamp curTime = TimeStamp::Now();
PRTime nextTimeoutAtInPR = mLastUserInteractionInPR +
(((PRTime)mDeltaToNextIdleSwitchInS) *
PR_USEC_PER_SEC);
TimeStamp nextTimeoutAt = mLastUserInteraction +
TimeDuration::FromSeconds(mDeltaToNextIdleSwitchInS);
TimeDuration nextTimeoutDuration = nextTimeoutAt - curTime;
PR_LOG(sLog, PR_LOG_DEBUG,
("idleService: next timeout %lld usec (%u msec from now)",
nextTimeoutAtInPR,
(uint32_t)((nextTimeoutAtInPR - curTimeInPR) / PR_USEC_PER_MSEC)));
("idleService: next timeout %0.f msec from now",
nextTimeoutDuration.ToMilliseconds()));
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService",
"next timeout %lld usec (%lld msec from now)",
nextTimeoutAtInPR,
((nextTimeoutAtInPR - curTimeInPR) / PR_USEC_PER_MSEC));
"next timeout %0.f msec from now",
nextTimeoutDuration.ToMilliseconds());
#endif
// Check if we should correct the timeout time because we should poll before.
if (mAnyObserverIdle && UsePollMode()) {
PRTime pollTimeout = curTimeInPR +
MIN_IDLE_POLL_INTERVAL_MSEC * PR_USEC_PER_MSEC;
TimeStamp pollTimeout =
curTime + TimeDuration::FromMilliseconds(MIN_IDLE_POLL_INTERVAL_MSEC);
if (nextTimeoutAtInPR > pollTimeout) {
if (nextTimeoutAt > pollTimeout) {
PR_LOG(sLog, PR_LOG_DEBUG,
("idleService: idle observers, reducing timeout to %u msec from now",
MIN_IDLE_POLL_INTERVAL_MSEC));
@ -868,10 +864,10 @@ nsIdleService::ReconfigureTimer(void)
"idle observers, reducing timeout to %u msec from now",
MIN_IDLE_POLL_INTERVAL_MSEC);
#endif
nextTimeoutAtInPR = pollTimeout;
nextTimeoutAt = pollTimeout;
}
}
SetTimerExpiryIfBefore(nextTimeoutAtInPR);
SetTimerExpiryIfBefore(nextTimeoutAt);
}

Просмотреть файл

@ -16,6 +16,7 @@
#include "nsIIdleService.h"
#include "nsCategoryCache.h"
#include "nsWeakReference.h"
#include "mozilla/TimeStamp.h"
/**
* Class we can use to store an observer with its associated idle time
@ -155,15 +156,15 @@ private:
*
* The function might not restart the timer if there is one running currently
*
* @param aNextTimeoutInPR
* @param aNextTimeout
* The last absolute time the timer should expire
*/
void SetTimerExpiryIfBefore(PRTime aNextTimeoutInPR);
void SetTimerExpiryIfBefore(mozilla::TimeStamp aNextTimeout);
/**
* Stores the next timeout time, 0 means timer not running
*/
PRTime mCurrentlySetToTimeoutAtInPR;
mozilla::TimeStamp mCurrentlySetToTimeoutAt;
/**
* mTimer holds the internal timer used by this class to detect when to poll
@ -199,7 +200,7 @@ private:
/**
* Absolute value for when the last user interaction took place.
*/
PRTime mLastUserInteractionInPR;
mozilla::TimeStamp mLastUserInteraction;
/**