Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / user_activity / user_activity_detector.cc
blobd6a4e4385278ab04012bdd158f96f62f1a6b335e
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.h"
13 namespace ui {
15 namespace {
17 UserActivityDetector* g_instance = nullptr;
19 // Returns a string describing |event|.
20 std::string GetEventDebugString(const ui::Event* event) {
21 std::string details = base::StringPrintf(
22 "type=%d name=%s flags=%d time=%" PRId64,
23 event->type(), event->name().c_str(), event->flags(),
24 event->time_stamp().InMilliseconds());
26 if (event->IsKeyEvent()) {
27 details += base::StringPrintf(" key_code=%d",
28 static_cast<const ui::KeyEvent*>(event)->key_code());
29 } else if (event->IsMouseEvent() || event->IsTouchEvent() ||
30 event->IsGestureEvent()) {
31 details += base::StringPrintf(" location=%s",
32 static_cast<const ui::LocatedEvent*>(
33 event)->location().ToString().c_str());
36 return details;
39 } // namespace
41 const int UserActivityDetector::kNotifyIntervalMs = 200;
43 // Too low and mouse events generated at the tail end of reconfiguration
44 // will be reported as user activity and turn the screen back on; too high
45 // and we'll ignore legitimate activity.
46 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000;
48 UserActivityDetector::UserActivityDetector() {
49 CHECK(!g_instance);
50 g_instance = this;
53 UserActivityDetector::~UserActivityDetector() {
54 g_instance = nullptr;
57 // static
58 UserActivityDetector* UserActivityDetector::Get() {
59 return g_instance;
62 bool UserActivityDetector::HasObserver(
63 const UserActivityObserver* observer) const {
64 return observers_.HasObserver(observer);
67 void UserActivityDetector::AddObserver(UserActivityObserver* observer) {
68 observers_.AddObserver(observer);
71 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) {
72 observers_.RemoveObserver(observer);
75 void UserActivityDetector::OnDisplayPowerChanging() {
76 honor_mouse_events_time_ = GetCurrentTime() +
77 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs);
80 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) {
81 HandleActivity(event);
84 void UserActivityDetector::OnMouseEvent(ui::MouseEvent* event) {
85 if (event->flags() & ui::EF_IS_SYNTHESIZED)
86 return;
87 if (!honor_mouse_events_time_.is_null() &&
88 GetCurrentTime() < honor_mouse_events_time_)
89 return;
91 HandleActivity(event);
94 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) {
95 HandleActivity(event);
98 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) {
99 HandleActivity(event);
102 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) {
103 HandleActivity(event);
106 base::TimeTicks UserActivityDetector::GetCurrentTime() const {
107 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
110 void UserActivityDetector::HandleActivity(const ui::Event* event) {
111 base::TimeTicks now = GetCurrentTime();
112 last_activity_time_ = now;
113 if (last_observer_notification_time_.is_null() ||
114 (now - last_observer_notification_time_).InMillisecondsF() >=
115 kNotifyIntervalMs) {
116 if (VLOG_IS_ON(1))
117 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event);
118 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event));
119 last_observer_notification_time_ = now;
123 } // namespace ui