OLD | NEW |
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 "components/payments/core/payments_profile_comparator.h" | 5 #include "components/payments/core/payments_profile_comparator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return false; | 111 return false; |
112 } | 112 } |
113 if (!HaveMergeableEmailAddresses(super, sub)) | 113 if (!HaveMergeableEmailAddresses(super, sub)) |
114 return false; | 114 return false; |
115 } | 115 } |
116 return true; | 116 return true; |
117 } | 117 } |
118 | 118 |
119 int PaymentsProfileComparator::GetContactCompletenessScore( | 119 int PaymentsProfileComparator::GetContactCompletenessScore( |
120 const autofill::AutofillProfile* profile) const { | 120 const autofill::AutofillProfile* profile) const { |
| 121 if (!profile) |
| 122 return 0; |
| 123 |
121 // Create a bitmask of the fields that are both present and required. | 124 // Create a bitmask of the fields that are both present and required. |
122 ProfileFields present = | 125 ProfileFields present = |
123 ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForContact(); | 126 ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForContact(); |
124 | 127 |
125 // Count how many are set. | 128 // Count how many are set. |
126 return !!(present & kName) + !!(present & kPhone) + !!(present & kEmail); | 129 return !!(present & kName) + !!(present & kPhone) + !!(present & kEmail); |
127 } | 130 } |
128 | 131 |
129 bool PaymentsProfileComparator::IsContactInfoComplete( | 132 bool PaymentsProfileComparator::IsContactInfoComplete( |
130 const autofill::AutofillProfile* profile) const { | 133 const autofill::AutofillProfile* profile) const { |
131 // Mask the fields that are missing with those that are requried. If any bits | 134 // Mask the fields that are missing with those that are requried. If any bits |
132 // are set (i.e., the result is nonzero), then contact info is incomplete. | 135 // are set (i.e., the result is nonzero), then contact info is incomplete. |
133 return !(GetMissingProfileFields(profile) & | 136 return !(GetMissingProfileFields(profile) & |
134 GetRequiredProfileFieldsForContact()); | 137 GetRequiredProfileFieldsForContact()); |
135 } | 138 } |
136 | 139 |
| 140 bool PaymentsProfileComparator::IsContactMoreComplete( |
| 141 const autofill::AutofillProfile* p1, |
| 142 const autofill::AutofillProfile* p2) const { |
| 143 return GetContactCompletenessScore(p1) > GetContactCompletenessScore(p2); |
| 144 } |
| 145 |
137 base::string16 PaymentsProfileComparator::GetStringForMissingContactFields( | 146 base::string16 PaymentsProfileComparator::GetStringForMissingContactFields( |
138 const autofill::AutofillProfile& profile) const { | 147 const autofill::AutofillProfile& profile) const { |
139 return GetStringForMissingFields(GetMissingProfileFields(&profile) & | 148 return GetStringForMissingFields(GetMissingProfileFields(&profile) & |
140 GetRequiredProfileFieldsForContact()); | 149 GetRequiredProfileFieldsForContact()); |
141 } | 150 } |
142 | 151 |
143 std::vector<autofill::AutofillProfile*> | |
144 PaymentsProfileComparator::FilterProfilesForShipping( | |
145 const std::vector<autofill::AutofillProfile*>& profiles) const { | |
146 // Since we'll be changing the order/contents of the const input vector, | |
147 // we make a copy. | |
148 std::vector<autofill::AutofillProfile*> processed = profiles; | |
149 | |
150 std::stable_sort( | |
151 processed.begin(), processed.end(), | |
152 std::bind(&PaymentsProfileComparator::IsShippingMoreComplete, this, | |
153 std::placeholders::_1, std::placeholders::_2)); | |
154 | |
155 // TODO(crbug.com/722949): Remove profiles with no relevant information, or | |
156 // which are subsets of more-complete profiles. | |
157 | |
158 return processed; | |
159 } | |
160 | |
161 int PaymentsProfileComparator::GetShippingCompletenessScore( | |
162 const autofill::AutofillProfile* profile) const { | |
163 // Create a bitmask of the fields that are both present and required. | |
164 ProfileFields present = | |
165 ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForShipping(); | |
166 | |
167 // Count how many are set. The completeness of the address is weighted so as | |
168 // to dominate the other fields. | |
169 return !!(present & kName) + !!(present & kPhone) + | |
170 (10 * !!(present & kAddress)); | |
171 } | |
172 | |
173 bool PaymentsProfileComparator::IsShippingComplete( | 152 bool PaymentsProfileComparator::IsShippingComplete( |
174 const autofill::AutofillProfile* profile) const { | 153 const autofill::AutofillProfile* profile) const { |
175 // Mask the fields that are missing with those that are requried. If any bits | 154 // Mask the fields that are missing with those that are requried. If any bits |
176 // are set (i.e., the result is nonzero), then shipping is incomplete. | 155 // are set (i.e., the result is nonzero), then shipping is incomplete. |
177 return !(GetMissingProfileFields(profile) & | 156 return !(GetMissingProfileFields(profile) & |
178 GetRequiredProfileFieldsForShipping()); | 157 GetRequiredProfileFieldsForShipping()); |
179 } | 158 } |
180 | 159 |
181 base::string16 PaymentsProfileComparator::GetStringForMissingShippingFields( | 160 base::string16 PaymentsProfileComparator::GetStringForMissingShippingFields( |
182 const autofill::AutofillProfile& profile) const { | 161 const autofill::AutofillProfile& profile) const { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 241 |
263 bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent( | 242 bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent( |
264 const autofill::AutofillProfile& profile) const { | 243 const autofill::AutofillProfile& profile) const { |
265 std::unique_ptr<::i18n::addressinput::AddressData> data = | 244 std::unique_ptr<::i18n::addressinput::AddressData> data = |
266 autofill::i18n::CreateAddressDataFromAutofillProfile(profile, | 245 autofill::i18n::CreateAddressDataFromAutofillProfile(profile, |
267 app_locale()); | 246 app_locale()); |
268 | 247 |
269 return autofill::addressinput::HasAllRequiredFields(*data); | 248 return autofill::addressinput::HasAllRequiredFields(*data); |
270 } | 249 } |
271 | 250 |
272 bool PaymentsProfileComparator::IsContactMoreComplete( | |
273 const autofill::AutofillProfile* p1, | |
274 const autofill::AutofillProfile* p2) const { | |
275 return GetContactCompletenessScore(p1) > GetContactCompletenessScore(p2); | |
276 } | |
277 | |
278 bool PaymentsProfileComparator::IsShippingMoreComplete( | |
279 const autofill::AutofillProfile* p1, | |
280 const autofill::AutofillProfile* p2) const { | |
281 return GetShippingCompletenessScore(p1) > GetShippingCompletenessScore(p2); | |
282 } | |
283 | |
284 } // namespace payments | 251 } // namespace payments |
OLD | NEW |