Introduce ProfilerMetricsProvider
[chromium-blink-merge.git] / chrome / browser / idle_win.cc
blob0457ed9948fb4520d90808cfe7e7c4e753f4a72b
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"
7 #include <limits.h>
8 #include <windows.h>
10 #include "ui/base/win/lock_state.h"
12 namespace {
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;
32 // Convert from ms to seconds.
33 current_idle_time /= 1000;
36 return current_idle_time;
39 bool IsScreensaverRunning() {
40 DWORD result = 0;
41 if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &result, 0))
42 return result != FALSE;
43 return false;
46 } // namespace
48 void CalculateIdleTime(IdleTimeCallback notify) {
49 notify.Run(static_cast<int>(CalculateIdleTimeInternal()));
52 bool CheckIdleStateIsLocked() {
53 return ui::IsWorkstationLocked() || IsScreensaverRunning();