Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 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_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_
      6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_
      7 
      8 #include <stddef.h>
      9 
     10 #include <iosfwd>
     11 #include <list>
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "base/compiler_specific.h"
     16 #include "base/strings/string16.h"
     17 #include "components/autofill/core/browser/address.h"
     18 #include "components/autofill/core/browser/autofill_data_model.h"
     19 #include "components/autofill/core/browser/autofill_type.h"
     20 #include "components/autofill/core/browser/contact_info.h"
     21 #include "components/autofill/core/browser/phone_number.h"
     22 
     23 namespace autofill {
     24 
     25 struct FormFieldData;
     26 
     27 // A collection of FormGroups stored in a profile.  AutofillProfile also
     28 // implements the FormGroup interface so that owners of this object can request
     29 // form information from the profile, and the profile will delegate the request
     30 // to the requested form group type.
     31 class AutofillProfile : public AutofillDataModel {
     32  public:
     33   AutofillProfile(const std::string& guid, const std::string& origin);
     34 
     35   // For use in STL containers.
     36   AutofillProfile();
     37   AutofillProfile(const AutofillProfile& profile);
     38   virtual ~AutofillProfile();
     39 
     40   AutofillProfile& operator=(const AutofillProfile& profile);
     41 
     42   // FormGroup:
     43   virtual void GetMatchingTypes(
     44       const base::string16& text,
     45       const std::string& app_locale,
     46       ServerFieldTypeSet* matching_types) const OVERRIDE;
     47   virtual base::string16 GetRawInfo(ServerFieldType type) const OVERRIDE;
     48   virtual void SetRawInfo(ServerFieldType type,
     49                           const base::string16& value) OVERRIDE;
     50   virtual base::string16 GetInfo(const AutofillType& type,
     51                                  const std::string& app_locale) const OVERRIDE;
     52   virtual bool SetInfo(const AutofillType& type,
     53                        const base::string16& value,
     54                        const std::string& app_locale) OVERRIDE;
     55 
     56   // AutofillDataModel:
     57   virtual base::string16 GetInfoForVariant(
     58       const AutofillType& type,
     59       size_t variant,
     60       const std::string& app_locale) const OVERRIDE;
     61 
     62   // Multi-value equivalents to |GetInfo| and |SetInfo|.
     63   void SetRawMultiInfo(ServerFieldType type,
     64                        const std::vector<base::string16>& values);
     65   void GetRawMultiInfo(ServerFieldType type,
     66                        std::vector<base::string16>* values) const;
     67   void SetMultiInfo(const AutofillType& type,
     68                     const std::vector<base::string16>& values,
     69                     const std::string& app_locale);
     70   void GetMultiInfo(const AutofillType& type,
     71                     const std::string& app_locale,
     72                     std::vector<base::string16>* values) const;
     73 
     74   // Returns true if there are no values (field types) set.
     75   bool IsEmpty(const std::string& app_locale) const;
     76 
     77   // Returns true if the |type| of data in this profile is present, but invalid.
     78   // Otherwise returns false.
     79   bool IsPresentButInvalid(ServerFieldType type) const;
     80 
     81   // Comparison for Sync.  Returns 0 if the profile is the same as |this|,
     82   // or < 0, or > 0 if it is different.  The implied ordering can be used for
     83   // culling duplicates.  The ordering is based on collation order of the
     84   // textual contents of the fields. Full profile comparison, comparison
     85   // includes multi-valued fields.
     86   //
     87   // GUIDs, origins, and language codes are not compared, only the contents
     88   // themselves.
     89   int Compare(const AutofillProfile& profile) const;
     90 
     91   // Same as operator==, but ignores differences in origin.
     92   bool EqualsSansOrigin(const AutofillProfile& profile) const;
     93 
     94   // Same as operator==, but ignores differences in GUID.
     95   bool EqualsSansGuid(const AutofillProfile& profile) const;
     96 
     97   // Equality operators compare GUIDs, origins, language code, and the contents
     98   // in the comparison.
     99   bool operator==(const AutofillProfile& profile) const;
    100   virtual bool operator!=(const AutofillProfile& profile) const;
    101 
    102   // Returns concatenation of full name and address line 1.  This acts as the
    103   // basis of comparison for new values that are submitted through forms to
    104   // aid with correct aggregation of new data.
    105   const base::string16 PrimaryValue() const;
    106 
    107   // Returns true if the data in this AutofillProfile is a subset of the data in
    108   // |profile|.
    109   bool IsSubsetOf(const AutofillProfile& profile,
    110                   const std::string& app_locale) const;
    111 
    112   // Overwrites the single-valued field data in |profile| with this
    113   // Profile.  Or, for multi-valued fields append the new values.
    114   void OverwriteWithOrAddTo(const AutofillProfile& profile,
    115                             const std::string& app_locale);
    116 
    117   // Returns |true| if |type| accepts multi-values.
    118   static bool SupportsMultiValue(ServerFieldType type);
    119 
    120   // Creates a differentiating label for each of the |profiles|.
    121   // Labels consist of the minimal differentiating combination of:
    122   // 1. Full name.
    123   // 2. Address.
    124   // 3. E-mail.
    125   // 4. Phone.
    126   // 5. Company name.
    127   static void CreateDifferentiatingLabels(
    128       const std::vector<AutofillProfile*>& profiles,
    129       const std::string& app_locale,
    130       std::vector<base::string16>* labels);
    131 
    132   // Creates inferred labels for |profiles|, according to the rules above and
    133   // stores them in |created_labels|. If |suggested_fields| is not NULL, the
    134   // resulting label fields are drawn from |suggested_fields|, except excluding
    135   // |excluded_field|. Otherwise, the label fields are drawn from a default set,
    136   // and |excluded_field| is ignored; by convention, it should be of
    137   // |UNKNOWN_TYPE| when |suggested_fields| is NULL. Each label includes at
    138   // least |minimal_fields_shown| fields, if possible.
    139   static void CreateInferredLabels(
    140       const std::vector<AutofillProfile*>& profiles,
    141       const std::vector<ServerFieldType>* suggested_fields,
    142       ServerFieldType excluded_field,
    143       size_t minimal_fields_shown,
    144       const std::string& app_locale,
    145       std::vector<base::string16>* labels);
    146 
    147   const std::string& language_code() const { return language_code_; }
    148   void set_language_code(const std::string& language_code) {
    149     language_code_ = language_code;
    150   }
    151 
    152  private:
    153   typedef std::vector<const FormGroup*> FormGroupList;
    154 
    155   // FormGroup:
    156   virtual void GetSupportedTypes(
    157       ServerFieldTypeSet* supported_types) const OVERRIDE;
    158 
    159   // Shared implementation for GetRawMultiInfo() and GetMultiInfo().  Pass an
    160   // empty |app_locale| to get the raw info; otherwise, the returned info is
    161   // canonicalized according to the given |app_locale|, if appropriate.
    162   void GetMultiInfoImpl(const AutofillType& type,
    163                         const std::string& app_locale,
    164                         std::vector<base::string16>* values) const;
    165 
    166   // Builds inferred label from the first |num_fields_to_include| non-empty
    167   // fields in |label_fields|. Uses as many fields as possible if there are not
    168   // enough non-empty fields.
    169   base::string16 ConstructInferredLabel(
    170       const std::vector<ServerFieldType>& label_fields,
    171       size_t num_fields_to_include,
    172       const std::string& app_locale) const;
    173 
    174   // Creates inferred labels for |profiles| at indices corresponding to
    175   // |indices|, and stores the results to the corresponding elements of
    176   // |labels|. These labels include enough fields to differentiate among the
    177   // profiles, if possible; and also at least |num_fields_to_include| fields, if
    178   // possible. The label fields are drawn from |fields|.
    179   static void CreateInferredLabelsHelper(
    180       const std::vector<AutofillProfile*>& profiles,
    181       const std::list<size_t>& indices,
    182       const std::vector<ServerFieldType>& fields,
    183       size_t num_fields_to_include,
    184       const std::string& app_locale,
    185       std::vector<base::string16>* labels);
    186 
    187   // Utilities for listing and lookup of the data members that constitute
    188   // user-visible profile information.
    189   FormGroupList FormGroups() const;
    190   const FormGroup* FormGroupForType(const AutofillType& type) const;
    191   FormGroup* MutableFormGroupForType(const AutofillType& type);
    192 
    193   // Appends unique names from |names| onto the |name_| list, dropping
    194   // duplicates. If a name in |names| has the same full name representation
    195   // as a name in |name_|, keeps the variant that has more information (i.e.
    196   // is not reconstructible via a heuristic parse of the full name string).
    197   void OverwriteOrAppendNames(const std::vector<NameInfo>& names,
    198                               const std::string& app_locale);
    199 
    200   // Personal information for this profile.
    201   std::vector<NameInfo> name_;
    202   std::vector<EmailInfo> email_;
    203   CompanyInfo company_;
    204   std::vector<PhoneNumber> phone_number_;
    205   Address address_;
    206 
    207   // The BCP 47 language code that can be used to format |address_| for display.
    208   std::string language_code_;
    209 };
    210 
    211 // So we can compare AutofillProfiles with EXPECT_EQ().
    212 std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile);
    213 
    214 }  // namespace autofill
    215 
    216 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_
    217