2010-04-16 10:37:16 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:expandtab:shiftwidth=2:tabstop=2:
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Gijs Kruitbosch <gijskruitbosch@gmail.com>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2009-01-31 20:58:32 -08:00
|
|
|
* Gijs Kruitbosch <gijskruitbosch@gmail.com>
|
|
|
|
* Edward Lee <edward.lee@engineering.uiuc.edu>
|
2012-02-28 15:40:38 -08:00
|
|
|
* Mike Kristoffersen <mozstuff@mikek.dk>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsIdleService.h"
|
|
|
|
#include "nsString.h"
|
2009-01-31 20:58:34 -08:00
|
|
|
#include "nsIObserverService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsCOMArray.h"
|
2010-04-16 10:37:16 -07:00
|
|
|
#include "prinrval.h"
|
2012-04-26 16:21:37 -07:00
|
|
|
#include "prlog.h"
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 09:59:13 -07:00
|
|
|
#include "mozilla/Services.h"
|
2011-05-23 07:54:03 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-02-29 12:23:08 -08:00
|
|
|
#include "mozilla/Telemetry.h"
|
2011-05-23 07:54:03 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// observer topics used:
|
|
|
|
#define OBSERVER_TOPIC_IDLE "idle"
|
|
|
|
#define OBSERVER_TOPIC_BACK "back"
|
2009-01-31 20:58:34 -08:00
|
|
|
#define OBSERVER_TOPIC_IDLE_DAILY "idle-daily"
|
2012-02-28 15:40:38 -08:00
|
|
|
// interval in milliseconds between internal idle time requests.
|
|
|
|
#define MIN_IDLE_POLL_INTERVAL_MSEC (5 * PR_MSEC_PER_SEC) /* 5 sec */
|
|
|
|
|
|
|
|
// Time used by the daily idle serivce to determine a significant idle time.
|
|
|
|
#define DAILY_SIGNIFICANT_IDLE_SERVICE_SEC 300 /* 5 min */
|
2010-04-16 10:37:16 -07:00
|
|
|
// Pref for last time (seconds since epoch) daily notification was sent.
|
2009-01-31 20:58:34 -08:00
|
|
|
#define PREF_LAST_DAILY "idle.lastDailyNotification"
|
2010-10-08 03:20:47 -07:00
|
|
|
// Number of seconds in a day.
|
|
|
|
#define SECONDS_PER_DAY 86400
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo *sLog = NULL;
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Use this to find previously added observers in our array:
|
|
|
|
class IdleListenerComparator
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Equals(IdleListener a, IdleListener b) const
|
2010-04-16 10:37:16 -07:00
|
|
|
{
|
|
|
|
return (a.observer == b.observer) &&
|
|
|
|
(a.reqIdleTime == b.reqIdleTime);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIdleServiceDaily
|
|
|
|
|
2012-03-07 08:01:24 -08:00
|
|
|
NS_IMPL_ISUPPORTS2(nsIdleServiceDaily, nsIObserver, nsISupportsWeakReference)
|
2010-04-16 10:37:16 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIdleServiceDaily::Observe(nsISupports *,
|
2012-03-07 08:01:24 -08:00
|
|
|
const char *aTopic,
|
2010-04-16 10:37:16 -07:00
|
|
|
const PRUnichar *)
|
|
|
|
{
|
2012-03-07 08:01:24 -08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mShutdownInProgress || strcmp(aTopic, OBSERVER_TOPIC_BACK) == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(strcmp(aTopic, OBSERVER_TOPIC_IDLE) == 0);
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// Notify anyone who cares.
|
|
|
|
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 09:59:13 -07:00
|
|
|
mozilla::services::GetObserverService();
|
2010-10-08 03:20:47 -07:00
|
|
|
NS_ENSURE_STATE(observerService);
|
|
|
|
(void)observerService->NotifyObservers(nsnull,
|
|
|
|
OBSERVER_TOPIC_IDLE_DAILY,
|
|
|
|
nsnull);
|
2010-10-08 03:20:50 -07:00
|
|
|
|
|
|
|
// Notify the category observers.
|
|
|
|
const nsCOMArray<nsIObserver> &entries = mCategoryObservers.GetEntries();
|
|
|
|
for (PRInt32 i = 0; i < entries.Count(); ++i) {
|
|
|
|
(void)entries[i]->Observe(nsnull, OBSERVER_TOPIC_IDLE_DAILY, nsnull);
|
|
|
|
}
|
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
// Stop observing idle for today.
|
2012-02-28 15:40:38 -08:00
|
|
|
(void)mIdleService->RemoveIdleObserver(this,
|
|
|
|
DAILY_SIGNIFICANT_IDLE_SERVICE_SEC);
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
// Set the last idle-daily time pref.
|
2011-05-23 07:54:03 -07:00
|
|
|
PRInt32 nowSec = static_cast<PRInt32>(PR_Now() / PR_USEC_PER_SEC);
|
|
|
|
Preferences::SetInt(PREF_LAST_DAILY, nowSec);
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
// Start timer for the next check in one day.
|
2012-02-28 15:40:38 -08:00
|
|
|
(void)mTimer->InitWithFuncCallback(DailyCallback,
|
|
|
|
this,
|
|
|
|
SECONDS_PER_DAY * PR_MSEC_PER_SEC,
|
2010-10-08 03:20:47 -07:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-02-24 05:03:59 -08:00
|
|
|
nsIdleServiceDaily::nsIdleServiceDaily(nsIIdleService* aIdleService)
|
2010-10-08 03:20:47 -07:00
|
|
|
: mIdleService(aIdleService)
|
|
|
|
, mTimer(do_CreateInstance(NS_TIMER_CONTRACTID))
|
2010-10-08 03:20:50 -07:00
|
|
|
, mCategoryObservers(OBSERVER_TOPIC_IDLE_DAILY)
|
2012-03-07 08:01:24 -08:00
|
|
|
, mShutdownInProgress(false)
|
2011-02-24 05:03:59 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIdleServiceDaily::Init()
|
2010-04-16 10:37:16 -07:00
|
|
|
{
|
2010-10-08 03:20:47 -07:00
|
|
|
// Check time of the last idle-daily notification. If it was more than 24
|
|
|
|
// hours ago listen for idle, otherwise set a timer for 24 hours from now.
|
|
|
|
PRInt32 nowSec = static_cast<PRInt32>(PR_Now() / PR_USEC_PER_SEC);
|
2011-05-23 07:54:03 -07:00
|
|
|
PRInt32 lastDaily = Preferences::GetInt(PREF_LAST_DAILY, 0);
|
|
|
|
if (lastDaily < 0 || lastDaily > nowSec) {
|
|
|
|
// The time is bogus, use default.
|
|
|
|
lastDaily = 0;
|
2010-10-08 03:20:47 -07:00
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
// Check if it has been a day since the last notification.
|
|
|
|
if (nowSec - lastDaily > SECONDS_PER_DAY) {
|
|
|
|
// Wait for the user to become idle, so we can do todays idle tasks.
|
|
|
|
DailyCallback(nsnull, this);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Start timer for the next check in one day.
|
2012-02-28 15:40:38 -08:00
|
|
|
(void)mTimer->InitWithFuncCallback(DailyCallback,
|
|
|
|
this,
|
|
|
|
SECONDS_PER_DAY * PR_MSEC_PER_SEC,
|
2010-10-08 03:20:47 -07:00
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
2012-03-07 08:01:24 -08:00
|
|
|
|
|
|
|
// Register for when we should terminate/pause
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->AddObserver(this, "xpcom-will-shutdown", true);
|
|
|
|
obs->AddObserver(this, "profile-change-teardown", true);
|
|
|
|
obs->AddObserver(this, "profile-after-change", true);
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
|
|
|
|
2011-02-24 05:03:59 -08:00
|
|
|
nsIdleServiceDaily::~nsIdleServiceDaily()
|
2010-04-16 10:37:16 -07:00
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
// static
|
2010-04-16 10:37:16 -07:00
|
|
|
void
|
|
|
|
nsIdleServiceDaily::DailyCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
nsIdleServiceDaily* me = static_cast<nsIdleServiceDaily*>(aClosure);
|
|
|
|
|
|
|
|
// The one thing we do every day is to start waiting for the user to "have
|
|
|
|
// a significant idle time".
|
2012-02-28 15:40:38 -08:00
|
|
|
(void)me->mIdleService->AddIdleObserver(me,
|
|
|
|
DAILY_SIGNIFICANT_IDLE_SERVICE_SEC);
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:38 -08: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 03:20:47 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIdleService
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::nsIdleService() : mCurrentlySetToTimeoutAtInPR(0),
|
|
|
|
mAnyObserverIdle(false),
|
|
|
|
mDeltaToNextIdleSwitchInS(PR_UINT32_MAX),
|
2012-04-26 16:21:37 -07:00
|
|
|
mLastUserInteractionInPR(PR_Now())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-26 16:21:37 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (sLog == NULL)
|
|
|
|
sLog = PR_NewLogModule("idleService");
|
|
|
|
#endif
|
2010-10-08 03:20:47 -07:00
|
|
|
mDailyIdle = new nsIdleServiceDaily(this);
|
2011-02-24 05:03:59 -08:00
|
|
|
mDailyIdle->Init();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIdleService::~nsIdleService()
|
|
|
|
{
|
2012-02-28 15:40:38 -08:00
|
|
|
if(mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::AddIdleObserver(nsIObserver* aObserver, PRUint32 aIdleTimeInS)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Register idle observer %x for %d seconds",
|
|
|
|
aObserver, aIdleTimeInS));
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
2012-02-28 15:40:38 -08:00
|
|
|
// 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, (PR_UINT32_MAX / 10) - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// Put the time + observer in a struct we can keep:
|
2012-02-28 15:40:38 -08:00
|
|
|
IdleListener listener(aObserver, aIdleTimeInS);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
if (!mArrayListeners.AppendElement(listener)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// Create our timer callback if it's not there already.
|
|
|
|
if (!mTimer) {
|
|
|
|
nsresult rv;
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Check if the newly added observer has a smaller wait time than what we
|
2012-04-26 16:21:37 -07:00
|
|
|
// are waiting for now.
|
2012-02-28 15:40:38 -08: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).
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Register: adjusting next switch from %d to %d seconds",
|
|
|
|
mDeltaToNextIdleSwitchInS, aIdleTimeInS));
|
2012-02-28 15:40:38 -08:00
|
|
|
mDeltaToNextIdleSwitchInS = aIdleTimeInS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure timer is running.
|
|
|
|
ReconfigureTimer();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::RemoveIdleObserver(nsIObserver* aObserver, PRUint32 aTimeInS)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-28 15:40:38 -08:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
2012-02-28 15:40:38 -08:00
|
|
|
NS_ENSURE_ARG(aTimeInS);
|
|
|
|
IdleListener listener(aObserver, aTimeInS);
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08: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 10:37:16 -07:00
|
|
|
IdleListenerComparator c;
|
|
|
|
if (mArrayListeners.RemoveElement(listener, c)) {
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Remove idle observer %x (%d seconds)",
|
|
|
|
aObserver, aTimeInS));
|
2010-04-16 10:37:16 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// If we get here, we haven't removed anything:
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_WARNING,
|
|
|
|
("idleService: Failed to remove idle observer %x (%d seconds)",
|
|
|
|
aObserver, aTimeInS));
|
2010-04-16 10:37:16 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::ResetIdleTimeOut(PRUint32 idleDeltaInMS)
|
2010-03-30 15:03:05 -07:00
|
|
|
{
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Reset idle timeout (last interaction %u msec)",
|
|
|
|
idleDeltaInMS));
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Store the time
|
|
|
|
mLastUserInteractionInPR = PR_Now() - (idleDeltaInMS * PR_USEC_PER_MSEC);
|
|
|
|
|
|
|
|
// If no one is idle, then we are done, any existing timers can keep running.
|
|
|
|
if (!mAnyObserverIdle) {
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Reset idle timeout: no idle observers"));
|
2012-02-28 15:40:38 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all idle services as non-idle, and calculate the next idle timeout.
|
2012-02-29 12:23:08 -08:00
|
|
|
Telemetry::AutoTimer<Telemetry::IDLE_NOTIFY_BACK_MS> timer;
|
2012-02-28 15:40:38 -08:00
|
|
|
nsCOMArray<nsIObserver> notifyList;
|
|
|
|
mDeltaToNextIdleSwitchInS = PR_UINT32_MAX;
|
|
|
|
|
2012-02-29 12:23:08 -08:00
|
|
|
// Loop through all listeners, and find any that have detected idle.
|
2012-02-28 15:40:38 -08:00
|
|
|
for (PRUint32 i = 0; i < mArrayListeners.Length(); i++) {
|
|
|
|
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.
|
|
|
|
mDeltaToNextIdleSwitchInS = PR_MIN(mDeltaToNextIdleSwitchInS,
|
|
|
|
curListener.reqIdleTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we are done, then we wont have anyone idle.
|
|
|
|
mAnyObserverIdle = false;
|
|
|
|
|
|
|
|
// Restart the idle timer, and do so before anyone can delay us.
|
|
|
|
ReconfigureTimer();
|
|
|
|
|
|
|
|
PRInt32 numberOfPendingNotifications = notifyList.Count();
|
2012-02-29 12:23:08 -08:00
|
|
|
Telemetry::Accumulate(Telemetry::IDLE_NOTIFY_BACK_LISTENERS,
|
|
|
|
numberOfPendingNotifications);
|
2012-02-28 15:40:38 -08:00
|
|
|
|
|
|
|
// Bail if nothing to do.
|
|
|
|
if (!numberOfPendingNotifications) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now send "back" events to all, if any should have timed out allready, then
|
|
|
|
// they will be reawaken by the timer that is already running.
|
|
|
|
|
|
|
|
// We need a text string to send with any state change events.
|
|
|
|
nsAutoString timeStr;
|
|
|
|
|
|
|
|
timeStr.AppendInt((PRInt32)(idleDeltaInMS / PR_MSEC_PER_SEC));
|
|
|
|
|
|
|
|
// Send the "non-idle" events.
|
|
|
|
while (numberOfPendingNotifications--) {
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Reset idle timeout: tell observer %x user is back",
|
|
|
|
notifyList[numberOfPendingNotifications]));
|
2012-02-28 15:40:38 -08:00
|
|
|
notifyList[numberOfPendingNotifications]->Observe(this,
|
|
|
|
OBSERVER_TOPIC_BACK,
|
|
|
|
timeStr.get());
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2010-03-30 15:03:05 -07:00
|
|
|
}
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIdleService::GetIdleTime(PRUint32* idleTime)
|
|
|
|
{
|
|
|
|
// Check sanity of in parameter.
|
|
|
|
if (!idleTime) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Polled idle time in ms.
|
2010-04-16 10:37:16 -07:00
|
|
|
PRUint32 polledIdleTimeMS;
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
bool polledIdleTimeIsValid = PollIdleTime(&polledIdleTimeMS);
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Get idle time: polled %u msec, valid = %d",
|
|
|
|
polledIdleTimeMS, polledIdleTimeIsValid));
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// timeSinceReset is in milliseconds.
|
|
|
|
PRUint32 timeSinceResetInMS = (PR_Now() - mLastUserInteractionInPR) /
|
|
|
|
PR_USEC_PER_MSEC;
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Get idle time: time since reset %u msec",
|
|
|
|
timeSinceResetInMS));
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// If we did't get pulled data, return the time since last idle reset.
|
2012-02-28 15:40:38 -08:00
|
|
|
if (!polledIdleTimeIsValid) {
|
2010-04-16 10:37:16 -07:00
|
|
|
// We need to convert to ms before returning the time.
|
2012-02-28 15:40:38 -08:00
|
|
|
*idleTime = timeSinceResetInMS;
|
2010-04-16 10:37:16 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise return the shortest time detected (in ms).
|
2012-02-28 15:40:38 -08:00
|
|
|
*idleTime = NS_MIN(timeSinceResetInMS, polledIdleTimeMS);
|
2010-04-16 10:37:16 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsIdleService::PollIdleTime(PRUint32* /*aIdleTime*/)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
// Default behavior is not to have the ability to poll an idle time.
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
bool
|
|
|
|
nsIdleService::UsePollMode()
|
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
|
|
|
return PollIdleTime(&dummy);
|
|
|
|
}
|
2009-01-31 20:58:32 -08:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
void
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::StaticIdleTimerCallback(nsITimer* aTimer, void* aClosure)
|
2010-04-16 10:37:16 -07:00
|
|
|
{
|
2012-02-28 15:40:38 -08:00
|
|
|
static_cast<nsIdleService*>(aClosure)->IdleTimerCallback();
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
void
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::IdleTimerCallback(void)
|
2010-04-16 10:37:16 -07:00
|
|
|
{
|
2012-02-28 15:40:38 -08:00
|
|
|
// Remember that we no longer have a timer running.
|
|
|
|
mCurrentlySetToTimeoutAtInPR = 0;
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Get the current idle time.
|
|
|
|
PRUint32 currentIdleTimeInMS;
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
if (NS_FAILED(GetIdleTime(¤tIdleTimeInMS))) {
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_ALWAYS,
|
|
|
|
("idleService: Idle timer callback: failed to get idle time"));
|
2010-10-09 11:08:24 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Idle timer callback: current idle time %u msec",
|
|
|
|
currentIdleTimeInMS));
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Check if we have had some user interaction we didn't handle previously
|
2012-04-26 16:21:37 -07:00
|
|
|
// we do the calculation in ms to lessen the chance for rounding errors to
|
2012-02-28 15:40:38 -08:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
// 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 06:38:16 -08:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// NOTE: We can't bail here, as we might have something already timed out.
|
2012-01-11 06:38:16 -08:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Find the idle time in S.
|
|
|
|
PRUint32 currentIdleTimeInS = currentIdleTimeInMS / PR_MSEC_PER_SEC;
|
2012-01-11 06:38:16 -08:00
|
|
|
|
2012-02-28 15:40:38 -08: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 10:37:16 -07:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Tell expired listeners they are expired,and find the next timeout
|
2012-02-29 12:23:08 -08:00
|
|
|
Telemetry::AutoTimer<Telemetry::IDLE_NOTIFY_IDLE_MS> timer;
|
2012-01-11 06:38:16 -08:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// We need to initialise the time to the next idle switch.
|
|
|
|
mDeltaToNextIdleSwitchInS = PR_UINT32_MAX;
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Create list of observers that should be notified.
|
2012-01-11 06:38:16 -08:00
|
|
|
nsCOMArray<nsIObserver> notifyList;
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-01-11 06:38:16 -08:00
|
|
|
for (PRUint32 i = 0; i < mArrayListeners.Length(); i++) {
|
|
|
|
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-28 15:40:38 -08:00
|
|
|
if (curListener.reqIdleTime <= currentIdleTimeInS) {
|
|
|
|
// Then add the listener to the list of listeners that should be
|
2012-01-11 06:38:16 -08:00
|
|
|
// notified.
|
|
|
|
notifyList.AppendObject(curListener.observer);
|
|
|
|
// This listener is now idle.
|
|
|
|
curListener.isIdle = true;
|
2012-02-28 15:40:38 -08:00
|
|
|
} else {
|
|
|
|
// Listeners that are not timed out yet are candidates for timing out.
|
|
|
|
mDeltaToNextIdleSwitchInS = PR_MIN(mDeltaToNextIdleSwitchInS,
|
|
|
|
curListener.reqIdleTime);
|
2012-01-11 06:38:16 -08:00
|
|
|
}
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
2012-01-11 06:38:16 -08:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Restart the timer before any notifications that could slow us down are
|
|
|
|
// done.
|
|
|
|
ReconfigureTimer();
|
|
|
|
|
2012-01-11 06:38:16 -08:00
|
|
|
PRInt32 numberOfPendingNotifications = notifyList.Count();
|
2012-02-29 12:23:08 -08:00
|
|
|
Telemetry::Accumulate(Telemetry::IDLE_NOTIFY_IDLE_LISTENERS,
|
|
|
|
numberOfPendingNotifications);
|
2012-01-11 06:38:16 -08:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Bail if nothing to do.
|
|
|
|
if (!numberOfPendingNotifications) {
|
|
|
|
return;
|
2012-01-11 06:38:16 -08:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Remember we have someone idle.
|
|
|
|
mAnyObserverIdle = true;
|
|
|
|
|
2012-01-11 06:38:16 -08:00
|
|
|
// We need a text string to send with any state change events.
|
|
|
|
nsAutoString timeStr;
|
2012-02-28 15:40:38 -08:00
|
|
|
timeStr.AppendInt(currentIdleTimeInS);
|
2012-01-11 06:38:16 -08:00
|
|
|
|
|
|
|
// Notify all listeners that just timed out.
|
2012-02-28 15:40:38 -08:00
|
|
|
while (numberOfPendingNotifications--) {
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: Idle timer callback: tell observer %x user is idle",
|
|
|
|
notifyList[numberOfPendingNotifications]));
|
2012-01-11 06:38:16 -08:00
|
|
|
notifyList[numberOfPendingNotifications]->Observe(this,
|
|
|
|
OBSERVER_TOPIC_IDLE,
|
|
|
|
timeStr.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::SetTimerExpiryIfBefore(PRTime aNextTimeoutInPR)
|
2012-01-11 06:38:16 -08:00
|
|
|
{
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: SetTimerExpiryIfBefore: next timeout %lld usec",
|
|
|
|
aNextTimeoutInPR));
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Bail if we don't have a timer service.
|
|
|
|
if (!mTimer) {
|
|
|
|
return;
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// 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) {
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRTime oldTimeout = mCurrentlySetToTimeoutAtInPR;
|
|
|
|
#endif
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
mCurrentlySetToTimeoutAtInPR = aNextTimeoutInPR ;
|
2009-01-31 20:58:34 -08:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Stop the current timer (it's ok to try'n stop it, even it isn't running).
|
|
|
|
mTimer->Cancel();
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Check that the timeout is actually in the future, otherwise make it so.
|
|
|
|
PRTime currentTimeInPR = PR_Now();
|
|
|
|
if (currentTimeInPR > mCurrentlySetToTimeoutAtInPR) {
|
|
|
|
mCurrentlySetToTimeoutAtInPR = currentTimeInPR;
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Add 10 ms to ensure we don't undershoot, and never get a "0" timer.
|
|
|
|
mCurrentlySetToTimeoutAtInPR += 10 * PR_USEC_PER_MSEC;
|
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: reset timer expiry from %lld usec to %lld usec",
|
|
|
|
oldTimeout, mCurrentlySetToTimeoutAtInPR));
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
// Start the timer
|
|
|
|
mTimer->InitWithFuncCallback(StaticIdleTimerCallback,
|
|
|
|
this,
|
|
|
|
(mCurrentlySetToTimeoutAtInPR -
|
|
|
|
currentTimeInPR) / PR_USEC_PER_MSEC,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2010-04-16 10:37:16 -07:00
|
|
|
|
|
|
|
}
|
2009-01-31 20:58:34 -08:00
|
|
|
}
|
|
|
|
|
2012-02-28 15:40:38 -08:00
|
|
|
|
2009-01-31 20:58:34 -08:00
|
|
|
void
|
2012-02-28 15:40:38 -08:00
|
|
|
nsIdleService::ReconfigureTimer(void)
|
2009-01-31 20:58:34 -08:00
|
|
|
{
|
2012-02-28 15:40:38 -08:00
|
|
|
// Check if either someone is idle, or someone will become idle.
|
|
|
|
if (!mAnyObserverIdle && PR_UINT32_MAX == mDeltaToNextIdleSwitchInS) {
|
|
|
|
// If not, just let any existing timers run to completion
|
|
|
|
// And bail out.
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: ReconfigureTimer: no idle or waiting observers"));
|
2012-02-28 15:40:38 -08:00
|
|
|
return;
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
2009-01-31 20:58:34 -08:00
|
|
|
|
2012-02-28 15:40:38 -08: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.
|
|
|
|
PRTime curTimeInPR = PR_Now();
|
|
|
|
|
|
|
|
PRTime nextTimeoutAtInPR = mLastUserInteractionInPR +
|
|
|
|
(((PRTime)mDeltaToNextIdleSwitchInS) *
|
|
|
|
PR_USEC_PER_SEC);
|
|
|
|
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: next timeout %lld usec (%u msec from now)",
|
|
|
|
nextTimeoutAtInPR,
|
|
|
|
(PRUint32)((nextTimeoutAtInPR - curTimeInPR) / PR_USEC_PER_MSEC)));
|
2012-02-28 15:40:38 -08:00
|
|
|
// Check if we should correct the timeout time because we should poll before.
|
|
|
|
if (mAnyObserverIdle && UsePollMode()) {
|
|
|
|
PRTime pollTimeout = curTimeInPR +
|
2012-04-26 16:21:37 -07:00
|
|
|
MIN_IDLE_POLL_INTERVAL_MSEC * PR_USEC_PER_MSEC;
|
2012-02-28 15:40:38 -08:00
|
|
|
|
|
|
|
if (nextTimeoutAtInPR > pollTimeout) {
|
2012-04-26 16:21:37 -07:00
|
|
|
PR_LOG(sLog, PR_LOG_DEBUG,
|
|
|
|
("idleService: idle observers, reducing timeout to %u msec from now",
|
|
|
|
MIN_IDLE_POLL_INTERVAL_MSEC));
|
2012-02-28 15:40:38 -08:00
|
|
|
nextTimeoutAtInPR = pollTimeout;
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
2012-02-28 15:40:38 -08:00
|
|
|
|
|
|
|
SetTimerExpiryIfBefore(nextTimeoutAtInPR);
|
2009-01-31 20:58:34 -08:00
|
|
|
}
|
2009-10-06 11:47:46 -07:00
|
|
|
|