File: omnibox_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 (207 lines) | stat: -rw-r--r-- 7,578 bytes parent folder | download | duplicates (2)
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
204
205
206
207
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/omnibox/omnibox_tab_helper.h"

#include <optional>
#include <string>
#include <string_view>

#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "chrome/browser/page_content_annotations/page_content_extraction_service.h"
#include "chrome/browser/page_content_annotations/page_content_extraction_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "components/omnibox/browser/lens_suggest_inputs_utils.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "components/omnibox/common/omnibox_feature_configs.h"
#include "content/public/browser/render_frame_host.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"

WEB_CONTENTS_USER_DATA_KEY_IMPL(OmniboxTabHelper);

namespace {

constexpr char kNavigationToPopupShownHistogramPrefix[] =
    "Omnibox.NavigationToPopupShown";
constexpr char kMainDocumentElementAvailableHistogramSuffix[] =
    "MainDocumentElementAvailable";
constexpr char kPrimaryPageChangedHistogramSuffix[] = "PrimaryPageChanged";
constexpr char kDomContentLoadedHistogramSuffix[] = "DomContentLoaded";
constexpr char kByPageContextHistogramPrefix[] = "ByPageContext";

void LogNavigationToPopupUma(std::string_view event_name,
                             std::string_view page_context,
                             base::TimeDelta time_to_log) {
  // Custom buckets from 1 millisecond to 1 minute, with 60 buckets in total.
  // Meaning each bucket is 1 second wide.
  base::UmaHistogramCustomTimes(
      base::StrCat({kNavigationToPopupShownHistogramPrefix, ".", event_name}),
      time_to_log, base::Milliseconds(0), base::Seconds(60), 60);
  base::UmaHistogramCustomTimes(
      base::StrCat({kNavigationToPopupShownHistogramPrefix, ".", event_name,
                    ".", kByPageContextHistogramPrefix, ".", page_context}),
      time_to_log, base::Milliseconds(0), base::Seconds(60), 60);
}

PaywallSignal ToPaywallSignal(std::optional<bool> paywall_signal) {
  if (paywall_signal.has_value()) {
    // If `paywall_signal` is available and true, it means the page is paywalled
    // and contextual suggestions should not be shown.
    return paywall_signal.value() ? PaywallSignal::kSignalPresent
                                  : PaywallSignal::kSignalNotPresent;
  }
  // Finally, if no signal was extracted from the page, then the signal is
  // unavailable due to missing page content.
  return PaywallSignal::kUnknown;
}

}  // namespace

OmniboxTabHelper::~OmniboxTabHelper() = default;
OmniboxTabHelper::OmniboxTabHelper(content::WebContents* contents,
                                   Profile* profile)
    : content::WebContentsUserData<OmniboxTabHelper>(*contents),
      content::WebContentsObserver(contents) {
  // Only fetch the APC paywall signal if the feature flag is enabled.
  if (omnibox_feature_configs::ContextualSearch::Get().use_apc_paywall_signal) {
    if (auto* service = page_content_annotations::
            PageContentExtractionServiceFactory::GetForProfile(profile)) {
      page_content_service_observation_.Observe(service);
    }
  }
}

void OmniboxTabHelper::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void OmniboxTabHelper::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

void OmniboxTabHelper::OnInputStateChanged() {
  for (auto& observer : observers_) {
    observer.OnOmniboxInputStateChanged();
  }
}

void OmniboxTabHelper::OnInputInProgress(bool in_progress) {
  for (auto& observer : observers_) {
    observer.OnOmniboxInputInProgress(in_progress);
  }
}

void OmniboxTabHelper::OnFocusChanged(OmniboxFocusState state,
                                      OmniboxFocusChangeReason reason) {
  for (auto& observer : observers_) {
    observer.OnOmniboxFocusChanged(state, reason);
  }
}

void OmniboxTabHelper::OnPopupVisibilityChanged(
    bool popup_is_open,
    metrics::OmniboxEventProto::PageClassification page_classification) {
  for (auto& observer : observers_) {
    observer.OnOmniboxPopupVisibilityChanged(popup_is_open);
  }

  if (popup_is_open) {
    MaybeLogPaywallSignal();
    MaybeLogNavigationToPopupShownTimings(page_classification);
  }
}

std::optional<bool> OmniboxTabHelper::IsPagePaywalled() {
  return page_has_apc_paywall_signal_;
}

void OmniboxTabHelper::OnPageContentExtracted(
    content::Page& page,
    const optimization_guide::proto::AnnotatedPageContent& page_content) {
  // Ignore if the APC does not belong to the primary page of this tabs web
  // contents.
  if (&page != &(GetWebContents().GetPrimaryPage())) {
    return;
  }
  page_has_apc_paywall_signal_ =
      page_content.has_main_frame_data() &&
      page_content.main_frame_data().has_paid_content_metadata() &&
      page_content.main_frame_data()
          .paid_content_metadata()
          .contains_paid_content();
}

void OmniboxTabHelper::PrimaryPageChanged(content::Page& page) {
  page_has_apc_paywall_signal_.reset();

  // Reset old times to avoid logging them incorrectly.
  primary_main_document_element_available_time_.reset();
  dom_content_loaded_time_.reset();
  logged_current_navigation_timings_ = false;

  primary_page_changed_time_ = base::ElapsedTimer();
}

void OmniboxTabHelper::PrimaryMainDocumentElementAvailable() {
  primary_main_document_element_available_time_ = base::ElapsedTimer();
}

void OmniboxTabHelper::DOMContentLoaded(
    content::RenderFrameHost* render_frame_host) {
  // Ignore events from subframes.
  if (render_frame_host->GetParent()) {
    return;
  }
  dom_content_loaded_time_ = base::ElapsedTimer();
}

void OmniboxTabHelper::MaybeLogNavigationToPopupShownTimings(
    metrics::OmniboxEventProto::PageClassification page_classification) {
  if (logged_current_navigation_timings_) {
    return;
  }
  logged_current_navigation_timings_ = true;

  // If the primary page hasn't changed, then there is nothing to log, and this
  // tab is probably on the NTP, so exit early to avoid skewing metrics.
  if (!primary_page_changed_time_.has_value()) {
    return;
  }

  const std::string page_context =
      metrics::OmniboxEventProto::PageClassification_Name(page_classification);

  LogNavigationToPopupUma(kPrimaryPageChangedHistogramSuffix, page_context,
                          primary_page_changed_time_->Elapsed());

  if (primary_main_document_element_available_time_.has_value()) {
    LogNavigationToPopupUma(
        kMainDocumentElementAvailableHistogramSuffix, page_context,
        primary_main_document_element_available_time_->Elapsed());
  }

  if (dom_content_loaded_time_.has_value()) {
    LogNavigationToPopupUma(kDomContentLoadedHistogramSuffix, page_context,
                            dom_content_loaded_time_->Elapsed());
  }
}

void OmniboxTabHelper::MaybeLogPaywallSignal() {
  // If the page content service is not observing, then the paywall signal is
  // unavailable to be fetched.
  if (!page_content_service_observation_.IsObserving()) {
    return;
  }

  const auto paywall_signal = ToPaywallSignal(page_has_apc_paywall_signal_);
  base::UmaHistogramEnumeration("Omnibox.OnPopupOpen.PaywallSignal",
                                paywall_signal);
}