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 13 matching lines...) Expand all Loading... | |
24 #include "components/autofill/core/browser/phone_number.h" | 24 #include "components/autofill/core/browser/phone_number.h" |
25 #include "components/autofill/core/common/autofill_pref_names.h" | 25 #include "components/autofill/core/common/autofill_pref_names.h" |
26 #include "grit/component_strings.h" | 26 #include "grit/component_strings.h" |
27 #include "ui/base/l10n/l10n_util_mac.h" | 27 #include "ui/base/l10n/l10n_util_mac.h" |
28 | 28 |
29 namespace autofill { | 29 namespace autofill { |
30 namespace { | 30 namespace { |
31 | 31 |
32 const char kAddressBookOrigin[] = "OS X Address Book"; | 32 const char kAddressBookOrigin[] = "OS X Address Book"; |
33 | 33 |
34 ABAddressBook* GetAddressBook() { | |
35 // +[ABAddressBook sharedAddressBook] throws an exception internally in | |
36 // circumstances that aren't clear. The exceptions are only observed in crash | |
37 // reports, so it is unknown whether they would be caught by AppKit and nil | |
38 // returned, or if they would take down the app. In either case, avoid | |
39 // crashing. http://crbug.com/129022 | |
40 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions( | |
41 ^{ return [ABAddressBook sharedAddressBook]; }); | |
42 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailable", addressBook != nil); | |
43 return addressBook; | |
44 } | |
45 | |
46 // Whether Chromium has prompted the user for permission to access the user's | |
47 // address book. | |
48 bool HasPromptedForAccessToAddressBook(PrefService* pref_service) { | |
49 return pref_service->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried); | |
50 } | |
51 | |
34 // This implementation makes use of the Address Book API. Profiles are | 52 // 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 | 53 // 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 | 54 // 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" | 55 // the constructer and then initiate the fetch from the Mac Address Book "me" |
38 // card using the main |GetAddressBookMeCard()| method. This clears any | 56 // card using the main |GetAddressBookMeCard()| method. This clears any |
39 // existing addresses and populates new addresses derived from the data found | 57 // existing addresses and populates new addresses derived from the data found |
40 // in the "me" card. | 58 // in the "me" card. |
41 class AuxiliaryProfilesImpl { | 59 class AuxiliaryProfilesImpl { |
42 public: | 60 public: |
43 // Constructor takes a reference to the |profiles| that will be filled in | 61 // Constructor takes a reference to the |profiles| that will be filled in |
(...skipping 30 matching lines...) Expand all Loading... | |
74 }; | 92 }; |
75 | 93 |
76 // This method uses the |ABAddressBook| system service to fetch the "me" card | 94 // 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 | 95 // 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 | 96 // information and translates it to the internal list of |AutofillProfile| data |
79 // structures. | 97 // structures. |
80 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, | 98 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
81 PrefService* pref_service) { | 99 PrefService* pref_service) { |
82 profiles_.clear(); | 100 profiles_.clear(); |
83 | 101 |
84 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 102 // 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 | 103 // 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 | 104 // no results. |
87 // returned, or if they would take down the app. In either case, avoid | 105 if (!HasPromptedForAccessToAddressBook(pref_service)) |
88 // crashing. http://crbug.com/129022 | 106 return; |
89 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ | 107 |
90 return [ABAddressBook sharedAddressBook]; | 108 ABAddressBook* addressBook = GetAddressBook(); |
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 | 109 |
99 ABPerson* me = [addressBook me]; | 110 ABPerson* me = [addressBook me]; |
100 if (!me) | 111 if (!me) |
101 return; | 112 return; |
102 | 113 |
103 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; | 114 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; |
104 | 115 |
105 // The number of characters at the end of the GUID to reserve for | 116 // 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 | 117 // 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 | 118 // 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 } | 285 } |
275 | 286 |
276 } // namespace | 287 } // namespace |
277 | 288 |
278 // Populate |auxiliary_profiles_| with the Address Book data. | 289 // Populate |auxiliary_profiles_| with the Address Book data. |
279 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 290 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
280 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 291 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
281 impl.GetAddressBookMeCard(app_locale_, pref_service_); | 292 impl.GetAddressBookMeCard(app_locale_, pref_service_); |
282 } | 293 } |
283 | 294 |
295 bool PersonalDataManager::HasPromptedForAccessToAddressBook() { | |
296 return ::autofill::HasPromptedForAccessToAddressBook(pref_service_); | |
297 } | |
298 | |
299 void PersonalDataManager::AccessAddressBook() { | |
300 // Immediately record that Chromium has requested permissions. | |
301 pref_service_->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); | |
Ilya Sherman
2014/05/21 11:29:15
I'd prefer that this remain in the GetAddressBook(
erikchen
2014/05/21 22:00:54
Good point. I've done as you suggested.
| |
302 | |
303 // Request permissions. | |
304 ABAddressBook* addressBook = GetAddressBook(); | |
305 | |
306 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", | |
307 addressBook != nil); | |
Ilya Sherman
2014/05/21 11:29:15
I'd prefer to be extra careful, and only set this
erikchen
2014/05/21 22:00:54
I've added a check for that pref value, as well as
| |
308 } | |
309 | |
284 } // namespace autofill | 310 } // namespace autofill |
OLD | NEW |