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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 67 |
68 void PhoneNumber::GetSupportedTypes(ServerFieldTypeSet* 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(ServerFieldType type) const { | 76 base::string16 PhoneNumber::GetRawInfo(ServerFieldType type) const { |
77 type = AutofillType::GetEquivalentFieldType(type); | 77 // TODO(isherman): Is GetStorableType even necessary? |
78 if (type == PHONE_HOME_WHOLE_NUMBER) | 78 if (AutofillType(type).GetStorableType() == 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(ServerFieldType type, | 87 void PhoneNumber::SetRawInfo(ServerFieldType type, |
88 const base::string16& value) { | 88 const base::string16& value) { |
89 type = AutofillType::GetEquivalentFieldType(type); | 89 // TODO(isherman): Is GetStorableType even necessary? |
90 if (type != PHONE_HOME_CITY_AND_NUMBER && | 90 type = AutofillType(type).GetStorableType(); |
91 type != PHONE_HOME_WHOLE_NUMBER) { | 91 if (type != PHONE_HOME_CITY_AND_NUMBER && 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(const AutofillType& type, | 107 base::string16 PhoneNumber::GetInfo(const AutofillType& type, |
108 const std::string& app_locale) const { | 108 const std::string& app_locale) const { |
109 ServerFieldType server_type = | 109 ServerFieldType storable_type = type.GetStorableType(); |
110 AutofillType::GetEquivalentFieldType(type.server_type()); | |
111 UpdateCacheIfNeeded(app_locale); | 110 UpdateCacheIfNeeded(app_locale); |
112 | 111 |
113 // Queries for whole numbers will return the non-normalized number if | 112 // Queries for whole numbers will return the non-normalized number if |
114 // normalization for the number fails. All other field types require | 113 // normalization for the number fails. All other field types require |
115 // normalization. | 114 // normalization. |
116 if (server_type != PHONE_HOME_WHOLE_NUMBER && | 115 if (storable_type != PHONE_HOME_WHOLE_NUMBER && |
117 !cached_parsed_phone_.IsValidNumber()) | 116 !cached_parsed_phone_.IsValidNumber()) |
118 return base::string16(); | 117 return base::string16(); |
119 | 118 |
120 switch (server_type) { | 119 switch (storable_type) { |
121 case PHONE_HOME_WHOLE_NUMBER: | 120 case PHONE_HOME_WHOLE_NUMBER: |
122 return cached_parsed_phone_.GetWholeNumber(); | 121 return cached_parsed_phone_.GetWholeNumber(); |
123 | 122 |
124 case PHONE_HOME_NUMBER: | 123 case PHONE_HOME_NUMBER: |
125 return cached_parsed_phone_.number(); | 124 return cached_parsed_phone_.number(); |
126 | 125 |
127 case PHONE_HOME_CITY_CODE: | 126 case PHONE_HOME_CITY_CODE: |
128 return cached_parsed_phone_.city_code(); | 127 return cached_parsed_phone_.city_code(); |
129 | 128 |
130 case PHONE_HOME_COUNTRY_CODE: | 129 case PHONE_HOME_COUNTRY_CODE: |
131 return cached_parsed_phone_.country_code(); | 130 return cached_parsed_phone_.country_code(); |
132 | 131 |
133 case PHONE_HOME_CITY_AND_NUMBER: | 132 case PHONE_HOME_CITY_AND_NUMBER: |
134 return | 133 return |
135 cached_parsed_phone_.city_code() + cached_parsed_phone_.number(); | 134 cached_parsed_phone_.city_code() + cached_parsed_phone_.number(); |
136 | 135 |
137 default: | 136 default: |
138 NOTREACHED(); | 137 NOTREACHED(); |
139 return base::string16(); | 138 return base::string16(); |
140 } | 139 } |
141 } | 140 } |
142 | 141 |
143 bool PhoneNumber::SetInfo(const AutofillType& type, | 142 bool PhoneNumber::SetInfo(const AutofillType& type, |
144 const base::string16& value, | 143 const base::string16& value, |
145 const std::string& app_locale) { | 144 const std::string& app_locale) { |
146 ServerFieldType server_type = | 145 SetRawInfo(type.GetStorableType(), value); |
147 AutofillType::GetEquivalentFieldType(type.server_type()); | |
148 SetRawInfo(server_type, value); | |
149 | 146 |
150 if (number_.empty()) | 147 if (number_.empty()) |
151 return true; | 148 return true; |
152 | 149 |
153 // Store a formatted (i.e., pretty printed) version of the number. | 150 // Store a formatted (i.e., pretty printed) version of the number. |
154 UpdateCacheIfNeeded(app_locale); | 151 UpdateCacheIfNeeded(app_locale); |
155 number_ = cached_parsed_phone_.GetFormattedNumber(); | 152 number_ = cached_parsed_phone_.GetFormattedNumber(); |
156 return !number_.empty(); | 153 return !number_.empty(); |
157 } | 154 } |
158 | 155 |
(...skipping 30 matching lines...) Expand all Loading... |
189 if (!number_.empty() && cached_parsed_phone_.region() != region) | 186 if (!number_.empty() && cached_parsed_phone_.region() != region) |
190 cached_parsed_phone_ = i18n::PhoneObject(number_, region); | 187 cached_parsed_phone_ = i18n::PhoneObject(number_, region); |
191 } | 188 } |
192 | 189 |
193 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { | 190 PhoneNumber::PhoneCombineHelper::PhoneCombineHelper() { |
194 } | 191 } |
195 | 192 |
196 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { | 193 PhoneNumber::PhoneCombineHelper::~PhoneCombineHelper() { |
197 } | 194 } |
198 | 195 |
199 bool PhoneNumber::PhoneCombineHelper::SetInfo(const AutofillType& field_type, | 196 bool PhoneNumber::PhoneCombineHelper::SetInfo(const AutofillType& type, |
200 const base::string16& value) { | 197 const base::string16& value) { |
201 ServerFieldType server_field_type = | 198 ServerFieldType storable_type = type.GetStorableType(); |
202 AutofillType::GetEquivalentFieldType(field_type.server_type()); | 199 if (storable_type == PHONE_HOME_COUNTRY_CODE) { |
203 if (server_field_type == PHONE_HOME_COUNTRY_CODE) { | |
204 country_ = value; | 200 country_ = value; |
205 return true; | 201 return true; |
206 } | 202 } |
207 | 203 |
208 if (server_field_type == PHONE_HOME_CITY_CODE) { | 204 if (storable_type == PHONE_HOME_CITY_CODE) { |
209 city_ = value; | 205 city_ = value; |
210 return true; | 206 return true; |
211 } | 207 } |
212 | 208 |
213 if (server_field_type == PHONE_HOME_CITY_AND_NUMBER) { | 209 if (storable_type == PHONE_HOME_CITY_AND_NUMBER) { |
214 phone_ = value; | 210 phone_ = value; |
215 return true; | 211 return true; |
216 } | 212 } |
217 | 213 |
218 if (server_field_type == PHONE_HOME_WHOLE_NUMBER) { | 214 if (storable_type == PHONE_HOME_WHOLE_NUMBER) { |
219 whole_number_ = value; | 215 whole_number_ = value; |
220 return true; | 216 return true; |
221 } | 217 } |
222 | 218 |
223 if (server_field_type == PHONE_HOME_NUMBER) { | 219 if (storable_type == PHONE_HOME_NUMBER) { |
224 phone_.append(value); | 220 phone_.append(value); |
225 return true; | 221 return true; |
226 } | 222 } |
227 | 223 |
228 return false; | 224 return false; |
229 } | 225 } |
230 | 226 |
231 bool PhoneNumber::PhoneCombineHelper::ParseNumber( | 227 bool PhoneNumber::PhoneCombineHelper::ParseNumber( |
232 const AutofillProfile& profile, | 228 const AutofillProfile& profile, |
233 const std::string& app_locale, | 229 const std::string& app_locale, |
234 base::string16* value) { | 230 base::string16* value) { |
235 if (IsEmpty()) | 231 if (IsEmpty()) |
236 return false; | 232 return false; |
237 | 233 |
238 if (!whole_number_.empty()) { | 234 if (!whole_number_.empty()) { |
239 *value = whole_number_; | 235 *value = whole_number_; |
240 return true; | 236 return true; |
241 } | 237 } |
242 | 238 |
243 return i18n::ConstructPhoneNumber( | 239 return i18n::ConstructPhoneNumber( |
244 country_, city_, phone_, GetRegion(profile, app_locale), value); | 240 country_, city_, phone_, GetRegion(profile, app_locale), value); |
245 } | 241 } |
246 | 242 |
247 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { | 243 bool PhoneNumber::PhoneCombineHelper::IsEmpty() const { |
248 return phone_.empty() && whole_number_.empty(); | 244 return phone_.empty() && whole_number_.empty(); |
249 } | 245 } |
250 | 246 |
251 } // namespace autofill | 247 } // namespace autofill |
OLD | NEW |