Don't suggest autofill profiles that are subsets of other profiles.
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_profile.h
blobe5cef3f043ff255d1d7453708a150dc2dc22402f
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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_
8 #include <stddef.h>
10 #include <iosfwd>
11 #include <list>
12 #include <string>
13 #include <vector>
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"
23 namespace autofill {
25 // A collection of FormGroups stored in a profile. AutofillProfile also
26 // implements the FormGroup interface so that owners of this object can request
27 // form information from the profile, and the profile will delegate the request
28 // to the requested form group type.
29 class AutofillProfile : public AutofillDataModel {
30 public:
31 enum RecordType {
32 // A profile stored and editable locally.
33 LOCAL_PROFILE,
35 // A profile synced down from the server. These are read-only locally.
36 SERVER_PROFILE,
39 AutofillProfile(const std::string& guid, const std::string& origin);
41 // Server profile constructor. The type must be SERVER_PROFILE (this serves
42 // to differentiate this constructor).
43 AutofillProfile(RecordType type, const std::string& server_id);
45 // For use in STL containers.
46 AutofillProfile();
47 AutofillProfile(const AutofillProfile& profile);
48 ~AutofillProfile() override;
50 AutofillProfile& operator=(const AutofillProfile& profile);
52 // FormGroup:
53 void GetMatchingTypes(const base::string16& text,
54 const std::string& app_locale,
55 ServerFieldTypeSet* matching_types) const override;
56 base::string16 GetRawInfo(ServerFieldType type) const override;
57 void SetRawInfo(ServerFieldType type, const base::string16& value) override;
58 base::string16 GetInfo(const AutofillType& type,
59 const std::string& app_locale) const override;
60 bool SetInfo(const AutofillType& type,
61 const base::string16& value,
62 const std::string& app_locale) override;
64 // AutofillDataModel:
65 base::string16 GetInfoForVariant(
66 const AutofillType& type,
67 size_t variant,
68 const std::string& app_locale) const override;
70 // How this card is stored.
71 RecordType record_type() const { return record_type_; }
73 // Multi-value equivalents to |GetInfo| and |SetInfo|.
74 void SetRawMultiInfo(ServerFieldType type,
75 const std::vector<base::string16>& values);
76 void GetRawMultiInfo(ServerFieldType type,
77 std::vector<base::string16>* values) const;
78 void SetMultiInfo(const AutofillType& type,
79 const std::vector<base::string16>& values,
80 const std::string& app_locale);
81 void GetMultiInfo(const AutofillType& type,
82 const std::string& app_locale,
83 std::vector<base::string16>* values) const;
85 // Returns true if there are no values (field types) set.
86 bool IsEmpty(const std::string& app_locale) const;
88 // Returns true if the |type| of data in this profile is present, but invalid.
89 // Otherwise returns false.
90 bool IsPresentButInvalid(ServerFieldType type) const;
92 // Comparison for Sync. Returns 0 if the profile is the same as |this|,
93 // or < 0, or > 0 if it is different. The implied ordering can be used for
94 // culling duplicates. The ordering is based on collation order of the
95 // textual contents of the fields. Full profile comparison, comparison
96 // includes multi-valued fields.
98 // GUIDs, origins, and language codes are not compared, only the contents
99 // themselves.
100 int Compare(const AutofillProfile& profile) const;
102 // Same as operator==, but ignores differences in origin.
103 bool EqualsSansOrigin(const AutofillProfile& profile) const;
105 // Same as operator==, but ignores differences in guid and cares about
106 // differences in usage stats.
107 bool EqualsForSyncPurposes(const AutofillProfile& profile) const;
109 // Equality operators compare GUIDs, origins, language code, and the contents
110 // in the comparison. Usage metadata (use count, use date, modification date)
111 // are NOT compared.
112 bool operator==(const AutofillProfile& profile) const;
113 virtual bool operator!=(const AutofillProfile& profile) const;
115 // Returns concatenation of full name and address line 1. This acts as the
116 // basis of comparison for new values that are submitted through forms to
117 // aid with correct aggregation of new data.
118 const base::string16 PrimaryValue() const;
120 // Returns true if the data in this AutofillProfile is a subset of the data in
121 // |profile|.
122 bool IsSubsetOf(const AutofillProfile& profile,
123 const std::string& app_locale) const;
125 // Like IsSubsetOf, but only considers the types present in |types|.
126 bool IsSubsetOfForFieldSet(const AutofillProfile& profile,
127 const std::string& app_locale,
128 const ServerFieldTypeSet& types) const;
130 // Overwrites the single-valued field data in |profile| with this
131 // Profile. Or, for multi-valued fields append the new values.
132 void OverwriteWithOrAddTo(const AutofillProfile& profile,
133 const std::string& app_locale);
135 // Sets |name_| to the user-input values in |names|, but uses names in |from|
136 // as a starting point. If the full names are equivalent, the parsing in
137 // |from| will take precedence. |from| can be null.
138 void CopyAndUpdateNameList(const std::vector<base::string16> names,
139 const AutofillProfile* from,
140 const std::string& app_locale);
142 // Returns |true| if |type| accepts multi-values.
143 static bool SupportsMultiValue(ServerFieldType type);
145 // Creates a differentiating label for each of the |profiles|.
146 // Labels consist of the minimal differentiating combination of:
147 // 1. Full name.
148 // 2. Address.
149 // 3. E-mail.
150 // 4. Phone.
151 // 5. Company name.
152 static void CreateDifferentiatingLabels(
153 const std::vector<AutofillProfile*>& profiles,
154 const std::string& app_locale,
155 std::vector<base::string16>* labels);
157 // Creates inferred labels for |profiles|, according to the rules above and
158 // stores them in |created_labels|. If |suggested_fields| is not NULL, the
159 // resulting label fields are drawn from |suggested_fields|, except excluding
160 // |excluded_field|. Otherwise, the label fields are drawn from a default set,
161 // and |excluded_field| is ignored; by convention, it should be of
162 // |UNKNOWN_TYPE| when |suggested_fields| is NULL. Each label includes at
163 // least |minimal_fields_shown| fields, if possible.
164 static void CreateInferredLabels(
165 const std::vector<AutofillProfile*>& profiles,
166 const std::vector<ServerFieldType>* suggested_fields,
167 ServerFieldType excluded_field,
168 size_t minimal_fields_shown,
169 const std::string& app_locale,
170 std::vector<base::string16>* labels);
172 const std::string& language_code() const { return language_code_; }
173 void set_language_code(const std::string& language_code) {
174 language_code_ = language_code;
177 // Nonempty only when type() == SERVER_PROFILE.
178 const std::string& server_id() const { return server_id_; }
180 // Returns a standardized representation of the given string for comparison
181 // purposes. The resulting string will be lower-cased with all punctuation
182 // substituted by spaces. Whitespace will be converted to ASCII space, and
183 // multiple whitespace characters will be collapsed.
185 // This string is designed for comparison purposes only and isn't suitable
186 // for storing or displaying to the user.
187 static base::string16 CanonicalizeProfileString(const base::string16& str);
189 // Returns true if the given two profile strings are similar enough that
190 // they probably refer to the same thing.
191 static bool AreProfileStringsSimilar(const base::string16& a,
192 const base::string16& b);
194 private:
195 typedef std::vector<const FormGroup*> FormGroupList;
197 // FormGroup:
198 void GetSupportedTypes(ServerFieldTypeSet* supported_types) const override;
200 // Shared implementation for GetRawMultiInfo() and GetMultiInfo(). Pass an
201 // empty |app_locale| to get the raw info; otherwise, the returned info is
202 // canonicalized according to the given |app_locale|, if appropriate.
203 void GetMultiInfoImpl(const AutofillType& type,
204 const std::string& app_locale,
205 std::vector<base::string16>* values) const;
207 // Builds inferred label from the first |num_fields_to_include| non-empty
208 // fields in |label_fields|. Uses as many fields as possible if there are not
209 // enough non-empty fields.
210 base::string16 ConstructInferredLabel(
211 const std::vector<ServerFieldType>& label_fields,
212 size_t num_fields_to_include,
213 const std::string& app_locale) const;
215 // Creates inferred labels for |profiles| at indices corresponding to
216 // |indices|, and stores the results to the corresponding elements of
217 // |labels|. These labels include enough fields to differentiate among the
218 // profiles, if possible; and also at least |num_fields_to_include| fields, if
219 // possible. The label fields are drawn from |fields|.
220 static void CreateInferredLabelsHelper(
221 const std::vector<AutofillProfile*>& profiles,
222 const std::list<size_t>& indices,
223 const std::vector<ServerFieldType>& fields,
224 size_t num_fields_to_include,
225 const std::string& app_locale,
226 std::vector<base::string16>* labels);
228 // Utilities for listing and lookup of the data members that constitute
229 // user-visible profile information.
230 FormGroupList FormGroups() const;
231 const FormGroup* FormGroupForType(const AutofillType& type) const;
232 FormGroup* MutableFormGroupForType(const AutofillType& type);
234 // Appends unique names from |names| onto the |name_| list, dropping
235 // duplicates. If a name in |names| has the same full name representation
236 // as a name in |name_|, keeps the variant that has more information (i.e.
237 // is not reconstructible via a heuristic parse of the full name string).
238 void OverwriteOrAppendNames(const std::vector<NameInfo>& names,
239 const std::string& app_locale);
241 // Same as operator==, but ignores differences in GUID.
242 bool EqualsSansGuid(const AutofillProfile& profile) const;
244 RecordType record_type_;
246 // Personal information for this profile.
247 std::vector<NameInfo> name_;
248 std::vector<EmailInfo> email_;
249 CompanyInfo company_;
250 std::vector<PhoneNumber> phone_number_;
251 Address address_;
253 // The BCP 47 language code that can be used to format |address_| for display.
254 std::string language_code_;
256 // ID assigned by the server. This will be set only for WALLET_PROFILEs.
257 std::string server_id_;
260 // So we can compare AutofillProfiles with EXPECT_EQ().
261 std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile);
263 } // namespace autofill
265 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_