Bug 762620 - Make daily timer callback resilient against Android misfiring. r=roc

This commit is contained in:
Gian-Carlo Pascutto 2012-07-06 07:31:07 +02:00
parent d569ba24da
commit aff45fa51a
2 changed files with 43 additions and 1 deletions

View File

@ -112,6 +112,9 @@ nsIdleServiceDaily::Observe(nsISupports *,
nowSec);
#endif
// Note the moment we started our timer.
mDailyTimerStart = PR_Now();
// Start timer for the next check in one day.
(void)mTimer->InitWithFuncCallback(DailyCallback,
this,
@ -153,6 +156,10 @@ nsIdleServiceDaily::Init()
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService", "Setting timer a day from now");
#endif
// Note the moment we started our timer.
mDailyTimerStart = PR_Now();
// Start timer for the next check in one day.
(void)mTimer->InitWithFuncCallback(DailyCallback,
this,
@ -182,11 +189,40 @@ void
nsIdleServiceDaily::DailyCallback(nsITimer* aTimer, void* aClosure)
{
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService", "DailyCallback running, registering Idle observer");
__android_log_print(ANDROID_LOG_INFO, "IdleService", "DailyCallback running");
#endif
nsIdleServiceDaily* me = static_cast<nsIdleServiceDaily*>(aClosure);
PRTime now = PR_Now();
PRTime launchTime = me->mDailyTimerStart + ((PRTime)SECONDS_PER_DAY * PR_USEC_PER_SEC);
// Check if it has been a day since we launched this timer.
if (now < launchTime) {
// Timer returned early, reschedule.
PRTime newTime = launchTime;
// Add 10 ms to ensure we don't undershoot, and never get a "0" timer.
newTime += 10 * PR_USEC_PER_MSEC;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService",
"DailyCallback resetting timer to %lld msec",
(newTime - now) / PR_USEC_PER_MSEC);
#endif
// Refire the timer.
(void)me->mTimer->InitWithFuncCallback(DailyCallback,
me,
(newTime - now) / PR_USEC_PER_MSEC,
nsITimer::TYPE_ONE_SHOT);
return;
}
#ifdef ANDROID
__android_log_print(ANDROID_LOG_INFO, "IdleService", "DailyCallback registering Idle observer");
#endif
// The one thing we do every day is to start waiting for the user to "have
// a significant idle time".
(void)me->mIdleService->AddIdleObserver(me,

View File

@ -89,6 +89,12 @@ private:
* Boolean set to true when daily idle notifications should be disabled.
*/
bool mShutdownInProgress;
/**
* Real time we fired off the one-day timer, in case timers aren't
* very reliable.
*/
PRTime mDailyTimerStart;
};
class nsIdleService : public nsIIdleServiceInternal