File: discounts_bubble_dialog_view.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (305 lines) | stat: -rw-r--r-- 12,402 bytes parent folder | download | duplicates (3)
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
// 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/commerce/discounts_bubble_dialog_view.h"

#include <memory>
#include <string>
#include <utility>

#include "base/functional/callback_forward.h"
#include "base/i18n/time_formatting.h"
#include "chrome/browser/ui/commerce/commerce_ui_tab_helper.h"
#include "chrome/browser/ui/tabs/public/tab_features.h"
#include "chrome/browser/ui/views/accessibility/theme_tracking_non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/commerce/discounts_coupon_code_label_view.h"
#include "chrome/browser/ui/views/controls/page_switcher_view.h"
#include "chrome/browser/ui/views/controls/subpage_view.h"
#include "chrome/grit/theme_resources.h"
#include "components/commerce/core/commerce_types.h"
#include "components/commerce/core/metrics/discounts_metric_collector.h"
#include "components/strings/grit/components_strings.h"
#include "components/tabs/public/tab_interface.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_tracker.h"
#include "url/gurl.h"

DEFINE_ELEMENT_IDENTIFIER_VALUE(kDiscountsBubbleDialogId);
DEFINE_ELEMENT_IDENTIFIER_VALUE(kDiscountsBubbleMainPageId);
DEFINE_ELEMENT_IDENTIFIER_VALUE(kDiscountsBubbleTermsAndConditionLabelId);
DEFINE_ELEMENT_IDENTIFIER_VALUE(kDiscountsBubbleTermsAndConditionPageId);

// DiscountsBubbleDialogView
DiscountsBubbleDialogView::DiscountsBubbleDialogView(
    View* anchor_view,
    content::WebContents* web_contents,
    const commerce::DiscountInfo& discount_info)
    : LocationBarBubbleDelegateView(anchor_view, web_contents, true),
      discount_info_(discount_info),
      ukm_source_id_(
          web_contents->GetPrimaryMainFrame()->GetPageUkmSourceId()) {
  SetProperty(views::kElementIdentifierKey, kDiscountsBubbleDialogId);

  SetShowCloseButton(true);
  SetCloseCallback(base::BindOnce(&DiscountsBubbleDialogView::OnDialogClosing,
                                  weak_factory_.GetWeakPtr()));

  set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_BUBBLE_PREFERRED_WIDTH));
  SetLayoutManager(std::make_unique<views::FillLayout>());
  auto dialog_insets =
      ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
          views::DialogContentType::kControl, views::DialogContentType::kText);
  set_margins(
      gfx::Insets::TLBR(dialog_insets.top(), 0, dialog_insets.bottom(), 0));
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));

  GURL url = web_contents->GetLastCommittedURL();
  std::string seller_domain = url.spec();

  page_container_ = AddChildView(std::make_unique<PageSwitcherView>(
      CreateMainPageContent(discount_info, seller_domain)));
}

DiscountsBubbleDialogView::~DiscountsBubbleDialogView() = default;

void DiscountsBubbleDialogView::AddedToWidget() {
  GetBubbleFrameView()->SetHeaderView(CreateMainPageHeaderView());
  GetBubbleFrameView()->SetTitleView(CreateMainPageTitleView(discount_info_));
}

std::unique_ptr<views::View>
DiscountsBubbleDialogView::CreateMainPageHeaderView() {
  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();

  return std::make_unique<ThemeTrackingNonAccessibleImageView>(
      *bundle.GetImageSkiaNamed(IDR_DISCOUNTS_BUBBLE_HEADER_LIGHT),
      *bundle.GetImageSkiaNamed(IDR_DISCOUNTS_BUBBLE_HEADER_DARK),
      base::BindRepeating(&views::BubbleDialogDelegate::background_color,
                          base::Unretained(this)));
}

std::unique_ptr<views::View> DiscountsBubbleDialogView::CreateMainPageTitleView(
    const commerce::DiscountInfo& discount_info) {
  auto title_view = std::make_unique<views::Label>(
      base::ASCIIToUTF16(discount_info_.description_detail),
      views::style::CONTEXT_DIALOG_TITLE);
  title_view->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  title_view->SetMultiLine(true);
  return title_view;
}

