1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/idle_timer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// IdleTimer is a recurring Timer which runs only when the system is idle. 1.9 +// System Idle time is defined as not having any user keyboard or mouse 1.10 +// activity for some period of time. Because the timer is user dependant, it 1.11 +// is possible for the timer to never fire. 1.12 +// 1.13 +// Usage should be for low-priority work, and may look like this: 1.14 +// 1.15 +// class MyIdleTimer : public IdleTimer { 1.16 +// public: 1.17 +// // This timer will run repeatedly after 5 seconds of idle time 1.18 +// MyIdleTimer() : IdleTimer(TimeDelta::FromSeconds(5), true) {}; 1.19 +// virtual void OnIdle() { do something }; 1.20 +// } 1.21 +// 1.22 +// MyIdleTimer *timer = new MyIdleTimer(); 1.23 +// timer->Start(); 1.24 +// 1.25 +// // As with all Timers, the caller must dispose the object. 1.26 +// delete timer; // Will Stop the timer and cleanup. 1.27 +// 1.28 +// NOTE: An IdleTimer can only be used on a thread that processes UI events. 1.29 +// Such a thread should be running a MessageLoopForUI. 1.30 + 1.31 +#ifndef BASE_IDLE_TIMER_H_ 1.32 +#define BASE_IDLE_TIMER_H_ 1.33 + 1.34 +#if defined(OS_WIN) 1.35 +#include <windows.h> 1.36 +#endif 1.37 + 1.38 +#include "base/basictypes.h" 1.39 +#include "base/task.h" 1.40 +#include "base/timer.h" 1.41 + 1.42 +namespace base { 1.43 + 1.44 +// Function prototype - Get the number of milliseconds that the user has been 1.45 +// idle. 1.46 +typedef bool (*IdleTimeSource)(int32_t *milliseconds_interval_since_last_event); 1.47 + 1.48 +class IdleTimer { 1.49 + public: 1.50 + // Create an IdleTimer. 1.51 + // idle_time: idle time required before this timer can run. 1.52 + // repeat: true if the timer should fire multiple times per idle, 1.53 + // false to fire once per idle. 1.54 + IdleTimer(TimeDelta idle_time, bool repeat); 1.55 + 1.56 + // On destruction, the IdleTimer will Stop itself. 1.57 + virtual ~IdleTimer(); 1.58 + 1.59 + // Start the IdleTimer. 1.60 + void Start(); 1.61 + 1.62 + // Stop the IdleTimer. 1.63 + void Stop(); 1.64 + 1.65 + // The method to run when the timer elapses. 1.66 + virtual void OnIdle() = 0; 1.67 + 1.68 + protected: 1.69 + // Override the IdleTimeSource. 1.70 + void set_idle_time_source(IdleTimeSource idle_time_source) { 1.71 + idle_time_source_ = idle_time_source; 1.72 + } 1.73 + 1.74 + private: 1.75 + // Called when timer_ expires. 1.76 + void Run(); 1.77 + 1.78 + // Start the timer. 1.79 + void StartTimer(); 1.80 + 1.81 + // Gets the number of milliseconds since the last input event. 1.82 + TimeDelta CurrentIdleTime(); 1.83 + 1.84 + // Compute time until idle. Returns 0 if we are now idle. 1.85 + TimeDelta TimeUntilIdle(); 1.86 + 1.87 + TimeDelta idle_interval_; 1.88 + bool repeat_; 1.89 + Time last_time_fired_; // The last time the idle timer fired. 1.90 + // will be 0 until the timer fires the first time. 1.91 + OneShotTimer<IdleTimer> timer_; 1.92 + 1.93 + IdleTimeSource idle_time_source_; 1.94 + 1.95 + DISALLOW_COPY_AND_ASSIGN(IdleTimer); 1.96 +}; 1.97 + 1.98 +} // namespace base 1.99 + 1.100 +#endif // BASE_IDLE_TIMER_H_