| 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 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "ui/base/l10n/l10n_util_mac.h" | 29 #include "ui/base/l10n/l10n_util_mac.h" |
| 30 | 30 |
| 31 namespace autofill { | 31 namespace autofill { |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 const char kAddressBookOrigin[] = "OS X Address Book"; | 34 const char kAddressBookOrigin[] = "OS X Address Book"; |
| 35 | 35 |
| 36 // Whether Chrome has prompted the user for permission to access the user's | 36 // Whether Chrome has prompted the user for permission to access the user's |
| 37 // address book. | 37 // address book. |
| 38 bool HasPromptedForAccessToAddressBook(PrefService* pref_service) { | 38 bool HasPromptedForAccessToAddressBook(PrefService* pref_service) { |
| 39 return pref_service->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried); | 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); |
| 40 } | 46 } |
| 41 | 47 |
| 42 ABAddressBook* GetAddressBook(PrefService* pref_service) { | 48 ABAddressBook* GetAddressBook(PrefService* pref_service) { |
| 43 bool first_access = !HasPromptedForAccessToAddressBook(pref_service); | 49 bool first_access = !HasPromptedForAccessToAddressBook(pref_service); |
| 44 | 50 |
| 45 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 51 // +[ABAddressBook sharedAddressBook] throws an exception internally in |
| 46 // circumstances that aren't clear. The exceptions are only observed in crash | 52 // circumstances that aren't clear. The exceptions are only observed in crash |
| 47 // reports, so it is unknown whether they would be caught by AppKit and nil | 53 // reports, so it is unknown whether they would be caught by AppKit and nil |
| 48 // returned, or if they would take down the app. In either case, avoid | 54 // returned, or if they would take down the app. In either case, avoid |
| 49 // crashing. http://crbug.com/129022 | 55 // crashing. http://crbug.com/129022 |
| 50 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions( | 56 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions( |
| 51 ^{ return [ABAddressBook sharedAddressBook]; }); | 57 ^{ return [ABAddressBook sharedAddressBook]; }); |
| 52 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); | 58 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); |
| 53 | 59 |
| 54 if (first_access) { | 60 if (first_access) { |
| 55 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", | 61 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", |
| 56 addressBook != nil); | 62 addressBook != nil); |
| 57 } | 63 } |
| 58 | 64 |
| 59 pref_service->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); | 65 pref_service->SetBoolean(prefs::kAutofillMacAddressBookQueried, true); |
| 60 return addressBook; | 66 return addressBook; |
| 61 } | 67 } |
| 62 | 68 |
| 63 // This implementation makes use of the Address Book API. Profiles are | 69 // This implementation makes use of the Address Book API. Profiles are |
| 64 // 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 |
| 65 // 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 |
| 66 // 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" |
| 67 // card using the main |GetAddressBookMeCard()| method. This clears any | 73 // card using the main |GetAddressBookMeCard()| method. This clears any |
| 68 // existing addresses and populates new addresses derived from the data found | 74 // existing addresses and populates new addresses derived from the data found |
| 69 // in the "me" card. | 75 // in the "me" card. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 }; | 109 }; |
| 104 | 110 |
| 105 // 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 |
| 106 // 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 |
| 107 // information and translates it to the internal list of |AutofillProfile| data | 113 // information and translates it to the internal list of |AutofillProfile| data |
| 108 // structures. | 114 // structures. |
| 109 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, | 115 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
| 110 PrefService* pref_service) { | 116 PrefService* pref_service) { |
| 111 profiles_.clear(); | 117 profiles_.clear(); |
| 112 | 118 |
| 113 // Chrome has not yet requested address book permissions. Attempting to do so | 119 // The user does not want Chrome to use the AddressBook to populate Autofill |
| 114 // presents a blocking modal dialog, which is undesirable. Instead, just show | 120 // entries. |
| 115 // no results. | 121 if (!ShouldUseAddressBook(pref_service)) |
| 116 if (!HasPromptedForAccessToAddressBook(pref_service)) | |
| 117 return; | 122 return; |
| 118 | 123 |
| 119 ABAddressBook* addressBook = GetAddressBook(pref_service); | 124 ABAddressBook* addressBook = GetAddressBook(pref_service); |
| 120 | 125 |
| 121 ABPerson* me = [addressBook me]; | 126 ABPerson* me = [addressBook me]; |
| 122 if (!me) | 127 if (!me) |
| 123 return; | 128 return; |
| 124 | 129 |
| 125 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; | 130 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; |
| 126 | 131 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 | 302 |
| 298 } // namespace | 303 } // namespace |
| 299 | 304 |
| 300 // Populate |auxiliary_profiles_| with the Address Book data. | 305 // Populate |auxiliary_profiles_| with the Address Book data. |
| 301 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 306 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
| 302 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 307 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
| 303 impl.GetAddressBookMeCard(app_locale_, pref_service_); | 308 impl.GetAddressBookMeCard(app_locale_, pref_service_); |
| 304 } | 309 } |
| 305 | 310 |
| 306 bool PersonalDataManager::AccessAddressBook() { | 311 bool PersonalDataManager::AccessAddressBook() { |
| 307 if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) | 312 // The user is attempting to give Chrome access to the user's Address Book. |
| 308 return false; | 313 // This implicitly acknowledges that the user wants to use auxiliary |
| 309 | 314 // profiles. |
| 310 if (HasPromptedForAccessToAddressBook(pref_service_)) | 315 pref_service_->SetBoolean(prefs::kAutofillUseMacAddressBook, true); |
| 311 return false; | |
| 312 | 316 |
| 313 // Request permissions. | 317 // Request permissions. |
| 314 GetAddressBook(pref_service_); | 318 GetAddressBook(pref_service_); |
| 315 return true; | 319 return true; |
| 316 } | 320 } |
| 317 | 321 |
| 318 bool PersonalDataManager::ShouldShowAccessAddressBookSuggestion( | 322 bool PersonalDataManager::ShouldShowAccessAddressBookSuggestion( |
| 319 AutofillType type) { | 323 AutofillType type) { |
| 320 if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) | |
| 321 return false; | |
| 322 | |
| 323 if (HasPromptedForAccessToAddressBook(pref_service_)) | 324 if (HasPromptedForAccessToAddressBook(pref_service_)) |
| 324 return false; | 325 return false; |
| 325 | 326 |
| 326 switch (type.group()) { | 327 switch (type.group()) { |
| 327 case ADDRESS_BILLING: | 328 case ADDRESS_BILLING: |
| 328 case ADDRESS_HOME: | 329 case ADDRESS_HOME: |
| 329 case EMAIL: | 330 case EMAIL: |
| 330 case NAME: | 331 case NAME: |
| 331 case NAME_BILLING: | 332 case NAME_BILLING: |
| 332 case PHONE_BILLING: | 333 case PHONE_BILLING: |
| 333 case PHONE_HOME: | 334 case PHONE_HOME: |
| 334 return true; | 335 return true; |
| 335 case NO_GROUP: | 336 case NO_GROUP: |
| 336 case COMPANY: | 337 case COMPANY: |
| 337 case CREDIT_CARD: | 338 case CREDIT_CARD: |
| 338 case PASSWORD_FIELD: | 339 case PASSWORD_FIELD: |
| 339 return false; | 340 return false; |
| 340 } | 341 } |
| 341 | 342 |
| 342 return false; | 343 return false; |
| 343 } | 344 } |
| 344 | 345 |
| 345 } // namespace autofill | 346 } // namespace autofill |
| OLD | NEW |