Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "components/autofill/core/browser/address.h" | 5 #include "components/autofill/core/browser/address.h" | 
| 6 | 6 | 
| 7 #include <stddef.h> | 7 #include <stddef.h> | 
| 8 | 8 | 
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" | 
| 10 #include "base/logging.h" | 10 #include "base/logging.h" | 
| 11 #include "base/strings/string_split.h" | |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" | 
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" | 
| 13 #include "components/autofill/core/browser/autofill_country.h" | 14 #include "components/autofill/core/browser/autofill_country.h" | 
| 14 #include "components/autofill/core/browser/autofill_field.h" | 15 #include "components/autofill/core/browser/autofill_field.h" | 
| 15 #include "components/autofill/core/browser/autofill_type.h" | 16 #include "components/autofill/core/browser/autofill_type.h" | 
| 16 | 17 | 
| 17 namespace autofill { | 18 namespace autofill { | 
| 18 | 19 | 
| 19 Address::Address() {} | 20 Address::Address() {} | 
| 20 | 21 | 
| (...skipping 30 matching lines...) Expand all Loading... | |
| 51 | 52 | 
| 52 case ADDRESS_HOME_STATE: | 53 case ADDRESS_HOME_STATE: | 
| 53 return state_; | 54 return state_; | 
| 54 | 55 | 
| 55 case ADDRESS_HOME_ZIP: | 56 case ADDRESS_HOME_ZIP: | 
| 56 return zip_code_; | 57 return zip_code_; | 
| 57 | 58 | 
| 58 case ADDRESS_HOME_COUNTRY: | 59 case ADDRESS_HOME_COUNTRY: | 
| 59 return ASCIIToUTF16(country_code_); | 60 return ASCIIToUTF16(country_code_); | 
| 60 | 61 | 
| 62 case ADDRESS_HOME_STREET_ADDRESS: { | |
| 63 base::string16 address = line1_; | |
| 64 if (!line2_.empty()) | |
| 65 address += ASCIIToUTF16("\n") + line2_; | |
| 66 return address; | |
| 67 } | |
| 68 | |
| 69 // TODO(isherman): Add support for these field types in support of i18n. | |
| 70 case ADDRESS_HOME_SORTING_CODE: | |
| 71 case ADDRESS_HOME_DEPENDENT_LOCALITY: | |
| 72 return base::string16(); | |
| 73 | |
| 61 default: | 74 default: | 
| 75 NOTREACHED(); | |
| 62 return base::string16(); | 76 return base::string16(); | 
| 63 } | 77 } | 
| 64 } | 78 } | 
| 65 | 79 | 
| 66 void Address::SetRawInfo(ServerFieldType type, const base::string16& value) { | 80 void Address::SetRawInfo(ServerFieldType type, const base::string16& value) { | 
| 67 DCHECK_EQ(ADDRESS_HOME, AutofillType(type).group()); | 81 DCHECK_EQ(ADDRESS_HOME, AutofillType(type).group()); | 
| 68 switch (type) { | 82 switch (type) { | 
| 69 case ADDRESS_HOME_LINE1: | 83 case ADDRESS_HOME_LINE1: | 
| 70 line1_ = value; | 84 line1_ = value; | 
| 71 break; | 85 break; | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 85 case ADDRESS_HOME_COUNTRY: | 99 case ADDRESS_HOME_COUNTRY: | 
| 86 DCHECK(value.empty() || | 100 DCHECK(value.empty() || | 
| 87 (value.length() == 2u && IsStringASCII(value))); | 101 (value.length() == 2u && IsStringASCII(value))); | 
| 88 country_code_ = UTF16ToASCII(value); | 102 country_code_ = UTF16ToASCII(value); | 
| 89 break; | 103 break; | 
| 90 | 104 | 
| 91 case ADDRESS_HOME_ZIP: | 105 case ADDRESS_HOME_ZIP: | 
| 92 zip_code_ = value; | 106 zip_code_ = value; | 
| 93 break; | 107 break; | 
| 94 | 108 | 
| 109 case ADDRESS_HOME_STREET_ADDRESS: { | |
| 110 // Clear any stale values, which might or might not get overwritten below. | |
| 111 line1_.clear(); | |
| 112 line2_.clear(); | |
| 113 | |
| 114 std::vector<base::string16> lines; | |
| 115 base::SplitString(value, char16('\n'), &lines); | |
| 116 if (lines.size() > 0) | |
| 117 line1_ = lines[0]; | |
| 118 if (lines.size() > 1) | |
| 119 line2_ = lines[1]; | |
| 120 | |
| 121 // TODO(isherman): Add support for additional address lines. | |
| 122 break; | |
| 123 } | |
| 124 | |
| 125 // TODO(isherman): Add support for these field types in support of i18n. | |
| 126 case ADDRESS_HOME_SORTING_CODE: | |
| 127 case ADDRESS_HOME_DEPENDENT_LOCALITY: | |
| 128 break; | |
| 129 | |
| 95 default: | 130 default: | 
| 96 NOTREACHED(); | 131 NOTREACHED(); | 
| 97 } | 132 } | 
| 98 } | 133 } | 
| 99 | 134 | 
| 100 base::string16 Address::GetInfo(const AutofillType& type, | 135 base::string16 Address::GetInfo(const AutofillType& type, | 
| 101 const std::string& app_locale) const { | 136 const std::string& app_locale) const { | 
| 102 if (type.html_type() == HTML_TYPE_COUNTRY_CODE) { | 137 if (type.html_type() == HTML_TYPE_COUNTRY_CODE) | 
| 103 return ASCIIToUTF16(country_code_); | 138 return ASCIIToUTF16(country_code_); | 
| 104 } else if (type.html_type() == HTML_TYPE_STREET_ADDRESS) { | 139 | 
| 140 ServerFieldType storable_type = type.GetStorableType(); | |
| 141 if (storable_type == ADDRESS_HOME_COUNTRY && !country_code_.empty()) | |
| 142 return AutofillCountry(country_code_, app_locale).name(); | |
| 143 | |
| 144 if (storable_type == ADDRESS_HOME_STREET_ADDRESS) { | |
| 145 // TODO(isherman): Remove this special-case. | |
| 105 base::string16 address = line1_; | 146 base::string16 address = line1_; | 
| 106 if (!line2_.empty()) | 147 if (!line2_.empty()) | 
| 107 address += ASCIIToUTF16(", ") + line2_; | 148 address += ASCIIToUTF16(", ") + line2_; | 
| 108 return address; | 149 return address; | 
| 109 } | 150 } | 
| 110 | 151 | 
| 111 ServerFieldType storable_type = type.GetStorableType(); | |
| 112 if (storable_type == ADDRESS_HOME_COUNTRY && !country_code_.empty()) | |
| 113 return AutofillCountry(country_code_, app_locale).name(); | |
| 114 | |
| 115 return GetRawInfo(storable_type); | 152 return GetRawInfo(storable_type); | 
| 116 } | 153 } | 
| 117 | 154 | 
| 118 bool Address::SetInfo(const AutofillType& type, | 155 bool Address::SetInfo(const AutofillType& type, | 
| 119 const base::string16& value, | 156 const base::string16& value, | 
| 120 const std::string& app_locale) { | 157 const std::string& app_locale) { | 
| 121 if (type.html_type() == HTML_TYPE_COUNTRY_CODE) { | 158 if (type.html_type() == HTML_TYPE_COUNTRY_CODE) { | 
| 122 if (!value.empty() && (value.size() != 2u || !IsStringASCII(value))) { | 159 if (!value.empty() && (value.size() != 2u || !IsStringASCII(value))) { | 
| 123 country_code_ = std::string(); | 160 country_code_ = std::string(); | 
| 124 return false; | 161 return false; | 
| 125 } | 162 } | 
| 126 | 163 | 
| 127 country_code_ = StringToUpperASCII(UTF16ToASCII(value)); | 164 country_code_ = StringToUpperASCII(UTF16ToASCII(value)); | 
| 128 return true; | 165 return true; | 
| 129 } else if (type.html_type() == HTML_TYPE_STREET_ADDRESS) { | |
| 130 // Don't attempt to parse the address into lines, since this is potentially | |
| 131 // a user-entered address in the user's own format, so the code would have | |
| 132 // to rely on iffy heuristics at best. Instead, just give up when importing | |
| 133 // addresses like this. | |
| 134 line1_ = line2_ = base::string16(); | |
| 135 return false; | |
| 136 } | 166 } | 
| 137 | 167 | 
| 138 ServerFieldType storable_type = type.GetStorableType(); | 168 ServerFieldType storable_type = type.GetStorableType(); | 
| 139 if (storable_type == ADDRESS_HOME_COUNTRY && !value.empty()) { | 169 if (storable_type == ADDRESS_HOME_COUNTRY && !value.empty()) { | 
| 140 country_code_ = AutofillCountry::GetCountryCode(value, app_locale); | 170 country_code_ = AutofillCountry::GetCountryCode(value, app_locale); | 
| 141 return !country_code_.empty(); | 171 return !country_code_.empty(); | 
| 142 } | 172 } | 
| 143 | 173 | 
| 174 // If the address doesn't have any newlines, don't attempt to parse it into | |
| 175 // lines, since this is potentially a user-entered address in the user's own | |
| 176 // format, so the code would have to rely on iffy heuristics at best. | |
| 177 // Instead, just give up when importing addresses like this. | |
| 178 if (storable_type == ADDRESS_HOME_STREET_ADDRESS && !value.empty() && | |
| 179 value.find(char16('\n')) == base::string16::npos) { | |
| 
 
Evan Stade
2013/10/16 23:18:33
is it possible to put this check at a layer which
 
Ilya Sherman
2013/10/17 01:12:35
I thought about it and couldn't come up with a goo
 
Evan Stade
2013/10/17 02:27:30
would it be too much of a hack or not work if you
 
Ilya Sherman
2013/10/17 02:53:26
Leading and trailing whitespace is trimmed off by
 
 | |
| 180 line1_ = line2_ = base::string16(); | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 144 SetRawInfo(storable_type, value); | 184 SetRawInfo(storable_type, value); | 
| 145 return true; | 185 return true; | 
| 146 } | 186 } | 
| 147 | 187 | 
| 148 void Address::GetMatchingTypes(const base::string16& text, | 188 void Address::GetMatchingTypes(const base::string16& text, | 
| 149 const std::string& app_locale, | 189 const std::string& app_locale, | 
| 150 ServerFieldTypeSet* matching_types) const { | 190 ServerFieldTypeSet* matching_types) const { | 
| 151 FormGroup::GetMatchingTypes(text, app_locale, matching_types); | 191 FormGroup::GetMatchingTypes(text, app_locale, matching_types); | 
| 152 | 192 | 
| 153 // Check to see if the |text| canonicalized as a country name is a match. | 193 // Check to see if the |text| canonicalized as a country name is a match. | 
| 154 std::string country_code = AutofillCountry::GetCountryCode(text, app_locale); | 194 std::string country_code = AutofillCountry::GetCountryCode(text, app_locale); | 
| 155 if (!country_code.empty() && country_code_ == country_code) | 195 if (!country_code.empty() && country_code_ == country_code) | 
| 156 matching_types->insert(ADDRESS_HOME_COUNTRY); | 196 matching_types->insert(ADDRESS_HOME_COUNTRY); | 
| 157 } | 197 } | 
| 158 | 198 | 
| 159 void Address::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { | 199 void Address::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { | 
| 160 supported_types->insert(ADDRESS_HOME_LINE1); | 200 supported_types->insert(ADDRESS_HOME_LINE1); | 
| 161 supported_types->insert(ADDRESS_HOME_LINE2); | 201 supported_types->insert(ADDRESS_HOME_LINE2); | 
| 162 supported_types->insert(ADDRESS_HOME_CITY); | 202 supported_types->insert(ADDRESS_HOME_CITY); | 
| 163 supported_types->insert(ADDRESS_HOME_STATE); | 203 supported_types->insert(ADDRESS_HOME_STATE); | 
| 164 supported_types->insert(ADDRESS_HOME_ZIP); | 204 supported_types->insert(ADDRESS_HOME_ZIP); | 
| 165 supported_types->insert(ADDRESS_HOME_COUNTRY); | 205 supported_types->insert(ADDRESS_HOME_COUNTRY); | 
| 166 } | 206 } | 
| 167 | 207 | 
| 168 } // namespace autofill | 208 } // namespace autofill | 
| OLD | NEW |