| 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/shipping_address_editor_view_controll
er.h" | 5 #include "chrome/browser/ui/views/payments/shipping_address_editor_view_controll
er.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 86 |
| 87 std::vector<EditorField> | 87 std::vector<EditorField> |
| 88 ShippingAddressEditorViewController::GetFieldDefinitions() { | 88 ShippingAddressEditorViewController::GetFieldDefinitions() { |
| 89 return editor_fields_; | 89 return editor_fields_; |
| 90 } | 90 } |
| 91 | 91 |
| 92 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( | 92 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( |
| 93 autofill::ServerFieldType type) { | 93 autofill::ServerFieldType type) { |
| 94 // Temporary profile has precedence over profile to edit since its existence | 94 // Temporary profile has precedence over profile to edit since its existence |
| 95 // is based on having unsaved stated to restore. | 95 // is based on having unsaved stated to restore. |
| 96 if (temporary_profile_.get()) { | 96 if (temporary_profile_.get()) |
| 97 return temporary_profile_->GetInfo(autofill::AutofillType(type), | 97 return temporary_profile_->GetInfo(autofill::AutofillType(type), |
| 98 state()->GetApplicationLocale()); | 98 state()->GetApplicationLocale()); |
| 99 } | |
| 100 | 99 |
| 101 if (!profile_to_edit_) | 100 if (!profile_to_edit_) |
| 102 return base::string16(); | 101 return base::string16(); |
| 103 | 102 |
| 104 return profile_to_edit_->GetInfo(autofill::AutofillType(type), | 103 return profile_to_edit_->GetInfo(autofill::AutofillType(type), |
| 105 state()->GetApplicationLocale()); | 104 state()->GetApplicationLocale()); |
| 106 } | 105 } |
| 107 | 106 |
| 108 bool ShippingAddressEditorViewController::ValidateModelAndSave() { | 107 bool ShippingAddressEditorViewController::ValidateModelAndSave() { |
| 109 // To validate the profile first, we use a temporary object. | 108 // To validate the profile first, we use a temporary object. |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 autofill::ADDRESS_HOME_COUNTRY, | 291 autofill::ADDRESS_HOME_COUNTRY, |
| 293 l10n_util::GetStringUTF16( | 292 l10n_util::GetStringUTF16( |
| 294 IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL), | 293 IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL), |
| 295 EditorField::LengthHint::HINT_LONG, /*required=*/true, | 294 EditorField::LengthHint::HINT_LONG, /*required=*/true, |
| 296 EditorField::ControlType::COMBOBOX); | 295 EditorField::ControlType::COMBOBOX); |
| 297 } | 296 } |
| 298 } | 297 } |
| 299 } | 298 } |
| 300 // Always add phone number at the end. | 299 // Always add phone number at the end. |
| 301 editor_fields_.emplace_back( | 300 editor_fields_.emplace_back( |
| 302 autofill::PHONE_HOME_NUMBER, | 301 autofill::PHONE_HOME_WHOLE_NUMBER, |
| 303 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE), | 302 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE), |
| 304 EditorField::LengthHint::HINT_LONG, /*required=*/false, | 303 EditorField::LengthHint::HINT_LONG, /*required=*/true, |
| 305 EditorField::ControlType::TEXTFIELD); | 304 EditorField::ControlType::TEXTFIELD); |
| 306 } | 305 } |
| 307 | 306 |
| 308 void ShippingAddressEditorViewController::OnDataChanged(bool synchronous) { | 307 void ShippingAddressEditorViewController::OnDataChanged(bool synchronous) { |
| 309 temporary_profile_.reset(new autofill::AutofillProfile); | 308 temporary_profile_.reset(new autofill::AutofillProfile); |
| 310 SaveFieldsToProfile(temporary_profile_.get(), /*ignore_errors*/ true); | 309 SaveFieldsToProfile(temporary_profile_.get(), /*ignore_errors*/ true); |
| 311 | 310 |
| 312 UpdateEditorFields(); | 311 UpdateEditorFields(); |
| 313 if (synchronous) { | 312 if (synchronous) { |
| 314 UpdateEditorView(); | 313 UpdateEditorView(); |
| 315 } else { | 314 } else { |
| 316 base::ThreadTaskRunnerHandle::Get()->PostTask( | 315 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 317 FROM_HERE, | 316 FROM_HERE, |
| 318 base::BindOnce(&ShippingAddressEditorViewController::UpdateEditorView, | 317 base::BindOnce(&ShippingAddressEditorViewController::UpdateEditorView, |
| 319 base::Unretained(this))); | 318 base::Unretained(this))); |
| 320 } | 319 } |
| 321 } | 320 } |
| 322 | 321 |
| 323 bool ShippingAddressEditorViewController::SaveFieldsToProfile( | 322 bool ShippingAddressEditorViewController::SaveFieldsToProfile( |
| 324 autofill::AutofillProfile* profile, | 323 autofill::AutofillProfile* profile, |
| 325 bool ignore_errors) { | 324 bool ignore_errors) { |
| 326 const std::string& locale = state()->GetApplicationLocale(); | 325 const std::string& locale = state()->GetApplicationLocale(); |
| 327 bool success = true; | 326 bool success = true; |
| 328 for (const auto& field : text_fields()) { | 327 for (const auto& field : text_fields()) { |
| 329 // Force a blur in case the value was left untouched. | 328 // Force a blur in case the value was left untouched. |
| 330 field.first->OnBlur(); | 329 field.first->OnBlur(); |
| 331 // ValidatingTextfield* is the key, EditorField is the value. | 330 // ValidatingTextfield* is the key, EditorField is the value. |
| 332 if (field.first->invalid()) { | 331 if (field.first->invalid()) { |
| 333 success = false; | 332 success = false; |
| 334 if (!ignore_errors) | 333 } else { |
| 335 return false; | 334 success = profile->SetInfo(autofill::AutofillType(field.second.type), |
| 335 field.first->text(), locale); |
| 336 } | 336 } |
| 337 profile->SetInfo(autofill::AutofillType(field.second.type), | 337 DCHECK(success || ignore_errors) |
| 338 field.first->text(), locale); | 338 << "Can't setinfo(" << field.second.type << ", " << field.first->text(); |
| 339 if (!success && !ignore_errors) |
| 340 return false; |
| 339 } | 341 } |
| 340 for (const auto& field : comboboxes()) { | 342 for (const auto& field : comboboxes()) { |
| 341 // ValidatingCombobox* is the key, EditorField is the value. | 343 // ValidatingCombobox* is the key, EditorField is the value. |
| 342 ValidatingCombobox* combobox = field.first; | 344 ValidatingCombobox* combobox = field.first; |
| 343 if (combobox->invalid()) { | 345 if (combobox->invalid()) { |
| 344 success = false; | 346 success = false; |
| 345 if (!ignore_errors) | 347 } else { |
| 346 return false; | 348 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) { |
| 349 success = profile->SetInfo( |
| 350 autofill::AutofillType(field.second.type), |
| 351 base::UTF8ToUTF16(country_codes_[combobox->selected_index()]), |
| 352 locale); |
| 353 } else { |
| 354 success = profile->SetInfo( |
| 355 autofill::AutofillType(field.second.type), |
| 356 combobox->GetTextForRow(combobox->selected_index()), locale); |
| 357 } |
| 347 } | 358 } |
| 359 DCHECK(success || ignore_errors) |
| 360 << "Can't setinfo(" << field.second.type << ", " |
| 348 | 361 |
| 349 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) { | 362 << combobox->GetTextForRow(combobox->selected_index()); |
| 350 profile->SetInfo( | 363 if (!success && !ignore_errors) |
| 351 autofill::AutofillType(field.second.type), | 364 return false; |
| 352 base::UTF8ToUTF16(country_codes_[combobox->selected_index()]), | |
| 353 locale); | |
| 354 } else { | |
| 355 profile->SetInfo(autofill::AutofillType(field.second.type), | |
| 356 combobox->GetTextForRow(combobox->selected_index()), | |
| 357 locale); | |
| 358 } | |
| 359 } | 365 } |
| 360 return success; | 366 return success; |
| 361 } | 367 } |
| 362 | 368 |
| 363 void ShippingAddressEditorViewController::OnComboboxModelChanged( | 369 void ShippingAddressEditorViewController::OnComboboxModelChanged( |
| 364 views::Combobox* combobox) { | 370 views::Combobox* combobox) { |
| 365 if (combobox->id() != autofill::ADDRESS_HOME_STATE) | 371 if (combobox->id() != autofill::ADDRESS_HOME_STATE) |
| 366 return; | 372 return; |
| 367 autofill::RegionComboboxModel* model = | 373 autofill::RegionComboboxModel* model = |
| 368 static_cast<autofill::RegionComboboxModel*>(combobox->model()); | 374 static_cast<autofill::RegionComboboxModel*>(combobox->model()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 407 } |
| 402 | 408 |
| 403 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 409 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 404 ComboboxModelChanged(views::Combobox* combobox) { | 410 ComboboxModelChanged(views::Combobox* combobox) { |
| 405 controller_->OnComboboxModelChanged(combobox); | 411 controller_->OnComboboxModelChanged(combobox); |
| 406 } | 412 } |
| 407 | 413 |
| 408 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 414 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 409 ValidateValue(const base::string16& value) { | 415 ValidateValue(const base::string16& value) { |
| 410 if (!value.empty()) { | 416 if (!value.empty()) { |
| 411 if (field_.type == autofill::PHONE_HOME_NUMBER && | 417 if (field_.type == autofill::PHONE_HOME_WHOLE_NUMBER && |
| 412 !autofill::IsValidPhoneNumber( | 418 !autofill::IsValidPhoneNumber( |
| 413 value, | 419 value, |
| 414 controller_->country_codes_[controller_->chosen_country_index_])) { | 420 controller_->country_codes_[controller_->chosen_country_index_])) { |
| 415 controller_->DisplayErrorMessageForField( | 421 controller_->DisplayErrorMessageForField( |
| 416 field_, l10n_util::GetStringUTF16( | 422 field_, l10n_util::GetStringUTF16( |
| 417 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); | 423 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); |
| 418 return false; | 424 return false; |
| 419 } | 425 } |
| 420 // As long as other field types are non-empty, they are valid. | 426 // As long as other field types are non-empty, they are valid. |
| 421 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); | 427 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); |
| 422 return true; | 428 return true; |
| 423 } | 429 } |
| 424 bool is_required_valid = !field_.required; | 430 bool is_required_valid = !field_.required; |
| 425 const base::string16 displayed_message = | 431 const base::string16 displayed_message = |
| 426 is_required_valid ? base::ASCIIToUTF16("") | 432 is_required_valid ? base::ASCIIToUTF16("") |
| 427 : l10n_util::GetStringUTF16( | 433 : l10n_util::GetStringUTF16( |
| 428 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 434 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 429 controller_->DisplayErrorMessageForField(field_, displayed_message); | 435 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 430 return is_required_valid; | 436 return is_required_valid; |
| 431 } | 437 } |
| 432 | 438 |
| 433 } // namespace payments | 439 } // namespace payments |
| OLD | NEW |