summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/contact_info.cc
blob: 0709a246d5df7ad5d4fe2ccf953946a9d382dc24 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// 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/contact_info.h"

#include "base/basictypes.h"
#include "base/string_util.h"
#include "chrome/browser/autofill/autofill_type.h"
#include "chrome/browser/autofill/field_types.h"

static const string16 kNameSplitChars = ASCIIToUTF16("-'. ");

static const AutoFillFieldType kAutoFillContactInfoTypes[] = {
  NAME_FIRST,
  NAME_MIDDLE,
  NAME_LAST,
  EMAIL_ADDRESS,
  COMPANY_NAME,
};

static const size_t kAutoFillContactInfoLength =
    arraysize(kAutoFillContactInfoTypes);

FormGroup* ContactInfo::Clone() const {
  return new ContactInfo(*this);
}

void ContactInfo::GetPossibleFieldTypes(const string16& text,
                                        FieldTypeSet* possible_types) const {
  DCHECK(possible_types);

  if (IsFirstName(text))
    possible_types->insert(NAME_FIRST);

  if (IsMiddleName(text))
    possible_types->insert(NAME_MIDDLE);

  if (IsLastName(text))
    possible_types->insert(NAME_LAST);

  if (IsMiddleInitial(text))
    possible_types->insert(NAME_MIDDLE_INITIAL);

  if (IsSuffix(text))
    possible_types->insert(NAME_SUFFIX);

  if (IsFullName(text))
    possible_types->insert(NAME_FULL);

  if (email_ == text)
    possible_types->insert(EMAIL_ADDRESS);

  if (company_name_ == text)
    possible_types->insert(COMPANY_NAME);
}

void ContactInfo::GetAvailableFieldTypes(FieldTypeSet* available_types) const {
  DCHECK(available_types);

  if (!first().empty())
    available_types->insert(NAME_FIRST);

  if (!middle().empty())
    available_types->insert(NAME_MIDDLE);

  if (!last().empty())
    available_types->insert(NAME_LAST);

  if (!MiddleInitial().empty())
    available_types->insert(NAME_MIDDLE_INITIAL);

  if (!FullName().empty())
    available_types->insert(NAME_FULL);

  if (!suffix().empty())
    available_types->insert(NAME_SUFFIX);

  if (!email().empty())
    available_types->insert(EMAIL_ADDRESS);

  if (!company_name().empty())
    available_types->insert(COMPANY_NAME);
}

void ContactInfo::FindInfoMatches(const AutoFillType& type,
                                  const string16& info,
                                  std::vector<string16>* matched_text) const {
  DCHECK(matched_text);

  string16 match;
  if (type.field_type() == UNKNOWN_TYPE) {
    for (size_t i = 0; i < kAutoFillContactInfoLength; i++) {
      if (FindInfoMatchesHelper(kAutoFillContactInfoTypes[i], info, &match))
        matched_text->push_back(match);
    }
  } else if (FindInfoMatchesHelper(type.field_type(), info, &match)) {
      matched_text->push_back(match);
  }
}

string16 ContactInfo::GetFieldText(const AutoFillType& type) const {
  AutoFillFieldType field_type = type.field_type();
  if (field_type == NAME_FIRST)
    return first();

  if (field_type == NAME_MIDDLE)
    return middle();

  if (field_type == NAME_LAST)
    return last();

  if (field_type == NAME_MIDDLE_INITIAL)
    return MiddleInitial();

  if (field_type == NAME_FULL)
    return FullName();

  if (field_type == NAME_SUFFIX)
    return suffix();

  if (field_type == EMAIL_ADDRESS)
    return email();

  if (field_type == COMPANY_NAME)
    return company_name();

  return string16();
}

void ContactInfo::SetInfo(const AutoFillType& type, const string16& value) {
  AutoFillFieldType field_type = type.field_type();
  DCHECK(type.group() == AutoFillType::CONTACT_INFO);
  if (field_type == NAME_FIRST)
    SetFirst(value);
  else if (field_type == NAME_MIDDLE || field_type == NAME_MIDDLE_INITIAL)
    SetMiddle(value);
  else if (field_type == NAME_LAST)
    SetLast(value);
  else if (field_type == NAME_SUFFIX)
    set_suffix(value);
  else if (field_type == EMAIL_ADDRESS)
    email_ = value;
  else if (field_type == COMPANY_NAME)
    company_name_ = value;
  else if (field_type == NAME_FULL)
    SetFullName(value);
  else
    NOTREACHED();
}

