1// Copyright (c) 2011 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 CHROME_BROWSER_AUTOFILL_AUTOFILL_PROFILE_H_
6#define CHROME_BROWSER_AUTOFILL_AUTOFILL_PROFILE_H_
7#pragma once
8
9#include <stddef.h>
10#include <list>
11#include <ostream>
12#include <string>
13#include <vector>
14
15#ifdef ANDROID
16#include "base/base_api.h"
17#endif
18#include "base/string16.h"
19#include "chrome/browser/autofill/address.h"
20#include "chrome/browser/autofill/autofill_type.h"
21#include "chrome/browser/autofill/contact_info.h"
22#include "chrome/browser/autofill/fax_number.h"
23#include "chrome/browser/autofill/field_types.h"
24#include "chrome/browser/autofill/form_group.h"
25#include "chrome/browser/autofill/home_phone_number.h"
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
32#ifdef ANDROID
33BASE_API
34#endif
35AutofillProfile : public FormGroup {
36 public:
37  explicit AutofillProfile(const std::string& guid);
38
39  // For use in STL containers.
40  AutofillProfile();
41  AutofillProfile(const AutofillProfile& profile);
42  virtual ~AutofillProfile();
43
44  AutofillProfile& operator=(const AutofillProfile& profile);
45
46  // FormGroup:
47  virtual void GetPossibleFieldTypes(const string16& text,
48                                     FieldTypeSet* possible_types) const;
49  virtual void GetAvailableFieldTypes(FieldTypeSet* available_types) const;
50  virtual string16 GetInfo(AutofillFieldType type) const;
51  virtual void SetInfo(AutofillFieldType type, const string16& value);
52
53  // Multi-value equivalents to |GetInfo| and |SetInfo|.
54  void SetMultiInfo(AutofillFieldType type,
55                    const std::vector<string16>& values);
56  void GetMultiInfo(AutofillFieldType type,
57                    std::vector<string16>* values) const;
58
59  // Returns |true| if |type| accepts multi-values.
60  static bool SupportsMultiValue(AutofillFieldType type);
61
62  // The user-visible label of the profile, generated in relation to other
63  // profiles. Shows at least 2 fields that differentiate profile from other
64  // profiles. See AdjustInferredLabels() further down for more description.
65  virtual const string16 Label() const;
66
67  // This guid is the primary identifier for |AutofillProfile| objects.
68  const std::string guid() const { return guid_; }
69  void set_guid(const std::string& guid) { guid_ = guid; }
70
71  // Accessors for the stored address's country code.
72  const std::string CountryCode() const;
73  void SetCountryCode(const std::string& country_code);
74
75  // Adjusts the labels according to profile data.
76  // Labels contain minimal different combination of:
77  // 1. Full name.
78  // 2. Address.
79  // 3. E-mail.
80  // 4. Phone.
81  // 5. Fax.
82  // 6. Company name.
83  // Profile labels are changed accordingly to these rules.
84  // Returns true if any of the profiles were updated.
85  // This function is useful if you want to adjust unique labels for all
86  // profiles. For non permanent situations (selection of profile, when user
87  // started typing in the field, for example) use CreateInferredLabels().
88  static bool AdjustInferredLabels(std::vector<AutofillProfile*>* profiles);
89
90  // Creates inferred labels for |profiles|, according to the rules above and
91  // stores them in |created_labels|. If |suggested_fields| is not NULL, the
92  // resulting label fields are drawn from |suggested_fields|, except excluding
93  // |excluded_field|. Otherwise, the label fields are drawn from a default set,
94  // and |excluded_field| is ignored; by convention, it should be of
95  // |UNKNOWN_TYPE| when |suggested_fields| is NULL. Each label includes at
96  // least |minimal_fields_shown| fields, if possible.
97  static void CreateInferredLabels(
98      const std::vector<AutofillProfile*>* profiles,
99      const std::vector<AutofillFieldType>* suggested_fields,
100      AutofillFieldType excluded_field,
101      size_t minimal_fields_shown,
102      std::vector<string16>* created_labels);
103
104  // Returns true if there are no values (field types) set.
105  bool IsEmpty() const;
106
107  // Comparison for Sync.  Returns 0 if the profile is the same as |this|,
108  // or < 0, or > 0 if it is different.  The implied ordering can be used for
109  // culling duplicates.  The ordering is based on collation order of the
110  // textual contents of the fields.
111  // GUIDs are not compared, only the values of the contents themselves.
112  // DEPRECATED: Use |CompareMulti| instead.  |Compare| does not compare
113  // multi-valued items.
114  int Compare(const AutofillProfile& profile) const;
115
116  // Comparison for Sync.  Same as |Compare| but includes multi-valued fields.
117  int CompareMulti(const AutofillProfile& profile) const;
118
119  // Equality operators compare GUIDs and the contents in the comparison.
120  // TODO(dhollowa): This needs to be made multi-profile once Sync updates.
121  bool operator==(const AutofillProfile& profile) const;
122  virtual bool operator!=(const AutofillProfile& profile) const;
123
124  // Returns concatenation of full name and address line 1.  This acts as the
125  // basis of comparison for new values that are submitted through forms to
126  // aid with correct aggregation of new data.
127  const string16 PrimaryValue() const;
128
129  // Overwrites the single-valued field data in |profile| with this
130  // Profile.  Or, for multi-valued fields append the new values.
131  void OverwriteWithOrAddTo(const AutofillProfile& profile);
132
133 private:
134  typedef std::vector<const FormGroup*> FormGroupList;
135
136  // Builds inferred label from the first |num_fields_to_include| non-empty
137  // fields in |label_fields|. Uses as many fields as possible if there are not
138  // enough non-empty fields.
139  string16 ConstructInferredLabel(
140      const std::vector<AutofillFieldType>& label_fields,
141      size_t num_fields_to_include) const;
142
143  // Creates inferred labels for |profiles| at indices corresponding to
144  // |indices|, and stores the results to the corresponding elements of
145  // |created_labels|. These labels include enough fields to differentiate among
146  // the profiles, if possible; and also at least |num_fields_to_include|
147  // fields, if possible. The label fields are drawn from |fields|.
148  static void CreateDifferentiatingLabels(
149      const std::vector<AutofillProfile*>& profiles,
150      const std::list<size_t>& indices,
151      const std::vector<AutofillFieldType>& fields,
152      size_t num_fields_to_include,
153      std::vector<string16>* created_labels);
154
155  // Utilities for listing and lookup of the data members that constitute
156  // user-visible profile information.
157  FormGroupList FormGroups() const;
158  const FormGroup* FormGroupForType(AutofillFieldType type) const;
159  FormGroup* MutableFormGroupForType(AutofillFieldType type);
160
161  // The label presented to the user when selecting a profile.
162  string16 label_;
163
164  // The guid of this profile.
165  std::string guid_;
166
167  // Personal information for this profile.
168  std::vector<NameInfo> name_;
169  std::vector<EmailInfo> email_;
170  CompanyInfo company_;
171  std::vector<HomePhoneNumber> home_number_;
172  std::vector<FaxNumber> fax_number_;
173  Address address_;
174};
175
176// So we can compare AutofillProfiles with EXPECT_EQ().
177std::ostream& operator<<(std::ostream& os, const AutofillProfile& profile);
178
179#endif  // CHROME_BROWSER_AUTOFILL_AUTOFILL_PROFILE_H_
180