std::unique_ptr<views::View> DiscountsBubbleDialogView::CreateMainPageContent(
    const commerce::DiscountInfo& discount_info,
    const std::string& seller_domain) {
  auto main_page_view = std::make_unique<views::View>();

  main_page_view->SetProperty(views::kElementIdentifierKey,
                              kDiscountsBubbleMainPageId);
  gfx::Insets dialog_insets =
      ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
          views::DialogContentType::kControl, views::DialogContentType::kText);

  auto* layout =
      main_page_view->SetLayoutManager(std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kVertical,
          gfx::Insets::TLBR(0, dialog_insets.left(), 0, dialog_insets.right()),
          ChromeLayoutProvider::Get()->GetDistanceMetric(
              views::DISTANCE_UNRELATED_CONTROL_VERTICAL)));
  layout->set_cross_axis_alignment(
      views::BoxLayout::CrossAxisAlignment::kStart);

  // coupon code
  main_page_view->AddChildView(std::make_unique<DiscountsCouponCodeLabelView>(
      base::ASCIIToUTF16(discount_info.discount_code.value()),
      base::BindRepeating(&DiscountsBubbleDialogView::CopyButtonClicked,
                          weak_factory_.GetWeakPtr())));

  // additional info with expiration date, and terms and conditions
  auto* additional_info_label = main_page_view->AddChildView(
      views::Builder<views::StyledLabel>()
          .SetDefaultTextStyle(views::style::STYLE_SECONDARY)
          .SetTextContext(views::style::CONTEXT_DIALOG_BODY_TEXT)
          .SetHorizontalAlignment(gfx::ALIGN_LEFT)
          .Build());

  auto additional_info_text =
      discount_info.expiry_time_sec.has_value()
          ? l10n_util::GetStringFUTF16(
                IDS_DISCOUNT_USE_THIS_CODE_AT_CHECKOUT_WITH_EXPIRATION_DATE,
                TimeFormatShortDate(base::Time::FromSecondsSinceUnixEpoch(
                    discount_info.expiry_time_sec.value())))
          : l10n_util::GetStringUTF16(IDS_DISCOUNT_USE_THIS_CODE_AT_CHECKOUT);

  if (discount_info.terms_and_conditions.has_value() &&
      !discount_info.terms_and_conditions.value().empty()) {
    std::vector<size_t> offsets;
    additional_info_text = l10n_util::GetStringFUTF16(
        IDS_TWO_STRINGS_CONNECTOR_WITH_SPACE, additional_info_text,
        l10n_util::GetStringUTF16(IDS_SEE_SELLER_TERMS_AND_CONDITIONS),
        &offsets);
    additional_info_label->SetText(additional_info_text);
    size_t terms_and_conditions_offset = offsets[1];
    base::RepeatingCallback<void()> callback = base::BindRepeating(
        &DiscountsBubbleDialogView::OpenTermsAndConditionsPage,
        weak_factory_.GetWeakPtr(), discount_info, seller_domain);
    views::StyledLabel::RangeStyleInfo terms_and_conditions_style_info =
        views::StyledLabel::RangeStyleInfo::CreateForLink(std::move(callback));
    additional_info_label->AddStyleRange(
        gfx::Range(terms_and_conditions_offset, additional_info_text.length()),
        terms_and_conditions_style_info);
    additional_info_label->SetProperty(
        views::kElementIdentifierKey, kDiscountsBubbleTermsAndConditionLabelId);
  } else {
    additional_info_label->SetText(additional_info_text);
  }

  return main_page_view;
}

void DiscountsBubbleDialogView::OpenMainPage(
    commerce::DiscountInfo discount_info,
    std::string seller_domain) {
  GetBubbleFrameView()->SetHeaderView(CreateMainPageHeaderView());
  GetBubbleFrameView()->SetTitleView(CreateMainPageTitleView(discount_info));
  page_container_->SwitchToPage(
      CreateMainPageContent(discount_info, seller_domain));
  GetBubbleFrameView()->SetFootnoteView(nullptr);
}

