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 |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/guid.h" | 12 #include "base/guid.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #import "base/mac/scoped_nsexception_enabler.h" | 14 #import "base/mac/scoped_nsexception_enabler.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/sys_string_conversions.h" | 18 #include "base/sys_string_conversions.h" |
| 19 #include "chrome/browser/autofill/autofill_country.h" |
19 #include "chrome/browser/autofill/autofill_profile.h" | 20 #include "chrome/browser/autofill/autofill_profile.h" |
20 #include "chrome/browser/autofill/phone_number.h" | 21 #include "chrome/browser/autofill/phone_number.h" |
21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
22 #include "ui/base/l10n/l10n_util_mac.h" | 23 #include "ui/base/l10n/l10n_util_mac.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // This implementation makes use of the Address Book API. Profiles are | 27 // This implementation makes use of the Address Book API. Profiles are |
27 // 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 |
28 // 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 |
(...skipping 11 matching lines...) Expand all Loading... |
40 } | 41 } |
41 virtual ~AuxiliaryProfilesImpl() {} | 42 virtual ~AuxiliaryProfilesImpl() {} |
42 | 43 |
43 // 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_|. |
44 void GetAddressBookMeCard(); | 45 void GetAddressBookMeCard(); |
45 | 46 |
46 private: | 47 private: |
47 void GetAddressBookNames(ABPerson* me, | 48 void GetAddressBookNames(ABPerson* me, |
48 NSString* addressLabelRaw, | 49 NSString* addressLabelRaw, |
49 AutofillProfile* profile); | 50 AutofillProfile* profile); |
50 void GetAddressBookAddresses(NSDictionary* address, AutofillProfile* profile); | 51 void GetAddressBookAddress(NSDictionary* address, AutofillProfile* profile); |
51 void GetAddressBookEmail(ABPerson* me, | 52 void GetAddressBookEmail(ABPerson* me, |
52 NSString* addressLabelRaw, | 53 NSString* addressLabelRaw, |
53 AutofillProfile* profile); | 54 AutofillProfile* profile); |
54 void GetAddressBookPhoneNumbers(ABPerson* me, | 55 void GetAddressBookPhoneNumbers(ABPerson* me, |
55 NSString* addressLabelRaw, | 56 NSString* addressLabelRaw, |
56 AutofillProfile* profile); | 57 AutofillProfile* profile); |
57 | 58 |
58 private: | 59 private: |
59 // A reference to the profiles this class populates. | 60 // A reference to the profiles this class populates. |
60 ScopedVector<AutofillProfile>& profiles_; | 61 ScopedVector<AutofillProfile>& profiles_; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 guid += base::StringPrintf(kAddressGUIDFormat.c_str(), i); | 114 guid += base::StringPrintf(kAddressGUIDFormat.c_str(), i); |
114 DCHECK_EQ(kGUIDLength, guid.size()); | 115 DCHECK_EQ(kGUIDLength, guid.size()); |
115 | 116 |
116 scoped_ptr<AutofillProfile> profile(new AutofillProfile(guid)); | 117 scoped_ptr<AutofillProfile> profile(new AutofillProfile(guid)); |
117 DCHECK(base::IsValidGUID(profile->guid())); | 118 DCHECK(base::IsValidGUID(profile->guid())); |
118 | 119 |
119 // Fill in name and company information. | 120 // Fill in name and company information. |
120 GetAddressBookNames(me, addressLabelRaw, profile.get()); | 121 GetAddressBookNames(me, addressLabelRaw, profile.get()); |
121 | 122 |
122 // Fill in address information. | 123 // Fill in address information. |
123 GetAddressBookAddresses(address, profile.get()); | 124 GetAddressBookAddress(address, profile.get()); |
124 | 125 |
125 // Fill in email information. | 126 // Fill in email information. |
126 GetAddressBookEmail(me, addressLabelRaw, profile.get()); | 127 GetAddressBookEmail(me, addressLabelRaw, profile.get()); |
127 | 128 |
128 // Fill in phone number information. | 129 // Fill in phone number information. |
129 GetAddressBookPhoneNumbers(me, addressLabelRaw, profile.get()); | 130 GetAddressBookPhoneNumbers(me, addressLabelRaw, profile.get()); |
130 | 131 |
131 profiles_.push_back(profile.release()); | 132 profiles_.push_back(profile.release()); |
132 } | 133 } |
133 } | 134 } |
(...skipping 15 matching lines...) Expand all Loading... |
149 profile->SetRawInfo(NAME_LAST, base::SysNSStringToUTF16(lastName)); | 150 profile->SetRawInfo(NAME_LAST, base::SysNSStringToUTF16(lastName)); |
150 if ([addressLabelRaw isEqualToString:kABAddressWorkLabel]) | 151 if ([addressLabelRaw isEqualToString:kABAddressWorkLabel]) |
151 profile->SetRawInfo(COMPANY_NAME, base::SysNSStringToUTF16(companyName)); | 152 profile->SetRawInfo(COMPANY_NAME, base::SysNSStringToUTF16(companyName)); |
152 } | 153 } |
153 | 154 |
154 // Addresss information from the Address Book may span multiple lines. | 155 // 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 | 156 // If it does then we represent the address with two lines in the profile. The |
156 // second line we join with commas. | 157 // second line we join with commas. |
157 // 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 |
158 // 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". |
159 void AuxiliaryProfilesImpl::GetAddressBookAddresses(NSDictionary* address, | 160 void AuxiliaryProfilesImpl::GetAddressBookAddress(NSDictionary* address, |
160 AutofillProfile* profile) { | 161 AutofillProfile* profile) { |
161 if (NSString* addressField = [address objectForKey:kABAddressStreetKey]) { | 162 if (NSString* addressField = [address objectForKey:kABAddressStreetKey]) { |
162 // If there are newlines in the address, split into two lines. | 163 // If there are newlines in the address, split into two lines. |
163 if ([addressField rangeOfCharacterFromSet: | 164 if ([addressField rangeOfCharacterFromSet: |
164 [NSCharacterSet newlineCharacterSet]].location != NSNotFound) { | 165 [NSCharacterSet newlineCharacterSet]].location != NSNotFound) { |
165 NSArray* chunks = [addressField componentsSeparatedByCharactersInSet: | 166 NSArray* chunks = [addressField componentsSeparatedByCharactersInSet: |
166 [NSCharacterSet newlineCharacterSet]]; | 167 [NSCharacterSet newlineCharacterSet]]; |
167 DCHECK([chunks count] > 1); | 168 DCHECK([chunks count] > 1); |
168 | 169 |
169 NSString* separator = l10n_util::GetNSString( | 170 NSString* separator = l10n_util::GetNSString( |
170 IDS_AUTOFILL_MAC_ADDRESS_LINE_SEPARATOR); | 171 IDS_AUTOFILL_MAC_ADDRESS_LINE_SEPARATOR); |
(...skipping 15 matching lines...) Expand all Loading... |
186 if (NSString* city = [address objectForKey:kABAddressCityKey]) | 187 if (NSString* city = [address objectForKey:kABAddressCityKey]) |
187 profile->SetRawInfo(ADDRESS_HOME_CITY, base::SysNSStringToUTF16(city)); | 188 profile->SetRawInfo(ADDRESS_HOME_CITY, base::SysNSStringToUTF16(city)); |
188 | 189 |
189 if (NSString* state = [address objectForKey:kABAddressStateKey]) | 190 if (NSString* state = [address objectForKey:kABAddressStateKey]) |
190 profile->SetRawInfo(ADDRESS_HOME_STATE, base::SysNSStringToUTF16(state)); | 191 profile->SetRawInfo(ADDRESS_HOME_STATE, base::SysNSStringToUTF16(state)); |
191 | 192 |
192 if (NSString* zip = [address objectForKey:kABAddressZIPKey]) | 193 if (NSString* zip = [address objectForKey:kABAddressZIPKey]) |
193 profile->SetRawInfo(ADDRESS_HOME_ZIP, base::SysNSStringToUTF16(zip)); | 194 profile->SetRawInfo(ADDRESS_HOME_ZIP, base::SysNSStringToUTF16(zip)); |
194 | 195 |
195 if (NSString* country = [address objectForKey:kABAddressCountryKey]) { | 196 if (NSString* country = [address objectForKey:kABAddressCountryKey]) { |
196 profile->SetRawInfo(ADDRESS_HOME_COUNTRY, | 197 profile->SetInfo(ADDRESS_HOME_COUNTRY, |
197 base::SysNSStringToUTF16(country)); | 198 base::SysNSStringToUTF16(country), |
| 199 AutofillCountry::ApplicationLocale()); |
198 } | 200 } |
199 } | 201 } |
200 | 202 |
201 // 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 |
202 // 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 |
203 // first we find (topmost) as preferred. | 205 // first we find (topmost) as preferred. |
204 void AuxiliaryProfilesImpl::GetAddressBookEmail( | 206 void AuxiliaryProfilesImpl::GetAddressBookEmail( |
205 ABPerson* me, | 207 ABPerson* me, |
206 NSString* addressLabelRaw, | 208 NSString* addressLabelRaw, |
207 AutofillProfile* profile) { | 209 AutofillProfile* profile) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } | 252 } |
251 } | 253 } |
252 | 254 |
253 } // namespace | 255 } // namespace |
254 | 256 |
255 // Populate |auxiliary_profiles_| with the Address Book data. | 257 // Populate |auxiliary_profiles_| with the Address Book data. |
256 void PersonalDataManager::LoadAuxiliaryProfiles() { | 258 void PersonalDataManager::LoadAuxiliaryProfiles() { |
257 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 259 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
258 impl.GetAddressBookMeCard(); | 260 impl.GetAddressBookMeCard(); |
259 } | 261 } |
OLD | NEW |