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

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

Issue 2803443003: [Payments] Added region load failure tolerance and tests to PR editor. (Closed)
Patch Set: Sign bot error fixes 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 field.second.type, 116 field.second.type,
117 base::UTF8ToUTF16(country_codes_[combobox->selected_index()])); 117 base::UTF8ToUTF16(country_codes_[combobox->selected_index()]));
118 } else { 118 } else {
119 profile.SetRawInfo(field.second.type, 119 profile.SetRawInfo(field.second.type,
120 combobox->GetTextForRow(combobox->selected_index())); 120 combobox->GetTextForRow(combobox->selected_index()));
121 } 121 }
122 } 122 }
123 123
124 // Add the profile (will not add a duplicate). 124 // Add the profile (will not add a duplicate).
125 state()->GetPersonalDataManager()->AddProfile(profile); 125 state()->GetPersonalDataManager()->AddProfile(profile);
126
127 return true; 126 return true;
128 } 127 }
129 128
130 std::unique_ptr<ValidationDelegate> 129 std::unique_ptr<ValidationDelegate>
131 ShippingAddressEditorViewController::CreateValidationDelegate( 130 ShippingAddressEditorViewController::CreateValidationDelegate(
132 const EditorField& field) { 131 const EditorField& field) {
133 return base::MakeUnique< 132 return base::MakeUnique<
134 ShippingAddressEditorViewController::ShippingAddressValidationDelegate>( 133 ShippingAddressEditorViewController::ShippingAddressValidationDelegate>(
135 this, field); 134 this, field);
136 } 135 }
(...skipping 11 matching lines...) Expand all
148 country_codes_.clear(); 147 country_codes_.clear();
149 for (size_t i = 0; i < model->countries().size(); ++i) { 148 for (size_t i = 0; i < model->countries().size(); ++i) {
150 if (model->countries()[i].get()) 149 if (model->countries()[i].get())
151 country_codes_.push_back(model->countries()[i]->country_code()); 150 country_codes_.push_back(model->countries()[i]->country_code());
152 else 151 else
153 country_codes_.push_back(""); // Separator. 152 country_codes_.push_back(""); // Separator.
154 } 153 }
155 return std::unique_ptr<ui::ComboboxModel>(model.release()); 154 return std::unique_ptr<ui::ComboboxModel>(model.release());
156 } 155 }
157 case autofill::ADDRESS_HOME_STATE: { 156 case autofill::ADDRESS_HOME_STATE: {
158 return std::unique_ptr< 157 std::unique_ptr<autofill::RegionComboboxModel> model = base::MakeUnique<
159 ui::ComboboxModel>(new autofill::RegionComboboxModel( 158 autofill::RegionComboboxModel>(
160 base::WrapUnique(new autofill::ChromeMetadataSource( 159 base::WrapUnique(new autofill::ChromeMetadataSource(
161 I18N_ADDRESS_VALIDATION_DATA_URL, 160 I18N_ADDRESS_VALIDATION_DATA_URL,
162 state()->GetPersonalDataManager()->GetURLRequestContextGetter())), 161 state()->GetPersonalDataManager()->GetURLRequestContextGetter())),
163 autofill::ValidationRulesStorageFactory::CreateStorage(), 162 autofill::ValidationRulesStorageFactory::CreateStorage(),
164 state()->GetApplicationLocale(), 163 state()->GetApplicationLocale(),
165 country_codes_[chosen_country_index_])); 164 country_codes_[chosen_country_index_]);
165 // If the data was already pre-loaded, the observer won't get notified so
166 // we have check for failure here.
Mathieu 2017/04/05 00:25:14 nit: *we have to check
MAD 2017/04/07 18:50:40 Done.
167 if (!model->pending_region_data_load()) {
168 failed_to_load_region_data_ = model->failed_to_load_data();
169 if (failed_to_load_region_data_)
170 OnDataChanged();
171 }
172 return std::unique_ptr<ui::ComboboxModel>(model.release());
166 } 173 }
167 default: 174 default:
168 NOTREACHED(); 175 NOTREACHED();
169 break; 176 break;
170 } 177 }
171 return std::unique_ptr<ui::ComboboxModel>(); 178 return std::unique_ptr<ui::ComboboxModel>();
172 } 179 }
173 180
174 void ShippingAddressEditorViewController::OnPerformAction( 181 void ShippingAddressEditorViewController::OnPerformAction(
175 views::Combobox* sender) { 182 views::Combobox* sender) {
176 EditorViewController::OnPerformAction(sender); 183 EditorViewController::OnPerformAction(sender);
177 if (sender->id() != autofill::ADDRESS_HOME_COUNTRY) 184 if (sender->id() != autofill::ADDRESS_HOME_COUNTRY)
178 return; 185 return;
179 DCHECK_GE(sender->selected_index(), 0); 186 DCHECK_GE(sender->selected_index(), 0);
180 if (chosen_country_index_ != static_cast<size_t>(sender->selected_index())) { 187 if (chosen_country_index_ != static_cast<size_t>(sender->selected_index())) {
181 chosen_country_index_ = sender->selected_index(); 188 chosen_country_index_ = sender->selected_index();
182 OnCountryChanged(sender); 189 failed_to_load_region_data_ = false;
190 OnDataChanged();
183 } 191 }
184 } 192 }
185 193
186 void ShippingAddressEditorViewController::UpdateEditorView() { 194 void ShippingAddressEditorViewController::UpdateEditorView() {
187 EditorViewController::UpdateEditorView(); 195 EditorViewController::UpdateEditorView();
188 if (chosen_country_index_ > 0UL) { 196 if (chosen_country_index_ > 0UL) {
189 views::Combobox* country_combo_box = static_cast<views::Combobox*>( 197 views::Combobox* country_combo_box = static_cast<views::Combobox*>(
190 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY)); 198 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY));
191 DCHECK(country_combo_box); 199 DCHECK(country_combo_box);
192 country_combo_box->SetSelectedIndex(chosen_country_index_); 200 country_combo_box->SetSelectedIndex(chosen_country_index_);
193 } 201 }
194 } 202 }
195 203
196 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { 204 base::string16 ShippingAddressEditorViewController::GetSheetTitle() {
197 return l10n_util::GetStringUTF16( 205 return l10n_util::GetStringUTF16(
198 IDS_PAYMENT_REQUEST_ADDRESS_EDITOR_ADD_TITLE); 206 IDS_PAYMENT_REQUEST_ADDRESS_EDITOR_ADD_TITLE);
199 } 207 }
200 208
201 void ShippingAddressEditorViewController::UpdateEditorFields() { 209 void ShippingAddressEditorViewController::UpdateEditorFields() {
202 editor_fields_.clear(); 210 editor_fields_.clear();
203 std::string chosen_country_code; 211 std::string chosen_country_code;
204 if (chosen_country_index_ < country_codes_.size()) 212 if (chosen_country_index_ < country_codes_.size())
205 chosen_country_code = country_codes_[chosen_country_index_]; 213 chosen_country_code = country_codes_[chosen_country_index_];
206 214
207 std::unique_ptr<base::ListValue> components(new base::ListValue); 215 std::unique_ptr<base::ListValue> components(new base::ListValue);
208 std::string unused; 216 std::string unused;
209 autofill::GetAddressComponents(chosen_country_code, 217 autofill::GetAddressComponents(chosen_country_code,
210 state()->GetApplicationLocale(), 218 state()->GetApplicationLocale(),
211 components.get(), &unused); 219 components.get(), &unused);
212
213 for (size_t line_index = 0; line_index < components->GetSize(); 220 for (size_t line_index = 0; line_index < components->GetSize();
214 ++line_index) { 221 ++line_index) {
215 const base::ListValue* line = nullptr; 222 const base::ListValue* line = nullptr;
216 if (!components->GetList(line_index, &line)) { 223 if (!components->GetList(line_index, &line)) {
217 NOTREACHED(); 224 NOTREACHED();
218 return; 225 return;
219 } 226 }
220 DCHECK_NE(nullptr, line); 227 DCHECK_NE(nullptr, line);
221 for (size_t component_index = 0; component_index < line->GetSize(); 228 for (size_t component_index = 0; component_index < line->GetSize();
222 ++component_index) { 229 ++component_index) {
(...skipping 20 matching lines...) Expand all
243 EditorField::LengthHint length_hint = EditorField::LengthHint::HINT_LONG; 250 EditorField::LengthHint length_hint = EditorField::LengthHint::HINT_LONG;
244 if (field_length == autofill::kShortField) 251 if (field_length == autofill::kShortField)
245 length_hint = EditorField::LengthHint::HINT_SHORT; 252 length_hint = EditorField::LengthHint::HINT_SHORT;
246 else 253 else
247 DCHECK_EQ(autofill::kLongField, field_length); 254 DCHECK_EQ(autofill::kLongField, field_length);
248 autofill::ServerFieldType server_field_type = 255 autofill::ServerFieldType server_field_type =
249 GetFieldTypeFromString(field_type); 256 GetFieldTypeFromString(field_type);
250 EditorField::ControlType control_type = 257 EditorField::ControlType control_type =
251 EditorField::ControlType::TEXTFIELD; 258 EditorField::ControlType::TEXTFIELD;
252 if (server_field_type == autofill::ADDRESS_HOME_COUNTRY || 259 if (server_field_type == autofill::ADDRESS_HOME_COUNTRY ||
253 server_field_type == autofill::ADDRESS_HOME_STATE) { 260 (server_field_type == autofill::ADDRESS_HOME_STATE &&
261 !failed_to_load_region_data_)) {
254 control_type = EditorField::ControlType::COMBOBOX; 262 control_type = EditorField::ControlType::COMBOBOX;
255 } 263 }
256 editor_fields_.emplace_back( 264 editor_fields_.emplace_back(
257 server_field_type, base::UTF8ToUTF16(field_name), length_hint, 265 server_field_type, base::UTF8ToUTF16(field_name), length_hint,
258 /*required=*/server_field_type != autofill::COMPANY_NAME, 266 /*required=*/server_field_type != autofill::COMPANY_NAME,
259 control_type); 267 control_type);
260 // Insert the Country combobox right after NAME_FULL. 268 // Insert the Country combobox right after NAME_FULL.
261 if (server_field_type == autofill::NAME_FULL) { 269 if (server_field_type == autofill::NAME_FULL) {
262 editor_fields_.emplace_back( 270 editor_fields_.emplace_back(
263 autofill::ADDRESS_HOME_COUNTRY, 271 autofill::ADDRESS_HOME_COUNTRY,
264 l10n_util::GetStringUTF16( 272 l10n_util::GetStringUTF16(
265 IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL), 273 IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL),
266 EditorField::LengthHint::HINT_LONG, /*required=*/true, 274 EditorField::LengthHint::HINT_LONG, /*required=*/true,
267 EditorField::ControlType::COMBOBOX); 275 EditorField::ControlType::COMBOBOX);
268 } 276 }
269 } 277 }
270 } 278 }
271 // Always add phone number at the end. 279 // Always add phone number at the end.
272 editor_fields_.emplace_back( 280 editor_fields_.emplace_back(
273 autofill::PHONE_HOME_NUMBER, 281 autofill::PHONE_HOME_NUMBER,
274 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE), 282 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE),
275 EditorField::LengthHint::HINT_LONG, /*required=*/false, 283 EditorField::LengthHint::HINT_LONG, /*required=*/false,
276 EditorField::ControlType::TEXTFIELD); 284 EditorField::ControlType::TEXTFIELD);
277 } 285 }
278 286
279 void ShippingAddressEditorViewController::OnCountryChanged( 287 void ShippingAddressEditorViewController::OnDataChanged() {
280 views::Combobox* combobox) {
281 // TODO(crbug.com/703764): save the current state so we can map it to the new 288 // TODO(crbug.com/703764): save the current state so we can map it to the new
282 // country fields as best we can. 289 // country fields as best we can.
283 UpdateEditorFields(); 290 UpdateEditorFields();
284 291
285 // The editor can't be updated while in the middle of a combobox event. 292 // The editor can't be updated while in the middle of a combobox event.
286 base::ThreadTaskRunnerHandle::Get()->PostTask( 293 base::ThreadTaskRunnerHandle::Get()->PostTask(
287 FROM_HERE, 294 FROM_HERE,
288 base::Bind(&ShippingAddressEditorViewController::UpdateEditorView, 295 base::Bind(&ShippingAddressEditorViewController::UpdateEditorView,
289 base::Unretained(this))); 296 base::Unretained(this)));
290 } 297 }
291 298
299 void ShippingAddressEditorViewController::OnComboboxModelChanged(
300 views::Combobox* combobox) {
301 if (combobox->id() != autofill::ADDRESS_HOME_STATE)
302 return;
303 autofill::RegionComboboxModel* model =
304 static_cast<autofill::RegionComboboxModel*>(combobox->model());
305 if (model->pending_region_data_load())
306 return;
307 if (model->failed_to_load_data()) {
308 failed_to_load_region_data_ = true;
309 OnDataChanged();
310 }
311 }
312
292 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 313 ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
293 ShippingAddressValidationDelegate( 314 ShippingAddressValidationDelegate(
294 ShippingAddressEditorViewController* controller, 315 ShippingAddressEditorViewController* controller,
295 const EditorField& field) 316 const EditorField& field)
296 : field_(field), controller_(controller) {} 317 : field_(field), controller_(controller) {}
297 318
298 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 319 ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
299 ~ShippingAddressValidationDelegate() {} 320 ~ShippingAddressValidationDelegate() {}
300 321
301 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 322 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
302 ValidateTextfield(views::Textfield* textfield) { 323 ValidateTextfield(views::Textfield* textfield) {
303 return ValidateValue(textfield->text()); 324 return ValidateValue(textfield->text());
304 } 325 }
305 326
306 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 327 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
307 ValidateCombobox(views::Combobox* combobox) { 328 ValidateCombobox(views::Combobox* combobox) {
308 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); 329 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()));
309 } 330 }
310 331
332 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
333 ComboboxModelChanged(views::Combobox* combobox) {
334 controller_->OnComboboxModelChanged(combobox);
335 }
336
311 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 337 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
312 ValidateValue(const base::string16& value) { 338 ValidateValue(const base::string16& value) {
313 if (!value.empty()) { 339 if (!value.empty()) {
314 if (field_.type == autofill::PHONE_HOME_NUMBER && 340 if (field_.type == autofill::PHONE_HOME_NUMBER &&
315 !autofill::IsValidPhoneNumber( 341 !autofill::IsValidPhoneNumber(
316 value, 342 value,
317 controller_->country_codes_[controller_->chosen_country_index_])) { 343 controller_->country_codes_[controller_->chosen_country_index_])) {
318 controller_->DisplayErrorMessageForField( 344 controller_->DisplayErrorMessageForField(
319 field_, l10n_util::GetStringUTF16( 345 field_, l10n_util::GetStringUTF16(
320 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); 346 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE));
321 return false; 347 return false;
322 } 348 }
323 // As long as other field types are non-empty, they are valid. 349 // As long as other field types are non-empty, they are valid.
324 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); 350 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16(""));
325 return true; 351 return true;
326 } 352 }
327 353
328 bool is_required_valid = !field_.required; 354 bool is_required_valid = !field_.required;
329 const base::string16 displayed_message = 355 const base::string16 displayed_message =
330 is_required_valid ? base::ASCIIToUTF16("") 356 is_required_valid ? base::ASCIIToUTF16("")
331 : l10n_util::GetStringUTF16( 357 : l10n_util::GetStringUTF16(
332 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); 358 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
333 controller_->DisplayErrorMessageForField(field_, displayed_message); 359 controller_->DisplayErrorMessageForField(field_, displayed_message);
334 return is_required_valid; 360 return is_required_valid;
335 } 361 }
336 362
337 } // namespace payments 363 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698