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 #ifndef COMPONENTS_PAYMENTS_CONTENT_PROFILE_UTIL_H_ | 5 #ifndef COMPONENTS_PAYMENTS_CONTENT_PROFILE_UTIL_H_ |
6 #define COMPONENTS_PAYMENTS_CONTENT_PROFILE_UTIL_H_ | 6 #define COMPONENTS_PAYMENTS_CONTENT_PROFILE_UTIL_H_ |
7 | 7 |
| 8 #include <map> |
8 #include <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "components/autofill/core/browser/autofill_profile_comparator.h" | 12 #include "components/autofill/core/browser/autofill_profile_comparator.h" |
12 | 13 |
13 // Utility functions used for processing and filtering address profiles | 14 // Utility functions used for processing and filtering address profiles |
14 // (AutofillProfile). | 15 // (AutofillProfile). |
15 | 16 |
16 namespace autofill { | 17 namespace autofill { |
17 class AutofillProfile; | 18 class AutofillProfile; |
18 } // namespace autofill | 19 } // namespace autofill |
19 | 20 |
20 namespace payments { | 21 namespace payments { |
21 | 22 |
22 class PaymentOptionsProvider; | 23 class PaymentOptionsProvider; |
23 | 24 |
24 namespace profile_util { | |
25 | |
26 // Returns profiles for contact info, ordered by completeness and deduplicated. | |
27 // |profiles| should be passed in order of frecency, and this order will be | |
28 // preserved among equally-complete profiles. Deduplication here means that | |
29 // profiles returned are excluded if they are a subset of a more complete or | |
30 // more frecent profile. Completeness here refers only to the presence of the | |
31 // fields requested per the request_payer_* fields in |options|. | |
32 std::vector<autofill::AutofillProfile*> FilterProfilesForContact( | |
33 const std::vector<autofill::AutofillProfile*>& profiles, | |
34 const std::string& app_locale, | |
35 const PaymentOptionsProvider& options); | |
36 | |
37 // Helper class which evaluates profiles for similarity and completeness. | 25 // Helper class which evaluates profiles for similarity and completeness. |
| 26 // Profiles are evaluated once for completeness, and the result is cached, |
| 27 // meaning one instance of this class should be used per-request to avoid |
| 28 // redoing expensive validation checks. |
| 29 // Note that, if a profile is modified and saved during the course of the |
| 30 // PaymentRequest, it is important to call the Invalidate method to ensure |
| 31 // it is properly evaluated. |
38 class PaymentsProfileComparator : public autofill::AutofillProfileComparator { | 32 class PaymentsProfileComparator : public autofill::AutofillProfileComparator { |
39 public: | 33 public: |
| 34 // Bitmask of potentially-required fields used in evaluating completeness. |
| 35 using ProfileFields = uint32_t; |
| 36 const static ProfileFields kName = 1 << 0; |
| 37 const static ProfileFields kPhone = 1 << 1; |
| 38 const static ProfileFields kEmail = 1 << 2; |
| 39 const static ProfileFields kAddress = 1 << 3; |
| 40 |
40 PaymentsProfileComparator(const std::string& app_locale, | 41 PaymentsProfileComparator(const std::string& app_locale, |
41 const PaymentOptionsProvider& options); | 42 const PaymentOptionsProvider& options); |
42 ~PaymentsProfileComparator(); | 43 ~PaymentsProfileComparator(); |
43 | 44 |
| 45 // Returns a bitmask indicating which fields (or groups of fields) on this |
| 46 // profile are not complete and valid. |
| 47 ProfileFields GetMissingProfileFields( |
| 48 const autofill::AutofillProfile* profile) const; |
| 49 |
| 50 // Returns profiles for contact info, ordered by completeness and |
| 51 // deduplicated. |profiles| should be passed in order of frecency, and this |
| 52 // order will be preserved among equally-complete profiles. Deduplication here |
| 53 // means that profiles returned are excluded if they are a subset of a more |
| 54 // complete or more frecent profile. Completeness here refers only to the |
| 55 // presence of the fields requested per the request_payer_* fields in |
| 56 // |options|. |
| 57 std::vector<autofill::AutofillProfile*> FilterProfilesForContact( |
| 58 const std::vector<autofill::AutofillProfile*>& profiles) const; |
| 59 |
44 // Returns true iff all of the contact info in |sub| also appears in |super|. | 60 // Returns true iff all of the contact info in |sub| also appears in |super|. |
45 // Only operates on fields requested in |options|. | 61 // Only operates on fields requested in |options|. |
46 bool IsContactEqualOrSuperset(const autofill::AutofillProfile& super, | 62 bool IsContactEqualOrSuperset(const autofill::AutofillProfile& super, |
47 const autofill::AutofillProfile& sub); | 63 const autofill::AutofillProfile& sub) const; |
48 | 64 |
49 // Returns the number of contact fields requested in |options| which are | 65 // Returns the number of contact fields requested in |options| which are |
50 // nonempty in |profile|. | 66 // nonempty in |profile|. |
51 int GetContactCompletenessScore(const autofill::AutofillProfile* profile); | 67 int GetContactCompletenessScore( |
| 68 const autofill::AutofillProfile* profile) const; |
52 | 69 |
53 // Returns true iff every contact field requested in |options| is nonempty in | 70 // Returns true iff every contact field requested in |options| is nonempty in |
54 // |profile|. | 71 // |profile|. |
55 bool IsContactInfoComplete(const autofill::AutofillProfile* profile); | 72 bool IsContactInfoComplete(const autofill::AutofillProfile* profile) const; |
56 | 73 |
57 // Comparison function suitable for sorting profiles by contact completeness | 74 // Comparison function suitable for sorting profiles by contact completeness |
58 // score with std::sort. | 75 // score with std::sort. |
59 bool IsContactMoreComplete(const autofill::AutofillProfile* p1, | 76 bool IsContactMoreComplete(const autofill::AutofillProfile* p1, |
60 const autofill::AutofillProfile* p2); | 77 const autofill::AutofillProfile* p2) const; |
| 78 |
| 79 // Returns a localized string to be displayed in UI indicating what action, |
| 80 // if any, must be taken for the given profile to be used as contact info. |
| 81 base::string16 GetStringForMissingContactFields( |
| 82 const autofill::AutofillProfile& profile) const; |
| 83 |
| 84 // Returns true iff every field needed to use |profile| as a shipping address |
| 85 // is populated. |
| 86 bool IsShippingComplete(const autofill::AutofillProfile* profile) const; |
| 87 |
| 88 // Returns a localized string to be displayed in UI indicating what action, |
| 89 // if any, must be taken for the given profile to be used as a shipping |
| 90 // address. |
| 91 base::string16 GetStringForMissingShippingFields( |
| 92 const autofill::AutofillProfile& profile) const; |
| 93 |
| 94 // Clears the cached evaluation result for |profile|. Must be called when a |
| 95 // profile is modified and saved during the course of a PaymentRequest. |
| 96 void Invalidate(const autofill::AutofillProfile& profile); |
61 | 97 |
62 private: | 98 private: |
| 99 ProfileFields GetRequiredProfileFieldsForContact() const; |
| 100 ProfileFields GetRequiredProfileFieldsForShipping() const; |
| 101 base::string16 ProfileFieldsToUIString(ProfileFields fields) const; |
| 102 bool AreRequiredAddressFieldsPresent( |
| 103 const autofill::AutofillProfile& profile) const; |
| 104 mutable std::map<std::string, ProfileFields> cache_; |
63 const PaymentOptionsProvider& options_; | 105 const PaymentOptionsProvider& options_; |
64 }; | 106 }; |
65 | 107 |
66 } // namespace profile_util | |
67 } // namespace payments | 108 } // namespace payments |
68 | 109 |
69 #endif // COMPONENTS_PAYMENTS_CONTENT_PROFILE_UTIL_H_ | 110 #endif // COMPONENTS_PAYMENTS_CONTENT_PROFILE_UTIL_H_ |
OLD | NEW |