1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:expandtab:shiftwidth=2:tabstop=2:
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is mozilla.org code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Gijs Kruitbosch <gijskruitbosch@gmail.com>.
21 : * Portions created by the Initial Developer are Copyright (C) 2007
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Gijs Kruitbosch <gijskruitbosch@gmail.com>
26 : * Mike Kristoffersen <mozstuff@mikek.dk>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either the GNU General Public License Version 2 or later (the "GPL"), or
30 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #ifndef nsIdleService_h__
43 : #define nsIdleService_h__
44 :
45 : #include "nsIIdleService.h"
46 : #include "nsCOMPtr.h"
47 : #include "nsITimer.h"
48 : #include "nsTArray.h"
49 : #include "nsIObserver.h"
50 : #include "nsIIdleService.h"
51 : #include "nsCategoryCache.h"
52 : #include "nsWeakReference.h"
53 :
54 : /**
55 : * Class we can use to store an observer with its associated idle time
56 : * requirement and whether or not the observer thinks it's "idle".
57 : */
58 1 : class IdleListener {
59 : public:
60 : nsCOMPtr<nsIObserver> observer;
61 : PRUint32 reqIdleTime;
62 : bool isIdle;
63 :
64 1 : IdleListener(nsIObserver* obs, PRUint32 reqIT, bool aIsIdle = false) :
65 1 : observer(obs), reqIdleTime(reqIT), isIdle(aIsIdle) {}
66 2 : ~IdleListener() {}
67 : };
68 :
69 : // This one will be declared later.
70 : class nsIdleService;
71 :
72 : /**
73 : * Class to handle the daily idle timer.
74 : */
75 : class nsIdleServiceDaily : public nsIObserver,
76 : public nsSupportsWeakReference
77 : {
78 : public:
79 : NS_DECL_ISUPPORTS
80 : NS_DECL_NSIOBSERVER
81 :
82 : nsIdleServiceDaily(nsIIdleService* aIdleService);
83 :
84 : /**
85 : * Initializes the daily idle observer.
86 : * Keep this separated from the constructor, since it could cause pointer
87 : * corruption due to AddRef/Release of "this".
88 : */
89 : void Init();
90 :
91 : virtual ~nsIdleServiceDaily();
92 :
93 : private:
94 : /**
95 : * @note This is a normal pointer, part to avoid creating a cycle with the
96 : * idle service, part to avoid potential pointer corruption due to this class
97 : * being instantiated in the constructor of the service itself.
98 : */
99 : nsIIdleService* mIdleService;
100 :
101 : /**
102 : * Set to true when the instantiated object has a idle observer.
103 : */
104 : bool mObservesIdle;
105 :
106 : /**
107 : * Place to hold the timer used by this class to determine when a day has
108 : * passed, after that it will wait for idle time to be detected.
109 : */
110 : nsCOMPtr<nsITimer> mTimer;
111 :
112 : /**
113 : * Function that is called back once a day.
114 : */
115 : static void DailyCallback(nsITimer* aTimer, void* aClosure);
116 :
117 : /**
118 : * Cache of observers for the "idle-daily" category.
119 : */
120 : nsCategoryCache<nsIObserver> mCategoryObservers;
121 :
122 : /**
123 : * Boolean set to true when daily idle notifications should be disabled.
124 : */
125 : bool mShutdownInProgress;
126 : };
127 :
128 : class nsIdleService : public nsIIdleService
129 : {
130 : public:
131 : nsIdleService();
132 :
133 : // Implement nsIIdleService methods.
134 : NS_IMETHOD AddIdleObserver(nsIObserver* aObserver, PRUint32 aIdleTime);
135 : NS_IMETHOD RemoveIdleObserver(nsIObserver* aObserver, PRUint32 aIdleTime);
136 : NS_IMETHOD GetIdleTime(PRUint32* idleTime);
137 :
138 : /**
139 : * Function that resets the idle time in the service, in other words it
140 : * sets the time for the last user interaction to now, or now-idleDelta
141 : *
142 : * @param idleDelta the time (in milliseconds) since the last user inter
143 : * action
144 : **/
145 : void ResetIdleTimeOut(PRUint32 idleDeltaInMS = 0);
146 :
147 : protected:
148 : virtual ~nsIdleService();
149 :
150 : /**
151 : * If there is a platform specific function to poll the system idel time
152 : * then that must be returned in this function, and the function MUST return
153 : * true, otherwise then the function should be left unimplemented or made
154 : * to return false (this can also be used for systems where it depends on
155 : * the configuration of the system if the idle time can be determined)
156 : *
157 : * @param aIdleTime
158 : * The idle time in ms.
159 : *
160 : * @return true if the idle time could be polled, false otherwise.
161 : *
162 : * @note The time returned by this function can be different than the one
163 : * returned by GetIdleTime, as that is corrected by any calls to
164 : * ResetIdleTimeOut(), unless you overwrite that function too...
165 : */
166 : virtual bool PollIdleTime(PRUint32* aIdleTime);
167 :
168 : /**
169 : * Function that determines if we are in poll mode or not.
170 : *
171 : * @return true if polling is supported, false otherwise.
172 : */
173 : virtual bool UsePollMode();
174 :
175 : private:
176 : /**
177 : * Ensure that the timer is expiring at least at the given time
178 : *
179 : * The function might not restart the timer if there is one running currently
180 : *
181 : * @param aNextTimeoutInPR
182 : * The last absolute time the timer should expire
183 : */
184 : void SetTimerExpiryIfBefore(PRTime aNextTimeoutInPR);
185 :
186 : /**
187 : * Stores the next timeout time, 0 means timer not running
188 : */
189 : PRTime mCurrentlySetToTimeoutAtInPR;
190 :
191 : /**
192 : * mTimer holds the internal timer used by this class to detect when to poll
193 : * for idle time, when to check if idle timers should expire etc.
194 : */
195 : nsCOMPtr<nsITimer> mTimer;
196 :
197 : /**
198 : * Array of listeners that wants to be notified about idle time.
199 : */
200 : nsTArray<IdleListener> mArrayListeners;
201 :
202 : /**
203 : * Object keeping track of the daily idle thingy.
204 : */
205 : nsRefPtr<nsIdleServiceDaily> mDailyIdle;
206 :
207 : /**
208 : * Boolean indicating if any observers are in idle mode
209 : */
210 : bool mAnyObserverIdle;
211 :
212 : /**
213 : * Delta time from last non idle time to when the next observer should switch
214 : * to idle mode
215 : *
216 : * Time in seconds
217 : *
218 : * If this value is 0 it means there are no active observers
219 : */
220 : PRUint32 mDeltaToNextIdleSwitchInS;
221 :
222 : /**
223 : * Absolute value for when the last user interaction took place.
224 : */
225 : PRTime mLastUserInteractionInPR;
226 :
227 :
228 : /**
229 : * Function that ensures the timer is running with at least the minimum time
230 : * needed. It will kill the timer if there are no active observers.
231 : */
232 : void ReconfigureTimer(void);
233 :
234 : /**
235 : * Callback function that is called when the internal timer expires.
236 : *
237 : * This in turn calls the IdleTimerCallback that does the real processing
238 : */
239 : static void StaticIdleTimerCallback(nsITimer* aTimer, void* aClosure);
240 :
241 : /**
242 : * Function that handles when a timer has expired
243 : */
244 : void IdleTimerCallback(void);
245 : };
246 :
247 : #endif // nsIdleService_h__
|