| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // IdleTimer is a recurring Timer which runs only when the system is idle. | 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 | 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 | 7 // activity for some period of time. Because the timer is user dependant, it |
| 8 // is possible for the timer to never fire. | 8 // is possible for the timer to never fire. |
| 9 // | 9 // |
| 10 // Usage should be for low-priority work, and may look like this: | 10 // Usage should be for low-priority work, and may look like this: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // | 21 // |
| 22 // // As with all Timers, the caller must dispose the object. | 22 // // As with all Timers, the caller must dispose the object. |
| 23 // delete timer; // Will Stop the timer and cleanup. | 23 // delete timer; // Will Stop the timer and cleanup. |
| 24 // | 24 // |
| 25 // NOTE: An IdleTimer can only be used on a thread that processes UI events. | 25 // NOTE: An IdleTimer can only be used on a thread that processes UI events. |
| 26 // Such a thread should be running a MessageLoopForUI. | 26 // Such a thread should be running a MessageLoopForUI. |
| 27 | 27 |
| 28 #ifndef BASE_IDLE_TIMER_H_ | 28 #ifndef BASE_IDLE_TIMER_H_ |
| 29 #define BASE_IDLE_TIMER_H_ | 29 #define BASE_IDLE_TIMER_H_ |
| 30 | 30 |
| 31 #if defined(OS_WIN) |
| 31 #include <windows.h> | 32 #include <windows.h> |
| 33 #endif // OS_WIN |
| 32 | 34 |
| 33 #include "base/basictypes.h" | 35 #include "base/basictypes.h" |
| 34 #include "base/task.h" | 36 #include "base/task.h" |
| 35 #include "base/timer.h" | 37 #include "base/timer.h" |
| 36 | 38 |
| 37 namespace base { | 39 namespace base { |
| 38 | 40 |
| 39 // Function prototype for GetLastInputInfo. | 41 // Function prototype for GetLastInputInfo. |
| 40 typedef BOOL (__stdcall *GetLastInputInfoFunction)(PLASTINPUTINFO plii); | 42 typedef bool (*GetIdleTimeFunction)(int32* milliseconds); |
| 41 | 43 |
| 42 class IdleTimer { | 44 class IdleTimer { |
| 43 public: | 45 public: |
| 44 // Create an IdleTimer. | 46 // Create an IdleTimer. |
| 45 // idle_time: idle time required before this timer can run. | 47 // idle_time: idle time required before this timer can run. |
| 46 // repeat: true if the timer should fire multiple times per idle, | 48 // repeat: true if the timer should fire multiple times per idle, |
| 47 // false to fire once per idle. | 49 // false to fire once per idle. |
| 48 IdleTimer(TimeDelta idle_time, bool repeat); | 50 IdleTimer(TimeDelta idle_time, bool repeat); |
| 49 | 51 |
| 50 // On destruction, the IdleTimer will Stop itself. | 52 // On destruction, the IdleTimer will Stop itself. |
| 51 virtual ~IdleTimer(); | 53 virtual ~IdleTimer(); |
| 52 | 54 |
| 53 // Start the IdleTimer. | 55 // Start the IdleTimer. |
| 54 void Start(); | 56 void Start(); |
| 55 | 57 |
| 56 // Stop the IdleTimer. | 58 // Stop the IdleTimer. |
| 57 void Stop(); | 59 void Stop(); |
| 58 | 60 |
| 59 // The method to run when the timer elapses. | 61 // The method to run when the timer elapses. |
| 60 virtual void OnIdle() = 0; | 62 virtual void OnIdle() = 0; |
| 61 | 63 |
| 62 protected: | 64 protected: |
| 63 // Override the GetLastInputInfo function. | 65 // Override the GetLastInputInfo function. |
| 64 void set_last_input_info_fn(GetLastInputInfoFunction function) { | 66 void set_idle_time_fn(GetIdleTimeFunction function) { |
| 65 get_last_input_info_fn_ = function; | 67 get_idle_time_fn_ = function; |
| 66 } | 68 } |
| 67 | 69 |
| 68 private: | 70 private: |
| 69 // Called when timer_ expires. | 71 // Called when timer_ expires. |
| 70 void Run(); | 72 void Run(); |
| 71 | 73 |
| 72 // Start the timer. | 74 // Start the timer. |
| 73 void StartTimer(); | 75 void StartTimer(); |
| 74 | 76 |
| 75 // Gets the number of milliseconds since the last input event. | 77 // Gets the number of milliseconds since the last input event. |
| 76 TimeDelta CurrentIdleTime(); | 78 TimeDelta CurrentIdleTime(); |
| 77 | 79 |
| 78 // Compute time until idle. Returns 0 if we are now idle. | 80 // Compute time until idle. Returns 0 if we are now idle. |
| 79 TimeDelta TimeUntilIdle(); | 81 TimeDelta TimeUntilIdle(); |
| 80 | 82 |
| 83 #if defined(OS_LINUX) |
| 84 struct XssInfoLazyInstanceTraits { |
| 85 static void New(void* instance); |
| 86 static void Delete(void* instance); |
| 87 }; |
| 88 struct XssInfo { |
| 89 bool have_xss; |
| 90 ThreadLocalPointer<XScreenSaverInfo> xss_info; |
| 91 }; |
| 92 #endif // OS_LINUX |
| 93 |
| 94 static bool DefaultIdleTimeFunction(int32* milliseconds); |
| 95 |
| 81 TimeDelta idle_interval_; | 96 TimeDelta idle_interval_; |
| 82 bool repeat_; | 97 bool repeat_; |
| 83 Time last_time_fired_; // The last time the idle timer fired. | 98 Time last_time_fired_; // The last time the idle timer fired. |
| 84 // will be 0 until the timer fires the first time. | 99 // will be 0 until the timer fires the first time. |
| 85 OneShotTimer<IdleTimer> timer_; | 100 OneShotTimer<IdleTimer> timer_; |
| 86 | 101 |
| 87 GetLastInputInfoFunction get_last_input_info_fn_; | 102 GetIdleTimeFunction get_idle_time_fn_; |
| 88 | 103 |
| 89 DISALLOW_COPY_AND_ASSIGN(IdleTimer); | 104 DISALLOW_COPY_AND_ASSIGN(IdleTimer); |
| 90 }; | 105 }; |
| 91 | 106 |
| 92 } // namespace base | 107 } // namespace base |
| 93 | 108 |
| 94 #endif // BASE_IDLE_TIMER_H_ | 109 #endif // BASE_IDLE_TIMER_H_ |
| OLD | NEW |