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
|
// 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/promos/ios_promo_bubble.h"
#include <memory>
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/not_fatal_until.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/promos/promos_pref_names.h"
#include "chrome/browser/promos/promos_types.h"
#include "chrome/browser/promos/promos_utils.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/promos/ios_promo_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/tracker.h"
#include "components/prefs/pref_service.h"
#include "components/qr_code_generator/bitmap_generator.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/page_navigator.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
namespace {
// Get the correct URL for the promo's QR code.
std::string GetIOSDesktopPromoQRCodeURL(IOSPromoType promo_type) {
switch (promo_type) {
case IOSPromoType::kPassword:
return IOSPromoConstants::kIOSPromoPasswordBubbleQRCodeURL;
case IOSPromoType::kAddress:
return IOSPromoConstants::kIOSPromoAddressBubbleQRCodeURL;
case IOSPromoType::kPayment:
return IOSPromoConstants::kIOSPromoPaymentBubbleQRCodeURL;
}
}
} // namespace
// Pointer to BubbleDialogDelegate instance.
views::BubbleDialogDelegate* IOSPromoBubble::ios_promo_delegate_ = nullptr;
IOSPromoType IOSPromoBubble::current_promo_type_;
class IOSPromoBubble::IOSPromoBubbleDelegate : public ui::DialogModelDelegate {
public:
IOSPromoBubbleDelegate(Profile* profile, IOSPromoType promo_type)
: profile_(profile),
promo_type_(promo_type),
ios_promo_prefs_config_(promos_utils::IOSPromoPrefsConfig(promo_type)) {
}
// Handler for when the window closes.
void OnWindowClosing() { ios_promo_delegate_ = nullptr; }
// Callback for when the bubble is dismissed.
void OnDismissal() {
feature_engagement::Tracker* tracker =
feature_engagement::TrackerFactory::GetForBrowserContext(profile_);
if (tracker && ios_promo_prefs_config_.promo_feature) {
tracker->Dismissed(*ios_promo_prefs_config_.promo_feature);
}
// Don't record a histogram if either of the buttons' callbacks have run
// and a histogram has already been recorded.
if (!impression_histogram_already_recorded_) {
RecordIOSDesktopPromoUserInteractionHistogram(
promo_type_,
profile_->GetPrefs()->GetInteger(
ios_promo_prefs_config_.promo_impressions_counter_pref_name),
promos_utils::DesktopIOSPromoAction::kDismissed);
}
}
// Callback for when the "No thanks" button is clicked.
void OnNoThanksButtonClicked() {
impression_histogram_already_recorded_ = true;
profile_->GetPrefs()->SetBoolean(
ios_promo_prefs_config_.promo_opt_out_pref_name, true);
promos_utils::RecordIOSDesktopPromoUserInteractionHistogram(
promo_type_,
profile_->GetPrefs()->GetInteger(
ios_promo_prefs_config_.promo_impressions_counter_pref_name),
promos_utils::DesktopIOSPromoAction::kNoThanksClicked);
ios_promo_delegate_->GetWidget()->Close();
}
private:
// Pointer to the current Profile.
const raw_ptr<Profile> profile_;
// Flag tracking whether the impression histogram has already been
// recorded.
bool impression_histogram_already_recorded_ = false;
// Promo type for the current promo bubble.
const IOSPromoType promo_type_;
// The structure that holds the configurations of the current promo type.
const promos_utils::IOSPromoPrefsConfig ios_promo_prefs_config_;
};
// static
// CreateFooter creates the view that is inserted as footer to the bubble.
std::unique_ptr<views::View> IOSPromoBubble::CreateFooter(
IOSPromoBubble::IOSPromoBubbleDelegate* bubble_delegate,
const IOSPromoConstants::IOSPromoTypeConfigs& ios_promo_config) {
views::LayoutProvider* provider = views::LayoutProvider::Get();
auto footer_title_container =
views::Builder<views::Label>()
.SetText(l10n_util::GetStringUTF16(ios_promo_config.promo_title_id))
.SetTextStyle(views::style::STYLE_BODY_2_MEDIUM)
.SetMultiLine(true)
.SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_TO_HEAD)
.SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(
(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_DIALOG_CONTENT_MARGIN_TOP_TEXT)),
0,
(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_DIALOG_CONTENT_MARGIN_TOP_TEXT)),
0));
auto footer_view =
views::Builder<views::BoxLayoutView>()
.SetOrientation(views::BoxLayout::Orientation::kVertical)
.SetCrossAxisAlignment(views::BoxLayout::MainAxisAlignment::kStart)
.SetMainAxisAlignment(views::BoxLayout::MainAxisAlignment::kStart)
.SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kStretch)
.SetBetweenChildSpacing(provider->GetDistanceMetric(
views::DistanceMetric::DISTANCE_VECTOR_ICON_PADDING));
auto decline_button_callback = base::BindRepeating(
&IOSPromoBubble::IOSPromoBubbleDelegate::OnNoThanksButtonClicked,
base::Unretained(bubble_delegate));
auto decline_button = views::Builder<views::MdTextButton>()
.SetText(l10n_util::GetStringUTF16(
ios_promo_config.decline_button_text_id))
.SetIsDefault(false)
.SetCallback(decline_button_callback);
auto description_label =
views::Builder<views::Label>()
.SetText(
l10n_util::GetStringUTF16(ios_promo_config.promo_description_id))
.SetTextContext(views::style::CONTEXT_BUBBLE_FOOTER)
.SetTextStyle(views::style::STYLE_SECONDARY)
.SetEnabledColor(kColorDesktopToIOSPromoFooterSubtitleLabel)
.SetMultiLine(true)
.SetProperty(views::kFlexBehaviorKey,
views::FlexSpecification(
views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kPreferred,
/*adjust_height_for_width=*/true))
.SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_TO_HEAD);
auto label_and_button_container =
views::Builder<views::FlexLayoutView>()
.SetOrientation(views::LayoutOrientation::kVertical)
.SetCrossAxisAlignment(views::LayoutAlignment::kEnd)
.AddChild(description_label)
.AddChild(decline_button)
.SetProperty(views::kFlexBehaviorKey,
views::FlexSpecification(
views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kPreferred,
/*adjust_height_for_width=*/true))
.SetProperty(views::kMarginsKey,
gfx::Insets::TLBR(
0,
(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_DIALOG_CONTENT_MARGIN_TOP_TEXT)),
0, 0));
views::ImageView* image_view;
auto qr_code_container =
views::Builder<views::ImageView>()
.CopyAddressTo(&image_view)
.SetHorizontalAlignment(views::ImageView::Alignment::kLeading)
.SetImageSize(gfx::Size(IOSPromoConstants::kQrCodeImageSize,
IOSPromoConstants::kQrCodeImageSize))
.SetBorder(views::CreateRoundedRectBorder(
/*thickness=*/2,
views::LayoutProvider::Get()->GetCornerRadiusMetric(
views::Emphasis::kHigh),
SK_ColorWHITE))
.SetVisible(true);
auto footer_content_container =
views::Builder<views::FlexLayoutView>()
.SetOrientation(views::LayoutOrientation::kHorizontal)
.SetCrossAxisAlignment(views::LayoutAlignment::kStart)
.AddChild(qr_code_container)
.AddChild(label_and_button_container);
auto built_footer_view =
std::move(footer_view.AddChild(footer_title_container)
.AddChild(footer_content_container))
.Build();
// Note that the absence of a quiet zone may interfere with decoding
// of QR codes even for small codes.
auto qr_image = qr_code_generator::GenerateImage(
base::as_byte_span(std::string_view(ios_promo_config.promo_qr_code_url)),
qr_code_generator::ModuleStyle::kCircles,
qr_code_generator::LocatorStyle::kRounded,
qr_code_generator::CenterImage::kProductLogo,
qr_code_generator::QuietZone::kIncluded);
// Generating QR code for `kQRCodeURL` should always succeed (e.g. it
// can't result in input-too-long error or other errors).
CHECK(qr_image.has_value());
image_view->SetImage(ui::ImageModel::FromImageSkia(qr_image.value()));
return built_footer_view;
}
// static
IOSPromoConstants::IOSPromoTypeConfigs IOSPromoBubble::SetUpBubble(
IOSPromoType promo_type) {
IOSPromoConstants::IOSPromoTypeConfigs ios_promo_config;
ios_promo_config.promo_qr_code_url = GetIOSDesktopPromoQRCodeURL(promo_type);
switch (promo_type) {
case IOSPromoType::kPassword:
// Set up iOS Password Promo Bubble.
ios_promo_config.bubble_title_id =
IDS_IOS_DESKTOP_PASSWORD_PROMO_BUBBLE_TITLE;
ios_promo_config.bubble_subtitle_id =
IDS_IOS_DESKTOP_PASSWORD_PROMO_BUBBLE_SUBTITLE;
ios_promo_config.promo_title_id =
IDS_IOS_DESKTOP_PASSWORD_PROMO_BUBBLE_FOOTER_TITLE;
ios_promo_config.promo_description_id =
IDS_IOS_DESKTOP_PASSWORD_PROMO_BUBBLE_FOOTER_DESCRIPTION_QR;
ios_promo_config.decline_button_text_id =
IDS_IOS_DESKTOP_PASSWORD_PROMO_BUBBLE_BUTTON_DECLINE;
break;
case IOSPromoType::kAddress:
// Set up iOS Address Promo Bubble.
ios_promo_config.bubble_title_id =
IDS_IOS_DESKTOP_ADDRESS_PROMO_BUBBLE_TITLE;
ios_promo_config.bubble_subtitle_id =
IDS_IOS_DESKTOP_ADDRESS_PROMO_BUBBLE_SUBTITLE;
ios_promo_config.promo_title_id =
IDS_IOS_DESKTOP_ADDRESS_PROMO_BUBBLE_FOOTER_TITLE;
ios_promo_config.promo_description_id =
IDS_IOS_DESKTOP_ADDRESS_PROMO_BUBBLE_FOOTER_DESCRIPTION_QR;
ios_promo_config.decline_button_text_id =
IDS_IOS_DESKTOP_ADDRESS_PROMO_BUBBLE_BUTTON_DECLINE;
break;
case IOSPromoType::kPayment:
// Set up iOS Payment Promo Bubble.
ios_promo_config.bubble_title_id =
IDS_AUTOFILL_SAVE_CARD_CONFIRMATION_SUCCESS_TITLE_TEXT;
ios_promo_config.bubble_subtitle_id =
IDS_AUTOFILL_SAVE_CARD_CONFIRMATION_SUCCESS_DESCRIPTION_TEXT;
ios_promo_config.promo_title_id =
IDS_IOS_DESKTOP_PAYMENT_PROMO_BUBBLE_FOOTER_TITLE;
ios_promo_config.promo_description_id =
IDS_IOS_DESKTOP_PAYMENT_PROMO_BUBBLE_FOOTER_DESCRIPTION_QR;
ios_promo_config.decline_button_text_id =
IDS_IOS_DESKTOP_PAYMENT_PROMO_BUBBLE_BUTTON_DECLINE;
break;
}
return ios_promo_config;
}
// static
void IOSPromoBubble::ShowPromoBubble(views::View* anchor_view,
PageActionIconView* highlighted_button,
Profile* profile,
IOSPromoType promo_type) {
IOSPromoConstants::IOSPromoTypeConfigs ios_promo_config =
SetUpBubble(promo_type);
if (ios_promo_delegate_) {
return;
}
auto bubble_delegate_unique =
std::make_unique<IOSPromoBubbleDelegate>(profile, promo_type);
IOSPromoBubbleDelegate* bubble_delegate = bubble_delegate_unique.get();
auto dialog_model_builder =
ui::DialogModel::Builder(std::move(bubble_delegate_unique));
dialog_model_builder.SetDialogDestroyingCallback(
base::BindOnce(&IOSPromoBubbleDelegate::OnWindowClosing,
base::Unretained(bubble_delegate)));
dialog_model_builder.SetCloseActionCallback(base::BindOnce(
&IOSPromoBubbleDelegate::OnDismissal, base::Unretained(bubble_delegate)));
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
auto banner_image = ui::ImageModel::FromImageSkia(
*bundle.GetImageSkiaNamed(IDR_SUCCESS_GREEN_CHECKMARK));
dialog_model_builder.SetBannerImage(banner_image);
dialog_model_builder.SetTitle(
l10n_util::GetStringUTF16(ios_promo_config.bubble_title_id));
dialog_model_builder.SetSubtitle(
l10n_util::GetStringUTF16(ios_promo_config.bubble_subtitle_id));
auto promo_bubble = std::make_unique<views::BubbleDialogModelHost>(
dialog_model_builder.Build(), anchor_view,
views::BubbleBorder::TOP_RIGHT);
ios_promo_delegate_ = promo_bubble.get();
current_promo_type_ = promo_type;
promo_bubble->SetHighlightedButton(highlighted_button);
promo_bubble->SetFootnoteView(
CreateFooter(bubble_delegate, ios_promo_config));
views::Widget* const widget =
views::BubbleDialogDelegate::CreateBubble(std::move(promo_bubble));
widget->Show();
// `highlighted_button` can be null when the promo_bubble's page action is
// anchored to the right hand side/RHS of the omnibox.
if (highlighted_button) {
highlighted_button->SetVisible(true);
}
}
// static
void IOSPromoBubble::Hide() {
if (ios_promo_delegate_) {
ios_promo_delegate_->GetWidget()->Close();
}
}
// static
bool IOSPromoBubble::IsPromoTypeVisible(IOSPromoType promo_type) {
if (!ios_promo_delegate_) {
return false;
}
return current_promo_type_ == promo_type;
}
|