summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_profile.cc
blob: a8f0397b09577cf95869d2e57c2b169f6812f08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/autofill_profile.h"

#include <vector>

#include "app/l10n_util.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "chrome/browser/autofill/address.h"
#include "chrome/browser/autofill/autofill_manager.h"
#include "chrome/browser/autofill/billing_address.h"
#include "chrome/browser/autofill/contact_info.h"
#include "chrome/browser/autofill/fax_number.h"
#include "chrome/browser/autofill/home_address.h"
#include "chrome/browser/autofill/home_phone_number.h"
#include "grit/generated_resources.h"

AutoFillProfile::AutoFillProfile(const string16& label, int unique_id)
    : label_(label),
      unique_id_(unique_id),
      use_billing_address_(true) {
  personal_info_[AutoFillType::CONTACT_INFO] = new ContactInfo();
  personal_info_[AutoFillType::PHONE_HOME] = new HomePhoneNumber();
  personal_info_[AutoFillType::PHONE_FAX] = new FaxNumber();
  personal_info_[AutoFillType::ADDRESS_HOME] = new HomeAddress();
  personal_info_[AutoFillType::ADDRESS_BILLING] = new BillingAddress();
}

AutoFillProfile::AutoFillProfile()
    : unique_id_(0),
      use_billing_address_(true) {
}

AutoFillProfile::AutoFillProfile(const AutoFillProfile& source)
    : FormGroup() {
  operator=(source);
}

AutoFillProfile::~AutoFillProfile() {
  STLDeleteContainerPairSecondPointers(personal_info_.begin(),
                                       personal_info_.end());
}

void AutoFillProfile::GetPossibleFieldTypes(
    const string16& text,
    FieldTypeSet* possible_types) const {
  FormGroupMap::const_iterator iter;
  for (iter = personal_info_.begin(); iter != personal_info_.end(); ++iter) {
    FormGroup* data = iter->second;
    DCHECK(data != NULL);
    if (data != NULL)
      data->GetPossibleFieldTypes(text, possible_types);
  }
}

string16 AutoFillProfile::GetFieldText(const AutoFillType& type) const {
  FormGroupMap::const_iterator iter = personal_info_.find(type.group());
  if (iter == personal_info_.end() || iter->second == NULL)
    return string16();

  return iter->second->GetFieldText(type);
}

void AutoFillProfile::FindInfoMatches(
    const AutoFillType& type,
    const string16& info,
    std::vector<string16>* matched_text) const {
  if (matched_text == NULL) {
    DLOG(ERROR) << "NULL matched text passed in";
    return;
  }

  string16 clean_info = StringToLowerASCII(CollapseWhitespace(info, false));

  // If the field_type is unknown, then match against all field types.
  if (type.field_type() == UNKNOWN_TYPE) {
    FormGroupMap::const_iterator iter;
    for (iter = personal_info_.begin(); iter != personal_info_.end(); ++iter) {
      iter->second->FindInfoMatches(type, clean_info, matched_text);
    }
  } else {
    FormGroupMap::const_iterator iter = personal_info_.find(type.group());
    DCHECK(iter != personal_info_.end() && iter->second != NULL);
    if (iter != personal_info_.end() && iter->second != NULL)
      iter->second->FindInfoMatches(type, clean_info, matched_text);
  }
}

void AutoFillProfile::SetInfo(const AutoFillType& type, const string16& value) {
  FormGroupMap::const_iterator iter = personal_info_.find(type.group());
  if (iter == personal_info_.end() || iter->second == NULL)
    return;

  iter->second->SetInfo(type, CollapseWhitespace(value, false));
}

FormGroup* AutoFillProfile::Clone() const {
  AutoFillProfile* profile = new AutoFillProfile();
  profile->label_ = label_;
  profile->unique_id_ = unique_id();
  profile->use_billing_address_ = use_billing_address_;

  FormGroupMap::const_iterator iter;
  for (iter = personal_info_.begin(); iter != personal_info_.end(); ++iter) {
    profile->personal_info_[iter->first] = iter->second->Clone();
  }

  return profile;
}

