File: user_activity_detector.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 3,907 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
#define UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_

#include "base/component_export.h"
#include "base/no_destructor.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "ui/events/event.h"
#include "ui/events/platform/platform_event_observer.h"

namespace ui {

class UserActivityObserver;

// Watches for input events and notifies observers that the user is active.
class COMPONENT_EXPORT(UI_BASE) UserActivityDetector
    : public PlatformEventObserver {
 public:
  // Minimum amount of time between notifications to observers.
  static const int kNotifyIntervalMs;

  // Amount of time that mouse events should be ignored after notification
  // is received that displays' power states are being changed.
  static const int kDisplayPowerChangeIgnoreMouseMs;

  UserActivityDetector(const UserActivityDetector&) = delete;
  UserActivityDetector& operator=(const UserActivityDetector&) = delete;

  // Returns the UserActivityDetector instance.
  static UserActivityDetector* Get();

  base::TimeTicks last_activity_time() const { return last_activity_time_; }
  std::string last_activity_name() const { return last_activity_name_; }

  void set_now_for_test(base::TimeTicks now) { now_for_test_ = now; }
  void set_last_activity_time_for_test(base::TimeTicks value) {
    last_activity_time_ = value;
  }

  bool HasObserver(const UserActivityObserver* observer) const;
  void AddObserver(UserActivityObserver* observer);
  void RemoveObserver(UserActivityObserver* observer);

  // Called when displays are about to be turned on or off.
  void OnDisplayPowerChanging();

  // Handles reports of user activity originating from outside of
  // PlatformEventSource (e.g. the window server).
  void HandleExternalUserActivity();

  // PlatformEventObserver:
  void WillProcessEvent(const PlatformEvent& platform_event) override {}
  void DidProcessEvent(const PlatformEvent& platform_event) override;
  void PlatformEventSourceDestroying() override;

  void ResetStateForTesting();
  void InitPlatformEventSourceObservationForTesting();

 private:
  friend class base::NoDestructor<UserActivityDetector>;
  friend class UserActivityDetectorTest;

  UserActivityDetector();
  ~UserActivityDetector() override;

  // Sets up the observation over the PlatformEventSource. The event source
  // must have been constructed before this is called.
  void InitPlatformEventSourceObservation();

  // Returns |now_for_test_| if set or base::TimeTicks::Now() otherwise.
  base::TimeTicks GetCurrentTime() const;

  // Processes the event after it has been converted from a PlatformEvent.
  void ProcessReceivedEvent(const ui::Event* event);

  // Updates |last_activity_time_|.  Additionally notifies observers and
  // updates |last_observer_notification_time_| if enough time has passed
  // since the last notification.
  void HandleActivity(const ui::Event* event);

  base::ObserverList<UserActivityObserver>::Unchecked observers_;

  // Last time at which user activity was observed.
  base::TimeTicks last_activity_time_;

  // Name of the last user activity event.
  std::string last_activity_name_;

  // Last time at which we notified observers that the user was active.
  base::TimeTicks last_observer_notification_time_;

  // If set, used when the current time is needed.  This can be set by tests to
  // simulate the passage of time.
  base::TimeTicks now_for_test_;

  // If set, mouse events will be ignored until this time is reached. This
  // is to avoid reporting mouse events that occur when displays are turned
  // on or off as user activity.
  base::TimeTicks honor_mouse_events_time_;
};

}  // namespace ui

#endif  // UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_