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 <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return autofill::ADDRESS_HOME_COUNTRY; | 69 return autofill::ADDRESS_HOME_COUNTRY; |
70 NOTREACHED(); | 70 NOTREACHED(); |
71 return autofill::UNKNOWN_TYPE; | 71 return autofill::UNKNOWN_TYPE; |
72 } | 72 } |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 ShippingAddressEditorViewController::ShippingAddressEditorViewController( | 76 ShippingAddressEditorViewController::ShippingAddressEditorViewController( |
77 PaymentRequestSpec* spec, | 77 PaymentRequestSpec* spec, |
78 PaymentRequestState* state, | 78 PaymentRequestState* state, |
79 PaymentRequestDialogView* dialog) | 79 PaymentRequestDialogView* dialog, |
80 : EditorViewController(spec, state, dialog) { | 80 autofill::AutofillProfile* profile) |
| 81 : EditorViewController(spec, state, dialog), profile_to_edit_(profile) { |
81 UpdateEditorFields(); | 82 UpdateEditorFields(); |
82 } | 83 } |
83 | 84 |
84 ShippingAddressEditorViewController::~ShippingAddressEditorViewController() {} | 85 ShippingAddressEditorViewController::~ShippingAddressEditorViewController() {} |
85 | 86 |
86 std::unique_ptr<views::View> | 87 std::unique_ptr<views::View> |
87 ShippingAddressEditorViewController::CreateHeaderView() { | 88 ShippingAddressEditorViewController::CreateHeaderView() { |
88 return base::MakeUnique<views::View>(); | 89 return base::MakeUnique<views::View>(); |
89 } | 90 } |
90 | 91 |
91 std::vector<EditorField> | 92 std::vector<EditorField> |
92 ShippingAddressEditorViewController::GetFieldDefinitions() { | 93 ShippingAddressEditorViewController::GetFieldDefinitions() { |
93 return editor_fields_; | 94 return editor_fields_; |
94 } | 95 } |
95 | 96 |
| 97 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( |
| 98 autofill::ServerFieldType type) { |
| 99 if (!profile_to_edit_) |
| 100 return base::string16(); |
| 101 |
| 102 return profile_to_edit_->GetInfo(autofill::AutofillType(type), |
| 103 state()->GetApplicationLocale()); |
| 104 } |
| 105 |
96 bool ShippingAddressEditorViewController::ValidateModelAndSave() { | 106 bool ShippingAddressEditorViewController::ValidateModelAndSave() { |
| 107 const std::string& locale = state()->GetApplicationLocale(); |
| 108 // To validate the profile first, we use a temporary object. |
97 autofill::AutofillProfile profile; | 109 autofill::AutofillProfile profile; |
98 profile.set_origin(autofill::kSettingsOrigin); | |
99 for (const auto& field : text_fields()) { | 110 for (const auto& field : text_fields()) { |
100 // Force a blur in case the value was left untouched. | 111 // Force a blur in case the value was left untouched. |
101 field.first->OnBlur(); | 112 field.first->OnBlur(); |
102 // ValidatingTextfield* is the key, EditorField is the value. | 113 // ValidatingTextfield* is the key, EditorField is the value. |
103 if (field.first->invalid()) | 114 if (field.first->invalid()) |
104 return false; | 115 return false; |
105 | 116 |
106 profile.SetRawInfo(field.second.type, field.first->text()); | 117 profile.SetInfo(autofill::AutofillType(field.second.type), |
| 118 field.first->text(), locale); |
107 } | 119 } |
108 for (const auto& field : comboboxes()) { | 120 for (const auto& field : comboboxes()) { |
109 // ValidatingCombobox* is the key, EditorField is the value. | 121 // ValidatingCombobox* is the key, EditorField is the value. |
110 ValidatingCombobox* combobox = field.first; | 122 ValidatingCombobox* combobox = field.first; |
111 if (combobox->invalid()) | 123 if (combobox->invalid()) |
112 return false; | 124 return false; |
113 | 125 |
114 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) { | 126 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) { |
115 profile.SetRawInfo( | 127 profile.SetInfo( |
116 field.second.type, | 128 autofill::AutofillType(field.second.type), |
117 base::UTF8ToUTF16(country_codes_[combobox->selected_index()])); | 129 base::UTF8ToUTF16(country_codes_[combobox->selected_index()]), |
| 130 locale); |
118 } else { | 131 } else { |
119 profile.SetRawInfo(field.second.type, | 132 profile.SetInfo(autofill::AutofillType(field.second.type), |
120 combobox->GetTextForRow(combobox->selected_index())); | 133 combobox->GetTextForRow(combobox->selected_index()), |
| 134 locale); |
121 } | 135 } |
122 } | 136 } |
123 | 137 |
124 // Add the profile (will not add a duplicate). | 138 if (!profile_to_edit_) { |
125 state()->GetPersonalDataManager()->AddProfile(profile); | 139 // Add the profile (will not add a duplicate). |
| 140 profile.set_origin(autofill::kSettingsOrigin); |
| 141 state()->GetPersonalDataManager()->AddProfile(profile); |
| 142 } else { |
| 143 // Copy the temporary object's data to the object to be edited. Prefer this |
| 144 // method to copying |profile| into |profile_to_edit_|, because the latter |
| 145 // object needs to retain other properties (use count, use date, guid, |
| 146 // etc.). |
| 147 for (const auto& field : text_fields()) { |
| 148 profile_to_edit_->SetInfo( |
| 149 autofill::AutofillType(field.second.type), |
| 150 profile.GetInfo(autofill::AutofillType(field.second.type), locale), |
| 151 locale); |
| 152 } |
| 153 for (const auto& field : comboboxes()) { |
| 154 profile_to_edit_->SetInfo( |
| 155 autofill::AutofillType(field.second.type), |
| 156 profile.GetInfo(autofill::AutofillType(field.second.type), locale), |
| 157 locale); |
| 158 } |
| 159 profile_to_edit_->set_origin(autofill::kSettingsOrigin); |
| 160 state()->GetPersonalDataManager()->UpdateProfile(*profile_to_edit_); |
| 161 } |
126 | 162 |
127 return true; | 163 return true; |
128 } | 164 } |
129 | 165 |
130 std::unique_ptr<ValidationDelegate> | 166 std::unique_ptr<ValidationDelegate> |
131 ShippingAddressEditorViewController::CreateValidationDelegate( | 167 ShippingAddressEditorViewController::CreateValidationDelegate( |
132 const EditorField& field) { | 168 const EditorField& field) { |
133 return base::MakeUnique< | 169 return base::MakeUnique< |
134 ShippingAddressEditorViewController::ShippingAddressValidationDelegate>( | 170 ShippingAddressEditorViewController::ShippingAddressValidationDelegate>( |
135 this, field); | 171 this, field); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 bool is_required_valid = !field_.required; | 364 bool is_required_valid = !field_.required; |
329 const base::string16 displayed_message = | 365 const base::string16 displayed_message = |
330 is_required_valid ? base::ASCIIToUTF16("") | 366 is_required_valid ? base::ASCIIToUTF16("") |
331 : l10n_util::GetStringUTF16( | 367 : l10n_util::GetStringUTF16( |
332 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 368 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
333 controller_->DisplayErrorMessageForField(field_, displayed_message); | 369 controller_->DisplayErrorMessageForField(field_, displayed_message); |
334 return is_required_valid; | 370 return is_required_valid; |
335 } | 371 } |
336 | 372 |
337 } // namespace payments | 373 } // namespace payments |
OLD | NEW |