Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / idle_detector.cc
1 // Copyright 2014 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/chromeos/idle_detector.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "ui/wm/core/user_activity_detector.h"
10
11 namespace chromeos {
12
13 IdleDetector::IdleDetector(const base::Closure& on_active_callback,
14                            const base::Closure& on_idle_callback)
15     : active_callback_(on_active_callback), idle_callback_(on_idle_callback) {}
16
17 IdleDetector::~IdleDetector() {
18   wm::UserActivityDetector* user_activity_detector =
19       wm::UserActivityDetector::Get();
20   if (user_activity_detector && user_activity_detector->HasObserver(this))
21     user_activity_detector->RemoveObserver(this);
22 }
23
24 void IdleDetector::OnUserActivity(const ui::Event* event) {
25   if (!active_callback_.is_null())
26     active_callback_.Run();
27   ResetTimer();
28 }
29
30 void IdleDetector::Start(const base::TimeDelta& timeout) {
31   timeout_ = timeout;
32   if (!wm::UserActivityDetector::Get()->HasObserver(this))
33     wm::UserActivityDetector::Get()->AddObserver(this);
34   ResetTimer();
35 }
36
37 void IdleDetector::ResetTimer() {
38   if (timer_.IsRunning())
39     timer_.Reset();
40   else
41     timer_.Start(FROM_HERE, timeout_, idle_callback_);
42 }
43
44 }  // namespace chromeos