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 #ifndef COMPONENTS_PAYMENTS_CORE_PAYMENTS_PROFILE_COMPARATOR_H_ |
| 6 #define COMPONENTS_PAYMENTS_CORE_PAYMENTS_PROFILE_COMPARATOR_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "components/autofill/core/browser/autofill_profile_comparator.h" |
| 13 |
| 14 // Utility functions used for processing and filtering address profiles |
| 15 // (AutofillProfile). |
| 16 |
| 17 namespace autofill { |
| 18 class AutofillProfile; |
| 19 } // namespace autofill |
| 20 |
| 21 namespace payments { |
| 22 |
| 23 class PaymentOptionsProvider; |
| 24 |
| 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. |
| 32 class PaymentsProfileComparator : public autofill::AutofillProfileComparator { |
| 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 |
| 41 PaymentsProfileComparator(const std::string& app_locale, |
| 42 const PaymentOptionsProvider& options); |
| 43 ~PaymentsProfileComparator(); |
| 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 |
| 60 // Returns true iff all of the contact info in |sub| also appears in |super|. |
| 61 // Only operates on fields requested in |options|. |
| 62 bool IsContactEqualOrSuperset(const autofill::AutofillProfile& super, |
| 63 const autofill::AutofillProfile& sub) const; |
| 64 |
| 65 // Returns the number of contact fields requested in |options| which are |
| 66 // nonempty in |profile|. |
| 67 int GetContactCompletenessScore( |
| 68 const autofill::AutofillProfile* profile) const; |
| 69 |
| 70 // Returns true iff every contact field requested in |options| is nonempty in |
| 71 // |profile|. |
| 72 bool IsContactInfoComplete(const autofill::AutofillProfile* profile) const; |
| 73 |
| 74 // Comparison function suitable for sorting profiles by contact completeness |
| 75 // score with std::sort. |
| 76 bool IsContactMoreComplete(const autofill::AutofillProfile* p1, |
| 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); |
| 97 |
| 98 private: |
| 99 ProfileFields ComputeMissingFields( |
| 100 const autofill::AutofillProfile& profile) const; |
| 101 ProfileFields GetRequiredProfileFieldsForContact() const; |
| 102 ProfileFields GetRequiredProfileFieldsForShipping() const; |
| 103 base::string16 GetStringForMissingFields(ProfileFields fields) const; |
| 104 bool AreRequiredAddressFieldsPresent( |
| 105 const autofill::AutofillProfile& profile) const; |
| 106 mutable std::map<std::string, ProfileFields> cache_; |
| 107 const PaymentOptionsProvider& options_; |
| 108 }; |
| 109 |
| 110 } // namespace payments |
| 111 |
| 112 #endif // COMPONENTS_PAYMENTS_CORE_PAYMENTS_PROFILE_COMPARATOR_H_ |
OLD | NEW |