|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 // IdleTimer is a recurring Timer which runs only when the system is idle. |
|
6 // System Idle time is defined as not having any user keyboard or mouse |
|
7 // activity for some period of time. Because the timer is user dependant, it |
|
8 // is possible for the timer to never fire. |
|
9 // |
|
10 // Usage should be for low-priority work, and may look like this: |
|
11 // |
|
12 // class MyIdleTimer : public IdleTimer { |
|
13 // public: |
|
14 // // This timer will run repeatedly after 5 seconds of idle time |
|
15 // MyIdleTimer() : IdleTimer(TimeDelta::FromSeconds(5), true) {}; |
|
16 // virtual void OnIdle() { do something }; |
|
17 // } |
|
18 // |
|
19 // MyIdleTimer *timer = new MyIdleTimer(); |
|
20 // timer->Start(); |
|
21 // |
|
22 // // As with all Timers, the caller must dispose the object. |
|
23 // delete timer; // Will Stop the timer and cleanup. |
|
24 // |
|
25 // NOTE: An IdleTimer can only be used on a thread that processes UI events. |
|
26 // Such a thread should be running a MessageLoopForUI. |
|
27 |
|
28 #ifndef BASE_IDLE_TIMER_H_ |
|
29 #define BASE_IDLE_TIMER_H_ |
|
30 |
|
31 #if defined(OS_WIN) |
|
32 #include <windows.h> |
|
33 #endif |
|
34 |
|
35 #include "base/basictypes.h" |
|
36 #include "base/task.h" |
|
37 #include "base/timer.h" |
|
38 |
|
39 namespace base { |
|
40 |
|
41 // Function prototype - Get the number of milliseconds that the user has been |
|
42 // idle. |
|
43 typedef bool (*IdleTimeSource)(int32_t *milliseconds_interval_since_last_event); |
|
44 |
|
45 class IdleTimer { |
|
46 public: |
|
47 // Create an IdleTimer. |
|
48 // idle_time: idle time required before this timer can run. |
|
49 // repeat: true if the timer should fire multiple times per idle, |
|
50 // false to fire once per idle. |
|
51 IdleTimer(TimeDelta idle_time, bool repeat); |
|
52 |
|
53 // On destruction, the IdleTimer will Stop itself. |
|
54 virtual ~IdleTimer(); |
|
55 |
|
56 // Start the IdleTimer. |
|
57 void Start(); |
|
58 |
|
59 // Stop the IdleTimer. |
|
60 void Stop(); |
|
61 |
|
62 // The method to run when the timer elapses. |
|
63 virtual void OnIdle() = 0; |
|
64 |
|
65 protected: |
|
66 // Override the IdleTimeSource. |
|
67 void set_idle_time_source(IdleTimeSource idle_time_source) { |
|
68 idle_time_source_ = idle_time_source; |
|
69 } |
|
70 |
|
71 private: |
|
72 // Called when timer_ expires. |
|
73 void Run(); |
|
74 |
|
75 // Start the timer. |
|
76 void StartTimer(); |
|
77 |
|
78 // Gets the number of milliseconds since the last input event. |
|
79 TimeDelta CurrentIdleTime(); |
|
80 |
|
81 // Compute time until idle. Returns 0 if we are now idle. |
|
82 TimeDelta TimeUntilIdle(); |
|
83 |
|
84 TimeDelta idle_interval_; |
|
85 bool repeat_; |
|
86 Time last_time_fired_; // The last time the idle timer fired. |
|
87 // will be 0 until the timer fires the first time. |
|
88 OneShotTimer<IdleTimer> timer_; |
|
89 |
|
90 IdleTimeSource idle_time_source_; |
|
91 |
|
92 DISALLOW_COPY_AND_ASSIGN(IdleTimer); |
|
93 }; |
|
94 |
|
95 } // namespace base |
|
96 |
|
97 #endif // BASE_IDLE_TIMER_H_ |