blob: ec3d628d76bf67f8444dceadefe671418c556841 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#ifndef BASE_IDLE_TIMER_H__
6#define BASE_IDLE_TIMER_H__
7
8#include <windows.h>
9
10#include "base/basictypes.h"
11#include "base/task.h"
12#include "base/timer.h"
13
14// IdleTimer is a recurring Timer task which runs only when the system is idle.
15// System Idle time is defined as not having any user keyboard or mouse
16// activity for some period of time. Because the timer is user dependant, it
17// is possible for the timer to never fire.
18
19//
20// Usage should be for low-priority tasks, and may look like this:
21//
22// class MyIdleTimerTask : public IdleTimerTask {
23// public:
24// // This task will run after 5 seconds of idle time
25// // and not more often than once per minute.
26// MyIdleTimerTask() : IdleTimerTask(5, 60) {};
27// virtual void OnIdle() { do something };
28// }
29//
30// MyIdleTimerTask *task = new MyIdleTimerTask();
31// task->Start();
32//
33// // As with all TimerTasks, the caller must dispose the object.
34// delete task; // Will Stop the timer and cleanup the task.
35
36class TimerManager;
37
38// Function prototype for GetLastInputInfo.
39typedef BOOL (__stdcall *GetLastInputInfoFunction)(PLASTINPUTINFO plii);
40
41class IdleTimerTask : public Task {
42 public:
43 // Create an IdleTimerTask.
44 // idle_time: idle time required before this task can run.
45 // repeat: true if the timer should fire multiple times per idle,
46 // false to fire once per idle.
47 IdleTimerTask(TimeDelta idle_time, bool repeat);
48
49 // On destruction, the IdleTimerTask will Stop itself and delete the Task.
50 virtual ~IdleTimerTask();
51
52 // Start the IdleTimerTask.
53 void Start();
54
55 // Stop the IdleTimertask.
56 void Stop();
57
58 // The method to run when the timer elapses.
59 virtual void OnIdle() = 0;
60
61 protected:
62 // Override the GetLastInputInfo function.
63 void set_last_input_info_fn(GetLastInputInfoFunction function) {
64 get_last_input_info_fn_ = function;
65 }
66
67 private:
68 // This task's run method.
69 virtual void Run();
70
71 // Start the timer.
72 void StartTimer();
73
74 // Gets the number of milliseconds since the last input event.
75 TimeDelta CurrentIdleTime();
76
77 // Compute time until idle. Returns 0 if we are now idle.
78 TimeDelta TimeUntilIdle();
79
80 TimeDelta idle_interval_;
81 bool repeat_;
82 Time last_time_fired_; // The last time the idle timer fired.
83 // will be 0 until the timer fires the first time.
84 scoped_ptr<OneShotTimer> timer_;
85
86 GetLastInputInfoFunction get_last_input_info_fn_;
87};
88
89#endif // BASE_IDLE_TIMER_H__
license.botf003cfe2008-08-24 09:55:55 +090090