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

Side by Side Diff: components/payments/core/payments_profile_comparator.cc

Issue 2884393002: [WebPayments] Adding FilterProfilesForShipping to profile comparator (Closed)
Patch Set: rebase Created 3 years, 7 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 "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
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(
141 const autofill::AutofillProfile* p1,
142 const autofill::AutofillProfile* p2) const {
143 return GetContactCompletenessScore(p1) > GetContactCompletenessScore(p2);
144 }
145
146 base::string16 PaymentsProfileComparator::GetStringForMissingContactFields( 137 base::string16 PaymentsProfileComparator::GetStringForMissingContactFields(
147 const autofill::AutofillProfile& profile) const { 138 const autofill::AutofillProfile& profile) const {
148 return GetStringForMissingFields(GetMissingProfileFields(&profile) & 139 return GetStringForMissingFields(GetMissingProfileFields(&profile) &
149 GetRequiredProfileFieldsForContact()); 140 GetRequiredProfileFieldsForContact());
150 } 141 }
151 142
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
152 bool PaymentsProfileComparator::IsShippingComplete( 173 bool PaymentsProfileComparator::IsShippingComplete(
153 const autofill::AutofillProfile* profile) const { 174 const autofill::AutofillProfile* profile) const {
154 // Mask the fields that are missing with those that are requried. If any bits 175 // 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. 176 // are set (i.e., the result is nonzero), then shipping is incomplete.
156 return !(GetMissingProfileFields(profile) & 177 return !(GetMissingProfileFields(profile) &
157 GetRequiredProfileFieldsForShipping()); 178 GetRequiredProfileFieldsForShipping());
158 } 179 }
159 180
160 base::string16 PaymentsProfileComparator::GetStringForMissingShippingFields( 181 base::string16 PaymentsProfileComparator::GetStringForMissingShippingFields(
161 const autofill::AutofillProfile& profile) const { 182 const autofill::AutofillProfile& profile) const {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 262
242 bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent( 263 bool PaymentsProfileComparator::AreRequiredAddressFieldsPresent(
243 const autofill::AutofillProfile& profile) const { 264 const autofill::AutofillProfile& profile) const {
244 std::unique_ptr<::i18n::addressinput::AddressData> data = 265 std::unique_ptr<::i18n::addressinput::AddressData> data =
245 autofill::i18n::CreateAddressDataFromAutofillProfile(profile, 266 autofill::i18n::CreateAddressDataFromAutofillProfile(profile,
246 app_locale()); 267 app_locale());
247 268
248 return autofill::addressinput::HasAllRequiredFields(*data); 269 return autofill::addressinput::HasAllRequiredFields(*data);
249 } 270 }
250 271
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
251 } // namespace payments 284 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698