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 <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <iterator> | 9 #include <iterator> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "components/autofill/core/browser/autofill-inl.h" | 17 #include "components/autofill/core/browser/autofill-inl.h" |
18 #include "components/autofill/core/browser/autofill_country.h" | 18 #include "components/autofill/core/browser/autofill_country.h" |
19 #include "components/autofill/core/browser/autofill_field.h" | 19 #include "components/autofill/core/browser/autofill_field.h" |
20 #include "components/autofill/core/browser/autofill_metrics.h" | 20 #include "components/autofill/core/browser/autofill_metrics.h" |
21 #include "components/autofill/core/browser/form_structure.h" | 21 #include "components/autofill/core/browser/form_structure.h" |
22 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 22 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
23 #include "components/autofill/core/browser/phone_number.h" | 23 #include "components/autofill/core/browser/phone_number.h" |
24 #include "components/autofill/core/browser/phone_number_i18n.h" | 24 #include "components/autofill/core/browser/phone_number_i18n.h" |
25 #include "components/autofill/core/browser/validation.h" | 25 #include "components/autofill/core/browser/validation.h" |
26 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 26 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
27 #include "components/autofill/core/common/autofill_pref_names.h" | 27 #include "components/autofill/core/common/autofill_pref_names.h" |
28 #include "components/user_prefs/user_prefs.h" | |
29 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
30 | 29 |
31 namespace autofill { | 30 namespace autofill { |
32 namespace { | 31 namespace { |
33 | 32 |
34 const base::string16::value_type kCreditCardPrefix[] = {'*', 0}; | 33 const base::string16::value_type kCreditCardPrefix[] = {'*', 0}; |
35 | 34 |
36 template<typename T> | 35 template<typename T> |
37 class FormGroupMatchesByGUIDFunctor { | 36 class FormGroupMatchesByGUIDFunctor { |
38 public: | 37 public: |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 148 |
150 PersonalDataManager::PersonalDataManager(const std::string& app_locale) | 149 PersonalDataManager::PersonalDataManager(const std::string& app_locale) |
151 : browser_context_(NULL), | 150 : browser_context_(NULL), |
152 is_data_loaded_(false), | 151 is_data_loaded_(false), |
153 pending_profiles_query_(0), | 152 pending_profiles_query_(0), |
154 pending_creditcards_query_(0), | 153 pending_creditcards_query_(0), |
155 app_locale_(app_locale), | 154 app_locale_(app_locale), |
156 metric_logger_(new AutofillMetrics), | 155 metric_logger_(new AutofillMetrics), |
157 has_logged_profile_count_(false) {} | 156 has_logged_profile_count_(false) {} |
158 | 157 |
159 void PersonalDataManager::Init(content::BrowserContext* browser_context) { | 158 void PersonalDataManager::Init(content::BrowserContext* browser_context, |
| 159 PrefService* pref_service) { |
160 browser_context_ = browser_context; | 160 browser_context_ = browser_context; |
| 161 pref_service_ = pref_service; |
161 | 162 |
162 if (!browser_context_->IsOffTheRecord()) | 163 if (!browser_context_->IsOffTheRecord()) |
163 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); | 164 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); |
164 | 165 |
165 scoped_refptr<AutofillWebDataService> autofill_data( | 166 scoped_refptr<AutofillWebDataService> autofill_data( |
166 AutofillWebDataService::FromBrowserContext(browser_context_)); | 167 AutofillWebDataService::FromBrowserContext(browser_context_)); |
167 | 168 |
168 // WebDataService may not be available in tests. | 169 // WebDataService may not be available in tests. |
169 if (!autofill_data.get()) | 170 if (!autofill_data.get()) |
170 return; | 171 return; |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 iter != credit_cards_.end(); ++iter) { | 543 iter != credit_cards_.end(); ++iter) { |
543 (*iter)->GetNonEmptyTypes(app_locale_, non_empty_types); | 544 (*iter)->GetNonEmptyTypes(app_locale_, non_empty_types); |
544 } | 545 } |
545 } | 546 } |
546 | 547 |
547 bool PersonalDataManager::IsDataLoaded() const { | 548 bool PersonalDataManager::IsDataLoaded() const { |
548 return is_data_loaded_; | 549 return is_data_loaded_; |
549 } | 550 } |
550 | 551 |
551 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles() const { | 552 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles() const { |
552 if (!user_prefs::UserPrefs::Get(browser_context_)->GetBoolean( | 553 if (!pref_service_->GetBoolean(prefs::kAutofillAuxiliaryProfilesEnabled)) { |
553 prefs::kAutofillAuxiliaryProfilesEnabled)) { | |
554 return web_profiles(); | 554 return web_profiles(); |
555 } | 555 } |
556 | 556 |
557 profiles_.clear(); | 557 profiles_.clear(); |
558 | 558 |
559 // Populates |auxiliary_profiles_|. | 559 // Populates |auxiliary_profiles_|. |
560 LoadAuxiliaryProfiles(); | 560 LoadAuxiliaryProfiles(); |
561 | 561 |
562 profiles_.insert(profiles_.end(), web_profiles_.begin(), web_profiles_.end()); | 562 profiles_.insert(profiles_.end(), web_profiles_.begin(), web_profiles_.end()); |
563 profiles_.insert(profiles_.end(), | 563 profiles_.insert(profiles_.end(), |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 | 697 |
698 values->push_back(creditcard_field_value); | 698 values->push_back(creditcard_field_value); |
699 labels->push_back(label); | 699 labels->push_back(label); |
700 icons->push_back(UTF8ToUTF16(credit_card->type())); | 700 icons->push_back(UTF8ToUTF16(credit_card->type())); |
701 guid_pairs->push_back(GUIDPair(credit_card->guid(), 0)); | 701 guid_pairs->push_back(GUIDPair(credit_card->guid(), 0)); |
702 } | 702 } |
703 } | 703 } |
704 } | 704 } |
705 | 705 |
706 bool PersonalDataManager::IsAutofillEnabled() const { | 706 bool PersonalDataManager::IsAutofillEnabled() const { |
707 return user_prefs::UserPrefs::Get(browser_context_)->GetBoolean( | 707 return pref_service_->GetBoolean(prefs::kAutofillEnabled); |
708 prefs::kAutofillEnabled); | |
709 } | 708 } |
710 | 709 |
711 // static | 710 // static |
712 bool PersonalDataManager::IsValidLearnableProfile( | 711 bool PersonalDataManager::IsValidLearnableProfile( |
713 const AutofillProfile& profile, | 712 const AutofillProfile& profile, |
714 const std::string& app_locale) { | 713 const std::string& app_locale) { |
715 if (!IsMinimumAddress(profile, app_locale)) | 714 if (!IsMinimumAddress(profile, app_locale)) |
716 return false; | 715 return false; |
717 | 716 |
718 base::string16 email = profile.GetRawInfo(EMAIL_ADDRESS); | 717 base::string16 email = profile.GetRawInfo(EMAIL_ADDRESS); |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1043 void PersonalDataManager::set_metric_logger( | 1042 void PersonalDataManager::set_metric_logger( |
1044 const AutofillMetrics* metric_logger) { | 1043 const AutofillMetrics* metric_logger) { |
1045 metric_logger_.reset(metric_logger); | 1044 metric_logger_.reset(metric_logger); |
1046 } | 1045 } |
1047 | 1046 |
1048 void PersonalDataManager::set_browser_context( | 1047 void PersonalDataManager::set_browser_context( |
1049 content::BrowserContext* context) { | 1048 content::BrowserContext* context) { |
1050 browser_context_ = context; | 1049 browser_context_ = context; |
1051 } | 1050 } |
1052 | 1051 |
| 1052 void PersonalDataManager::set_pref_service(PrefService* pref_service) { |
| 1053 pref_service_ = pref_service; |
| 1054 } |
| 1055 |
1053 std::string PersonalDataManager::MostCommonCountryCodeFromProfiles() const { | 1056 std::string PersonalDataManager::MostCommonCountryCodeFromProfiles() const { |
1054 // Count up country codes from existing profiles. | 1057 // Count up country codes from existing profiles. |
1055 std::map<std::string, int> votes; | 1058 std::map<std::string, int> votes; |
1056 // TODO(estade): can we make this GetProfiles() instead? It seems to cause | 1059 // TODO(estade): can we make this GetProfiles() instead? It seems to cause |
1057 // errors in tests on mac trybots. See http://crbug.com/57221 | 1060 // errors in tests on mac trybots. See http://crbug.com/57221 |
1058 const std::vector<AutofillProfile*>& profiles = web_profiles(); | 1061 const std::vector<AutofillProfile*>& profiles = web_profiles(); |
1059 std::vector<std::string> country_codes; | 1062 std::vector<std::string> country_codes; |
1060 AutofillCountry::GetAvailableCountries(&country_codes); | 1063 AutofillCountry::GetAvailableCountries(&country_codes); |
1061 for (size_t i = 0; i < profiles.size(); ++i) { | 1064 for (size_t i = 0; i < profiles.size(); ++i) { |
1062 std::string country_code = StringToUpperASCII(UTF16ToASCII( | 1065 std::string country_code = StringToUpperASCII(UTF16ToASCII( |
(...skipping 10 matching lines...) Expand all Loading... |
1073 if (!votes.empty()) { | 1076 if (!votes.empty()) { |
1074 std::map<std::string, int>::iterator iter = | 1077 std::map<std::string, int>::iterator iter = |
1075 std::max_element(votes.begin(), votes.end(), CompareVotes); | 1078 std::max_element(votes.begin(), votes.end(), CompareVotes); |
1076 return iter->first; | 1079 return iter->first; |
1077 } | 1080 } |
1078 | 1081 |
1079 return std::string(); | 1082 return std::string(); |
1080 } | 1083 } |
1081 | 1084 |
1082 } // namespace autofill | 1085 } // namespace autofill |
OLD | NEW |