Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / ash / system / chromeos / power / user_activity_notifier.cc
1 // Copyright 2013 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 "ash/system/chromeos/power/user_activity_notifier.h"
6
7 #include "ash/shell.h"
8 #include "ash/wm/user_activity_detector.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/power_manager_client.h"
11 #include "ui/events/event.h"
12 #include "ui/events/event_constants.h"
13 #include "ui/events/keycodes/keyboard_codes_posix.h"
14
15 namespace ash {
16 namespace internal {
17
18 namespace {
19
20 // Minimum number of seconds between notifications.
21 const int kNotifyIntervalSec = 5;
22
23 // Returns a UserActivityType describing |event|.
24 power_manager::UserActivityType GetUserActivityTypeForEvent(
25     const ui::Event* event) {
26   if (!event || event->type() != ui::ET_KEY_PRESSED)
27     return power_manager::USER_ACTIVITY_OTHER;
28
29   switch (static_cast<const ui::KeyEvent*>(event)->key_code()) {
30     case ui::VKEY_BRIGHTNESS_DOWN:
31       return power_manager::USER_ACTIVITY_BRIGHTNESS_DOWN_KEY_PRESS;
32     case ui::VKEY_BRIGHTNESS_UP:
33       return power_manager::USER_ACTIVITY_BRIGHTNESS_UP_KEY_PRESS;
34     case ui::VKEY_VOLUME_DOWN:
35       return power_manager::USER_ACTIVITY_VOLUME_DOWN_KEY_PRESS;
36     case ui::VKEY_VOLUME_MUTE:
37       return power_manager::USER_ACTIVITY_VOLUME_MUTE_KEY_PRESS;
38     case ui::VKEY_VOLUME_UP:
39       return power_manager::USER_ACTIVITY_VOLUME_UP_KEY_PRESS;
40     default:
41       return power_manager::USER_ACTIVITY_OTHER;
42   }
43 }
44
45 }  // namespace
46
47 UserActivityNotifier::UserActivityNotifier(UserActivityDetector* detector)
48     : detector_(detector) {
49   detector_->AddObserver(this);
50 }
51
52 UserActivityNotifier::~UserActivityNotifier() {
53   detector_->RemoveObserver(this);
54 }
55
56 void UserActivityNotifier::OnUserActivity(const ui::Event* event) {
57   base::TimeTicks now = base::TimeTicks::Now();
58   // InSeconds() truncates rather than rounding, so it's fine for this
59   // comparison.
60   if (last_notify_time_.is_null() ||
61       (now - last_notify_time_).InSeconds() >= kNotifyIntervalSec) {
62     chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
63         NotifyUserActivity(GetUserActivityTypeForEvent(event));
64     last_notify_time_ = now;
65   }
66 }
67
68 }  // namespace internal
69 }  // namespace ash