void DiscountsBubbleDialogView::OpenTermsAndConditionsPage(
    commerce::DiscountInfo discount_info,
    std::string seller_domain) {
  auto bubble_width = views::LayoutProvider::Get()->GetDistanceMetric(
      views::DISTANCE_BUBBLE_PREFERRED_WIDTH);

  gfx::FontList font_list = views::TypographyProvider::Get().GetFont(
      views::style::CONTEXT_LABEL, views::style::STYLE_PRIMARY);

  auto footer_message = l10n_util::GetStringFUTF16(
      IDS_SELLER_TERMS_AND_CONDITIONS_DIALOG_FOOTER,
      url_formatter::ElideHost(GURL(seller_domain), font_list, bubble_width));

  page_container_->SwitchToPage(
      views::Builder<SubpageView>(
          std::make_unique<SubpageView>(
              base::BindRepeating(&DiscountsBubbleDialogView::OpenMainPage,
                                  weak_factory_.GetWeakPtr(), discount_info,
                                  seller_domain),
              GetBubbleFrameView()))
          .SetProperty(views::kElementIdentifierKey,
                       kDiscountsBubbleTermsAndConditionPageId)
          .SetTitle(l10n_util::GetStringUTF16(
              IDS_SELLER_TERMS_AND_CONDITIONS_DIALOG_TITLE))
          .SetContentView(
              views::Builder<views::Label>()
                  .SetText(base::ASCIIToUTF16(
                      discount_info.terms_and_conditions.value()))
                  .SetMultiLine(true)
                  .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
                  .Build())
          .SetHeaderView(nullptr)
          .SetFootnoteView(
              views::Builder<views::Label>()
                  .SetText(footer_message)
                  .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
                  .SetMultiLine(true)
                  .SetAllowCharacterBreak(true)
                  .Build())
          .Build());
}

void DiscountsBubbleDialogView::CopyButtonClicked() {
  commerce::metrics::DiscountsMetricCollector::
      RecordDiscountsBubbleCopyButtonClicked(ukm_source_id_);

  auto* tab = tabs::TabInterface::MaybeGetFromContents(web_contents());
  if (!tab || !tab->GetTabFeatures()) {
    return;
  }

  auto* tab_helper = tab->GetTabFeatures()->commerce_ui_tab_helper();
  if (!tab_helper) {
    return;
  }

  tab_helper->OnDiscountsCouponCodeCopied();
}

void DiscountsBubbleDialogView::OnDialogClosing() {
  auto* tab = tabs::TabInterface::MaybeGetFromContents(web_contents());
  if (!tab || !tab->GetTabFeatures()) {
    return;
  }

  auto* tab_helper = tab->GetTabFeatures()->commerce_ui_tab_helper();
  if (!tab_helper) {
    return;
  }

  commerce::metrics::DiscountsMetricCollector::
      DiscountsBubbleCopyStatusOnBubbleClosed(
          tab_helper->IsDiscountsCouponCodeCopied(),
          tab_helper->GetDiscounts());
}

BEGIN_METADATA(DiscountsBubbleDialogView)
END_METADATA

// DiscountsBubbleCoordinator
DiscountsBubbleCoordinator::DiscountsBubbleCoordinator() = default;

DiscountsBubbleCoordinator::~DiscountsBubbleCoordinator() = default;

// WidgetObserver:
void DiscountsBubbleCoordinator::OnWidgetDestroying(views::Widget* widget) {
  CHECK(bubble_widget_observation_.IsObservingSource(widget));
  bubble_widget_observation_.Reset();

  std::move(on_dialog_closing_callback_).Run();
}

void DiscountsBubbleCoordinator::Show(
    views::View* anchor_view,
    content::WebContents* web_contents,
    const commerce::DiscountInfo& discount_info,
    base::OnceClosure on_dialog_closing_callback) {
  CHECK(!IsShowing());

  on_dialog_closing_callback_ = std::move(on_dialog_closing_callback);

  auto bubble = std::make_unique<DiscountsBubbleDialogView>(
      anchor_view, web_contents, discount_info);
  tracker_.SetView(bubble.get());
  auto* widget = DiscountsBubbleDialogView::CreateBubble(std::move(bubble));
  bubble_widget_observation_.Observe(widget);
  widget->Show();
}

void DiscountsBubbleCoordinator::Hide() {
  if (IsShowing()) {
    tracker_.view()->GetWidget()->Close();
  }
  tracker_.SetView(nullptr);
}

DiscountsBubbleDialogView* DiscountsBubbleCoordinator::GetBubble() const {
  return tracker_.view() ? views::AsViewClass<DiscountsBubbleDialogView>(
                               const_cast<views::View*>(tracker_.view()))
                         : nullptr;
}

bool DiscountsBubbleCoordinator::IsShowing() {
  return tracker_.view() != nullptr;
}