autofill_profile.h revision 116680a4aac90f2aa7413d9095a592090648e557
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
23namespace autofill {
24
25struct 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.
31class 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 GetMultiInfo(const AutofillType& type,
68                    const std::string& app_locale,
69                    std::vector<base::string16>* values) const;
70
71  // Returns true if there are no values (field types) set.
72  bool IsEmpty(const std::string& app_locale) const;
73
74  // Returns true if the |type| of data in this profile is present, but invalid.
75  // Otherwise returns false.
76  bool IsPresentButInvalid(ServerFieldType type) const;
77
78  // Comparison for Sync.  Returns 0 if the profile is the same as |this|,
79  // or < 0, or > 0 if it is different.  The implied ordering can be used for
80  // culling duplicates.  The ordering is based on collation order of the
81  // textual contents of the fields. Full profile comparison, comparison
82  // includes multi-valued fields.
83  //
84  // GUIDs, origins, and language codes are not compared, only the contents
85  // themselves.
86  int Compare(const AutofillProfile& profile) const;
87
88  // Same as operator==, but ignores differences in origin.
89  bool EqualsSansOrigin(const AutofillProfile& profile) const;
90
91  // Same as operator==, but ignores differences in GUID.
92  bool EqualsSansGuid(const AutofillProfile& profile) const;
93
94  // Equality operators compare GUIDs, origins, language code, and the contents
95  // in the comparison.
96  bool operator==(const AutofillProfile& profile) const;
97  virtual bool operator!=(const AutofillProfile& profile) const;
98
99  // Returns concatenation of full name and address line 1.  This acts as the
100  // basis of comparison for new values that are submitted through forms to
101  // aid with correct aggregation of new data.
102  const base::string16 PrimaryValue() const;
103
104  // Returns true if the data in this AutofillProfile is a subset of the data in
105  // |profile|.
106  bool IsSubsetOf(const AutofillProfile& profile,
107                  const std::string& app_locale) const;
108
109  // Overwrites the single-valued field data in |profile| with this
110  // Profile.  Or, for multi-valued fields append the new values.
111  void OverwriteWithOrAddTo(const AutofillProfile& profile,
112                            const std::string& app_locale);
113
114  // Returns |true| if |type| accepts multi-values.
115  static bool SupportsMultiValue(ServerFieldType type);
116
117  // Creates a differentiating label for each of the |profiles|.
118  // Labels consist of the minimal differentiating combination of:
119  // 1. Full name.
120  // 2. Address.
121  // 3. E-mail.
122  // 4. Phone.
123  // 5. Company name.
124  static void CreateDifferentiatingLabels(
125      const std::vector<AutofillProfile*>& profiles,
126      const std::string& app_locale,
127      std::vector<base::string16>* labels);
128
129  // Creates inferred labels for |profiles|, according to the rules above and
130  // stores them in |created_labels|. If |suggested_fields| is not NULL, the
131  // resulting label fields are drawn from |suggested_fields|, except excluding
132  // |excluded_field|. Otherwise, the label fields are drawn from a default set,
133  // and |excluded_field| is ignored; by convention, it should be of
134  // |UNKNOWN_TYPE| when |suggested_fields| is NULL. Each label includes at
135  // least |minimal_fields_shown| fields, if possible.
136  static void CreateInferredLabels(
137      const std::vector<AutofillProfile*>& profiles,
138      const std::vector<ServerFieldType>* suggested_fields,
139      ServerFieldType excluded_field,
140      size_t minimal_fields_shown,
141      const std::string& app_locale,
142      std::vector<base::string16>* labels);
143
144  const std::string& language_code() const { return language_code_; }
145  void set_language_code(const std::string& language_code) {
146    language_code_ = language_code;
147  }
148
149 private:
150  typedef std::vector<const FormGroup*> FormGroupList;
151
152  // FormGroup:
153  virtual void GetSupportedTypes(
154      ServerFieldTypeSet* supported_types) const OVERRIDE;
155
156  // Shared implementation for GetRawMultiInfo() and GetMultiInfo().  Pass an
157  // empty |app_locale| to get the raw info; otherwise, the returned info is
158  // canonicalized according to the given |app_locale|, if appropriate.
159  void GetMultiInfoImpl(const AutofillType& type,
160                        const std::string& app_locale,
161                        std::vector<base::string16>* values) const;
162
163  // Builds inferred label from the first |num_fields_to_include| non-empty
164  // fields in |label_fields|. Uses as many fields as possible if there are not
165  // enough non-empty fields.
166  base::string16 ConstructInferredLabel(
167      const std::vector<ServerFieldType>& label_fields,
168      size_t num_fields_to_include,
169      const std::string& app_locale) const;
170
171  // Creates inferred labels for |profiles| at indices corresponding to
172  // |indices|, and stores the results to the corresponding elements of
173  // |labels|. These labels include enough fields to differentiate among the
174  // profiles, if possible; and also at least |num_fields_to_include| fields, if
175  // possible. The label fields are drawn from |fields|.
176  static void CreateInferredLabelsHelper(
177      const std::vector<AutofillProfile*>& profiles,
178      const std::list<size_t>& indices,
179      const std::vector<ServerFieldType>& fields,
180      size_t num_fields_to_include,
181      const std::string& app_locale,
182      std::vector<base::string16>* labels);
183
184  // Utilities for listing and lookup of the data members that constitute
185  // user-visible profile information.
186  FormGroupList FormGroups() const;
187  const FormGroup* FormGroupForType(const AutofillType& type) const;
188  FormGroup* MutableFormGroupForType(const AutofillType& type);
189
190  // Appends unique names from |names| onto the |name_| list, dropping
191  // duplicates. If a name in |names| has the same full name representation
192  // as a name in |name_|, keeps the variant that has more information (i.e.
193  // is not reconstructible via a heuristic parse of the full name string).
194  void OverwriteOrAppendNames(const std::vector<NameInfo>& names,
195                              const std::string& app_locale);
196
197  // Personal information for this profile.
198  std::vector<NameInfo> name_;
199  std::vector<EmailInfo> email_;
200  CompanyInfo company_;
201  std::vector<PhoneNumber> phone_number_;
202  Address address_;
203
204  // The BCP 47 language code that can be used to format |address_| for display.
205  std::string language_code_;
206};
207
208// So we can compare AutofillProfiles with EXPECT_EQ().
209std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile);
210
211}  // namespace autofill
212
213#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_
214