2010-04-16 21:37:16 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=2:tabstop=2:
|
2007-01-08 21:13:16 +03:00
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2007-01-08 21:13:16 +03:00
|
|
|
|
|
|
|
#include "nsIdleService.h"
|
|
|
|
#include "nsString.h"
|
2009-02-01 07:58:34 +03:00
|
|
|
#include "nsIObserverService.h"
|
2007-01-08 21:13:16 +03:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsCOMArray.h"
|
2013-12-13 20:28:46 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2010-04-16 21:37:16 +04:00
|
|
|
#include "prinrval.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-07-03 19:56:26 +04:00
|
|
|
#include "prtime.h"
|
2013-12-13 20:28:46 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2011-05-23 18:54:03 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-03-01 00:23:08 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2011-05-23 18:54:03 +04:00
|
|
|
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2012-06-13 13:50:40 +04:00
|
|
|
# include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-23 18:54:03 +04:00
|
|
|
using namespace mozilla;
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// interval in milliseconds between internal idle time requests.
|
|
|
|
#define MIN_IDLE_POLL_INTERVAL_MSEC (5 * PR_MSEC_PER_SEC) /* 5 sec */
|
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// After the twenty four hour period expires for an idle daily, this is the
|
|
|
|
// amount of idle time we wait for before actually firing the idle-daily
|
|
|
|
// event.
|
|
|
|
#define DAILY_SIGNIFICANT_IDLE_SERVICE_SEC (3 * 60)
|
|
|
|
|
|
|
|
// In cases where it's been longer than twenty four hours since the last
|
|
|
|
// idle-daily, this is the shortend amount of idle time we wait for before
|
|
|
|
// firing the idle-daily event.
|
|
|
|
#define DAILY_SHORTENED_IDLE_SERVICE_SEC 60
|
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
// Pref for last time (seconds since epoch) daily notification was sent.
|
2009-02-01 07:58:34 +03:00
|
|
|
#define PREF_LAST_DAILY "idle.lastDailyNotification"
|
2012-10-12 17:02:24 +04:00
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
// Number of seconds in a day.
|
|
|
|
#define SECONDS_PER_DAY 86400
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2016-05-18 22:55:42 +03:00
|
|
|
static LazyLogModule sLog("idleService");
|
2012-04-27 03:21:37 +04:00
|
|
|
|
2014-11-01 02:39:46 +03:00
|
|
|
#define LOG_TAG "GeckoIdleService"
|
|
|
|
#define LOG_LEVEL ANDROID_LOG_DEBUG
|
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
// Use this to find previously added observers in our array:
|
|
|
|
class IdleListenerComparator {
|
|
|
|
public:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(IdleListener a, IdleListener b) const {
|
2010-04-16 21:37:16 +04:00
|
|
|
return (a.observer == b.observer) && (a.reqIdleTime == b.reqIdleTime);
|
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
};
|
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIdleServiceDaily
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsIdleServiceDaily, nsIObserver, nsISupportsWeakReference)
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIdleServiceDaily::Observe(nsISupports*, const char* aTopic, const char16_t*) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Observe '%s' (%d)", aTopic, mShutdownInProgress));
|
|
|
|
|
2012-03-07 20:01:24 +04:00
|
|
|
if (strcmp(aTopic, "profile-after-change") == 0) {
|
|
|
|
// We are back. Start sending notifications again.
|
|
|
|
mShutdownInProgress = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(aTopic, "xpcom-will-shutdown") == 0 ||
|
|
|
|
strcmp(aTopic, "profile-change-teardown") == 0) {
|
|
|
|
mShutdownInProgress = true;
|
|
|
|
}
|
|
|
|
|
2012-06-29 12:32:21 +04:00
|
|
|
if (mShutdownInProgress || strcmp(aTopic, OBSERVER_TOPIC_ACTIVE) == 0) {
|
2012-03-07 20:01:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(strcmp(aTopic, OBSERVER_TOPIC_IDLE) == 0);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Notifying idle-daily observers"));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG, "Notifying idle-daily observers");
|
2012-06-13 13:50:40 +04:00
|
|
|
#endif
|
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// Send the idle-daily observer event
|
2010-04-16 21:37:16 +04:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2010-10-08 14:20:47 +04:00
|
|
|
NS_ENSURE_STATE(observerService);
|
2012-07-30 18:20:58 +04:00
|
|
|
(void)observerService->NotifyObservers(nullptr, OBSERVER_TOPIC_IDLE_DAILY,
|
|
|
|
nullptr);
|
2010-10-08 14:20:50 +04:00
|
|
|
|
|
|
|
// Notify the category observers.
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCOMArray<nsIObserver> entries;
|
|
|
|
mCategoryObservers.GetEntries(entries);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < entries.Count(); ++i) {
|
2012-07-30 18:20:58 +04:00
|
|
|
(void)entries[i]->Observe(nullptr, OBSERVER_TOPIC_IDLE_DAILY, nullptr);
|
2010-10-08 14:20:50 +04:00
|
|
|
}
|
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
// Stop observing idle for today.
|
2012-10-12 17:02:24 +04:00
|
|
|
(void)mIdleService->RemoveIdleObserver(this, mIdleDailyTriggerWait);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
// Set the last idle-daily time pref.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nowSec = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
|
2011-05-23 18:54:03 +04:00
|
|
|
Preferences::SetInt(PREF_LAST_DAILY, nowSec);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-07-06 09:30:55 +04:00
|
|
|
// Force that to be stored so we don't retrigger twice a day under
|
|
|
|
// any circumstances.
|
|
|
|
nsIPrefService* prefs = Preferences::GetService();
|
|
|
|
if (prefs) {
|
2012-07-30 18:20:58 +04:00
|
|
|
prefs->SavePrefFile(nullptr);
|
2012-07-06 09:30:55 +04:00
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Storing last idle time as %d sec.", nowSec));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG, "Storing last idle time as %d",
|
2012-10-12 17:02:24 +04:00
|
|
|
nowSec);
|
2012-06-13 13:50:40 +04:00
|
|
|
#endif
|
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// Note the moment we expect to get the next timer callback
|
|
|
|
mExpectedTriggerTime =
|
|
|
|
PR_Now() + ((PRTime)SECONDS_PER_DAY * (PRTime)PR_USEC_PER_SEC);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Restarting daily timer"));
|
2012-07-06 09:31:07 +04:00
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
// Start timer for the next check in one day.
|
2017-06-12 22:34:10 +03:00
|
|
|
(void)mTimer->InitWithNamedFuncCallback(
|
|
|
|
DailyCallback, this, SECONDS_PER_DAY * PR_MSEC_PER_SEC,
|
|
|
|
nsITimer::TYPE_ONE_SHOT, "nsIdleServiceDaily::Observe");
|
2010-10-08 14:20:47 +04:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-24 16:03:59 +03:00
|
|
|
nsIdleServiceDaily::nsIdleServiceDaily(nsIIdleService* aIdleService)
|
2010-10-08 14:20:47 +04:00
|
|
|
: mIdleService(aIdleService),
|
2017-10-16 09:12:54 +03:00
|
|
|
mTimer(NS_NewTimer()),
|
2010-10-08 14:20:50 +04:00
|
|
|
mCategoryObservers(OBSERVER_TOPIC_IDLE_DAILY),
|
2012-10-14 01:31:02 +04:00
|
|
|
mShutdownInProgress(false),
|
2012-10-12 17:02:24 +04:00
|
|
|
mExpectedTriggerTime(0),
|
|
|
|
mIdleDailyTriggerWait(DAILY_SIGNIFICANT_IDLE_SERVICE_SEC) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-02-24 16:03:59 +03:00
|
|
|
void nsIdleServiceDaily::Init() {
|
2012-10-12 17:02:24 +04:00
|
|
|
// First check the time of the last idle-daily event notification. If it
|
|
|
|
// has been 24 hours or higher, or if we have never sent an idle-daily,
|
|
|
|
// get ready to send an idle-daily event. Otherwise set a timer targeted
|
|
|
|
// at 24 hours past the last idle-daily we sent.
|
|
|
|
|
2019-02-08 20:34:20 +03:00
|
|
|
int32_t lastDaily = Preferences::GetInt(PREF_LAST_DAILY, 0);
|
2019-02-09 16:29:21 +03:00
|
|
|
// Setting the pref to -1 allows to disable idle-daily, and it's particularly
|
|
|
|
// useful in tests. Normally there should be no need for the user to set
|
|
|
|
// this value.
|
|
|
|
if (lastDaily == -1) {
|
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
|
|
|
("nsIdleServiceDaily: Init: disabled idle-daily"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t nowSec = static_cast<int32_t>(PR_Now() / PR_USEC_PER_SEC);
|
2011-05-23 18:54:03 +04:00
|
|
|
if (lastDaily < 0 || lastDaily > nowSec) {
|
|
|
|
// The time is bogus, use default.
|
|
|
|
lastDaily = 0;
|
2010-10-08 14:20:47 +04:00
|
|
|
}
|
2012-10-12 17:02:24 +04:00
|
|
|
int32_t secondsSinceLastDaily = nowSec - lastDaily;
|
2012-10-09 23:45:28 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Init: seconds since last daily: %d",
|
|
|
|
secondsSinceLastDaily));
|
2012-07-06 09:31:07 +04:00
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// If it has been twenty four hours or more or if we have never sent an
|
|
|
|
// idle-daily event get ready to send it during the next idle period.
|
|
|
|
if (secondsSinceLastDaily > SECONDS_PER_DAY) {
|
|
|
|
// Check for a "long wait", e.g. 48-hours or more.
|
|
|
|
bool hasBeenLongWait =
|
|
|
|
(lastDaily && (secondsSinceLastDaily > (SECONDS_PER_DAY * 2)));
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: has been long wait? %d", hasBeenLongWait));
|
|
|
|
|
|
|
|
// StageIdleDaily sets up a wait for the user to become idle and then
|
|
|
|
// sends the idle-daily event.
|
|
|
|
StageIdleDaily(hasBeenLongWait);
|
|
|
|
} else {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Setting timer a day from now"));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG, "Setting timer a day from now");
|
2012-10-10 04:43:39 +04:00
|
|
|
#endif
|
2012-10-09 23:45:28 +04:00
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// According to our last idle-daily pref, the last idle-daily was fired
|
|
|
|
// less then 24 hours ago. Set a wait for the amount of time remaining.
|
|
|
|
int32_t milliSecLeftUntilDaily =
|
|
|
|
(SECONDS_PER_DAY - secondsSinceLastDaily) * PR_MSEC_PER_SEC;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Seconds till next timeout: %d",
|
|
|
|
(SECONDS_PER_DAY - secondsSinceLastDaily)));
|
|
|
|
|
|
|
|
// Mark the time at which we expect this to fire. On systems with faulty
|
|
|
|
// timers, we need to be able to cross check that the timer fired at the
|
|
|
|
// expected time.
|
|
|
|
mExpectedTriggerTime =
|
|
|
|
PR_Now() + (milliSecLeftUntilDaily * PR_USEC_PER_MSEC);
|
2012-07-06 09:31:07 +04:00
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
(void)mTimer->InitWithNamedFuncCallback(
|
|
|
|
DailyCallback, this, milliSecLeftUntilDaily, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"nsIdleServiceDaily::Init");
|
2010-10-08 14:20:47 +04:00
|
|
|
}
|
2012-03-07 20:01:24 +04:00
|
|
|
|
|
|
|
// Register for when we should terminate/pause
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Registering for system event observers."));
|
2012-03-07 20:01:24 +04:00
|
|
|
obs->AddObserver(this, "xpcom-will-shutdown", true);
|
|
|
|
obs->AddObserver(this, "profile-change-teardown", true);
|
|
|
|
obs->AddObserver(this, "profile-after-change", true);
|
|
|
|
}
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:03:59 +03:00
|
|
|
nsIdleServiceDaily::~nsIdleServiceDaily() {
|
2010-04-16 21:37:16 +04:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mTimer = nullptr;
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
void nsIdleServiceDaily::StageIdleDaily(bool aHasBeenLongWait) {
|
|
|
|
NS_ASSERTION(mIdleService, "No idle service available?");
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("nsIdleServiceDaily: Registering Idle observer callback "
|
|
|
|
"(short wait requested? %d)",
|
|
|
|
aHasBeenLongWait));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG, "Registering Idle observer callback");
|
2012-10-12 17:02:24 +04:00
|
|
|
#endif
|
|
|
|
mIdleDailyTriggerWait =
|
|
|
|
(aHasBeenLongWait ? DAILY_SHORTENED_IDLE_SERVICE_SEC
|
|
|
|
: DAILY_SIGNIFICANT_IDLE_SERVICE_SEC);
|
|
|
|
(void)mIdleService->AddIdleObserver(this, mIdleDailyTriggerWait);
|
|
|
|
}
|
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
// static
|
2010-04-16 21:37:16 +04:00
|
|
|
void nsIdleServiceDaily::DailyCallback(nsITimer* aTimer, void* aClosure) {
|
2012-10-12 17:02:24 +04:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug, ("nsIdleServiceDaily: DailyCallback running"));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG, "DailyCallback running");
|
2012-06-28 16:57:54 +04:00
|
|
|
#endif
|
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
nsIdleServiceDaily* self = static_cast<nsIdleServiceDaily*>(aClosure);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// Check to be sure the timer didn't fire early. This currently only
|
|
|
|
// happens on android.
|
2012-07-06 09:31:07 +04:00
|
|
|
PRTime now = PR_Now();
|
2012-10-12 17:02:24 +04:00
|
|
|
if (self->mExpectedTriggerTime && now < self->mExpectedTriggerTime) {
|
|
|
|
// Timer returned early, reschedule to the appropriate time.
|
2013-03-01 01:16:19 +04:00
|
|
|
PRTime delayTime = self->mExpectedTriggerTime - now;
|
2012-07-06 09:31:07 +04:00
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// Add 10 ms to ensure we don't undershoot, and never get a "0" timer.
|
2013-03-01 01:16:19 +04:00
|
|
|
delayTime += 10 * PR_USEC_PER_MSEC;
|
2012-07-06 09:31:07 +04:00
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
|
|
|
("nsIdleServiceDaily: DailyCallback resetting timer to %" PRId64
|
|
|
|
" msec",
|
2013-03-01 01:16:19 +04:00
|
|
|
delayTime / PR_USEC_PER_MSEC));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2016-12-16 06:16:31 +03:00
|
|
|
"DailyCallback resetting timer to %" PRId64 " msec",
|
2013-03-01 01:16:19 +04:00
|
|
|
delayTime / PR_USEC_PER_MSEC);
|
2012-07-06 09:31:07 +04:00
|
|
|
#endif
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
(void)self->mTimer->InitWithNamedFuncCallback(
|
|
|
|
DailyCallback, self, delayTime / PR_USEC_PER_MSEC,
|
|
|
|
nsITimer::TYPE_ONE_SHOT, "nsIdleServiceDaily::DailyCallback");
|
2012-10-12 17:02:24 +04:00
|
|
|
return;
|
2012-07-06 09:31:07 +04:00
|
|
|
}
|
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
// Register for a short term wait for idle event. When this fires we fire
|
|
|
|
// our idle-daily event.
|
|
|
|
self->StageIdleDaily(false);
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
/**
|
|
|
|
* The idle services goal is to notify subscribers when a certain time has
|
|
|
|
* passed since the last user interaction with the system.
|
|
|
|
*
|
|
|
|
* On some platforms this is defined as the last time user events reached this
|
|
|
|
* application, on other platforms it is a system wide thing - the preferred
|
|
|
|
* implementation is to use the system idle time, rather than the application
|
|
|
|
* idle time, as the things depending on the idle service are likely to use
|
|
|
|
* significant resources (network, disk, memory, cpu, etc.).
|
|
|
|
*
|
|
|
|
* When the idle service needs to use the system wide idle timer, it typically
|
|
|
|
* needs to poll the idle time value by the means of a timer. It needs to
|
|
|
|
* poll fast when it is in active idle mode (when it has a listener in the idle
|
|
|
|
* mode) as it needs to detect if the user is active in other applications.
|
|
|
|
*
|
|
|
|
* When the service is waiting for the first listener to become idle, or when
|
|
|
|
* it is only monitoring application idle time, it only needs to have the timer
|
|
|
|
* expire at the time the next listener goes idle.
|
|
|
|
*
|
|
|
|
* The core state of the service is determined by:
|
|
|
|
*
|
|
|
|
* - A list of listeners.
|
|
|
|
*
|
|
|
|
* - A boolean that tells if any listeners are in idle mode.
|
|
|
|
*
|
|
|
|
* - A delta value that indicates when, measured from the last non-idle time,
|
|
|
|
* the next listener should switch to idle mode.
|
|
|
|
*
|
|
|
|
* - An absolute time of the last time idle mode was detected (this is used to
|
|
|
|
* judge if we have been out of idle mode since the last invocation of the
|
|
|
|
* service.
|
|
|
|
*
|
|
|
|
* There are four entry points into the system:
|
|
|
|
*
|
|
|
|
* - A new listener is registered.
|
|
|
|
*
|
|
|
|
* - An existing listener is deregistered.
|
|
|
|
*
|
|
|
|
* - User interaction is detected.
|
|
|
|
*
|
|
|
|
* - The timer expires.
|
|
|
|
*
|
|
|
|
* When a new listener is added its idle timeout, is compared with the next idle
|
|
|
|
* timeout, and if lower, that time is stored as the new timeout, and the timer
|
|
|
|
* is reconfigured to ensure a timeout around the time the new listener should
|
|
|
|
* timeout.
|
|
|
|
*
|
|
|
|
* If the next idle time is above the idle time requested by the new listener
|
|
|
|
* it won't be informed until the timer expires, this is to avoid recursive
|
|
|
|
* behavior and to simplify the code. In this case the timer will be set to
|
|
|
|
* about 10 ms.
|
|
|
|
*
|
|
|
|
* When an existing listener is deregistered, it is just removed from the list
|
|
|
|
* of active listeners, we don't stop the timer, we just let it expire.
|
|
|
|
*
|
|
|
|
* When user interaction is detected, either because it was directly detected or
|
|
|
|
* because we polled the system timer and found it to be unexpected low, then we
|
|
|
|
* check the flag that tells us if any listeners are in idle mode, if there are
|
|
|
|
* they are removed from idle mode and told so, and we reset our state
|
|
|
|
* caculating the next timeout and restart the timer if needed.
|
|
|
|
*
|
|
|
|
* ---- Build in logic
|
|
|
|
*
|
|
|
|
* In order to avoid restarting the timer endlessly, the timer function has
|
|
|
|
* logic that will only restart the timer, if the requested timeout is before
|
|
|
|
* the current timeout.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-10-08 14:20:47 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIdleService
|
|
|
|
|
2012-06-29 12:32:21 +04:00
|
|
|
namespace {
|
|
|
|
nsIdleService* gIdleService;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-06-29 12:32:21 +04:00
|
|
|
|
|
|
|
already_AddRefed<nsIdleService> nsIdleService::GetInstance() {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIdleService> instance(gIdleService);
|
2012-06-29 12:32:21 +04:00
|
|
|
return instance.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
nsIdleService::nsIdleService()
|
|
|
|
: mCurrentlySetToTimeoutAt(TimeStamp()),
|
2013-02-21 20:29:30 +04:00
|
|
|
mIdleObserverCount(0),
|
2012-09-28 10:57:33 +04:00
|
|
|
mDeltaToNextIdleSwitchInS(UINT32_MAX),
|
2012-10-26 06:17:36 +04:00
|
|
|
mLastUserInteraction(TimeStamp::Now()) {
|
2012-06-29 12:32:21 +04:00
|
|
|
MOZ_ASSERT(!gIdleService);
|
|
|
|
gIdleService = this;
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
2013-12-13 20:28:46 +04:00
|
|
|
mDailyIdle = new nsIdleServiceDaily(this);
|
|
|
|
mDailyIdle->Init();
|
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIdleService::~nsIdleService() {
|
2012-02-29 03:40:38 +04:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2012-06-29 12:32:21 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(gIdleService == this);
|
2012-07-30 18:20:58 +04:00
|
|
|
gIdleService = nullptr;
|
2007-01-08 21:13:16 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsIdleService, nsIIdleService, nsIIdleServiceInternal)
|
2012-06-29 12:32:21 +04:00
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIdleService::AddIdleObserver(nsIObserver* aObserver, uint32_t aIdleTimeInS) {
|
2013-12-13 20:28:46 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
|
|
|
// We don't accept idle time at 0, and we can't handle idle time that are too
|
|
|
|
// high either - no more than ~136 years.
|
|
|
|
NS_ENSURE_ARG_RANGE(aIdleTimeInS, 1, (UINT32_MAX / 10) - 1);
|
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
2013-12-13 20:28:46 +04:00
|
|
|
dom::ContentChild* cpc = dom::ContentChild::GetSingleton();
|
|
|
|
cpc->AddIdleObserver(aObserver, aIdleTimeInS);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2014-04-22 19:02:36 +04:00
|
|
|
("idleService: Register idle observer %p for %d seconds", aObserver,
|
2013-12-13 20:28:46 +04:00
|
|
|
aIdleTimeInS));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2014-04-22 19:02:36 +04:00
|
|
|
"Register idle observer %p for %d seconds", aObserver,
|
2012-06-13 13:50:40 +04:00
|
|
|
aIdleTimeInS);
|
|
|
|
#endif
|
2012-04-27 03:21:37 +04:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
// Put the time + observer in a struct we can keep:
|
2012-02-29 03:40:38 +04:00
|
|
|
IdleListener listener(aObserver, aIdleTimeInS);
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
if (!mArrayListeners.AppendElement(listener)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
// Create our timer callback if it's not there already.
|
|
|
|
if (!mTimer) {
|
2017-10-16 09:12:54 +03:00
|
|
|
mTimer = NS_NewTimer();
|
|
|
|
NS_ENSURE_TRUE(mTimer, NS_ERROR_OUT_OF_MEMORY);
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Check if the newly added observer has a smaller wait time than what we
|
2012-04-27 03:21:37 +04:00
|
|
|
// are waiting for now.
|
2012-02-29 03:40:38 +04:00
|
|
|
if (mDeltaToNextIdleSwitchInS > aIdleTimeInS) {
|
|
|
|
// If it is, then this is the next to move to idle (at this point we
|
|
|
|
// don't care if it should have switched already).
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Register: adjusting next switch from %d to %d seconds",
|
|
|
|
mDeltaToNextIdleSwitchInS, aIdleTimeInS));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-06-13 13:50:40 +04:00
|
|
|
"Register: adjusting next switch from %d to %d seconds",
|
|
|
|
mDeltaToNextIdleSwitchInS, aIdleTimeInS);
|
|
|
|
#endif
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
mDeltaToNextIdleSwitchInS = aIdleTimeInS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure timer is running.
|
|
|
|
ReconfigureTimer();
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
return NS_OK;
|
2007-01-08 21:13:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIdleService::RemoveIdleObserver(nsIObserver* aObserver, uint32_t aTimeInS) {
|
2010-04-16 21:37:16 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
2012-02-29 03:40:38 +04:00
|
|
|
NS_ENSURE_ARG(aTimeInS);
|
2013-12-13 20:28:46 +04:00
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
2013-12-13 20:28:46 +04:00
|
|
|
dom::ContentChild* cpc = dom::ContentChild::GetSingleton();
|
|
|
|
cpc->RemoveIdleObserver(aObserver, aTimeInS);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
IdleListener listener(aObserver, aTimeInS);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Find the entry and remove it, if it was the last entry, we just let the
|
|
|
|
// existing timer run to completion (there might be a new registration in a
|
|
|
|
// little while.
|
2010-04-16 21:37:16 +04:00
|
|
|
IdleListenerComparator c;
|
2013-02-21 20:29:30 +04:00
|
|
|
nsTArray<IdleListener>::index_type listenerIndex =
|
|
|
|
mArrayListeners.IndexOf(listener, 0, c);
|
|
|
|
if (listenerIndex != mArrayListeners.NoIndex) {
|
|
|
|
if (mArrayListeners.ElementAt(listenerIndex).isIdle) mIdleObserverCount--;
|
|
|
|
mArrayListeners.RemoveElementAt(listenerIndex);
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2014-04-22 19:02:36 +04:00
|
|
|
("idleService: Remove observer %p (%d seconds), %d remain idle",
|
2013-02-21 20:29:30 +04:00
|
|
|
aObserver, aTimeInS, mIdleObserverCount));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2014-04-22 19:02:36 +04:00
|
|
|
"Remove observer %p (%d seconds), %d remain idle",
|
2013-02-21 20:29:30 +04:00
|
|
|
aObserver, aTimeInS, mIdleObserverCount);
|
2012-06-13 13:50:40 +04:00
|
|
|
#endif
|
2010-04-16 21:37:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
// If we get here, we haven't removed anything:
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Warning,
|
2014-04-22 19:02:36 +04:00
|
|
|
("idleService: Failed to remove idle observer %p (%d seconds)",
|
2012-04-27 03:21:37 +04:00
|
|
|
aObserver, aTimeInS));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2014-04-22 19:02:36 +04:00
|
|
|
"Failed to remove idle observer %p (%d seconds)",
|
2012-06-13 13:50:40 +04:00
|
|
|
aObserver, aTimeInS);
|
|
|
|
#endif
|
2010-04-16 21:37:16 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-01-08 21:13:16 +03:00
|
|
|
}
|
|
|
|
|
2012-06-29 12:32:21 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIdleService::ResetIdleTimeOut(uint32_t idleDeltaInMS) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Reset idle timeout (last interaction %u msec)",
|
|
|
|
idleDeltaInMS));
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Store the time
|
2012-10-26 06:17:36 +04:00
|
|
|
mLastUserInteraction =
|
|
|
|
TimeStamp::Now() - TimeDuration::FromMilliseconds(idleDeltaInMS);
|
2012-02-29 03:40:38 +04:00
|
|
|
|
|
|
|
// If no one is idle, then we are done, any existing timers can keep running.
|
2013-02-21 20:29:30 +04:00
|
|
|
if (mIdleObserverCount == 0) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Reset idle timeout: no idle observers"));
|
2012-06-29 12:32:21 +04:00
|
|
|
return NS_OK;
|
2012-02-29 03:40:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all idle services as non-idle, and calculate the next idle timeout.
|
|
|
|
nsCOMArray<nsIObserver> notifyList;
|
2012-09-28 10:57:33 +04:00
|
|
|
mDeltaToNextIdleSwitchInS = UINT32_MAX;
|
2012-02-29 03:40:38 +04:00
|
|
|
|
2012-03-01 00:23:08 +04:00
|
|
|
// Loop through all listeners, and find any that have detected idle.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mArrayListeners.Length(); i++) {
|
2012-02-29 03:40:38 +04:00
|
|
|
IdleListener& curListener = mArrayListeners.ElementAt(i);
|
|
|
|
|
|
|
|
// If the listener was idle, then he shouldn't be any longer.
|
|
|
|
if (curListener.isIdle) {
|
|
|
|
notifyList.AppendObject(curListener.observer);
|
|
|
|
curListener.isIdle = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the listener is the next one to timeout.
|
2013-01-15 16:22:03 +04:00
|
|
|
mDeltaToNextIdleSwitchInS =
|
|
|
|
std::min(mDeltaToNextIdleSwitchInS, curListener.reqIdleTime);
|
2012-02-29 03:40:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// When we are done, then we wont have anyone idle.
|
2013-02-21 20:29:30 +04:00
|
|
|
mIdleObserverCount = 0;
|
2012-02-29 03:40:38 +04:00
|
|
|
|
|
|
|
// Restart the idle timer, and do so before anyone can delay us.
|
|
|
|
ReconfigureTimer();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numberOfPendingNotifications = notifyList.Count();
|
2012-02-29 03:40:38 +04:00
|
|
|
|
|
|
|
// Bail if nothing to do.
|
|
|
|
if (!numberOfPendingNotifications) {
|
2012-06-29 12:32:21 +04:00
|
|
|
return NS_OK;
|
2012-02-29 03:40:38 +04:00
|
|
|
}
|
|
|
|
|
2014-01-08 21:43:03 +04:00
|
|
|
// Now send "active" events to all, if any should have timed out already,
|
|
|
|
// then they will be reawaken by the timer that is already running.
|
2012-02-29 03:40:38 +04:00
|
|
|
|
|
|
|
// We need a text string to send with any state change events.
|
|
|
|
nsAutoString timeStr;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
timeStr.AppendInt((int32_t)(idleDeltaInMS / PR_MSEC_PER_SEC));
|
2012-02-29 03:40:38 +04:00
|
|
|
|
|
|
|
// Send the "non-idle" events.
|
|
|
|
while (numberOfPendingNotifications--) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2014-04-22 19:02:36 +04:00
|
|
|
("idleService: Reset idle timeout: tell observer %p user is back",
|
2012-04-27 03:21:37 +04:00
|
|
|
notifyList[numberOfPendingNotifications]));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2014-04-22 19:02:36 +04:00
|
|
|
"Reset idle timeout: tell observer %p user is back",
|
2012-06-13 13:50:40 +04:00
|
|
|
notifyList[numberOfPendingNotifications]);
|
|
|
|
#endif
|
2012-02-29 03:40:38 +04:00
|
|
|
notifyList[numberOfPendingNotifications]->Observe(
|
2012-06-29 12:32:21 +04:00
|
|
|
this, OBSERVER_TOPIC_ACTIVE, timeStr.get());
|
2012-02-29 03:40:38 +04:00
|
|
|
}
|
2012-06-29 12:32:21 +04:00
|
|
|
return NS_OK;
|
2010-03-31 02:03:05 +04:00
|
|
|
}
|
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsIdleService::GetIdleTime(uint32_t* idleTime) {
|
2010-04-16 21:37:16 +04:00
|
|
|
// Check sanity of in parameter.
|
|
|
|
if (!idleTime) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Polled idle time in ms.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t polledIdleTimeMS;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
bool polledIdleTimeIsValid = PollIdleTime(&polledIdleTimeMS);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Get idle time: polled %u msec, valid = %d",
|
|
|
|
polledIdleTimeMS, polledIdleTimeIsValid));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// timeSinceReset is in milliseconds.
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeDuration timeSinceReset = TimeStamp::Now() - mLastUserInteraction;
|
|
|
|
uint32_t timeSinceResetInMS = timeSinceReset.ToMilliseconds();
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Get idle time: time since reset %u msec",
|
|
|
|
timeSinceResetInMS));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-06-13 13:50:40 +04:00
|
|
|
"Get idle time: time since reset %u msec",
|
|
|
|
timeSinceResetInMS);
|
|
|
|
#endif
|
2012-04-27 03:21:37 +04:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
// If we did't get pulled data, return the time since last idle reset.
|
2012-02-29 03:40:38 +04:00
|
|
|
if (!polledIdleTimeIsValid) {
|
2010-04-16 21:37:16 +04:00
|
|
|
// We need to convert to ms before returning the time.
|
2012-02-29 03:40:38 +04:00
|
|
|
*idleTime = timeSinceResetInMS;
|
2010-04-16 21:37:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise return the shortest time detected (in ms).
|
2013-01-15 16:22:03 +04:00
|
|
|
*idleTime = std::min(timeSinceResetInMS, polledIdleTimeMS);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool nsIdleService::PollIdleTime(uint32_t* /*aIdleTime*/) {
|
2010-04-16 21:37:16 +04:00
|
|
|
// Default behavior is not to have the ability to poll an idle time.
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
bool nsIdleService::UsePollMode() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dummy;
|
2010-04-16 21:37:16 +04:00
|
|
|
return PollIdleTime(&dummy);
|
|
|
|
}
|
2009-02-01 07:58:32 +03:00
|
|
|
|
2019-01-25 04:27:24 +03:00
|
|
|
nsresult nsIdleService::GetDisabled(bool* aResult) {
|
|
|
|
*aResult = mDisabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsIdleService::SetDisabled(bool aDisabled) {
|
|
|
|
mDisabled = aDisabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
void nsIdleService::StaticIdleTimerCallback(nsITimer* aTimer, void* aClosure) {
|
|
|
|
static_cast<nsIdleService*>(aClosure)->IdleTimerCallback();
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
void nsIdleService::IdleTimerCallback(void) {
|
|
|
|
// Remember that we no longer have a timer running.
|
2012-10-26 06:17:36 +04:00
|
|
|
mCurrentlySetToTimeoutAt = TimeStamp();
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-12-03 06:21:00 +04:00
|
|
|
// Find the last detected idle time.
|
|
|
|
uint32_t lastIdleTimeInMS = static_cast<uint32_t>(
|
|
|
|
(TimeStamp::Now() - mLastUserInteraction).ToMilliseconds());
|
2012-02-29 03:40:38 +04:00
|
|
|
// Get the current idle time.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t currentIdleTimeInMS;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
if (NS_FAILED(GetIdleTime(¤tIdleTimeInMS))) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Info,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Idle timer callback: failed to get idle time"));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-06-13 13:50:40 +04:00
|
|
|
"Idle timer callback: failed to get idle time");
|
|
|
|
#endif
|
2010-10-09 22:08:24 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: Idle timer callback: current idle time %u msec",
|
|
|
|
currentIdleTimeInMS));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-06-13 13:50:40 +04:00
|
|
|
"Idle timer callback: current idle time %u msec",
|
|
|
|
currentIdleTimeInMS);
|
|
|
|
#endif
|
2012-04-27 03:21:37 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Check if we have had some user interaction we didn't handle previously
|
2012-04-27 03:21:37 +04:00
|
|
|
// we do the calculation in ms to lessen the chance for rounding errors to
|
2012-12-03 06:21:00 +04:00
|
|
|
// trigger wrong results.
|
|
|
|
if (lastIdleTimeInMS > currentIdleTimeInMS) {
|
2012-02-29 03:40:38 +04:00
|
|
|
// 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.
|
|
|
|
ResetIdleTimeOut(currentIdleTimeInMS);
|
2012-01-11 18:38:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// NOTE: We can't bail here, as we might have something already timed out.
|
2012-01-11 18:38:16 +04:00
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Find the idle time in S.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t currentIdleTimeInS = currentIdleTimeInMS / PR_MSEC_PER_SEC;
|
2012-01-11 18:38:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Restart timer and bail if no-one are expected to be in idle
|
|
|
|
if (mDeltaToNextIdleSwitchInS > currentIdleTimeInS) {
|
|
|
|
// If we didn't expect anyone to be idle, then just re-start the timer.
|
|
|
|
ReconfigureTimer();
|
|
|
|
return;
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
|
|
|
|
2019-01-25 04:27:24 +03:00
|
|
|
if (mDisabled) {
|
|
|
|
MOZ_LOG(sLog, LogLevel::Info,
|
|
|
|
("idleService: Skipping idle callback while disabled"));
|
|
|
|
|
|
|
|
ReconfigureTimer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Tell expired listeners they are expired,and find the next timeout
|
2012-03-01 00:23:08 +04:00
|
|
|
Telemetry::AutoTimer<Telemetry::IDLE_NOTIFY_IDLE_MS> timer;
|
2012-01-11 18:38:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// We need to initialise the time to the next idle switch.
|
2012-09-28 10:57:33 +04:00
|
|
|
mDeltaToNextIdleSwitchInS = UINT32_MAX;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Create list of observers that should be notified.
|
2012-01-11 18:38:16 +04:00
|
|
|
nsCOMArray<nsIObserver> notifyList;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mArrayListeners.Length(); i++) {
|
2012-01-11 18:38:16 +04:00
|
|
|
IdleListener& curListener = mArrayListeners.ElementAt(i);
|
|
|
|
|
|
|
|
// We are only interested in items, that are not in the idle state.
|
|
|
|
if (!curListener.isIdle) {
|
|
|
|
// If they have an idle time smaller than the actual idle time.
|
2012-02-29 03:40:38 +04:00
|
|
|
if (curListener.reqIdleTime <= currentIdleTimeInS) {
|
|
|
|
// Then add the listener to the list of listeners that should be
|
2012-01-11 18:38:16 +04:00
|
|
|
// notified.
|
|
|
|
notifyList.AppendObject(curListener.observer);
|
|
|
|
// This listener is now idle.
|
|
|
|
curListener.isIdle = true;
|
2012-08-02 21:38:30 +04:00
|
|
|
// Remember we have someone idle.
|
2013-02-21 20:29:30 +04:00
|
|
|
mIdleObserverCount++;
|
2012-02-29 03:40:38 +04:00
|
|
|
} else {
|
|
|
|
// Listeners that are not timed out yet are candidates for timing out.
|
2013-01-15 16:22:03 +04:00
|
|
|
mDeltaToNextIdleSwitchInS =
|
|
|
|
std::min(mDeltaToNextIdleSwitchInS, curListener.reqIdleTime);
|
2012-01-11 18:38:16 +04:00
|
|
|
}
|
|
|
|
}
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
2012-01-11 18:38:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Restart the timer before any notifications that could slow us down are
|
|
|
|
// done.
|
|
|
|
ReconfigureTimer();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numberOfPendingNotifications = notifyList.Count();
|
2012-01-11 18:38:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Bail if nothing to do.
|
|
|
|
if (!numberOfPendingNotifications) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2012-10-12 17:02:24 +04:00
|
|
|
("idleService: **** Idle timer callback: no observers to message."));
|
2012-02-29 03:40:38 +04:00
|
|
|
return;
|
2012-01-11 18:38:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We need a text string to send with any state change events.
|
|
|
|
nsAutoString timeStr;
|
2012-02-29 03:40:38 +04:00
|
|
|
timeStr.AppendInt(currentIdleTimeInS);
|
2012-01-11 18:38:16 +04:00
|
|
|
|
|
|
|
// Notify all listeners that just timed out.
|
2012-02-29 03:40:38 +04:00
|
|
|
while (numberOfPendingNotifications--) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2014-04-22 19:02:36 +04:00
|
|
|
("idleService: **** Idle timer callback: tell observer %p user is idle",
|
2012-04-27 03:21:37 +04:00
|
|
|
notifyList[numberOfPendingNotifications]));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2014-04-22 19:02:36 +04:00
|
|
|
"Idle timer callback: tell observer %p user is idle",
|
2012-06-13 13:50:40 +04:00
|
|
|
notifyList[numberOfPendingNotifications]);
|
|
|
|
#endif
|
2012-01-11 18:38:16 +04:00
|
|
|
notifyList[numberOfPendingNotifications]->Observe(this, OBSERVER_TOPIC_IDLE,
|
|
|
|
timeStr.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
void nsIdleService::SetTimerExpiryIfBefore(TimeStamp aNextTimeout) {
|
|
|
|
TimeDuration nextTimeoutDuration = aNextTimeout - TimeStamp::Now();
|
2013-10-28 10:12:55 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2012-10-26 06:17:36 +04:00
|
|
|
("idleService: SetTimerExpiryIfBefore: next timeout %0.f msec from now",
|
|
|
|
nextTimeoutDuration.ToMilliseconds()));
|
2013-10-28 10:12:55 +04:00
|
|
|
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-10-26 06:17:36 +04:00
|
|
|
"SetTimerExpiryIfBefore: next timeout %0.f msec from now",
|
|
|
|
nextTimeoutDuration.ToMilliseconds());
|
2012-06-13 13:50:40 +04:00
|
|
|
#endif
|
2012-04-27 03:21:37 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Bail if we don't have a timer service.
|
|
|
|
if (!mTimer) {
|
|
|
|
return;
|
|
|
|
}
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// If the new timeout is before the old one or we don't have a timer running,
|
|
|
|
// then restart the timer.
|
2012-10-26 06:17:36 +04:00
|
|
|
if (mCurrentlySetToTimeoutAt.IsNull() ||
|
|
|
|
mCurrentlySetToTimeoutAt > aNextTimeout) {
|
|
|
|
mCurrentlySetToTimeoutAt = aNextTimeout;
|
2009-02-01 07:58:34 +03:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Stop the current timer (it's ok to try'n stop it, even it isn't running).
|
|
|
|
mTimer->Cancel();
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Check that the timeout is actually in the future, otherwise make it so.
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeStamp currentTime = TimeStamp::Now();
|
|
|
|
if (currentTime > mCurrentlySetToTimeoutAt) {
|
|
|
|
mCurrentlySetToTimeoutAt = currentTime;
|
2012-02-29 03:40:38 +04:00
|
|
|
}
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Add 10 ms to ensure we don't undershoot, and never get a "0" timer.
|
2012-10-26 06:17:36 +04:00
|
|
|
mCurrentlySetToTimeoutAt += TimeDuration::FromMilliseconds(10);
|
2012-02-29 03:40:38 +04:00
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeDuration deltaTime = mCurrentlySetToTimeoutAt - currentTime;
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2012-12-03 06:21:00 +04:00
|
|
|
("idleService: IdleService reset timer expiry to %0.f msec from now",
|
2012-10-26 06:17:36 +04:00
|
|
|
deltaTime.ToMilliseconds()));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-10-26 06:17:36 +04:00
|
|
|
"reset timer expiry to %0.f msec from now",
|
|
|
|
deltaTime.ToMilliseconds());
|
2012-06-13 13:50:40 +04:00
|
|
|
#endif
|
2012-04-27 03:21:37 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Start the timer
|
2017-06-12 22:34:10 +03:00
|
|
|
mTimer->InitWithNamedFuncCallback(
|
|
|
|
StaticIdleTimerCallback, this, deltaTime.ToMilliseconds(),
|
|
|
|
nsITimer::TYPE_ONE_SHOT, "nsIdleService::SetTimerExpiryIfBefore");
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
2009-02-01 07:58:34 +03:00
|
|
|
}
|
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
void nsIdleService::ReconfigureTimer(void) {
|
|
|
|
// Check if either someone is idle, or someone will become idle.
|
2013-02-21 20:29:30 +04:00
|
|
|
if ((mIdleObserverCount == 0) && UINT32_MAX == mDeltaToNextIdleSwitchInS) {
|
2012-02-29 03:40:38 +04:00
|
|
|
// If not, just let any existing timers run to completion
|
|
|
|
// And bail out.
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-04-27 03:21:37 +04:00
|
|
|
("idleService: ReconfigureTimer: no idle or waiting observers"));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG,
|
2012-06-13 13:50:40 +04:00
|
|
|
"ReconfigureTimer: no idle or waiting observers");
|
|
|
|
#endif
|
2012-02-29 03:40:38 +04:00
|
|
|
return;
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
2009-02-01 07:58:34 +03:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Find the next timeout value, assuming we are not polling.
|
|
|
|
|
|
|
|
// We need to store the current time, so we don't get artifacts from the time
|
|
|
|
// ticking while we are processing.
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeStamp curTime = TimeStamp::Now();
|
2012-02-29 03:40:38 +04:00
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeStamp nextTimeoutAt =
|
|
|
|
mLastUserInteraction +
|
|
|
|
TimeDuration::FromSeconds(mDeltaToNextIdleSwitchInS);
|
2012-02-29 03:40:38 +04:00
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeDuration nextTimeoutDuration = nextTimeoutAt - curTime;
|
2013-10-28 10:12:55 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(sLog, LogLevel::Debug,
|
2012-10-26 06:17:36 +04:00
|
|
|
("idleService: next timeout %0.f msec from now",
|
|
|
|
nextTimeoutDuration.ToMilliseconds()));
|
2013-10-28 10:12:55 +04:00
|
|
|
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(LOG_LEVEL, LOG_TAG, "next timeout %0.f msec from now",
|
2012-10-26 06:17:36 +04:00
|
|
|
nextTimeoutDuration.ToMilliseconds());
|
2012-06-13 13:50:40 +04:00
|
|
|
#endif
|
2013-10-28 10:12:55 +04:00
|
|
|
|
2012-02-29 03:40:38 +04:00
|
|
|
// Check if we should correct the timeout time because we should poll before.
|
2013-02-21 20:29:30 +04:00
|
|
|
if ((mIdleObserverCount > 0) && UsePollMode()) {
|
2012-10-26 06:17:36 +04:00
|
|
|
TimeStamp pollTimeout =
|
|
|
|
curTime + TimeDuration::FromMilliseconds(MIN_IDLE_POLL_INTERVAL_MSEC);
|
2012-02-29 03:40:38 +04:00
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
if (nextTimeoutAt > pollTimeout) {
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(
|
|
|
|
sLog, LogLevel::Debug,
|
2014-04-22 19:02:36 +04:00
|
|
|
("idleService: idle observers, reducing timeout to %lu msec from now",
|
2012-04-27 03:21:37 +04:00
|
|
|
MIN_IDLE_POLL_INTERVAL_MSEC));
|
2013-01-26 02:00:31 +04:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2014-11-01 02:39:46 +03:00
|
|
|
__android_log_print(
|
|
|
|
LOG_LEVEL, LOG_TAG,
|
2014-04-22 19:02:36 +04:00
|
|
|
"idle observers, reducing timeout to %lu msec from now",
|
2012-06-13 13:50:40 +04:00
|
|
|
MIN_IDLE_POLL_INTERVAL_MSEC);
|
|
|
|
#endif
|
2012-10-26 06:17:36 +04:00
|
|
|
nextTimeoutAt = pollTimeout;
|
2012-02-29 03:40:38 +04:00
|
|
|
}
|
2010-04-16 21:37:16 +04:00
|
|
|
}
|
2012-02-29 03:40:38 +04:00
|
|
|
|
2012-10-26 06:17:36 +04:00
|
|
|
SetTimerExpiryIfBefore(nextTimeoutAt);
|
2009-02-01 07:58:34 +03:00
|
|
|
}
|