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/base/models/simple_combobox_model.h" | |
20 #include "ui/views/controls/label.h" | |
21 #include "ui/views/controls/textfield/textfield.h" | |
22 | |
23 namespace payments { | |
24 | |
25 ContactInfoEditorViewController::ContactInfoEditorViewController( | |
26 PaymentRequestSpec* spec, | |
27 PaymentRequestState* state, | |
28 PaymentRequestDialogView* dialog, | |
29 autofill::AutofillProfile* profile) | |
30 : EditorViewController(spec, state, dialog), profile_to_edit_(profile) {} | |
31 | |
32 ContactInfoEditorViewController::~ContactInfoEditorViewController() {} | |
33 | |
34 std::unique_ptr<views::View> | |
35 ContactInfoEditorViewController::CreateHeaderView() { | |
36 return base::MakeUnique<views::View>(); | |
37 } | |
38 | |
39 std::vector<EditorField> | |
40 ContactInfoEditorViewController::GetFieldDefinitions() { | |
41 std::vector<EditorField> fields; | |
42 if (spec()->request_payer_name()) { | |
anthonyvd
2017/04/18 15:55:43
Quick question: shouldn't we still display those f
tmartino
2017/04/18 19:39:18
I'm not sure. From a data perspective it's nice, b
| |
43 fields.push_back(EditorField( | |
44 autofill::NAME_FULL, | |
45 l10n_util::GetStringUTF16(IDS_PAYMENTS_NAME_FIELD_IN_CONTACT_DETAILS), | |
46 EditorField::LengthHint::HINT_LONG, /*required=*/true)); | |
47 } | |
48 if (spec()->request_payer_phone()) { | |
49 fields.push_back(EditorField( | |
50 autofill::PHONE_HOME_WHOLE_NUMBER, | |
51 l10n_util::GetStringUTF16(IDS_PAYMENTS_PHONE_FIELD_IN_CONTACT_DETAILS), | |
52 EditorField::LengthHint::HINT_LONG, /*required=*/true)); | |
53 } | |
54 if (spec()->request_payer_email()) { | |
55 fields.push_back(EditorField( | |
56 autofill::EMAIL_ADDRESS, | |
57 l10n_util::GetStringUTF16(IDS_PAYMENTS_EMAIL_FIELD_IN_CONTACT_DETAILS), | |
58 EditorField::LengthHint::HINT_LONG, /*required=*/true)); | |
59 } | |
60 return fields; | |
61 } | |
62 | |
63 base::string16 ContactInfoEditorViewController::GetInitialValueForType( | |
64 autofill::ServerFieldType type) { | |
65 if (!profile_to_edit_) | |
66 return base::string16(); | |
67 | |
68 return profile_to_edit_->GetInfo(autofill::AutofillType(type), | |
69 state()->GetApplicationLocale()); | |
70 } | |
71 | |
72 bool ContactInfoEditorViewController::ValidateModelAndSave() { | |
73 // TODO(crbug.com/712224): Move this method and its helpers to a base class | |
74 // shared with the Shipping Address editor. | |
75 if (!ValidateModel()) | |
76 return false; | |
77 | |
78 if (profile_to_edit_) { | |
79 PopulateProfile(profile_to_edit_); | |
80 state()->GetPersonalDataManager()->UpdateProfile(*profile_to_edit_); | |
81 } else { | |
82 std::unique_ptr<autofill::AutofillProfile> profile = | |
83 base::MakeUnique<autofill::AutofillProfile>(); | |
84 PopulateProfile(profile.get()); | |
85 state()->GetPersonalDataManager()->AddProfile(*profile); | |
86 // TODO(crbug.com/712224): Add to profile cache in state_. | |
87 } | |
88 return true; | |
89 } | |
90 | |
91 std::unique_ptr<ValidationDelegate> | |
92 ContactInfoEditorViewController::CreateValidationDelegate( | |
93 const EditorField& field) { | |
94 return base::MakeUnique<ContactInfoValidationDelegate>( | |
95 field, state()->GetApplicationLocale(), this); | |
96 } | |
97 | |
98 std::unique_ptr<ui::ComboboxModel> | |
99 ContactInfoEditorViewController::GetComboboxModelForType( | |
100 const autofill::ServerFieldType& type) { | |
101 NOTREACHED(); | |
102 return nullptr; | |
103 } | |
104 | |
105 base::string16 ContactInfoEditorViewController::GetSheetTitle() { | |
106 return profile_to_edit_ ? l10n_util::GetStringUTF16( | |
107 IDS_PAYMENTS_EDIT_CONTACT_DETAILS_LABEL) | |
108 : l10n_util::GetStringUTF16( | |
109 IDS_PAYMENTS_ADD_CONTACT_DETAILS_LABEL); | |
110 } | |
111 | |
112 bool ContactInfoEditorViewController::ValidateModel() { | |
113 for (const auto& field : text_fields()) { | |
114 // Force a blur, as validation only occurs after the first blur. | |
115 field.first->OnBlur(); | |
anthonyvd
2017/04/18 15:55:43
Should we change the base so that this isn't requi
tmartino
2017/04/18 19:39:18
Added a comment to crbug.com/712224 which deals wi
| |
116 if (field.first->invalid()) | |
117 return false; | |
118 } | |
119 return true; | |
120 } | |
121 | |
122 void ContactInfoEditorViewController::PopulateProfile( | |
123 autofill::AutofillProfile* profile) { | |
124 for (const auto& field : text_fields()) { | |
125 profile->SetInfo(autofill::AutofillType(field.second.type), | |
126 field.first->text(), state()->GetApplicationLocale()); | |
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_); | |
sebsg
2017/04/18 15:44:33
Do you think we could use the country of the profi
tmartino
2017/04/18 19:39:18
Resolved offline
| |
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 |