Chromium Code Reviews| Index: chrome/browser/chromeos/idle_detector.cc |
| diff --git a/chrome/browser/chromeos/idle_detector.cc b/chrome/browser/chromeos/idle_detector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e56026bac770a3c7322ccbb36d39469698f52421 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/idle_detector.cc |
| @@ -0,0 +1,46 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/idle_detector.h" |
| + |
| +#include "ash/shell.h" |
| +#include "ash/wm/user_activity_detector.h" |
| +#include "base/bind.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/logging.h" |
| +#include "base/message_loop/message_loop.h" |
| + |
| +namespace chromeos { |
| + |
| +IdleDetector::IdleDetector(const base::Closure& on_active_callback, |
| + const base::Closure& on_idle_callback) |
| + : active_callback_(on_active_callback), idle_callback_(on_idle_callback) {} |
| + |
| +IdleDetector::~IdleDetector() { |
| + if (ash::Shell::HasInstance() && |
| + ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this)) |
| + ash::Shell::GetInstance()->user_activity_detector()->RemoveObserver(this); |
| +} |
| + |
| +void IdleDetector::OnUserActivity(const ui::Event* event) { |
| + active_callback_.Run(); |
|
xiyuan
2014/02/11 03:42:45
OnUserActivity is called quite frequently and we s
rkc
2014/02/11 04:35:57
Done.
|
| + ResetTimer(); |
| +} |
| + |
| +void IdleDetector::Start(const base::TimeDelta& timeout) { |
| + timeout_ = timeout; |
| + if (!ash::Shell::GetInstance()->user_activity_detector()->HasObserver(this)) |
| + ash::Shell::GetInstance()->user_activity_detector()->AddObserver(this); |
| + ResetTimer(); |
| +} |
| + |
| +void IdleDetector::ResetTimer() { |
| + if (timer_.IsRunning()) { |
| + timer_.Reset(); |
| + } else { |
| + timer_.Start(FROM_HERE, timeout_, idle_callback_); |
| + } |
| +} |
| + |
| +} // namespace chromeos |