Chromium Code Reviews| 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 | |
| 124 // Create a bitmask of the fields that are both present and required. | 121 // Create a bitmask of the fields that are both present and required. |
| 125 ProfileFields present = | 122 ProfileFields present = |
| 126 ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForContact(); | 123 ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForContact(); |
| 127 | 124 |
| 128 // Count how many are set. | 125 // Count how many are set. |
| 129 return !!(present & kName) + !!(present & kPhone) + !!(present & kEmail); | 126 return !!(present & kName) + !!(present & kPhone) + !!(present & kEmail); |
| 130 } | 127 } |
| 131 | 128 |
| 132 bool PaymentsProfileComparator::IsContactInfoComplete( | 129 bool PaymentsProfileComparator::IsContactInfoComplete( |
| 133 const autofill::AutofillProfile* profile) const { | 130 const autofill::AutofillProfile* profile) const { |
| 134 // Mask the fields that are missing with those that are requried. If any bits | 131 // Mask the fields that are missing with those that are requried. If any bits |
| 135 // are set (i.e., the result is nonzero), then contact info is incomplete. | 132 // are set (i.e., the result is nonzero), then contact info is incomplete. |
| 136 return !(GetMissingProfileFields(profile) & | 133 return !(GetMissingProfileFields(profile) & |
| 137 GetRequiredProfileFieldsForContact()); | 134 GetRequiredProfileFieldsForContact()); |
| 138 } | 135 } |
| 139 | 136 |
| 140 bool PaymentsProfileComparator::IsContactMoreComplete( | 137 bool PaymentsProfileComparator::IsContactMoreComplete( |
| 141 const autofill::AutofillProfile* p1, | 138 const autofill::AutofillProfile* p1, |
| 142 const autofill::AutofillProfile* p2) const { | 139 const autofill::AutofillProfile* p2) const { |
| 143 return GetContactCompletenessScore(p1) > GetContactCompletenessScore(p2); | 140 return GetContactCompletenessScore(p1) > GetContactCompletenessScore(p2); |
| 144 } | 141 } |
| 145 | 142 |
| 146 base::string16 PaymentsProfileComparator::GetStringForMissingContactFields( | 143 base::string16 PaymentsProfileComparator::GetStringForMissingContactFields( |
| 147 const autofill::AutofillProfile& profile) const { | 144 const autofill::AutofillProfile& profile) const { |
| 148 return GetStringForMissingFields(GetMissingProfileFields(&profile) & | 145 return GetStringForMissingFields(GetMissingProfileFields(&profile) & |
| 149 GetRequiredProfileFieldsForContact()); | 146 GetRequiredProfileFieldsForContact()); |
| 150 } | 147 } |
| 151 | 148 |
| 149 std::vector<autofill::AutofillProfile*> | |
| 150 PaymentsProfileComparator::FilterProfilesForShipping( | |
| 151 const std::vector<autofill::AutofillProfile*>& profiles) const { | |
| 152 // Since we'll be changing the order/contents of the const input vector, | |
| 153 // we make a copy. | |
| 154 std::vector<autofill::AutofillProfile*> processed = profiles; | |
| 155 | |
| 156 std::stable_sort( | |
| 157 processed.begin(), processed.end(), | |
| 158 std::bind(&PaymentsProfileComparator::IsShippingMoreComplete, this, | |
| 159 std::placeholders::_1, std::placeholders::_2)); | |
| 160 | |
| 161 // TODO(tmartino): Remove profiles with no relevant information, or which | |
|
Mathieu
2017/05/16 20:16:51
TODO(crbug.com/xxxxxx) please
tmartino
2017/05/18 20:49:37
Done
| |
| 162 // are subsets of more-complete profiles. | |
| 163 | |
| 164 return processed; | |
| 165 } | |
| 166 | |
| 167 int PaymentsProfileComparator::GetShippingCompletenessScore( | |
| 168 const autofill::AutofillProfile* profile) const { | |
| 169 // Create a bitmask of the fields that are both present and required. | |
| 170 ProfileFields present = | |
| 171 ~GetMissingProfileFields(profile) & GetRequiredProfileFieldsForShipping(); | |
| 172 | |
| 173 // Count how many are set. The completeness of the address is weighted so as | |
| 174 // to dominate the other fields. | |
| 175 return !!(present & kName) + !!(present & kPhone) + | |
| 176 (10 * !!(present & kAddress)); | |
| 177 } | |
| 178 | |
| 152 bool PaymentsProfileComparator::IsShippingComplete( | 179 bool PaymentsProfileComparator::IsShippingComplete( |
| 153 const autofill::AutofillProfile* profile) const { | 180 const autofill::AutofillProfile* profile) const { |
| 154 // Mask the fields that are missing with those that are requried. If any bits | 181 // Mask the fields that are missing with those that are requried. If any bits |
| 155 // are set (i.e., the result is nonzero), then shipping is incomplete. | 182 // are set (i.e., the result is nonzero), then shipping is incomplete. |
| 156 return !(GetMissingProfileFields(profile) & | 183 return !(GetMissingProfileFields(profile) & |
| 157 GetRequiredProfileFieldsForShipping()); | 184 GetRequiredProfileFieldsForShipping()); |
| 158 } | 185 } |
| 159 | 186 |
| 187 bool PaymentsProfileComparator::IsShippingMoreComplete( | |
| 188 const autofill::AutofillProfile* p1, | |
| 189 const autofill::AutofillProfile* p2) const { | |
| 190 return GetShippingCompletenessScore(p1) > GetShippingCompletenessScore(p2); | |
| 191 } | |
| 192 | |
| 160 base::string16 PaymentsProfileComparator::GetStringForMissingShippingFields( | 193 base::string16 PaymentsProfileComparator::GetStringForMissingShippingFields( |
| 161 const autofill::AutofillProfile& profile) const { | 194 const autofill::AutofillProfile& profile) const { |
| 162 return GetStringForMissingFields(GetMissingProfileFields(&profile) & | 195 return GetStringForMissingFields(GetMissingProfileFields(&profile) & |
| 163 GetRequiredProfileFieldsForShipping()); | 196 GetRequiredProfileFieldsForShipping()); |
| 164 } | 197 } |
| 165 | 198 |
| 166 void PaymentsProfileComparator::Invalidate( | 199 void PaymentsProfileComparator::Invalidate( |
| 167 const autofill::AutofillProfile& profile) { | 200 const autofill::AutofillProfile& profile) { |
| 168 cache_.erase(profile.guid()); | 201 cache_.erase(profile.guid()); |
| 169 } | 202 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent( | 275 bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent( |
| 243 const autofill::AutofillProfile& profile) const { | 276 const autofill::AutofillProfile& profile) const { |
| 244 std::unique_ptr<::i18n::addressinput::AddressData> data = | 277 std::unique_ptr<::i18n::addressinput::AddressData> data = |
| 245 autofill::i18n::CreateAddressDataFromAutofillProfile(profile, | 278 autofill::i18n::CreateAddressDataFromAutofillProfile(profile, |
| 246 app_locale()); | 279 app_locale()); |
| 247 | 280 |
| 248 return autofill::addressinput::HasAllRequiredFields(*data); | 281 return autofill::addressinput::HasAllRequiredFields(*data); |
| 249 } | 282 } |
| 250 | 283 |
| 251 } // namespace payments | 284 } // namespace payments |
| OLD | NEW |