OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/user_activity/user_activity_detector.h" | 5 #include "ui/base/user_activity/user_activity_detector.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "ui/base/user_activity/user_activity_observer.h" | 10 #include "ui/base/user_activity/user_activity_observer.h" |
11 #include "ui/events/event_utils.h" | 11 #include "ui/events/event.h" |
12 #include "ui/events/platform/platform_event_source.h" | |
13 | 12 |
14 namespace ui { | 13 namespace ui { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 UserActivityDetector* g_instance = nullptr; | 17 UserActivityDetector* g_instance = nullptr; |
19 | 18 |
20 // Returns a string describing |event|. | 19 // Returns a string describing |event|. |
21 std::string GetEventDebugString(const ui::Event* event) { | 20 std::string GetEventDebugString(const ui::Event* event) { |
22 std::string details = base::StringPrintf( | 21 std::string details = base::StringPrintf( |
(...skipping 19 matching lines...) Expand all Loading... |
42 const int UserActivityDetector::kNotifyIntervalMs = 200; | 41 const int UserActivityDetector::kNotifyIntervalMs = 200; |
43 | 42 |
44 // Too low and mouse events generated at the tail end of reconfiguration | 43 // 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 | 44 // will be reported as user activity and turn the screen back on; too high |
46 // and we'll ignore legitimate activity. | 45 // and we'll ignore legitimate activity. |
47 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000; | 46 const int UserActivityDetector::kDisplayPowerChangeIgnoreMouseMs = 1000; |
48 | 47 |
49 UserActivityDetector::UserActivityDetector() { | 48 UserActivityDetector::UserActivityDetector() { |
50 CHECK(!g_instance); | 49 CHECK(!g_instance); |
51 g_instance = this; | 50 g_instance = this; |
52 | |
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); | |
60 } | 51 } |
61 | 52 |
62 UserActivityDetector::~UserActivityDetector() { | 53 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; | 54 g_instance = nullptr; |
71 } | 55 } |
72 | 56 |
73 // static | 57 // static |
74 UserActivityDetector* UserActivityDetector::Get() { | 58 UserActivityDetector* UserActivityDetector::Get() { |
75 return g_instance; | 59 return g_instance; |
76 } | 60 } |
77 | 61 |
78 bool UserActivityDetector::HasObserver( | 62 bool UserActivityDetector::HasObserver( |
79 const UserActivityObserver* observer) const { | 63 const UserActivityObserver* observer) const { |
80 return observers_.HasObserver(observer); | 64 return observers_.HasObserver(observer); |
81 } | 65 } |
82 | 66 |
83 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { | 67 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { |
84 observers_.AddObserver(observer); | 68 observers_.AddObserver(observer); |
85 } | 69 } |
86 | 70 |
87 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { | 71 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { |
88 observers_.RemoveObserver(observer); | 72 observers_.RemoveObserver(observer); |
89 } | 73 } |
90 | 74 |
91 void UserActivityDetector::OnDisplayPowerChanging() { | 75 void UserActivityDetector::OnDisplayPowerChanging() { |
92 honor_mouse_events_time_ = GetCurrentTime() + | 76 honor_mouse_events_time_ = GetCurrentTime() + |
93 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs); | 77 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs); |
94 } | 78 } |
95 | 79 |
96 void UserActivityDetector::WillProcessEvent( | 80 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) { |
97 const PlatformEvent& platform_event) { | 81 HandleActivity(event); |
98 scoped_ptr<ui::Event> event(ui::EventFromNative(platform_event)); | 82 } |
99 ProcessReceivedEvent(event.get()); | 83 |
| 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; |
| 90 |
| 91 HandleActivity(event); |
| 92 } |
| 93 |
| 94 void UserActivityDetector::OnScrollEvent(ui::ScrollEvent* event) { |
| 95 HandleActivity(event); |
| 96 } |
| 97 |
| 98 void UserActivityDetector::OnTouchEvent(ui::TouchEvent* event) { |
| 99 HandleActivity(event); |
| 100 } |
| 101 |
| 102 void UserActivityDetector::OnGestureEvent(ui::GestureEvent* event) { |
| 103 HandleActivity(event); |
100 } | 104 } |
101 | 105 |
102 base::TimeTicks UserActivityDetector::GetCurrentTime() const { | 106 base::TimeTicks UserActivityDetector::GetCurrentTime() const { |
103 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); | 107 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); |
104 } | 108 } |
105 | 109 |
106 void UserActivityDetector::ProcessReceivedEvent(const ui::Event* event) { | |
107 if (!event) | |
108 return; | |
109 | |
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; | |
116 } | |
117 | |
118 HandleActivity(event); | |
119 } | |
120 | |
121 void UserActivityDetector::HandleActivity(const ui::Event* event) { | 110 void UserActivityDetector::HandleActivity(const ui::Event* event) { |
122 base::TimeTicks now = GetCurrentTime(); | 111 base::TimeTicks now = GetCurrentTime(); |
123 last_activity_time_ = now; | 112 last_activity_time_ = now; |
124 if (last_observer_notification_time_.is_null() || | 113 if (last_observer_notification_time_.is_null() || |
125 (now - last_observer_notification_time_).InMillisecondsF() >= | 114 (now - last_observer_notification_time_).InMillisecondsF() >= |
126 kNotifyIntervalMs) { | 115 kNotifyIntervalMs) { |
127 if (VLOG_IS_ON(1)) | 116 if (VLOG_IS_ON(1)) |
128 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); | 117 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); |
129 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); | 118 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); |
130 last_observer_notification_time_ = now; | 119 last_observer_notification_time_ = now; |
131 } | 120 } |
132 } | 121 } |
133 | 122 |
134 } // namespace ui | 123 } // namespace ui |
OLD | NEW |