| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 default: | 157 default: |
| 158 NOTREACHED(); | 158 NOTREACHED(); |
| 159 break; | 159 break; |
| 160 } | 160 } |
| 161 return std::unique_ptr<ui::ComboboxModel>(); | 161 return std::unique_ptr<ui::ComboboxModel>(); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void ShippingAddressEditorViewController::OnPerformAction( | 164 void ShippingAddressEditorViewController::OnPerformAction( |
| 165 views::Combobox* sender) { | 165 views::Combobox* sender) { |
| 166 EditorViewController::OnPerformAction(sender); | 166 EditorViewController::OnPerformAction(sender); |
| 167 if (sender->id() != autofill::ADDRESS_HOME_COUNTRY) | 167 if (sender->id() != GetInputFieldViewId(autofill::ADDRESS_HOME_COUNTRY)) |
| 168 return; | 168 return; |
| 169 DCHECK_GE(sender->selected_index(), 0); | 169 DCHECK_GE(sender->selected_index(), 0); |
| 170 if (chosen_country_index_ != static_cast<size_t>(sender->selected_index())) { | 170 if (chosen_country_index_ != static_cast<size_t>(sender->selected_index())) { |
| 171 chosen_country_index_ = sender->selected_index(); | 171 chosen_country_index_ = sender->selected_index(); |
| 172 failed_to_load_region_data_ = false; | 172 failed_to_load_region_data_ = false; |
| 173 // View update must be asynchronous to let the combobox finish performing | 173 // View update must be asynchronous to let the combobox finish performing |
| 174 // the action. | 174 // the action. |
| 175 OnDataChanged(/*synchronous=*/false); | 175 OnDataChanged(/*synchronous=*/false); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 void ShippingAddressEditorViewController::UpdateEditorView() { | 179 void ShippingAddressEditorViewController::UpdateEditorView() { |
| 180 EditorViewController::UpdateEditorView(); | 180 EditorViewController::UpdateEditorView(); |
| 181 if (chosen_country_index_ > 0UL && | 181 if (chosen_country_index_ > 0UL && |
| 182 chosen_country_index_ < countries_.size()) { | 182 chosen_country_index_ < countries_.size()) { |
| 183 views::Combobox* country_combo_box = static_cast<views::Combobox*>( | 183 views::Combobox* country_combo_box = |
| 184 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY)); | 184 static_cast<views::Combobox*>(dialog()->GetViewByID( |
| 185 GetInputFieldViewId(autofill::ADDRESS_HOME_COUNTRY))); |
| 185 DCHECK(country_combo_box); | 186 DCHECK(country_combo_box); |
| 186 DCHECK_EQ(countries_.size(), | 187 DCHECK_EQ(countries_.size(), |
| 187 static_cast<size_t>(country_combo_box->GetRowCount())); | 188 static_cast<size_t>(country_combo_box->GetRowCount())); |
| 188 country_combo_box->SetSelectedIndex(chosen_country_index_); | 189 country_combo_box->SetSelectedIndex(chosen_country_index_); |
| 189 } else if (countries_.size() > 0UL) { | 190 } else if (countries_.size() > 0UL) { |
| 190 chosen_country_index_ = 0UL; | 191 chosen_country_index_ = 0UL; |
| 191 } else { | 192 } else { |
| 192 chosen_country_index_ = kInvalidCountryIndex; | 193 chosen_country_index_ = kInvalidCountryIndex; |
| 193 } | 194 } |
| 194 // Ignore temporary profile once the editor view has been updated. | 195 // Ignore temporary profile once the editor view has been updated. |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 base::Unretained(this))); | 354 base::Unretained(this))); |
| 354 } | 355 } |
| 355 } | 356 } |
| 356 | 357 |
| 357 bool ShippingAddressEditorViewController::SaveFieldsToProfile( | 358 bool ShippingAddressEditorViewController::SaveFieldsToProfile( |
| 358 autofill::AutofillProfile* profile, | 359 autofill::AutofillProfile* profile, |
| 359 bool ignore_errors) { | 360 bool ignore_errors) { |
| 360 const std::string& locale = state()->GetApplicationLocale(); | 361 const std::string& locale = state()->GetApplicationLocale(); |
| 361 // The country must be set first, because the profile uses the country to | 362 // The country must be set first, because the profile uses the country to |
| 362 // interpret some of the data (e.g., phone numbers) passed to SetInfo. | 363 // interpret some of the data (e.g., phone numbers) passed to SetInfo. |
| 363 views::Combobox* combobox = static_cast<views::Combobox*>( | 364 views::Combobox* combobox = |
| 364 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY)); | 365 static_cast<views::Combobox*>(dialog()->GetViewByID( |
| 366 GetInputFieldViewId(autofill::ADDRESS_HOME_COUNTRY))); |
| 365 // The combobox can be null when saving to temporary profile while updating | 367 // The combobox can be null when saving to temporary profile while updating |
| 366 // the view. | 368 // the view. |
| 367 if (combobox) { | 369 if (combobox) { |
| 368 base::string16 country(combobox->GetTextForRow(combobox->selected_index())); | 370 base::string16 country(combobox->GetTextForRow(combobox->selected_index())); |
| 369 bool success = | 371 bool success = |
| 370 profile->SetInfo(autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY), | 372 profile->SetInfo(autofill::AutofillType(autofill::ADDRESS_HOME_COUNTRY), |
| 371 country, locale); | 373 country, locale); |
| 372 LOG_IF(ERROR, !success && !ignore_errors) | 374 LOG_IF(ERROR, !success && !ignore_errors) |
| 373 << "Can't set profile country to: " << country; | 375 << "Can't set profile country to: " << country; |
| 374 if (!success && !ignore_errors) | 376 if (!success && !ignore_errors) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 388 } | 390 } |
| 389 LOG_IF(ERROR, !success && !ignore_errors) | 391 LOG_IF(ERROR, !success && !ignore_errors) |
| 390 << "Can't setinfo(" << field.second.type << ", " << field.first->text(); | 392 << "Can't setinfo(" << field.second.type << ", " << field.first->text(); |
| 391 if (!success && !ignore_errors) | 393 if (!success && !ignore_errors) |
| 392 return false; | 394 return false; |
| 393 } | 395 } |
| 394 for (const auto& field : comboboxes()) { | 396 for (const auto& field : comboboxes()) { |
| 395 // ValidatingCombobox* is the key, EditorField is the value. | 397 // ValidatingCombobox* is the key, EditorField is the value. |
| 396 ValidatingCombobox* combobox = field.first; | 398 ValidatingCombobox* combobox = field.first; |
| 397 // The country has already been dealt with. | 399 // The country has already been dealt with. |
| 398 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) | 400 if (combobox->id() == GetInputFieldViewId(autofill::ADDRESS_HOME_COUNTRY)) |
| 399 continue; | 401 continue; |
| 400 if (combobox->IsValid()) { | 402 if (combobox->IsValid()) { |
| 401 success = profile->SetInfo( | 403 success = profile->SetInfo( |
| 402 autofill::AutofillType(field.second.type), | 404 autofill::AutofillType(field.second.type), |
| 403 combobox->GetTextForRow(combobox->selected_index()), locale); | 405 combobox->GetTextForRow(combobox->selected_index()), locale); |
| 404 } else { | 406 } else { |
| 405 success = false; | 407 success = false; |
| 406 } | 408 } |
| 407 LOG_IF(ERROR, !success && !ignore_errors) | 409 LOG_IF(ERROR, !success && !ignore_errors) |
| 408 << "Can't setinfo(" << field.second.type << ", " | 410 << "Can't setinfo(" << field.second.type << ", " |
| 409 << combobox->GetTextForRow(combobox->selected_index()); | 411 << combobox->GetTextForRow(combobox->selected_index()); |
| 410 if (!success && !ignore_errors) | 412 if (!success && !ignore_errors) |
| 411 return false; | 413 return false; |
| 412 } | 414 } |
| 413 return success; | 415 return success; |
| 414 } | 416 } |
| 415 | 417 |
| 416 void ShippingAddressEditorViewController::OnComboboxModelChanged( | 418 void ShippingAddressEditorViewController::OnComboboxModelChanged( |
| 417 views::Combobox* combobox) { | 419 views::Combobox* combobox) { |
| 418 if (combobox->id() != autofill::ADDRESS_HOME_STATE) | 420 if (combobox->id() != GetInputFieldViewId(autofill::ADDRESS_HOME_STATE)) |
| 419 return; | 421 return; |
| 420 autofill::RegionComboboxModel* model = | 422 autofill::RegionComboboxModel* model = |
| 421 static_cast<autofill::RegionComboboxModel*>(combobox->model()); | 423 static_cast<autofill::RegionComboboxModel*>(combobox->model()); |
| 422 if (model->IsPendingRegionDataLoad()) | 424 if (model->IsPendingRegionDataLoad()) |
| 423 return; | 425 return; |
| 424 if (model->failed_to_load_data()) { | 426 if (model->failed_to_load_data()) { |
| 425 failed_to_load_region_data_ = true; | 427 failed_to_load_region_data_ = true; |
| 426 // It is safe to update synchronously since the change comes from the model | 428 // It is safe to update synchronously since the change comes from the model |
| 427 // and not from the UI. | 429 // and not from the UI. |
| 428 OnDataChanged(/*synchronous=*/true); | 430 OnDataChanged(/*synchronous=*/true); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 451 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 453 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 452 IsValidCombobox(views::Combobox* combobox) { | 454 IsValidCombobox(views::Combobox* combobox) { |
| 453 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), | 455 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), |
| 454 nullptr); | 456 nullptr); |
| 455 } | 457 } |
| 456 | 458 |
| 457 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 459 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 458 TextfieldValueChanged(views::Textfield* textfield) { | 460 TextfieldValueChanged(views::Textfield* textfield) { |
| 459 base::string16 error_message; | 461 base::string16 error_message; |
| 460 bool is_valid = ValidateValue(textfield->text(), &error_message); | 462 bool is_valid = ValidateValue(textfield->text(), &error_message); |
| 461 controller_->DisplayErrorMessageForField(field_, error_message); | 463 controller_->DisplayErrorMessageForField(field_.type, error_message); |
| 462 return is_valid; | 464 return is_valid; |
| 463 } | 465 } |
| 464 | 466 |
| 465 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 467 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 466 ComboboxValueChanged(views::Combobox* combobox) { | 468 ComboboxValueChanged(views::Combobox* combobox) { |
| 467 base::string16 error_message; | 469 base::string16 error_message; |
| 468 bool is_valid = ValidateValue( | 470 bool is_valid = ValidateValue( |
| 469 combobox->GetTextForRow(combobox->selected_index()), &error_message); | 471 combobox->GetTextForRow(combobox->selected_index()), &error_message); |
| 470 controller_->DisplayErrorMessageForField(field_, error_message); | 472 controller_->DisplayErrorMessageForField(field_.type, error_message); |
| 471 return is_valid; | 473 return is_valid; |
| 472 } | 474 } |
| 473 | 475 |
| 474 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 476 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 475 ComboboxModelChanged(views::Combobox* combobox) { | 477 ComboboxModelChanged(views::Combobox* combobox) { |
| 476 controller_->OnComboboxModelChanged(combobox); | 478 controller_->OnComboboxModelChanged(combobox); |
| 477 } | 479 } |
| 478 | 480 |
| 479 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 481 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 480 ValidateValue(const base::string16& value, base::string16* error_message) { | 482 ValidateValue(const base::string16& value, base::string16* error_message) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 494 return true; | 496 return true; |
| 495 } | 497 } |
| 496 if (error_message && field_.required) { | 498 if (error_message && field_.required) { |
| 497 *error_message = l10n_util::GetStringUTF16( | 499 *error_message = l10n_util::GetStringUTF16( |
| 498 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 500 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 499 } | 501 } |
| 500 return !field_.required; | 502 return !field_.required; |
| 501 } | 503 } |
| 502 | 504 |
| 503 } // namespace payments | 505 } // namespace payments |
| OLD | NEW |