File: user_input_tracker.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (79 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
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
// 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/macros.h"
#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:
  // Only public for tests.
  static const size_t kMaxTrackedEvents;
  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 RemoveInputEventsUpToInclusive(base::TimeTicks cutoff);

  static base::TimeDelta GetOldEventThreshold();

  std::vector<base::TimeTicks> 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_