File: ntp_user_data_logger.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,647 bytes parent folder | download | duplicates (6)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_SEARCH_NTP_USER_DATA_LOGGER_H_
#define CHROME_BROWSER_UI_SEARCH_NTP_USER_DATA_LOGGER_H_

#include <stddef.h>

#include <array>
#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/search/ntp_logging_events.h"
#include "components/ntp_tiles/constants.h"
#include "components/ntp_tiles/ntp_tile_impression.h"

#if BUILDFLAG(IS_ANDROID)
#error "Instant is only used on desktop";
#endif

// Helper class for logging data from the NTP. Attached to each NTP instance.
class NTPUserDataLogger {
 public:
  // Creates a NTPUserDataLogger. MUST be called only when the NTP is active.
  NTPUserDataLogger(Profile* profile,
                    const GURL& ntp_url,
                    base::Time ntp_navigation_start_time);

  NTPUserDataLogger(const NTPUserDataLogger&) = delete;
  NTPUserDataLogger& operator=(const NTPUserDataLogger&) = delete;

  virtual ~NTPUserDataLogger();

  // Called when a One Google Bar fetch has been completed after |duration|.
  // |success| is true if the fetch was successful.
  static void LogOneGoogleBarFetchDuration(bool success,
                                           const base::TimeDelta& duration);

  // Called when an event occurs on the NTP that requires a counter to be
  // incremented. |time| is the delta time from navigation start until this
  // event happened. The NTP_ALL_TILES_LOADED event may be logged from all NTPs;
  // all others require Google as the default search provider.
  void LogEvent(NTPLoggingEventType event, base::TimeDelta time);

  // Called when all NTP tiles have finished loading (successfully or failing).
  void LogMostVisitedLoaded(base::TimeDelta time,
                            bool using_most_visited,
                            bool is_visible);

  // Logs an impression on one of the NTP tiles by given details.
  void LogMostVisitedImpression(const ntp_tiles::NTPTileImpression& impression);

  // Logs a navigation on one of the NTP tiles by a given impression.
  void LogMostVisitedNavigation(const ntp_tiles::NTPTileImpression& impression);

 private:
  // Returns whether Google is selected as the default search engine. Virtual
  // for testing.
  virtual bool DefaultSearchProviderIsGoogle() const;

  // Returns whether a custom background is configured. Virtual for testing.
  virtual bool CustomBackgroundIsConfigured() const;

  // Logs a number of statistics regarding the NTP. Called when an NTP tab is
  // about to be deactivated (be it by switching tabs, losing focus or closing
  // the tab/shutting down Chrome), or when the user navigates to a URL.
  void EmitNtpStatistics(base::TimeDelta load_time,
                         bool using_most_visited,
                         bool is_visible);

  void EmitNtpTraceEvent(const char* event_name, base::TimeDelta duration);

  void RecordDoodleImpression(base::TimeDelta time,
                              bool is_cta,
                              bool from_cache);

  // Logs the user |action| via base::RecordAction.
  void RecordAction(const char* action);

  // Records whether we have yet logged an impression for the tile at a given
  // index and if so the corresponding details. A typical NTP will log 9
  // impressions, but could record fewer for new users that haven't built up a
  // history yet. If the user has customized their shortcuts, this number can
  // increase up to 10 impressions.
  //
  // If something happens that causes the NTP to pull tiles from different
  // sources, such as signing in (switching from client to server tiles), then
  // only the impressions for the first source will be logged, leaving the
  // number of impressions for a source slightly out-of-sync with navigations.
  std::array<std::optional<ntp_tiles::NTPTileImpression>,
             ntp_tiles::kMaxNumTiles>
      logged_impressions_;

  // Whether we have already emitted NTP stats for this web contents.
  bool has_emitted_ = false;

  bool should_record_doodle_load_time_ = true;

  // Are stats being logged during Chrome startup?
  bool during_startup_;

  // The URL of this New Tab Page - varies based on NTP version.
  GURL ntp_url_;

  // The profile in which this New Tab Page was loaded.
  raw_ptr<Profile> profile_;

  // Keeps the starting time of NTP navigation.
  const base::TimeTicks ntp_navigation_start_time_;
};

#endif  // CHROME_BROWSER_UI_SEARCH_NTP_USER_DATA_LOGGER_H_