Bug 564118 - Waking up every second due to IdleService. r=MikeK a=blocking-fennec

This commit is contained in:
Alon Zakai 2010-10-09 11:08:24 -07:00
Родитель 984b79bca9
Коммит 1686e61067
3 изменённых файлов: 28 добавлений и 7 удалений

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

@ -55,6 +55,10 @@ interface nsIIdleService : nsISupports
/**
* The amount of time in milliseconds that has passed
* since the last user activity.
*
* If we do not have a valid idle time to report, 0 is returned
* (this can happen if the user never interacted with the browser
* at all, and if we are also unable to poll for idle time manually).
*/
readonly attribute unsigned long idleTime;

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

@ -179,7 +179,9 @@ nsIdleServiceDaily::DailyCallback(nsITimer* aTimer, void* aClosure)
////////////////////////////////////////////////////////////////////////////////
//// nsIdleService
nsIdleService::nsIdleService() : mLastIdleReset(0), mLastHandledActivity(0)
nsIdleService::nsIdleService() : mLastIdleReset(0)
, mLastHandledActivity(0)
, mPolledIdleTimeIsValid(false)
{
mDailyIdle = new nsIdleServiceDaily(this);
}
@ -239,13 +241,18 @@ nsIdleService::RemoveIdleObserver(nsIObserver* aObserver, PRUint32 aTime)
void
nsIdleService::ResetIdleTimeOut()
{
mLastIdleReset = PR_IntervalToSeconds(PR_IntervalNow());
// A zero in mLastIdleReset indicates that this function has never been
// called.
bool calledBefore = mLastIdleReset != 0;
mLastIdleReset = PR_IntervalToSeconds(PR_IntervalNow());
if (!mLastIdleReset) mLastIdleReset = 1;
// Now check if this changes anything
CheckAwayState(true);
// Note that if we have never been called before, we cannot do the
// optimization of passing true to CheckAwayState, which avoids
// calculating the timer (because if we have never been called before,
// we need to recalculate the timer and start it there).
CheckAwayState(calledBefore);
}
NS_IMETHODIMP
@ -258,12 +265,11 @@ nsIdleService::GetIdleTime(PRUint32* idleTime)
// Polled idle time in ms
PRUint32 polledIdleTimeMS;
bool polledIdleTimeIsValid;
polledIdleTimeIsValid = PollIdleTime(&polledIdleTimeMS);
mPolledIdleTimeIsValid = PollIdleTime(&polledIdleTimeMS);
// If we don't have any valid data, then we are not in idle - pr. definition.
if (!polledIdleTimeIsValid && 0 == mLastIdleReset) {
if (!mPolledIdleTimeIsValid && 0 == mLastIdleReset) {
*idleTime = 0;
return NS_OK;
}
@ -279,7 +285,7 @@ nsIdleService::GetIdleTime(PRUint32* idleTime)
PR_IntervalToSeconds(PR_IntervalNow()) - mLastIdleReset;
// If we did't get pulled data, return the time since last idle reset.
if (!polledIdleTimeIsValid) {
if (!mPolledIdleTimeIsValid) {
// We need to convert to ms before returning the time.
*idleTime = timeSinceReset * 1000;
return NS_OK;
@ -329,6 +335,11 @@ nsIdleService::CheckAwayState(bool aNoTimeReset)
return;
}
// If we have no valid data about the idle time, stop
if (!mPolledIdleTimeIsValid && 0 == mLastIdleReset) {
return;
}
// GetIdleTime returns the time in ms, internally we only calculate in s.
idleTime /= 1000;

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

@ -215,6 +215,12 @@ private:
* Callback function that is called when the internal timer expires.
*/
static void IdleTimerCallback(nsITimer* aTimer, void* aClosure);
/**
* Whether the idle time calculated in the last call to GetIdleTime is
* actually valid (see nsIdleService.idl - we return 0 when it isn't).
*/
bool mPolledIdleTimeIsValid;
};
#endif // nsIdleService_h__