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" | 17 #include "base/metrics/histogram.h" |
18 #include "base/prefs/pref_service.h" | 18 #include "base/prefs/pref_service.h" |
19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
20 #include "base/strings/sys_string_conversions.h" | 20 #include "base/strings/sys_string_conversions.h" |
21 #include "components/autofill/core/browser/autofill_country.h" | 21 #include "components/autofill/core/browser/autofill_country.h" |
22 #include "components/autofill/core/browser/autofill_profile.h" | 22 #include "components/autofill/core/browser/autofill_profile.h" |
23 #include "components/autofill/core/browser/autofill_type.h" | 23 #include "components/autofill/core/browser/autofill_type.h" |
| 24 #include "components/autofill/core/browser/form_structure.h" |
24 #include "components/autofill/core/browser/phone_number.h" | 25 #include "components/autofill/core/browser/phone_number.h" |
25 #include "components/autofill/core/common/autofill_pref_names.h" | 26 #include "components/autofill/core/common/autofill_pref_names.h" |
| 27 #include "components/autofill/core/common/form_data.h" |
26 #include "grit/components_strings.h" | 28 #include "grit/components_strings.h" |
27 #include "ui/base/l10n/l10n_util_mac.h" | 29 #include "ui/base/l10n/l10n_util_mac.h" |
28 | 30 |
29 namespace autofill { | 31 namespace autofill { |
30 namespace { | 32 namespace { |
31 | 33 |
32 const char kAddressBookOrigin[] = "OS X Address Book"; | 34 const char kAddressBookOrigin[] = "OS X Address Book"; |
33 | 35 |
| 36 // Whether Chrome has prompted the user for permission to access the user's |
| 37 // address book. |
| 38 bool HasPromptedForAccessToAddressBook(PrefService* pref_service) { |
| 39 return pref_service->GetBoolean(prefs::kAutofillMacAddressBookQueried); |
| 40 } |
| 41 |
| 42 // Whether the user wants Chrome to use the AddressBook to populate Autofill |
| 43 // entries. |
| 44 bool ShouldUseAddressBook(PrefService* pref_service) { |
| 45 return pref_service->GetBoolean(prefs::kAutofillUseMacAddressBook); |
| 46 } |
| 47 |
| 48 ABAddressBook* GetAddressBook(PrefService* pref_service) { |
| 49 bool first_access = !HasPromptedForAccessToAddressBook(pref_service); |
| 50 |
| 51 // +[ABAddressBook sharedAddressBook] throws an exception internally in |
| 52 // circumstances that aren't clear. The exceptions are only observed in crash |
| 53 // reports, so it is unknown whether they would be caught by AppKit and nil |
| 54 // returned, or if they would take down the app. In either case, avoid |
| 55 // crashing. http://crbug.com/129022 |
| 56 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions( |
| 57 ^{ return [ABAddressBook sharedAddressBook]; }); |
| 58 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); |
| 59 |
| 60 if (first_access) { |
| 61 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", |
| 62 addressBook != nil); |
| 63 } |
| 64 |
| 65 pref_service->SetBoolean(prefs::kAutofillMacAddressBookQueried, true); |
| 66 return addressBook; |
| 67 } |
| 68 |
34 // This implementation makes use of the Address Book API. Profiles are | 69 // This implementation makes use of the Address Book API. Profiles are |
35 // generated that correspond to addresses in the "me" card that reside in the | 70 // generated that correspond to addresses in the "me" card that reside in the |
36 // user's Address Book. The caller passes a vector of profiles into the | 71 // user's Address Book. The caller passes a vector of profiles into the |
37 // the constructer and then initiate the fetch from the Mac Address Book "me" | 72 // the constructer and then initiate the fetch from the Mac Address Book "me" |
38 // card using the main |GetAddressBookMeCard()| method. This clears any | 73 // card using the main |GetAddressBookMeCard()| method. This clears any |
39 // existing addresses and populates new addresses derived from the data found | 74 // existing addresses and populates new addresses derived from the data found |
40 // in the "me" card. | 75 // in the "me" card. |
41 class AuxiliaryProfilesImpl { | 76 class AuxiliaryProfilesImpl { |
42 public: | 77 public: |
43 // Constructor takes a reference to the |profiles| that will be filled in | 78 // Constructor takes a reference to the |profiles| that will be filled in |
(...skipping 30 matching lines...) Expand all Loading... |
74 }; | 109 }; |
75 | 110 |
76 // This method uses the |ABAddressBook| system service to fetch the "me" card | 111 // This method uses the |ABAddressBook| system service to fetch the "me" card |
77 // from the active user's address book. It looks for the user address | 112 // from the active user's address book. It looks for the user address |
78 // information and translates it to the internal list of |AutofillProfile| data | 113 // information and translates it to the internal list of |AutofillProfile| data |
79 // structures. | 114 // structures. |
80 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, | 115 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
81 PrefService* pref_service) { | 116 PrefService* pref_service) { |
82 profiles_.clear(); | 117 profiles_.clear(); |
83 | 118 |
84 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 119 // The user does not want Chrome to use the AddressBook to populate Autofill |
85 // circumstances that aren't clear. The exceptions are only observed in crash | 120 // entries. |
86 // reports, so it is unknown whether they would be caught by AppKit and nil | 121 if (!ShouldUseAddressBook(pref_service)) |
87 // returned, or if they would take down the app. In either case, avoid | 122 return; |
88 // crashing. http://crbug.com/129022 | 123 |
89 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ | 124 ABAddressBook* addressBook = GetAddressBook(pref_service); |
90 return [ABAddressBook sharedAddressBook]; | |
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 | 125 |
99 ABPerson* me = [addressBook me]; | 126 ABPerson* me = [addressBook me]; |
100 if (!me) | 127 if (!me) |
101 return; | 128 return; |
102 | 129 |
103 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; | 130 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; |
104 | 131 |
105 // The number of characters at the end of the GUID to reserve for | 132 // The number of characters at the end of the GUID to reserve for |
106 // distinguishing addresses within the "me" card. Cap the number of addresses | 133 // distinguishing addresses within the "me" card. Cap the number of addresses |
107 // we will fetch to the number that can be distinguished by this fragment of | 134 // we will fetch to the number that can be distinguished by this fragment of |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } | 301 } |
275 | 302 |
276 } // namespace | 303 } // namespace |
277 | 304 |
278 // Populate |auxiliary_profiles_| with the Address Book data. | 305 // Populate |auxiliary_profiles_| with the Address Book data. |
279 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 306 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
280 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 307 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
281 impl.GetAddressBookMeCard(app_locale_, pref_service_); | 308 impl.GetAddressBookMeCard(app_locale_, pref_service_); |
282 } | 309 } |
283 | 310 |
| 311 bool PersonalDataManager::AccessAddressBook() { |
| 312 // The user is attempting to give Chrome access to the user's Address Book. |
| 313 // This implicitly acknowledges that the user wants to use auxiliary |
| 314 // profiles. |
| 315 pref_service_->SetBoolean(prefs::kAutofillUseMacAddressBook, true); |
| 316 |
| 317 // Request permissions. |
| 318 GetAddressBook(pref_service_); |
| 319 return true; |
| 320 } |
| 321 |
| 322 bool PersonalDataManager::ShouldShowAccessAddressBookSuggestion( |
| 323 AutofillType type) { |
| 324 if (HasPromptedForAccessToAddressBook(pref_service_)) |
| 325 return false; |
| 326 |
| 327 switch (type.group()) { |
| 328 case ADDRESS_BILLING: |
| 329 case ADDRESS_HOME: |
| 330 case EMAIL: |
| 331 case NAME: |
| 332 case NAME_BILLING: |
| 333 case PHONE_BILLING: |
| 334 case PHONE_HOME: |
| 335 return true; |
| 336 case NO_GROUP: |
| 337 case COMPANY: |
| 338 case CREDIT_CARD: |
| 339 case PASSWORD_FIELD: |
| 340 return false; |
| 341 } |
| 342 |
| 343 return false; |
| 344 } |
| 345 |
284 } // namespace autofill | 346 } // namespace autofill |
OLD | NEW |