summaryrefslogtreecommitdiffstats
path: root/base/idle_timer.h
diff options
context:
space:
mode:
authordarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-28 20:50:12 +0000
committerdarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-28 20:50:12 +0000
commitaeab57ea8560065d6c513fcd46bb43e1bfbfd7a6 (patch)
treea63f2d36e86361d5c27122a6d6ef4098b755d7d9 /base/idle_timer.h
parente115558691eb08608fad56bb32f40265fdfa4ac5 (diff)
downloadchromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.zip
chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.tar.gz
chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.tar.bz2
Simplify OneShotTimer and RepeatingTimer. Fix up all consumers.
Major changes: OneShotTimer and RepeatingTimer become template classes that no longer require a Task or a Timer object. They just use PostDelayedTask. Under the hood that still uses a Timer object. The API is much simpler for consumers as they now no longer need to worry about allocating a Task or managing the lifetime of the object pointer held by the Task. I added some new unit tests to timer_unittest.cc to cover the API. I preserved the old TimerManager / Timer API for now, but I plan to soon kill it. R=brettw BUG=1346553 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1502 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/idle_timer.h')
-rw-r--r--base/idle_timer.h72
1 files changed, 38 insertions, 34 deletions
diff --git a/base/idle_timer.h b/base/idle_timer.h
index ec3d628..7b9446e 100644
--- a/base/idle_timer.h
+++ b/base/idle_timer.h
@@ -2,57 +2,58 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef BASE_IDLE_TIMER_H__
-#define BASE_IDLE_TIMER_H__
-
-#include <windows.h>
-
-#include "base/basictypes.h"
-#include "base/task.h"
-#include "base/timer.h"
-
-// IdleTimer is a recurring Timer task which runs only when the system is idle.
+// IdleTimer is a recurring Timer which runs only when the system is idle.
// System Idle time is defined as not having any user keyboard or mouse
// activity for some period of time. Because the timer is user dependant, it
// is possible for the timer to never fire.
-
//
-// Usage should be for low-priority tasks, and may look like this:
+// Usage should be for low-priority work, and may look like this:
//
-// class MyIdleTimerTask : public IdleTimerTask {
+// class MyIdleTimer : public IdleTimer {
// public:
-// // This task will run after 5 seconds of idle time
-// // and not more often than once per minute.
-// MyIdleTimerTask() : IdleTimerTask(5, 60) {};
+// // This timer will run repeatedly after 5 seconds of idle time
+// MyIdleTimer() : IdleTimer(TimeDelta::FromSeconds(5), true) {};
// virtual void OnIdle() { do something };
// }
//
-// MyIdleTimerTask *task = new MyIdleTimerTask();
-// task->Start();
+// MyIdleTimer *timer = new MyIdleTimer();
+// timer->Start();
+//
+// // As with all Timers, the caller must dispose the object.
+// delete timer; // Will Stop the timer and cleanup.
//
-// // As with all TimerTasks, the caller must dispose the object.
-// delete task; // Will Stop the timer and cleanup the task.
+// NOTE: An IdleTimer can only be used on a thread that processes UI events.
+// Such a thread should be running a MessageLoopForUI.
-class TimerManager;
+#ifndef BASE_IDLE_TIMER_H_
+#define BASE_IDLE_TIMER_H_
+
+#include <windows.h>
+
+#include "base/basictypes.h"
+#include "base/task.h"
+#include "base/timer.h"
+
+namespace base {
// Function prototype for GetLastInputInfo.
typedef BOOL (__stdcall *GetLastInputInfoFunction)(PLASTINPUTINFO plii);
-class IdleTimerTask : public Task {
+class IdleTimer {
public:
- // Create an IdleTimerTask.
- // idle_time: idle time required before this task can run.
+ // Create an IdleTimer.
+ // idle_time: idle time required before this timer can run.
// repeat: true if the timer should fire multiple times per idle,
// false to fire once per idle.
- IdleTimerTask(TimeDelta idle_time, bool repeat);
+ IdleTimer(TimeDelta idle_time, bool repeat);
- // On destruction, the IdleTimerTask will Stop itself and delete the Task.
- virtual ~IdleTimerTask();
+ // On destruction, the IdleTimer will Stop itself.
+ virtual ~IdleTimer();
- // Start the IdleTimerTask.
+ // Start the IdleTimer.
void Start();
- // Stop the IdleTimertask.
+ // Stop the IdleTimer.
void Stop();
// The method to run when the timer elapses.
@@ -61,12 +62,12 @@ class IdleTimerTask : public Task {
protected:
// Override the GetLastInputInfo function.
void set_last_input_info_fn(GetLastInputInfoFunction function) {
- get_last_input_info_fn_ = function;
+ get_last_input_info_fn_ = function;
}
private:
- // This task's run method.
- virtual void Run();
+ // Called when timer_ expires.
+ void Run();
// Start the timer.
void StartTimer();
@@ -81,10 +82,13 @@ class IdleTimerTask : public Task {
bool repeat_;
Time last_time_fired_; // The last time the idle timer fired.
// will be 0 until the timer fires the first time.
- scoped_ptr<OneShotTimer> timer_;
+ OneShotTimer<IdleTimer> timer_;
GetLastInputInfoFunction get_last_input_info_fn_;
+
+ DISALLOW_COPY_AND_ASSIGN(IdleTimer);
};
-#endif // BASE_IDLE_TIMER_H__
+} // namespace base
+#endif // BASE_IDLE_TIMER_H_