ContactInfo::ContactInfo(const ContactInfo& contact_info)
    : FormGroup(),
      first_tokens_(contact_info.first_tokens_),
      middle_tokens_(contact_info.middle_tokens_),
      last_tokens_(contact_info.last_tokens_),
      first_(contact_info.first_),
      middle_(contact_info.middle_),
      last_(contact_info.last_),
      suffix_(contact_info.suffix_),
      email_(contact_info.email_),
      company_name_(contact_info.company_name_) {
}

string16 ContactInfo::FullName() const {
  if (first_.empty())
    return string16();

  std::vector<string16> full_name;
  full_name.push_back(first_);

  if (!middle_.empty())
    full_name.push_back(middle_);

  if (!last_.empty())
    full_name.push_back(last_);

  if (!suffix_.empty())
    full_name.push_back(suffix_);

  return JoinString(full_name, ' ');
}

string16 ContactInfo::MiddleInitial() const {
  if (middle_.empty())
    return string16();

  string16 middle_name(middle());
  string16 initial;
  initial.push_back(middle_name[0]);
  return initial;
}

bool ContactInfo::FindInfoMatchesHelper(const AutoFillFieldType& field_type,
                                        const string16& info,
                                        string16* match) const {
  if (match == NULL) {
    DLOG(ERROR) << "NULL match string passed in";
    return false;
  }

  match->clear();
  if (field_type == NAME_FIRST &&
      StartsWith(first(), info, false)) {
    *match = first();
  } else if (field_type == NAME_MIDDLE &&
             StartsWith(middle(), info, false)) {
    *match = middle();
  } else if (field_type == NAME_LAST &&
             StartsWith(last(), info, false)) {
    *match = last();
  } else if (field_type == NAME_SUFFIX &&
             StartsWith(suffix(), info, false)) {
    *match = suffix();
  } else if (field_type == NAME_MIDDLE_INITIAL && IsMiddleInitial(info)) {
    *match = MiddleInitial();
  } else if (field_type == NAME_FULL &&
             StartsWith(FullName(), info, false)) {
    *match = FullName();
  } else if (field_type == EMAIL_ADDRESS &&
             StartsWith(email(), info, false)) {
    *match = email();
  } else if (field_type == COMPANY_NAME &&
             StartsWith(company_name(), info, false)) {
    *match = company_name();
  }

  return !match->empty();
}

// If each of the 'words' contained in the text are also present in the first
// name then we will consider the text to be of type kFirstName. This means
// that people with multiple first names will be able to enter any one of
// their first names and have it correctly recognized.
bool ContactInfo::IsFirstName(const string16& text) const {
  return IsNameMatch(text, first_tokens_);
}

// If each of the 'words' contained in the text are also present in the middle
// name then we will consider the text to be of type kMiddleName.
bool ContactInfo::IsMiddleName(const string16& text) const {
  return IsNameMatch(text, middle_tokens_);
}

// If each of the 'words' contained in the text are also present in the last
// name then we will consider the text to be of type kLastName.
bool ContactInfo::IsLastName(const string16& text) const {
  return IsNameMatch(text, last_tokens_);
}

bool ContactInfo::IsSuffix(const string16& text) const {
  string16 lower_suffix = StringToLowerASCII(suffix_);
  return (lower_suffix == text);
}

bool ContactInfo::IsMiddleInitial(const string16& text) const {
  if (text.length() != 1)
     return false;

  string16 lower_case = StringToLowerASCII(text);
  // If the text entered was a single character and it matches the first letter
  // of any of the given middle names then we consider it to be a middle
  // initial field.
  size_t middle_tokens_size = middle_tokens_.size();
  for (size_t i = 0; i < middle_tokens_size; ++i) {
    if (middle_tokens_[i][0] == lower_case[0])
      return true;
  }

  return false;
}

