[email protected] | b112e4c | 2011-05-21 21:50:32 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 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 | |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | #include <windows.h> |
| 9 | |
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 10 | namespace { |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 11 | |
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 12 | DWORD CalculateIdleTimeInternal() { |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 13 | LASTINPUTINFO last_input_info = {0}; |
| 14 | last_input_info.cbSize = sizeof(LASTINPUTINFO); |
| 15 | DWORD current_idle_time = 0; |
| 16 | if (::GetLastInputInfo(&last_input_info)) { |
[email protected] | b112e4c | 2011-05-21 21:50:32 | [diff] [blame] | 17 | DWORD now = ::GetTickCount(); |
| 18 | if (now < last_input_info.dwTime) { |
| 19 | // GetTickCount() wraps around every 49.7 days -- assume it wrapped just |
| 20 | // once. |
| 21 | const DWORD kMaxDWORD = static_cast<DWORD>(-1); |
| 22 | DWORD time_before_wrap = kMaxDWORD - last_input_info.dwTime; |
| 23 | DWORD time_after_wrap = now; |
| 24 | // The sum is always smaller than kMaxDWORD. |
| 25 | current_idle_time = time_before_wrap + time_after_wrap; |
| 26 | } else { |
| 27 | current_idle_time = now - last_input_info.dwTime; |
| 28 | } |
| 29 | |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 30 | // Convert from ms to seconds. |
| 31 | current_idle_time /= 1000; |
| 32 | } |
| 33 | |
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 34 | return current_idle_time; |
[email protected] | f520541 | 2010-03-16 00:19:34 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool IsScreensaverRunning() { |
| 38 | DWORD result = 0; |
| 39 | if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &result, 0)) |
| 40 | return result != FALSE; |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | bool IsWorkstationLocked() { |
| 45 | bool is_locked = true; |
| 46 | HDESK input_desk = ::OpenInputDesktop(0, 0, GENERIC_READ); |
| 47 | if (input_desk) { |
| 48 | wchar_t name[256] = {0}; |
| 49 | DWORD needed = 0; |
| 50 | if (::GetUserObjectInformation(input_desk, |
| 51 | UOI_NAME, |
| 52 | name, |
| 53 | sizeof(name), |
| 54 | &needed)) { |
| 55 | is_locked = lstrcmpi(name, L"default") != 0; |
| 56 | } |
| 57 | ::CloseDesktop(input_desk); |
| 58 | } |
| 59 | return is_locked; |
| 60 | } |
[email protected] | 80722b2 | 2011-09-10 07:54:50 | [diff] [blame] | 61 | |
[email protected] | 4c120af | 2012-11-17 03:27:34 | [diff] [blame] | 62 | } // namespace |
| 63 | |
| 64 | void CalculateIdleTime(IdleTimeCallback notify) { |
| 65 | notify.Run(static_cast<int>(CalculateIdleTimeInternal())); |
| 66 | } |
| 67 | |
[email protected] | 80722b2 | 2011-09-10 07:54:50 | [diff] [blame] | 68 | bool CheckIdleStateIsLocked() { |
| 69 | return IsWorkstationLocked() || IsScreensaverRunning(); |
| 70 | } |