Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: chrome/browser/page_load_metrics/user_input_tracker.cc

Issue 2540183003: Add UserInputTracker, which keeps track of recent user input events. (Closed)
Patch Set: address comment Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include "chrome/browser/page_load_metrics/user_input_tracker.h"
6
7 #include <algorithm>
8
9 #include "third_party/WebKit/public/platform/WebInputEvent.h"
10
11 namespace page_load_metrics {
12
13 namespace {
14
15 // Blink's UserGestureIndicator allows events to be associated with gestures
16 // that are up to 1 second old, based on guidance in the HTML spec:
17 // https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-act ivation.
18 const int kMaxEventAgeSeconds = 1;
19
20 // Allow for up to 2x the oldest time. This allows consumers to continue to
21 // find events for timestamps up to 1 second in the past.
22 const int kOldestAllowedEventAgeSeconds = kMaxEventAgeSeconds * 2;
23
24 // In order to limit to at most kMaxTrackedEvents, we rate limit the recorded
25 // events,
26 // allowing one per rate limit period.
27 const int kRateLimitClampMillis = (kOldestAllowedEventAgeSeconds * 1000) /
28 UserInputTracker::kMaxTrackedEvents;
29
30 bool IsInterestingInputEvent(const blink::WebInputEvent& event) {
31 // Ignore synthesized auto repeat events.
32 if (event.modifiers & blink::WebInputEvent::IsAutoRepeat)
33 return false;
34
35 switch (event.type) {
36 case blink::WebInputEvent::MouseDown:
37 case blink::WebInputEvent::MouseUp:
38 case blink::WebInputEvent::RawKeyDown:
39 case blink::WebInputEvent::KeyDown:
40 case blink::WebInputEvent::Char:
41 case blink::WebInputEvent::TouchStart:
42 case blink::WebInputEvent::TouchEnd:
43 return true;
44 default:
45 return false;
46 }
47 }
48
49 base::TimeTicks GetTimeTicksFromSeconds(double seconds) {
50 // WebInputEvent::timeStampSeconds is a double representing number of
51 // monotonic seconds in TimeTicks time base. There's no convenience API for
52 // initializing a TimeTicks from such a value. The canonical way to perform
53 // this initialization is to create a TimeTicks with value 0 and add a
54 // TimeDelta to it.
55 return base::TimeTicks() + base::TimeDelta::FromSecondsD(seconds);
56 }
57
58 } // namespace
59
60 UserInputTracker::UserInputTracker() {
61 sorted_event_times_.reserve(kMaxTrackedEvents);
62 }
63
64 UserInputTracker::~UserInputTracker() {}
65
66 const size_t UserInputTracker::kMaxTrackedEvents = 100;
67
68 // static
69 base::TimeTicks UserInputTracker::GetEventTime(
70 const blink::WebInputEvent& event) {
71 return GetTimeTicksFromSeconds(event.timeStampSeconds);
72 }
73
74 // static
75 base::TimeTicks UserInputTracker::RoundToRateLimitedOffset(
76 base::TimeTicks time) {
77 base::TimeDelta time_as_delta = time - base::TimeTicks();
78 base::TimeDelta rate_limit_remainder =
79 time_as_delta % base::TimeDelta::FromMilliseconds(kRateLimitClampMillis);
80 return time - rate_limit_remainder;
81 }
82
83 void UserInputTracker::OnInputEvent(const blink::WebInputEvent& event) {
84 RemoveInputEventsUpToInclusive(base::TimeTicks::Now() -
85 GetOldEventThreshold());
86
87 if (!IsInterestingInputEvent(event))
88 return;
89
90 // TODO(bmcquade): ideally we'd limit tracking to events generated by a user
91 // action, as opposed to those generated from JavaScript. The JS API isTrusted
92 // can be used to distinguish these cases. isTrusted isn't yet a property of
93 // WebInputEvent. We should consider adding it.
94
95 const base::TimeTicks now = base::TimeTicks::Now();
96 base::TimeTicks time = RoundToRateLimitedOffset(GetEventTime(event));
97 if (time <=
98 std::max(most_recent_consumed_time_, now - GetOldEventThreshold()))
99 return;
100
101 if (time > now) {
102 DCHECK(!base::TimeTicks::IsHighResolution());
103 return;
104 }
105
106 // lower_bound finds the first element >= |time|.
107 auto it = std::lower_bound(sorted_event_times_.begin(),
108 sorted_event_times_.end(), time);
109 if (it != sorted_event_times_.end() && *it == time) {
110 // Don't insert duplicate values.
111 return;
112 }
113
114 sorted_event_times_.insert(it, time);
115 DCHECK_LE(sorted_event_times_.size(), kMaxTrackedEvents);
116 DCHECK(
117 std::is_sorted(sorted_event_times_.begin(), sorted_event_times_.end()));
118 }
119
120 bool UserInputTracker::FindAndConsumeInputEventsBefore(base::TimeTicks time) {
121 base::TimeTicks recent_input_event_time =
122 FindMostRecentUserInputEventBefore(time);
123
124 if (recent_input_event_time.is_null())
125 return false;
126
127 RemoveInputEventsUpToInclusive(recent_input_event_time);
128 return true;
129 }
130
131 base::TimeTicks UserInputTracker::FindMostRecentUserInputEventBefore(
132 base::TimeTicks time) {
133 RemoveInputEventsUpToInclusive(base::TimeTicks::Now() -
134 GetOldEventThreshold());
135
136 if (sorted_event_times_.empty())
137 return base::TimeTicks();
138
139 // lower_bound finds the first element >= |time|.
140 auto it = std::lower_bound(sorted_event_times_.begin(),
141 sorted_event_times_.end(), time);
142
143 // If all times are after the requested time, then we don't have a time to
144 // return.
145 if (it == sorted_event_times_.begin())
146 return base::TimeTicks();
147
148 // |it| points to the first event >= the specified time, so decrement once to
149 // find the greatest event before the specified time.
150 --it;
151 base::TimeTicks candidate = *it;
152 DCHECK_LT(candidate, time);
153
154 // If the most recent event is too old, then don't return it.
155 if (candidate < time - base::TimeDelta::FromSeconds(kMaxEventAgeSeconds))
156 return base::TimeTicks();
157
158 return candidate;
159 }
160
161 void UserInputTracker::RemoveInputEventsUpToInclusive(base::TimeTicks cutoff) {
162 cutoff = std::max(RoundToRateLimitedOffset(cutoff),
163 base::TimeTicks::Now() - GetOldEventThreshold());
164 most_recent_consumed_time_ = std::max(most_recent_consumed_time_, cutoff);
165 sorted_event_times_.erase(
166 sorted_event_times_.begin(),
167 std::upper_bound(sorted_event_times_.begin(), sorted_event_times_.end(),
168 cutoff));
169 }
170
171 // static
172 base::TimeDelta UserInputTracker::GetOldEventThreshold() {
173 return base::TimeDelta::FromSeconds(kOldestAllowedEventAgeSeconds);
174 }
175
176 } // namespace page_load_metrics
OLDNEW
« no previous file with comments | « chrome/browser/page_load_metrics/user_input_tracker.h ('k') | chrome/browser/page_load_metrics/user_input_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698