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
|
// 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/plus_addresses/plus_address_error_dialog.h"
#include <memory>
#include <string>
#include <utility>
#include "base/functional/callback.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/plus_addresses/grit/plus_addresses_strings.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/base/models/dialog_model_field.h"
namespace plus_addresses {
namespace {
// Helper function to create a `OnceClosure` that records `action`.
base::OnceClosure CreateUserActionClosure(
const base::UserMetricsAction& action) {
return base::BindOnce(
[](const base::UserMetricsAction& a) { base::RecordAction(a); }, action);
}
std::unique_ptr<ui::DialogModel> CreateErrorDialogWithWithCancelAndAccept(
std::u16string description,
base::OnceClosure on_accepted) {
return ui::DialogModel::Builder()
.SetTitle(
l10n_util::GetStringUTF16(IDS_PLUS_ADDRESS_CREATE_INLINE_ERROR_TITLE))
.AddParagraph(ui::DialogModelLabel(std::move(description)))
.AddCancelButton(CreateUserActionClosure(base::UserMetricsAction(
"PlusAddresses.CreateErrorCanceled")),
ui::DialogModel::Button::Params().SetId(
kPlusAddressErrorDialogCancelButton))
.AddOkButton(CreateUserActionClosure(
base::UserMetricsAction(
"PlusAddresses.CreateErrorTryAgainClicked"))
.Then(std::move(on_accepted)),
ui::DialogModel::Button::Params()
.SetId(kPlusAddressErrorDialogAcceptButton)
.SetLabel(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_ERROR_ACCEPT_BUTTON)))
.Build();
}
std::unique_ptr<ui::DialogModel> CreateGenericErrorDialog(
base::OnceClosure on_accepted) {
return CreateErrorDialogWithWithCancelAndAccept(
/*description=*/l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_ERROR_DESCRIPTION),
std::move(on_accepted));
}
std::unique_ptr<ui::DialogModel> CreateTimeoutErrorDialog(
base::OnceClosure on_accepted) {
return CreateErrorDialogWithWithCancelAndAccept(
/*description=*/l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_TIMEOUT_ERROR_DESCRIPTION),
std::move(on_accepted));
}
std::unique_ptr<ui::DialogModel> CreateQuotaErrorDialog(
base::OnceClosure on_accepted) {
return ui::DialogModel::Builder()
.SetTitle(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_QUOTA_ERROR_TITLE))
.AddParagraph(ui::DialogModelLabel(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_QUOTA_ERROR_DESCRIPTION)))
.AddOkButton(
CreateUserActionClosure(
base::UserMetricsAction("PlusAddresses.QuotaErrorAccepted"))
.Then(std::move(on_accepted)),
ui::DialogModel::Button::Params().SetId(
kPlusAddressErrorDialogAcceptButton))
.Build();
}
} // namespace
DEFINE_ELEMENT_IDENTIFIER_VALUE(kPlusAddressErrorDialogAcceptButton);
DEFINE_ELEMENT_IDENTIFIER_VALUE(kPlusAddressErrorDialogCancelButton);
void ShowInlineCreationAffiliationErrorDialog(
content::WebContents* web_contents,
std::u16string affiliated_domain,
std::u16string affiliated_plus_address,
base::OnceClosure on_accepted) {
constrained_window::ShowWebModal(
ui::DialogModel::Builder()
.SetTitle(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_AFFILATION_ERROR_TITLE))
.AddParagraph(ui::DialogModelLabel::CreateWithReplacements(
IDS_PLUS_ADDRESS_CREATE_INLINE_AFFILATION_ERROR_DESCRIPTION,
{ui::DialogModelLabel::CreateEmphasizedText(
std::move(affiliated_domain)),
ui::DialogModelLabel::CreateEmphasizedText(
std::move(affiliated_plus_address))}))
.AddCancelButton(CreateUserActionClosure(base::UserMetricsAction(
"PlusAddresses.AffiliationErrorCanceled")),
ui::DialogModel::Button::Params().SetId(
kPlusAddressErrorDialogCancelButton))
.AddOkButton(
CreateUserActionClosure(
base::UserMetricsAction(
"PlusAddresses.AffiliationErrorFilledExisting"))
.Then(std::move(on_accepted)),
ui::DialogModel::Button::Params()
.SetId(kPlusAddressErrorDialogAcceptButton)
.SetLabel(l10n_util::GetStringUTF16(
IDS_PLUS_ADDRESS_CREATE_INLINE_AFFILATION_ERROR_ACCEPT_BUTTON)))
.Build(),
web_contents);
}
void ShowInlineCreationErrorDialog(content::WebContents* web_contents,
PlusAddressErrorDialogType error_dialog_type,
base::OnceClosure on_accepted) {
auto create_model = [&]() {
switch (error_dialog_type) {
case PlusAddressErrorDialogType::kGenericError:
return CreateGenericErrorDialog(std::move(on_accepted));
case PlusAddressErrorDialogType::kTimeout:
return CreateTimeoutErrorDialog(std::move(on_accepted));
case PlusAddressErrorDialogType::kQuotaExhausted:
return CreateQuotaErrorDialog(std::move(on_accepted));
}
NOTREACHED();
};
constrained_window::ShowWebModal(create_model(), web_contents);
}
} // namespace plus_addresses
|