Chromium Code Reviews| Index: components/autofill/core/browser/personal_data_manager_mac.mm |
| diff --git a/components/autofill/core/browser/personal_data_manager_mac.mm b/components/autofill/core/browser/personal_data_manager_mac.mm |
| index 745a60e6b99ce914b4adb2717f9138f7d183f683..725154fab3c4c1470b0f19038baf37580f637e74 100644 |
| --- a/components/autofill/core/browser/personal_data_manager_mac.mm |
| +++ b/components/autofill/core/browser/personal_data_manager_mac.mm |
| @@ -21,8 +21,10 @@ |
| #include "components/autofill/core/browser/autofill_country.h" |
| #include "components/autofill/core/browser/autofill_profile.h" |
| #include "components/autofill/core/browser/autofill_type.h" |
| +#include "components/autofill/core/browser/form_structure.h" |
| #include "components/autofill/core/browser/phone_number.h" |
| #include "components/autofill/core/common/autofill_pref_names.h" |
| +#include "components/autofill/core/common/form_data.h" |
| #include "grit/component_strings.h" |
| #include "ui/base/l10n/l10n_util_mac.h" |
| @@ -31,6 +33,25 @@ namespace { |
| const char kAddressBookOrigin[] = "OS X Address Book"; |
| +ABAddressBook* GetAddressBook(PrefService* pref_service) { |
| + // +[ABAddressBook sharedAddressBook] throws an exception internally in |
| + // circumstances that aren't clear. The exceptions are only observed in crash |
| + // reports, so it is unknown whether they would be caught by AppKit and nil |
| + // returned, or if they would take down the app. In either case, avoid |
| + // crashing. http://crbug.com/129022 |
| + ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions( |
| + ^{ return [ABAddressBook sharedAddressBook]; }); |
| + UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); |
| + pref_service->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); |
| + return addressBook; |
| +} |
| + |
| +// Whether Chrome has prompted the user for permission to access the user's |
| +// address book. |
| +bool HasPromptedForAccessToAddressBook(PrefService* pref_service) { |
| + return pref_service->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried); |
| +} |
| + |
| // This implementation makes use of the Address Book API. Profiles are |
| // generated that correspond to addresses in the "me" card that reside in the |
| // user's Address Book. The caller passes a vector of profiles into the |
| @@ -81,20 +102,13 @@ void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
| PrefService* pref_service) { |
| profiles_.clear(); |
| - // +[ABAddressBook sharedAddressBook] throws an exception internally in |
| - // circumstances that aren't clear. The exceptions are only observed in crash |
| - // reports, so it is unknown whether they would be caught by AppKit and nil |
| - // returned, or if they would take down the app. In either case, avoid |
| - // crashing. http://crbug.com/129022 |
| - ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ |
| - return [ABAddressBook sharedAddressBook]; |
| - }); |
| - UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); |
| - if (!pref_service->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried)) { |
| - pref_service->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); |
| - UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", |
| - addressBook != nil); |
| - } |
| + // Chrome has not yet requested address book permissions. Attempting to do so |
| + // presents a blocking modal dialog, which is undesirable. Instead, just show |
| + // no results. |
| + if (!HasPromptedForAccessToAddressBook(pref_service)) |
| + return; |
| + |
| + ABAddressBook* addressBook = GetAddressBook(pref_service); |
| ABPerson* me = [addressBook me]; |
| if (!me) |
| @@ -281,4 +295,62 @@ void PersonalDataManager::LoadAuxiliaryProfiles() const { |
| impl.GetAddressBookMeCard(app_locale_, pref_service_); |
| } |
| +bool PersonalDataManager::AccessAddressBook() { |
| + if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) |
| + return false; |
| + |
| + if (HasPromptedForAccessToAddressBook(pref_service_)) |
| + return false; |
| + |
| + // Request permissions. |
| + ABAddressBook* addressBook = GetAddressBook(pref_service_); |
|
erikchen
2014/05/21 22:00:54
It's possible for a user to mess with the configur
|
| + |
| + UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", |
| + addressBook != nil); |
|
Ilya Sherman
2014/05/22 15:13:59
I'd prefer for this to be in GetAddressBook() as w
erikchen
2014/05/22 20:40:59
Done.
|
| + return true; |
| +} |
| + |
| +bool PersonalDataManager::ShouldShowAccessAddressBookSuggestion( |
| + const FormData& data, |
| + const FormFieldData& field_data, |
| + const ScopedVector<FormStructure>& form_structures) { |
| + if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) |
| + return false; |
| + |
| + if (HasPromptedForAccessToAddressBook(pref_service_)) |
| + return false; |
| + |
| + for (ScopedVector<FormStructure>::const_iterator it = form_structures.begin(); |
| + it != form_structures.end(); |
| + ++it) { |
| + // The form does not match. |
| + if ((*it)->ToFormData().name != data.name) |
| + continue; |
| + for (size_t index = 0; index < (*it)->field_count(); ++index) { |
| + const AutofillField* field = (*it)->field(index); |
| + // The field does not match. |
| + if (field->name != field_data.name) |
| + continue; |
| + |
| + switch (field->Type().group()) { |
| + case ADDRESS_BILLING: |
| + case ADDRESS_HOME: |
| + case EMAIL: |
| + case NAME: |
| + case NAME_BILLING: |
| + case PHONE_BILLING: |
| + case PHONE_HOME: |
| + return true; |
| + case NO_GROUP: |
| + case COMPANY: |
| + case CREDIT_CARD: |
| + case PASSWORD_FIELD: |
| + return false; |
| + } |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| } // namespace autofill |