ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_profile.h
blobfa09e75b50573ed2d98680273cdec8696c0e35dd
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 // Overwrites the single-valued field data in |profile| with this
126 // Profile. Or, for multi-valued fields append the new values.
127 void OverwriteWithOrAddTo(const AutofillProfile& profile,
128 const std::string& app_locale);
130 // Sets |name_| to the user-input values in |names|, but uses names in |from|
131 // as a starting point. If the full names are equivalent, the parsing in
132 // |from| will take precedence. |from| can be null.
133 void CopyAndUpdateNameList(const std::vector<base::string16> names,
134 const AutofillProfile* from,
135 const std::string& app_locale);
137 // Returns |true| if |type| accepts multi-values.
138 static bool SupportsMultiValue(ServerFieldType type);
140 // Creates a differentiating label for each of the |profiles|.
141 // Labels consist of the minimal differentiating combination of:
142 // 1. Full name.
143 // 2. Address.
144 // 3. E-mail.
145 // 4. Phone.
146 // 5. Company name.
147 static void CreateDifferentiatingLabels(
148 const std::vector<AutofillProfile*>& profiles,
149 const std::string& app_locale,
150 std::vector<base::string16>* labels);
152 // Creates inferred labels for |profiles|, according to the rules above and
153 // stores them in |created_labels|. If |suggested_fields| is not NULL, the
154 // resulting label fields are drawn from |suggested_fields|, except excluding
155 // |excluded_field|. Otherwise, the label fields are drawn from a default set,
156 // and |excluded_field| is ignored; by convention, it should be of
157 // |UNKNOWN_TYPE| when |suggested_fields| is NULL. Each label includes at
158 // least |minimal_fields_shown| fields, if possible.
159 static void CreateInferredLabels(
160 const std::vector<AutofillProfile*>& profiles,
161 const std::vector<ServerFieldType>* suggested_fields,
162 ServerFieldType excluded_field,
163 size_t minimal_fields_shown,
164 const std::string& app_locale,
165 std::vector<base::string16>* labels);
167 const std::string& language_code() const { return language_code_; }
168 void set_language_code(const std::string& language_code) {
169 language_code_ = language_code;
172 // Nonempty only when type() == SERVER_PROFILE.
173 const std::string& server_id() const { return server_id_; }
175 // Returns a standardized representation of the given string for comparison
176 // purposes. The resulting string will be lower-cased with all punctuation
177 // substituted by spaces. Whitespace will be converted to ASCII space, and
178 // multiple whitespace characters will be collapsed.
180 // This string is designed for comparison purposes only and isn't suitable
181 // for storing or displaying to the user.
182 static base::string16 CanonicalizeProfileString(const base::string16& str);
184 // Returns true if the given two profile strings are similar enough that
185 // they probably refer to the same thing.
186 static bool AreProfileStringsSimilar(const base::string16& a,
187 const base::string16& b);
189 private:
190 typedef std::vector<const FormGroup*> FormGroupList;
192 // FormGroup:
193 void GetSupportedTypes(ServerFieldTypeSet* supported_types) const override;
195 // Shared implementation for GetRawMultiInfo() and GetMultiInfo(). Pass an
196 // empty |app_locale| to get the raw info; otherwise, the returned info is
197 // canonicalized according to the given |app_locale|, if appropriate.
198 void GetMultiInfoImpl(const AutofillType& type,
199 const std::string& app_locale,
200 std::vector<base::string16>* values) const;
202 // Builds inferred label from the first |num_fields_to_include| non-empty
203 // fields in |label_fields|. Uses as many fields as possible if there are not
204 // enough non-empty fields.
205 base::string16 ConstructInferredLabel(
206 const std::vector<ServerFieldType>& label_fields,
207 size_t num_fields_to_include,
208 const std::string& app_locale) const;
210 // Creates inferred labels for |profiles| at indices corresponding to
211 // |indices|, and stores the results to the corresponding elements of
212 // |labels|. These labels include enough fields to differentiate among the
213 // profiles, if possible; and also at least |num_fields_to_include| fields, if
214 // possible. The label fields are drawn from |fields|.
215 static void CreateInferredLabelsHelper(
216 const std::vector<AutofillProfile*>& profiles,
217 const std::list<size_t>& indices,
218 const std::vector<ServerFieldType>& fields,
219 size_t num_fields_to_include,
220 const std::string& app_locale,
221 std::vector<base::string16>* labels);
223 // Utilities for listing and lookup of the data members that constitute
224 // user-visible profile information.
225 FormGroupList FormGroups() const;
226 const FormGroup* FormGroupForType(const AutofillType& type) const;
227 FormGroup* MutableFormGroupForType(const AutofillType& type);
229 // Appends unique names from |names| onto the |name_| list, dropping
230 // duplicates. If a name in |names| has the same full name representation
231 // as a name in |name_|, keeps the variant that has more information (i.e.
232 // is not reconstructible via a heuristic parse of the full name string).
233 void OverwriteOrAppendNames(const std::vector<NameInfo>& names,
234 const std::string& app_locale);
236 // Same as operator==, but ignores differences in GUID.
237 bool EqualsSansGuid(const AutofillProfile& profile) const;
239 RecordType record_type_;
241 // Personal information for this profile.
242 std::vector<NameInfo> name_;
243 std::vector<EmailInfo> email_;
244 CompanyInfo company_;
245 std::vector<PhoneNumber> phone_number_;
246 Address address_;
248 // The BCP 47 language code that can be used to format |address_| for display.
249 std::string language_code_;
251 // ID assigned by the server. This will be set only for WALLET_PROFILEs.
252 std::string server_id_;
255 // So we can compare AutofillProfiles with EXPECT_EQ().
256 std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile);
258 } // namespace autofill
260 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_H_