address.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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.
4
5#include "components/autofill/core/browser/address.h"
6
7#include <stddef.h>
8
9#include "base/basictypes.h"
10#include "base/logging.h"
11#include "base/strings/string_util.h"
12#include "base/strings/utf_string_conversions.h"
13#include "components/autofill/core/browser/autofill_country.h"
14#include "components/autofill/core/browser/autofill_field.h"
15#include "components/autofill/core/browser/autofill_type.h"
16#include "components/autofill/core/browser/field_types.h"
17
18namespace {
19
20const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0};
21
22}  // namespace
23
24namespace autofill {
25
26Address::Address() {}
27
28Address::Address(const Address& address) : FormGroup() {
29  *this = address;
30}
31
32Address::~Address() {}
33
34Address& Address::operator=(const Address& address) {
35  if (this == &address)
36    return *this;
37
38  line1_ = address.line1_;
39  line2_ = address.line2_;
40  city_ = address.city_;
41  state_ = address.state_;
42  country_code_ = address.country_code_;
43  zip_code_ = address.zip_code_;
44  return *this;
45}
46
47void Address::GetSupportedTypes(FieldTypeSet* supported_types) const {
48  supported_types->insert(ADDRESS_HOME_LINE1);
49  supported_types->insert(ADDRESS_HOME_LINE2);
50  supported_types->insert(ADDRESS_HOME_CITY);
51  supported_types->insert(ADDRESS_HOME_STATE);
52  supported_types->insert(ADDRESS_HOME_ZIP);
53  supported_types->insert(ADDRESS_HOME_COUNTRY);
54}
55
56base::string16 Address::GetRawInfo(AutofillFieldType type) const {
57  type = AutofillType::GetEquivalentFieldType(type);
58  if (type == ADDRESS_HOME_LINE1)
59    return line1_;
60
61  if (type == ADDRESS_HOME_LINE2)
62    return line2_;
63
64  if (type == ADDRESS_HOME_CITY)
65    return city_;
66
67  if (type == ADDRESS_HOME_STATE)
68    return state_;
69
70  if (type ==  ADDRESS_HOME_ZIP)
71    return zip_code_;
72
73  if (type == ADDRESS_HOME_COUNTRY)
74    return country_code_;
75
76  return base::string16();
77}
78
79void Address::SetRawInfo(AutofillFieldType type, const base::string16& value) {
80  type = AutofillType::GetEquivalentFieldType(type);
81  if (type == ADDRESS_HOME_LINE1) {
82    line1_ = value;
83  } else if (type == ADDRESS_HOME_LINE2) {
84    line2_ = value;
85  } else if (type == ADDRESS_HOME_CITY) {
86    city_ = value;
87  } else if (type == ADDRESS_HOME_STATE) {
88    state_ = value;
89  } else if (type == ADDRESS_HOME_COUNTRY) {
90    DCHECK(value.empty() || value.length() == 2u);
91    country_code_ = value;
92  } else if (type == ADDRESS_HOME_ZIP) {
93    zip_code_ = value;
94  } else {
95    NOTREACHED();
96  }
97}
98
99base::string16 Address::GetInfo(AutofillFieldType type,
100                                const std::string& app_locale) const {
101  type = AutofillType::GetEquivalentFieldType(type);
102  if (type == ADDRESS_HOME_COUNTRY && !country_code_.empty())
103    return AutofillCountry(UTF16ToASCII(country_code_), app_locale).name();
104
105  return GetRawInfo(type);
106}
107
108bool Address::SetInfo(AutofillFieldType type,
109                      const base::string16& value,
110                      const std::string& app_locale) {
111  type = AutofillType::GetEquivalentFieldType(type);
112  if (type == ADDRESS_HOME_COUNTRY && !value.empty()) {
113    country_code_ =
114        ASCIIToUTF16(AutofillCountry::GetCountryCode(value, app_locale));
115    return !country_code_.empty();
116  }
117
118  SetRawInfo(type, value);
119  return true;
120}
121
122void Address::GetMatchingTypes(const base::string16& text,
123                               const std::string& app_locale,
124                               FieldTypeSet* matching_types) const {
125  FormGroup::GetMatchingTypes(text, app_locale, matching_types);
126
127  // Check to see if the |text| canonicalized as a country name is a match.
128  std::string country_code = AutofillCountry::GetCountryCode(text, app_locale);
129  if (!country_code.empty() && country_code_ == ASCIIToUTF16(country_code))
130    matching_types->insert(ADDRESS_HOME_COUNTRY);
131}
132
133}  // namespace autofill
134