| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/phone_number.h" | 5 #include "chrome/browser/autofill/phone_number.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 87 |
| 88 void PhoneNumber::SetInfo(AutofillFieldType type, const string16& value) { | 88 void PhoneNumber::SetInfo(AutofillFieldType type, const string16& value) { |
| 89 if (type != PHONE_HOME_CITY_AND_NUMBER && | 89 if (type != PHONE_HOME_CITY_AND_NUMBER && |
| 90 type != PHONE_HOME_WHOLE_NUMBER) { | 90 type != PHONE_HOME_WHOLE_NUMBER) { |
| 91 // Only full phone numbers should be set directly. The remaining field | 91 // Only full phone numbers should be set directly. The remaining field |
| 92 // field types are read-only. | 92 // field types are read-only. |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 | 95 |
| 96 number_ = value; | 96 number_ = value; |
| 97 cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale()); | 97 cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, GetLocale()); |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Normalize phones if |type| is a whole number: | 100 // Normalize phones if |type| is a whole number: |
| 101 // (650)2345678 -> 6502345678 | 101 // (650)2345678 -> 6502345678 |
| 102 // 1-800-FLOWERS -> 18003569377 | 102 // 1-800-FLOWERS -> 18003569377 |
| 103 // If the phone cannot be normalized, returns the stored value verbatim. | 103 // If the phone cannot be normalized, returns the stored value verbatim. |
| 104 string16 PhoneNumber::GetCanonicalizedInfo(AutofillFieldType type) const { | 104 string16 PhoneNumber::GetCanonicalizedInfo(AutofillFieldType type) const { |
| 105 string16 phone = GetInfo(type); | 105 string16 phone = GetInfo(type); |
| 106 if (type != PHONE_HOME_WHOLE_NUMBER) | 106 if (type != PHONE_HOME_WHOLE_NUMBER) |
| 107 return phone; | 107 return phone; |
| 108 | 108 |
| 109 string16 normalized_phone = autofill_i18n::NormalizePhoneNumber(phone, | 109 string16 normalized_phone = autofill_i18n::NormalizePhoneNumber(phone, |
| 110 locale()); | 110 GetLocale()); |
| 111 if (!normalized_phone.empty()) | 111 if (!normalized_phone.empty()) |
| 112 return normalized_phone; | 112 return normalized_phone; |
| 113 | 113 |
| 114 return phone; | 114 return phone; |
| 115 } | 115 } |
| 116 | 116 |
| 117 bool PhoneNumber::SetCanonicalizedInfo(AutofillFieldType type, | 117 bool PhoneNumber::SetCanonicalizedInfo(AutofillFieldType type, |
| 118 const string16& value) { | 118 const string16& value) { |
| 119 string16 number = value; | 119 string16 number = value; |
| 120 StripPunctuation(&number); | 120 StripPunctuation(&number); |
| 121 SetInfo(type, number); | 121 SetInfo(type, number); |
| 122 | 122 |
| 123 return NormalizePhone(); | 123 return NormalizePhone(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void PhoneNumber::GetMatchingTypes(const string16& text, | 126 void PhoneNumber::GetMatchingTypes(const string16& text, |
| 127 FieldTypeSet* matching_types) const { | 127 FieldTypeSet* matching_types) const { |
| 128 string16 stripped_text = text; | 128 string16 stripped_text = text; |
| 129 StripPunctuation(&stripped_text); | 129 StripPunctuation(&stripped_text); |
| 130 FormGroup::GetMatchingTypes(stripped_text, matching_types); | 130 FormGroup::GetMatchingTypes(stripped_text, matching_types); |
| 131 | 131 |
| 132 // For US numbers, also compare to the three-digit prefix and the four-digit | 132 // For US numbers, also compare to the three-digit prefix and the four-digit |
| 133 // suffix, since web sites often split numbers into these two fields. | 133 // suffix, since web sites often split numbers into these two fields. |
| 134 string16 number = GetCanonicalizedInfo(PHONE_HOME_NUMBER); | 134 string16 number = GetCanonicalizedInfo(PHONE_HOME_NUMBER); |
| 135 if (locale() == "US" && number.size() == (kPrefixLength + kSuffixLength)) { | 135 if (GetLocale() == "US" && number.size() == (kPrefixLength + kSuffixLength)) { |
| 136 string16 prefix = number.substr(kPrefixOffset, kPrefixLength); | 136 string16 prefix = number.substr(kPrefixOffset, kPrefixLength); |
| 137 string16 suffix = number.substr(kSuffixOffset, kSuffixLength); | 137 string16 suffix = number.substr(kSuffixOffset, kSuffixLength); |
| 138 if (text == prefix || text == suffix) | 138 if (text == prefix || text == suffix) |
| 139 matching_types->insert(PHONE_HOME_NUMBER); | 139 matching_types->insert(PHONE_HOME_NUMBER); |
| 140 } | 140 } |
| 141 | 141 |
| 142 string16 whole_number = GetCanonicalizedInfo(PHONE_HOME_WHOLE_NUMBER); | 142 string16 whole_number = GetCanonicalizedInfo(PHONE_HOME_WHOLE_NUMBER); |
| 143 if (!whole_number.empty() && | 143 if (!whole_number.empty() && |
| 144 autofill_i18n::NormalizePhoneNumber(text, locale()) == whole_number) { | 144 autofill_i18n::NormalizePhoneNumber(text, GetLocale()) == whole_number) { |
| 145 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); | 145 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 | 148 |
| 149 bool PhoneNumber::NormalizePhone() { | 149 bool PhoneNumber::NormalizePhone() { |
| 150 // Empty number does not need normalization. | 150 // Empty number does not need normalization. |
| 151 if (number_.empty()) | 151 if (number_.empty()) |
| 152 return true; | 152 return true; |
| 153 | 153 |
| 154 UpdateCacheIfNeeded(); | 154 UpdateCacheIfNeeded(); |
| 155 number_ = cached_parsed_phone_.GetWholeNumber(); | 155 number_ = cached_parsed_phone_.GetWholeNumber(); |
| 156 return !number_.empty(); | 156 return !number_.empty(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 std::string PhoneNumber::locale() const { | 159 std::string PhoneNumber::GetLocale() const { |
| 160 if (!profile_) { | 160 if (!profile_) { |
| 161 NOTREACHED(); | 161 NOTREACHED(); |
| 162 return "US"; | 162 return "US"; |
| 163 } | 163 } |
| 164 | 164 |
| 165 return profile_->CountryCode(); | 165 return profile_->CountryCode(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void PhoneNumber::UpdateCacheIfNeeded() const { | 168 void PhoneNumber::UpdateCacheIfNeeded() const { |
| 169 if (!number_.empty() && cached_parsed_phone_.GetLocale() != locale()) | 169 std::string locale = GetLocale(); |
| 170 cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale()); | 170 if (!number_.empty() && cached_parsed_phone_.GetLocale() != locale) |
| 171 cached_parsed_phone_ = autofill_i18n::PhoneObject(number_, locale); |
| 171 } | 172 } |
| 172 | 173 |
| 173 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { | 174 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
| 174 } | 175 } |
| 175 | 176 |
| 176 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { | 177 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { |
| 177 } | 178 } |
| 178 | 179 |
| 179 bool PhoneNumber::PhoneCombineHelper::SetInfo(AutofillFieldType field_type, | 180 bool PhoneNumber::PhoneCombineHelper::SetInfo(AutofillFieldType field_type, |
| 180 const string16& value) { | 181 const string16& value) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 country_, city_, phone_, | 218 country_, city_, phone_, |
| 218 locale, | 219 locale, |
| 219 (country_.empty() ? | 220 (country_.empty() ? |
| 220 autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), | 221 autofill_i18n::NATIONAL : autofill_i18n::INTERNATIONAL), |
| 221 value); | 222 value); |
| 222 } | 223 } |
| 223 | 224 |
| 224 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { | 225 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
| 225 return phone_.empty() && whole_number_.empty(); | 226 return phone_.empty() && whole_number_.empty(); |
| 226 } | 227 } |
| OLD | NEW |