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