Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / user_activity / user_activity_detector.cc
blob78812bc76b7f7987994b9452adb85c346dc3a11e
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.
5 #include "ui/base/user_activity/user_activity_detector.h"
7 #include "base/format_macros.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "ui/base/user_activity/user_activity_observer.h"
11 #include "ui/events/event_utils.h"
12 #include "ui/events/platform/platform_event_source.h"
14 namespace ui {
16 namespace {
18 UserActivityDetector* g_instance = nullptr;
20 // Returns a string describing |event|.
21 std::string GetEventDebugString(const ui::Event* event) {
22 std::string details = base::StringPrintf(
23 "type=%d name=%s flags=%d time=%" PRId64,
24 event->type(), event->name().c_str(), event->flags(),
25 event->time_stamp().InMilliseconds());
27 if (event->IsKeyEvent()) {
28 details += base::StringPrintf(" key_code=%d",
29 static_cast<const ui::KeyEvent*>(event)->key_code());
30 } else if (event->IsMouseEvent() || event->IsTouchEvent() ||
31 event->IsGestureEvent()) {
32 details += base::StringPrintf(" location=%s",
33 static_cast<const ui::LocatedEvent*>(
34 event)->location().ToString().c_str());
37 return details;
40 } // namespace
42 const int UserActivityDetector::kNotifyIntervalMs = 200;
44 // Too low and mouse events generated at the tail end of reconfiguration
45 // will be reported as user activity and turn the screen back on; too high
46 // and we'll ignore legitimate activity.
47 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000;
49 UserActivityDetector::UserActivityDetector() {
50 CHECK(!g_instance);
51 g_instance = this;
53 ui::PlatformEventSource* platform_event_source =
54 ui::PlatformEventSource::GetInstance();
55 #if defined(OS_CHROMEOS) || defined(OS_LINUX)
56 CHECK(platform_event_source);
57 #endif
58 if (platform_event_source)
59 platform_event_source->AddPlatformEventObserver(this);
62 UserActivityDetector::~UserActivityDetector() {
63 ui::PlatformEventSource* platform_event_source =
64 ui::PlatformEventSource::GetInstance();
65 #if defined(OS_CHROMEOS) || defined(OS_LINUX)
66 CHECK(platform_event_source);
67 #endif
68 if (platform_event_source)
69 platform_event_source->RemovePlatformEventObserver(this);
70 g_instance = nullptr;
73 // static
74 UserActivityDetector* UserActivityDetector::Get() {
75 return g_instance;
78 bool UserActivityDetector::HasObserver(
79 const UserActivityObserver* observer) const {
80 return observers_.HasObserver(observer);
83 void UserActivityDetector::AddObserver(UserActivityObserver* observer) {
84 observers_.AddObserver(observer);
87 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) {
88 observers_.RemoveObserver(observer);
91 void UserActivityDetector::OnDisplayPowerChanging() {
92 honor_mouse_events_time_ = GetCurrentTime() +
93 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs);
96 void UserActivityDetector::DidProcessEvent(
97 const PlatformEvent& platform_event) {
98 scoped_ptr<ui::Event> event(ui::EventFromNative(platform_event));
99 ProcessReceivedEvent(event.get());
102 base::TimeTicks UserActivityDetector::GetCurrentTime() const {
103 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
106 void UserActivityDetector::ProcessReceivedEvent(const ui::Event* event) {
107 if (!event)
108 return;
110 if (event->IsMouseEvent() || event->IsMouseWheelEvent()) {
111 if (event->flags() & ui::EF_IS_SYNTHESIZED)
112 return;
113 if (!honor_mouse_events_time_.is_null()
114 && GetCurrentTime() < honor_mouse_events_time_)
115 return;
118 HandleActivity(event);
121 void UserActivityDetector::HandleActivity(const ui::Event* event) {
122 base::TimeTicks now = GetCurrentTime();
123 last_activity_time_ = now;
124 if (last_observer_notification_time_.is_null() ||
125 (now - last_observer_notification_time_).InMillisecondsF() >=
126 kNotifyIntervalMs) {
127 if (VLOG_IS_ON(1))
128 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event);
129 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event));
130 last_observer_notification_time_ = now;
134 } // namespace ui