OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/personal_data_manager.h" | 5 #include "chrome/browser/autofill/personal_data_manager.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #import <AddressBook/AddressBook.h> | 9 #import <AddressBook/AddressBook.h> |
10 | 10 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 // We only propagate the company name to work profiles. | 137 // We only propagate the company name to work profiles. |
138 void AuxiliaryProfilesImpl::GetAddressBookNames( | 138 void AuxiliaryProfilesImpl::GetAddressBookNames( |
139 ABPerson* me, | 139 ABPerson* me, |
140 NSString* addressLabelRaw, | 140 NSString* addressLabelRaw, |
141 AutofillProfile* profile) { | 141 AutofillProfile* profile) { |
142 NSString* firstName = [me valueForProperty:kABFirstNameProperty]; | 142 NSString* firstName = [me valueForProperty:kABFirstNameProperty]; |
143 NSString* middleName = [me valueForProperty:kABMiddleNameProperty]; | 143 NSString* middleName = [me valueForProperty:kABMiddleNameProperty]; |
144 NSString* lastName = [me valueForProperty:kABLastNameProperty]; | 144 NSString* lastName = [me valueForProperty:kABLastNameProperty]; |
145 NSString* companyName = [me valueForProperty:kABOrganizationProperty]; | 145 NSString* companyName = [me valueForProperty:kABOrganizationProperty]; |
146 | 146 |
147 profile->SetInfo(NAME_FIRST, base::SysNSStringToUTF16(firstName)); | 147 profile->SetRawInfo(NAME_FIRST, base::SysNSStringToUTF16(firstName)); |
148 profile->SetInfo(NAME_MIDDLE, base::SysNSStringToUTF16(middleName)); | 148 profile->SetRawInfo(NAME_MIDDLE, base::SysNSStringToUTF16(middleName)); |
149 profile->SetInfo(NAME_LAST, base::SysNSStringToUTF16(lastName)); | 149 profile->SetRawInfo(NAME_LAST, base::SysNSStringToUTF16(lastName)); |
150 if ([addressLabelRaw isEqualToString:kABAddressWorkLabel]) | 150 if ([addressLabelRaw isEqualToString:kABAddressWorkLabel]) |
151 profile->SetInfo(COMPANY_NAME, base::SysNSStringToUTF16(companyName)); | 151 profile->SetRawInfo(COMPANY_NAME, base::SysNSStringToUTF16(companyName)); |
152 } | 152 } |
153 | 153 |
154 // Addresss information from the Address Book may span multiple lines. | 154 // Addresss information from the Address Book may span multiple lines. |
155 // If it does then we represent the address with two lines in the profile. The | 155 // If it does then we represent the address with two lines in the profile. The |
156 // second line we join with commas. | 156 // second line we join with commas. |
157 // For example: "c/o John Doe\n1122 Other Avenue\nApt #7" translates to | 157 // For example: "c/o John Doe\n1122 Other Avenue\nApt #7" translates to |
158 // line 1: "c/o John Doe", line 2: "1122 Other Avenue, Apt #7". | 158 // line 1: "c/o John Doe", line 2: "1122 Other Avenue, Apt #7". |
159 void AuxiliaryProfilesImpl::GetAddressBookAddresses(NSDictionary* address, | 159 void AuxiliaryProfilesImpl::GetAddressBookAddresses(NSDictionary* address, |
160 AutofillProfile* profile) { | 160 AutofillProfile* profile) { |
161 if (NSString* addressField = [address objectForKey:kABAddressStreetKey]) { | 161 if (NSString* addressField = [address objectForKey:kABAddressStreetKey]) { |
162 // If there are newlines in the address, split into two lines. | 162 // If there are newlines in the address, split into two lines. |
163 if ([addressField rangeOfCharacterFromSet: | 163 if ([addressField rangeOfCharacterFromSet: |
164 [NSCharacterSet newlineCharacterSet]].location != NSNotFound) { | 164 [NSCharacterSet newlineCharacterSet]].location != NSNotFound) { |
165 NSArray* chunks = [addressField componentsSeparatedByCharactersInSet: | 165 NSArray* chunks = [addressField componentsSeparatedByCharactersInSet: |
166 [NSCharacterSet newlineCharacterSet]]; | 166 [NSCharacterSet newlineCharacterSet]]; |
167 DCHECK([chunks count] > 1); | 167 DCHECK([chunks count] > 1); |
168 | 168 |
169 NSString* separator = l10n_util::GetNSString( | 169 NSString* separator = l10n_util::GetNSString( |
170 IDS_AUTOFILL_MAC_ADDRESS_LINE_SEPARATOR); | 170 IDS_AUTOFILL_MAC_ADDRESS_LINE_SEPARATOR); |
171 | 171 |
172 NSString* addressField1 = [chunks objectAtIndex:0]; | 172 NSString* addressField1 = [chunks objectAtIndex:0]; |
173 NSString* addressField2 = | 173 NSString* addressField2 = |
174 [[chunks subarrayWithRange:NSMakeRange(1, [chunks count] - 1)] | 174 [[chunks subarrayWithRange:NSMakeRange(1, [chunks count] - 1)] |
175 componentsJoinedByString:separator]; | 175 componentsJoinedByString:separator]; |
176 profile->SetInfo(ADDRESS_HOME_LINE1, | 176 profile->SetRawInfo(ADDRESS_HOME_LINE1, |
177 base::SysNSStringToUTF16(addressField1)); | 177 base::SysNSStringToUTF16(addressField1)); |
178 profile->SetInfo(ADDRESS_HOME_LINE2, | 178 profile->SetRawInfo(ADDRESS_HOME_LINE2, |
179 base::SysNSStringToUTF16(addressField2)); | 179 base::SysNSStringToUTF16(addressField2)); |
180 } else { | 180 } else { |
181 profile->SetInfo(ADDRESS_HOME_LINE1, | 181 profile->SetRawInfo(ADDRESS_HOME_LINE1, |
182 base::SysNSStringToUTF16(addressField)); | 182 base::SysNSStringToUTF16(addressField)); |
183 } | 183 } |
184 } | 184 } |
185 | 185 |
186 if (NSString* city = [address objectForKey:kABAddressCityKey]) | 186 if (NSString* city = [address objectForKey:kABAddressCityKey]) |
187 profile->SetInfo(ADDRESS_HOME_CITY, base::SysNSStringToUTF16(city)); | 187 profile->SetRawInfo(ADDRESS_HOME_CITY, base::SysNSStringToUTF16(city)); |
Dan Beam
2012/11/09 18:46:30
nit: I find \n after if () statements more readabl
Ilya Sherman
2012/11/10 03:21:33
Done.
| |
188 if (NSString* state = [address objectForKey:kABAddressStateKey]) | 188 if (NSString* state = [address objectForKey:kABAddressStateKey]) |
189 profile->SetInfo(ADDRESS_HOME_STATE, base::SysNSStringToUTF16(state)); | 189 profile->SetRawInfo(ADDRESS_HOME_STATE, base::SysNSStringToUTF16(state)); |
190 if (NSString* zip = [address objectForKey:kABAddressZIPKey]) | 190 if (NSString* zip = [address objectForKey:kABAddressZIPKey]) |
191 profile->SetInfo(ADDRESS_HOME_ZIP, base::SysNSStringToUTF16(zip)); | 191 profile->SetRawInfo(ADDRESS_HOME_ZIP, base::SysNSStringToUTF16(zip)); |
192 if (NSString* country = [address objectForKey:kABAddressCountryKey]) | 192 if (NSString* country = [address objectForKey:kABAddressCountryKey]) { |
193 profile->SetInfo(ADDRESS_HOME_COUNTRY, base::SysNSStringToUTF16(country)); | 193 profile->SetRawInfo(ADDRESS_HOME_COUNTRY, |
194 base::SysNSStringToUTF16(country)); | |
195 } | |
194 } | 196 } |
195 | 197 |
196 // Fills in email address matching current address label. Note that there may | 198 // Fills in email address matching current address label. Note that there may |
197 // be multiple matching email addresses for a given label. We take the | 199 // be multiple matching email addresses for a given label. We take the |
198 // first we find (topmost) as preferred. | 200 // first we find (topmost) as preferred. |
199 void AuxiliaryProfilesImpl::GetAddressBookEmail( | 201 void AuxiliaryProfilesImpl::GetAddressBookEmail( |
200 ABPerson* me, | 202 ABPerson* me, |
201 NSString* addressLabelRaw, | 203 NSString* addressLabelRaw, |
202 AutofillProfile* profile) { | 204 AutofillProfile* profile) { |
203 ABMultiValue* emailAddresses = [me valueForProperty:kABEmailProperty]; | 205 ABMultiValue* emailAddresses = [me valueForProperty:kABEmailProperty]; |
204 NSString* emailAddress = nil; | 206 NSString* emailAddress = nil; |
205 for (NSUInteger j = 0, emailCount = [emailAddresses count]; | 207 for (NSUInteger j = 0, emailCount = [emailAddresses count]; |
206 j < emailCount; j++) { | 208 j < emailCount; j++) { |
207 NSString* emailAddressLabelRaw = [emailAddresses labelAtIndex:j]; | 209 NSString* emailAddressLabelRaw = [emailAddresses labelAtIndex:j]; |
208 if ([emailAddressLabelRaw isEqualToString:addressLabelRaw]) { | 210 if ([emailAddressLabelRaw isEqualToString:addressLabelRaw]) { |
209 emailAddress = [emailAddresses valueAtIndex:j]; | 211 emailAddress = [emailAddresses valueAtIndex:j]; |
210 break; | 212 break; |
211 } | 213 } |
212 } | 214 } |
213 profile->SetInfo(EMAIL_ADDRESS, base::SysNSStringToUTF16(emailAddress)); | 215 profile->SetRawInfo(EMAIL_ADDRESS, base::SysNSStringToUTF16(emailAddress)); |
214 } | 216 } |
215 | 217 |
216 // Fills in telephone numbers. Each of these are special cases. | 218 // Fills in telephone numbers. Each of these are special cases. |
217 // We match two cases: home/tel, work/tel. | 219 // We match two cases: home/tel, work/tel. |
218 // Note, we traverse in reverse order so that top values in address book | 220 // Note, we traverse in reverse order so that top values in address book |
219 // take priority. | 221 // take priority. |
220 void AuxiliaryProfilesImpl::GetAddressBookPhoneNumbers( | 222 void AuxiliaryProfilesImpl::GetAddressBookPhoneNumbers( |
221 ABPerson* me, | 223 ABPerson* me, |
222 NSString* addressLabelRaw, | 224 NSString* addressLabelRaw, |
223 AutofillProfile* profile) { | 225 AutofillProfile* profile) { |
224 ABMultiValue* phoneNumbers = [me valueForProperty:kABPhoneProperty]; | 226 ABMultiValue* phoneNumbers = [me valueForProperty:kABPhoneProperty]; |
225 for (NSUInteger k = 0, phoneCount = [phoneNumbers count]; | 227 for (NSUInteger k = 0, phoneCount = [phoneNumbers count]; |
226 k < phoneCount; k++) { | 228 k < phoneCount; k++) { |
227 NSUInteger reverseK = phoneCount - k - 1; | 229 NSUInteger reverseK = phoneCount - k - 1; |
228 NSString* phoneLabelRaw = [phoneNumbers labelAtIndex:reverseK]; | 230 NSString* phoneLabelRaw = [phoneNumbers labelAtIndex:reverseK]; |
229 if ([addressLabelRaw isEqualToString:kABAddressHomeLabel] && | 231 if ([addressLabelRaw isEqualToString:kABAddressHomeLabel] && |
230 [phoneLabelRaw isEqualToString:kABPhoneHomeLabel]) { | 232 [phoneLabelRaw isEqualToString:kABPhoneHomeLabel]) { |
231 string16 homePhone = base::SysNSStringToUTF16( | 233 string16 homePhone = base::SysNSStringToUTF16( |
232 [phoneNumbers valueAtIndex:reverseK]); | 234 [phoneNumbers valueAtIndex:reverseK]); |
233 profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, homePhone); | 235 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, homePhone); |
234 } else if ([addressLabelRaw isEqualToString:kABAddressWorkLabel] && | 236 } else if ([addressLabelRaw isEqualToString:kABAddressWorkLabel] && |
235 [phoneLabelRaw isEqualToString:kABPhoneWorkLabel]) { | 237 [phoneLabelRaw isEqualToString:kABPhoneWorkLabel]) { |
236 string16 workPhone = base::SysNSStringToUTF16( | 238 string16 workPhone = base::SysNSStringToUTF16( |
237 [phoneNumbers valueAtIndex:reverseK]); | 239 [phoneNumbers valueAtIndex:reverseK]); |
238 profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, workPhone); | 240 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, workPhone); |
239 } else if ([phoneLabelRaw isEqualToString:kABPhoneMobileLabel] || | 241 } else if ([phoneLabelRaw isEqualToString:kABPhoneMobileLabel] || |
240 [phoneLabelRaw isEqualToString:kABPhoneMainLabel]) { | 242 [phoneLabelRaw isEqualToString:kABPhoneMainLabel]) { |
241 string16 phone = base::SysNSStringToUTF16( | 243 string16 phone = base::SysNSStringToUTF16( |
242 [phoneNumbers valueAtIndex:reverseK]); | 244 [phoneNumbers valueAtIndex:reverseK]); |
243 profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, phone); | 245 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, phone); |
244 } | 246 } |
245 } | 247 } |
246 } | 248 } |
247 | 249 |
248 } // namespace | 250 } // namespace |
249 | 251 |
250 // Populate |auxiliary_profiles_| with the Address Book data. | 252 // Populate |auxiliary_profiles_| with the Address Book data. |
251 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 253 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
252 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 254 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
253 impl.GetAddressBookMeCard(); | 255 impl.GetAddressBookMeCard(); |
254 } | 256 } |
OLD | NEW |