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 | |
34 // This implementation makes use of the Address Book API. Profiles are | 46 // 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 | 47 // 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 | 48 // 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" | 49 // the constructer and then initiate the fetch from the Mac Address Book "me" |
38 // card using the main |GetAddressBookMeCard()| method. This clears any | 50 // card using the main |GetAddressBookMeCard()| method. This clears any |
39 // existing addresses and populates new addresses derived from the data found | 51 // existing addresses and populates new addresses derived from the data found |
40 // in the "me" card. | 52 // in the "me" card. |
41 class AuxiliaryProfilesImpl { | 53 class AuxiliaryProfilesImpl { |
42 public: | 54 public: |
43 // Constructor takes a reference to the |profiles| that will be filled in | 55 // Constructor takes a reference to the |profiles| that will be filled in |
(...skipping 30 matching lines...) Expand all Loading... | |
74 }; | 86 }; |
75 | 87 |
76 // This method uses the |ABAddressBook| system service to fetch the "me" card | 88 // 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 | 89 // 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 | 90 // information and translates it to the internal list of |AutofillProfile| data |
79 // structures. | 91 // structures. |
80 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, | 92 void AuxiliaryProfilesImpl::GetAddressBookMeCard(const std::string& app_locale, |
81 PrefService* pref_service) { | 93 PrefService* pref_service) { |
82 profiles_.clear(); | 94 profiles_.clear(); |
83 | 95 |
84 // +[ABAddressBook sharedAddressBook] throws an exception internally in | 96 // 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 | 97 // 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 | 98 // no results. |
87 // returned, or if they would take down the app. In either case, avoid | 99 if (!pref_service->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried)) |
88 // crashing. http://crbug.com/129022 | 100 return; |
Ilya Sherman
2014/05/16 22:54:03
nit: Please call into HasAccessMacContacts() here
erikchen
2014/05/19 20:58:32
I've created an anonymous-namespace scoped functio
| |
89 ABAddressBook* addressBook = base::mac::RunBlockIgnoringExceptions(^{ | 101 |
90 return [ABAddressBook sharedAddressBook]; | 102 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 | 103 |
99 ABPerson* me = [addressBook me]; | 104 ABPerson* me = [addressBook me]; |
100 if (!me) | 105 if (!me) |
101 return; | 106 return; |
102 | 107 |
103 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; | 108 ABMultiValue* addresses = [me valueForProperty:kABAddressProperty]; |
104 | 109 |
105 // The number of characters at the end of the GUID to reserve for | 110 // 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 | 111 // 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 | 112 // 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 } | 279 } |
275 | 280 |
276 } // namespace | 281 } // namespace |
277 | 282 |
278 // Populate |auxiliary_profiles_| with the Address Book data. | 283 // Populate |auxiliary_profiles_| with the Address Book data. |
279 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 284 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
280 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); | 285 AuxiliaryProfilesImpl impl(&auxiliary_profiles_); |
281 impl.GetAddressBookMeCard(app_locale_, pref_service_); | 286 impl.GetAddressBookMeCard(app_locale_, pref_service_); |
282 } | 287 } |
283 | 288 |
289 bool PersonalDataManager::HasAccessMacContacts() { | |
290 return pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried); | |
291 } | |
292 | |
293 void PersonalDataManager::AccessMacContacts() { | |
294 // Immediately record that Chromium has requested permissions. | |
295 pref_service_->SetBoolean(prefs::kAutofillAuxiliaryProfilesQueried, true); | |
296 | |
297 // Request permissions. | |
298 ABAddressBook* addressBook = GetAddressBook(); | |
299 | |
300 if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesQueried)) { | |
Ilya Sherman
2014/05/16 22:54:03
This will always be false now, right?
erikchen
2014/05/19 20:58:32
Right you are. I've removed the conditional.
| |
301 UMA_HISTOGRAM_BOOLEAN("Autofill.AddressBookAvailableOnFirstAttempt", | |
302 addressBook != nil); | |
303 } | |
304 } | |
305 | |
284 } // namespace autofill | 306 } // namespace autofill |
OLD | NEW |