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