• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/idle.h"
6 
7 #include <limits.h>
8 #include <windows.h>
9 
10 #include "ui/base/win/lock_state.h"
11 
12 namespace {
13 
CalculateIdleTimeInternal()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
22       // once.
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;
28     } else {
29       current_idle_time = now - last_input_info.dwTime;
30     }
31 
32     // Convert from ms to seconds.
33     current_idle_time /= 1000;
34   }
35 
36   return current_idle_time;
37 }
38 
IsScreensaverRunning()39 bool IsScreensaverRunning() {
40   DWORD result = 0;
41   if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &result, 0))
42     return result != FALSE;
43   return false;
44 }
45 
46 }  // namespace
47 
CalculateIdleTime(IdleTimeCallback notify)48 void CalculateIdleTime(IdleTimeCallback notify) {
49   notify.Run(static_cast<int>(CalculateIdleTimeInternal()));
50 }
51 
CheckIdleStateIsLocked()52 bool CheckIdleStateIsLocked() {
53   return ui::IsWorkstationLocked() || IsScreensaverRunning();
54 }
55