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/phone_number.h" | 5 #include "components/autofill/core/browser/phone_number.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) { | 58 PhoneNumber& PhoneNumber::operator=(const PhoneNumber& number) { |
59 if (this == &number) | 59 if (this == &number) |
60 return *this; | 60 return *this; |
61 | 61 |
62 number_ = number.number_; | 62 number_ = number.number_; |
63 profile_ = number.profile_; | 63 profile_ = number.profile_; |
64 cached_parsed_phone_ = number.cached_parsed_phone_; | 64 cached_parsed_phone_ = number.cached_parsed_phone_; |
65 return *this; | 65 return *this; |
66 } | 66 } |
67 | 67 |
68 void PhoneNumber::GetSupportedTypes(FieldTypeSet* supported_types) const { | 68 void PhoneNumber::GetSupportedTypes(ServerFieldTypeSet* supported_types) const { |
69 supported_types->insert(PHONE_HOME_WHOLE_NUMBER); | 69 supported_types->insert(PHONE_HOME_WHOLE_NUMBER); |
70 supported_types->insert(PHONE_HOME_NUMBER); | 70 supported_types->insert(PHONE_HOME_NUMBER); |
71 supported_types->insert(PHONE_HOME_CITY_CODE); | 71 supported_types->insert(PHONE_HOME_CITY_CODE); |
72 supported_types->insert(PHONE_HOME_CITY_AND_NUMBER); | 72 supported_types->insert(PHONE_HOME_CITY_AND_NUMBER); |
73 supported_types->insert(PHONE_HOME_COUNTRY_CODE); | 73 supported_types->insert(PHONE_HOME_COUNTRY_CODE); |
74 } | 74 } |
75 | 75 |
76 base::string16 PhoneNumber::GetRawInfo(AutofillFieldType type) const { | 76 base::string16 PhoneNumber::GetRawInfo(ServerFieldType type) const { |
77 type = AutofillType::GetEquivalentFieldType(type); | 77 type = AutofillType::GetEquivalentFieldType(type); |
78 if (type == PHONE_HOME_WHOLE_NUMBER) | 78 if (type == PHONE_HOME_WHOLE_NUMBER) |
79 return number_; | 79 return number_; |
80 | 80 |
81 // Only the whole number is available as raw data. All of the other types are | 81 // Only the whole number is available as raw data. All of the other types are |
82 // parsed from this raw info, and parsing requires knowledge of the phone | 82 // parsed from this raw info, and parsing requires knowledge of the phone |
83 // number's region, which is only available via GetInfo(). | 83 // number's region, which is only available via GetInfo(). |
84 return base::string16(); | 84 return base::string16(); |
85 } | 85 } |
86 | 86 |
87 void PhoneNumber::SetRawInfo(AutofillFieldType type, | 87 void PhoneNumber::SetRawInfo(ServerFieldType type, |
88 const base::string16& value) { | 88 const base::string16& value) { |
89 type = AutofillType::GetEquivalentFieldType(type); | 89 type = AutofillType::GetEquivalentFieldType(type); |
90 if (type != PHONE_HOME_CITY_AND_NUMBER && | 90 if (type != PHONE_HOME_CITY_AND_NUMBER && |
91 type != PHONE_HOME_WHOLE_NUMBER) { | 91 type != PHONE_HOME_WHOLE_NUMBER) { |
92 // Only full phone numbers should be set directly. The remaining field | 92 // Only full phone numbers should be set directly. The remaining field |
93 // field types are read-only. | 93 // field types are read-only. |
94 return; | 94 return; |
95 } | 95 } |
96 | 96 |
97 number_ = value; | 97 number_ = value; |
98 | 98 |
99 // Invalidate the cached number. | 99 // Invalidate the cached number. |
100 cached_parsed_phone_ = i18n::PhoneObject(); | 100 cached_parsed_phone_ = i18n::PhoneObject(); |
101 } | 101 } |
102 | 102 |
103 // Normalize phones if |type| is a whole number: | 103 // Normalize phones if |type| is a whole number: |
104 // (650)2345678 -> 6502345678 | 104 // (650)2345678 -> 6502345678 |
105 // 1-800-FLOWERS -> 18003569377 | 105 // 1-800-FLOWERS -> 18003569377 |
106 // If the phone cannot be normalized, returns the stored value verbatim. | 106 // If the phone cannot be normalized, returns the stored value verbatim. |
107 base::string16 PhoneNumber::GetInfo(AutofillFieldType type, | 107 base::string16 PhoneNumber::GetInfo(const AutofillType& type, |
108 const std::string& app_locale) const { | 108 const std::string& app_locale) const { |
109 type = AutofillType::GetEquivalentFieldType(type); | 109 ServerFieldType server_type = |
| 110 AutofillType::GetEquivalentFieldType(type.server_type()); |
110 UpdateCacheIfNeeded(app_locale); | 111 UpdateCacheIfNeeded(app_locale); |
111 | 112 |
112 // Queries for whole numbers will return the non-normalized number if | 113 // Queries for whole numbers will return the non-normalized number if |
113 // normalization for the number fails. All other field types require | 114 // normalization for the number fails. All other field types require |
114 // normalization. | 115 // normalization. |
115 if (type != PHONE_HOME_WHOLE_NUMBER && !cached_parsed_phone_.IsValidNumber()) | 116 if (server_type != PHONE_HOME_WHOLE_NUMBER && |
| 117 !cached_parsed_phone_.IsValidNumber()) |
116 return base::string16(); | 118 return base::string16(); |
117 | 119 |
118 switch (type) { | 120 switch (server_type) { |
119 case PHONE_HOME_WHOLE_NUMBER: | 121 case PHONE_HOME_WHOLE_NUMBER: |
120 return cached_parsed_phone_.GetWholeNumber(); | 122 return cached_parsed_phone_.GetWholeNumber(); |
121 | 123 |
122 case PHONE_HOME_NUMBER: | 124 case PHONE_HOME_NUMBER: |
123 return cached_parsed_phone_.number(); | 125 return cached_parsed_phone_.number(); |
124 | 126 |
125 case PHONE_HOME_CITY_CODE: | 127 case PHONE_HOME_CITY_CODE: |
126 return cached_parsed_phone_.city_code(); | 128 return cached_parsed_phone_.city_code(); |
127 | 129 |
128 case PHONE_HOME_COUNTRY_CODE: | 130 case PHONE_HOME_COUNTRY_CODE: |
129 return cached_parsed_phone_.country_code(); | 131 return cached_parsed_phone_.country_code(); |
130 | 132 |
131 case PHONE_HOME_CITY_AND_NUMBER: | 133 case PHONE_HOME_CITY_AND_NUMBER: |
132 return | 134 return |
133 cached_parsed_phone_.city_code() + cached_parsed_phone_.number(); | 135 cached_parsed_phone_.city_code() + cached_parsed_phone_.number(); |
134 | 136 |
135 default: | 137 default: |
136 NOTREACHED(); | 138 NOTREACHED(); |
137 return base::string16(); | 139 return base::string16(); |
138 } | 140 } |
139 } | 141 } |
140 | 142 |
141 bool PhoneNumber::SetInfo(AutofillFieldType type, | 143 bool PhoneNumber::SetInfo(const AutofillType& type, |
142 const base::string16& value, | 144 const base::string16& value, |
143 const std::string& app_locale) { | 145 const std::string& app_locale) { |
144 type = AutofillType::GetEquivalentFieldType(type); | 146 ServerFieldType server_type = |
145 SetRawInfo(type, value); | 147 AutofillType::GetEquivalentFieldType(type.server_type()); |
| 148 SetRawInfo(server_type, value); |
146 | 149 |
147 if (number_.empty()) | 150 if (number_.empty()) |
148 return true; | 151 return true; |
149 | 152 |
150 // Store a formatted (i.e., pretty printed) version of the number. | 153 // Store a formatted (i.e., pretty printed) version of the number. |
151 UpdateCacheIfNeeded(app_locale); | 154 UpdateCacheIfNeeded(app_locale); |
152 number_ = cached_parsed_phone_.GetFormattedNumber(); | 155 number_ = cached_parsed_phone_.GetFormattedNumber(); |
153 return !number_.empty(); | 156 return !number_.empty(); |
154 } | 157 } |
155 | 158 |
156 void PhoneNumber::GetMatchingTypes(const base::string16& text, | 159 void PhoneNumber::GetMatchingTypes(const base::string16& text, |
157 const std::string& app_locale, | 160 const std::string& app_locale, |
158 FieldTypeSet* matching_types) const { | 161 ServerFieldTypeSet* matching_types) const { |
159 base::string16 stripped_text = text; | 162 base::string16 stripped_text = text; |
160 StripPunctuation(&stripped_text); | 163 StripPunctuation(&stripped_text); |
161 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); | 164 FormGroup::GetMatchingTypes(stripped_text, app_locale, matching_types); |
162 | 165 |
163 // For US numbers, also compare to the three-digit prefix and the four-digit | 166 // For US numbers, also compare to the three-digit prefix and the four-digit |
164 // suffix, since web sites often split numbers into these two fields. | 167 // suffix, since web sites often split numbers into these two fields. |
165 base::string16 number = GetInfo(PHONE_HOME_NUMBER, app_locale); | 168 base::string16 number = GetInfo(AutofillType(PHONE_HOME_NUMBER), app_locale); |
166 if (GetRegion(*profile_, app_locale) == "US" && | 169 if (GetRegion(*profile_, app_locale) == "US" && |
167 number.size() == (kPrefixLength + kSuffixLength)) { | 170 number.size() == (kPrefixLength + kSuffixLength)) { |
168 base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength); | 171 base::string16 prefix = number.substr(kPrefixOffset, kPrefixLength); |
169 base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength); | 172 base::string16 suffix = number.substr(kSuffixOffset, kSuffixLength); |
170 if (text == prefix || text == suffix) | 173 if (text == prefix || text == suffix) |
171 matching_types->insert(PHONE_HOME_NUMBER); | 174 matching_types->insert(PHONE_HOME_NUMBER); |
172 } | 175 } |
173 | 176 |
174 base::string16 whole_number = GetInfo(PHONE_HOME_WHOLE_NUMBER, app_locale); | 177 base::string16 whole_number = |
| 178 GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), app_locale); |
175 if (!whole_number.empty()) { | 179 if (!whole_number.empty()) { |
176 base::string16 normalized_number = | 180 base::string16 normalized_number = |
177 i18n::NormalizePhoneNumber(text, GetRegion(*profile_, app_locale)); | 181 i18n::NormalizePhoneNumber(text, GetRegion(*profile_, app_locale)); |
178 if (normalized_number == whole_number) | 182 if (normalized_number == whole_number) |
179 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); | 183 matching_types->insert(PHONE_HOME_WHOLE_NUMBER); |
180 } | 184 } |
181 } | 185 } |
182 | 186 |
183 void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { | 187 void PhoneNumber::UpdateCacheIfNeeded(const std::string& app_locale) const { |
184 std::string region = GetRegion(*profile_, app_locale); | 188 std::string region = GetRegion(*profile_, app_locale); |
185 if (!number_.empty() && cached_parsed_phone_.region() != region) | 189 if (!number_.empty() && cached_parsed_phone_.region() != region) |
186 cached_parsed_phone_ = i18n::PhoneObject(number_, region); | 190 cached_parsed_phone_ = i18n::PhoneObject(number_, region); |
187 } | 191 } |
188 | 192 |
189 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { | 193 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
190 } | 194 } |
191 | 195 |
192 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { | 196 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { |
193 } | 197 } |
194 | 198 |
195 bool PhoneNumber::PhoneCombineHelper::SetInfo(AutofillFieldType field_type, | 199 bool PhoneNumber::PhoneCombineHelper::SetInfo(const AutofillType& field_type, |
196 const base::string16& value) { | 200 const base::string16& value) { |
197 field_type = AutofillType::GetEquivalentFieldType(field_type); | 201 ServerFieldType server_field_type = |
198 if (field_type == PHONE_HOME_COUNTRY_CODE) { | 202 AutofillType::GetEquivalentFieldType(field_type.server_type()); |
| 203 if (server_field_type == PHONE_HOME_COUNTRY_CODE) { |
199 country_ = value; | 204 country_ = value; |
200 return true; | 205 return true; |
201 } | 206 } |
202 | 207 |
203 if (field_type == PHONE_HOME_CITY_CODE) { | 208 if (server_field_type == PHONE_HOME_CITY_CODE) { |
204 city_ = value; | 209 city_ = value; |
205 return true; | 210 return true; |
206 } | 211 } |
207 | 212 |
208 if (field_type == PHONE_HOME_CITY_AND_NUMBER) { | 213 if (server_field_type == PHONE_HOME_CITY_AND_NUMBER) { |
209 phone_ = value; | 214 phone_ = value; |
210 return true; | 215 return true; |
211 } | 216 } |
212 | 217 |
213 if (field_type == PHONE_HOME_WHOLE_NUMBER) { | 218 if (server_field_type == PHONE_HOME_WHOLE_NUMBER) { |
214 whole_number_ = value; | 219 whole_number_ = value; |
215 return true; | 220 return true; |
216 } | 221 } |
217 | 222 |
218 if (field_type == PHONE_HOME_NUMBER) { | 223 if (server_field_type == PHONE_HOME_NUMBER) { |
219 phone_.append(value); | 224 phone_.append(value); |
220 return true; | 225 return true; |
221 } | 226 } |
222 | 227 |
223 return false; | 228 return false; |
224 } | 229 } |
225 | 230 |
226 bool PhoneNumber::PhoneCombineHelper::ParseNumber( | 231 bool PhoneNumber::PhoneCombineHelper::ParseNumber( |
227 const AutofillProfile& profile, | 232 const AutofillProfile& profile, |
228 const std::string& app_locale, | 233 const std::string& app_locale, |
229 base::string16* value) { | 234 base::string16* value) { |
230 if (IsEmpty()) | 235 if (IsEmpty()) |
231 return false; | 236 return false; |
232 | 237 |
233 if (!whole_number_.empty()) { | 238 if (!whole_number_.empty()) { |
234 *value = whole_number_; | 239 *value = whole_number_; |
235 return true; | 240 return true; |
236 } | 241 } |
237 | 242 |
238 return i18n::ConstructPhoneNumber( | 243 return i18n::ConstructPhoneNumber( |
239 country_, city_, phone_, GetRegion(profile, app_locale), value); | 244 country_, city_, phone_, GetRegion(profile, app_locale), value); |
240 } | 245 } |
241 | 246 |
242 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { | 247 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
243 return phone_.empty() && whole_number_.empty(); | 248 return phone_.empty() && whole_number_.empty(); |
244 } | 249 } |
245 | 250 |
246 } // namespace autofill | 251 } // namespace autofill |
OLD | NEW |