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

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

Powered by Google App Engine
This is Rietveld 408576698