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