string16 AutoFillProfile::PreviewSummary() const {
  // Fetch the components of the summary string.  Any or all of these
  // may be an empty string.
  string16 first_name = GetFieldText(AutoFillType(NAME_FIRST));
  string16 last_name = GetFieldText(AutoFillType(NAME_LAST));
  string16 address = GetFieldText(AutoFillType(ADDRESS_HOME_LINE1));

  // String separators depend (below) on the existence of the various fields.
  bool have_first_name = first_name.length() > 0;
  bool have_last_name = last_name.length() > 0;
  bool have_address = address.length() > 0;

  // Name separator defaults to "".  Space if we have first and last name.
  string16 name_separator;
  if (have_first_name && have_last_name) {
    name_separator = l10n_util::GetStringUTF16(
        IDS_AUTOFILL_DIALOG_ADDRESS_NAME_SEPARATOR);
  }

  // E.g. "John Smith", or "John", or "Smith", or "".
  string16 name_format = l10n_util::GetStringFUTF16(
      IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_NAME_FORMAT,
      first_name,
      name_separator,
      last_name);

  // Summary separator defaults to "".  ", " if we have name and address.
  string16 summary_separator;
  if ((have_first_name || have_last_name) && have_address) {
    summary_separator = l10n_util::GetStringUTF16(
        IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_SEPARATOR);
  }

  // E.g. "John Smith, 123 Main Street".
  string16 summary_format = l10n_util::GetStringFUTF16(
      IDS_AUTOFILL_DIALOG_ADDRESS_SUMMARY_FORMAT,
      name_format,
      summary_separator,
      address);

  return summary_format;
}

void AutoFillProfile::operator=(const AutoFillProfile& source) {
  label_ = source.label_;
  unique_id_ = source.unique_id_;
  use_billing_address_ = source.use_billing_address_;

  STLDeleteContainerPairSecondPointers(personal_info_.begin(),
                                       personal_info_.end());
  personal_info_.clear();

  FormGroupMap::const_iterator iter;
  for (iter = source.personal_info_.begin();
       iter != source.personal_info_.end();
       ++iter) {
    personal_info_[iter->first] = iter->second->Clone();
  }
}

bool AutoFillProfile::operator==(const AutoFillProfile& profile) const {
  // The following AutoFill field types are the only types we store in the WebDB
  // so far, so we're only concerned with matching these types in the profile.
  const AutoFillFieldType types[] = { NAME_FIRST,
                                      NAME_MIDDLE,
                                      NAME_LAST,
                                      EMAIL_ADDRESS,
                                      COMPANY_NAME,
                                      ADDRESS_HOME_LINE1,
                                      ADDRESS_HOME_LINE2,
                                      ADDRESS_HOME_CITY,
                                      ADDRESS_HOME_STATE,
                                      ADDRESS_HOME_ZIP,
                                      ADDRESS_HOME_COUNTRY,
                                      PHONE_HOME_NUMBER,
                                      PHONE_FAX_NUMBER };

  if (label_ != profile.label_ ||
      unique_id_ != profile.unique_id_ ||
      use_billing_address_ != profile.use_billing_address_) {
    return false;
  }

  for (size_t index = 0; index < arraysize(types); ++index) {
    if (GetFieldText(AutoFillType(types[index])) !=
        profile.GetFieldText(AutoFillType(types[index])))
      return false;
  }

  return true;
}

bool AutoFillProfile::operator!=(const AutoFillProfile& profile) const {
  return !operator==(profile);
}

void AutoFillProfile::set_use_billing_address(bool use) {
  if (use_billing_address_ == use)
    return;

  Address* billing_address = GetBillingAddress();

  if (use) {
    // If we were using the home address as a billing address then the home
    // address information should be cleared out of the billing address object.
    billing_address->Clear();
  } else {
    // If we no longer want to use an alternate billing address then clone the
    // home address information for our billing address.
    Address* home_address = GetHomeAddress();
    billing_address->Clone(*home_address);
  }

  use_billing_address_ = use;
}

Address* AutoFillProfile::GetBillingAddress() {
  return static_cast<Address*>(personal_info_[AutoFillType::ADDRESS_BILLING]);
}

Address* AutoFillProfile::GetHomeAddress() {
  return static_cast<Address*>(personal_info_[AutoFillType::ADDRESS_HOME]);
}

// So we can compare AutoFillProfiles with EXPECT_EQ().
std::ostream& operator<<(std::ostream& os, const AutoFillProfile& profile) {
  return os
      << UTF16ToUTF8(profile.Label())
      << " "
      << profile.unique_id()
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_FIRST)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_MIDDLE)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(NAME_LAST)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(EMAIL_ADDRESS)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(COMPANY_NAME)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE1)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_LINE2)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_CITY)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_STATE)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_ZIP)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(ADDRESS_HOME_COUNTRY)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(
             PHONE_HOME_WHOLE_NUMBER)))
      << " "
      << UTF16ToUTF8(profile.GetFieldText(AutoFillType(
             PHONE_FAX_WHOLE_NUMBER)));
}