File: page_info_merchant_trust_controller.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (153 lines) | stat: -rw-r--r-- 6,143 bytes parent folder | download | duplicates (5)
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
// 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/ui/views/page_info/page_info_merchant_trust_controller.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/page_info/merchant_trust_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/hats/survey_config.h"
#include "chrome/browser/ui/page_info/chrome_page_info_ui_delegate.h"
#include "chrome/browser/ui/page_info/merchant_trust_side_panel.h"
#include "chrome/browser/ui/views/page_info/page_info_merchant_trust_content_view.h"
#include "components/page_info/core/features.h"
#include "components/page_info/core/merchant_trust_service.h"
#include "components/page_info/core/page_info_types.h"
#include "components/page_info/core/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"

PageInfoMerchantTrustController::PageInfoMerchantTrustController(
    PageInfoMerchantTrustContentView* content_view,
    content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents), content_view_(content_view) {
  InitCallbacks();

  auto* profile =
      Profile::FromBrowserContext(web_contents->GetBrowserContext());
  service_ = MerchantTrustServiceFactory::GetForProfile(profile);
  service_->GetMerchantTrustInfo(
      web_contents->GetVisibleURL(),
      base::BindOnce(
          &PageInfoMerchantTrustController::OnMerchantTrustDataFetched,
          base::Unretained(this)));

  auto* hats_service =
      HatsServiceFactory::GetForProfile(profile, /*create_if_necessary=*/true);
  if (hats_service) {
    content_view_->SetHatsButtonVisibility(hats_service->CanShowSurvey(
        kHatsSurveyTriggerMerchantTrustLearnSurvey));
  }

  interaction_timer_.Start(
      FROM_HERE, page_info::kMerchantTrustRequiredInteractionDuration.Get(),
      base::BindOnce(&PageInfoMerchantTrustController::RecordInteractionPref,
                     weak_ptr_factory_.GetWeakPtr()));
}

PageInfoMerchantTrustController::~PageInfoMerchantTrustController() = default;

void PageInfoMerchantTrustController::MerchantBubbleOpened(
    page_info::MerchantBubbleOpenReferrer referrer) {
  RecordInteraction(
      referrer == page_info::MerchantBubbleOpenReferrer::kPageInfo
          ? page_info::MerchantTrustInteraction::kBubbleOpenedFromPageInfo
          : page_info::MerchantTrustInteraction::
                kBubbleOpenedFromLocationBarChip);
}

void PageInfoMerchantTrustController::MerchantBubbleClosed() {
  RecordInteraction(page_info::MerchantTrustInteraction::kBubbleClosed);
}

void PageInfoMerchantTrustController::OnMerchantTrustDataFetched(
    const GURL& url,
    std::optional<page_info::MerchantData> merchant_data) {
  if (!merchant_data.has_value()) {
    return;
  }

  merchant_data_ = merchant_data.value();
  content_view_->SetReviewsSummary(
      base::UTF8ToUTF16(merchant_data->reviews_summary));
  content_view_->SetRatingAndReviewCount(merchant_data->star_rating,
                                         merchant_data->count_rating);
}

void PageInfoMerchantTrustController::LearnMoreLinkPressed(
    const ui::Event& event) {
  // TODO(crbug.com/381405880): Open learn more link.
}

void PageInfoMerchantTrustController::ViewReviewsPressed() {
  ShowMerchantTrustSidePanel(web_contents(), merchant_data_.page_url);
  RecordInteraction(page_info::MerchantTrustInteraction::kSidePanelOpened);
}

void PageInfoMerchantTrustController::HatsButtonPressed() {
  auto* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  auto* hats_service =
      HatsServiceFactory::GetForProfile(profile, /*create_if_necessary=*/true);
  if (!hats_service) {
    return;
  }
  hats_service->LaunchSurvey(
      kHatsSurveyTriggerMerchantTrustLearnSurvey,
      base::BindOnce(&PageInfoMerchantTrustController::OnSurveyLoaded,
                     weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&PageInfoMerchantTrustController::OnSurveyFailed,
                     weak_ptr_factory_.GetWeakPtr()));
  content_view_->SetHatsButtonTitleId(
      IDS_PAGE_INFO_MERCHANT_TRUST_HATS_LOADING_BUTTON);
}

void PageInfoMerchantTrustController::OnSurveyLoaded() {
  content_view_->GetWidget()->Close();
}

void PageInfoMerchantTrustController::OnSurveyFailed() {
  content_view_->SetHatsButtonTitleId(
      IDS_PAGE_INFO_MERCHANT_TRUST_HATS_FAILED_BUTTON);
}

void PageInfoMerchantTrustController::InitCallbacks() {
  learn_more_link_callback_ =
      content_view_->RegisterLearnMoreLinkPressedCallback(base::BindRepeating(
          &PageInfoMerchantTrustController::LearnMoreLinkPressed,
          base::Unretained(this)));

  view_reviews_button_callback_ =
      content_view_->RegisterViewReviewsButtonPressedCallback(
          base::BindRepeating(
              &PageInfoMerchantTrustController::ViewReviewsPressed,
              base::Unretained(this)));

  hats_button_callback_ = content_view_->RegisterHatsButtonPressedCallback(
      base::BindRepeating(&PageInfoMerchantTrustController::HatsButtonPressed,
                          base::Unretained(this)));
}

void PageInfoMerchantTrustController::RecordInteractionPref() {
  auto* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  profile->GetPrefs()->SetTime(prefs::kMerchantTrustUiLastInteractionTime,
                               clock_->Now());
}

void PageInfoMerchantTrustController::RecordInteraction(
    page_info::MerchantTrustInteraction interaction) {
  auto url =
      web_contents() != nullptr ? web_contents()->GetVisibleURL() : GURL();
  service_->RecordMerchantTrustInteraction(url, interaction);

  auto ukm_source_id =
      web_contents() != nullptr
          ? web_contents()->GetPrimaryMainFrame()->GetPageUkmSourceId()
          : ukm::kInvalidSourceId;
  service_->RecordMerchantTrustUkm(ukm_source_id, interaction);
}