Chromium Code Reviews| Index: chrome/browser/page_load_metrics/user_input_tracker.h |
| diff --git a/chrome/browser/page_load_metrics/user_input_tracker.h b/chrome/browser/page_load_metrics/user_input_tracker.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d3354490a960129622c6d755cabe4bd602b369d6 |
| --- /dev/null |
| +++ b/chrome/browser/page_load_metrics/user_input_tracker.h |
| @@ -0,0 +1,78 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_USER_INPUT_TRACKER_H_ |
| +#define CHROME_BROWSER_PAGE_LOAD_METRICS_USER_INPUT_TRACKER_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/time/time.h" |
| + |
| +namespace blink { |
| +class WebInputEvent; |
| +} // namespace blink |
| + |
| +namespace page_load_metrics { |
| + |
| +// UserInputTracker keeps track of user input events processed by web pages, and |
| +// allows clients to find and consume those input events. This allows us to |
| +// heuristically attribute user input events to navigations, in order to keep |
| +// track of which page loads and aborts were initiated by a user action. |
| +// |
| +// There are issues with the existing user gesture tracking in Blink and content |
| +// that make it unsuitable for our needs. For example, Blink considers events |
| +// such as navigations that occur within 1 second of a user action event to have |
| +// been initiated by a user action, based on the HTML spec |
| +// (https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-activation). |
| +// This can be problematic in cases where a web page issues many navigations in |
| +// rapid succession, e.g. JS code that dispatches new navigation requests in a |
| +// tight loop can result in dozens of programmatically generated navigations |
| +// being user initiated. |
| +// |
| +// Note that UserInputTracker does not keep track of input events processed by |
| +// the browser, such as interactions with the Chrome browser UI (e.g. clicking |
| +// the 'reload' button). |
| +class UserInputTracker { |
| + public: |
| + static const size_t kMaxEntries; |
|
Charlie Harrison
2016/12/05 19:17:46
s/kMaxEntries/kMaxTrackedEvents or something simil
Bryan McQuade
2016/12/06 01:52:25
Sure, renamed. This is only public for tests.
|
| + |
| + static base::TimeTicks GetEventTime(const blink::WebInputEvent& event); |
| + |
| + // Given a time, round to the nearest rate-limited offset. UserInputTracker |
| + // rate limits events, such that at most one event will be recorded per every |
| + // 20ms. RoundToRateLimitedOffset round a TimeTicks down to its nearest whole |
| + // 20ms. |
| + static base::TimeTicks RoundToRateLimitedOffset(base::TimeTicks time); |
| + |
| + UserInputTracker(); |
| + ~UserInputTracker(); |
| + |
| + void OnInputEvent(const blink::WebInputEvent& event); |
| + |
| + // Attempts to find the most recent user input event before the given time, |
| + // and, if that input event exists, consumes all events up to that |
| + // event. Returns whether an input event before the given time was found and |
| + // consumed. |
| + bool FindAndConsumeInputEventsBefore(base::TimeTicks time); |
| + |
| + // Finds the time of the most recent user input event before the given time, |
| + // or a null TimeTicks if there are no user input events before the given |
| + // time. Consumers of this class should use |
| + // FindAndConsumeInputEventsBefore. This method is public only for testing. |
| + base::TimeTicks FindMostRecentUserInputEventBefore(base::TimeTicks time); |
| + |
| + private: |
| + void RemoveInputEventsUpTo(base::TimeTicks cutoff); |
| + void RemoveOldInputEvents(); |
|
Charlie Harrison
2016/12/05 19:17:46
This one might need some documentation. What is "O
Bryan McQuade
2016/12/06 01:52:25
Done. It's somewhat of an implementation detail, b
|
| + static base::TimeTicks GetOldestAllowedEventTime(); |
| + |
| + std::vector<base::TimeTicks> event_times_; |
|
Charlie Harrison
2016/12/05 19:17:46
Please document that this vector should always be
Bryan McQuade
2016/12/06 01:52:25
Renamed to sorted_event_times_
|
| + base::TimeTicks most_recent_consumed_time_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(UserInputTracker); |
| +}; |
| + |
| +} // namespace page_load_metrics |
| + |
| +#endif // CHROME_BROWSER_PAGE_LOAD_METRICS_USER_INPUT_TRACKER_H_ |