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