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_
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"
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
{
32 // A profile stored and editable locally.
35 // A profile synced down from the server. These are read-only locally.
38 // An auxiliary profile, such as a Mac address book entry.
42 AutofillProfile(const std::string
& guid
, const std::string
& origin
);
44 // Server profile constructor. The type must be SERVER_PROFILE (this serves
45 // to differentiate this constructor). |server_id| can be empty. If empty,
46 // callers should invoke GenerateServerProfileIdentifier after setting data.
47 AutofillProfile(RecordType type
, const std::string
& server_id
);
49 // For use in STL containers.
51 AutofillProfile(const AutofillProfile
& profile
);
52 ~AutofillProfile() override
;
54 AutofillProfile
& operator=(const AutofillProfile
& profile
);
57 void GetMatchingTypes(const base::string16
& text
,
58 const std::string
& app_locale
,
59 ServerFieldTypeSet
* matching_types
) const override
;
60 base::string16
GetRawInfo(ServerFieldType type
) const override
;
61 void SetRawInfo(ServerFieldType type
, const base::string16
& value
) override
;
62 base::string16
GetInfo(const AutofillType
& type
,
63 const std::string
& app_locale
) const override
;
64 bool SetInfo(const AutofillType
& type
,
65 const base::string16
& value
,
66 const std::string
& app_locale
) override
;
68 // How this card is stored.
69 RecordType
record_type() const { return record_type_
; }
70 void set_record_type(RecordType type
) { record_type_
= type
; }
72 // Returns true if there are no values (field types) set.
73 bool IsEmpty(const std::string
& app_locale
) const;
75 // Returns true if the |type| of data in this profile is present, but invalid.
76 // Otherwise returns false.
77 bool IsPresentButInvalid(ServerFieldType type
) const;
79 // Comparison for Sync. Returns 0 if the profile is the same as |this|,
80 // or < 0, or > 0 if it is different. The implied ordering can be used for
81 // culling duplicates. The ordering is based on collation order of the
82 // textual contents of the fields. Full profile comparison, comparison
83 // includes multi-valued fields.
85 // GUIDs, origins, and language codes are not compared, only the contents
87 int Compare(const AutofillProfile
& profile
) const;
89 // Same as operator==, but ignores differences in origin.
90 bool EqualsSansOrigin(const AutofillProfile
& profile
) const;
92 // Same as operator==, but ignores differences in guid and cares about
93 // differences in usage stats.
94 bool EqualsForSyncPurposes(const AutofillProfile
& profile
) const;
96 // Equality operators compare GUIDs, origins, language code, and the contents
97 // in the comparison. Usage metadata (use count, use date, modification date)
99 bool operator==(const AutofillProfile
& profile
) const;
100 virtual bool operator!=(const AutofillProfile
& profile
) const;
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;
107 // Returns true if the data in this AutofillProfile is a subset of the data in
109 bool IsSubsetOf(const AutofillProfile
& profile
,
110 const std::string
& app_locale
) const;
112 // Like IsSubsetOf, but only considers the types present in |types|.
113 bool IsSubsetOfForFieldSet(const AutofillProfile
& profile
,
114 const std::string
& app_locale
,
115 const ServerFieldTypeSet
& types
) const;
117 // Overwrites the field data in |profile| with this Profile.
118 void OverwriteWith(const AutofillProfile
& profile
,
119 const std::string
& app_locale
);
121 // Saves info from |profile| into |this|, provided |this| and |profile| do not
122 // have any direct conflicts (i.e. data is present but different). Will not
123 // make changes if |this| is verified and |profile| is not. Returns true if
124 // |this| and |profile| are similar.
125 bool SaveAdditionalInfo(const AutofillProfile
& profile
,
126 const std::string
& app_locale
);
128 // Returns |true| if |type| accepts multi-values.
129 static bool SupportsMultiValue(ServerFieldType type
);
131 // Creates a differentiating label for each of the |profiles|.
132 // Labels consist of the minimal differentiating combination of:
138 static void CreateDifferentiatingLabels(
139 const std::vector
<AutofillProfile
*>& profiles
,
140 const std::string
& app_locale
,
141 std::vector
<base::string16
>* labels
);
143 // Creates inferred labels for |profiles|, according to the rules above and
144 // stores them in |created_labels|. If |suggested_fields| is not NULL, the
145 // resulting label fields are drawn from |suggested_fields|, except excluding
146 // |excluded_field|. Otherwise, the label fields are drawn from a default set,
147 // and |excluded_field| is ignored; by convention, it should be of
148 // |UNKNOWN_TYPE| when |suggested_fields| is NULL. Each label includes at
149 // least |minimal_fields_shown| fields, if possible.
150 static void CreateInferredLabels(
151 const std::vector
<AutofillProfile
*>& profiles
,
152 const std::vector
<ServerFieldType
>* suggested_fields
,
153 ServerFieldType excluded_field
,
154 size_t minimal_fields_shown
,
155 const std::string
& app_locale
,
156 std::vector
<base::string16
>* labels
);
158 const std::string
& language_code() const { return language_code_
; }
159 void set_language_code(const std::string
& language_code
) {
160 language_code_
= language_code
;
163 // Nonempty only when type() == SERVER_PROFILE. base::kSHA1Length bytes long.
164 // Not necessarily valid UTF-8.
165 const std::string
& server_id() const { return server_id_
; }
167 // Creates an identifier and saves it as |server_id_|. Only used for
168 // server credit cards. The server doesn't attach an identifier so Chrome
169 // creates its own. The ID is a hash of the data contained in the profile.
170 void GenerateServerProfileIdentifier();
172 // Returns a standardized representation of the given string for comparison
173 // purposes. The resulting string will be lower-cased with all punctuation
174 // substituted by spaces. Whitespace will be converted to ASCII space, and
175 // multiple whitespace characters will be collapsed.
177 // This string is designed for comparison purposes only and isn't suitable
178 // for storing or displaying to the user.
179 static base::string16
CanonicalizeProfileString(const base::string16
& str
);
181 // Returns true if the given two profile strings are similar enough that
182 // they probably refer to the same thing.
183 static bool AreProfileStringsSimilar(const base::string16
& a
,
184 const base::string16
& b
);
187 typedef std::vector
<const FormGroup
*> FormGroupList
;
190 void GetSupportedTypes(ServerFieldTypeSet
* supported_types
) const override
;
192 // Builds inferred label from the first |num_fields_to_include| non-empty
193 // fields in |label_fields|. Uses as many fields as possible if there are not
194 // enough non-empty fields.
195 base::string16
ConstructInferredLabel(
196 const std::vector
<ServerFieldType
>& label_fields
,
197 size_t num_fields_to_include
,
198 const std::string
& app_locale
) const;
200 // Creates inferred labels for |profiles| at indices corresponding to
201 // |indices|, and stores the results to the corresponding elements of
202 // |labels|. These labels include enough fields to differentiate among the
203 // profiles, if possible; and also at least |num_fields_to_include| fields, if
204 // possible. The label fields are drawn from |fields|.
205 static void CreateInferredLabelsHelper(
206 const std::vector
<AutofillProfile
*>& profiles
,
207 const std::list
<size_t>& indices
,
208 const std::vector
<ServerFieldType
>& fields
,
209 size_t num_fields_to_include
,
210 const std::string
& app_locale
,
211 std::vector
<base::string16
>* labels
);
213 // Utilities for listing and lookup of the data members that constitute
214 // user-visible profile information.
215 FormGroupList
FormGroups() const;
216 const FormGroup
* FormGroupForType(const AutofillType
& type
) const;
217 FormGroup
* MutableFormGroupForType(const AutofillType
& type
);
219 // If |name| has the same full name representation as |name_|,
220 // this will keep the one that has more information (i.e.
221 // is not reconstructible via a heuristic parse of the full name string).
222 void OverwriteName(const NameInfo
& name
, const std::string
& app_locale
);
224 // Same as operator==, but ignores differences in GUID.
225 bool EqualsSansGuid(const AutofillProfile
& profile
) const;
227 RecordType record_type_
;
229 // Personal information for this profile.
232 CompanyInfo company_
;
233 PhoneNumber phone_number_
;
236 // The BCP 47 language code that can be used to format |address_| for display.
237 std::string language_code_
;
239 // ID used for identifying this profile. Only set for SERVER_PROFILEs. This is
240 // a hash of the contents.
241 std::string server_id_
;
244 // So we can compare AutofillProfiles with EXPECT_EQ().
245 std::ostream
& operator<<(std::ostream
& os
, const AutofillProfile
& profile
);
247 } // namespace autofill
249 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_