| OLD | NEW |
| (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 #ifndef CHROME_BROWSER_PAGE_LOAD_METRICS_USER_INPUT_TRACKER_H_ |
| 6 #define CHROME_BROWSER_PAGE_LOAD_METRICS_USER_INPUT_TRACKER_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/time/time.h" |
| 12 |
| 13 namespace blink { |
| 14 class WebInputEvent; |
| 15 } // namespace blink |
| 16 |
| 17 namespace page_load_metrics { |
| 18 |
| 19 // UserInputTracker keeps track of user input events processed by web pages, and |
| 20 // allows clients to find and consume those input events. This allows us to |
| 21 // heuristically attribute user input events to navigations, in order to keep |
| 22 // track of which page loads and aborts were initiated by a user action. |
| 23 // |
| 24 // There are issues with the existing user gesture tracking in Blink and content |
| 25 // that make it unsuitable for our needs. For example, Blink considers events |
| 26 // such as navigations that occur within 1 second of a user action event to have |
| 27 // been initiated by a user action, based on the HTML spec |
| 28 // (https://html.spec.whatwg.org/multipage/interaction.html#triggered-by-user-ac
tivation). |
| 29 // This can be problematic in cases where a web page issues many navigations in |
| 30 // rapid succession, e.g. JS code that dispatches new navigation requests in a |
| 31 // tight loop can result in dozens of programmatically generated navigations |
| 32 // being user initiated. |
| 33 // |
| 34 // Note that UserInputTracker does not keep track of input events processed by |
| 35 // the browser, such as interactions with the Chrome browser UI (e.g. clicking |
| 36 // the 'reload' button). |
| 37 class UserInputTracker { |
| 38 public: |
| 39 // Only public for tests. |
| 40 static const size_t kMaxTrackedEvents; |
| 41 static base::TimeTicks GetEventTime(const blink::WebInputEvent& event); |
| 42 |
| 43 // Given a time, round to the nearest rate-limited offset. UserInputTracker |
| 44 // rate limits events, such that at most one event will be recorded per every |
| 45 // 20ms. RoundToRateLimitedOffset round a TimeTicks down to its nearest whole |
| 46 // 20ms. |
| 47 static base::TimeTicks RoundToRateLimitedOffset(base::TimeTicks time); |
| 48 |
| 49 UserInputTracker(); |
| 50 ~UserInputTracker(); |
| 51 |
| 52 void OnInputEvent(const blink::WebInputEvent& event); |
| 53 |
| 54 // Attempts to find the most recent user input event before the given time, |
| 55 // and, if that input event exists, consumes all events up to that |
| 56 // event. Returns whether an input event before the given time was found and |
| 57 // consumed. |
| 58 bool FindAndConsumeInputEventsBefore(base::TimeTicks time); |
| 59 |
| 60 // Finds the time of the most recent user input event before the given time, |
| 61 // or a null TimeTicks if there are no user input events before the given |
| 62 // time. Consumers of this class should use |
| 63 // FindAndConsumeInputEventsBefore. This method is public only for testing. |
| 64 base::TimeTicks FindMostRecentUserInputEventBefore(base::TimeTicks time); |
| 65 |
| 66 private: |
| 67 void RemoveInputEventsUpToInclusive(base::TimeTicks cutoff); |
| 68 |
| 69 static base::TimeDelta GetOldEventThreshold(); |
| 70 |
| 71 std::vector<base::TimeTicks> sorted_event_times_; |
| 72 base::TimeTicks most_recent_consumed_time_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(UserInputTracker); |
| 75 }; |
| 76 |
| 77 } // namespace page_load_metrics |
| 78 |
| 79 #endif // CHROME_BROWSER_PAGE_LOAD_METRICS_USER_INPUT_TRACKER_H_ |
| OLD | NEW |