OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/views/payments/contact_info_editor_view_controller.h " | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "chrome/browser/ui/views/payments/validating_textfield.h" | |
9 #include "components/autofill/core/browser/autofill_country.h" | |
10 #include "components/autofill/core/browser/autofill_profile.h" | |
11 #include "components/autofill/core/browser/autofill_type.h" | |
12 #include "components/autofill/core/browser/personal_data_manager.h" | |
13 #include "components/autofill/core/browser/validation.h" | |
14 #include "components/autofill/core/common/autofill_constants.h" | |
15 #include "components/payments/content/payment_request_spec.h" | |
16 #include "components/payments/content/payment_request_state.h" | |
17 #include "components/strings/grit/components_strings.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 #include "ui/views/controls/label.h" | |
20 #include "ui/views/controls/textfield/textfield.h" | |
21 | |
22 namespace payments { | |
23 | |
24 ContactInfoEditorViewController::ContactInfoEditorViewController( | |
25 PaymentRequestSpec* spec, | |
26 PaymentRequestState* state, | |
27 PaymentRequestDialogView* dialog, | |
28 autofill::AutofillProfile* profile) | |
29 : EditorViewController(spec, state, dialog), profile_to_edit_(profile) {} | |
30 | |
31 ContactInfoEditorViewController::~ContactInfoEditorViewController() {} | |
32 | |
33 std::unique_ptr<views::View> | |
34 ContactInfoEditorViewController::CreateHeaderView() { | |
35 return base::MakeUnique<views::View>(); | |
36 } | |
37 | |
38 std::vector<EditorField> | |
39 ContactInfoEditorViewController::GetFieldDefinitions() { | |
40 std::vector<EditorField> fields; | |
41 if (spec()->request_payer_name()) { | |
42 fields.push_back(EditorField( | |
43 autofill::NAME_FULL, | |
44 l10n_util::GetStringUTF16(IDS_PAYMENTS_NAME_FIELD_IN_CONTACT_DETAILS), | |
45 EditorField::LengthHint::HINT_LONG, /* required= */ true)); | |
Mathieu
2017/04/17 02:50:09
very nit: I started preferring /*required=*/true s
tmartino
2017/04/18 19:39:17
Done
| |
46 } | |
47 if (spec()->request_payer_phone()) { | |
48 fields.push_back(EditorField( | |
49 autofill::PHONE_HOME_WHOLE_NUMBER, | |
50 l10n_util::GetStringUTF16(IDS_PAYMENTS_PHONE_FIELD_IN_CONTACT_DETAILS), | |
51 EditorField::LengthHint::HINT_LONG, /* required= */ true)); | |
52 } | |
53 if (spec()->request_payer_email()) { | |
54 fields.push_back(EditorField( | |
55 autofill::EMAIL_ADDRESS, | |
56 l10n_util::GetStringUTF16(IDS_PAYMENTS_EMAIL_FIELD_IN_CONTACT_DETAILS), | |
57 EditorField::LengthHint::HINT_LONG, /* required= */ true)); | |
58 } | |
59 return fields; | |
60 } | |
61 | |
62 base::string16 ContactInfoEditorViewController::GetInitialValueForType( | |
63 autofill::ServerFieldType type) { | |
64 if (!profile_to_edit_) | |
65 return base::string16(); | |
66 | |
67 return profile_to_edit_->GetInfo(autofill::AutofillType(type), | |
68 state()->GetApplicationLocale()); | |
69 } | |
70 | |
71 bool ContactInfoEditorViewController::ValidateModelAndSave() { | |
72 // TODO(tmartino): Move this method and its helpers to a base class shared | |
Mathieu
2017/04/17 02:50:09
Since the code is getting pretty mature, we should
tmartino
2017/04/18 19:39:17
crbug.com/712224
| |
73 // with the Shipping Address editor. | |
74 if (!ValidateModel()) | |
75 return false; | |
76 | |
77 if (profile_to_edit_) { | |
78 PopulateProfile(profile_to_edit_); | |
79 state()->GetPersonalDataManager()->UpdateProfile(*profile_to_edit_); | |
80 } else { | |
81 std::unique_ptr<autofill::AutofillProfile> profile = | |
82 base::MakeUnique<autofill::AutofillProfile>(); | |
83 PopulateProfile(profile.get()); | |
84 state()->GetPersonalDataManager()->AddProfile(*profile); | |
85 // TODO(tmartino): Add to profile cache in state_. | |
86 } | |
87 return true; | |
88 } | |
89 | |
90 std::unique_ptr<ValidationDelegate> | |
91 ContactInfoEditorViewController::CreateValidationDelegate( | |
92 const EditorField& field) { | |
93 return base::MakeUnique<ContactInfoValidationDelegate>( | |
94 field, state()->GetApplicationLocale(), this); | |
95 } | |
96 | |
97 std::unique_ptr<ui::ComboboxModel> | |
98 ContactInfoEditorViewController::GetComboboxModelForType( | |
99 const autofill::ServerFieldType& type) { | |
100 NOTREACHED(); | |
101 return std::unique_ptr<ui::ComboboxModel>(nullptr); | |
Mathieu
2017/04/17 02:50:09
nit: I wonder if "return nullptr;" also works here
tmartino
2017/04/18 19:39:17
Yup! Done.
| |
102 } | |
103 | |
104 base::string16 ContactInfoEditorViewController::GetSheetTitle() { | |
105 return profile_to_edit_ ? l10n_util::GetStringUTF16( | |
106 IDS_PAYMENTS_EDIT_CONTACT_DETAILS_LABEL) | |
107 : l10n_util::GetStringUTF16( | |
108 IDS_PAYMENTS_ADD_CONTACT_DETAILS_LABEL); | |
Mathieu
2017/04/17 02:50:09
Cool, I filed crbug.com/712074 for so that the oth
| |
109 } | |
110 | |
111 bool ContactInfoEditorViewController::ValidateModel() { | |
112 for (const auto& field : text_fields()) { | |
113 // Force a blur, as validation only occurs after the first blur. | |
114 field.first->OnBlur(); | |
Mathieu
2017/04/17 02:50:09
interesting, I thought when this was called it was
tmartino
2017/04/18 19:39:17
I got this behavior from the Shipping Address Edit
| |
115 if (field.first->invalid()) | |
116 return false; | |
117 } | |
118 return true; | |
119 } | |
120 | |
121 void ContactInfoEditorViewController::PopulateProfile( | |
122 autofill::AutofillProfile* profile) { | |
123 const std::string& locale = state()->GetApplicationLocale(); | |
124 for (const auto& field : text_fields()) { | |
125 profile->SetInfo(autofill::AutofillType(field.second.type), | |
126 field.first->text(), locale); | |
Mathieu
2017/04/17 02:50:09
nit: can inline state()->GetApplicationLocale() wi
tmartino
2017/04/18 19:39:17
Done
| |
127 } | |
128 profile->set_origin(autofill::kSettingsOrigin); | |
129 } | |
130 | |
131 ContactInfoEditorViewController::ContactInfoValidationDelegate:: | |
132 ContactInfoValidationDelegate(const EditorField& field, | |
133 const std::string& locale, | |
134 EditorViewController* controller) | |
135 : field_(field), controller_(controller), locale_(locale) {} | |
136 | |
137 ContactInfoEditorViewController::ContactInfoValidationDelegate:: | |
138 ~ContactInfoValidationDelegate() {} | |
139 | |
140 bool ContactInfoEditorViewController::ContactInfoValidationDelegate:: | |
141 ValidateTextfield(views::Textfield* textfield) { | |
142 bool is_valid = true; | |
143 base::string16 error_message; | |
144 | |
145 if (textfield->text().empty()) { | |
146 is_valid = false; | |
147 error_message = l10n_util::GetStringUTF16( | |
148 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | |
149 } else { | |
150 switch (field_.type) { | |
151 case autofill::PHONE_HOME_WHOLE_NUMBER: { | |
152 const std::string default_region_code = | |
153 autofill::AutofillCountry::CountryCodeForLocale(locale_); | |
154 if (!autofill::IsValidPhoneNumber(textfield->text(), | |
155 default_region_code)) { | |
156 is_valid = false; | |
157 error_message = l10n_util::GetStringUTF16( | |
158 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE); | |
159 } | |
160 break; | |
161 } | |
162 | |
163 case autofill::EMAIL_ADDRESS: { | |
164 if (!autofill::IsValidEmailAddress(textfield->text())) { | |
165 is_valid = false; | |
166 error_message = l10n_util::GetStringUTF16( | |
167 IDS_PAYMENTS_EMAIL_INVALID_VALIDATION_MESSAGE); | |
168 } | |
169 break; | |
170 } | |
171 | |
172 case autofill::NAME_FULL: { | |
173 // We have already determined that name is nonempty, which is the only | |
174 // requirement. | |
175 break; | |
176 } | |
177 | |
178 default: { | |
179 NOTREACHED(); | |
180 break; | |
181 } | |
182 } | |
183 } | |
184 | |
185 controller_->DisplayErrorMessageForField(field_, error_message); | |
186 return is_valid; | |
187 } | |
188 | |
189 bool ContactInfoEditorViewController::ContactInfoValidationDelegate:: | |
190 ValidateCombobox(views::Combobox* combobox) { | |
191 // This UI doesn't contain any comboboxes. | |
192 NOTREACHED(); | |
193 return true; | |
194 } | |
195 | |
196 } // namespace payments | |
OLD | NEW |