summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/address.cc
blob: 6b2d3454ee43f2ea80a0394111144765272ab565 (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
// Copyright (c) 2011 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/address.h"

#include <stddef.h>

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

namespace {

const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0};

const AutofillType::FieldTypeSubGroup kAutofillAddressTypes[] = {
  AutofillType::ADDRESS_LINE1,
  AutofillType::ADDRESS_LINE2,
  AutofillType::ADDRESS_CITY,
  AutofillType::ADDRESS_STATE,
  AutofillType::ADDRESS_ZIP,
  AutofillType::ADDRESS_COUNTRY,
};

const int kAutofillAddressLength = arraysize(kAutofillAddressTypes);

// Returns the country code corresponding to |country|, which should be a
// localized country name.
std::string ToCountryCode(const string16& country) {
  std::string app_locale = AutofillCountry::ApplicationLocale();
  return AutofillCountry::GetCountryCode(country, app_locale);
}

}  // namespace

Address::Address() {}

Address::Address(const Address& address) : FormGroup() {
  *this = address;
}

Address::~Address() {}

Address& Address::operator=(const Address& address) {
  if (this == &address)
    return *this;

  line1_ = address.line1_;
  line2_ = address.line2_;
  city_ = address.city_;
  state_ = address.state_;
  country_code_ = address.country_code_;
  zip_code_ = address.zip_code_;
  return *this;
}

void Address::GetSupportedTypes(FieldTypeSet* supported_types) const {
  supported_types->insert(ADDRESS_HOME_LINE1);
  supported_types->insert(ADDRESS_HOME_LINE2);
  supported_types->insert(ADDRESS_HOME_CITY);
  supported_types->insert(ADDRESS_HOME_STATE);
  supported_types->insert(ADDRESS_HOME_ZIP);
  supported_types->insert(ADDRESS_HOME_COUNTRY);
}

string16 Address::GetInfo(AutofillFieldType type) const {
  if (type == ADDRESS_HOME_LINE1)
    return line1_;

  if (type == ADDRESS_HOME_LINE2)
    return line2_;

  if (type == ADDRESS_HOME_CITY)
    return city_;

  if (type == ADDRESS_HOME_STATE)
    return state_;

  if (type ==  ADDRESS_HOME_ZIP)
    return zip_code_;

  if (type == ADDRESS_HOME_COUNTRY)
    return Country();

  return string16();
}

void Address::SetInfo(AutofillFieldType type, const string16& value) {
  FieldTypeSubGroup subgroup = AutofillType(type).subgroup();
  if (subgroup == AutofillType::ADDRESS_LINE1)
    line1_ = value;
  else if (subgroup == AutofillType::ADDRESS_LINE2)
    line2_ = value;
  else if (subgroup == AutofillType::ADDRESS_CITY)
    city_ = value;
  else if (subgroup == AutofillType::ADDRESS_STATE)
    state_ = value;
  else if (subgroup == AutofillType::ADDRESS_COUNTRY)
    country_code_ = ToCountryCode(value);
  else if (subgroup == AutofillType::ADDRESS_ZIP)
    zip_code_ = value;
  else
    NOTREACHED();
}

void Address::GetMatchingTypes(const string16& text,
                               FieldTypeSet* matching_types) const {
  FormGroup::GetMatchingTypes(text, matching_types);

  // Check to see if the |text| canonicalized as a country name is a match.
  std::string country_code = ToCountryCode(text);
  if (!country_code.empty() && country_code_ == country_code)
    matching_types->insert(ADDRESS_HOME_COUNTRY);
}

string16 Address::Country() const {
  if (country_code().empty())
    return string16();

  std::string app_locale = AutofillCountry::ApplicationLocale();
  return AutofillCountry(country_code(), app_locale).name();
}