Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 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" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 16 | |
| 17 | namespace { |
| 18 | |
| 19 | const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0}; |
| 20 | |
| 21 | } // namespace |
| 22 | |
| 23 | namespace autofill { |
| 24 | |
| 25 | Address::Address() {} |
| 26 | |
| 27 | Address::Address(const Address& address) : FormGroup() { |
| 28 | *this = address; |
| 29 | } |
| 30 | |
| 31 | Address::~Address() {} |
| 32 | |
| 33 | Address& Address::operator=(const Address& address) { |
| 34 | if (this == &address) |
| 35 | return *this; |
| 36 | |
| 37 | line1_ = address.line1_; |
| 38 | line2_ = address.line2_; |
| 39 | city_ = address.city_; |
| 40 | state_ = address.state_; |
| 41 | country_code_ = address.country_code_; |
| 42 | zip_code_ = address.zip_code_; |
| 43 | return *this; |
| 44 | } |
| 45 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 46 | base::string16 Address::GetRawInfo(ServerFieldType type) const { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 47 | // TODO(isherman): Is GetStorableType even necessary? |
| 48 | switch (AutofillType(type).GetStorableType()) { |
| 49 | case ADDRESS_HOME_LINE1: |
| 50 | return line1_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 51 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 52 | case ADDRESS_HOME_LINE2: |
| 53 | return line2_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 54 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 55 | case ADDRESS_HOME_CITY: |
| 56 | return city_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 57 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 58 | case ADDRESS_HOME_STATE: |
| 59 | return state_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 60 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 61 | case ADDRESS_HOME_ZIP: |
| 62 | return zip_code_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 63 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 64 | case ADDRESS_HOME_COUNTRY: |
| 65 | return country_code_; |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 66 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 67 | default: |
| 68 | return base::string16(); |
| 69 | } |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 72 | void Address::SetRawInfo(ServerFieldType type, const base::string16& value) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 73 | // TODO(isherman): Is GetStorableType even necessary? |
| 74 | switch (AutofillType(type).GetStorableType()) { |
| 75 | case ADDRESS_HOME_LINE1: |
| 76 | line1_ = value; |
| 77 | break; |
| 78 | |
| 79 | case ADDRESS_HOME_LINE2: |
| 80 | line2_ = value; |
| 81 | break; |
| 82 | |
| 83 | case ADDRESS_HOME_CITY: |
| 84 | city_ = value; |
| 85 | break; |
| 86 | |
| 87 | case ADDRESS_HOME_STATE: |
| 88 | state_ = value; |
| 89 | break; |
| 90 | |
| 91 | case ADDRESS_HOME_COUNTRY: |
| 92 | DCHECK(value.empty() || value.length() == 2u); |
| 93 | country_code_ = value; |
| 94 | break; |
| 95 | |
| 96 | case ADDRESS_HOME_ZIP: |
| 97 | zip_code_ = value; |
| 98 | break; |
| 99 | |
| 100 | default: |
| 101 | NOTREACHED(); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 105 | base::string16 Address::GetInfo(const AutofillType& type, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 106 | const std::string& app_locale) const { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 107 | ServerFieldType storable_type = type.GetStorableType(); |
| 108 | if (storable_type == ADDRESS_HOME_COUNTRY && !country_code_.empty()) |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 109 | return AutofillCountry(UTF16ToASCII(country_code_), app_locale).name(); |
| 110 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 111 | return GetRawInfo(storable_type); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 114 | bool Address::SetInfo(const AutofillType& type, |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 115 | const base::string16& value, |
| 116 | const std::string& app_locale) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 117 | ServerFieldType storable_type = type.GetStorableType(); |
| 118 | if (storable_type == ADDRESS_HOME_COUNTRY && !value.empty()) { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 119 | country_code_ = |
| 120 | ASCIIToUTF16(AutofillCountry::GetCountryCode(value, app_locale)); |
| 121 | return !country_code_.empty(); |
| 122 | } |
| 123 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 124 | SetRawInfo(storable_type, value); |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
| 128 | void Address::GetMatchingTypes(const base::string16& text, |
| 129 | const std::string& app_locale, |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 130 | ServerFieldTypeSet* matching_types) const { |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 131 | FormGroup::GetMatchingTypes(text, app_locale, matching_types); |
| 132 | |
| 133 | // Check to see if the |text| canonicalized as a country name is a match. |
| 134 | std::string country_code = AutofillCountry::GetCountryCode(text, app_locale); |
| 135 | if (!country_code.empty() && country_code_ == ASCIIToUTF16(country_code)) |
| 136 | matching_types->insert(ADDRESS_HOME_COUNTRY); |
| 137 | } |
| 138 | |
Ben Murdoch | 3240926 | 2013-08-07 11:04:47 +0100 | [diff] [blame] | 139 | void Address::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { |
| 140 | supported_types->insert(ADDRESS_HOME_LINE1); |
| 141 | supported_types->insert(ADDRESS_HOME_LINE2); |
| 142 | supported_types->insert(ADDRESS_HOME_CITY); |
| 143 | supported_types->insert(ADDRESS_HOME_STATE); |
| 144 | supported_types->insert(ADDRESS_HOME_ZIP); |
| 145 | supported_types->insert(ADDRESS_HOME_COUNTRY); |
| 146 | } |
| 147 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 148 | } // namespace autofill |