ipc/chromium/src/base/idle_timer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 // IdleTimer is a recurring Timer which runs only when the system is idle.
michael@0 6 // System Idle time is defined as not having any user keyboard or mouse
michael@0 7 // activity for some period of time. Because the timer is user dependant, it
michael@0 8 // is possible for the timer to never fire.
michael@0 9 //
michael@0 10 // Usage should be for low-priority work, and may look like this:
michael@0 11 //
michael@0 12 // class MyIdleTimer : public IdleTimer {
michael@0 13 // public:
michael@0 14 // // This timer will run repeatedly after 5 seconds of idle time
michael@0 15 // MyIdleTimer() : IdleTimer(TimeDelta::FromSeconds(5), true) {};
michael@0 16 // virtual void OnIdle() { do something };
michael@0 17 // }
michael@0 18 //
michael@0 19 // MyIdleTimer *timer = new MyIdleTimer();
michael@0 20 // timer->Start();
michael@0 21 //
michael@0 22 // // As with all Timers, the caller must dispose the object.
michael@0 23 // delete timer; // Will Stop the timer and cleanup.
michael@0 24 //
michael@0 25 // NOTE: An IdleTimer can only be used on a thread that processes UI events.
michael@0 26 // Such a thread should be running a MessageLoopForUI.
michael@0 27
michael@0 28 #ifndef BASE_IDLE_TIMER_H_
michael@0 29 #define BASE_IDLE_TIMER_H_
michael@0 30
michael@0 31 #if defined(OS_WIN)
michael@0 32 #include <windows.h>
michael@0 33 #endif
michael@0 34
michael@0 35 #include "base/basictypes.h"
michael@0 36 #include "base/task.h"
michael@0 37 #include "base/timer.h"
michael@0 38
michael@0 39 namespace base {
michael@0 40
michael@0 41 // Function prototype - Get the number of milliseconds that the user has been
michael@0 42 // idle.
michael@0 43 typedef bool (*IdleTimeSource)(int32_t *milliseconds_interval_since_last_event);
michael@0 44
michael@0 45 class IdleTimer {
michael@0 46 public:
michael@0 47 // Create an IdleTimer.
michael@0 48 // idle_time: idle time required before this timer can run.
michael@0 49 // repeat: true if the timer should fire multiple times per idle,
michael@0 50 // false to fire once per idle.
michael@0 51 IdleTimer(TimeDelta idle_time, bool repeat);
michael@0 52
michael@0 53 // On destruction, the IdleTimer will Stop itself.
michael@0 54 virtual ~IdleTimer();
michael@0 55
michael@0 56 // Start the IdleTimer.
michael@0 57 void Start();
michael@0 58
michael@0 59 // Stop the IdleTimer.
michael@0 60 void Stop();
michael@0 61
michael@0 62 // The method to run when the timer elapses.
michael@0 63 virtual void OnIdle() = 0;
michael@0 64
michael@0 65 protected:
michael@0 66 // Override the IdleTimeSource.
michael@0 67 void set_idle_time_source(IdleTimeSource idle_time_source) {
michael@0 68 idle_time_source_ = idle_time_source;
michael@0 69 }
michael@0 70
michael@0 71 private:
michael@0 72 // Called when timer_ expires.
michael@0 73 void Run();
michael@0 74
michael@0 75 // Start the timer.
michael@0 76 void StartTimer();
michael@0 77
michael@0 78 // Gets the number of milliseconds since the last input event.
michael@0 79 TimeDelta CurrentIdleTime();
michael@0 80
michael@0 81 // Compute time until idle. Returns 0 if we are now idle.
michael@0 82 TimeDelta TimeUntilIdle();
michael@0 83
michael@0 84 TimeDelta idle_interval_;
michael@0 85 bool repeat_;
michael@0 86 Time last_time_fired_; // The last time the idle timer fired.
michael@0 87 // will be 0 until the timer fires the first time.
michael@0 88 OneShotTimer<IdleTimer> timer_;
michael@0 89
michael@0 90 IdleTimeSource idle_time_source_;
michael@0 91
michael@0 92 DISALLOW_COPY_AND_ASSIGN(IdleTimer);
michael@0 93 };
michael@0 94
michael@0 95 } // namespace base
michael@0 96
michael@0 97 #endif // BASE_IDLE_TIMER_H_

mercurial