Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 18 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 19 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" | |
| 18 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" | 20 #include "chrome/browser/ui/views/payments/payment_request_views_util.h" |
| 19 #include "chrome/browser/ui/views/payments/preselected_combobox_model.h" | 21 #include "chrome/browser/ui/views/payments/preselected_combobox_model.h" |
| 20 #include "chrome/browser/ui/views/payments/validating_combobox.h" | 22 #include "chrome/browser/ui/views/payments/validating_combobox.h" |
| 21 #include "chrome/browser/ui/views/payments/validating_textfield.h" | 23 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 22 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
| 25 #include "components/autofill/core/browser/address_combobox_model.h" | |
| 23 #include "components/autofill/core/browser/autofill_data_util.h" | 26 #include "components/autofill/core/browser/autofill_data_util.h" |
| 24 #include "components/autofill/core/browser/autofill_type.h" | 27 #include "components/autofill/core/browser/autofill_type.h" |
| 25 #include "components/autofill/core/browser/credit_card.h" | 28 #include "components/autofill/core/browser/credit_card.h" |
| 26 #include "components/autofill/core/browser/field_types.h" | 29 #include "components/autofill/core/browser/field_types.h" |
| 27 #include "components/autofill/core/browser/personal_data_manager.h" | 30 #include "components/autofill/core/browser/personal_data_manager.h" |
| 28 #include "components/autofill/core/browser/validation.h" | 31 #include "components/autofill/core/browser/validation.h" |
| 29 #include "components/autofill/core/common/autofill_clock.h" | 32 #include "components/autofill/core/common/autofill_clock.h" |
| 30 #include "components/autofill/core/common/autofill_constants.h" | 33 #include "components/autofill/core/common/autofill_constants.h" |
| 31 #include "components/payments/content/payment_request_spec.h" | 34 #include "components/payments/content/payment_request_spec.h" |
| 32 #include "components/payments/content/payment_request_state.h" | 35 #include "components/payments/content/payment_request_state.h" |
| 33 #include "components/strings/grit/components_strings.h" | 36 #include "components/strings/grit/components_strings.h" |
| 34 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 35 #include "ui/native_theme/native_theme.h" | 38 #include "ui/native_theme/native_theme.h" |
| 39 #include "ui/views/controls/button/md_text_button.h" | |
| 36 #include "ui/views/controls/image_view.h" | 40 #include "ui/views/controls/image_view.h" |
| 37 #include "ui/views/controls/label.h" | 41 #include "ui/views/controls/label.h" |
| 38 #include "ui/views/controls/textfield/textfield.h" | 42 #include "ui/views/controls/textfield/textfield.h" |
| 39 #include "ui/views/layout/box_layout.h" | 43 #include "ui/views/layout/box_layout.h" |
| 44 #include "ui/views/layout/grid_layout.h" | |
| 40 #include "ui/views/view.h" | 45 #include "ui/views/view.h" |
| 41 | 46 |
| 42 namespace payments { | 47 namespace payments { |
| 43 | 48 |
| 44 namespace { | 49 namespace { |
| 45 | 50 |
| 46 // Number of years (including the current one) to be shown in the expiration | 51 // Number of years (including the current one) to be shown in the expiration |
| 47 // year dropdown. | 52 // year dropdown. |
| 48 const int kNumberOfExpirationYears = 10; | 53 const int kNumberOfExpirationYears = 10; |
| 49 | 54 |
| 55 // This is not quite right but is the closest server type that wasn't already | |
| 56 // used. | |
| 57 const auto kBillingAddressType = autofill::ADDRESS_BILLING_LINE1; | |
| 58 | |
| 50 // Returns the items that are in the expiration month dropdown. Will return the | 59 // Returns the items that are in the expiration month dropdown. Will return the |
| 51 // months in order starting at "01" until "12". Uses a clock so that the | 60 // months in order starting at "01" until "12". Uses a clock so that the |
| 52 // |default_index| is set to the current month. | 61 // |default_index| is set to the current month. |
| 53 std::vector<base::string16> GetExpirationMonthItems(int* default_index) { | 62 std::vector<base::string16> GetExpirationMonthItems(int* default_index) { |
| 54 std::vector<base::string16> months; | 63 std::vector<base::string16> months; |
| 55 months.reserve(12); | 64 months.reserve(12); |
| 56 for (int i = 1; i <= 12; i++) | 65 for (int i = 1; i <= 12; i++) |
| 57 months.push_back(base::UTF8ToUTF16(base::StringPrintf("%02d", i))); | 66 months.push_back(base::UTF8ToUTF16(base::StringPrintf("%02d", i))); |
| 58 | 67 |
| 59 base::Time::Exploded now_exploded; | 68 base::Time::Exploded now_exploded; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 76 } | 85 } |
| 77 return years; | 86 return years; |
| 78 } | 87 } |
| 79 | 88 |
| 80 } // namespace | 89 } // namespace |
| 81 | 90 |
| 82 CreditCardEditorViewController::CreditCardEditorViewController( | 91 CreditCardEditorViewController::CreditCardEditorViewController( |
| 83 PaymentRequestSpec* spec, | 92 PaymentRequestSpec* spec, |
| 84 PaymentRequestState* state, | 93 PaymentRequestState* state, |
| 85 PaymentRequestDialogView* dialog, | 94 PaymentRequestDialogView* dialog, |
| 95 BackNavigationType back_navigation, | |
| 96 int next_ui_tag, | |
| 86 base::OnceClosure on_edited, | 97 base::OnceClosure on_edited, |
| 87 base::OnceCallback<void(const autofill::CreditCard&)> on_added, | 98 base::OnceCallback<void(const autofill::CreditCard&)> on_added, |
| 88 autofill::CreditCard* credit_card) | 99 autofill::CreditCard* credit_card) |
| 89 : EditorViewController(spec, state, dialog), | 100 : EditorViewController(spec, state, dialog, back_navigation), |
| 90 on_edited_(std::move(on_edited)), | 101 on_edited_(std::move(on_edited)), |
| 91 on_added_(std::move(on_added)), | 102 on_added_(std::move(on_added)), |
| 92 credit_card_to_edit_(credit_card) {} | 103 credit_card_to_edit_(credit_card), |
| 104 add_billing_address_button_tag_(next_ui_tag) {} | |
| 93 | 105 |
| 94 CreditCardEditorViewController::~CreditCardEditorViewController() {} | 106 CreditCardEditorViewController::~CreditCardEditorViewController() {} |
| 95 | 107 |
| 96 // Creates the "Cards accepted" view with a row of icons at the top of the | 108 // Creates the "Cards accepted" view with a row of icons at the top of the |
| 97 // credit card editor. | 109 // credit card editor. |
| 98 // +----------------------------------------------+ | 110 // +----------------------------------------------+ |
| 99 // | Cards Accepted | | 111 // | Cards Accepted | |
| 100 // | | | 112 // | | |
| 101 // | | VISA | | MC | | AMEX | | | 113 // | | VISA | | MC | | AMEX | | |
| 102 // +----------------------------------------------+ | 114 // +----------------------------------------------+ |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 base::UTF8ToUTF16(supported_network)); | 153 base::UTF8ToUTF16(supported_network)); |
| 142 card_icon_view->SetImageSize(kCardIconSize); | 154 card_icon_view->SetImageSize(kCardIconSize); |
| 143 | 155 |
| 144 icons_row->AddChildView(card_icon_view.release()); | 156 icons_row->AddChildView(card_icon_view.release()); |
| 145 } | 157 } |
| 146 view->AddChildView(icons_row.release()); | 158 view->AddChildView(icons_row.release()); |
| 147 | 159 |
| 148 return view; | 160 return view; |
| 149 } | 161 } |
| 150 | 162 |
| 163 // Creates the "Billing Address" custom field view. | |
| 164 // +------------------------------------+ | |
| 165 // Label* | | Combobox | | Add button | | | |
| 166 // +------------------------------------+ | |
| 167 std::unique_ptr<views::View> | |
| 168 CreditCardEditorViewController::CreateCustomFieldView( | |
| 169 autofill::ServerFieldType type) { | |
| 170 if (type != kBillingAddressType) | |
| 171 return std::unique_ptr<views::View>(); | |
| 172 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); | |
| 173 | |
| 174 std::unique_ptr<views::GridLayout> layout = | |
| 175 base::MakeUnique<views::GridLayout>(view.get()); | |
| 176 | |
| 177 // Combobox column. | |
| 178 views::ColumnSet* columns = layout->AddColumnSet(0); | |
| 179 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::CENTER, 0, | |
| 180 views::GridLayout::FIXED, kMaximumLabelWidth, 0); | |
|
MAD
2017/05/05 18:32:34
We could also use kNumCharactersInLongField if you
| |
| 181 | |
| 182 // This is the horizontal padding between the combobox and the add button. | |
| 183 constexpr int kComboboxAddButtonHorizontalPadding = 8; | |
| 184 columns->AddPaddingColumn(0, kComboboxAddButtonHorizontalPadding); | |
| 185 | |
| 186 columns->AddColumn(views::GridLayout::LEADING, views::GridLayout::CENTER, 0, | |
| 187 views::GridLayout::USE_PREF, 0, 0); | |
| 188 | |
| 189 layout->StartRow(0, 0); | |
| 190 | |
| 191 EditorField billing_address_field( | |
| 192 kBillingAddressType, | |
| 193 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_BILLING_ADDRESS), | |
| 194 EditorField::LengthHint::HINT_SHORT, /*required=*/true, | |
| 195 EditorField::ControlType::COMBOBOX); | |
| 196 | |
| 197 // The combobox filled with potential billing addresses. | |
| 198 std::unique_ptr<autofill::AddressComboboxModel> address_combobox_model = | |
| 199 base::MakeUnique<autofill::AddressComboboxModel>( | |
| 200 *state()->GetPersonalDataManager(), state()->GetApplicationLocale()); | |
| 201 int selected_index = -1; | |
| 202 if (credit_card_to_edit_ && | |
| 203 !credit_card_to_edit_->billing_address_id().empty()) { | |
| 204 selected_index = address_combobox_model->GetIndexOfIdentifier( | |
| 205 credit_card_to_edit_->billing_address_id()); | |
| 206 } | |
| 207 // This takes care of rare cases where the the billing address set on the | |
| 208 // current card isn't valid anymore. | |
| 209 if (selected_index == -1) | |
| 210 selected_index = address_combobox_model->GetDefaultIndex(); | |
| 211 | |
| 212 ValidatingCombobox* combobox = | |
| 213 new ValidatingCombobox(std::move(address_combobox_model), | |
| 214 CreateValidationDelegate(billing_address_field)); | |
| 215 combobox->SetSelectedIndex(selected_index); | |
| 216 | |
| 217 // Using autofill field type as a view ID (for testing). | |
| 218 combobox->set_id(static_cast<int>(billing_address_field.type)); | |
| 219 combobox->set_listener(this); | |
| 220 | |
| 221 // |combobox| will now be owned by |row|. | |
| 222 layout->AddView(combobox); | |
| 223 | |
| 224 // The button to add new billing addresses. | |
| 225 std::unique_ptr<views::Button> add_button( | |
| 226 views::MdTextButton::Create(this, l10n_util::GetStringUTF16(IDS_ADD))); | |
| 227 add_button->set_id( | |
| 228 static_cast<int>(DialogViewID::ADD_BILLING_ADDRESS_BUTTON)); | |
| 229 add_button->set_tag(add_billing_address_button_tag_); | |
| 230 | |
| 231 // |add_button| will now be owned by |row|. | |
| 232 layout->AddView(add_button.release()); | |
| 233 view->SetLayoutManager(layout.release()); | |
| 234 return view; | |
| 235 } | |
| 236 | |
| 151 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { | 237 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { |
| 152 return std::vector<EditorField>{ | 238 return std::vector<EditorField>{ |
| 153 {autofill::CREDIT_CARD_NUMBER, | 239 {autofill::CREDIT_CARD_NUMBER, |
| 154 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), | 240 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), |
| 155 EditorField::LengthHint::HINT_LONG, /* required= */ true}, | 241 EditorField::LengthHint::HINT_LONG, /* required= */ true}, |
| 156 {autofill::CREDIT_CARD_NAME_FULL, | 242 {autofill::CREDIT_CARD_NAME_FULL, |
| 157 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), | 243 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), |
| 158 EditorField::LengthHint::HINT_LONG, /* required= */ true}, | 244 EditorField::LengthHint::HINT_LONG, /* required= */ true}, |
| 159 {autofill::CREDIT_CARD_EXP_MONTH, | 245 {autofill::CREDIT_CARD_EXP_MONTH, |
| 160 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), | 246 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), |
| 161 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 247 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 162 EditorField::ControlType::COMBOBOX}, | 248 EditorField::ControlType::COMBOBOX}, |
| 163 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, | 249 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 164 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), | 250 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), |
| 165 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 251 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 166 EditorField::ControlType::COMBOBOX}}; | 252 EditorField::ControlType::COMBOBOX}, |
| 253 {kBillingAddressType, | |
| 254 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_BILLING_ADDRESS), | |
| 255 EditorField::LengthHint::HINT_LONG, /* required= */ true, | |
| 256 EditorField::ControlType::CUSTOMFIELD}}; | |
| 167 } | 257 } |
| 168 | 258 |
| 169 base::string16 CreditCardEditorViewController::GetInitialValueForType( | 259 base::string16 CreditCardEditorViewController::GetInitialValueForType( |
| 170 autofill::ServerFieldType type) { | 260 autofill::ServerFieldType type) { |
| 171 if (!credit_card_to_edit_) | 261 if (!credit_card_to_edit_) |
| 172 return base::string16(); | 262 return base::string16(); |
| 173 | 263 |
| 174 return credit_card_to_edit_->GetInfo(autofill::AutofillType(type), | 264 return credit_card_to_edit_->GetInfo(autofill::AutofillType(type), |
| 175 state()->GetApplicationLocale()); | 265 state()->GetApplicationLocale()); |
| 176 } | 266 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 197 autofill::AutofillType(field.second.type).group()); | 287 autofill::AutofillType(field.second.type).group()); |
| 198 ValidatingCombobox* combobox = field.first; | 288 ValidatingCombobox* combobox = field.first; |
| 199 if (combobox->invalid()) | 289 if (combobox->invalid()) |
| 200 return false; | 290 return false; |
| 201 | 291 |
| 202 credit_card.SetInfo(autofill::AutofillType(field.second.type), | 292 credit_card.SetInfo(autofill::AutofillType(field.second.type), |
| 203 combobox->GetTextForRow(combobox->selected_index()), | 293 combobox->GetTextForRow(combobox->selected_index()), |
| 204 locale); | 294 locale); |
| 205 } | 295 } |
| 206 | 296 |
| 297 views::Combobox* address_combobox = | |
| 298 static_cast<views::Combobox*>(dialog()->GetViewByID(kBillingAddressType)); | |
| 299 autofill::AddressComboboxModel* model = | |
| 300 static_cast<autofill::AddressComboboxModel*>(address_combobox->model()); | |
| 301 | |
| 302 credit_card.set_billing_address_id( | |
| 303 model->GetItemIdentifierAt(address_combobox->selected_index())); | |
| 304 | |
| 207 // TODO(crbug.com/711365): Display global error message. | 305 // TODO(crbug.com/711365): Display global error message. |
| 208 if (autofill::GetCompletionStatusForCard(credit_card, locale) != | 306 if (autofill::GetCompletionStatusForCard( |
| 307 credit_card, locale, | |
| 308 state()->GetPersonalDataManager()->GetProfiles()) != | |
| 209 autofill::CREDIT_CARD_COMPLETE) { | 309 autofill::CREDIT_CARD_COMPLETE) { |
| 210 return false; | 310 return false; |
| 211 } | 311 } |
| 212 | 312 |
| 213 if (!credit_card_to_edit_) { | 313 if (!credit_card_to_edit_) { |
| 214 // Add the card (will not add a duplicate). | 314 // Add the card (will not add a duplicate). |
| 215 state()->GetPersonalDataManager()->AddCreditCard(credit_card); | 315 state()->GetPersonalDataManager()->AddCreditCard(credit_card); |
| 216 std::move(on_added_).Run(credit_card); | 316 std::move(on_added_).Run(credit_card); |
| 217 } else { | 317 } else { |
| 318 credit_card_to_edit_->set_billing_address_id( | |
| 319 credit_card.billing_address_id()); | |
| 218 // We were in edit mode. Copy the data from the temporary object to retain | 320 // We were in edit mode. Copy the data from the temporary object to retain |
| 219 // the edited object's other properties (use count, use date, guid, etc.). | 321 // the edited object's other properties (use count, use date, guid, etc.). |
| 220 for (const auto& field : text_fields()) { | 322 for (const auto& field : text_fields()) { |
| 221 credit_card_to_edit_->SetInfo( | 323 credit_card_to_edit_->SetInfo( |
| 222 autofill::AutofillType(field.second.type), | 324 autofill::AutofillType(field.second.type), |
| 223 credit_card.GetInfo(autofill::AutofillType(field.second.type), | 325 credit_card.GetInfo(autofill::AutofillType(field.second.type), |
| 224 locale), | 326 locale), |
| 225 locale); | 327 locale); |
| 226 } | 328 } |
| 227 for (const auto& field : comboboxes()) { | 329 for (const auto& field : comboboxes()) { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 252 } | 354 } |
| 253 | 355 |
| 254 std::unique_ptr<ui::ComboboxModel> | 356 std::unique_ptr<ui::ComboboxModel> |
| 255 CreditCardEditorViewController::GetComboboxModelForType( | 357 CreditCardEditorViewController::GetComboboxModelForType( |
| 256 const autofill::ServerFieldType& type) { | 358 const autofill::ServerFieldType& type) { |
| 257 switch (type) { | 359 switch (type) { |
| 258 case autofill::CREDIT_CARD_EXP_MONTH: { | 360 case autofill::CREDIT_CARD_EXP_MONTH: { |
| 259 int default_index = 0; | 361 int default_index = 0; |
| 260 std::vector<base::string16> months = | 362 std::vector<base::string16> months = |
| 261 GetExpirationMonthItems(&default_index); | 363 GetExpirationMonthItems(&default_index); |
| 262 return std::unique_ptr<ui::ComboboxModel>( | 364 return base::MakeUnique<PreselectedComboboxModel>(months, default_index); |
| 263 new PreselectedComboboxModel(months, default_index)); | |
| 264 } | 365 } |
| 265 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: | 366 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 266 return std::unique_ptr<ui::ComboboxModel>( | 367 return base::MakeUnique<ui::SimpleComboboxModel>( |
| 267 new ui::SimpleComboboxModel(GetExpirationYearItems())); | 368 GetExpirationYearItems()); |
| 268 default: | 369 default: |
| 269 NOTREACHED(); | 370 NOTREACHED(); |
| 270 break; | 371 break; |
| 271 } | 372 } |
| 272 return std::unique_ptr<ui::ComboboxModel>(); | 373 return std::unique_ptr<ui::ComboboxModel>(); |
| 273 } | 374 } |
| 274 | 375 |
| 275 base::string16 CreditCardEditorViewController::GetSheetTitle() { | 376 base::string16 CreditCardEditorViewController::GetSheetTitle() { |
| 276 if (!credit_card_to_edit_) | 377 if (!credit_card_to_edit_) |
| 277 return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD); | 378 return l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_CARD); |
| 278 | 379 |
| 279 // Gets the completion message, or empty if nothing is missing from the card. | 380 // Gets the completion message, or empty if nothing is missing from the card. |
| 280 base::string16 title = autofill::GetCompletionMessageForCard( | 381 base::string16 title = autofill::GetCompletionMessageForCard( |
| 281 autofill::GetCompletionStatusForCard(*credit_card_to_edit_, | 382 autofill::GetCompletionStatusForCard( |
| 282 state()->GetApplicationLocale())); | 383 *credit_card_to_edit_, state()->GetApplicationLocale(), |
| 384 state()->GetPersonalDataManager()->GetProfiles())); | |
| 283 return title.empty() ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_CARD) | 385 return title.empty() ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_CARD) |
| 284 : title; | 386 : title; |
| 285 } | 387 } |
| 286 | 388 |
| 389 void CreditCardEditorViewController::ButtonPressed(views::Button* sender, | |
| 390 const ui::Event& event) { | |
| 391 if (sender->tag() == add_billing_address_button_tag_) { | |
| 392 dialog()->ShowShippingAddressEditor( | |
| 393 BackNavigationType::kOneStep, | |
| 394 /*on_edited=*/ | |
| 395 base::OnceClosure(), | |
| 396 /*on_added=*/ | |
| 397 base::BindOnce( | |
| 398 &CreditCardEditorViewController::AddAndSelectNewBillingAddress, | |
| 399 base::Unretained(this)), | |
| 400 /*profile=*/nullptr); | |
| 401 } else { | |
| 402 EditorViewController::ButtonPressed(sender, event); | |
| 403 } | |
| 404 } | |
| 405 | |
| 406 void CreditCardEditorViewController::AddAndSelectNewBillingAddress( | |
| 407 const autofill::AutofillProfile& profile) { | |
| 408 views::Combobox* address_combobox = | |
| 409 static_cast<views::Combobox*>(dialog()->GetViewByID(kBillingAddressType)); | |
| 410 autofill::AddressComboboxModel* model = | |
| 411 static_cast<autofill::AddressComboboxModel*>(address_combobox->model()); | |
| 412 int index = model->AddNewProfile(profile); | |
| 413 address_combobox->SetSelectedIndex(index); | |
| 414 // Force revalidation. | |
| 415 address_combobox->OnBlur(); | |
| 416 } | |
| 417 | |
| 287 CreditCardEditorViewController::CreditCardValidationDelegate:: | 418 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 288 CreditCardValidationDelegate( | 419 CreditCardValidationDelegate( |
| 289 const EditorField& field, | 420 const EditorField& field, |
| 290 EditorViewController* controller, | 421 EditorViewController* controller, |
| 291 const std::vector<std::string>& supported_card_networks) | 422 const std::vector<std::string>& supported_card_networks) |
| 292 : field_(field), | 423 : field_(field), |
| 293 controller_(controller), | 424 controller_(controller), |
| 294 supported_card_networks_(supported_card_networks.begin(), | 425 supported_card_networks_(supported_card_networks.begin(), |
| 295 supported_card_networks.end()) {} | 426 supported_card_networks.end()) {} |
| 296 CreditCardEditorViewController::CreditCardValidationDelegate:: | 427 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 297 ~CreditCardValidationDelegate() {} | 428 ~CreditCardValidationDelegate() {} |
| 298 | 429 |
| 299 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 430 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 300 ValidateTextfield(views::Textfield* textfield) { | 431 ValidateTextfield(views::Textfield* textfield) { |
| 301 return ValidateValue(textfield->text()); | 432 return ValidateValue(textfield->text()); |
| 302 } | 433 } |
| 303 | 434 |
| 304 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 435 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 305 ValidateCombobox(views::Combobox* combobox) { | 436 ValidateCombobox(views::Combobox* combobox) { |
| 437 // The billing address ID is the selected item identifier and not the combobox | |
| 438 // value itself. | |
| 439 if (field_.type == kBillingAddressType) { | |
| 440 // TODO(crbug.com/718905) Find a way to deal with existing incomplete | |
| 441 // addresses when choosing them as billing addresses. | |
| 442 autofill::AddressComboboxModel* model = | |
| 443 static_cast<autofill::AddressComboboxModel*>(combobox->model()); | |
| 444 return !model->GetItemIdentifierAt(combobox->selected_index()).empty(); | |
| 445 } | |
| 306 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); | 446 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); |
| 307 } | 447 } |
| 308 | 448 |
| 309 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 449 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 310 ValidateValue(const base::string16& value) { | 450 ValidateValue(const base::string16& value) { |
| 311 if (!value.empty()) { | 451 if (!value.empty()) { |
| 312 base::string16 error_message; | 452 base::string16 error_message; |
| 313 bool is_valid = | 453 bool is_valid = |
| 314 field_.type == autofill::CREDIT_CARD_NUMBER | 454 field_.type == autofill::CREDIT_CARD_NUMBER |
| 315 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( | 455 ? autofill::IsValidCreditCardNumberForBasicCardNetworks( |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 327 controller_->DisplayErrorMessageForField(field_, displayed_message); | 467 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 328 return is_required_valid; | 468 return is_required_valid; |
| 329 } | 469 } |
| 330 | 470 |
| 331 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { | 471 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { |
| 332 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; | 472 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; |
| 333 return true; | 473 return true; |
| 334 } | 474 } |
| 335 | 475 |
| 336 } // namespace payments | 476 } // namespace payments |
| OLD | NEW |