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