1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/idle.h"
10 #include "ui/base/win/lock_state.h"
14 DWORD
CalculateIdleTimeInternal() {
15 LASTINPUTINFO last_input_info
= {0};
16 last_input_info
.cbSize
= sizeof(LASTINPUTINFO
);
17 DWORD current_idle_time
= 0;
18 if (::GetLastInputInfo(&last_input_info
)) {
19 DWORD now
= ::GetTickCount();
20 if (now
< last_input_info
.dwTime
) {
21 // GetTickCount() wraps around every 49.7 days -- assume it wrapped just
23 const DWORD kMaxDWORD
= static_cast<DWORD
>(-1);
24 DWORD time_before_wrap
= kMaxDWORD
- last_input_info
.dwTime
;
25 DWORD time_after_wrap
= now
;
26 // The sum is always smaller than kMaxDWORD.
27 current_idle_time
= time_before_wrap
+ time_after_wrap
;
29 current_idle_time
= now
- last_input_info
.dwTime
;
32 // Convert from ms to seconds.
33 current_idle_time
/= 1000;
36 return current_idle_time
;
39 bool IsScreensaverRunning() {
41 if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING
, 0, &result
, 0))
42 return result
!= FALSE
;
48 void CalculateIdleTime(IdleTimeCallback notify
) {
49 notify
.Run(static_cast<int>(CalculateIdleTimeInternal()));
52 bool CheckIdleStateIsLocked() {
53 return ui::IsWorkstationLocked() || IsScreensaverRunning();