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

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: Fixed bot 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 autofill::AutofillProfile* profile) 80 autofill::AutofillProfile* profile)
81 : EditorViewController(spec, state, dialog), profile_to_edit_(profile) { 81 : EditorViewController(spec, state, dialog),
82 profile_to_edit_(profile),
83 chosen_country_index_(0),
84 failed_to_load_region_data_(false) {
82 UpdateEditorFields(); 85 UpdateEditorFields();
83 } 86 }
84 87
85 ShippingAddressEditorViewController::~ShippingAddressEditorViewController() {} 88 ShippingAddressEditorViewController::~ShippingAddressEditorViewController() {}
86 89
87 std::unique_ptr<views::View> 90 std::unique_ptr<views::View>
88 ShippingAddressEditorViewController::CreateHeaderView() { 91 ShippingAddressEditorViewController::CreateHeaderView() {
89 return base::MakeUnique<views::View>(); 92 return base::MakeUnique<views::View>();
90 } 93 }
91 94
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 model->SetCountries(*state()->GetPersonalDataManager(), 184 model->SetCountries(*state()->GetPersonalDataManager(),
182 base::Callback<bool(const std::string&)>(), 185 base::Callback<bool(const std::string&)>(),
183 state()->GetApplicationLocale()); 186 state()->GetApplicationLocale());
184 country_codes_.clear(); 187 country_codes_.clear();
185 for (size_t i = 0; i < model->countries().size(); ++i) { 188 for (size_t i = 0; i < model->countries().size(); ++i) {
186 if (model->countries()[i].get()) 189 if (model->countries()[i].get())
187 country_codes_.push_back(model->countries()[i]->country_code()); 190 country_codes_.push_back(model->countries()[i]->country_code());
188 else 191 else
189 country_codes_.push_back(""); // Separator. 192 country_codes_.push_back(""); // Separator.
190 } 193 }
191 return std::unique_ptr<ui::ComboboxModel>(model.release()); 194 return std::move(model);
192 } 195 }
193 case autofill::ADDRESS_HOME_STATE: { 196 case autofill::ADDRESS_HOME_STATE: {
194 return std::unique_ptr< 197 std::unique_ptr<autofill::RegionComboboxModel> model = base::MakeUnique<
195 ui::ComboboxModel>(new autofill::RegionComboboxModel( 198 autofill::RegionComboboxModel>(
196 base::WrapUnique(new autofill::ChromeMetadataSource( 199 base::WrapUnique(new autofill::ChromeMetadataSource(
197 I18N_ADDRESS_VALIDATION_DATA_URL, 200 I18N_ADDRESS_VALIDATION_DATA_URL,
198 state()->GetPersonalDataManager()->GetURLRequestContextGetter())), 201 state()->GetPersonalDataManager()->GetURLRequestContextGetter())),
199 autofill::ValidationRulesStorageFactory::CreateStorage(), 202 autofill::ValidationRulesStorageFactory::CreateStorage(),
200 state()->GetApplicationLocale(), 203 state()->GetApplicationLocale(),
201 country_codes_[chosen_country_index_])); 204 country_codes_[chosen_country_index_]);
205 // If the data was already pre-loaded, the observer won't get notified so
206 // we have to check for failure here.
207 if (!model->pending_region_data_load()) {
208 failed_to_load_region_data_ = model->failed_to_load_data();
209 if (failed_to_load_region_data_)
210 OnDataChanged();
211 }
212 return std::move(model);
202 } 213 }
203 default: 214 default:
204 NOTREACHED(); 215 NOTREACHED();
205 break; 216 break;
206 } 217 }
207 return std::unique_ptr<ui::ComboboxModel>(); 218 return std::unique_ptr<ui::ComboboxModel>();
208 } 219 }
209 220
210 void ShippingAddressEditorViewController::OnPerformAction( 221 void ShippingAddressEditorViewController::OnPerformAction(
211 views::Combobox* sender) { 222 views::Combobox* sender) {
212 EditorViewController::OnPerformAction(sender); 223 EditorViewController::OnPerformAction(sender);
213 if (sender->id() != autofill::ADDRESS_HOME_COUNTRY) 224 if (sender->id() != autofill::ADDRESS_HOME_COUNTRY)
214 return; 225 return;
215 DCHECK_GE(sender->selected_index(), 0); 226 DCHECK_GE(sender->selected_index(), 0);
216 if (chosen_country_index_ != static_cast<size_t>(sender->selected_index())) { 227 if (chosen_country_index_ != static_cast<size_t>(sender->selected_index())) {
217 chosen_country_index_ = sender->selected_index(); 228 chosen_country_index_ = sender->selected_index();
218 OnCountryChanged(sender); 229 failed_to_load_region_data_ = false;
230 OnDataChanged();
219 } 231 }
220 } 232 }
221 233
222 void ShippingAddressEditorViewController::UpdateEditorView() { 234 void ShippingAddressEditorViewController::UpdateEditorView() {
223 EditorViewController::UpdateEditorView(); 235 EditorViewController::UpdateEditorView();
224 if (chosen_country_index_ > 0UL) { 236 if (chosen_country_index_ > 0UL) {
225 views::Combobox* country_combo_box = static_cast<views::Combobox*>( 237 views::Combobox* country_combo_box = static_cast<views::Combobox*>(
226 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY)); 238 dialog()->GetViewByID(autofill::ADDRESS_HOME_COUNTRY));
227 DCHECK(country_combo_box); 239 DCHECK(country_combo_box);
228 country_combo_box->SetSelectedIndex(chosen_country_index_); 240 country_combo_box->SetSelectedIndex(chosen_country_index_);
229 } 241 }
230 } 242 }
231 243
232 base::string16 ShippingAddressEditorViewController::GetSheetTitle() { 244 base::string16 ShippingAddressEditorViewController::GetSheetTitle() {
233 return l10n_util::GetStringUTF16( 245 return l10n_util::GetStringUTF16(
234 IDS_PAYMENT_REQUEST_ADDRESS_EDITOR_ADD_TITLE); 246 IDS_PAYMENT_REQUEST_ADDRESS_EDITOR_ADD_TITLE);
235 } 247 }
236 248
237 void ShippingAddressEditorViewController::UpdateEditorFields() { 249 void ShippingAddressEditorViewController::UpdateEditorFields() {
238 editor_fields_.clear(); 250 editor_fields_.clear();
239 std::string chosen_country_code; 251 std::string chosen_country_code;
240 if (chosen_country_index_ < country_codes_.size()) 252 if (chosen_country_index_ < country_codes_.size())
241 chosen_country_code = country_codes_[chosen_country_index_]; 253 chosen_country_code = country_codes_[chosen_country_index_];
242 254
243 std::unique_ptr<base::ListValue> components(new base::ListValue); 255 std::unique_ptr<base::ListValue> components(new base::ListValue);
244 std::string unused; 256 std::string unused;
245 autofill::GetAddressComponents(chosen_country_code, 257 autofill::GetAddressComponents(chosen_country_code,
246 state()->GetApplicationLocale(), 258 state()->GetApplicationLocale(),
247 components.get(), &unused); 259 components.get(), &unused);
248
249 for (size_t line_index = 0; line_index < components->GetSize(); 260 for (size_t line_index = 0; line_index < components->GetSize();
250 ++line_index) { 261 ++line_index) {
251 const base::ListValue* line = nullptr; 262 const base::ListValue* line = nullptr;
252 if (!components->GetList(line_index, &line)) { 263 if (!components->GetList(line_index, &line)) {
253 NOTREACHED(); 264 NOTREACHED();
254 return; 265 return;
255 } 266 }
256 DCHECK_NE(nullptr, line); 267 DCHECK_NE(nullptr, line);
257 for (size_t component_index = 0; component_index < line->GetSize(); 268 for (size_t component_index = 0; component_index < line->GetSize();
258 ++component_index) { 269 ++component_index) {
(...skipping 20 matching lines...) Expand all
279 EditorField::LengthHint length_hint = EditorField::LengthHint::HINT_LONG; 290 EditorField::LengthHint length_hint = EditorField::LengthHint::HINT_LONG;
280 if (field_length == autofill::kShortField) 291 if (field_length == autofill::kShortField)
281 length_hint = EditorField::LengthHint::HINT_SHORT; 292 length_hint = EditorField::LengthHint::HINT_SHORT;
282 else 293 else
283 DCHECK_EQ(autofill::kLongField, field_length); 294 DCHECK_EQ(autofill::kLongField, field_length);
284 autofill::ServerFieldType server_field_type = 295 autofill::ServerFieldType server_field_type =
285 GetFieldTypeFromString(field_type); 296 GetFieldTypeFromString(field_type);
286 EditorField::ControlType control_type = 297 EditorField::ControlType control_type =
287 EditorField::ControlType::TEXTFIELD; 298 EditorField::ControlType::TEXTFIELD;
288 if (server_field_type == autofill::ADDRESS_HOME_COUNTRY || 299 if (server_field_type == autofill::ADDRESS_HOME_COUNTRY ||
289 server_field_type == autofill::ADDRESS_HOME_STATE) { 300 (server_field_type == autofill::ADDRESS_HOME_STATE &&
301 !failed_to_load_region_data_)) {
290 control_type = EditorField::ControlType::COMBOBOX; 302 control_type = EditorField::ControlType::COMBOBOX;
291 } 303 }
292 editor_fields_.emplace_back( 304 editor_fields_.emplace_back(
293 server_field_type, base::UTF8ToUTF16(field_name), length_hint, 305 server_field_type, base::UTF8ToUTF16(field_name), length_hint,
294 /*required=*/server_field_type != autofill::COMPANY_NAME, 306 /*required=*/server_field_type != autofill::COMPANY_NAME,
295 control_type); 307 control_type);
296 // Insert the Country combobox right after NAME_FULL. 308 // Insert the Country combobox right after NAME_FULL.
297 if (server_field_type == autofill::NAME_FULL) { 309 if (server_field_type == autofill::NAME_FULL) {
298 editor_fields_.emplace_back( 310 editor_fields_.emplace_back(
299 autofill::ADDRESS_HOME_COUNTRY, 311 autofill::ADDRESS_HOME_COUNTRY,
300 l10n_util::GetStringUTF16( 312 l10n_util::GetStringUTF16(
301 IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL), 313 IDS_LIBADDRESSINPUT_COUNTRY_OR_REGION_LABEL),
302 EditorField::LengthHint::HINT_LONG, /*required=*/true, 314 EditorField::LengthHint::HINT_LONG, /*required=*/true,
303 EditorField::ControlType::COMBOBOX); 315 EditorField::ControlType::COMBOBOX);
304 } 316 }
305 } 317 }
306 } 318 }
307 // Always add phone number at the end. 319 // Always add phone number at the end.
308 editor_fields_.emplace_back( 320 editor_fields_.emplace_back(
309 autofill::PHONE_HOME_NUMBER, 321 autofill::PHONE_HOME_NUMBER,
310 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE), 322 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_PHONE),
311 EditorField::LengthHint::HINT_LONG, /*required=*/false, 323 EditorField::LengthHint::HINT_LONG, /*required=*/false,
312 EditorField::ControlType::TEXTFIELD); 324 EditorField::ControlType::TEXTFIELD);
313 } 325 }
314 326
315 void ShippingAddressEditorViewController::OnCountryChanged( 327 void ShippingAddressEditorViewController::OnDataChanged() {
316 views::Combobox* combobox) {
317 // TODO(crbug.com/703764): save the current state so we can map it to the new 328 // TODO(crbug.com/703764): save the current state so we can map it to the new
318 // country fields as best we can. 329 // country fields as best we can.
319 UpdateEditorFields(); 330 UpdateEditorFields();
320 331
321 // The editor can't be updated while in the middle of a combobox event. 332 // The editor can't be updated while in the middle of a combobox event.
322 base::ThreadTaskRunnerHandle::Get()->PostTask( 333 base::ThreadTaskRunnerHandle::Get()->PostTask(
323 FROM_HERE, 334 FROM_HERE,
324 base::Bind(&ShippingAddressEditorViewController::UpdateEditorView, 335 base::Bind(&ShippingAddressEditorViewController::UpdateEditorView,
325 base::Unretained(this))); 336 base::Unretained(this)));
326 } 337 }
327 338
339 void ShippingAddressEditorViewController::OnComboboxModelChanged(
340 views::Combobox* combobox) {
341 if (combobox->id() != autofill::ADDRESS_HOME_STATE)
342 return;
343 autofill::RegionComboboxModel* model =
344 static_cast<autofill::RegionComboboxModel*>(combobox->model());
345 if (model->pending_region_data_load())
346 return;
347 if (model->failed_to_load_data()) {
348 failed_to_load_region_data_ = true;
349 OnDataChanged();
350 }
351 }
352
328 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 353 ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
329 ShippingAddressValidationDelegate( 354 ShippingAddressValidationDelegate(
330 ShippingAddressEditorViewController* controller, 355 ShippingAddressEditorViewController* controller,
331 const EditorField& field) 356 const EditorField& field)
332 : field_(field), controller_(controller) {} 357 : field_(field), controller_(controller) {}
333 358
334 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 359 ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
335 ~ShippingAddressValidationDelegate() {} 360 ~ShippingAddressValidationDelegate() {}
336 361
337 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 362 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
338 ValidateTextfield(views::Textfield* textfield) { 363 ValidateTextfield(views::Textfield* textfield) {
339 return ValidateValue(textfield->text()); 364 return ValidateValue(textfield->text());
340 } 365 }
341 366
342 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 367 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
343 ValidateCombobox(views::Combobox* combobox) { 368 ValidateCombobox(views::Combobox* combobox) {
344 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); 369 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()));
345 } 370 }
346 371
372 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
373 ComboboxModelChanged(views::Combobox* combobox) {
374 controller_->OnComboboxModelChanged(combobox);
375 }
376
347 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: 377 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate::
348 ValidateValue(const base::string16& value) { 378 ValidateValue(const base::string16& value) {
349 if (!value.empty()) { 379 if (!value.empty()) {
350 if (field_.type == autofill::PHONE_HOME_NUMBER && 380 if (field_.type == autofill::PHONE_HOME_NUMBER &&
351 !autofill::IsValidPhoneNumber( 381 !autofill::IsValidPhoneNumber(
352 value, 382 value,
353 controller_->country_codes_[controller_->chosen_country_index_])) { 383 controller_->country_codes_[controller_->chosen_country_index_])) {
354 controller_->DisplayErrorMessageForField( 384 controller_->DisplayErrorMessageForField(
355 field_, l10n_util::GetStringUTF16( 385 field_, l10n_util::GetStringUTF16(
356 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); 386 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE));
357 return false; 387 return false;
358 } 388 }
359 // As long as other field types are non-empty, they are valid. 389 // As long as other field types are non-empty, they are valid.
360 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); 390 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16(""));
361 return true; 391 return true;
362 } 392 }
363 393
364 bool is_required_valid = !field_.required; 394 bool is_required_valid = !field_.required;
365 const base::string16 displayed_message = 395 const base::string16 displayed_message =
366 is_required_valid ? base::ASCIIToUTF16("") 396 is_required_valid ? base::ASCIIToUTF16("")
367 : l10n_util::GetStringUTF16( 397 : l10n_util::GetStringUTF16(
368 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); 398 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
369 controller_->DisplayErrorMessageForField(field_, displayed_message); 399 controller_->DisplayErrorMessageForField(field_, displayed_message);
370 return is_required_valid; 400 return is_required_valid;
371 } 401 }
372 402
373 } // namespace payments 403 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698