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" | |
14 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
17 #include "components/autofill/core/browser/autofill-inl.h" | 16 #include "components/autofill/core/browser/autofill-inl.h" |
18 #include "components/autofill/core/browser/autofill_country.h" | 17 #include "components/autofill/core/browser/autofill_country.h" |
19 #include "components/autofill/core/browser/autofill_field.h" | 18 #include "components/autofill/core/browser/autofill_field.h" |
20 #include "components/autofill/core/browser/autofill_metrics.h" | |
21 #include "components/autofill/core/browser/form_structure.h" | 19 #include "components/autofill/core/browser/form_structure.h" |
22 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 20 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
23 #include "components/autofill/core/browser/phone_number.h" | 21 #include "components/autofill/core/browser/phone_number.h" |
24 #include "components/autofill/core/browser/phone_number_i18n.h" | 22 #include "components/autofill/core/browser/phone_number_i18n.h" |
25 #include "components/autofill/core/browser/validation.h" | 23 #include "components/autofill/core/browser/validation.h" |
26 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | |
27 #include "components/autofill/core/common/autofill_pref_names.h" | 24 #include "components/autofill/core/common/autofill_pref_names.h" |
28 #include "content/public/browser/browser_context.h" | |
29 | 25 |
30 namespace autofill { | 26 namespace autofill { |
31 namespace { | 27 namespace { |
32 | 28 |
33 const base::string16::value_type kCreditCardPrefix[] = {'*', 0}; | 29 const base::string16::value_type kCreditCardPrefix[] = {'*', 0}; |
34 | 30 |
35 template<typename T> | 31 template<typename T> |
36 class FormGroupMatchesByGUIDFunctor { | 32 class FormGroupMatchesByGUIDFunctor { |
37 public: | 33 public: |
38 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) | 34 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 | 136 |
141 // A helper function for finding the maximum value in a string->int map. | 137 // A helper function for finding the maximum value in a string->int map. |
142 static bool CompareVotes(const std::pair<std::string, int>& a, | 138 static bool CompareVotes(const std::pair<std::string, int>& a, |
143 const std::pair<std::string, int>& b) { | 139 const std::pair<std::string, int>& b) { |
144 return a.second < b.second; | 140 return a.second < b.second; |
145 } | 141 } |
146 | 142 |
147 } // namespace | 143 } // namespace |
148 | 144 |
149 PersonalDataManager::PersonalDataManager(const std::string& app_locale) | 145 PersonalDataManager::PersonalDataManager(const std::string& app_locale) |
150 : browser_context_(NULL), | 146 : database_(NULL), |
151 is_data_loaded_(false), | 147 is_data_loaded_(false), |
152 pending_profiles_query_(0), | 148 pending_profiles_query_(0), |
153 pending_creditcards_query_(0), | 149 pending_creditcards_query_(0), |
154 app_locale_(app_locale), | 150 app_locale_(app_locale), |
155 metric_logger_(new AutofillMetrics), | 151 metric_logger_(new AutofillMetrics), |
156 is_off_the_record_(false), | 152 is_off_the_record_(false), |
157 has_logged_profile_count_(false) {} | 153 has_logged_profile_count_(false) {} |
158 | 154 |
159 void PersonalDataManager::Init(content::BrowserContext* browser_context, | 155 void PersonalDataManager::Init( |
160 PrefService* pref_service, | 156 scoped_refptr<AutofillWebDataService> database, |
Ilya Sherman
2013/11/14 23:45:55
nit: Looks like this doesn't need to wrap now.
blundell
2013/11/15 16:04:54
Done.
| |
161 bool is_off_the_record) { | 157 PrefService* pref_service, |
162 browser_context_ = browser_context; | 158 bool is_off_the_record) { |
159 database_ = database; | |
163 pref_service_ = pref_service; | 160 pref_service_ = pref_service; |
164 is_off_the_record_ = is_off_the_record; | 161 is_off_the_record_ = is_off_the_record; |
165 | 162 |
166 if (!is_off_the_record_) | 163 if (!is_off_the_record_) |
167 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); | 164 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); |
168 | 165 |
169 scoped_refptr<AutofillWebDataService> autofill_data( | |
170 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
171 | |
172 // WebDataService may not be available in tests. | 166 // WebDataService may not be available in tests. |
173 if (!autofill_data.get()) | 167 if (!database_.get()) |
174 return; | 168 return; |
175 | 169 |
176 LoadProfiles(); | 170 LoadProfiles(); |
177 LoadCreditCards(); | 171 LoadCreditCards(); |
178 | 172 |
179 autofill_data->AddObserver(this); | 173 database_->AddObserver(this); |
180 } | 174 } |
181 | 175 |
182 PersonalDataManager::~PersonalDataManager() { | 176 PersonalDataManager::~PersonalDataManager() { |
183 CancelPendingQuery(&pending_profiles_query_); | 177 CancelPendingQuery(&pending_profiles_query_); |
184 CancelPendingQuery(&pending_creditcards_query_); | 178 CancelPendingQuery(&pending_creditcards_query_); |
185 | 179 |
186 if (!browser_context_) | 180 if (database_.get()) |
187 return; | 181 database_->RemoveObserver(this); |
188 | |
189 scoped_refptr<AutofillWebDataService> autofill_data( | |
190 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
191 if (autofill_data.get()) | |
192 autofill_data->RemoveObserver(this); | |
193 } | 182 } |
194 | 183 |
195 void PersonalDataManager::OnWebDataServiceRequestDone( | 184 void PersonalDataManager::OnWebDataServiceRequestDone( |
196 WebDataServiceBase::Handle h, | 185 WebDataServiceBase::Handle h, |
197 const WDTypedResult* result) { | 186 const WDTypedResult* result) { |
198 DCHECK(pending_profiles_query_ || pending_creditcards_query_); | 187 DCHECK(pending_profiles_query_ || pending_creditcards_query_); |
199 | 188 |
200 if (!result) { | 189 if (!result) { |
201 // Error from the web database. | 190 // Error from the web database. |
202 if (h == pending_creditcards_query_) | 191 if (h == pending_creditcards_query_) |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 if (is_off_the_record_) | 377 if (is_off_the_record_) |
389 return; | 378 return; |
390 | 379 |
391 if (profile.IsEmpty(app_locale_)) | 380 if (profile.IsEmpty(app_locale_)) |
392 return; | 381 return; |
393 | 382 |
394 // Don't add an existing profile. | 383 // Don't add an existing profile. |
395 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) | 384 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) |
396 return; | 385 return; |
397 | 386 |
398 scoped_refptr<AutofillWebDataService> autofill_data( | 387 if (!database_.get()) |
399 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
400 if (!autofill_data.get()) | |
401 return; | 388 return; |
402 | 389 |
403 // Don't add a duplicate. | 390 // Don't add a duplicate. |
404 if (FindByContents(web_profiles_, profile)) | 391 if (FindByContents(web_profiles_, profile)) |
405 return; | 392 return; |
406 | 393 |
407 // Add the new profile to the web database. | 394 // Add the new profile to the web database. |
408 autofill_data->AddAutofillProfile(profile); | 395 database_->AddAutofillProfile(profile); |
409 | 396 |
410 // Refresh our local cache and send notifications to observers. | 397 // Refresh our local cache and send notifications to observers. |
411 Refresh(); | 398 Refresh(); |
412 } | 399 } |
413 | 400 |
414 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) { | 401 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) { |
415 if (is_off_the_record_) | 402 if (is_off_the_record_) |
416 return; | 403 return; |
417 | 404 |
418 AutofillProfile* existing_profile = GetProfileByGUID(profile.guid()); | 405 AutofillProfile* existing_profile = GetProfileByGUID(profile.guid()); |
419 if (!existing_profile) | 406 if (!existing_profile) |
420 return; | 407 return; |
421 | 408 |
422 // Don't overwrite the origin for a profile that is already stored. | 409 // Don't overwrite the origin for a profile that is already stored. |
423 if (existing_profile->Compare(profile) == 0) | 410 if (existing_profile->Compare(profile) == 0) |
424 return; | 411 return; |
425 | 412 |
426 if (profile.IsEmpty(app_locale_)) { | 413 if (profile.IsEmpty(app_locale_)) { |
427 RemoveByGUID(profile.guid()); | 414 RemoveByGUID(profile.guid()); |
428 return; | 415 return; |
429 } | 416 } |
430 | 417 |
431 scoped_refptr<AutofillWebDataService> autofill_data( | 418 if (!database_.get()) |
432 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
433 if (!autofill_data.get()) | |
434 return; | 419 return; |
435 | 420 |
436 // Make the update. | 421 // Make the update. |
437 autofill_data->UpdateAutofillProfile(profile); | 422 database_->UpdateAutofillProfile(profile); |
438 | 423 |
439 // Refresh our local cache and send notifications to observers. | 424 // Refresh our local cache and send notifications to observers. |
440 Refresh(); | 425 Refresh(); |
441 } | 426 } |
442 | 427 |
443 AutofillProfile* PersonalDataManager::GetProfileByGUID( | 428 AutofillProfile* PersonalDataManager::GetProfileByGUID( |
444 const std::string& guid) { | 429 const std::string& guid) { |
445 const std::vector<AutofillProfile*>& profiles = GetProfiles(); | 430 const std::vector<AutofillProfile*>& profiles = GetProfiles(); |
446 std::vector<AutofillProfile*>::const_iterator iter = | 431 std::vector<AutofillProfile*>::const_iterator iter = |
447 FindElementByGUID<AutofillProfile>(profiles, guid); | 432 FindElementByGUID<AutofillProfile>(profiles, guid); |
448 return (iter != profiles.end()) ? *iter : NULL; | 433 return (iter != profiles.end()) ? *iter : NULL; |
449 } | 434 } |
450 | 435 |
451 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) { | 436 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) { |
452 if (is_off_the_record_) | 437 if (is_off_the_record_) |
453 return; | 438 return; |
454 | 439 |
455 if (credit_card.IsEmpty(app_locale_)) | 440 if (credit_card.IsEmpty(app_locale_)) |
456 return; | 441 return; |
457 | 442 |
458 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) | 443 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) |
459 return; | 444 return; |
460 | 445 |
461 scoped_refptr<AutofillWebDataService> autofill_data( | 446 if (!database_.get()) |
462 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
463 if (!autofill_data.get()) | |
464 return; | 447 return; |
465 | 448 |
466 // Don't add a duplicate. | 449 // Don't add a duplicate. |
467 if (FindByContents(credit_cards_, credit_card)) | 450 if (FindByContents(credit_cards_, credit_card)) |
468 return; | 451 return; |
469 | 452 |
470 // Add the new credit card to the web database. | 453 // Add the new credit card to the web database. |
471 autofill_data->AddCreditCard(credit_card); | 454 database_->AddCreditCard(credit_card); |
472 | 455 |
473 // Refresh our local cache and send notifications to observers. | 456 // Refresh our local cache and send notifications to observers. |
474 Refresh(); | 457 Refresh(); |
475 } | 458 } |
476 | 459 |
477 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) { | 460 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) { |
478 if (is_off_the_record_) | 461 if (is_off_the_record_) |
479 return; | 462 return; |
480 | 463 |
481 CreditCard* existing_credit_card = GetCreditCardByGUID(credit_card.guid()); | 464 CreditCard* existing_credit_card = GetCreditCardByGUID(credit_card.guid()); |
482 if (!existing_credit_card) | 465 if (!existing_credit_card) |
483 return; | 466 return; |
484 | 467 |
485 // Don't overwrite the origin for a credit card that is already stored. | 468 // Don't overwrite the origin for a credit card that is already stored. |
486 if (existing_credit_card->Compare(credit_card) == 0) | 469 if (existing_credit_card->Compare(credit_card) == 0) |
487 return; | 470 return; |
488 | 471 |
489 if (credit_card.IsEmpty(app_locale_)) { | 472 if (credit_card.IsEmpty(app_locale_)) { |
490 RemoveByGUID(credit_card.guid()); | 473 RemoveByGUID(credit_card.guid()); |
491 return; | 474 return; |
492 } | 475 } |
493 | 476 |
494 scoped_refptr<AutofillWebDataService> autofill_data( | 477 if (!database_.get()) |
495 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
496 if (!autofill_data.get()) | |
497 return; | 478 return; |
498 | 479 |
499 // Make the update. | 480 // Make the update. |
500 autofill_data->UpdateCreditCard(credit_card); | 481 database_->UpdateCreditCard(credit_card); |
501 | 482 |
502 // Refresh our local cache and send notifications to observers. | 483 // Refresh our local cache and send notifications to observers. |
503 Refresh(); | 484 Refresh(); |
504 } | 485 } |
505 | 486 |
506 void PersonalDataManager::RemoveByGUID(const std::string& guid) { | 487 void PersonalDataManager::RemoveByGUID(const std::string& guid) { |
507 if (is_off_the_record_) | 488 if (is_off_the_record_) |
508 return; | 489 return; |
509 | 490 |
510 bool is_credit_card = FindByGUID<CreditCard>(credit_cards_, guid); | 491 bool is_credit_card = FindByGUID<CreditCard>(credit_cards_, guid); |
511 bool is_profile = !is_credit_card && | 492 bool is_profile = !is_credit_card && |
512 FindByGUID<AutofillProfile>(web_profiles_, guid); | 493 FindByGUID<AutofillProfile>(web_profiles_, guid); |
513 if (!is_credit_card && !is_profile) | 494 if (!is_credit_card && !is_profile) |
514 return; | 495 return; |
515 | 496 |
516 scoped_refptr<AutofillWebDataService> autofill_data( | 497 if (!database_.get()) |
517 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
518 if (!autofill_data.get()) | |
519 return; | 498 return; |
520 | 499 |
521 if (is_credit_card) | 500 if (is_credit_card) |
522 autofill_data->RemoveCreditCard(guid); | 501 database_->RemoveCreditCard(guid); |
523 else | 502 else |
524 autofill_data->RemoveAutofillProfile(guid); | 503 database_->RemoveAutofillProfile(guid); |
525 | 504 |
526 // Refresh our local cache and send notifications to observers. | 505 // Refresh our local cache and send notifications to observers. |
527 Refresh(); | 506 Refresh(); |
528 } | 507 } |
529 | 508 |
530 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { | 509 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { |
531 const std::vector<CreditCard*>& credit_cards = GetCreditCards(); | 510 const std::vector<CreditCard*>& credit_cards = GetCreditCards(); |
532 std::vector<CreditCard*>::const_iterator iter = | 511 std::vector<CreditCard*>::const_iterator iter = |
533 FindElementByGUID<CreditCard>(credit_cards, guid); | 512 FindElementByGUID<CreditCard>(credit_cards, guid); |
534 return (iter != credit_cards.end()) ? *iter : NULL; | 513 return (iter != credit_cards.end()) ? *iter : NULL; |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
797 | 776 |
798 // Ensure that profile labels are up to date. Currently, sync relies on | 777 // Ensure that profile labels are up to date. Currently, sync relies on |
799 // labels to identify a profile. | 778 // labels to identify a profile. |
800 // TODO(dhollowa): We need to deprecate labels and update the way sync | 779 // TODO(dhollowa): We need to deprecate labels and update the way sync |
801 // identifies profiles. | 780 // identifies profiles. |
802 std::vector<AutofillProfile*> profile_pointers(profiles->size()); | 781 std::vector<AutofillProfile*> profile_pointers(profiles->size()); |
803 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(), | 782 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(), |
804 address_of<AutofillProfile>); | 783 address_of<AutofillProfile>); |
805 AutofillProfile::AdjustInferredLabels(&profile_pointers); | 784 AutofillProfile::AdjustInferredLabels(&profile_pointers); |
806 | 785 |
807 scoped_refptr<AutofillWebDataService> autofill_data( | 786 if (!database_.get()) |
808 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
809 if (!autofill_data.get()) | |
810 return; | 787 return; |
811 | 788 |
812 // Any profiles that are not in the new profile list should be removed from | 789 // Any profiles that are not in the new profile list should be removed from |
813 // the web database. | 790 // the web database. |
814 for (std::vector<AutofillProfile*>::const_iterator iter = | 791 for (std::vector<AutofillProfile*>::const_iterator iter = |
815 web_profiles_.begin(); | 792 web_profiles_.begin(); |
816 iter != web_profiles_.end(); ++iter) { | 793 iter != web_profiles_.end(); ++iter) { |
817 if (!FindByGUID<AutofillProfile>(*profiles, (*iter)->guid())) | 794 if (!FindByGUID<AutofillProfile>(*profiles, (*iter)->guid())) |
818 autofill_data->RemoveAutofillProfile((*iter)->guid()); | 795 database_->RemoveAutofillProfile((*iter)->guid()); |
819 } | 796 } |
820 | 797 |
821 // Update the web database with the existing profiles. | 798 // Update the web database with the existing profiles. |
822 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); | 799 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); |
823 iter != profiles->end(); ++iter) { | 800 iter != profiles->end(); ++iter) { |
824 if (FindByGUID<AutofillProfile>(web_profiles_, iter->guid())) | 801 if (FindByGUID<AutofillProfile>(web_profiles_, iter->guid())) |
825 autofill_data->UpdateAutofillProfile(*iter); | 802 database_->UpdateAutofillProfile(*iter); |
826 } | 803 } |
827 | 804 |
828 // Add the new profiles to the web database. Don't add a duplicate. | 805 // Add the new profiles to the web database. Don't add a duplicate. |
829 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); | 806 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); |
830 iter != profiles->end(); ++iter) { | 807 iter != profiles->end(); ++iter) { |
831 if (!FindByGUID<AutofillProfile>(web_profiles_, iter->guid()) && | 808 if (!FindByGUID<AutofillProfile>(web_profiles_, iter->guid()) && |
832 !FindByContents(web_profiles_, *iter)) | 809 !FindByContents(web_profiles_, *iter)) |
833 autofill_data->AddAutofillProfile(*iter); | 810 database_->AddAutofillProfile(*iter); |
834 } | 811 } |
835 | 812 |
836 // Copy in the new profiles. | 813 // Copy in the new profiles. |
837 web_profiles_.clear(); | 814 web_profiles_.clear(); |
838 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); | 815 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); |
839 iter != profiles->end(); ++iter) { | 816 iter != profiles->end(); ++iter) { |
840 web_profiles_.push_back(new AutofillProfile(*iter)); | 817 web_profiles_.push_back(new AutofillProfile(*iter)); |
841 } | 818 } |
842 | 819 |
843 // Refresh our local cache and send notifications to observers. | 820 // Refresh our local cache and send notifications to observers. |
844 Refresh(); | 821 Refresh(); |
845 } | 822 } |
846 | 823 |
847 void PersonalDataManager::SetCreditCards( | 824 void PersonalDataManager::SetCreditCards( |
848 std::vector<CreditCard>* credit_cards) { | 825 std::vector<CreditCard>* credit_cards) { |
849 if (is_off_the_record_) | 826 if (is_off_the_record_) |
850 return; | 827 return; |
851 | 828 |
852 // Remove empty credit cards from input. | 829 // Remove empty credit cards from input. |
853 credit_cards->erase(std::remove_if(credit_cards->begin(), credit_cards->end(), | 830 credit_cards->erase(std::remove_if(credit_cards->begin(), credit_cards->end(), |
854 IsEmptyFunctor<CreditCard>(app_locale_)), | 831 IsEmptyFunctor<CreditCard>(app_locale_)), |
855 credit_cards->end()); | 832 credit_cards->end()); |
856 | 833 |
857 scoped_refptr<AutofillWebDataService> autofill_data( | 834 if (!database_.get()) |
858 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
859 if (!autofill_data.get()) | |
860 return; | 835 return; |
861 | 836 |
862 // Any credit cards that are not in the new credit card list should be | 837 // Any credit cards that are not in the new credit card list should be |
863 // removed. | 838 // removed. |
864 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); | 839 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); |
865 iter != credit_cards_.end(); ++iter) { | 840 iter != credit_cards_.end(); ++iter) { |
866 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid())) | 841 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid())) |
867 autofill_data->RemoveCreditCard((*iter)->guid()); | 842 database_->RemoveCreditCard((*iter)->guid()); |
868 } | 843 } |
869 | 844 |
870 // Update the web database with the existing credit cards. | 845 // Update the web database with the existing credit cards. |
871 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 846 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
872 iter != credit_cards->end(); ++iter) { | 847 iter != credit_cards->end(); ++iter) { |
873 if (FindByGUID<CreditCard>(credit_cards_, iter->guid())) | 848 if (FindByGUID<CreditCard>(credit_cards_, iter->guid())) |
874 autofill_data->UpdateCreditCard(*iter); | 849 database_->UpdateCreditCard(*iter); |
875 } | 850 } |
876 | 851 |
877 // Add the new credit cards to the web database. Don't add a duplicate. | 852 // Add the new credit cards to the web database. Don't add a duplicate. |
878 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 853 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
879 iter != credit_cards->end(); ++iter) { | 854 iter != credit_cards->end(); ++iter) { |
880 if (!FindByGUID<CreditCard>(credit_cards_, iter->guid()) && | 855 if (!FindByGUID<CreditCard>(credit_cards_, iter->guid()) && |
881 !FindByContents(credit_cards_, *iter)) | 856 !FindByContents(credit_cards_, *iter)) |
882 autofill_data->AddCreditCard(*iter); | 857 database_->AddCreditCard(*iter); |
883 } | 858 } |
884 | 859 |
885 // Copy in the new credit cards. | 860 // Copy in the new credit cards. |
886 credit_cards_.clear(); | 861 credit_cards_.clear(); |
887 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 862 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
888 iter != credit_cards->end(); ++iter) { | 863 iter != credit_cards->end(); ++iter) { |
889 credit_cards_.push_back(new CreditCard(*iter)); | 864 credit_cards_.push_back(new CreditCard(*iter)); |
890 } | 865 } |
891 | 866 |
892 // Refresh our local cache and send notifications to observers. | 867 // Refresh our local cache and send notifications to observers. |
893 Refresh(); | 868 Refresh(); |
894 } | 869 } |
895 | 870 |
896 void PersonalDataManager::LoadProfiles() { | 871 void PersonalDataManager::LoadProfiles() { |
897 scoped_refptr<AutofillWebDataService> autofill_data( | 872 if (!database_.get()) { |
898 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
899 if (!autofill_data.get()) { | |
900 NOTREACHED(); | 873 NOTREACHED(); |
901 return; | 874 return; |
902 } | 875 } |
903 | 876 |
904 CancelPendingQuery(&pending_profiles_query_); | 877 CancelPendingQuery(&pending_profiles_query_); |
905 | 878 |
906 pending_profiles_query_ = autofill_data->GetAutofillProfiles(this); | 879 pending_profiles_query_ = database_->GetAutofillProfiles(this); |
907 } | 880 } |
908 | 881 |
909 // Win and Linux implementations do nothing. Mac and Android implementations | 882 // Win and Linux implementations do nothing. Mac and Android implementations |
910 // fill in the contents of |auxiliary_profiles_|. | 883 // fill in the contents of |auxiliary_profiles_|. |
911 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 884 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
912 void PersonalDataManager::LoadAuxiliaryProfiles() const { | 885 void PersonalDataManager::LoadAuxiliaryProfiles() const { |
913 } | 886 } |
914 #endif | 887 #endif |
915 | 888 |
916 void PersonalDataManager::LoadCreditCards() { | 889 void PersonalDataManager::LoadCreditCards() { |
917 scoped_refptr<AutofillWebDataService> autofill_data( | 890 if (!database_.get()) { |
918 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
919 if (!autofill_data.get()) { | |
920 NOTREACHED(); | 891 NOTREACHED(); |
921 return; | 892 return; |
922 } | 893 } |
923 | 894 |
924 CancelPendingQuery(&pending_creditcards_query_); | 895 CancelPendingQuery(&pending_creditcards_query_); |
925 | 896 |
926 pending_creditcards_query_ = autofill_data->GetCreditCards(this); | 897 pending_creditcards_query_ = database_->GetCreditCards(this); |
927 } | 898 } |
928 | 899 |
929 void PersonalDataManager::ReceiveLoadedProfiles(WebDataServiceBase::Handle h, | 900 void PersonalDataManager::ReceiveLoadedProfiles(WebDataServiceBase::Handle h, |
930 const WDTypedResult* result) { | 901 const WDTypedResult* result) { |
931 DCHECK_EQ(pending_profiles_query_, h); | 902 DCHECK_EQ(pending_profiles_query_, h); |
932 | 903 |
933 pending_profiles_query_ = 0; | 904 pending_profiles_query_ = 0; |
934 web_profiles_.clear(); | 905 web_profiles_.clear(); |
935 | 906 |
936 const WDResult<std::vector<AutofillProfile*> >* r = | 907 const WDResult<std::vector<AutofillProfile*> >* r = |
(...skipping 21 matching lines...) Expand all Loading... | |
958 std::vector<CreditCard*> credit_cards = r->GetValue(); | 929 std::vector<CreditCard*> credit_cards = r->GetValue(); |
959 for (std::vector<CreditCard*>::iterator iter = credit_cards.begin(); | 930 for (std::vector<CreditCard*>::iterator iter = credit_cards.begin(); |
960 iter != credit_cards.end(); ++iter) { | 931 iter != credit_cards.end(); ++iter) { |
961 credit_cards_.push_back(*iter); | 932 credit_cards_.push_back(*iter); |
962 } | 933 } |
963 } | 934 } |
964 | 935 |
965 void PersonalDataManager::CancelPendingQuery( | 936 void PersonalDataManager::CancelPendingQuery( |
966 WebDataServiceBase::Handle* handle) { | 937 WebDataServiceBase::Handle* handle) { |
967 if (*handle) { | 938 if (*handle) { |
968 scoped_refptr<AutofillWebDataService> autofill_data( | 939 if (!database_.get()) { |
969 AutofillWebDataService::FromBrowserContext(browser_context_)); | |
970 if (!autofill_data.get()) { | |
971 NOTREACHED(); | 940 NOTREACHED(); |
972 return; | 941 return; |
973 } | 942 } |
974 autofill_data->CancelRequest(*handle); | 943 database_->CancelRequest(*handle); |
975 } | 944 } |
976 *handle = 0; | 945 *handle = 0; |
977 } | 946 } |
978 | 947 |
979 std::string PersonalDataManager::SaveImportedProfile( | 948 std::string PersonalDataManager::SaveImportedProfile( |
980 const AutofillProfile& imported_profile) { | 949 const AutofillProfile& imported_profile) { |
981 if (is_off_the_record_) | 950 if (is_off_the_record_) |
982 return std::string(); | 951 return std::string(); |
983 | 952 |
984 // Don't save a web profile if the data in the profile is a subset of an | 953 // Don't save a web profile if the data in the profile is a subset of an |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1031 return guid; | 1000 return guid; |
1032 } | 1001 } |
1033 | 1002 |
1034 void PersonalDataManager::LogProfileCount() const { | 1003 void PersonalDataManager::LogProfileCount() const { |
1035 if (!has_logged_profile_count_) { | 1004 if (!has_logged_profile_count_) { |
1036 metric_logger_->LogStoredProfileCount(web_profiles_.size()); | 1005 metric_logger_->LogStoredProfileCount(web_profiles_.size()); |
1037 has_logged_profile_count_ = true; | 1006 has_logged_profile_count_ = true; |
1038 } | 1007 } |
1039 } | 1008 } |
1040 | 1009 |
1041 const AutofillMetrics* PersonalDataManager::metric_logger() const { | |
1042 return metric_logger_.get(); | |
1043 } | |
1044 | |
1045 void PersonalDataManager::set_metric_logger( | |
1046 const AutofillMetrics* metric_logger) { | |
1047 metric_logger_.reset(metric_logger); | |
1048 } | |
1049 | |
1050 void PersonalDataManager::set_browser_context( | |
1051 content::BrowserContext* context) { | |
1052 browser_context_ = context; | |
1053 } | |
1054 | |
1055 void PersonalDataManager::set_pref_service(PrefService* pref_service) { | |
1056 pref_service_ = pref_service; | |
1057 } | |
1058 | |
1059 std::string PersonalDataManager::MostCommonCountryCodeFromProfiles() const { | 1010 std::string PersonalDataManager::MostCommonCountryCodeFromProfiles() const { |
1060 // Count up country codes from existing profiles. | 1011 // Count up country codes from existing profiles. |
1061 std::map<std::string, int> votes; | 1012 std::map<std::string, int> votes; |
1062 // TODO(estade): can we make this GetProfiles() instead? It seems to cause | 1013 // TODO(estade): can we make this GetProfiles() instead? It seems to cause |
1063 // errors in tests on mac trybots. See http://crbug.com/57221 | 1014 // errors in tests on mac trybots. See http://crbug.com/57221 |
1064 const std::vector<AutofillProfile*>& profiles = web_profiles(); | 1015 const std::vector<AutofillProfile*>& profiles = web_profiles(); |
1065 std::vector<std::string> country_codes; | 1016 std::vector<std::string> country_codes; |
1066 AutofillCountry::GetAvailableCountries(&country_codes); | 1017 AutofillCountry::GetAvailableCountries(&country_codes); |
1067 for (size_t i = 0; i < profiles.size(); ++i) { | 1018 for (size_t i = 0; i < profiles.size(); ++i) { |
1068 std::string country_code = StringToUpperASCII(UTF16ToASCII( | 1019 std::string country_code = StringToUpperASCII(UTF16ToASCII( |
(...skipping 10 matching lines...) Expand all Loading... | |
1079 if (!votes.empty()) { | 1030 if (!votes.empty()) { |
1080 std::map<std::string, int>::iterator iter = | 1031 std::map<std::string, int>::iterator iter = |
1081 std::max_element(votes.begin(), votes.end(), CompareVotes); | 1032 std::max_element(votes.begin(), votes.end(), CompareVotes); |
1082 return iter->first; | 1033 return iter->first; |
1083 } | 1034 } |
1084 | 1035 |
1085 return std::string(); | 1036 return std::string(); |
1086 } | 1037 } |
1087 | 1038 |
1088 } // namespace autofill | 1039 } // namespace autofill |
OLD | NEW |