Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autofill/address.h" | 5 #include "chrome/browser/autofill/address.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | |
|
James Hawkins
2011/02/16 19:49:01
Where is it used?
Ilya Sherman
2011/02/17 23:09:11
Removed. I wish cpplint.py would tell me about th
| |
| 10 #include "chrome/browser/autofill/autofill_country.h" | |
| 9 #include "chrome/browser/autofill/autofill_type.h" | 11 #include "chrome/browser/autofill/autofill_type.h" |
| 10 #include "chrome/browser/autofill/field_types.h" | 12 #include "chrome/browser/autofill/field_types.h" |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0}; | 16 const char16 kAddressSplitChars[] = {'-', ',', '#', '.', ' ', 0}; |
| 15 | 17 |
| 16 const AutoFillType::FieldTypeSubGroup kAutoFillAddressTypes[] = { | 18 const AutoFillType::FieldTypeSubGroup kAutoFillAddressTypes[] = { |
| 17 AutoFillType::ADDRESS_LINE1, | 19 AutoFillType::ADDRESS_LINE1, |
| 18 AutoFillType::ADDRESS_LINE2, | 20 AutoFillType::ADDRESS_LINE2, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 | 78 |
| 77 if (!city().empty()) | 79 if (!city().empty()) |
| 78 available_types->insert(GetCityType()); | 80 available_types->insert(GetCityType()); |
| 79 | 81 |
| 80 if (!state().empty()) | 82 if (!state().empty()) |
| 81 available_types->insert(GetStateType()); | 83 available_types->insert(GetStateType()); |
| 82 | 84 |
| 83 if (!zip_code().empty()) | 85 if (!zip_code().empty()) |
| 84 available_types->insert(GetZipCodeType()); | 86 available_types->insert(GetZipCodeType()); |
| 85 | 87 |
| 86 if (!country().empty()) | 88 if (!country_code().empty()) |
| 87 available_types->insert(GetCountryType()); | 89 available_types->insert(GetCountryType()); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void Address::FindInfoMatches(const AutoFillType& type, | 92 void Address::FindInfoMatches(const AutoFillType& type, |
| 91 const string16& info, | 93 const string16& info, |
| 92 std::vector<string16>* matched_text) const { | 94 std::vector<string16>* matched_text) const { |
| 93 DCHECK(matched_text); | 95 DCHECK(matched_text); |
| 94 | 96 |
| 95 string16 match; | 97 string16 match; |
| 96 if (type.field_type() == UNKNOWN_TYPE) { | 98 if (type.field_type() == UNKNOWN_TYPE) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 118 if (field_type == GetCityType()) | 120 if (field_type == GetCityType()) |
| 119 return city(); | 121 return city(); |
| 120 | 122 |
| 121 if (field_type == GetStateType()) | 123 if (field_type == GetStateType()) |
| 122 return state(); | 124 return state(); |
| 123 | 125 |
| 124 if (field_type == GetZipCodeType()) | 126 if (field_type == GetZipCodeType()) |
| 125 return zip_code(); | 127 return zip_code(); |
| 126 | 128 |
| 127 if (field_type == GetCountryType()) | 129 if (field_type == GetCountryType()) |
| 128 return country(); | 130 return Country(); |
| 129 | 131 |
| 130 return string16(); | 132 return string16(); |
| 131 } | 133 } |
| 132 | 134 |
| 133 void Address::SetInfo(const AutoFillType& type, const string16& value) { | 135 void Address::SetInfo(const AutoFillType& type, const string16& value) { |
| 134 FieldTypeSubGroup subgroup = type.subgroup(); | 136 FieldTypeSubGroup subgroup = type.subgroup(); |
| 135 if (subgroup == AutoFillType::ADDRESS_LINE1) | 137 if (subgroup == AutoFillType::ADDRESS_LINE1) |
| 136 set_line1(value); | 138 set_line1(value); |
| 137 else if (subgroup == AutoFillType::ADDRESS_LINE2) | 139 else if (subgroup == AutoFillType::ADDRESS_LINE2) |
| 138 set_line2(value); | 140 set_line2(value); |
| 139 else if (subgroup == AutoFillType::ADDRESS_APT_NUM) | 141 else if (subgroup == AutoFillType::ADDRESS_APT_NUM) |
| 140 set_apt_num(value); | 142 set_apt_num(value); |
| 141 else if (subgroup == AutoFillType::ADDRESS_CITY) | 143 else if (subgroup == AutoFillType::ADDRESS_CITY) |
| 142 set_city(value); | 144 set_city(value); |
| 143 else if (subgroup == AutoFillType::ADDRESS_STATE) | 145 else if (subgroup == AutoFillType::ADDRESS_STATE) |
| 144 set_state(value); | 146 set_state(value); |
| 145 else if (subgroup == AutoFillType::ADDRESS_COUNTRY) | 147 else if (subgroup == AutoFillType::ADDRESS_COUNTRY) |
| 146 set_country(value); | 148 SetCountry(value); |
| 147 else if (subgroup == AutoFillType::ADDRESS_ZIP) | 149 else if (subgroup == AutoFillType::ADDRESS_ZIP) |
| 148 set_zip_code(value); | 150 set_zip_code(value); |
| 149 else | 151 else |
| 150 NOTREACHED(); | 152 NOTREACHED(); |
| 151 } | 153 } |
| 152 | 154 |
| 153 void Address::Clear() { | 155 void Address::Clear() { |
| 154 line1_tokens_.clear(); | 156 line1_tokens_.clear(); |
| 155 line1_.clear(); | 157 line1_.clear(); |
| 156 line2_tokens_.clear(); | 158 line2_tokens_.clear(); |
| 157 line2_.clear(); | 159 line2_.clear(); |
| 158 apt_num_.clear(); | 160 apt_num_.clear(); |
| 159 city_.clear(); | 161 city_.clear(); |
| 160 state_.clear(); | 162 state_.clear(); |
| 161 country_.clear(); | 163 country_code_.clear(); |
| 162 zip_code_.clear(); | 164 zip_code_.clear(); |
| 163 } | 165 } |
| 164 | 166 |
| 165 void Address::Clone(const Address& address) { | 167 void Address::Clone(const Address& address) { |
| 166 set_line1(address.line1()); | 168 line1_tokens_ = address.line1_tokens_; |
| 167 set_line2(address.line2()); | 169 line2_tokens_ = address.line2_tokens_; |
| 168 set_apt_num(address.apt_num()); | 170 line1_ = address.line1_; |
| 169 set_city(address.city()); | 171 line2_ = address.line2_; |
| 170 set_state(address.state()); | 172 apt_num_ = address.apt_num_; |
| 171 set_country(address.country()); | 173 city_ = address.city_; |
| 172 set_zip_code(address.zip_code()); | 174 state_ = address.state_; |
| 175 country_code_ = address.country_code_; | |
| 176 zip_code_ = address.zip_code_; | |
| 173 } | 177 } |
| 174 | 178 |
| 175 Address::Address(const Address& address) | 179 Address::Address(const Address& address) |
| 176 : FormGroup(), | 180 : FormGroup(), |
| 177 line1_tokens_(address.line1_tokens_), | 181 line1_tokens_(address.line1_tokens_), |
| 178 line2_tokens_(address.line2_tokens_), | 182 line2_tokens_(address.line2_tokens_), |
| 179 line1_(address.line1_), | 183 line1_(address.line1_), |
| 180 line2_(address.line2_), | 184 line2_(address.line2_), |
| 181 apt_num_(address.apt_num_), | 185 apt_num_(address.apt_num_), |
| 182 city_(address.city_), | 186 city_(address.city_), |
| 183 state_(address.state_), | 187 state_(address.state_), |
| 184 country_(address.country_), | 188 country_code_(address.country_code_), |
| 185 zip_code_(address.zip_code_) { | 189 zip_code_(address.zip_code_) { |
| 186 } | 190 } |
| 187 | 191 |
| 192 string16 Address::Country() const { | |
| 193 if (country_code().empty()) | |
| 194 return string16(); | |
| 195 | |
| 196 return AutoFillCountry(country_code()).name(); | |
| 197 } | |
| 198 | |
| 188 void Address::set_line1(const string16& line1) { | 199 void Address::set_line1(const string16& line1) { |
| 189 line1_ = line1; | 200 line1_ = line1; |
| 190 line1_tokens_.clear(); | 201 line1_tokens_.clear(); |
| 191 Tokenize(line1, kAddressSplitChars, &line1_tokens_); | 202 Tokenize(line1, kAddressSplitChars, &line1_tokens_); |
| 192 LineTokens::iterator iter; | 203 LineTokens::iterator iter; |
| 193 for (iter = line1_tokens_.begin(); iter != line1_tokens_.end(); ++iter) | 204 for (iter = line1_tokens_.begin(); iter != line1_tokens_.end(); ++iter) |
| 194 *iter = StringToLowerASCII(*iter); | 205 *iter = StringToLowerASCII(*iter); |
| 195 } | 206 } |
| 196 | 207 |
| 197 void Address::set_line2(const string16& line2) { | 208 void Address::set_line2(const string16& line2) { |
| 198 line2_ = line2; | 209 line2_ = line2; |
| 199 line2_tokens_.clear(); | 210 line2_tokens_.clear(); |
| 200 Tokenize(line2, kAddressSplitChars, &line2_tokens_); | 211 Tokenize(line2, kAddressSplitChars, &line2_tokens_); |
| 201 LineTokens::iterator iter; | 212 LineTokens::iterator iter; |
| 202 for (iter = line2_tokens_.begin(); iter != line2_tokens_.end(); ++iter) | 213 for (iter = line2_tokens_.begin(); iter != line2_tokens_.end(); ++iter) |
| 203 *iter = StringToLowerASCII(*iter); | 214 *iter = StringToLowerASCII(*iter); |
| 204 } | 215 } |
| 205 | 216 |
| 217 void Address::SetCountry(const string16& country) { | |
| 218 country_code_ = AutoFillCountry::GetCountryCode(country); | |
| 219 } | |
| 220 | |
| 206 bool Address::IsLine1(const string16& text) const { | 221 bool Address::IsLine1(const string16& text) const { |
| 207 return IsLineMatch(text, line1_tokens_); | 222 return IsLineMatch(text, line1_tokens_); |
| 208 } | 223 } |
| 209 | 224 |
| 210 bool Address::IsLine2(const string16& text) const { | 225 bool Address::IsLine2(const string16& text) const { |
| 211 return IsLineMatch(text, line2_tokens_); | 226 return IsLineMatch(text, line2_tokens_); |
| 212 } | 227 } |
| 213 | 228 |
| 214 bool Address::IsAptNum(const string16& text) const { | 229 bool Address::IsAptNum(const string16& text) const { |
| 215 return (StringToLowerASCII(apt_num_) == StringToLowerASCII(text)); | 230 return (StringToLowerASCII(apt_num_) == StringToLowerASCII(text)); |
| 216 } | 231 } |
| 217 | 232 |
| 218 bool Address::IsCity(const string16& text) const { | 233 bool Address::IsCity(const string16& text) const { |
| 219 return (StringToLowerASCII(city_) == StringToLowerASCII(text)); | 234 return (StringToLowerASCII(city_) == StringToLowerASCII(text)); |
| 220 } | 235 } |
| 221 | 236 |
| 222 bool Address::IsState(const string16& text) const { | 237 bool Address::IsState(const string16& text) const { |
| 223 return (StringToLowerASCII(state_) == StringToLowerASCII(text)); | 238 return (StringToLowerASCII(state_) == StringToLowerASCII(text)); |
| 224 } | 239 } |
| 225 | 240 |
| 226 bool Address::IsCountry(const string16& text) const { | 241 bool Address::IsCountry(const string16& text) const { |
| 227 return (StringToLowerASCII(country_) == StringToLowerASCII(text)); | 242 std::string country_code = AutoFillCountry::GetCountryCode(text); |
| 243 return (!country_code.empty() && country_code_ == country_code); | |
| 228 } | 244 } |
| 229 | 245 |
| 230 bool Address::IsZipCode(const string16& text) const { | 246 bool Address::IsZipCode(const string16& text) const { |
| 231 return zip_code_ == text; | 247 return zip_code_ == text; |
| 232 } | 248 } |
| 233 | 249 |
| 234 bool Address::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup, | 250 bool Address::FindInfoMatchesHelper(const FieldTypeSubGroup& subgroup, |
| 235 const string16& info, | 251 const string16& info, |
| 236 string16* match) const { | 252 string16* match) const { |
| 237 DCHECK(match); | 253 DCHECK(match); |
| 238 | 254 |
| 239 match->clear(); | 255 match->clear(); |
| 240 if (subgroup == AutoFillType::ADDRESS_LINE1 && | 256 if (subgroup == AutoFillType::ADDRESS_LINE1 && |
| 241 StartsWith(line1(), info, false)) { | 257 StartsWith(line1(), info, false)) { |
| 242 *match = line1(); | 258 *match = line1(); |
| 243 } else if (subgroup == AutoFillType::ADDRESS_LINE2 && | 259 } else if (subgroup == AutoFillType::ADDRESS_LINE2 && |
| 244 StartsWith(line2(), info, false)) { | 260 StartsWith(line2(), info, false)) { |
| 245 *match = line2(); | 261 *match = line2(); |
| 246 } else if (subgroup == AutoFillType::ADDRESS_APT_NUM && | 262 } else if (subgroup == AutoFillType::ADDRESS_APT_NUM && |
| 247 StartsWith(apt_num(), info, false)) { | 263 StartsWith(apt_num(), info, false)) { |
| 248 *match = apt_num(); | 264 *match = apt_num(); |
| 249 } else if (subgroup == AutoFillType::ADDRESS_CITY && | 265 } else if (subgroup == AutoFillType::ADDRESS_CITY && |
| 250 StartsWith(city(), info, false)) { | 266 StartsWith(city(), info, false)) { |
| 251 *match = city(); | 267 *match = city(); |
| 252 } else if (subgroup == AutoFillType::ADDRESS_STATE && | 268 } else if (subgroup == AutoFillType::ADDRESS_STATE && |
| 253 StartsWith(state(), info, false)) { | 269 StartsWith(state(), info, false)) { |
| 254 *match = state(); | 270 *match = state(); |
| 255 } else if (subgroup == AutoFillType::ADDRESS_COUNTRY && | 271 } else if (subgroup == AutoFillType::ADDRESS_COUNTRY && |
| 256 StartsWith(country(), info, false)) { | 272 StartsWith(Country(), info, false)) { |
| 257 *match = country(); | 273 *match = Country(); |
| 258 } else if (subgroup == AutoFillType::ADDRESS_ZIP && | 274 } else if (subgroup == AutoFillType::ADDRESS_ZIP && |
| 259 StartsWith(zip_code(), info, true)) { | 275 StartsWith(zip_code(), info, true)) { |
| 260 *match = zip_code(); | 276 *match = zip_code(); |
| 261 } | 277 } |
| 262 | 278 |
| 263 return !match->empty(); | 279 return !match->empty(); |
| 264 } | 280 } |
| 265 | 281 |
| 266 bool Address::IsLineMatch(const string16& text, | 282 bool Address::IsLineMatch(const string16& text, |
| 267 const LineTokens& line_tokens) const { | 283 const LineTokens& line_tokens) const { |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 292 bool Address::IsWordInLine(const string16& word, | 308 bool Address::IsWordInLine(const string16& word, |
| 293 const LineTokens& line_tokens) const { | 309 const LineTokens& line_tokens) const { |
| 294 LineTokens::const_iterator iter; | 310 LineTokens::const_iterator iter; |
| 295 for (iter = line_tokens.begin(); iter != line_tokens.end(); ++iter) { | 311 for (iter = line_tokens.begin(); iter != line_tokens.end(); ++iter) { |
| 296 if (StringToLowerASCII(word) == *iter) | 312 if (StringToLowerASCII(word) == *iter) |
| 297 return true; | 313 return true; |
| 298 } | 314 } |
| 299 | 315 |
| 300 return false; | 316 return false; |
| 301 } | 317 } |
| OLD | NEW |