Chromium Code Reviews| 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/component_strings.h" | 28 #include "grit/component_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::kAutofillAuxiliaryProfilesQueried); | |
| 40 } | |
| 41 | |
| 42 ABAddressBook* GetAddressBook(PrefService* pref_service) { | |
| 43 bool first_access = !HasPromptedForAccessToAddressBook(pref_service); | |
|
Ilya Sherman
2014/05/23 15:47:10
nit: I'd leave a blank line after this one.
erikchen
2014/05/23 17:19:38
Done.
| |
| 44 // +[ABAddressBook sharedAddressBook] throws an exception internally in | |
| 45 // circumstances that aren't clear. The exceptions are only observed in crash | |
| 46 // reports, so it is unknown whether they would be caught by AppKit and nil | |
| 47 // returned, or if they would take down the app. In either case, avoid | |
| 48 // crashing. http://crbug.com/129022 | |
| 49 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions( | |
| 50 ^{ return [ABAddressBook sharedAddressBook]; }); | |
| 51 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); | |
| 52 | |
| 53 if (first_access) { | |
| 54 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", | |
| 55 addressBook != nil); | |
| 56 } | |
| 57 | |
| 58 pref_service->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); | |
| 59 return addressBook; | |
| 60 } | |
| 61 | |
| 34 // This implementation makes use of the Address Book API. Profiles are | 62 // 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 | 63 // 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 | 64 // 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" | 65 // the constructer and then initiate the fetch from the Mac Address Book "me" |
| 38 // card using the main |GetAddressBookMeCard()| method. This clears any | 66 // card using the main |GetAddressBookMeCard()| method. This clears any |
| 39 // existing addresses and populates new addresses derived from the data found | 67 // existing addresses and populates new addresses derived from the data found |
| 40 // in the "me" card. | 68 // in the "me" card. |
| 41 class AuxiliaryProfilesImpl { | 69 class AuxiliaryProfilesImpl { |
| 42 public: | 70 public: |
| 43 // Constructor takes a reference to the |profiles| that will be filled in | 71 // Constructor takes a reference to the |profiles| that will be filled in |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 74 }; | 102 }; |
| 75 | 103 |
| 76 // This method uses the |ABAddressBook| system service to fetch the "me" card | 104 // 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 | 105 // 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 | 106 // information and translates it to the internal list of |AutofillProfile| data |
| 79 // structures. | 107 // structures. |
| 80 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, | 108 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
| 81 PrefService* pref_service) { | 109 PrefService* pref_service) { |
| 82 profiles_.clear(); | 110 profiles_.clear(); |
| 83 | 111 |
| 84 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 112 // Chrome has not yet requested address book permissions. Attempting to do so |
| 85 // circumstances that aren't clear. The exceptions are only observed in crash | 113 // presents a blocking modal dialog, which is undesirable. Instead, just show |
| 86 // reports, so it is unknown whether they would be caught by AppKit and nil | 114 // no results. |
| 87 // returned, or if they would take down the app. In either case, avoid | 115 if (!HasPromptedForAccessToAddressBook(pref_service)) |
| 88 // crashing. http://crbug.com/129022 | 116 return; |
| 89 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ | 117 |
| 90 return [ABAddressBook sharedAddressBook]; | 118 ABAddressBook* addressBook = GetAddressBook(pref_service); |
| 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 | 119 |
| 99 ABPerson* me = [addressBook me]; | 120 ABPerson* me = [addressBook me]; |
| 100 if (!me) | 121 if (!me) |
| 101 return; | 122 return; |
| 102 | 123 |
| 103 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; | 124 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; |
| 104 | 125 |
| 105 // The number of characters at the end of the GUID to reserve for | 126 // 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 | 127 // 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 | 128 // 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 } | 295 } |
| 275 | 296 |
| 276 } // namespace | 297 } // namespace |
| 277 | 298 |
| 278 // Populate |auxiliary_profiles_| with the Address Book data. | 299 // Populate |auxiliary_profiles_| with the Address Book data. |
| 279 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 300 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
| 280 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 301 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
| 281 impl.GetAddressBookMeCard(app_locale_, pref_service_); | 302 impl.GetAddressBookMeCard(app_locale_, pref_service_); |
| 282 } | 303 } |
| 283 | 304 |
| 305 bool PersonalDataManager::AccessAddressBook() { | |
| 306 if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) | |
| 307 return false; | |
| 308 | |
| 309 if (HasPromptedForAccessToAddressBook(pref_service_)) | |
| 310 return false; | |
| 311 | |
| 312 // Request permissions. | |
| 313 GetAddressBook(pref_service_); | |
| 314 return true; | |
| 315 } | |
| 316 | |
| 317 bool PersonalDataManager::ShouldShowAccessAddressBookSuggestion( | |
| 318 AutofillType type) { | |
| 319 if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) | |
| 320 return false; | |
| 321 | |
| 322 if (HasPromptedForAccessToAddressBook(pref_service_)) | |
| 323 return false; | |
| 324 | |
| 325 switch (type.group()) { | |
| 326 case ADDRESS_BILLING: | |
| 327 case ADDRESS_HOME: | |
| 328 case EMAIL: | |
| 329 case NAME: | |
| 330 case NAME_BILLING: | |
| 331 case PHONE_BILLING: | |
| 332 case PHONE_HOME: | |
| 333 return true; | |
| 334 case NO_GROUP: | |
| 335 case COMPANY: | |
| 336 case CREDIT_CARD: | |
| 337 case PASSWORD_FIELD: | |
| 338 return false; | |
| 339 } | |
| 340 | |
| 341 return false; | |
| 342 } | |
| 343 | |
| 284 } // namespace autofill | 344 } // namespace autofill |
| OLD | NEW |