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/personal_data_manager.h" | 5 #include "components/autofill/core/browser/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/metrics/histogram.h" |
| 18 #include "base/prefs/pref_service.h" |
17 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
18 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
19 #include "components/autofill/core/browser/autofill_country.h" | 21 #include "components/autofill/core/browser/autofill_country.h" |
20 #include "components/autofill/core/browser/autofill_profile.h" | 22 #include "components/autofill/core/browser/autofill_profile.h" |
21 #include "components/autofill/core/browser/autofill_type.h" | 23 #include "components/autofill/core/browser/autofill_type.h" |
22 #include "components/autofill/core/browser/phone_number.h" | 24 #include "components/autofill/core/browser/phone_number.h" |
| 25 #include "components/autofill/core/common/autofill_pref_names.h" |
23 #include "grit/component_strings.h" | 26 #include "grit/component_strings.h" |
24 #include "ui/base/l10n/l10n_util_mac.h" | 27 #include "ui/base/l10n/l10n_util_mac.h" |
25 | 28 |
26 namespace autofill { | 29 namespace autofill { |
27 namespace { | 30 namespace { |
28 | 31 |
29 const char kAddressBookOrigin[] = "OS X Address Book"; | 32 const char kAddressBookOrigin[] = "OS X Address Book"; |
30 | 33 |
31 // This implementation makes use of the Address Book API. Profiles are | 34 // This implementation makes use of the Address Book API. Profiles are |
32 // generated that correspond to addresses in the "me" card that reside in the | 35 // generated that correspond to addresses in the "me" card that reside in the |
33 // user's Address Book. The caller passes a vector of profiles into the | 36 // user's Address Book. The caller passes a vector of profiles into the |
34 // the constructer and then initiate the fetch from the Mac Address Book "me" | 37 // the constructer and then initiate the fetch from the Mac Address Book "me" |
35 // card using the main |GetAddressBookMeCard()| method. This clears any | 38 // card using the main |GetAddressBookMeCard()| method. This clears any |
36 // existing addresses and populates new addresses derived from the data found | 39 // existing addresses and populates new addresses derived from the data found |
37 // in the "me" card. | 40 // in the "me" card. |
38 class AuxiliaryProfilesImpl { | 41 class AuxiliaryProfilesImpl { |
39 public: | 42 public: |
40 // Constructor takes a reference to the |profiles| that will be filled in | 43 // Constructor takes a reference to the |profiles| that will be filled in |
41 // by the subsequent call to |GetAddressBookMeCard()|. |profiles| may not | 44 // by the subsequent call to |GetAddressBookMeCard()|. |profiles| may not |
42 // be NULL. | 45 // be NULL. |
43 explicit AuxiliaryProfilesImpl(ScopedVector<AutofillProfile>* profiles) | 46 explicit AuxiliaryProfilesImpl(ScopedVector<AutofillProfile>* profiles) |
44 : profiles_(*profiles) { | 47 : profiles_(*profiles) { |
45 } | 48 } |
46 virtual ~AuxiliaryProfilesImpl() {} | 49 virtual ~AuxiliaryProfilesImpl() {} |
47 | 50 |
48 // Import the "me" card from the Mac Address Book and fill in |profiles_|. | 51 // Import the "me" card from the Mac Address Book and fill in |profiles_|. |
49 void GetAddressBookMeCard(const std::string& app_locale); | 52 void GetAddressBookMeCard(const std::string& app_locale, |
| 53 PrefService* pref_service); |
50 | 54 |
51 private: | 55 private: |
52 void GetAddressBookNames(ABPerson* me, | 56 void GetAddressBookNames(ABPerson* me, |
53 NSString* addressLabelRaw, | 57 NSString* addressLabelRaw, |
54 AutofillProfile* profile); | 58 AutofillProfile* profile); |
55 void GetAddressBookAddress(const std::string& app_locale, | 59 void GetAddressBookAddress(const std::string& app_locale, |
56 NSDictionary* address, | 60 NSDictionary* address, |
57 AutofillProfile* profile); | 61 AutofillProfile* profile); |
58 void GetAddressBookEmail(ABPerson* me, | 62 void GetAddressBookEmail(ABPerson* me, |
59 NSString* addressLabelRaw, | 63 NSString* addressLabelRaw, |
60 AutofillProfile* profile); | 64 AutofillProfile* profile); |
61 void GetAddressBookPhoneNumbers(ABPerson* me, | 65 void GetAddressBookPhoneNumbers(ABPerson* me, |
62 NSString* addressLabelRaw, | 66 NSString* addressLabelRaw, |
63 AutofillProfile* profile); | 67 AutofillProfile* profile); |
64 | 68 |
65 private: | 69 private: |
66 // A reference to the profiles this class populates. | 70 // A reference to the profiles this class populates. |
67 ScopedVector<AutofillProfile>& profiles_; | 71 ScopedVector<AutofillProfile>& profiles_; |
68 | 72 |
69 DISALLOW_COPY_AND_ASSIGN(AuxiliaryProfilesImpl); | 73 DISALLOW_COPY_AND_ASSIGN(AuxiliaryProfilesImpl); |
70 }; | 74 }; |
71 | 75 |
72 // This method uses the |ABAddressBook| system service to fetch the "me" card | 76 // This method uses the |ABAddressBook| system service to fetch the "me" card |
73 // from the active user's address book. It looks for the user address | 77 // from the active user's address book. It looks for the user address |
74 // information and translates it to the internal list of |AutofillProfile| data | 78 // information and translates it to the internal list of |AutofillProfile| data |
75 // structures. | 79 // structures. |
76 void AuxiliaryProfilesImpl::GetAddressBookMeCard( | 80 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
77 const std::string& app_locale) { | 81 PrefService* pref_service) { |
78 profiles_.clear(); | 82 profiles_.clear(); |
79 | 83 |
80 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 84 // +[ABAddressBook sharedAddressBook] throws an exception internally in |
81 // circumstances that aren't clear. The exceptions are only observed in crash | 85 // circumstances that aren't clear. The exceptions are only observed in crash |
82 // reports, so it is unknown whether they would be caught by AppKit and nil | 86 // reports, so it is unknown whether they would be caught by AppKit and nil |
83 // returned, or if they would take down the app. In either case, avoid | 87 // returned, or if they would take down the app. In either case, avoid |
84 // crashing. http://crbug.com/129022 | 88 // crashing. http://crbug.com/129022 |
85 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ | 89 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ |
86 return [ABAddressBook sharedAddressBook]; | 90 return [ABAddressBook sharedAddressBook]; |
87 }); | 91 }); |
| 92 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); |
| 93 if (!pref_service->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried)) { |
| 94 pref_service->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); |
| 95 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", |
| 96 addressBook != nil); |
| 97 } |
| 98 |
88 ABPerson* me = [addressBook me]; | 99 ABPerson* me = [addressBook me]; |
89 if (!me) | 100 if (!me) |
90 return; | 101 return; |
91 | 102 |
92 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; | 103 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; |
93 | 104 |
94 // The number of characters at the end of the GUID to reserve for | 105 // The number of characters at the end of the GUID to reserve for |
95 // distinguishing addresses within the "me" card. Cap the number of addresses | 106 // distinguishing addresses within the "me" card. Cap the number of addresses |
96 // we will fetch to the number that can be distinguished by this fragment of | 107 // we will fetch to the number that can be distinguished by this fragment of |
97 // the GUID. | 108 // the GUID. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, phone); | 271 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, phone); |
261 } | 272 } |
262 } | 273 } |
263 } | 274 } |
264 | 275 |
265 } // namespace | 276 } // namespace |
266 | 277 |
267 // Populate |auxiliary_profiles_| with the Address Book data. | 278 // Populate |auxiliary_profiles_| with the Address Book data. |
268 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 279 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
269 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 280 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
270 impl.GetAddressBookMeCard(app_locale_); | 281 impl.GetAddressBookMeCard(app_locale_, pref_service_); |
271 } | 282 } |
272 | 283 |
273 } // namespace autofill | 284 } // namespace autofill |
OLD | NEW |