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 CHECK(platform_event_source); |
| 56 platform_event_source->AddPlatformEventObserver(this); |
51 } | 57 } |
52 | 58 |
53 UserActivityDetector::~UserActivityDetector() { | 59 UserActivityDetector::~UserActivityDetector() { |
| 60 ui::PlatformEventSource* platform_event_source = |
| 61 ui::PlatformEventSource::GetInstance(); |
| 62 CHECK(platform_event_source); |
| 63 platform_event_source->RemovePlatformEventObserver(this); |
54 g_instance = nullptr; | 64 g_instance = nullptr; |
55 } | 65 } |
56 | 66 |
57 // static | 67 // static |
58 UserActivityDetector* UserActivityDetector::Get() { | 68 UserActivityDetector* UserActivityDetector::Get() { |
59 return g_instance; | 69 return g_instance; |
60 } | 70 } |
61 | 71 |
62 bool UserActivityDetector::HasObserver( | 72 bool UserActivityDetector::HasObserver( |
63 const UserActivityObserver* observer) const { | 73 const UserActivityObserver* observer) const { |
64 return observers_.HasObserver(observer); | 74 return observers_.HasObserver(observer); |
65 } | 75 } |
66 | 76 |
67 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { | 77 void UserActivityDetector::AddObserver(UserActivityObserver* observer) { |
68 observers_.AddObserver(observer); | 78 observers_.AddObserver(observer); |
69 } | 79 } |
70 | 80 |
71 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { | 81 void UserActivityDetector::RemoveObserver(UserActivityObserver* observer) { |
72 observers_.RemoveObserver(observer); | 82 observers_.RemoveObserver(observer); |
73 } | 83 } |
74 | 84 |
75 void UserActivityDetector::OnDisplayPowerChanging() { | 85 void UserActivityDetector::OnDisplayPowerChanging() { |
76 honor_mouse_events_time_ = GetCurrentTime() + | 86 honor_mouse_events_time_ = GetCurrentTime() + |
77 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs); | 87 base::TimeDelta::FromMilliseconds(kDisplayPowerChangeIgnoreMouseMs); |
78 } | 88 } |
79 | 89 |
80 void UserActivityDetector::OnKeyEvent(ui::KeyEvent* event) { | 90 void UserActivityDetector::WillProcessEvent( |
81 HandleActivity(event); | 91 const PlatformEvent& platform_event) { |
82 } | 92 scoped_ptr<ui::Event> event(ui::EventFromNative(platform_event)); |
83 | 93 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 } | 94 } |
105 | 95 |
106 base::TimeTicks UserActivityDetector::GetCurrentTime() const { | 96 base::TimeTicks UserActivityDetector::GetCurrentTime() const { |
107 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); | 97 return !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now(); |
108 } | 98 } |
109 | 99 |
| 100 void UserActivityDetector::ProcessReceivedEvent(const ui::Event* event) { |
| 101 if (!event) |
| 102 return; |
| 103 |
| 104 if (event->IsMouseEvent() || event->IsMouseWheelEvent()) { |
| 105 if (event->flags() & ui::EF_IS_SYNTHESIZED) |
| 106 return; |
| 107 if (!honor_mouse_events_time_.is_null() |
| 108 && GetCurrentTime() < honor_mouse_events_time_) |
| 109 return; |
| 110 } |
| 111 |
| 112 HandleActivity(event); |
| 113 } |
| 114 |
110 void UserActivityDetector::HandleActivity(const ui::Event* event) { | 115 void UserActivityDetector::HandleActivity(const ui::Event* event) { |
111 base::TimeTicks now = GetCurrentTime(); | 116 base::TimeTicks now = GetCurrentTime(); |
112 last_activity_time_ = now; | 117 last_activity_time_ = now; |
113 if (last_observer_notification_time_.is_null() || | 118 if (last_observer_notification_time_.is_null() || |
114 (now - last_observer_notification_time_).InMillisecondsF() >= | 119 (now - last_observer_notification_time_).InMillisecondsF() >= |
115 kNotifyIntervalMs) { | 120 kNotifyIntervalMs) { |
116 if (VLOG_IS_ON(1)) | 121 if (VLOG_IS_ON(1)) |
117 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); | 122 VLOG(1) << "Reporting user activity: " << GetEventDebugString(event); |
118 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); | 123 FOR_EACH_OBSERVER(UserActivityObserver, observers_, OnUserActivity(event)); |
119 last_observer_notification_time_ = now; | 124 last_observer_notification_time_ = now; |
120 } | 125 } |
121 } | 126 } |
122 | 127 |
123 } // namespace ui | 128 } // namespace ui |
OLD | NEW |