Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/idle.h" | 5 #include "chrome/browser/idle.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 static bool IsScreensaverRunning(); | 10 static bool IsScreensaverRunning(); |
| 11 static bool IsWorkstationLocked(); | 11 static bool IsWorkstationLocked(); |
| 12 | 12 |
| 13 IdleState CalculateIdleState(unsigned int idle_threshold) { | 13 IdleState CalculateIdleState(unsigned int idle_threshold) { |
| 14 if (IsScreensaverRunning() || IsWorkstationLocked()) | 14 if (IsScreensaverRunning() || IsWorkstationLocked()) |
| 15 return IDLE_STATE_LOCKED; | 15 return IDLE_STATE_LOCKED; |
| 16 | 16 |
| 17 LASTINPUTINFO last_input_info = {0}; | 17 LASTINPUTINFO last_input_info = {0}; |
| 18 last_input_info.cbSize = sizeof(LASTINPUTINFO); | 18 last_input_info.cbSize = sizeof(LASTINPUTINFO); |
| 19 DWORD current_idle_time = 0; | 19 DWORD current_idle_time = 0; |
| 20 if (::GetLastInputInfo(&last_input_info)) { | 20 if (::GetLastInputInfo(&last_input_info)) { |
| 21 current_idle_time = ::GetTickCount() - last_input_info.dwTime; | 21 DWORD now = ::GetTickCount(); |
| 22 // Will go -ve if we have been idle for a long time (2gb seconds). | 22 if (now < last_input_info.dwTime) { |
| 23 if (current_idle_time < 0) | 23 // GetTickCount() wraps around every 49.7 days -- assume it wrapped just |
| 24 current_idle_time = INT_MAX; | 24 // once. |
| 25 const DWORD MAX_DWORD = static_cast<DWORD>(-1); | |
|
Evan Martin
2011/05/20 21:25:46
kMaxDWORD ?
Nico
2011/05/20 21:31:34
Done.
| |
| 26 DWORD time_before_wrap = MAX_DWORD - last_input_info.dwTime; | |
| 27 DWORD time_after_wrap = now; | |
| 28 // The sum is always smaller than MAX_DWORD. | |
| 29 current_idle_time = time_after_wrap + time_after_wrap; | |
| 30 } else { | |
| 31 current_idle_time = now - last_input_info.dwTime; | |
| 32 } | |
| 33 | |
| 25 // Convert from ms to seconds. | 34 // Convert from ms to seconds. |
| 26 current_idle_time /= 1000; | 35 current_idle_time /= 1000; |
| 27 } | 36 } |
| 28 | 37 |
| 29 if (current_idle_time >= idle_threshold) | 38 if (current_idle_time >= idle_threshold) |
| 30 return IDLE_STATE_IDLE; | 39 return IDLE_STATE_IDLE; |
| 31 return IDLE_STATE_ACTIVE; | 40 return IDLE_STATE_ACTIVE; |
| 32 } | 41 } |
| 33 | 42 |
| 34 bool IsScreensaverRunning() { | 43 bool IsScreensaverRunning() { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 48 UOI_NAME, | 57 UOI_NAME, |
| 49 name, | 58 name, |
| 50 sizeof(name), | 59 sizeof(name), |
| 51 &needed)) { | 60 &needed)) { |
| 52 is_locked = lstrcmpi(name, L"default") != 0; | 61 is_locked = lstrcmpi(name, L"default") != 0; |
| 53 } | 62 } |
| 54 ::CloseDesktop(input_desk); | 63 ::CloseDesktop(input_desk); |
| 55 } | 64 } |
| 56 return is_locked; | 65 return is_locked; |
| 57 } | 66 } |
| OLD | NEW |