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
|
|
|
|
|
|
|
#ifndef nsIdleService_h__
|
|
|
|
#define nsIdleService_h__
|
|
|
|
|
2012-06-29 12:32:21 +04:00
|
|
|
#include "nsIIdleServiceInternal.h"
|
2007-01-08 21:13:16 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsIObserver.h"
|
2010-04-16 21:37:16 +04:00
|
|
|
#include "nsIIdleService.h"
|
2010-10-08 14:20:50 +04:00
|
|
|
#include "nsCategoryCache.h"
|
2012-03-07 20:01:24 +04:00
|
|
|
#include "nsWeakReference.h"
|
2012-10-26 06:17:36 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
/**
|
|
|
|
* Class we can use to store an observer with its associated idle time
|
|
|
|
* requirement and whether or not the observer thinks it's "idle".
|
|
|
|
*/
|
2007-01-08 21:13:16 +03:00
|
|
|
class IdleListener {
|
|
|
|
public:
|
2010-04-16 21:37:16 +04:00
|
|
|
nsCOMPtr<nsIObserver> observer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t reqIdleTime;
|
2010-04-16 21:37:16 +04:00
|
|
|
bool isIdle;
|
2007-01-08 21:13:16 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
IdleListener(nsIObserver* obs, uint32_t reqIT, bool aIsIdle = false)
|
2010-04-16 21:37:16 +04:00
|
|
|
: observer(obs), reqIdleTime(reqIT), isIdle(aIsIdle) {}
|
|
|
|
~IdleListener() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// This one will be declared later.
|
|
|
|
class nsIdleService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to handle the daily idle timer.
|
|
|
|
*/
|
2012-03-07 20:01:24 +04:00
|
|
|
class nsIdleServiceDaily : public nsIObserver, public nsSupportsWeakReference {
|
2010-04-16 21:37:16 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2014-09-01 07:33:13 +04:00
|
|
|
explicit nsIdleServiceDaily(nsIIdleService* aIdleService);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
2011-02-24 16:03:59 +03:00
|
|
|
* Initializes the daily idle observer.
|
|
|
|
* Keep this separated from the constructor, since it could cause pointer
|
|
|
|
* corruption due to AddRef/Release of "this".
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2011-02-24 16:03:59 +03:00
|
|
|
void Init();
|
|
|
|
|
2014-06-24 20:36:44 +04:00
|
|
|
private:
|
2011-02-24 16:03:59 +03:00
|
|
|
virtual ~nsIdleServiceDaily();
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2012-10-12 17:02:24 +04:00
|
|
|
/**
|
|
|
|
* StageIdleDaily is the interim call made when an idle-daily event is due.
|
|
|
|
* However we don't want to fire idle-daily until the user is idle for this
|
|
|
|
* session, so this sets up a short wait for an idle event which triggers
|
|
|
|
* the actual idle-daily event.
|
|
|
|
*
|
|
|
|
* @param aHasBeenLongWait Pass true indicating nsIdleServiceDaily is having
|
|
|
|
* trouble getting the idle-daily event fired. If true StageIdleDaily will
|
|
|
|
* use a shorter idle wait time before firing idle-daily.
|
|
|
|
*/
|
|
|
|
void StageIdleDaily(bool aHasBeenLongWait);
|
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
/**
|
2011-02-24 16:03:59 +03:00
|
|
|
* @note This is a normal pointer, part to avoid creating a cycle with the
|
|
|
|
* idle service, part to avoid potential pointer corruption due to this class
|
|
|
|
* being instantiated in the constructor of the service itself.
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2011-02-24 16:03:59 +03:00
|
|
|
nsIIdleService* mIdleService;
|
2010-10-08 14:20:47 +04:00
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
/**
|
|
|
|
* Place to hold the timer used by this class to determine when a day has
|
|
|
|
* passed, after that it will wait for idle time to be detected.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that is called back once a day.
|
|
|
|
*/
|
|
|
|
static void DailyCallback(nsITimer* aTimer, void* aClosure);
|
2010-10-08 14:20:50 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache of observers for the "idle-daily" category.
|
|
|
|
*/
|
|
|
|
nsCategoryCache<nsIObserver> mCategoryObservers;
|
2012-03-07 20:01:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean set to true when daily idle notifications should be disabled.
|
|
|
|
*/
|
|
|
|
bool mShutdownInProgress;
|
2012-07-06 09:31:07 +04:00
|
|
|
|
|
|
|
/**
|
2012-10-12 17:02:24 +04:00
|
|
|
* Next time we expect an idle-daily timer to fire, in case timers aren't
|
|
|
|
* very reliable on the platform. Value is in PR_Now microsecond units.
|
|
|
|
*/
|
|
|
|
PRTime mExpectedTriggerTime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks which idle daily observer callback we ask for. There are two: a
|
|
|
|
* regular long idle wait and a shorter wait if we've been waiting to fire
|
|
|
|
* idle daily for an extended period. Set by StageIdleDaily.
|
2012-07-06 09:31:07 +04:00
|
|
|
*/
|
2012-10-12 17:02:24 +04:00
|
|
|
int32_t mIdleDailyTriggerWait;
|
2007-01-08 21:13:16 +03:00
|
|
|
};
|
|
|
|
|
2012-06-29 12:32:21 +04:00
|
|
|
class nsIdleService : public nsIIdleServiceInternal {
|
2007-01-08 21:13:16 +03:00
|
|
|
public:
|
2012-06-29 12:32:21 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIIDLESERVICE
|
|
|
|
NS_DECL_NSIIDLESERVICEINTERNAL
|
2007-01-08 21:13:16 +03:00
|
|
|
|
|
|
|
protected:
|
2012-06-29 12:32:21 +04:00
|
|
|
static already_AddRefed<nsIdleService> GetInstance();
|
|
|
|
|
|
|
|
nsIdleService();
|
2012-02-29 03:40:38 +04:00
|
|
|
virtual ~nsIdleService();
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is a platform specific function to poll the system idel time
|
|
|
|
* then that must be returned in this function, and the function MUST return
|
|
|
|
* true, otherwise then the function should be left unimplemented or made
|
|
|
|
* to return false (this can also be used for systems where it depends on
|
|
|
|
* the configuration of the system if the idle time can be determined)
|
|
|
|
*
|
|
|
|
* @param aIdleTime
|
|
|
|
* The idle time in ms.
|
|
|
|
*
|
|
|
|
* @return true if the idle time could be polled, false otherwise.
|
|
|
|
*
|
|
|
|
* @note The time returned by this function can be different than the one
|
|
|
|
* returned by GetIdleTime, as that is corrected by any calls to
|
|
|
|
* ResetIdleTimeOut(), unless you overwrite that function too...
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual bool PollIdleTime(uint32_t* aIdleTime);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that determines if we are in poll mode or not.
|
|
|
|
*
|
|
|
|
* @return true if polling is supported, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool UsePollMode();
|
|
|
|
|
2007-01-08 21:13:16 +03:00
|
|
|
private:
|
2010-04-16 21:37:16 +04:00
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Ensure that the timer is expiring at least at the given time
|
2010-04-16 21:37:16 +04:00
|
|
|
*
|
2012-02-29 03:40:38 +04:00
|
|
|
* The function might not restart the timer if there is one running currently
|
|
|
|
*
|
2012-10-26 06:17:36 +04:00
|
|
|
* @param aNextTimeout
|
2012-02-29 03:40:38 +04:00
|
|
|
* The last absolute time the timer should expire
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2012-10-26 06:17:36 +04:00
|
|
|
void SetTimerExpiryIfBefore(mozilla::TimeStamp aNextTimeout);
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Stores the next timeout time, 0 means timer not running
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2012-10-26 06:17:36 +04:00
|
|
|
mozilla::TimeStamp mCurrentlySetToTimeoutAt;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* mTimer holds the internal timer used by this class to detect when to poll
|
|
|
|
* for idle time, when to check if idle timers should expire etc.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of listeners that wants to be notified about idle time.
|
|
|
|
*/
|
|
|
|
nsTArray<IdleListener> mArrayListeners;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object keeping track of the daily idle thingy.
|
|
|
|
*/
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIdleServiceDaily> mDailyIdle;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
2013-02-21 20:29:30 +04:00
|
|
|
* Number of observers currently in idle mode.
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2013-02-21 20:29:30 +04:00
|
|
|
uint32_t mIdleObserverCount;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Delta time from last non idle time to when the next observer should switch
|
|
|
|
* to idle mode
|
|
|
|
*
|
|
|
|
* Time in seconds
|
|
|
|
*
|
|
|
|
* If this value is 0 it means there are no active observers
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mDeltaToNextIdleSwitchInS;
|
2010-04-16 21:37:16 +04:00
|
|
|
|
2019-01-25 04:27:24 +03:00
|
|
|
/**
|
|
|
|
* If true, the idle service is temporarily disabled, and all idle events
|
|
|
|
* will be ignored.
|
|
|
|
*/
|
|
|
|
bool mDisabled = false;
|
|
|
|
|
2010-04-16 21:37:16 +04:00
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Absolute value for when the last user interaction took place.
|
2010-04-16 21:37:16 +04:00
|
|
|
*/
|
2012-10-26 06:17:36 +04:00
|
|
|
mozilla::TimeStamp mLastUserInteraction;
|
2010-10-09 22:08:24 +04:00
|
|
|
|
2012-01-11 18:38:16 +04:00
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Function that ensures the timer is running with at least the minimum time
|
|
|
|
* needed. It will kill the timer if there are no active observers.
|
2012-01-11 18:38:16 +04:00
|
|
|
*/
|
2012-02-29 03:40:38 +04:00
|
|
|
void ReconfigureTimer(void);
|
2012-01-11 18:38:16 +04:00
|
|
|
|
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Callback function that is called when the internal timer expires.
|
2012-01-11 18:38:16 +04:00
|
|
|
*
|
2012-02-29 03:40:38 +04:00
|
|
|
* This in turn calls the IdleTimerCallback that does the real processing
|
2012-01-11 18:38:16 +04:00
|
|
|
*/
|
2012-02-29 03:40:38 +04:00
|
|
|
static void StaticIdleTimerCallback(nsITimer* aTimer, void* aClosure);
|
2012-01-11 18:38:16 +04:00
|
|
|
|
|
|
|
/**
|
2012-02-29 03:40:38 +04:00
|
|
|
* Function that handles when a timer has expired
|
2012-01-11 18:38:16 +04:00
|
|
|
*/
|
2012-02-29 03:40:38 +04:00
|
|
|
void IdleTimerCallback(void);
|
2007-01-08 21:13:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsIdleService_h__
|