OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/idle_timer.h" | 5 #include "base/idle_timer.h" |
6 | 6 |
| 7 #if defined(OS_MACOSX) |
| 8 #include <ApplicationServices/ApplicationServices.h> |
| 9 #endif |
| 10 |
7 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
8 #include "base/time.h" | 12 #include "base/time.h" |
9 | 13 |
10 namespace base { | 14 namespace base { |
11 | 15 |
| 16 #if defined(OS_WIN) |
| 17 bool OSIdleTimeSource(int32 *milliseconds_interval_since_last_event) { |
| 18 LASTINPUTINFO lastInputInfo; |
| 19 lastInputInfo.cbSize = sizeof(lastInputInfo); |
| 20 if (GetLastInputInfo(&lastInputInfo) == 0) { |
| 21 return false; |
| 22 } |
| 23 int32 last_input_time = lastInputInfo.dwTime; |
| 24 |
| 25 // Note: On Windows GetLastInputInfo returns a 32bit value which rolls over |
| 26 // ~49days. |
| 27 int32 current_time = GetTickCount(); |
| 28 int32 delta = current_time - last_input_time; |
| 29 // delta will go negative if we've been idle for 2GB of ticks. |
| 30 if (delta < 0) |
| 31 delta = -delta; |
| 32 *milliseconds_interval_since_last_event = delta; |
| 33 return true; |
| 34 } |
| 35 #elif defined(OS_MACOSX) |
| 36 bool OSIdleTimeSource(int32 *milliseconds_interval_since_last_event) { |
| 37 *milliseconds_interval_since_last_event = |
| 38 CGEventSourceSecondsSinceLastEventType( |
| 39 kCGEventSourceStateCombinedSessionState, |
| 40 kCGAnyInputEventType) * 1000.0; |
| 41 return true; |
| 42 } |
| 43 #endif |
| 44 |
12 IdleTimer::IdleTimer(TimeDelta idle_time, bool repeat) | 45 IdleTimer::IdleTimer(TimeDelta idle_time, bool repeat) |
13 : idle_interval_(idle_time), | 46 : idle_interval_(idle_time), |
14 repeat_(repeat), | 47 repeat_(repeat), |
15 get_last_input_info_fn_(GetLastInputInfo) { | 48 idle_time_source_(OSIdleTimeSource) { |
16 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()) << | 49 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()) << |
17 "Requires a thread that processes Windows UI events"; | 50 "Requires a thread that processes Windows UI events"; |
18 } | 51 } |
19 | 52 |
20 IdleTimer::~IdleTimer() { | 53 IdleTimer::~IdleTimer() { |
21 Stop(); | 54 Stop(); |
22 } | 55 } |
23 | 56 |
24 void IdleTimer::Start() { | 57 void IdleTimer::Start() { |
25 StartTimer(); | 58 StartTimer(); |
(...skipping 15 matching lines...) Expand all Loading... |
41 | 74 |
42 void IdleTimer::StartTimer() { | 75 void IdleTimer::StartTimer() { |
43 DCHECK(!timer_.IsRunning()); | 76 DCHECK(!timer_.IsRunning()); |
44 TimeDelta delay = TimeUntilIdle(); | 77 TimeDelta delay = TimeUntilIdle(); |
45 if (delay.InMilliseconds() < 0) | 78 if (delay.InMilliseconds() < 0) |
46 delay = TimeDelta(); | 79 delay = TimeDelta(); |
47 timer_.Start(delay, this, &IdleTimer::Run); | 80 timer_.Start(delay, this, &IdleTimer::Run); |
48 } | 81 } |
49 | 82 |
50 TimeDelta IdleTimer::CurrentIdleTime() { | 83 TimeDelta IdleTimer::CurrentIdleTime() { |
51 // TODO(mbelshe): This is windows-specific code. | 84 int32 interval = 0; |
52 LASTINPUTINFO info; | 85 if (idle_time_source_(&interval)) { |
53 info.cbSize = sizeof(info); | |
54 if (get_last_input_info_fn_(&info)) { | |
55 // Note: GetLastInputInfo returns a 32bit value which rolls over ~49days. | |
56 int32 last_input_time = info.dwTime; | |
57 int32 current_time = GetTickCount(); | |
58 int32 interval = current_time - last_input_time; | |
59 // Interval will go negative if we've been idle for 2GB of ticks. | |
60 if (interval < 0) | |
61 interval = -interval; | |
62 return TimeDelta::FromMilliseconds(interval); | 86 return TimeDelta::FromMilliseconds(interval); |
63 } | 87 } |
64 NOTREACHED(); | 88 NOTREACHED(); |
65 return TimeDelta::FromMilliseconds(0); | 89 return TimeDelta::FromMilliseconds(0); |
66 } | 90 } |
67 | 91 |
68 TimeDelta IdleTimer::TimeUntilIdle() { | 92 TimeDelta IdleTimer::TimeUntilIdle() { |
69 TimeDelta time_since_last_fire = Time::Now() - last_time_fired_; | 93 TimeDelta time_since_last_fire = Time::Now() - last_time_fired_; |
70 TimeDelta current_idle_time = CurrentIdleTime(); | 94 TimeDelta current_idle_time = CurrentIdleTime(); |
71 if (current_idle_time > time_since_last_fire) { | 95 if (current_idle_time > time_since_last_fire) { |
72 if (repeat_) | 96 if (repeat_) |
73 return idle_interval_ - time_since_last_fire; | 97 return idle_interval_ - time_since_last_fire; |
74 return idle_interval_; | 98 return idle_interval_; |
75 } | 99 } |
76 return idle_interval_ - current_idle_time; | 100 return idle_interval_ - current_idle_time; |
77 } | 101 } |
78 | 102 |
79 } // namespace base | 103 } // namespace base |
OLD | NEW |