widget/xpwidgets/nsIdleService.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/xpwidgets/nsIdleService.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,225 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:expandtab:shiftwidth=2:tabstop=2:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#ifndef nsIdleService_h__
    1.12 +#define nsIdleService_h__
    1.13 +
    1.14 +#include "nsIIdleServiceInternal.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsITimer.h"
    1.17 +#include "nsTArray.h"
    1.18 +#include "nsIObserver.h"
    1.19 +#include "nsIIdleService.h"
    1.20 +#include "nsCategoryCache.h"
    1.21 +#include "nsWeakReference.h"
    1.22 +#include "mozilla/TimeStamp.h"
    1.23 +
    1.24 +/**
    1.25 + * Class we can use to store an observer with its associated idle time
    1.26 + * requirement and whether or not the observer thinks it's "idle".
    1.27 + */
    1.28 +class IdleListener {
    1.29 +public:
    1.30 +  nsCOMPtr<nsIObserver> observer;
    1.31 +  uint32_t reqIdleTime;
    1.32 +  bool isIdle;
    1.33 +
    1.34 +  IdleListener(nsIObserver* obs, uint32_t reqIT, bool aIsIdle = false) :
    1.35 +    observer(obs), reqIdleTime(reqIT), isIdle(aIsIdle) {}
    1.36 +  ~IdleListener() {}
    1.37 +};
    1.38 +
    1.39 +// This one will be declared later.
    1.40 +class nsIdleService;
    1.41 +
    1.42 +/**
    1.43 + * Class to handle the daily idle timer.
    1.44 + */
    1.45 +class nsIdleServiceDaily : public nsIObserver,
    1.46 +                           public nsSupportsWeakReference
    1.47 +{
    1.48 +public:
    1.49 +  NS_DECL_ISUPPORTS
    1.50 +  NS_DECL_NSIOBSERVER
    1.51 +
    1.52 +  nsIdleServiceDaily(nsIIdleService* aIdleService);
    1.53 +
    1.54 +  /**
    1.55 +   * Initializes the daily idle observer.
    1.56 +   * Keep this separated from the constructor, since it could cause pointer
    1.57 +   * corruption due to AddRef/Release of "this".
    1.58 +   */
    1.59 +  void Init();
    1.60 +
    1.61 +  virtual ~nsIdleServiceDaily();
    1.62 +
    1.63 +private:
    1.64 +  /**
    1.65 +   * StageIdleDaily is the interim call made when an idle-daily event is due.
    1.66 +   * However we don't want to fire idle-daily until the user is idle for this
    1.67 +   * session, so this sets up a short wait for an idle event which triggers
    1.68 +   * the actual idle-daily event.
    1.69 +   *
    1.70 +   * @param aHasBeenLongWait Pass true indicating nsIdleServiceDaily is having
    1.71 +   * trouble getting the idle-daily event fired. If true StageIdleDaily will
    1.72 +   * use a shorter idle wait time before firing idle-daily.
    1.73 +   */
    1.74 +  void StageIdleDaily(bool aHasBeenLongWait);
    1.75 +
    1.76 +  /**
    1.77 +   * @note This is a normal pointer, part to avoid creating a cycle with the
    1.78 +   * idle service, part to avoid potential pointer corruption due to this class
    1.79 +   * being instantiated in the constructor of the service itself.
    1.80 +   */
    1.81 +  nsIIdleService* mIdleService;
    1.82 +
    1.83 +  /**
    1.84 +   * Place to hold the timer used by this class to determine when a day has
    1.85 +   * passed, after that it will wait for idle time to be detected.
    1.86 +   */
    1.87 +  nsCOMPtr<nsITimer> mTimer;
    1.88 +
    1.89 +  /**
    1.90 +   * Function that is called back once a day.
    1.91 +   */
    1.92 +  static void DailyCallback(nsITimer* aTimer, void* aClosure);
    1.93 +
    1.94 +  /**
    1.95 +   * Cache of observers for the "idle-daily" category.
    1.96 +   */
    1.97 +  nsCategoryCache<nsIObserver> mCategoryObservers;
    1.98 +
    1.99 +  /**
   1.100 +   * Boolean set to true when daily idle notifications should be disabled.
   1.101 +   */
   1.102 +  bool mShutdownInProgress;
   1.103 +
   1.104 +  /**
   1.105 +   * Next time we expect an idle-daily timer to fire, in case timers aren't
   1.106 +   * very reliable on the platform. Value is in PR_Now microsecond units.
   1.107 +   */
   1.108 +  PRTime mExpectedTriggerTime;
   1.109 +
   1.110 +  /**
   1.111 +   * Tracks which idle daily observer callback we ask for. There are two: a
   1.112 +   * regular long idle wait and a shorter wait if we've been waiting to fire
   1.113 +   * idle daily for an extended period. Set by StageIdleDaily.
   1.114 +   */
   1.115 +  int32_t mIdleDailyTriggerWait;
   1.116 +};
   1.117 +
   1.118 +class nsIdleService : public nsIIdleServiceInternal
   1.119 +{
   1.120 +public:
   1.121 +  NS_DECL_ISUPPORTS
   1.122 +  NS_DECL_NSIIDLESERVICE
   1.123 +  NS_DECL_NSIIDLESERVICEINTERNAL
   1.124 +
   1.125 +protected:
   1.126 +  static already_AddRefed<nsIdleService> GetInstance();
   1.127 +
   1.128 +  nsIdleService();
   1.129 +  virtual ~nsIdleService();
   1.130 +
   1.131 +  /**
   1.132 +   * If there is a platform specific function to poll the system idel time
   1.133 +   * then that must be returned in this function, and the function MUST return
   1.134 +   * true, otherwise then the function should be left unimplemented or made
   1.135 +   * to return false (this can also be used for systems where it depends on
   1.136 +   * the configuration of the system if the idle time can be determined)
   1.137 +   *
   1.138 +   * @param aIdleTime
   1.139 +   *        The idle time in ms.
   1.140 +   *
   1.141 +   * @return true if the idle time could be polled, false otherwise.
   1.142 +   *
   1.143 +   * @note The time returned by this function can be different than the one
   1.144 +   *       returned by GetIdleTime, as that is corrected by any calls to
   1.145 +   *       ResetIdleTimeOut(), unless you overwrite that function too...
   1.146 +   */
   1.147 +  virtual bool PollIdleTime(uint32_t* aIdleTime);
   1.148 +
   1.149 +  /**
   1.150 +   * Function that determines if we are in poll mode or not.
   1.151 +   *
   1.152 +   * @return true if polling is supported, false otherwise.
   1.153 +   */
   1.154 +  virtual bool UsePollMode();
   1.155 +
   1.156 +private:
   1.157 +  /**
   1.158 +   * Ensure that the timer is expiring at least at the given time
   1.159 +   *
   1.160 +   * The function might not restart the timer if there is one running currently
   1.161 +   *
   1.162 +   * @param aNextTimeout
   1.163 +   *        The last absolute time the timer should expire
   1.164 +   */
   1.165 +  void SetTimerExpiryIfBefore(mozilla::TimeStamp aNextTimeout);
   1.166 +
   1.167 +  /**
   1.168 +   * Stores the next timeout time, 0 means timer not running
   1.169 +   */
   1.170 +  mozilla::TimeStamp mCurrentlySetToTimeoutAt;
   1.171 +
   1.172 +  /**
   1.173 +   * mTimer holds the internal timer used by this class to detect when to poll
   1.174 +   * for idle time, when to check if idle timers should expire etc.
   1.175 +   */
   1.176 +  nsCOMPtr<nsITimer> mTimer;
   1.177 +
   1.178 +  /**
   1.179 +   * Array of listeners that wants to be notified about idle time.
   1.180 +   */
   1.181 +  nsTArray<IdleListener> mArrayListeners;
   1.182 +
   1.183 +  /**
   1.184 +   * Object keeping track of the daily idle thingy.
   1.185 +   */
   1.186 +  nsRefPtr<nsIdleServiceDaily> mDailyIdle;
   1.187 +
   1.188 +  /**
   1.189 +   * Number of observers currently in idle mode.
   1.190 +   */
   1.191 +  uint32_t mIdleObserverCount;
   1.192 +
   1.193 +  /**
   1.194 +   * Delta time from last non idle time to when the next observer should switch
   1.195 +   * to idle mode
   1.196 +   *
   1.197 +   * Time in seconds
   1.198 +   *
   1.199 +   * If this value is 0 it means there are no active observers
   1.200 +   */
   1.201 +  uint32_t mDeltaToNextIdleSwitchInS;
   1.202 +
   1.203 +  /**
   1.204 +   * Absolute value for when the last user interaction took place.
   1.205 +   */
   1.206 +  mozilla::TimeStamp mLastUserInteraction;
   1.207 +
   1.208 +
   1.209 +  /**
   1.210 +   * Function that ensures the timer is running with at least the minimum time
   1.211 +   * needed.  It will kill the timer if there are no active observers.
   1.212 +   */
   1.213 +  void ReconfigureTimer(void);
   1.214 +
   1.215 +  /**
   1.216 +   * Callback function that is called when the internal timer expires.
   1.217 +   *
   1.218 +   * This in turn calls the IdleTimerCallback that does the real processing
   1.219 +   */
   1.220 +  static void StaticIdleTimerCallback(nsITimer* aTimer, void* aClosure);
   1.221 +
   1.222 +  /**
   1.223 +   * Function that handles when a timer has expired
   1.224 +   */
   1.225 +  void IdleTimerCallback(void);
   1.226 +};
   1.227 +
   1.228 +#endif // nsIdleService_h__

mercurial