diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-28 20:50:12 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-28 20:50:12 +0000 |
commit | aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6 (patch) | |
tree | a63f2d36e86361d5c27122a6d6ef4098b755d7d9 /base/idle_timer.cc | |
parent | e115558691eb08608fad56bb32f40265fdfa4ac5 (diff) | |
download | chromium_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.cc')
-rw-r--r-- | base/idle_timer.cc | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/base/idle_timer.cc b/base/idle_timer.cc index 39b9bd4..ad29b2c 100644 --- a/base/idle_timer.cc +++ b/base/idle_timer.cc @@ -2,35 +2,34 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <windows.h> - #include "base/idle_timer.h" #include "base/message_loop.h" #include "base/time.h" -IdleTimerTask::IdleTimerTask(TimeDelta idle_time, bool repeat) - : idle_interval_(idle_time), - repeat_(repeat), - get_last_input_info_fn_(GetLastInputInfo) { +namespace base { + +IdleTimer::IdleTimer(TimeDelta idle_time, bool repeat) + : idle_interval_(idle_time), + repeat_(repeat), + get_last_input_info_fn_(GetLastInputInfo) { DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()) << "Requires a thread that processes Windows UI events"; } -IdleTimerTask::~IdleTimerTask() { +IdleTimer::~IdleTimer() { Stop(); } -void IdleTimerTask::Start() { - DCHECK(!timer_.get()); +void IdleTimer::Start() { StartTimer(); } -void IdleTimerTask::Stop() { - timer_.reset(); +void IdleTimer::Stop() { + timer_.Stop(); } -void IdleTimerTask::Run() { +void IdleTimer::Run() { // Verify we can fire the idle timer. if (TimeUntilIdle().InMilliseconds() <= 0) { OnIdle(); @@ -40,17 +39,15 @@ void IdleTimerTask::Run() { StartTimer(); // Restart the timer for next run. } -void IdleTimerTask::StartTimer() { - DCHECK(timer_ == NULL); +void IdleTimer::StartTimer() { + DCHECK(!timer_.IsRunning()); TimeDelta delay = TimeUntilIdle(); if (delay.InMilliseconds() < 0) delay = TimeDelta(); - timer_.reset(new OneShotTimer(delay)); - timer_->set_unowned_task(this); - timer_->Start(); + timer_.Start(delay, this, &IdleTimer::Run); } -TimeDelta IdleTimerTask::CurrentIdleTime() { +TimeDelta IdleTimer::CurrentIdleTime() { // TODO(mbelshe): This is windows-specific code. LASTINPUTINFO info; info.cbSize = sizeof(info); @@ -68,7 +65,7 @@ TimeDelta IdleTimerTask::CurrentIdleTime() { return TimeDelta::FromMilliseconds(0); } -TimeDelta IdleTimerTask::TimeUntilIdle() { +TimeDelta IdleTimer::TimeUntilIdle() { TimeDelta time_since_last_fire = Time::Now() - last_time_fired_; TimeDelta current_idle_time = CurrentIdleTime(); if (current_idle_time > time_since_last_fire) { @@ -79,3 +76,4 @@ TimeDelta IdleTimerTask::TimeUntilIdle() { return idle_interval_ - current_idle_time; } +} // namespace base |