// A field will be considered to be of type NAME_FULL if:
//    1) it contains at least one word from the first name.
//    2) it contains at least one word from the last name.
//    3) all of the words in the field match a word in either the first,
//       middle, or last name.
bool ContactInfo::IsFullName(const string16& text) const {
  size_t first_tokens_size = first_tokens_.size();
  if (first_tokens_size == 0)
    return false;

  size_t middle_tokens_size = middle_tokens_.size();

  size_t last_tokens_size = last_tokens_.size();
  if (last_tokens_size == 0)
    return false;

  NameTokens text_tokens;
  Tokenize(text, kNameSplitChars, &text_tokens);
  size_t text_tokens_size = text_tokens.size();
  if (text_tokens_size == 0 || text_tokens_size < 2)
    return false;

  size_t name_tokens_size =
      first_tokens_size + middle_tokens_size + last_tokens_size;
  if (text_tokens_size > name_tokens_size)
    return false;

  bool first_name_match = false;
  bool last_name_match = false;
  NameTokens::iterator iter;
  for (iter = text_tokens.begin(); iter != text_tokens.end(); ++iter) {
    bool match = false;
    if (IsWordInName(*iter, first_tokens_)) {
      match = true;
      first_name_match = true;
    }

    if (IsWordInName(*iter, last_tokens_)) {
      match = true;
      last_name_match = true;
    }

    if (IsWordInName(*iter, middle_tokens_))
      match = true;

    if (!match)
      return false;
  }

  return (first_name_match && last_name_match);
}

bool ContactInfo::IsNameMatch(const string16& text,
                              const NameTokens& name_tokens) const {
  size_t name_tokens_size = name_tokens.size();
  if (name_tokens_size == 0)
    return false;

  NameTokens text_tokens;
  Tokenize(text, kNameSplitChars, &text_tokens);
  size_t text_tokens_size = text_tokens.size();
  if (text_tokens_size == 0)
    return false;

  if (text_tokens_size > name_tokens_size)
    return false;

  // If each of the 'words' contained in the text are also present in the name,
  // then we will consider the text to match the name.
  NameTokens::iterator iter;
  for (iter = text_tokens.begin(); iter != text_tokens.end(); ++iter) {
    if (!IsWordInName(*iter, name_tokens))
      return false;
  }

  return true;
}

bool ContactInfo::IsWordInName(const string16& word,
                               const NameTokens& name_tokens) const {
  NameTokens::const_iterator iter;
  for (iter = name_tokens.begin(); iter != name_tokens.end(); ++iter) {
    // |*iter| is already lower-cased.
    if (StringToLowerASCII(word) == *iter)
      return true;
  }

  return false;
}

void ContactInfo::SetFirst(const string16& first) {
  first_ = first;
  first_tokens_.clear();
  Tokenize(first, kNameSplitChars, &first_tokens_);
  NameTokens::iterator iter;
  for (iter = first_tokens_.begin(); iter != first_tokens_.end(); ++iter)
    *iter = StringToLowerASCII(*iter);
}

void ContactInfo::SetMiddle(const string16& middle) {
  middle_ = middle;
  middle_tokens_.clear();
  Tokenize(middle, kNameSplitChars, &middle_tokens_);
  NameTokens::iterator iter;
  for (iter = middle_tokens_.begin(); iter != middle_tokens_.end(); ++iter)
    *iter = StringToLowerASCII(*iter);
}

void ContactInfo::SetLast(const string16& last) {
  last_ = last;
  last_tokens_.clear();
  Tokenize(last, kNameSplitChars, &last_tokens_);
  NameTokens::iterator iter;
  for (iter = last_tokens_.begin(); iter != last_tokens_.end(); ++iter)
    *iter = StringToLowerASCII(*iter);
}

void ContactInfo::SetFullName(const string16& full) {
  NameTokens full_name_tokens;
  Tokenize(full, ASCIIToUTF16(" "), &full_name_tokens);
  // Clear the names.
  SetFirst(string16());
  SetMiddle(string16());
  SetLast(string16());

  // There are four possibilities: empty; first name; first and last names;
  // first, middle (possibly multiple strings) and then the last name.
  if (full_name_tokens.size() > 0) {
    SetFirst(full_name_tokens[0]);
    if (full_name_tokens.size() > 1) {
      SetLast(full_name_tokens.back());
      if (full_name_tokens.size() > 2) {
        full_name_tokens.erase(full_name_tokens.begin());
        full_name_tokens.pop_back();
        SetMiddle(JoinString(full_name_tokens, ' '));
      }
    }
  }
}