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.
5 #ifndef UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
6 #define UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/observer_list.h"
11 #include "base/time/time.h"
12 #include "ui/base/ui_base_export.h"
13 #include "ui/events/event.h"
14 #include "ui/events/platform/platform_event_observer.h"
18 class UserActivityObserver
;
20 // Watches for input events and notifies observers that the user is active.
21 class UI_BASE_EXPORT UserActivityDetector
: public ui::PlatformEventObserver
{
23 // Minimum amount of time between notifications to observers.
24 static const int kNotifyIntervalMs
;
26 // Amount of time that mouse events should be ignored after notification
27 // is received that displays' power states are being changed.
28 static const int kDisplayPowerChangeIgnoreMouseMs
;
30 UserActivityDetector();
31 ~UserActivityDetector() override
;
33 // Returns the UserActivityDetector instance if one was created.
34 static UserActivityDetector
* Get();
36 base::TimeTicks
last_activity_time() const { return last_activity_time_
; }
38 void set_now_for_test(base::TimeTicks now
) { now_for_test_
= now
; }
40 bool HasObserver(const UserActivityObserver
* observer
) const;
41 void AddObserver(UserActivityObserver
* observer
);
42 void RemoveObserver(UserActivityObserver
* observer
);
44 // Called when displays are about to be turned on or off.
45 void OnDisplayPowerChanging();
47 // ui::PlatformEventObserver:
48 void WillProcessEvent(const PlatformEvent
& platform_event
) override
{}
49 void DidProcessEvent(const PlatformEvent
& platform_event
) override
;
52 friend class UserActivityDetectorTest
;
54 // Returns |now_for_test_| if set or base::TimeTicks::Now() otherwise.
55 base::TimeTicks
GetCurrentTime() const;
57 // Processes the event after it has been converted from a PlatformEvent.
58 void ProcessReceivedEvent(const ui::Event
* event
);
60 // Updates |last_activity_time_|. Additionally notifies observers and
61 // updates |last_observer_notification_time_| if enough time has passed
62 // since the last notification.
63 void HandleActivity(const ui::Event
* event
);
65 ObserverList
<UserActivityObserver
> observers_
;
67 // Last time at which user activity was observed.
68 base::TimeTicks last_activity_time_
;
70 // Last time at which we notified observers that the user was active.
71 base::TimeTicks last_observer_notification_time_
;
73 // If set, used when the current time is needed. This can be set by tests to
74 // simulate the passage of time.
75 base::TimeTicks now_for_test_
;
77 // If set, mouse events will be ignored until this time is reached. This
78 // is to avoid reporting mouse events that occur when displays are turned
79 // on or off as user activity.
80 base::TimeTicks honor_mouse_events_time_
;
82 DISALLOW_COPY_AND_ASSIGN(UserActivityDetector
);
87 #endif // UI_BASE_USER_ACTIVITY_USER_ACTIVITY_DETECTOR_H_