user_activity_detector.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.
  4. #ifndef UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
  5. #define UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
  6. #include "base/component_export.h"
  7. #include "base/observer_list.h"
  8. #include "base/time/time.h"
  9. #include "ui/events/event.h"
  10. #include "ui/events/platform/platform_event_observer.h"
  11. namespace ui {
  12. class UserActivityObserver;
  13. // Watches for input events and notifies observers that the user is active.
  14. class COMPONENT_EXPORT(UI_BASE) UserActivityDetector
  15. : public PlatformEventObserver {
  16. public:
  17. // Minimum amount of time between notifications to observers.
  18. static const int kNotifyIntervalMs;
  19. // Amount of time that mouse events should be ignored after notification
  20. // is received that displays' power states are being changed.
  21. static const int kDisplayPowerChangeIgnoreMouseMs;
  22. UserActivityDetector();
  23. UserActivityDetector(const UserActivityDetector&) = delete;
  24. UserActivityDetector& operator=(const UserActivityDetector&) = delete;
  25. ~UserActivityDetector() override;
  26. // Returns the UserActivityDetector instance if one was created.
  27. static UserActivityDetector* Get();
  28. base::TimeTicks last_activity_time() const { return last_activity_time_; }
  29. std::string last_activity_name() const { return last_activity_name_; }
  30. void set_now_for_test(base::TimeTicks now) { now_for_test_ = now; }
  31. void set_last_activity_time_for_test(base::TimeTicks value) {
  32. last_activity_time_ = value;
  33. }
  34. bool HasObserver(const UserActivityObserver* observer) const;
  35. void AddObserver(UserActivityObserver* observer);
  36. void RemoveObserver(UserActivityObserver* observer);
  37. // Called when displays are about to be turned on or off.
  38. void OnDisplayPowerChanging();
  39. // Handles reports of user activity originating from outside of
  40. // PlatformEventSource (e.g. the window server).
  41. void HandleExternalUserActivity();
  42. // PlatformEventObserver:
  43. void WillProcessEvent(const PlatformEvent& platform_event) override {}
  44. void DidProcessEvent(const PlatformEvent& platform_event) override;
  45. private:
  46. friend class UserActivityDetectorTest;
  47. // Returns |now_for_test_| if set or base::TimeTicks::Now() otherwise.
  48. base::TimeTicks GetCurrentTime() const;
  49. // Processes the event after it has been converted from a PlatformEvent.
  50. void ProcessReceivedEvent(const ui::Event* event);
  51. // Updates |last_activity_time_|. Additionally notifies observers and
  52. // updates |last_observer_notification_time_| if enough time has passed
  53. // since the last notification.
  54. void HandleActivity(const ui::Event* event);
  55. base::ObserverList<UserActivityObserver>::Unchecked observers_;
  56. // Last time at which user activity was observed.
  57. base::TimeTicks last_activity_time_;
  58. // Name of the last user activity event.
  59. std::string last_activity_name_;
  60. // Last time at which we notified observers that the user was active.
  61. base::TimeTicks last_observer_notification_time_;
  62. // If set, used when the current time is needed. This can be set by tests to
  63. // simulate the passage of time.
  64. base::TimeTicks now_for_test_;
  65. // If set, mouse events will be ignored until this time is reached. This
  66. // is to avoid reporting mouse events that occur when displays are turned
  67. // on or off as user activity.
  68. base::TimeTicks honor_mouse_events_time_;
  69. };
  70. } // namespace ui
  71. #endif // UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_