Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 23 #include "ui/base/l10n/l10n_util_mac.h" | 23 #include "ui/base/l10n/l10n_util_mac.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // This implementation makes use of the Address Book API. Profiles are | 27 // This implementation makes use of the Address Book API. Profiles are |
| 28 // generated that correspond to addresses in the "me" card that reside in the | 28 // generated that correspond to addresses in the "me" card that reside in the |
| 29 // user's Address Book. The caller passes a vector of profiles into the | 29 // user's Address Book. The caller passes a vector of profiles into the |
| 30 // the constructer and then initiate the fetch from the Mac Address Book "me" | 30 // the constructer and then initiate the fetch from the Mac Address Book "me" |
| 31 // card using the main |GetAddressBookMeCard()| method. This clears any | 31 // card using the main |GetAddressBookMeCard()| method. This clears any |
| 32 // existing addresses and populates new addresses derived from the data found | 32 // existing addresses and populates new addresses derived from the data found |
| 33 // in the "me" card. | 33 // in the "me" card. |
|
Yaron
2013/02/28 20:40:43
This is awkward. We shouldn't have "Android" in mm
apiccion
2013/03/01 01:04:34
Oops!
| |
| 34 class AuxiliaryProfilesImpl { | 34 class AuxiliaryProfilesAndroid { |
| 35 public: | 35 public: |
| 36 // Constructor takes a reference to the |profiles| that will be filled in | 36 // Constructor takes a reference to the |profiles| that will be filled in |
| 37 // by the subsequent call to |GetAddressBookMeCard()|. |profiles| may not | 37 // by the subsequent call to |GetAddressBookMeCard()|. |profiles| may not |
| 38 // be NULL. | 38 // be NULL. |
| 39 explicit AuxiliaryProfilesImpl(ScopedVector<AutofillProfile>* profiles) | 39 explicit AuxiliaryProfilesAndroid(ScopedVector<AutofillProfile>* profiles) |
| 40 : profiles_(*profiles) { | 40 : profiles_(*profiles) { |
| 41 } | 41 } |
| 42 virtual ~AuxiliaryProfilesImpl() {} | 42 virtual ~AuxiliaryProfilesAndroid() {} |
| 43 | 43 |
| 44 // Import the "me" card from the Mac Address Book and fill in |profiles_|. | 44 // Import the "me" card from the Mac Address Book and fill in |profiles_|. |
| 45 void GetAddressBookMeCard(); | 45 void GetAddressBookMeCard(); |
| 46 | 46 |
| 47 private: | 47 private: |
| 48 void GetAddressBookNames(ABPerson* me, | 48 void GetAddressBookNames(ABPerson* me, |
| 49 NSString* addressLabelRaw, | 49 NSString* addressLabelRaw, |
| 50 AutofillProfile* profile); | 50 AutofillProfile* profile); |
| 51 void GetAddressBookAddress(NSDictionary* address, AutofillProfile* profile); | 51 void GetAddressBookAddress(NSDictionary* address, AutofillProfile* profile); |
| 52 void GetAddressBookEmail(ABPerson* me, | 52 void GetAddressBookEmail(ABPerson* me, |
| 53 NSString* addressLabelRaw, | 53 NSString* addressLabelRaw, |
| 54 AutofillProfile* profile); | 54 AutofillProfile* profile); |
| 55 void GetAddressBookPhoneNumbers(ABPerson* me, | 55 void GetAddressBookPhoneNumbers(ABPerson* me, |
| 56 NSString* addressLabelRaw, | 56 NSString* addressLabelRaw, |
| 57 AutofillProfile* profile); | 57 AutofillProfile* profile); |
| 58 | 58 |
| 59 private: | 59 private: |
| 60 // A reference to the profiles this class populates. | 60 // A reference to the profiles this class populates. |
| 61 ScopedVector<AutofillProfile>& profiles_; | 61 ScopedVector<AutofillProfile>& profiles_; |
| 62 | 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(AuxiliaryProfilesImpl); | 63 DISALLOW_COPY_AND_ASSIGN(AuxiliaryProfilesAndroid); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 // This method uses the |ABAddressBook| system service to fetch the "me" card | 66 // This method uses the |ABAddressBook| system service to fetch the "me" card |
| 67 // from the active user's address book. It looks for the user address | 67 // from the active user's address book. It looks for the user address |
| 68 // information and translates it to the internal list of |AutofillProfile| data | 68 // information and translates it to the internal list of |AutofillProfile| data |
| 69 // structures. | 69 // structures. |
| 70 void AuxiliaryProfilesImpl::GetAddressBookMeCard() { | 70 void AuxiliaryProfilesAndroid::GetAddressBookMeCard() { |
| 71 profiles_.clear(); | 71 profiles_.clear(); |
| 72 | 72 |
| 73 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 73 // +[ABAddressBook sharedAddressBook] throws an exception internally in |
| 74 // circumstances that aren't clear. The exceptions are only observed in crash | 74 // circumstances that aren't clear. The exceptions are only observed in crash |
| 75 // reports, so it is unknown whether they would be caught by AppKit and nil | 75 // reports, so it is unknown whether they would be caught by AppKit and nil |
| 76 // returned, or if they would take down the app. In either case, avoid | 76 // returned, or if they would take down the app. In either case, avoid |
| 77 // crashing. http://crbug.com/129022 | 77 // crashing. http://crbug.com/129022 |
| 78 ABAddressBook* addressBook = base::mac::PerformSelectorIgnoringExceptions( | 78 ABAddressBook* addressBook = base::mac::PerformSelectorIgnoringExceptions( |
| 79 NSClassFromString(@"ABAddressBook"), @selector(sharedAddressBook)); | 79 NSClassFromString(@"ABAddressBook"), @selector(sharedAddressBook)); |
| 80 ABPerson* me = [addressBook me]; | 80 ABPerson* me = [addressBook me]; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 // Fill in phone number information. | 129 // Fill in phone number information. |
| 130 GetAddressBookPhoneNumbers(me, addressLabelRaw, profile.get()); | 130 GetAddressBookPhoneNumbers(me, addressLabelRaw, profile.get()); |
| 131 | 131 |
| 132 profiles_.push_back(profile.release()); | 132 profiles_.push_back(profile.release()); |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Name and company information is stored once in the Address Book against | 136 // Name and company information is stored once in the Address Book against |
| 137 // multiple addresses. We replicate that information for each profile. | 137 // multiple addresses. We replicate that information for each profile. |
| 138 // We only propagate the company name to work profiles. | 138 // We only propagate the company name to work profiles. |
| 139 void AuxiliaryProfilesImpl::GetAddressBookNames( | 139 void AuxiliaryProfilesAndroid::GetAddressBookNames( |
| 140 ABPerson* me, | 140 ABPerson* me, |
| 141 NSString* addressLabelRaw, | 141 NSString* addressLabelRaw, |
| 142 AutofillProfile* profile) { | 142 AutofillProfile* profile) { |
| 143 NSString* firstName = [me valueForProperty:kABFirstNameProperty]; | 143 NSString* firstName = [me valueForProperty:kABFirstNameProperty]; |
| 144 NSString* middleName = [me valueForProperty:kABMiddleNameProperty]; | 144 NSString* middleName = [me valueForProperty:kABMiddleNameProperty]; |
| 145 NSString* lastName = [me valueForProperty:kABLastNameProperty]; | 145 NSString* lastName = [me valueForProperty:kABLastNameProperty]; |
| 146 NSString* companyName = [me valueForProperty:kABOrganizationProperty]; | 146 NSString* companyName = [me valueForProperty:kABOrganizationProperty]; |
| 147 | 147 |
| 148 profile->SetRawInfo(NAME_FIRST, base::SysNSStringToUTF16(firstName)); | 148 profile->SetRawInfo(NAME_FIRST, base::SysNSStringToUTF16(firstName)); |
| 149 profile->SetRawInfo(NAME_MIDDLE, base::SysNSStringToUTF16(middleName)); | 149 profile->SetRawInfo(NAME_MIDDLE, base::SysNSStringToUTF16(middleName)); |
| 150 profile->SetRawInfo(NAME_LAST, base::SysNSStringToUTF16(lastName)); | 150 profile->SetRawInfo(NAME_LAST, base::SysNSStringToUTF16(lastName)); |
| 151 if ([addressLabelRaw isEqualToString:kABAddressWorkLabel]) | 151 if ([addressLabelRaw isEqualToString:kABAddressWorkLabel]) |
| 152 profile->SetRawInfo(COMPANY_NAME, base::SysNSStringToUTF16(companyName)); | 152 profile->SetRawInfo(COMPANY_NAME, base::SysNSStringToUTF16(companyName)); |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Addresss information from the Address Book may span multiple lines. | 155 // Addresss information from the Address Book may span multiple lines. |
| 156 // If it does then we represent the address with two lines in the profile. The | 156 // If it does then we represent the address with two lines in the profile. The |
| 157 // second line we join with commas. | 157 // second line we join with commas. |
| 158 // For example: "c/o John Doe\n1122 Other Avenue\nApt #7" translates to | 158 // For example: "c/o John Doe\n1122 Other Avenue\nApt #7" translates to |
| 159 // line 1: "c/o John Doe", line 2: "1122 Other Avenue, Apt #7". | 159 // line 1: "c/o John Doe", line 2: "1122 Other Avenue, Apt #7". |
| 160 void AuxiliaryProfilesImpl::GetAddressBookAddress(NSDictionary* address, | 160 void AuxiliaryProfilesAndroid::GetAddressBookAddress(NSDictionary* address, |
| 161 AutofillProfile* profile) { | 161 AutofillProfile* profile) { |
| 162 if (NSString* addressField = [address objectForKey:kABAddressStreetKey]) { | 162 if (NSString* addressField = [address objectForKey:kABAddressStreetKey]) { |
| 163 // If there are newlines in the address, split into two lines. | 163 // If there are newlines in the address, split into two lines. |
| 164 if ([addressField rangeOfCharacterFromSet: | 164 if ([addressField rangeOfCharacterFromSet: |
| 165 [NSCharacterSet newlineCharacterSet]].location != NSNotFound) { | 165 [NSCharacterSet newlineCharacterSet]].location != NSNotFound) { |
| 166 NSArray* chunks = [addressField componentsSeparatedByCharactersInSet: | 166 NSArray* chunks = [addressField componentsSeparatedByCharactersInSet: |
| 167 [NSCharacterSet newlineCharacterSet]]; | 167 [NSCharacterSet newlineCharacterSet]]; |
| 168 DCHECK([chunks count] > 1); | 168 DCHECK([chunks count] > 1); |
| 169 | 169 |
| 170 NSString* separator = l10n_util::GetNSString( | 170 NSString* separator = l10n_util::GetNSString( |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 196 if (NSString* country = [address objectForKey:kABAddressCountryKey]) { | 196 if (NSString* country = [address objectForKey:kABAddressCountryKey]) { |
| 197 profile->SetInfo(ADDRESS_HOME_COUNTRY, | 197 profile->SetInfo(ADDRESS_HOME_COUNTRY, |
| 198 base::SysNSStringToUTF16(country), | 198 base::SysNSStringToUTF16(country), |
| 199 AutofillCountry::ApplicationLocale()); | 199 AutofillCountry::ApplicationLocale()); |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 | 202 |
| 203 // Fills in email address matching current address label. Note that there may | 203 // Fills in email address matching current address label. Note that there may |
| 204 // be multiple matching email addresses for a given label. We take the | 204 // be multiple matching email addresses for a given label. We take the |
| 205 // first we find (topmost) as preferred. | 205 // first we find (topmost) as preferred. |
| 206 void AuxiliaryProfilesImpl::GetAddressBookEmail( | 206 void AuxiliaryProfilesAndroid::GetAddressBookEmail( |
| 207 ABPerson* me, | 207 ABPerson* me, |
| 208 NSString* addressLabelRaw, | 208 NSString* addressLabelRaw, |
| 209 AutofillProfile* profile) { | 209 AutofillProfile* profile) { |
| 210 ABMultiValue* emailAddresses = [me valueForProperty:kABEmailProperty]; | 210 ABMultiValue* emailAddresses = [me valueForProperty:kABEmailProperty]; |
| 211 NSString* emailAddress = nil; | 211 NSString* emailAddress = nil; |
| 212 for (NSUInteger j = 0, emailCount = [emailAddresses count]; | 212 for (NSUInteger j = 0, emailCount = [emailAddresses count]; |
| 213 j < emailCount; j++) { | 213 j < emailCount; j++) { |
| 214 NSString* emailAddressLabelRaw = [emailAddresses labelAtIndex:j]; | 214 NSString* emailAddressLabelRaw = [emailAddresses labelAtIndex:j]; |
| 215 if ([emailAddressLabelRaw isEqualToString:addressLabelRaw]) { | 215 if ([emailAddressLabelRaw isEqualToString:addressLabelRaw]) { |
| 216 emailAddress = [emailAddresses valueAtIndex:j]; | 216 emailAddress = [emailAddresses valueAtIndex:j]; |
| 217 break; | 217 break; |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 profile->SetRawInfo(EMAIL_ADDRESS, base::SysNSStringToUTF16(emailAddress)); | 220 profile->SetRawInfo(EMAIL_ADDRESS, base::SysNSStringToUTF16(emailAddress)); |
| 221 } | 221 } |
| 222 | 222 |
| 223 // Fills in telephone numbers. Each of these are special cases. | 223 // Fills in telephone numbers. Each of these are special cases. |
| 224 // We match two cases: home/tel, work/tel. | 224 // We match two cases: home/tel, work/tel. |
| 225 // Note, we traverse in reverse order so that top values in address book | 225 // Note, we traverse in reverse order so that top values in address book |
| 226 // take priority. | 226 // take priority. |
| 227 void AuxiliaryProfilesImpl::GetAddressBookPhoneNumbers( | 227 void AuxiliaryProfilesAndroid::GetAddressBookPhoneNumbers( |
| 228 ABPerson* me, | 228 ABPerson* me, |
| 229 NSString* addressLabelRaw, | 229 NSString* addressLabelRaw, |
| 230 AutofillProfile* profile) { | 230 AutofillProfile* profile) { |
| 231 ABMultiValue* phoneNumbers = [me valueForProperty:kABPhoneProperty]; | 231 ABMultiValue* phoneNumbers = [me valueForProperty:kABPhoneProperty]; |
| 232 for (NSUInteger k = 0, phoneCount = [phoneNumbers count]; | 232 for (NSUInteger k = 0, phoneCount = [phoneNumbers count]; |
| 233 k < phoneCount; k++) { | 233 k < phoneCount; k++) { |
| 234 NSUInteger reverseK = phoneCount - k - 1; | 234 NSUInteger reverseK = phoneCount - k - 1; |
| 235 NSString* phoneLabelRaw = [phoneNumbers labelAtIndex:reverseK]; | 235 NSString* phoneLabelRaw = [phoneNumbers labelAtIndex:reverseK]; |
| 236 if ([addressLabelRaw isEqualToString:kABAddressHomeLabel] && | 236 if ([addressLabelRaw isEqualToString:kABAddressHomeLabel] && |
| 237 [phoneLabelRaw isEqualToString:kABPhoneHomeLabel]) { | 237 [phoneLabelRaw isEqualToString:kABPhoneHomeLabel]) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 249 [phoneNumbers valueAtIndex:reverseK]); | 249 [phoneNumbers valueAtIndex:reverseK]); |
| 250 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, phone); | 250 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, phone); |
| 251 } | 251 } |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 | 254 |
| 255 } // namespace | 255 } // namespace |
| 256 | 256 |
| 257 // Populate |auxiliary_profiles_| with the Address Book data. | 257 // Populate |auxiliary_profiles_| with the Address Book data. |
| 258 void PersonalDataManager::LoadAuxiliaryProfiles() { | 258 void PersonalDataManager::LoadAuxiliaryProfiles() { |
| 259 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 259 AuxiliaryProfilesAndroid impl(&auxiliary_profiles_); |
| 260 impl.GetAddressBookMeCard(); | 260 impl.GetAddressBookMeCard(); |
| 261 } | 261 } |
| OLD | NEW |