| 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> | 87 std::unique_ptr<views::View> |
| 86 ShippingAddressEditorViewController::CreateHeaderView() { | 88 ShippingAddressEditorViewController::CreateHeaderView() { |
| 87 return base::MakeUnique<views::View>(); | 89 return base::MakeUnique<views::View>(); |
| 88 } | 90 } |
| 89 | 91 |
| 92 std::unique_ptr<views::View> |
| 93 ShippingAddressEditorViewController::CreateCustomFieldView( |
| 94 autofill::ServerFieldType type) { |
| 95 return base::MakeUnique<views::View>(); |
| 96 } |
| 97 |
| 90 std::vector<EditorField> | 98 std::vector<EditorField> |
| 91 ShippingAddressEditorViewController::GetFieldDefinitions() { | 99 ShippingAddressEditorViewController::GetFieldDefinitions() { |
| 92 return editor_fields_; | 100 return editor_fields_; |
| 93 } | 101 } |
| 94 | 102 |
| 95 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( | 103 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( |
| 96 autofill::ServerFieldType type) { | 104 autofill::ServerFieldType type) { |
| 97 // Temporary profile has precedence over profile to edit since its existence | 105 // Temporary profile has precedence over profile to edit since its existence |
| 98 // is based on having unsaved stated to restore. | 106 // is based on having unsaved stated to restore. |
| 99 if (temporary_profile_.get()) { | 107 if (temporary_profile_.get()) { |
| 100 return temporary_profile_->GetInfo(autofill::AutofillType(type), | 108 return temporary_profile_->GetInfo(autofill::AutofillType(type), |
| 101 state()->GetApplicationLocale()); | 109 state()->GetApplicationLocale()); |
| 102 } | 110 } |
| 103 | 111 |
| 104 if (!profile_to_edit_) | 112 if (!profile_to_edit_) |
| 105 return base::string16(); | 113 return base::string16(); |
| 106 | 114 |
| 107 return profile_to_edit_->GetInfo(autofill::AutofillType(type), | 115 return profile_to_edit_->GetInfo(autofill::AutofillType(type), |
| 108 state()->GetApplicationLocale()); | 116 state()->GetApplicationLocale()); |
| 109 } | 117 } |
| 110 | 118 |
| 111 bool ShippingAddressEditorViewController::ValidateModelAndSave() { | 119 bool ShippingAddressEditorViewController::ValidateModelAndSave() { |
| 112 // To validate the profile first, we use a temporary object. | 120 // To validate the profile first, we use a temporary object. |
| 113 autofill::AutofillProfile profile; | 121 autofill::AutofillProfile profile; |
| 114 if (!SaveFieldsToProfile(&profile, /*ignore_errors=*/false)) | 122 if (!SaveFieldsToProfile(&profile, /*ignore_errors=*/false)) |
| 115 return false; | 123 return false; |
| 116 | |
| 117 if (!profile_to_edit_) { | 124 if (!profile_to_edit_) { |
| 118 // Add the profile (will not add a duplicate). | 125 // Add the profile (will not add a duplicate). |
| 119 profile.set_origin(autofill::kSettingsOrigin); | 126 profile.set_origin(autofill::kSettingsOrigin); |
| 120 state()->GetPersonalDataManager()->AddProfile(profile); | 127 state()->GetPersonalDataManager()->AddProfile(profile); |
| 121 std::move(on_added_).Run(profile); | 128 std::move(on_added_).Run(profile); |
| 122 on_edited_.Reset(); | 129 on_edited_.Reset(); |
| 123 } else { | 130 } else { |
| 124 // Copy the temporary object's data to the object to be edited. Prefer this | 131 // 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 | 132 // method to copying |profile| into |profile_to_edit_|, because the latter |
| 126 // object needs to retain other properties (use count, use date, guid, | 133 // 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); | 223 temporary_profile_.reset(nullptr); |
| 217 } | 224 } |
| 218 | 225 |
| 219 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { | 226 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { |
| 220 // TODO(crbug.com/712074): Editor title should reflect the missing information | 227 // TODO(crbug.com/712074): Editor title should reflect the missing information |
| 221 // in the case that one or more fields are missing. | 228 // in the case that one or more fields are missing. |
| 222 return profile_to_edit_ ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_ADDRESS) | 229 return profile_to_edit_ ? l10n_util::GetStringUTF16(IDS_PAYMENTS_EDIT_ADDRESS) |
| 223 : l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_ADDRESS); | 230 : l10n_util::GetStringUTF16(IDS_PAYMENTS_ADD_ADDRESS); |
| 224 } | 231 } |
| 225 | 232 |
| 233 std::unique_ptr<views::Button> |
| 234 ShippingAddressEditorViewController::CreatePrimaryButton() { |
| 235 std::unique_ptr<views::Button> button( |
| 236 EditorViewController::CreatePrimaryButton()); |
| 237 button->set_id(static_cast<int>(DialogViewID::SAVE_ADDRESS_BUTTON)); |
| 238 return button; |
| 239 } |
| 240 |
| 226 void ShippingAddressEditorViewController::UpdateEditorFields() { | 241 void ShippingAddressEditorViewController::UpdateEditorFields() { |
| 227 editor_fields_.clear(); | 242 editor_fields_.clear(); |
| 228 std::string chosen_country_code; | 243 std::string chosen_country_code; |
| 229 if (chosen_country_index_ < country_codes_.size()) | 244 if (chosen_country_index_ < country_codes_.size()) |
| 230 chosen_country_code = country_codes_[chosen_country_index_]; | 245 chosen_country_code = country_codes_[chosen_country_index_]; |
| 231 | 246 |
| 232 std::unique_ptr<base::ListValue> components(new base::ListValue); | 247 std::unique_ptr<base::ListValue> components(new base::ListValue); |
| 233 std::string unused; | 248 std::string unused; |
| 234 autofill::GetAddressComponents(chosen_country_code, | 249 autofill::GetAddressComponents(chosen_country_code, |
| 235 state()->GetApplicationLocale(), | 250 state()->GetApplicationLocale(), |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 controller_->country_codes_[controller_->chosen_country_index_])) { | 425 controller_->country_codes_[controller_->chosen_country_index_])) { |
| 411 controller_->DisplayErrorMessageForField( | 426 controller_->DisplayErrorMessageForField( |
| 412 field_, l10n_util::GetStringUTF16( | 427 field_, l10n_util::GetStringUTF16( |
| 413 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); | 428 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); |
| 414 return false; | 429 return false; |
| 415 } | 430 } |
| 416 // As long as other field types are non-empty, they are valid. | 431 // As long as other field types are non-empty, they are valid. |
| 417 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); | 432 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); |
| 418 return true; | 433 return true; |
| 419 } | 434 } |
| 420 | |
| 421 bool is_required_valid = !field_.required; | 435 bool is_required_valid = !field_.required; |
| 422 const base::string16 displayed_message = | 436 const base::string16 displayed_message = |
| 423 is_required_valid ? base::ASCIIToUTF16("") | 437 is_required_valid ? base::ASCIIToUTF16("") |
| 424 : l10n_util::GetStringUTF16( | 438 : l10n_util::GetStringUTF16( |
| 425 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 439 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 426 controller_->DisplayErrorMessageForField(field_, displayed_message); | 440 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 427 return is_required_valid; | 441 return is_required_valid; |
| 428 } | 442 } |
| 429 | 443 |
| 430 } // namespace payments | 444 } // namespace payments |
| OLD | NEW |