Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: chrome/browser/ui/views/payments/shipping_address_editor_view_controller.cc

Issue 2827773003: Restore Payments shipping address info when changing country (Closed)
Patch Set: Fix other platforms compile error Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 return base::MakeUnique<views::View>(); 89 return base::MakeUnique<views::View>();
90 } 90 }
91 91
92 std::vector<EditorField> 92 std::vector<EditorField>
93 ShippingAddressEditorViewController::GetFieldDefinitions() { 93 ShippingAddressEditorViewController::GetFieldDefinitions() {
94 return editor_fields_; 94 return editor_fields_;
95 } 95 }
96 96
97 base::string16 ShippingAddressEditorViewController::GetInitialValueForType( 97 base::string16 ShippingAddressEditorViewController::GetInitialValueForType(
98 autofill::ServerFieldType type) { 98 autofill::ServerFieldType type) {
99 // Temporary profile has precedence over profile to edit since its existence
100 // is based on having unsaved stated to restore.
101 if (temporary_profile_.get()) {
102 return temporary_profile_->GetInfo(autofill::AutofillType(type),
103 state()->GetApplicationLocale());
104 }
105
99 if (!profile_to_edit_) 106 if (!profile_to_edit_)
100 return base::string16(); 107 return base::string16();
101 108
102 return profile_to_edit_->GetInfo(autofill::AutofillType(type), 109 return profile_to_edit_->GetInfo(autofill::AutofillType(type),
103 state()->GetApplicationLocale()); 110 state()->GetApplicationLocale());
104 } 111 }
105 112
106 bool ShippingAddressEditorViewController::ValidateModelAndSave() { 113 bool ShippingAddressEditorViewController::ValidateModelAndSave() {
107 const std::string& locale = state()->GetApplicationLocale();
108 // To validate the profile first, we use a temporary object. 114 // To validate the profile first, we use a temporary object.
109 autofill::AutofillProfile profile; 115 autofill::AutofillProfile profile;
110 for (const auto& field : text_fields()) { 116 if (!SaveFieldsToProfile(&profile, /*ignore_errors=*/false))
111 // Force a blur in case the value was left untouched. 117 return false;
112 field.first->OnBlur();
113 // ValidatingTextfield* is the key, EditorField is the value.
114 if (field.first->invalid())
115 return false;
116
117 profile.SetInfo(autofill::AutofillType(field.second.type),
118 field.first->text(), locale);
119 }
120 for (const auto& field : comboboxes()) {
121 // ValidatingCombobox* is the key, EditorField is the value.
122 ValidatingCombobox* combobox = field.first;
123 if (combobox->invalid())
124 return false;
125
126 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) {
127 profile.SetInfo(
128 autofill::AutofillType(field.second.type),
129 base::UTF8ToUTF16(country_codes_[combobox->selected_index()]),
130 locale);
131 } else {
132 profile.SetInfo(autofill::AutofillType(field.second.type),
133 combobox->GetTextForRow(combobox->selected_index()),
134 locale);
135 }
136 }
137 118
138 if (!profile_to_edit_) { 119 if (!profile_to_edit_) {
139 // Add the profile (will not add a duplicate). 120 // Add the profile (will not add a duplicate).
140 profile.set_origin(autofill::kSettingsOrigin); 121 profile.set_origin(autofill::kSettingsOrigin);
141 state()->GetPersonalDataManager()->AddProfile(profile); 122 state()->GetPersonalDataManager()->AddProfile(profile);
142 } else { 123 } else {
143 // Copy the temporary object's data to the object to be edited. Prefer this 124 // 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 125 // method to copying |profile| into |profile_to_edit_|, because the latter
145 // object needs to retain other properties (use count, use date, guid, 126 // object needs to retain other properties (use count, use date, guid,
146 // etc.). 127 // etc.).
147 for (const auto& field : text_fields()) { 128 bool success = SaveFieldsToProfile(profile_to_edit_,
148 profile_to_edit_->SetInfo( 129 /*ignore_errors=*/false);
149 autofill::AutofillType(field.second.type), 130 DCHECK(success);
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); 131 profile_to_edit_->set_origin(autofill::kSettingsOrigin);
160 state()->GetPersonalDataManager()->UpdateProfile(*profile_to_edit_); 132 state()->GetPersonalDataManager()->UpdateProfile(*profile_to_edit_);
161 } 133 }
162 134
163 return true; 135 return true;
164 } 136 }
165 137
166 std::unique_ptr<ValidationDelegate> 138 std::unique_ptr<ValidationDelegate>
167 ShippingAddressEditorViewController::CreateValidationDelegate( 139 ShippingAddressEditorViewController::CreateValidationDelegate(
168 const EditorField& field) { 140 const EditorField& field) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 199 }
228 200
229 void ShippingAddressEditorViewController::UpdateEditorView() { 201 void ShippingAddressEditorViewController::UpdateEditorView() {
230 EditorViewController::UpdateEditorView(); 202 EditorViewController::UpdateEditorView();
231 if (chosen_country_index_ > 0UL) { 203 if (chosen_country_index_ > 0UL) {
232 views::Combobox* country_combo_box = static_cast<views::Combobox*>( 204 views::Combobox* country_combo_box = static_cast<views::Combobox*>(
233 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY)); 205 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY));
234 DCHECK(country_combo_box); 206 DCHECK(country_combo_box);
235 country_combo_box->SetSelectedIndex(chosen_country_index_); 207 country_combo_box->SetSelectedIndex(chosen_country_index_);
236 } 208 }
209 // Ignore temporary profile once the editor view has been updated.
210 temporary_profile_.reset(nullptr);
237 } 211 }
238 212
239 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { 213 base::string16 ShippingAddressEditorViewController::GetSheetTitle() {
240 return l10n_util::GetStringUTF16( 214 return l10n_util::GetStringUTF16(
241 IDS_PAYMENT_REQUEST_ADDRESS_EDITOR_ADD_TITLE); 215 IDS_PAYMENT_REQUEST_ADDRESS_EDITOR_ADD_TITLE);
242 } 216 }
243 217
244 void ShippingAddressEditorViewController::UpdateEditorFields() { 218 void ShippingAddressEditorViewController::UpdateEditorFields() {
245 editor_fields_.clear(); 219 editor_fields_.clear();
246 std::string chosen_country_code; 220 std::string chosen_country_code;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 287 }
314 // Always add phone number at the end. 288 // Always add phone number at the end.
315 editor_fields_.emplace_back( 289 editor_fields_.emplace_back(
316 autofill::PHONE_HOME_NUMBER, 290 autofill::PHONE_HOME_NUMBER,
317 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE), 291 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE),
318 EditorField::LengthHint::HINT_LONG, /*required=*/false, 292 EditorField::LengthHint::HINT_LONG, /*required=*/false,
319 EditorField::ControlType::TEXTFIELD); 293 EditorField::ControlType::TEXTFIELD);
320 } 294 }
321 295
322 void ShippingAddressEditorViewController::OnDataChanged() { 296 void ShippingAddressEditorViewController::OnDataChanged() {
323 // TODO(crbug.com/703764): save the current state so we can map it to the new 297 temporary_profile_.reset(new autofill::AutofillProfile);
324 // country fields as best we can. 298
299 SaveFieldsToProfile(temporary_profile_.get(), /*ignore_errors*/ true);
325 UpdateEditorFields(); 300 UpdateEditorFields();
326 301
327 // The editor can't be updated while in the middle of a combobox event. 302 // The editor can't be updated while in the middle of a combobox event.
328 base::ThreadTaskRunnerHandle::Get()->PostTask( 303 base::ThreadTaskRunnerHandle::Get()->PostTask(
329 FROM_HERE, 304 FROM_HERE,
330 base::Bind(&ShippingAddressEditorViewController::UpdateEditorView, 305 base::Bind(&ShippingAddressEditorViewController::UpdateEditorView,
331 base::Unretained(this))); 306 base::Unretained(this)));
332 } 307 }
333 308
309 bool ShippingAddressEditorViewController::SaveFieldsToProfile(
310 autofill::AutofillProfile* profile,
311 bool ignore_errors) {
312 const std::string& locale = state()->GetApplicationLocale();
313 bool success = true;
314 for (const auto& field : text_fields()) {
315 // Force a blur in case the value was left untouched.
316 field.first->OnBlur();
317 // ValidatingTextfield* is the key, EditorField is the value.
318 if (field.first->invalid()) {
319 success = false;
320 if (!ignore_errors)
321 return false;
322 }
323 profile->SetInfo(autofill::AutofillType(field.second.type),
324 field.first->text(), locale);
325 }
326 for (const auto& field : comboboxes()) {
327 // ValidatingCombobox* is the key, EditorField is the value.
328 ValidatingCombobox* combobox = field.first;
329 if (combobox->invalid()) {
330 success = false;
331 if (!ignore_errors)
332 return false;
333 }
334
335 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) {
336 profile->SetInfo(
337 autofill::AutofillType(field.second.type),
338 base::UTF8ToUTF16(country_codes_[combobox->selected_index()]),
339 locale);
340 } else {
341 profile->SetInfo(autofill::AutofillType(field.second.type),
342 combobox->GetTextForRow(combobox->selected_index()),
343 locale);
344 }
345 }
346 return success;
347 }
348
334 void ShippingAddressEditorViewController::OnComboboxModelChanged( 349 void ShippingAddressEditorViewController::OnComboboxModelChanged(
335 views::Combobox* combobox) { 350 views::Combobox* combobox) {
336 if (combobox->id() != autofill::ADDRESS_HOME_STATE) 351 if (combobox->id() != autofill::ADDRESS_HOME_STATE)
337 return; 352 return;
338 autofill::RegionComboboxModel* model = 353 autofill::RegionComboboxModel* model =
339 static_cast<autofill::RegionComboboxModel*>(combobox->model()); 354 static_cast<autofill::RegionComboboxModel*>(combobox->model());
340 if (model->pending_region_data_load()) 355 if (model->pending_region_data_load())
341 return; 356 return;
342 if (model->failed_to_load_data()) { 357 if (model->failed_to_load_data()) {
343 failed_to_load_region_data_ = true; 358 failed_to_load_region_data_ = true;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 bool is_required_valid = !field_.required; 404 bool is_required_valid = !field_.required;
390 const base::string16 displayed_message = 405 const base::string16 displayed_message =
391 is_required_valid ? base::ASCIIToUTF16("") 406 is_required_valid ? base::ASCIIToUTF16("")
392 : l10n_util::GetStringUTF16( 407 : l10n_util::GetStringUTF16(
393 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); 408 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
394 controller_->DisplayErrorMessageForField(field_, displayed_message); 409 controller_->DisplayErrorMessageForField(field_, displayed_message);
395 return is_required_valid; 410 return is_required_valid;
396 } 411 }
397 412
398 } // namespace payments 413 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698