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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
// Copyright 2024 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/contextual_cueing/contextual_cueing_helper.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_enums.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_features.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_page_data.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_service.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_service_factory.h"
#include "chrome/browser/contextual_cueing/zero_state_suggestions_page_data.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/tabs/glic_nudge_controller.h"
#include "components/optimization_guide/core/hints/hints_processing_util.h"
#include "components/optimization_guide/core/hints/optimization_guide_decider.h"
#include "components/optimization_guide/core/hints/optimization_metadata.h"
#include "components/optimization_guide/core/model_execution/model_execution_features_controller.h"
#include "components/optimization_guide/proto/contextual_cueing_metadata.pb.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "url/gurl.h"
#include "url/origin.h"
#if BUILDFLAG(ENABLE_GLIC)
#include "chrome/browser/glic/glic_enabling.h"
#include "chrome/browser/glic/glic_keyed_service_factory.h"
#endif
namespace contextual_cueing {
class ScopedNudgeDecisionRecorder {
public:
ScopedNudgeDecisionRecorder(
optimization_guide::proto::OptimizationType optimization_type,
ukm::SourceId source_id)
: optimization_type_(optimization_type), source_id_(source_id) {}
~ScopedNudgeDecisionRecorder() {
base::UmaHistogramEnumeration(
"ContextualCueing.NudgeDecision." +
optimization_guide::GetStringNameForOptimizationType(
optimization_type_),
nudge_decision_);
auto* ukm_recorder = ukm::UkmRecorder::Get();
ukm::builders::ContextualCueing_NudgeDecision(source_id_)
.SetNudgeDecision(static_cast<int64_t>(nudge_decision_))
.SetOptimizationType(optimization_type_)
.Record(ukm_recorder->Get());
}
void set_nudge_decision(NudgeDecision nudge_decision) {
nudge_decision_ = nudge_decision;
}
NudgeDecision nudge_decision() const { return nudge_decision_; }
private:
optimization_guide::proto::OptimizationType optimization_type_;
ukm::SourceId source_id_;
NudgeDecision nudge_decision_ = NudgeDecision::kUnknown;
};
ContextualCueingHelper::ContextualCueingHelper(
content::WebContents* web_contents,
OptimizationGuideKeyedService* ogks,
ContextualCueingService* ccs)
: content::WebContentsObserver(web_contents),
content::WebContentsUserData<ContextualCueingHelper>(*web_contents),
optimization_guide_keyed_service_(ogks),
contextual_cueing_service_(ccs) {
if (base::FeatureList::IsEnabled(kContextualCueing)) {
// LINT.IfChange(OptType)
optimization_guide_keyed_service_->RegisterOptimizationTypes(
{optimization_guide::proto::GLIC_CONTEXTUAL_CUEING});
// LINT.ThenChange(//tools/metrics/histograms/metadata/contextual_cueing/histograms.xml:OptType)
}
}
ContextualCueingHelper::~ContextualCueingHelper() = default;
tabs::GlicNudgeController* ContextualCueingHelper::GetGlicNudgeController() {
if (!base::FeatureList::IsEnabled(kContextualCueing)) {
return nullptr;
}
Browser* browser = chrome::FindBrowserWithTab(web_contents());
if (!browser) {
return nullptr;
}
return browser->browser_window_features()->glic_nudge_controller();
}
void ContextualCueingHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
// Ignore sub-frame and uncommitted navigations.
if (!navigation_handle->IsInPrimaryMainFrame()) {
return;
}
if (!navigation_handle->HasCommitted()) {
return;
}
last_same_doc_navigation_committed_ =
navigation_handle->IsSameDocument()
? std::make_optional(base::TimeTicks::Now())
: std::nullopt;
// Ignore reloads.
if (PageTransitionCoreTypeIs(navigation_handle->GetPageTransition(),
ui::PAGE_TRANSITION_RELOAD)) {
return;
}
// Ignore fragment changes.
if (navigation_handle->GetPreviousPrimaryMainFrameURL().GetWithoutRef() ==
navigation_handle->GetURL().GetWithoutRef()) {
return;
}
// Reset FCP state.
has_first_contentful_paint_ = false;
// Clear zero state suggestions if needed.
if (base::FeatureList::IsEnabled(kGlicZeroStateSuggestions) &&
ZeroStateSuggestionsPageData::GetForPage(
web_contents()->GetPrimaryPage())) {
ZeroStateSuggestionsPageData::DeleteForPage(
web_contents()->GetPrimaryPage());
}
if (!base::FeatureList::IsEnabled(kContextualCueing)) {
return;
}
// Make sure we always clear the nudge label anyway despite operating on
// pages.
auto* glic_nudge_controller = GetGlicNudgeController();
if (glic_nudge_controller) {
glic_nudge_controller->UpdateNudgeLabel(
web_contents(), std::string(),
tabs::GlicNudgeActivity::kNudgeIgnoredNavigation, base::DoNothing());
}
// Do not report page loads for these types of navigations.
if (navigation_handle->IsErrorPage() ||
!navigation_handle->ShouldUpdateHistory()) {
return;
}
// We have already initiated nudging sequence for the page. Do not report page
// load.
if (ContextualCueingPageData::GetForPage(web_contents()->GetPrimaryPage())) {
return;
}
contextual_cueing_service_->ReportPageLoad();
}
void ContextualCueingHelper::PrimaryMainDocumentElementAvailable() {
if (!base::FeatureList::IsEnabled(kContextualCueing)) {
return;
}
// We have already initiated nudging sequence for the page. Do not see if we
// should nudge.
if (ContextualCueingPageData::GetForPage(web_contents()->GetPrimaryPage())) {
return;
}
auto* glic_nudge_controller = GetGlicNudgeController();
if (!glic_nudge_controller ||
!web_contents()->GetLastCommittedURL().SchemeIsHTTPOrHTTPS()) {
return;
}
// Determine if server data indicates a nudge should be shown.
optimization_guide_keyed_service_->CanApplyOptimization(
web_contents()->GetLastCommittedURL(),
optimization_guide::proto::GLIC_CONTEXTUAL_CUEING,
base::BindOnce(&ContextualCueingHelper::OnOptimizationGuideCueingMetadata,
weak_ptr_factory_.GetWeakPtr(), base::TimeTicks::Now()));
}
void ContextualCueingHelper::OnFirstContentfulPaintInPrimaryMainFrame() {
if (!base::FeatureList::IsEnabled(kGlicZeroStateSuggestions)) {
return;
}
has_first_contentful_paint_ = true;
ZeroStateSuggestionsPageData* page_data =
ZeroStateSuggestionsPageData::GetForPage(
web_contents()->GetPrimaryPage());
if (page_data) {
page_data->InitiatePageContentExtraction();
}
}
void ContextualCueingHelper::DocumentOnLoadCompletedInPrimaryMainFrame() {
if (!base::FeatureList::IsEnabled(kGlicZeroStateSuggestions)) {
return;
}
ZeroStateSuggestionsPageData* page_data =
ZeroStateSuggestionsPageData::GetForPage(
web_contents()->GetPrimaryPage());
if (page_data) {
page_data->InitiatePageContentExtraction();
}
}
void ContextualCueingHelper::OnOptimizationGuideCueingMetadata(
base::TimeTicks document_available_time,
optimization_guide::OptimizationGuideDecision decision,
const optimization_guide::OptimizationMetadata& metadata) {
std::unique_ptr<ScopedNudgeDecisionRecorder> recorder =
std::make_unique<ScopedNudgeDecisionRecorder>(
optimization_guide::proto::GLIC_CONTEXTUAL_CUEING,
web_contents()->GetPrimaryMainFrame()->GetPageUkmSourceId());
if (decision != optimization_guide::OptimizationGuideDecision::kTrue ||
metadata.empty()) {
recorder->set_nudge_decision(NudgeDecision::kServerDataUnavailable);
return;
}
auto parsed = metadata.ParsedMetadata<
optimization_guide::proto::GlicContextualCueingMetadata>();
if (!parsed) {
recorder->set_nudge_decision(NudgeDecision::kServerDataMalformed);
return;
}
ContextualCueingPageData::CreateForPage(
web_contents()->GetPrimaryPage(), std::move(*parsed),
base::BindOnce(&ContextualCueingHelper::OnCueingDecision,
weak_ptr_factory_.GetWeakPtr(), std::move(recorder),
document_available_time));
}
bool ContextualCueingHelper::IsBrowserBlockingNudges(
ScopedNudgeDecisionRecorder* recorder) {
// Determine if the Browser is available for nudging.
if (!web_contents()) {
return false;
}
auto* tab_interface = tabs::TabInterface::GetFromContents(web_contents());
if (!tab_interface) {
return false;
}
auto* browser_window_interface = tab_interface->GetBrowserWindowInterface();
if (!browser_window_interface) {
return false;
}
auto* user_education_interface =
browser_window_interface->GetUserEducationInterface();
if (!user_education_interface) {
return false;
}
if (user_education_interface->IsFeaturePromoActive(
feature_engagement::kIPHGlicPromoFeature)) {
recorder->set_nudge_decision(NudgeDecision::kNudgeNotShownIPH);
return true;
}
#if BUILDFLAG(ENABLE_GLIC)
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
if (!glic::GlicEnabling::IsEnabledForProfile(profile)) {
return true;
}
auto* glic_service =
glic::GlicKeyedServiceFactory::GetGlicKeyedService(profile);
if (glic_service->IsWindowShowing()) {
recorder->set_nudge_decision(NudgeDecision::kNudgeNotShownWindowShowing);
return true;
}
#endif // BUILDFLAG(ENABLE_GLIC)
return false;
}
void ContextualCueingHelper::OnCueingDecision(
std::unique_ptr<ScopedNudgeDecisionRecorder> decision_recorder,
base::TimeTicks document_available_time,
base::expected<std::string, NudgeDecision> decision_result) {
CHECK_EQ(NudgeDecision::kUnknown, decision_recorder->nudge_decision());
if (ContextualCueingPageData::GetForPage(web_contents()->GetPrimaryPage())) {
ContextualCueingPageData::DeleteForPage(web_contents()->GetPrimaryPage());
}
if (!decision_result.has_value()) {
decision_recorder->set_nudge_decision(decision_result.error());
return;
}
std::string cue_label = decision_result.value();
if (IsBrowserBlockingNudges(decision_recorder.get())) {
return;
}
const GURL& url = web_contents()->GetLastCommittedURL();
auto can_show_decision = contextual_cueing_service_->CanShowNudge(url);
decision_recorder->set_nudge_decision(can_show_decision);
if (can_show_decision != NudgeDecision::kSuccess) {
return;
}
GetGlicNudgeController()->UpdateNudgeLabel(
web_contents(), cue_label, /*activity=*/std::nullopt,
base::BindRepeating(&ContextualCueingService::OnNudgeActivity,
contextual_cueing_service_->GetWeakPtr(),
web_contents(), document_available_time));
}
// static
void ContextualCueingHelper::MaybeCreateForWebContents(
content::WebContents* web_contents) {
if (!base::FeatureList::IsEnabled(contextual_cueing::kContextualCueing) &&
!base::FeatureList::IsEnabled(
contextual_cueing::kGlicZeroStateSuggestions)) {
return;
}
#if BUILDFLAG(ENABLE_GLIC)
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (!glic::GlicEnabling::IsProfileEligible(profile)) {
return;
}
auto* optimization_guide_keyed_service =
OptimizationGuideKeyedServiceFactory::GetForProfile(profile);
if (!optimization_guide_keyed_service) {
return;
}
auto* contextual_cueing_service =
ContextualCueingServiceFactory::GetForProfile(profile);
if (!contextual_cueing_service) {
return;
}
ContextualCueingHelper::CreateForWebContents(web_contents,
optimization_guide_keyed_service,
contextual_cueing_service);
#endif // BUILDFLAG(ENABLE_GLIC)
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(ContextualCueingHelper);
} // namespace contextual_cueing
|