File: breadcrumb_manager_tab_helper.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (203 lines) | stat: -rw-r--r-- 6,875 bytes parent folder | download | duplicates (8)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/breadcrumbs/core/breadcrumb_manager_tab_helper.h"

#include "base/format_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_delegate.h"
#include "components/infobars/core/infobar_manager.h"
#include "net/base/net_errors.h"

namespace breadcrumbs {

namespace {

// Returns true if navigation URL host is google.com or www.google.com.
bool IsGoogleUrl(const GURL& url) {
  return url.host() == "google.com" || url.host() == "www.google.com";
}

}  // namespace

const char kBreadcrumbDidStartNavigation[] = "StartNav";
const char kBreadcrumbDidFinishNavigation[] = "FinishNav";
const char kBreadcrumbPageLoaded[] = "PageLoad";
const char kBreadcrumbDidChangeVisibleSecurityState[] = "SecurityChange";

const char kBreadcrumbInfobarAdded[] = "AddInfobar";
const char kBreadcrumbInfobarRemoved[] = "RemoveInfobar";
const char kBreadcrumbInfobarReplaced[] = "ReplaceInfobar";

const char kBreadcrumbScroll[] = "Scroll";
const char kBreadcrumbZoom[] = "Zoom";

const char kBreadcrumbAuthenticationBroken[] = "#broken";
const char kBreadcrumbDownload[] = "#download";
const char kBreadcrumbMixedContent[] = "#mixed";
const char kBreadcrumbInfobarNotAnimated[] = "#not-animated";
const char kBreadcrumbNtpNavigation[] = "#ntp";
const char kBreadcrumbGoogleNavigation[] = "#google";
const char kBreadcrumbPdfLoad[] = "#pdf";
const char kBreadcrumbPageLoadFailure[] = "#failure";
const char kBreadcrumbRendererInitiatedByUser[] = "#renderer-user";
const char kBreadcrumbRendererInitiatedByScript[] = "#renderer-script";

BreadcrumbManagerTabHelper::BreadcrumbManagerTabHelper(
    infobars::InfoBarManager* infobar_manager) {
  static int next_unique_id = 1;
  unique_id_ = next_unique_id++;

  infobar_manager_ = infobar_manager;
  infobar_observation_.Observe(infobar_manager_.get());
}

BreadcrumbManagerTabHelper::~BreadcrumbManagerTabHelper() = default;

void BreadcrumbManagerTabHelper::LogDidStartNavigation(
    int64_t navigation_id,
    GURL url,
    bool is_ntp_url,
    bool is_renderer_initiated,
    bool has_user_gesture,
    ui::PageTransition page_transition) {
  std::vector<std::string> event = {
      base::StringPrintf("%s%" PRIu64, kBreadcrumbDidStartNavigation,
                         navigation_id),
  };

  if (is_ntp_url) {
    event.push_back(kBreadcrumbNtpNavigation);
  } else if (IsGoogleUrl(url)) {
    event.push_back(kBreadcrumbGoogleNavigation);
  }

  if (is_renderer_initiated) {
    if (has_user_gesture) {
      event.push_back(kBreadcrumbRendererInitiatedByUser);
    } else {
      event.push_back(kBreadcrumbRendererInitiatedByScript);
    }
  }

  event.push_back(base::StringPrintf(
      "#%s", ui::PageTransitionGetCoreTransitionString(page_transition)));

  LogEvent(base::JoinString(event, " "));
}

void BreadcrumbManagerTabHelper::LogDidFinishNavigation(int64_t navigation_id,
                                                        bool is_download,
                                                        int error_code) {
  std::vector<std::string> event = {
      base::StringPrintf("%s%" PRIu64,
                         breadcrumbs::kBreadcrumbDidFinishNavigation,
                         navigation_id),
  };
  if (is_download)
    event.push_back(breadcrumbs::kBreadcrumbDownload);
  if (error_code)
    event.push_back(net::ErrorToShortString(error_code));
  LogEvent(base::JoinString(event, " "));
}

void BreadcrumbManagerTabHelper::LogPageLoaded(
    bool is_ntp_url,
    GURL url,
    bool page_load_success,
    const std::string& contents_mime_type) {
  std::vector<std::string> event = {breadcrumbs::kBreadcrumbPageLoaded};

  if (is_ntp_url) {
    // NTP load can't fail, so there is no need to report success/failure.
    event.push_back(breadcrumbs::kBreadcrumbNtpNavigation);
  } else {
    if (IsGoogleUrl(url)) {
      event.push_back(breadcrumbs::kBreadcrumbGoogleNavigation);
    }

    if (page_load_success) {
      if (contents_mime_type == "application/pdf") {
        event.push_back(breadcrumbs::kBreadcrumbPdfLoad);
      }
    } else {
      event.push_back(breadcrumbs::kBreadcrumbPageLoadFailure);
    }
  }

  LogEvent(base::JoinString(event, " "));
}

void BreadcrumbManagerTabHelper::LogDidChangeVisibleSecurityState(
    bool displayed_mixed_content,
    bool security_style_authentication_broken) {
  std::vector<std::string> event;
  if (displayed_mixed_content)
    event.push_back(breadcrumbs::kBreadcrumbMixedContent);
  if (security_style_authentication_broken)
    event.push_back(breadcrumbs::kBreadcrumbAuthenticationBroken);

  if (!event.empty()) {
    event.insert(event.begin(),
                 breadcrumbs::kBreadcrumbDidChangeVisibleSecurityState);
    LogEvent(base::JoinString(event, " "));
  }
}

void BreadcrumbManagerTabHelper::LogRenderProcessGone() {
  LogEvent("RenderProcessGone");
}

void BreadcrumbManagerTabHelper::LogEvent(const std::string& event) {
  PlatformLogEvent(
      base::StringPrintf("Tab%d %s", GetUniqueId(), event.c_str()));
}

bool BreadcrumbManagerTabHelper::ShouldLogRepeatedEvent(int count) {
  return count == 1 || count == 2 || count == 5 || count == 20 ||
         count == 100 || count == 200;
}

void BreadcrumbManagerTabHelper::OnInfoBarAdded(infobars::InfoBar* infobar) {
  sequentially_replaced_infobars_ = 0;
  LogEvent(base::StringPrintf("%s%d", kBreadcrumbInfobarAdded,
                              infobar->GetIdentifier()));
}

void BreadcrumbManagerTabHelper::OnInfoBarRemoved(infobars::InfoBar* infobar,
                                                  bool animate) {
  sequentially_replaced_infobars_ = 0;
  std::vector<std::string> event = {
      base::StringPrintf("%s%d", kBreadcrumbInfobarRemoved,
                         infobar->GetIdentifier()),
  };
  if (!animate)
    event.push_back(kBreadcrumbInfobarNotAnimated);
  LogEvent(base::JoinString(event, " "));
}

void BreadcrumbManagerTabHelper::OnInfoBarReplaced(
    infobars::InfoBar* old_infobar,
    infobars::InfoBar* new_infobar) {
  sequentially_replaced_infobars_++;

  if (ShouldLogRepeatedEvent(sequentially_replaced_infobars_)) {
    LogEvent(base::StringPrintf("%s%d %d", kBreadcrumbInfobarReplaced,
                                new_infobar->GetIdentifier(),
                                sequentially_replaced_infobars_));
  }
}

void BreadcrumbManagerTabHelper::OnManagerShuttingDown(
    infobars::InfoBarManager* manager) {
  DCHECK_EQ(infobar_manager_, manager);
  DCHECK(infobar_observation_.IsObservingSource(manager));
  infobar_observation_.Reset();
  infobar_manager_ = nullptr;
  sequentially_replaced_infobars_ = 0;
}

}  // namespace breadcrumbs