Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:expandtab:shiftwidth=2:tabstop=2: |
michael@0 | 3 | */ |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef nsIdleService_h__ |
michael@0 | 9 | #define nsIdleService_h__ |
michael@0 | 10 | |
michael@0 | 11 | #include "nsIIdleServiceInternal.h" |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nsITimer.h" |
michael@0 | 14 | #include "nsTArray.h" |
michael@0 | 15 | #include "nsIObserver.h" |
michael@0 | 16 | #include "nsIIdleService.h" |
michael@0 | 17 | #include "nsCategoryCache.h" |
michael@0 | 18 | #include "nsWeakReference.h" |
michael@0 | 19 | #include "mozilla/TimeStamp.h" |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * Class we can use to store an observer with its associated idle time |
michael@0 | 23 | * requirement and whether or not the observer thinks it's "idle". |
michael@0 | 24 | */ |
michael@0 | 25 | class IdleListener { |
michael@0 | 26 | public: |
michael@0 | 27 | nsCOMPtr<nsIObserver> observer; |
michael@0 | 28 | uint32_t reqIdleTime; |
michael@0 | 29 | bool isIdle; |
michael@0 | 30 | |
michael@0 | 31 | IdleListener(nsIObserver* obs, uint32_t reqIT, bool aIsIdle = false) : |
michael@0 | 32 | observer(obs), reqIdleTime(reqIT), isIdle(aIsIdle) {} |
michael@0 | 33 | ~IdleListener() {} |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | // This one will be declared later. |
michael@0 | 37 | class nsIdleService; |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * Class to handle the daily idle timer. |
michael@0 | 41 | */ |
michael@0 | 42 | class nsIdleServiceDaily : public nsIObserver, |
michael@0 | 43 | public nsSupportsWeakReference |
michael@0 | 44 | { |
michael@0 | 45 | public: |
michael@0 | 46 | NS_DECL_ISUPPORTS |
michael@0 | 47 | NS_DECL_NSIOBSERVER |
michael@0 | 48 | |
michael@0 | 49 | nsIdleServiceDaily(nsIIdleService* aIdleService); |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * Initializes the daily idle observer. |
michael@0 | 53 | * Keep this separated from the constructor, since it could cause pointer |
michael@0 | 54 | * corruption due to AddRef/Release of "this". |
michael@0 | 55 | */ |
michael@0 | 56 | void Init(); |
michael@0 | 57 | |
michael@0 | 58 | virtual ~nsIdleServiceDaily(); |
michael@0 | 59 | |
michael@0 | 60 | private: |
michael@0 | 61 | /** |
michael@0 | 62 | * StageIdleDaily is the interim call made when an idle-daily event is due. |
michael@0 | 63 | * However we don't want to fire idle-daily until the user is idle for this |
michael@0 | 64 | * session, so this sets up a short wait for an idle event which triggers |
michael@0 | 65 | * the actual idle-daily event. |
michael@0 | 66 | * |
michael@0 | 67 | * @param aHasBeenLongWait Pass true indicating nsIdleServiceDaily is having |
michael@0 | 68 | * trouble getting the idle-daily event fired. If true StageIdleDaily will |
michael@0 | 69 | * use a shorter idle wait time before firing idle-daily. |
michael@0 | 70 | */ |
michael@0 | 71 | void StageIdleDaily(bool aHasBeenLongWait); |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * @note This is a normal pointer, part to avoid creating a cycle with the |
michael@0 | 75 | * idle service, part to avoid potential pointer corruption due to this class |
michael@0 | 76 | * being instantiated in the constructor of the service itself. |
michael@0 | 77 | */ |
michael@0 | 78 | nsIIdleService* mIdleService; |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * Place to hold the timer used by this class to determine when a day has |
michael@0 | 82 | * passed, after that it will wait for idle time to be detected. |
michael@0 | 83 | */ |
michael@0 | 84 | nsCOMPtr<nsITimer> mTimer; |
michael@0 | 85 | |
michael@0 | 86 | /** |
michael@0 | 87 | * Function that is called back once a day. |
michael@0 | 88 | */ |
michael@0 | 89 | static void DailyCallback(nsITimer* aTimer, void* aClosure); |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * Cache of observers for the "idle-daily" category. |
michael@0 | 93 | */ |
michael@0 | 94 | nsCategoryCache<nsIObserver> mCategoryObservers; |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Boolean set to true when daily idle notifications should be disabled. |
michael@0 | 98 | */ |
michael@0 | 99 | bool mShutdownInProgress; |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Next time we expect an idle-daily timer to fire, in case timers aren't |
michael@0 | 103 | * very reliable on the platform. Value is in PR_Now microsecond units. |
michael@0 | 104 | */ |
michael@0 | 105 | PRTime mExpectedTriggerTime; |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Tracks which idle daily observer callback we ask for. There are two: a |
michael@0 | 109 | * regular long idle wait and a shorter wait if we've been waiting to fire |
michael@0 | 110 | * idle daily for an extended period. Set by StageIdleDaily. |
michael@0 | 111 | */ |
michael@0 | 112 | int32_t mIdleDailyTriggerWait; |
michael@0 | 113 | }; |
michael@0 | 114 | |
michael@0 | 115 | class nsIdleService : public nsIIdleServiceInternal |
michael@0 | 116 | { |
michael@0 | 117 | public: |
michael@0 | 118 | NS_DECL_ISUPPORTS |
michael@0 | 119 | NS_DECL_NSIIDLESERVICE |
michael@0 | 120 | NS_DECL_NSIIDLESERVICEINTERNAL |
michael@0 | 121 | |
michael@0 | 122 | protected: |
michael@0 | 123 | static already_AddRefed<nsIdleService> GetInstance(); |
michael@0 | 124 | |
michael@0 | 125 | nsIdleService(); |
michael@0 | 126 | virtual ~nsIdleService(); |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * If there is a platform specific function to poll the system idel time |
michael@0 | 130 | * then that must be returned in this function, and the function MUST return |
michael@0 | 131 | * true, otherwise then the function should be left unimplemented or made |
michael@0 | 132 | * to return false (this can also be used for systems where it depends on |
michael@0 | 133 | * the configuration of the system if the idle time can be determined) |
michael@0 | 134 | * |
michael@0 | 135 | * @param aIdleTime |
michael@0 | 136 | * The idle time in ms. |
michael@0 | 137 | * |
michael@0 | 138 | * @return true if the idle time could be polled, false otherwise. |
michael@0 | 139 | * |
michael@0 | 140 | * @note The time returned by this function can be different than the one |
michael@0 | 141 | * returned by GetIdleTime, as that is corrected by any calls to |
michael@0 | 142 | * ResetIdleTimeOut(), unless you overwrite that function too... |
michael@0 | 143 | */ |
michael@0 | 144 | virtual bool PollIdleTime(uint32_t* aIdleTime); |
michael@0 | 145 | |
michael@0 | 146 | /** |
michael@0 | 147 | * Function that determines if we are in poll mode or not. |
michael@0 | 148 | * |
michael@0 | 149 | * @return true if polling is supported, false otherwise. |
michael@0 | 150 | */ |
michael@0 | 151 | virtual bool UsePollMode(); |
michael@0 | 152 | |
michael@0 | 153 | private: |
michael@0 | 154 | /** |
michael@0 | 155 | * Ensure that the timer is expiring at least at the given time |
michael@0 | 156 | * |
michael@0 | 157 | * The function might not restart the timer if there is one running currently |
michael@0 | 158 | * |
michael@0 | 159 | * @param aNextTimeout |
michael@0 | 160 | * The last absolute time the timer should expire |
michael@0 | 161 | */ |
michael@0 | 162 | void SetTimerExpiryIfBefore(mozilla::TimeStamp aNextTimeout); |
michael@0 | 163 | |
michael@0 | 164 | /** |
michael@0 | 165 | * Stores the next timeout time, 0 means timer not running |
michael@0 | 166 | */ |
michael@0 | 167 | mozilla::TimeStamp mCurrentlySetToTimeoutAt; |
michael@0 | 168 | |
michael@0 | 169 | /** |
michael@0 | 170 | * mTimer holds the internal timer used by this class to detect when to poll |
michael@0 | 171 | * for idle time, when to check if idle timers should expire etc. |
michael@0 | 172 | */ |
michael@0 | 173 | nsCOMPtr<nsITimer> mTimer; |
michael@0 | 174 | |
michael@0 | 175 | /** |
michael@0 | 176 | * Array of listeners that wants to be notified about idle time. |
michael@0 | 177 | */ |
michael@0 | 178 | nsTArray<IdleListener> mArrayListeners; |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Object keeping track of the daily idle thingy. |
michael@0 | 182 | */ |
michael@0 | 183 | nsRefPtr<nsIdleServiceDaily> mDailyIdle; |
michael@0 | 184 | |
michael@0 | 185 | /** |
michael@0 | 186 | * Number of observers currently in idle mode. |
michael@0 | 187 | */ |
michael@0 | 188 | uint32_t mIdleObserverCount; |
michael@0 | 189 | |
michael@0 | 190 | /** |
michael@0 | 191 | * Delta time from last non idle time to when the next observer should switch |
michael@0 | 192 | * to idle mode |
michael@0 | 193 | * |
michael@0 | 194 | * Time in seconds |
michael@0 | 195 | * |
michael@0 | 196 | * If this value is 0 it means there are no active observers |
michael@0 | 197 | */ |
michael@0 | 198 | uint32_t mDeltaToNextIdleSwitchInS; |
michael@0 | 199 | |
michael@0 | 200 | /** |
michael@0 | 201 | * Absolute value for when the last user interaction took place. |
michael@0 | 202 | */ |
michael@0 | 203 | mozilla::TimeStamp mLastUserInteraction; |
michael@0 | 204 | |
michael@0 | 205 | |
michael@0 | 206 | /** |
michael@0 | 207 | * Function that ensures the timer is running with at least the minimum time |
michael@0 | 208 | * needed. It will kill the timer if there are no active observers. |
michael@0 | 209 | */ |
michael@0 | 210 | void ReconfigureTimer(void); |
michael@0 | 211 | |
michael@0 | 212 | /** |
michael@0 | 213 | * Callback function that is called when the internal timer expires. |
michael@0 | 214 | * |
michael@0 | 215 | * This in turn calls the IdleTimerCallback that does the real processing |
michael@0 | 216 | */ |
michael@0 | 217 | static void StaticIdleTimerCallback(nsITimer* aTimer, void* aClosure); |
michael@0 | 218 | |
michael@0 | 219 | /** |
michael@0 | 220 | * Function that handles when a timer has expired |
michael@0 | 221 | */ |
michael@0 | 222 | void IdleTimerCallback(void); |
michael@0 | 223 | }; |
michael@0 | 224 | |
michael@0 | 225 | #endif // nsIdleService_h__ |