diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-30 21:27:34 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-30 21:27:34 +0000 |
commit | c6c2b472bc6a260975e9fe9ab82e05867f0cac70 (patch) | |
tree | ba08dce285ee587910ef34b7cf21e5d931e81aa6 /base | |
parent | 65055ebfc2f1af07452599ac1117a1bc9b61dd8f (diff) | |
download | chromium_src-c6c2b472bc6a260975e9fe9ab82e05867f0cac70.zip chromium_src-c6c2b472bc6a260975e9fe9ab82e05867f0cac70.tar.gz chromium_src-c6c2b472bc6a260975e9fe9ab82e05867f0cac70.tar.bz2 |
Remove IdleTimer, which no one uses.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/244049
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27658 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gyp | 14 | ||||
-rw-r--r-- | base/idle_timer.cc | 161 | ||||
-rw-r--r-- | base/idle_timer.h | 96 | ||||
-rw-r--r-- | base/idle_timer_none.cc | 25 | ||||
-rw-r--r-- | base/idletimer_unittest.cc | 240 |
5 files changed, 0 insertions, 536 deletions
diff --git a/base/base.gyp b/base/base.gyp index 4e09866..a7860c2 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -147,9 +147,6 @@ 'iat_patch.h', 'icu_util.cc', 'icu_util.h', - 'idle_timer.cc', - 'idle_timer.h', - 'idle_timer_none.cc', 'id_map.h', 'image_util.cc', 'image_util.h', @@ -375,12 +372,6 @@ }, 'sources/': [ ['exclude', '_(mac|win|chromeos)\\.cc$'], ['exclude', '\\.mm?$' ] ], - 'sources!': [ - # Linux has an implementation of idle_timer that depends - # on XScreenSaver, but it's unclear if we want it yet, - # so use idle_timer_none.cc instead. - 'idle_timer.cc', - ], 'conditions': [ [ 'chromeos==1', { 'sources/': [ ['include', '_chromeos\\.cc$'] ] @@ -422,7 +413,6 @@ 'atomicops_internals_x86_gcc.cc', 'directory_watcher_inotify.cc', 'hmac_nss.cc', - 'idle_timer_none.cc', 'linux_util.cc', 'message_pump_glib.cc', 'nss_init.cc', @@ -618,7 +608,6 @@ 'gmock_unittest.cc', 'histogram_unittest.cc', 'hmac_unittest.cc', - 'idletimer_unittest.cc', 'id_map_unittest.cc', 'json_reader_unittest.cc', 'json_writer_unittest.cc', @@ -693,9 +682,6 @@ ['OS == "linux" or OS == "freebsd"', { 'sources!': [ 'file_version_info_unittest.cc', - # Linux has an implementation of idle_timer, but it's unclear - # if we want it yet, so leave it 'unported' for now. - 'idletimer_unittest.cc', 'worker_pool_linux_unittest.cc', ], 'dependencies': [ diff --git a/base/idle_timer.cc b/base/idle_timer.cc deleted file mode 100644 index de980f1..0000000 --- a/base/idle_timer.cc +++ /dev/null @@ -1,161 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/idle_timer.h" - -// We may not want to port idle_timer to Linux, but we have implemented it -// anyway. Define this to 1 to enable the Linux idle timer and then add the -// libs that need to be linked (Xss). -#define ENABLE_XSS_SUPPORT 0 - -#if defined(OS_MACOSX) -#include <ApplicationServices/ApplicationServices.h> -#endif - -#if defined(OS_LINUX) && ENABLE_XSS_SUPPORT -// We may not want to port idle_timer to Linux, but we have implemented it -// anyway. Remove the 0 above if we want it. -#include <gdk/gdkx.h> -#include <X11/extensions/scrnsaver.h> -#include "base/lazy_instance.h" -#include "base/thread_local.h" -#endif - -#include "base/message_loop.h" -#include "base/time.h" - -namespace base { - -#if defined(OS_WIN) -bool OSIdleTimeSource(int32 *milliseconds_interval_since_last_event) { - LASTINPUTINFO lastInputInfo; - lastInputInfo.cbSize = sizeof(lastInputInfo); - if (GetLastInputInfo(&lastInputInfo) == 0) { - return false; - } - int32 last_input_time = lastInputInfo.dwTime; - - // Note: On Windows GetLastInputInfo returns a 32bit value which rolls over - // ~49days. - int32 current_time = GetTickCount(); - int32 delta = current_time - last_input_time; - // delta will go negative if we've been idle for 2GB of ticks. - if (delta < 0) - delta = -delta; - *milliseconds_interval_since_last_event = delta; - return true; -} -#elif defined(OS_MACOSX) -bool OSIdleTimeSource(int32 *milliseconds_interval_since_last_event) { - *milliseconds_interval_since_last_event = - CGEventSourceSecondsSinceLastEventType( - kCGEventSourceStateCombinedSessionState, - kCGAnyInputEventType) * 1000.0; - return true; -} -#elif defined(OS_LINUX) && ENABLE_XSS_SUPPORT -class IdleState { - public: - IdleState() { - int event_base, error_base; - have_idle_info_ = XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, - &error_base); - if (have_idle_info_) - idle_info_.Set(XScreenSaverAllocInfo()); - } - - ~IdleState() { - if (idle_info_.Get()) { - XFree(idle_info_.Get()); - idle_info_.~ThreadLocalPointer(); - } - } - - int32 IdleTime() { - if (have_idle_info_ && idle_info_.Get()) { - XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), - idle_info_.Get()); - return idle_info_.Get()->idle; - } - return -1; - } - - private: - bool have_idle_info_; - ThreadLocalPointer<XScreenSaverInfo> idle_info_; - - DISALLOW_COPY_AND_ASSIGN(IdleState); -}; - -bool OSIdleTimeSource(int32* milliseconds_interval_since_last_event) { - static LazyInstance<IdleState> state_instance(base::LINKER_INITIALIZED); - IdleState* state = state_instance.Pointer(); - int32 idle_time = state->IdleTime(); - if (0 < idle_time) { - *milliseconds_interval_since_last_event = idle_time; - return true; - } - return false; -} -#endif - -IdleTimer::IdleTimer(TimeDelta idle_time, bool repeat) - : idle_interval_(idle_time), - repeat_(repeat), - idle_time_source_(OSIdleTimeSource) { - DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()) << - "Requires a thread that processes Windows UI events"; -} - -IdleTimer::~IdleTimer() { - Stop(); -} - -void IdleTimer::Start() { - StartTimer(); -} - -void IdleTimer::Stop() { - timer_.Stop(); -} - -void IdleTimer::Run() { - // Verify we can fire the idle timer. - if (TimeUntilIdle().InMilliseconds() <= 0) { - OnIdle(); - last_time_fired_ = Time::Now(); - } - Stop(); - StartTimer(); // Restart the timer for next run. -} - -void IdleTimer::StartTimer() { - DCHECK(!timer_.IsRunning()); - TimeDelta delay = TimeUntilIdle(); - if (delay.InMilliseconds() < 0) - delay = TimeDelta(); - timer_.Start(delay, this, &IdleTimer::Run); -} - -TimeDelta IdleTimer::CurrentIdleTime() { - int32 interval = 0; - if (idle_time_source_(&interval)) { - return TimeDelta::FromMilliseconds(interval); - } - NOTREACHED(); - return TimeDelta::FromMilliseconds(0); -} - -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) { - if (repeat_) - return idle_interval_ - time_since_last_fire; - return idle_interval_; - } - return idle_interval_ - current_idle_time; -} - -} // namespace base diff --git a/base/idle_timer.h b/base/idle_timer.h deleted file mode 100644 index 1f6c08c..0000000 --- a/base/idle_timer.h +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// 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 work, and may look like this: -// -// class MyIdleTimer : public IdleTimer { -// public: -// // This timer will run repeatedly after 5 seconds of idle time -// MyIdleTimer() : IdleTimer(TimeDelta::FromSeconds(5), true) {}; -// virtual void OnIdle() { do something }; -// } -// -// MyIdleTimer *timer = new MyIdleTimer(); -// timer->Start(); -// -// // As with all Timers, the caller must dispose the object. -// delete timer; // Will Stop the timer and cleanup. -// -// NOTE: An IdleTimer can only be used on a thread that processes UI events. -// Such a thread should be running a MessageLoopForUI. - -#ifndef BASE_IDLE_TIMER_H_ -#define BASE_IDLE_TIMER_H_ - -#if defined(OS_WIN) -#include <windows.h> -#endif - -#include "base/basictypes.h" -#include "base/timer.h" - -namespace base { - -// Function prototype - Get the number of milliseconds that the user has been -// idle. -typedef bool (*IdleTimeSource)(int32 *milliseconds_interval_since_last_event); - -class IdleTimer { - public: - // 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. - IdleTimer(TimeDelta idle_time, bool repeat); - - // On destruction, the IdleTimer will Stop itself. - virtual ~IdleTimer(); - - // Start the IdleTimer. - void Start(); - - // Stop the IdleTimer. - void Stop(); - - // The method to run when the timer elapses. - virtual void OnIdle() = 0; - - protected: - // Override the IdleTimeSource. - void set_idle_time_source(IdleTimeSource idle_time_source) { - idle_time_source_ = idle_time_source; - } - - private: - // Called when timer_ expires. - void Run(); - - // Start the timer. - void StartTimer(); - - // Gets the number of milliseconds since the last input event. - TimeDelta CurrentIdleTime(); - - // Compute time until idle. Returns 0 if we are now idle. - TimeDelta TimeUntilIdle(); - - TimeDelta idle_interval_; - bool repeat_; - Time last_time_fired_; // The last time the idle timer fired. - // will be 0 until the timer fires the first time. - OneShotTimer<IdleTimer> timer_; - - IdleTimeSource idle_time_source_; - - DISALLOW_COPY_AND_ASSIGN(IdleTimer); -}; - -} // namespace base - -#endif // BASE_IDLE_TIMER_H_ diff --git a/base/idle_timer_none.cc b/base/idle_timer_none.cc deleted file mode 100644 index 946535e..0000000 --- a/base/idle_timer_none.cc +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// A stub implementation of IdleTimer used to provide symbols needed by Chrome. -// It's unclear whether we need the idle timer in the linux port, so we're -// leaving it unported for now. - -#include "base/idle_timer.h" - -namespace base { - -IdleTimer::IdleTimer(TimeDelta idle_time, bool repeat) { -} - -IdleTimer::~IdleTimer() { -} - -void IdleTimer::Start() { -} - -void IdleTimer::Stop() { -} - -} // namespace base diff --git a/base/idletimer_unittest.cc b/base/idletimer_unittest.cc deleted file mode 100644 index 07a8bbd..0000000 --- a/base/idletimer_unittest.cc +++ /dev/null @@ -1,240 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/idle_timer.h" -#include "base/message_loop.h" -#include "testing/gtest/include/gtest/gtest.h" - -using base::Time; -using base::TimeDelta; -using base::IdleTimer; - - -// If the timers fire too quickly, it can be tricky to make timer tests -// reliable on all buildbots. This constant sets a minimum timer delta where -// we expect that we should be able to reliably count timers without problems -// due to slight clock/scheduling variances. -const int kSafeTestIntervalMs = 500; - -namespace { - -// We Mock the GetLastInputInfo function to return -// the time stored here. -static Time mock_timer_started; - -bool MockIdleTimeSource(int32 *milliseconds_interval_since_last_event) { - TimeDelta delta = Time::Now() - mock_timer_started; - *milliseconds_interval_since_last_event = - static_cast<int32>(delta.InMilliseconds()); - return true; -} - -// TestIdle task fires after 100ms of idle time. -class TestIdleTask : public IdleTimer { - public: - TestIdleTask(bool repeat) - : IdleTimer(TimeDelta::FromMilliseconds(kSafeTestIntervalMs), repeat), - idle_counter_(0) { - set_idle_time_source(MockIdleTimeSource); - } - - int get_idle_counter() { return idle_counter_; } - - virtual void OnIdle() { - idle_counter_++; - } - - private: - int idle_counter_; -}; - -// A task to help us quit the test. -class TestFinishedTask { - public: - TestFinishedTask() {} - void Run() { - MessageLoop::current()->Quit(); - } -}; - -// A timer which resets the idle clock. -class ResetIdleTask { - public: - ResetIdleTask() {} - void Run() { - mock_timer_started = Time::Now(); - } -}; - -class IdleTimerTest : public testing::Test { - private: - // IdleTimer requires a UI message loop on the current thread. - MessageLoopForUI message_loop_; -}; - -/////////////////////////////////////////////////////////////////////////////// -// NoRepeat tests: -// A non-repeating idle timer will fire once on idle, and -// then will not fire again unless it goes non-idle first. - -TEST_F(IdleTimerTest, NoRepeatIdle) { - // Create an IdleTimer, which should fire once after 500ms. - // Create a Quit timer which will fire after 1s. - // Verify that we fired exactly once. - - mock_timer_started = Time::Now(); - TestIdleTask test_task(false); - - TestFinishedTask finish_task; - base::OneShotTimer<TestFinishedTask> timer; - timer.Start(TimeDelta::FromMilliseconds(2 * kSafeTestIntervalMs), - &finish_task, &TestFinishedTask::Run); - - test_task.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(test_task.get_idle_counter(), 1); -} - -TEST_F(IdleTimerTest, NoRepeatFlipIdleOnce) { - // Create an IdleTimer, which should fire once after 500ms. - // Create a Quit timer which will fire after 5s. - // Create a timer to reset once, idle after 2s. - // Verify that we fired exactly twice. - - mock_timer_started = Time::Now(); - TestIdleTask test_task(false); - - TestFinishedTask finish_task; - ResetIdleTask reset_task; - - base::OneShotTimer<TestFinishedTask> t1; - t1.Start(TimeDelta::FromMilliseconds(10 * kSafeTestIntervalMs), &finish_task, - &TestFinishedTask::Run); - - base::OneShotTimer<ResetIdleTask> t2; - t2.Start(TimeDelta::FromMilliseconds(4 * kSafeTestIntervalMs), &reset_task, - &ResetIdleTask::Run); - - test_task.Start(); - MessageLoop::current()->Run(); - - EXPECT_EQ(test_task.get_idle_counter(), 2); -} - -TEST_F(IdleTimerTest, NoRepeatNotIdle) { - // Create an IdleTimer, which should fire once after 500ms. - // Create a Quit timer which will fire after 5s. - // Create a timer to reset idle every 50ms. - // Verify that we never fired. - - mock_timer_started = Time::Now(); - TestIdleTask test_task(false); - - TestFinishedTask finish_task; - ResetIdleTask reset_task; - - base::OneShotTimer<TestFinishedTask> t; - t.Start(TimeDelta::FromMilliseconds(10 * kSafeTestIntervalMs), &finish_task, - &TestFinishedTask::Run); - - base::RepeatingTimer<ResetIdleTask> reset_timer; - reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task, - &ResetIdleTask::Run); - - test_task.Start(); - - MessageLoop::current()->Run(); - - reset_timer.Stop(); - - EXPECT_EQ(test_task.get_idle_counter(), 0); -} - -/////////////////////////////////////////////////////////////////////////////// -// Repeat tests: -// A repeating idle timer will fire repeatedly on each interval, as long -// as it has been idle. So, if the machine remains idle, it will continue -// firing over and over. - -TEST_F(IdleTimerTest, Repeat) { - // Create an IdleTimer, which should fire repeatedly after 500ms. - // Create a Quit timer which will fire after 1.5s. - // Verify that we fired 2-3 times. - mock_timer_started = Time::Now(); - TestIdleTask test_task(true); - - TestFinishedTask finish_task; - - base::OneShotTimer<TestFinishedTask> t; - t.Start(TimeDelta::FromMilliseconds(kSafeTestIntervalMs * 3), &finish_task, - &TestFinishedTask::Run); - - test_task.Start(); - MessageLoop::current()->Run(); - - // In a perfect world, the idle_counter should be 2. However, - // due to timer 'slop', accept 2 or 3. - EXPECT_GE(test_task.get_idle_counter(), 2); - EXPECT_LE(test_task.get_idle_counter(), 3); -} - -TEST_F(IdleTimerTest, RepeatIdleReset) { - // Create an IdleTimer, which should fire repeatedly after 500ms. - // Create a Quit timer which will fire after 5s. - // Create a reset timer, which fires after 2500ms - // Verify that we fired 8-10 times. - mock_timer_started = Time::Now(); - TestIdleTask test_task(true); - - ResetIdleTask reset_task; - TestFinishedTask finish_task; - - base::OneShotTimer<TestFinishedTask> t1; - t1.Start(TimeDelta::FromMilliseconds(10 * kSafeTestIntervalMs), &finish_task, - &TestFinishedTask::Run); - - base::OneShotTimer<ResetIdleTask> t2; - t2.Start(TimeDelta::FromMilliseconds(5 * kSafeTestIntervalMs), &reset_task, - &ResetIdleTask::Run); - - test_task.Start(); - MessageLoop::current()->Run(); - - // In a perfect world, the idle_counter should be 9. However, - // since timers aren't guaranteed to fire perfectly, this can - // be less. Accept 8-10. - EXPECT_GE(test_task.get_idle_counter(), 8); - EXPECT_LE(test_task.get_idle_counter(), 10); -} - -TEST_F(IdleTimerTest, RepeatNotIdle) { - // Create an IdleTimer, which should fire repeatedly after 500ms. - // Create a Quit timer which will fire after 4s. - // Create a timer to reset idle every 50ms. - // Verify that we never fired. - - mock_timer_started = Time::Now(); - TestIdleTask test_task(true); - - TestFinishedTask finish_task; - ResetIdleTask reset_task; - - base::OneShotTimer<TestFinishedTask> t; - t.Start(TimeDelta::FromMilliseconds(8 * kSafeTestIntervalMs), &finish_task, - &TestFinishedTask::Run); - - base::RepeatingTimer<ResetIdleTask> reset_timer; - reset_timer.Start(TimeDelta::FromMilliseconds(50), &reset_task, - &ResetIdleTask::Run); - - test_task.Start(); - MessageLoop::current()->Run(); - - reset_timer.Stop(); - - EXPECT_EQ(test_task.get_idle_counter(), 0); -} - -} // namespace |