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>
|
2010-04-16 10:37:16 -07:00
|
|
|
* Mike Kristoffersen <moz@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"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.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"
|
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"
|
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"
|
2010-04-16 10:37:16 -07:00
|
|
|
// interval in seconds between internal idle time requests.
|
|
|
|
#define MIN_IDLE_POLL_INTERVAL 5
|
|
|
|
#define MAX_IDLE_POLL_INTERVAL 300 /* 5 min */
|
|
|
|
// 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
|
|
|
|
|
|
|
// Use this to find previously added observers in our array:
|
|
|
|
class IdleListenerComparator
|
|
|
|
{
|
|
|
|
public:
|
2010-04-16 10:37:16 -07:00
|
|
|
PRBool Equals(IdleListener a, IdleListener b) const
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsIdleServiceDaily, nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIdleServiceDaily::Observe(nsISupports *,
|
|
|
|
const char *,
|
|
|
|
const PRUnichar *)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
// Stop observing idle for today.
|
|
|
|
if (NS_SUCCEEDED(mIdleService->RemoveIdleObserver(this, MAX_IDLE_POLL_INTERVAL))) {
|
|
|
|
mObservesIdle = false;
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
// Set the last idle-daily time pref.
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (pref) {
|
|
|
|
PRInt32 nowSec = static_cast<PRInt32>(PR_Now() / PR_USEC_PER_SEC);
|
|
|
|
(void)pref->SetIntPref(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.
|
|
|
|
(void)mTimer->InitWithFuncCallback(DailyCallback, this, SECONDS_PER_DAY * 1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
nsIdleServiceDaily::nsIdleServiceDaily(nsIdleService* aIdleService)
|
|
|
|
: mIdleService(aIdleService)
|
|
|
|
, mObservesIdle(false)
|
|
|
|
, mTimer(do_CreateInstance(NS_TIMER_CONTRACTID))
|
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 lastDaily = 0;
|
|
|
|
PRInt32 nowSec = static_cast<PRInt32>(PR_Now() / PR_USEC_PER_SEC);
|
|
|
|
nsCOMPtr<nsIPrefBranch> pref = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (pref) {
|
|
|
|
if (NS_FAILED(pref->GetIntPref(PREF_LAST_DAILY, &lastDaily)) ||
|
|
|
|
lastDaily < 0 || lastDaily > nowSec) {
|
|
|
|
// The time is bogus, use default.
|
|
|
|
lastDaily = 0;
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
(void)mTimer->InitWithFuncCallback(DailyCallback, this, SECONDS_PER_DAY * 1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIdleServiceDaily::Shutdown()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nsnull;
|
|
|
|
}
|
2010-10-08 03:20:47 -07:00
|
|
|
if (mIdleService && mObservesIdle) {
|
|
|
|
if (NS_SUCCEEDED(mIdleService->RemoveIdleObserver(this, MAX_IDLE_POLL_INTERVAL))) {
|
|
|
|
mObservesIdle = false;
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
mIdleService = 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".
|
2010-10-08 03:20:47 -07:00
|
|
|
if (NS_SUCCEEDED(me->mIdleService->AddIdleObserver(me, MAX_IDLE_POLL_INTERVAL))) {
|
|
|
|
me->mObservesIdle = true;
|
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
|
|
|
|
2010-10-08 03:20:47 -07:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIdleService
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
nsIdleService::nsIdleService() : mLastIdleReset(0), mLastHandledActivity(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-10-08 03:20:47 -07:00
|
|
|
mDailyIdle = new nsIdleServiceDaily(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIdleService::~nsIdleService()
|
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
StopTimer();
|
|
|
|
mDailyIdle->Shutdown();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIdleService::AddIdleObserver(nsIObserver* aObserver, PRUint32 aIdleTime)
|
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
|
|
|
NS_ENSURE_ARG(aIdleTime);
|
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:
|
|
|
|
IdleListener listener(aObserver, aIdleTime);
|
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
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// Make sure our observer goes into 'idle' immediately if applicable.
|
|
|
|
CheckAwayState(false);
|
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
|
|
|
|
nsIdleService::RemoveIdleObserver(nsIObserver* aObserver, PRUint32 aTime)
|
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aObserver);
|
|
|
|
NS_ENSURE_ARG(aTime);
|
|
|
|
IdleListener listener(aObserver, aTime);
|
|
|
|
|
|
|
|
// Find the entry and remove it:
|
|
|
|
IdleListenerComparator c;
|
|
|
|
if (mArrayListeners.RemoveElement(listener, c)) {
|
|
|
|
if (mArrayListeners.IsEmpty()) {
|
|
|
|
StopTimer();
|
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
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// If we get here, we haven't removed anything:
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-16 10:37:16 -07:00
|
|
|
nsIdleService::ResetIdleTimeOut()
|
2010-03-30 15:03:05 -07:00
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
mLastIdleReset = PR_IntervalToSeconds(PR_IntervalNow());
|
|
|
|
// A zero in mLastIdleReset indicates that this function has never been
|
|
|
|
// called.
|
|
|
|
if (!mLastIdleReset) mLastIdleReset = 1;
|
|
|
|
|
|
|
|
// Now check if this changes anything
|
|
|
|
CheckAwayState(true);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polled idle time in ms
|
|
|
|
PRUint32 polledIdleTimeMS;
|
|
|
|
bool polledIdleTimeIsValid;
|
|
|
|
|
|
|
|
polledIdleTimeIsValid = PollIdleTime(&polledIdleTimeMS);
|
|
|
|
|
|
|
|
// If we don't have any valid data, then we are not in idle - pr. definition.
|
|
|
|
if (!polledIdleTimeIsValid && 0 == mLastIdleReset) {
|
|
|
|
*idleTime = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we never got a reset, just return the pulled time.
|
|
|
|
if (0 == mLastIdleReset) {
|
|
|
|
*idleTime = polledIdleTimeMS;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// timeSinceReset is in seconds.
|
|
|
|
PRUint32 timeSinceReset =
|
|
|
|
PR_IntervalToSeconds(PR_IntervalNow()) - mLastIdleReset;
|
|
|
|
|
|
|
|
// If we did't get pulled data, return the time since last idle reset.
|
|
|
|
if (!polledIdleTimeIsValid) {
|
|
|
|
// We need to convert to ms before returning the time.
|
|
|
|
*idleTime = timeSinceReset * 1000;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise return the shortest time detected (in ms).
|
|
|
|
*idleTime = NS_MIN(timeSinceReset * 1000, polledIdleTimeMS);
|
|
|
|
|
|
|
|
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
|
|
|
|
nsIdleService::IdleTimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
static_cast<nsIdleService*>(aClosure)->CheckAwayState(false);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
void
|
|
|
|
nsIdleService::CheckAwayState(bool aNoTimeReset)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Find our last detected idle time (it's important this happens before the
|
|
|
|
* call below to GetIdleTime, as we use the two values to detect if there
|
|
|
|
* has been user activity since the last time we were here).
|
|
|
|
*/
|
2010-10-08 03:20:47 -07:00
|
|
|
PRUint32 curTime = static_cast<PRUint32>(PR_Now() / PR_USEC_PER_SEC);
|
2010-04-16 10:37:16 -07:00
|
|
|
PRUint32 lastTime = curTime - mLastHandledActivity;
|
|
|
|
|
|
|
|
// Get the idle time (in seconds).
|
|
|
|
PRUint32 idleTime;
|
|
|
|
if (NS_FAILED(GetIdleTime(&idleTime))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdleTime returns the time in ms, internally we only calculate in s.
|
|
|
|
idleTime /= 1000;
|
|
|
|
|
|
|
|
// We need a text string to send with any state change events.
|
|
|
|
nsAutoString timeStr;
|
|
|
|
timeStr.AppendInt(idleTime);
|
|
|
|
|
|
|
|
// Set the time for last user activity.
|
|
|
|
mLastHandledActivity = curTime - idleTime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now, if the idle time, is less than what we expect, it means the
|
|
|
|
* user was active since last time that we checked.
|
|
|
|
*/
|
|
|
|
nsCOMArray<nsIObserver> notifyList;
|
|
|
|
|
|
|
|
if (lastTime > idleTime) {
|
|
|
|
// Loop trough all listeners, and find any that have detected idle.
|
2009-01-31 20:58:32 -08:00
|
|
|
for (PRUint32 i = 0; i < mArrayListeners.Length(); i++) {
|
2010-04-16 10:37:16 -07:00
|
|
|
IdleListener& curListener = mArrayListeners.ElementAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
if (curListener.isIdle) {
|
|
|
|
notifyList.AppendObject(curListener.observer);
|
|
|
|
curListener.isIdle = false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// Send the "non-idle" events.
|
|
|
|
for (PRInt32 i = 0; i < notifyList.Count(); i++) {
|
|
|
|
notifyList[i]->Observe(this, OBSERVER_TOPIC_BACK, timeStr.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now we need to check for listeners that have expired, and while we are
|
|
|
|
* looping through all the elements, we will also calculate when, if ever
|
|
|
|
* the next one will need to be notified.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Clean up the list, so it's ready for the next iteration.
|
|
|
|
notifyList.Clear();
|
|
|
|
|
|
|
|
// Bail out if we don't need to calculate new times.
|
|
|
|
if (aNoTimeReset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Placet to store the wait time to the next notification, note that
|
|
|
|
* PR_UINT32_MAX means no-one are listening (or that they have such a big
|
|
|
|
* delay that it doesn't matter).
|
|
|
|
*/
|
|
|
|
PRUint32 nextWaitTime = PR_UINT32_MAX;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place to remember if there are any listeners that are in the idle state,
|
|
|
|
* if there are, we need to poll frequently in a polling environment to detect
|
|
|
|
* when the user becomes active again.
|
|
|
|
*/
|
|
|
|
bool anyOneIdle = false;
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (curListener.reqIdleTime <= idleTime) {
|
|
|
|
// then add the listener to the list of listeners that should be
|
|
|
|
// notified.
|
|
|
|
notifyList.AppendObject(curListener.observer);
|
|
|
|
// This listener is now idle.
|
|
|
|
curListener.isIdle = true;
|
|
|
|
} else {
|
|
|
|
// If it hasn't expired yet, then we should note the time when it should
|
|
|
|
// expire.
|
|
|
|
nextWaitTime = PR_MIN(nextWaitTime, curListener.reqIdleTime);
|
|
|
|
}
|
2009-01-31 20:58:34 -08:00
|
|
|
}
|
|
|
|
|
2010-04-16 10:37:16 -07:00
|
|
|
// Remember if anyone becomes idle (it's safe to do this as a binary compare
|
|
|
|
// as we are or'ing).
|
|
|
|
anyOneIdle |= curListener.isIdle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to find when the next idle event should time out, we need to
|
|
|
|
// subtract the time we should wait, from the time that has already passed.
|
|
|
|
nextWaitTime -= idleTime;
|
|
|
|
|
|
|
|
// Notify all listeners that just timed out.
|
|
|
|
for (PRInt32 i = 0; i < notifyList.Count(); i++) {
|
|
|
|
notifyList[i]->Observe(this, OBSERVER_TOPIC_IDLE, timeStr.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are in poll mode, we need to poll for activity if anyone are idle,
|
|
|
|
// otherwise we can wait polling until they would expire.
|
|
|
|
if (UsePollMode() &&
|
|
|
|
anyOneIdle &&
|
|
|
|
nextWaitTime > MIN_IDLE_POLL_INTERVAL) {
|
|
|
|
nextWaitTime = MIN_IDLE_POLL_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the timer if there is anything to wait for.
|
|
|
|
if (PR_UINT32_MAX != nextWaitTime) {
|
|
|
|
StartTimer(nextWaitTime);
|
|
|
|
}
|
2009-01-31 20:58:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIdleService::StartTimer(PRUint32 aDelay)
|
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
if (mTimer) {
|
|
|
|
StopTimer();
|
|
|
|
if (aDelay) {
|
|
|
|
mTimer->InitWithFuncCallback(IdleTimerCallback, this, aDelay*1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2009-01-31 20:58:32 -08:00
|
|
|
}
|
2010-04-16 10:37:16 -07:00
|
|
|
}
|
2009-01-31 20:58:32 -08:00
|
|
|
}
|
2009-01-31 20:58:34 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
nsIdleService::StopTimer()
|
|
|
|
{
|
2010-04-16 10:37:16 -07:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2009-01-31 20:58:34 -08:00
|
|
|
}
|
2009-10-06 11:47:46 -07:00
|
|
|
|