Commit 33a338b4 authored by Rouslan Solomakhin's avatar Rouslan Solomakhin Committed by Commit Bot

Unify shipping address label creation for web payments UI.

Before this patch, the desktop web payments UI omitted sorting code and
dependent locality when constructing labels for shipping addresses. That
would confuse users located in such countries as France, where sorting
code is used.

This patch moves the iOS functions to create labels for shipping and
billing address labels into components/payments/core/strings_util.h and
uses these functions from both iOS and desktop. After this patch, the
shipping addresses on desktop show i18n addresses from all countries
correctly.

Bug: 725182
Change-Id: I9d1fcc513d11914b54553644f82743d2685972c0
Reviewed-on: https://chromium-review.googlesource.com/511102Reviewed-by: default avatarmahmadi <mahmadi@chromium.org>
Reviewed-by: default avatarAnthony Vallee-Dubois <anthonyvd@chromium.org>
Commit-Queue: Rouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#473951}
parent 92ac1582
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "components/payments/core/payment_options_provider.h" #include "components/payments/core/payment_options_provider.h"
#include "components/payments/core/payment_request_data_util.h" #include "components/payments/core/payment_request_data_util.h"
#include "components/payments/core/payments_profile_comparator.h" #include "components/payments/core/payments_profile_comparator.h"
#include "components/payments/core/strings_util.h"
#include "ui/base/default_style.h" #include "ui/base/default_style.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
...@@ -50,23 +51,6 @@ namespace payments { ...@@ -50,23 +51,6 @@ namespace payments {
namespace { namespace {
// TODO(tmartino): Consider combining this with the Android equivalent in
// PersonalDataManager.java
base::string16 GetAddressFromProfile(const autofill::AutofillProfile& profile,
const std::string& locale) {
std::vector<autofill::ServerFieldType> fields;
fields.push_back(autofill::COMPANY_NAME);
fields.push_back(autofill::ADDRESS_HOME_LINE1);
fields.push_back(autofill::ADDRESS_HOME_LINE2);
fields.push_back(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY);
fields.push_back(autofill::ADDRESS_HOME_CITY);
fields.push_back(autofill::ADDRESS_HOME_STATE);
fields.push_back(autofill::ADDRESS_HOME_ZIP);
fields.push_back(autofill::ADDRESS_HOME_SORTING_CODE);
return profile.ConstructInferredLabel(fields, fields.size(), locale);
}
// |s1|, |s2|, and |s3| are lines identifying the profile. |s1| is the // |s1|, |s2|, and |s3| are lines identifying the profile. |s1| is the
// "headline" which may be emphasized depending on |type|. If |disabled_state| // "headline" which may be emphasized depending on |type|. If |disabled_state|
// is true, the labels will look disabled. // is true, the labels will look disabled.
...@@ -131,7 +115,8 @@ std::unique_ptr<views::View> GetShippingAddressLabel( ...@@ -131,7 +115,8 @@ std::unique_ptr<views::View> GetShippingAddressLabel(
base::string16 name = base::string16 name =
profile.GetInfo(autofill::AutofillType(autofill::NAME_FULL), locale); profile.GetInfo(autofill::AutofillType(autofill::NAME_FULL), locale);
base::string16 address = GetAddressFromProfile(profile, locale); base::string16 address =
GetShippingAddressLabelFormAutofillProfile(profile, locale);
base::string16 phone = base::string16 phone =
data_util::GetFormattedPhoneNumberForDisplay(profile, locale); data_util::GetFormattedPhoneNumberForDisplay(profile, locale);
......
...@@ -86,7 +86,7 @@ class PreviewEliderLabel : public views::Label { ...@@ -86,7 +86,7 @@ class PreviewEliderLabel : public views::Label {
PreviewEliderLabel(const base::string16& preview_text, PreviewEliderLabel(const base::string16& preview_text,
const base::string16& format_string, const base::string16& format_string,
int n) int n)
: views::Label(base::ASCIIToUTF16("")), : views::Label(base::string16()),
preview_text_(preview_text), preview_text_(preview_text),
format_string_(format_string), format_string_(format_string),
n_(n) {} n_(n) {}
...@@ -109,7 +109,7 @@ class PreviewEliderLabel : public views::Label { ...@@ -109,7 +109,7 @@ class PreviewEliderLabel : public views::Label {
// TODO(crbug.com/714776): Display something meaningful if the preview can't // TODO(crbug.com/714776): Display something meaningful if the preview can't
// be elided enough for the string to fit. // be elided enough for the string to fit.
return base::ASCIIToUTF16(""); return base::string16();
} }
// views::View: // views::View:
...@@ -155,17 +155,17 @@ std::unique_ptr<views::Button> CreatePaymentSheetRow( ...@@ -155,17 +155,17 @@ std::unique_ptr<views::Button> CreatePaymentSheetRow(
columns->AddPaddingColumn(0, kPaddingAfterName); columns->AddPaddingColumn(0, kPaddingAfterName);
// A column for the content. // A column for the content.
columns->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, columns->AddColumn(views::GridLayout::FILL, views::GridLayout::LEADING, 1,
1, views::GridLayout::USE_PREF, 0, 0); views::GridLayout::USE_PREF, 0, 0);
// A column for the extra content. // A column for the extra content.
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0,
0, views::GridLayout::USE_PREF, 0, 0); views::GridLayout::USE_PREF, 0, 0);
constexpr int kPaddingColumnsWidth = 25; constexpr int kPaddingColumnsWidth = 25;
columns->AddPaddingColumn(0, kPaddingColumnsWidth); columns->AddPaddingColumn(0, kPaddingColumnsWidth);
// A column for the trailing_button. // A column for the trailing_button.
columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, columns->AddColumn(views::GridLayout::TRAILING, views::GridLayout::CENTER, 0,
0, views::GridLayout::USE_PREF, 0, 0); views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0); layout->StartRow(0, 0);
std::unique_ptr<views::Label> name_label = CreateMediumLabel(section_name); std::unique_ptr<views::Label> name_label = CreateMediumLabel(section_name);
...@@ -442,8 +442,8 @@ PaymentSheetViewController::CreateExtraFooterView() { ...@@ -442,8 +442,8 @@ PaymentSheetViewController::CreateExtraFooterView() {
return CreateProductLogoFooterView(); return CreateProductLogoFooterView();
} }
void PaymentSheetViewController::ButtonPressed( void PaymentSheetViewController::ButtonPressed(views::Button* sender,
views::Button* sender, const ui::Event& event) { const ui::Event& event) {
switch (sender->tag()) { switch (sender->tag()) {
case static_cast<int>( case static_cast<int>(
PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON): PaymentSheetViewControllerTags::SHOW_ORDER_SUMMARY_BUTTON):
...@@ -656,18 +656,14 @@ std::unique_ptr<views::Button> PaymentSheetViewController::CreateShippingRow() { ...@@ -656,18 +656,14 @@ std::unique_ptr<views::Button> PaymentSheetViewController::CreateShippingRow() {
// If the button is "Add", clicking it should navigate to the editor // If the button is "Add", clicking it should navigate to the editor
// instead of the list. // instead of the list.
builder.Tag(PaymentSheetViewControllerTags::ADD_SHIPPING_BUTTON); builder.Tag(PaymentSheetViewControllerTags::ADD_SHIPPING_BUTTON);
return builder.CreateWithButton(base::ASCIIToUTF16(""), return builder.CreateWithButton(base::string16(),
l10n_util::GetStringUTF16(IDS_ADD), l10n_util::GetStringUTF16(IDS_ADD),
/*button_enabled=*/true); /*button_enabled=*/true);
} else if (state()->shipping_profiles().size() == 1) { } else if (state()->shipping_profiles().size() == 1) {
base::string16 truncated_content = base::string16 truncated_content =
state()->shipping_profiles()[0]->ConstructInferredLabel( GetShippingAddressLabelFormAutofillProfile(
{ *state()->shipping_profiles()[0],
autofill::ADDRESS_HOME_LINE1, autofill::ADDRESS_HOME_LINE2, state()->GetApplicationLocale());
autofill::ADDRESS_HOME_CITY, autofill::ADDRESS_HOME_STATE,
autofill::ADDRESS_HOME_COUNTRY,
},
6, state()->GetApplicationLocale());
return builder.CreateWithButton(truncated_content, return builder.CreateWithButton(truncated_content,
l10n_util::GetStringUTF16(IDS_CHOOSE), l10n_util::GetStringUTF16(IDS_CHOOSE),
/*button_enabled=*/true); /*button_enabled=*/true);
...@@ -675,14 +671,8 @@ std::unique_ptr<views::Button> PaymentSheetViewController::CreateShippingRow() { ...@@ -675,14 +671,8 @@ std::unique_ptr<views::Button> PaymentSheetViewController::CreateShippingRow() {
base::string16 format = l10n_util::GetPluralStringFUTF16( base::string16 format = l10n_util::GetPluralStringFUTF16(
IDS_PAYMENT_REQUEST_SHIPPING_ADDRESSES_PREVIEW, IDS_PAYMENT_REQUEST_SHIPPING_ADDRESSES_PREVIEW,
state()->shipping_profiles().size() - 1); state()->shipping_profiles().size() - 1);
base::string16 label = base::string16 label = GetShippingAddressLabelFormAutofillProfile(
state()->shipping_profiles()[0]->ConstructInferredLabel( *state()->shipping_profiles()[0], state()->GetApplicationLocale());
{
autofill::ADDRESS_HOME_LINE1, autofill::ADDRESS_HOME_LINE2,
autofill::ADDRESS_HOME_CITY, autofill::ADDRESS_HOME_STATE,
autofill::ADDRESS_HOME_COUNTRY,
},
6, state()->GetApplicationLocale());
return builder.CreateWithButton(label, format, return builder.CreateWithButton(label, format,
state()->shipping_profiles().size() - 1, state()->shipping_profiles().size() - 1,
l10n_util::GetStringUTF16(IDS_CHOOSE), l10n_util::GetStringUTF16(IDS_CHOOSE),
...@@ -715,8 +705,8 @@ PaymentSheetViewController::CreatePaymentMethodRow() { ...@@ -715,8 +705,8 @@ PaymentSheetViewController::CreatePaymentMethodRow() {
views::GridLayout* layout = new views::GridLayout(content_view.get()); views::GridLayout* layout = new views::GridLayout(content_view.get());
content_view->SetLayoutManager(layout); content_view->SetLayoutManager(layout);
views::ColumnSet* columns = layout->AddColumnSet(0); views::ColumnSet* columns = layout->AddColumnSet(0);
columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 1,
1, views::GridLayout::USE_PREF, 0, 0); views::GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, 0); layout->StartRow(0, 0);
std::unique_ptr<views::Label> selected_instrument_label = std::unique_ptr<views::Label> selected_instrument_label =
...@@ -741,7 +731,7 @@ PaymentSheetViewController::CreatePaymentMethodRow() { ...@@ -741,7 +731,7 @@ PaymentSheetViewController::CreatePaymentMethodRow() {
if (state()->available_instruments().empty()) { if (state()->available_instruments().empty()) {
// If the button is "Add", navigate to the editor directly. // If the button is "Add", navigate to the editor directly.
builder.Tag(PaymentSheetViewControllerTags::ADD_PAYMENT_METHOD_BUTTON); builder.Tag(PaymentSheetViewControllerTags::ADD_PAYMENT_METHOD_BUTTON);
return builder.CreateWithButton(base::ASCIIToUTF16(""), return builder.CreateWithButton(base::string16(),
l10n_util::GetStringUTF16(IDS_ADD), l10n_util::GetStringUTF16(IDS_ADD),
/*button_enabled=*/true); /*button_enabled=*/true);
} else if (state()->available_instruments().size() == 1) { } else if (state()->available_instruments().size() == 1) {
...@@ -794,29 +784,23 @@ PaymentSheetViewController::CreateContactInfoRow() { ...@@ -794,29 +784,23 @@ PaymentSheetViewController::CreateContactInfoRow() {
if (state()->contact_profiles().empty()) { if (state()->contact_profiles().empty()) {
// If the button is "Add", navigate directly to the editor. // If the button is "Add", navigate directly to the editor.
builder.Tag(PaymentSheetViewControllerTags::ADD_CONTACT_INFO_BUTTON); builder.Tag(PaymentSheetViewControllerTags::ADD_CONTACT_INFO_BUTTON);
return builder.CreateWithButton(base::ASCIIToUTF16(""), return builder.CreateWithButton(base::string16(),
l10n_util::GetStringUTF16(IDS_ADD), l10n_util::GetStringUTF16(IDS_ADD),
/*button_enabled=*/true); /*button_enabled=*/true);
} else if (state()->contact_profiles().size() == 1) { } else if (state()->contact_profiles().size() == 1) {
base::string16 truncated_content = base::string16 truncated_content =
state()->contact_profiles()[0]->ConstructInferredLabel( state()->contact_profiles()[0]->ConstructInferredLabel(
{ {autofill::NAME_FULL, autofill::PHONE_HOME_WHOLE_NUMBER,
autofill::ADDRESS_HOME_LINE1, autofill::ADDRESS_HOME_LINE2, autofill::EMAIL_ADDRESS},
autofill::ADDRESS_HOME_CITY, autofill::ADDRESS_HOME_STATE, 3, state()->GetApplicationLocale());
autofill::ADDRESS_HOME_COUNTRY,
},
6, state()->GetApplicationLocale());
return builder.CreateWithButton(truncated_content, return builder.CreateWithButton(truncated_content,
l10n_util::GetStringUTF16(IDS_CHOOSE), l10n_util::GetStringUTF16(IDS_CHOOSE),
/*button_enabled=*/true); /*button_enabled=*/true);
} else { } else {
base::string16 preview = base::string16 preview =
state()->contact_profiles()[0]->ConstructInferredLabel( state()->contact_profiles()[0]->ConstructInferredLabel(
{ {autofill::NAME_FULL, autofill::PHONE_HOME_WHOLE_NUMBER,
autofill::ADDRESS_HOME_LINE1, autofill::ADDRESS_HOME_LINE2, autofill::EMAIL_ADDRESS},
autofill::ADDRESS_HOME_CITY, autofill::ADDRESS_HOME_STATE,
autofill::ADDRESS_HOME_COUNTRY,
},
6, state()->GetApplicationLocale()); 6, state()->GetApplicationLocale());
base::string16 format = l10n_util::GetPluralStringFUTF16( base::string16 format = l10n_util::GetPluralStringFUTF16(
IDS_PAYMENT_REQUEST_CONTACTS_PREVIEW, IDS_PAYMENT_REQUEST_CONTACTS_PREVIEW,
......
...@@ -4,12 +4,53 @@ ...@@ -4,12 +4,53 @@
#include "components/payments/core/strings_util.h" #include "components/payments/core/strings_util.h"
#include <vector>
#include "base/logging.h" #include "base/logging.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
namespace payments { namespace payments {
base::string16 GetShippingAddressLabelFormAutofillProfile(
const autofill::AutofillProfile& profile,
const std::string& locale) {
// Name, phone number, and country are not included in the shipping address
// label.
static const std::vector<autofill::ServerFieldType> label_fields{
autofill::COMPANY_NAME,
autofill::ADDRESS_HOME_STREET_ADDRESS,
autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
autofill::ADDRESS_HOME_CITY,
autofill::ADDRESS_HOME_STATE,
autofill::ADDRESS_HOME_ZIP,
autofill::ADDRESS_HOME_SORTING_CODE,
};
return profile.ConstructInferredLabel(label_fields, label_fields.size(),
locale);
}
base::string16 GetBillingAddressLabelFromAutofillProfile(
const autofill::AutofillProfile& profile,
const std::string& locale) {
// Name, company, phone number, and country are not included in the billing
// address label.
static const std::vector<autofill::ServerFieldType> label_fields{
autofill::ADDRESS_HOME_STREET_ADDRESS,
autofill::ADDRESS_HOME_DEPENDENT_LOCALITY,
autofill::ADDRESS_HOME_CITY,
autofill::ADDRESS_HOME_STATE,
autofill::ADDRESS_HOME_ZIP,
autofill::ADDRESS_HOME_SORTING_CODE,
};
return profile.ConstructInferredLabel(label_fields, label_fields.size(),
locale);
}
base::string16 GetShippingAddressSelectorInfoMessage( base::string16 GetShippingAddressSelectorInfoMessage(
PaymentShippingType shipping_type) { PaymentShippingType shipping_type) {
switch (shipping_type) { switch (shipping_type) {
......
...@@ -5,11 +5,27 @@ ...@@ -5,11 +5,27 @@
#ifndef COMPONENTS_PAYMENTS_CORE_STRINGS_UTIL_H_ #ifndef COMPONENTS_PAYMENTS_CORE_STRINGS_UTIL_H_
#define COMPONENTS_PAYMENTS_CORE_STRINGS_UTIL_H_ #define COMPONENTS_PAYMENTS_CORE_STRINGS_UTIL_H_
#include <string>
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "components/payments/core/payment_options_provider.h" #include "components/payments/core/payment_options_provider.h"
namespace autofill {
class AutofillProfile;
}
namespace payments { namespace payments {
// Helper function to create a shipping address label from an autofill profile.
base::string16 GetShippingAddressLabelFormAutofillProfile(
const autofill::AutofillProfile& profile,
const std::string& locale);
// Helper function to create a billing address label from an autofill profile.
base::string16 GetBillingAddressLabelFromAutofillProfile(
const autofill::AutofillProfile& profile,
const std::string& locale);
// Gets the informational message to be displayed in the shipping address // Gets the informational message to be displayed in the shipping address
// selector view when there are no valid shipping options. // selector view when there are no valid shipping options.
base::string16 GetShippingAddressSelectorInfoMessage( base::string16 GetShippingAddressSelectorInfoMessage(
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "components/autofill/core/browser/autofill_profile.h" #include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/personal_data_manager.h" #include "components/autofill/core/browser/personal_data_manager.h"
#include "components/payments/core/strings_util.h"
#include "components/strings/grit/components_strings.h" #include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/application_context.h" #include "ios/chrome/browser/application_context.h"
#include "ios/chrome/browser/payments/payment_request.h" #include "ios/chrome/browser/payments/payment_request.h"
...@@ -32,38 +33,15 @@ NSString* GetNameLabelFromAutofillProfile( ...@@ -32,38 +33,15 @@ NSString* GetNameLabelFromAutofillProfile(
NSString* GetShippingAddressLabelFromAutofillProfile( NSString* GetShippingAddressLabelFromAutofillProfile(
const autofill::AutofillProfile& profile) { const autofill::AutofillProfile& profile) {
// Name, phone number, and country are not included in the shipping address base::string16 label = payments::GetShippingAddressLabelFormAutofillProfile(
// label. profile, GetApplicationContext()->GetApplicationLocale());
std::vector<autofill::ServerFieldType> label_fields;
label_fields.push_back(autofill::COMPANY_NAME);
label_fields.push_back(autofill::ADDRESS_HOME_STREET_ADDRESS);
label_fields.push_back(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY);
label_fields.push_back(autofill::ADDRESS_HOME_CITY);
label_fields.push_back(autofill::ADDRESS_HOME_STATE);
label_fields.push_back(autofill::ADDRESS_HOME_ZIP);
label_fields.push_back(autofill::ADDRESS_HOME_SORTING_CODE);
base::string16 label = profile.ConstructInferredLabel(
label_fields, label_fields.size(),
GetApplicationContext()->GetApplicationLocale());
return !label.empty() ? base::SysUTF16ToNSString(label) : nil; return !label.empty() ? base::SysUTF16ToNSString(label) : nil;
} }
NSString* GetBillingAddressLabelFromAutofillProfile( NSString* GetBillingAddressLabelFromAutofillProfile(
const autofill::AutofillProfile& profile) { const autofill::AutofillProfile& profile) {
// Name, company, phone number, and country are not included in the billing base::string16 label = payments::GetBillingAddressLabelFromAutofillProfile(
// address label. profile, GetApplicationContext()->GetApplicationLocale());
std::vector<autofill::ServerFieldType> label_fields;
label_fields.push_back(autofill::ADDRESS_HOME_STREET_ADDRESS);
label_fields.push_back(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY);
label_fields.push_back(autofill::ADDRESS_HOME_CITY);
label_fields.push_back(autofill::ADDRESS_HOME_STATE);
label_fields.push_back(autofill::ADDRESS_HOME_ZIP);
label_fields.push_back(autofill::ADDRESS_HOME_SORTING_CODE);
base::string16 label = profile.ConstructInferredLabel(
label_fields, label_fields.size(),
GetApplicationContext()->GetApplicationLocale());
return !label.empty() ? base::SysUTF16ToNSString(label) : nil; return !label.empty() ? base::SysUTF16ToNSString(label) : nil;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment