| 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" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" | 15 #include "chrome/browser/ui/views/payments/payment_request_dialog_view.h" |
| 16 #include "chrome/browser/ui/views/payments/payment_request_dialog_view_ids.h" |
| 16 #include "chrome/browser/ui/views/payments/validating_combobox.h" | 17 #include "chrome/browser/ui/views/payments/validating_combobox.h" |
| 17 #include "chrome/browser/ui/views/payments/validating_textfield.h" | 18 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 18 #include "chrome/grit/generated_resources.h" | 19 #include "chrome/grit/generated_resources.h" |
| 19 #include "components/autofill/core/browser/autofill_address_util.h" | 20 #include "components/autofill/core/browser/autofill_address_util.h" |
| 20 #include "components/autofill/core/browser/autofill_country.h" | 21 #include "components/autofill/core/browser/autofill_country.h" |
| 21 #include "components/autofill/core/browser/autofill_type.h" | 22 #include "components/autofill/core/browser/autofill_type.h" |
| 22 #include "components/autofill/core/browser/country_combobox_model.h" | 23 #include "components/autofill/core/browser/country_combobox_model.h" |
| 23 #include "components/autofill/core/browser/field_types.h" | 24 #include "components/autofill/core/browser/field_types.h" |
| 24 #include "components/autofill/core/browser/personal_data_manager.h" | 25 #include "components/autofill/core/browser/personal_data_manager.h" |
| 25 #include "components/autofill/core/browser/region_combobox_model.h" | 26 #include "components/autofill/core/browser/region_combobox_model.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 NOTREACHED(); | 62 NOTREACHED(); |
| 62 return autofill::UNKNOWN_TYPE; | 63 return autofill::UNKNOWN_TYPE; |
| 63 } | 64 } |
| 64 | 65 |
| 65 } // namespace | 66 } // namespace |
| 66 | 67 |
| 67 ShippingAddressEditorViewController::ShippingAddressEditorViewController( | 68 ShippingAddressEditorViewController::ShippingAddressEditorViewController( |
| 68 PaymentRequestSpec* spec, | 69 PaymentRequestSpec* spec, |
| 69 PaymentRequestState* state, | 70 PaymentRequestState* state, |
| 70 PaymentRequestDialogView* dialog, | 71 PaymentRequestDialogView* dialog, |
| 72 BackNavigationType back_navigation_type, |
| 71 base::OnceClosure on_edited, | 73 base::OnceClosure on_edited, |
| 72 base::OnceCallback<void(const autofill::AutofillProfile&)> on_added, | 74 base::OnceCallback<void(const autofill::AutofillProfile&)> on_added, |
| 73 autofill::AutofillProfile* profile) | 75 autofill::AutofillProfile* profile) |
| 74 : EditorViewController(spec, state, dialog), | 76 : EditorViewController(spec, state, dialog, back_navigation_type), |
| 75 on_edited_(std::move(on_edited)), | 77 on_edited_(std::move(on_edited)), |
| 76 on_added_(std::move(on_added)), | 78 on_added_(std::move(on_added)), |
| 77 profile_to_edit_(profile), | 79 profile_to_edit_(profile), |
| 78 chosen_country_index_(0), | 80 chosen_country_index_(0), |
| 79 failed_to_load_region_data_(false) { | 81 failed_to_load_region_data_(false) { |
| 80 UpdateEditorFields(); | 82 UpdateEditorFields(); |
| 81 } | 83 } |
| 82 | 84 |
| 83 ShippingAddressEditorViewController::~ShippingAddressEditorViewController() {} | 85 ShippingAddressEditorViewController::~ShippingAddressEditorViewController() {} |
| 84 | 86 |
| 85 std::unique_ptr<views::View> | |
| 86 ShippingAddressEditorViewController::CreateHeaderView() { | |
| 87 return base::MakeUnique<views::View>(); | |
| 88 } | |
| 89 | |
| 90 std::vector<EditorField> | 87 std::vector<EditorField> |
| 91 ShippingAddressEditorViewController::GetFieldDefinitions() { | 88 ShippingAddressEditorViewController::GetFieldDefinitions() { |
| 92 return editor_fields_; | 89 return editor_fields_; |
| 93 } | 90 } |
| 94 | 91 |
| 95 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( | 92 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( |
| 96 autofill::ServerFieldType type) { | 93 autofill::ServerFieldType type) { |
| 97 // Temporary profile has precedence over profile to edit since its existence | 94 // Temporary profile has precedence over profile to edit since its existence |
| 98 // is based on having unsaved stated to restore. | 95 // is based on having unsaved stated to restore. |
| 99 if (temporary_profile_.get()) { | 96 if (temporary_profile_.get()) { |
| 100 return temporary_profile_->GetInfo(autofill::AutofillType(type), | 97 return temporary_profile_->GetInfo(autofill::AutofillType(type), |
| 101 state()->GetApplicationLocale()); | 98 state()->GetApplicationLocale()); |
| 102 } | 99 } |
| 103 | 100 |
| 104 if (!profile_to_edit_) | 101 if (!profile_to_edit_) |
| 105 return base::string16(); | 102 return base::string16(); |
| 106 | 103 |
| 107 return profile_to_edit_->GetInfo(autofill::AutofillType(type), | 104 return profile_to_edit_->GetInfo(autofill::AutofillType(type), |
| 108 state()->GetApplicationLocale()); | 105 state()->GetApplicationLocale()); |
| 109 } | 106 } |
| 110 | 107 |
| 111 bool ShippingAddressEditorViewController::ValidateModelAndSave() { | 108 bool ShippingAddressEditorViewController::ValidateModelAndSave() { |
| 112 // To validate the profile first, we use a temporary object. | 109 // To validate the profile first, we use a temporary object. |
| 113 autofill::AutofillProfile profile; | 110 autofill::AutofillProfile profile; |
| 114 if (!SaveFieldsToProfile(&profile, /*ignore_errors=*/false)) | 111 if (!SaveFieldsToProfile(&profile, /*ignore_errors=*/false)) |
| 115 return false; | 112 return false; |
| 116 | |
| 117 if (!profile_to_edit_) { | 113 if (!profile_to_edit_) { |
| 118 // Add the profile (will not add a duplicate). | 114 // Add the profile (will not add a duplicate). |
| 119 profile.set_origin(autofill::kSettingsOrigin); | 115 profile.set_origin(autofill::kSettingsOrigin); |
| 120 state()->GetPersonalDataManager()->AddProfile(profile); | 116 state()->GetPersonalDataManager()->AddProfile(profile); |
| 121 std::move(on_added_).Run(profile); | 117 std::move(on_added_).Run(profile); |
| 122 on_edited_.Reset(); | 118 on_edited_.Reset(); |
| 123 } else { | 119 } else { |
| 124 // Copy the temporary object's data to the object to be edited. Prefer this | 120 // Copy the temporary object's data to the object to be edited. Prefer this |
| 125 // method to copying |profile| into |profile_to_edit_|, because the latter | 121 // method to copying |profile| into |profile_to_edit_|, because the latter |
| 126 // object needs to retain other properties (use count, use date, guid, | 122 // object needs to retain other properties (use count, use date, guid, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 temporary_profile_.reset(nullptr); | 212 temporary_profile_.reset(nullptr); |
| 217 } | 213 } |
| 218 | 214 |
| 219 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { | 215 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { |
| 220 // TODO(crbug.com/712074): Editor title should reflect the missing information | 216 // TODO(crbug.com/712074): Editor title should reflect the missing information |
| 221 // in the case that one or more fields are missing. | 217 // in the case that one or more fields are missing. |
| 222 return profile_to_edit_ ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_ADDRESS) | 218 return profile_to_edit_ ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_ADDRESS) |
| 223 : l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_ADDRESS); | 219 : l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_ADDRESS); |
| 224 } | 220 } |
| 225 | 221 |
| 222 std::unique_ptr<views::Button> |
| 223 ShippingAddressEditorViewController::CreatePrimaryButton() { |
| 224 std::unique_ptr<views::Button> button( |
| 225 EditorViewController::CreatePrimaryButton()); |
| 226 button->set_id(static_cast<int>(DialogViewID::SAVE_ADDRESS_BUTTON)); |
| 227 return button; |
| 228 } |
| 229 |
| 226 void ShippingAddressEditorViewController::UpdateEditorFields() { | 230 void ShippingAddressEditorViewController::UpdateEditorFields() { |
| 227 editor_fields_.clear(); | 231 editor_fields_.clear(); |
| 228 std::string chosen_country_code; | 232 std::string chosen_country_code; |
| 229 if (chosen_country_index_ < country_codes_.size()) | 233 if (chosen_country_index_ < country_codes_.size()) |
| 230 chosen_country_code = country_codes_[chosen_country_index_]; | 234 chosen_country_code = country_codes_[chosen_country_index_]; |
| 231 | 235 |
| 232 std::unique_ptr<base::ListValue> components(new base::ListValue); | 236 std::unique_ptr<base::ListValue> components(new base::ListValue); |
| 233 std::string unused; | 237 std::string unused; |
| 234 autofill::GetAddressComponents(chosen_country_code, | 238 autofill::GetAddressComponents(chosen_country_code, |
| 235 state()->GetApplicationLocale(), | 239 state()->GetApplicationLocale(), |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 controller_->country_codes_[controller_->chosen_country_index_])) { | 414 controller_->country_codes_[controller_->chosen_country_index_])) { |
| 411 controller_->DisplayErrorMessageForField( | 415 controller_->DisplayErrorMessageForField( |
| 412 field_, l10n_util::GetStringUTF16( | 416 field_, l10n_util::GetStringUTF16( |
| 413 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); | 417 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); |
| 414 return false; | 418 return false; |
| 415 } | 419 } |
| 416 // As long as other field types are non-empty, they are valid. | 420 // As long as other field types are non-empty, they are valid. |
| 417 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); | 421 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); |
| 418 return true; | 422 return true; |
| 419 } | 423 } |
| 420 | |
| 421 bool is_required_valid = !field_.required; | 424 bool is_required_valid = !field_.required; |
| 422 const base::string16 displayed_message = | 425 const base::string16 displayed_message = |
| 423 is_required_valid ? base::ASCIIToUTF16("") | 426 is_required_valid ? base::ASCIIToUTF16("") |
| 424 : l10n_util::GetStringUTF16( | 427 : l10n_util::GetStringUTF16( |
| 425 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 428 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 426 controller_->DisplayErrorMessageForField(field_, displayed_message); | 429 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 427 return is_required_valid; | 430 return is_required_valid; |
| 428 } | 431 } |
| 429 | 432 |
| 430 } // namespace payments | 433 } // namespace payments |
| OLD | NEW |