Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autofill/personal_data_manager.h" | 5 #include "chrome/browser/autofill/personal_data_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/autofill/autofill_field.h" | 13 #include "chrome/browser/autofill/autofill_field.h" |
| 14 #include "chrome/browser/autofill/autofill-inl.h" | |
| 14 #include "chrome/browser/autofill/form_structure.h" | 15 #include "chrome/browser/autofill/form_structure.h" |
| 15 #include "chrome/browser/autofill/phone_number.h" | 16 #include "chrome/browser/autofill/phone_number.h" |
| 16 #include "chrome/browser/browser_thread.h" | 17 #include "chrome/browser/browser_thread.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/webdata/web_data_service.h" | 19 #include "chrome/browser/webdata/web_data_service.h" |
| 19 #include "chrome/browser/prefs/pref_service.h" | 20 #include "chrome/browser/prefs/pref_service.h" |
| 20 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // The minimum number of fields that must contain user data and have known types | 25 // The minimum number of fields that must contain user data and have known types |
| 25 // before AutoFill will attempt to import the data into a profile or a credit | 26 // before AutoFill will attempt to import the data into a profile or a credit |
| 26 // card. | 27 // card. |
| 27 const int kMinProfileImportSize = 3; | 28 const int kMinProfileImportSize = 3; |
| 28 const int kMinCreditCardImportSize = 2; | 29 const int kMinCreditCardImportSize = 2; |
| 29 | 30 |
| 30 template<typename T> | 31 template<typename T> |
| 31 class FormGroupGUIDMatchesFunctor { | 32 class FormGroupMatchesByGUIDFunctor { |
| 32 public: | 33 public: |
| 33 explicit FormGroupGUIDMatchesFunctor(const std::string& guid) : guid_(guid) {} | 34 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) |
| 35 : guid_(guid) { | |
| 36 } | |
| 34 | 37 |
| 35 bool operator()(const T& form_group) { | 38 bool operator()(const T& form_group) { |
| 36 return form_group.guid() == guid_; | 39 return form_group.guid() == guid_; |
| 37 } | 40 } |
| 38 | 41 |
| 39 private: | 42 private: |
| 40 std::string guid_; | 43 std::string guid_; |
| 41 }; | 44 }; |
| 42 | 45 |
| 43 template<typename T> | 46 template<typename T> |
| 44 class FormGroupGUIDMatchesFunctor<T *> { | 47 class FormGroupMatchesByGUIDFunctor<T *> { |
| 45 public: | 48 public: |
| 46 explicit FormGroupGUIDMatchesFunctor(const std::string& guid) : guid_(guid) {} | 49 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) |
| 50 : guid_(guid) { | |
| 51 } | |
| 47 | 52 |
| 48 bool operator()(const T* form_group) { | 53 bool operator()(const T* form_group) { |
| 49 return form_group->guid() == guid_; | 54 return form_group->guid() == guid_; |
| 50 } | 55 } |
| 51 | 56 |
| 52 private: | 57 private: |
| 53 std::string guid_; | 58 std::string guid_; |
| 54 }; | 59 }; |
|
Ilya Sherman
2010/12/20 23:41:32
nit: Can we similarly merge these two |FormGroupMa
dhollowa
2010/12/20 23:55:34
Done.
| |
| 55 | 60 |
| 61 template<typename T, typename C> | |
| 62 bool FindByGUID(const C& container, const std::string& guid) { | |
| 63 return std::find_if( | |
| 64 container.begin(), | |
| 65 container.end(), | |
| 66 FormGroupMatchesByGUIDFunctor<T>(guid)) != container.end(); | |
| 67 } | |
| 68 | |
| 56 template<typename T> | 69 template<typename T> |
| 57 class DereferenceFunctor { | 70 class DereferenceFunctor { |
| 58 public: | 71 public: |
| 59 template<typename T_Iterator> | 72 template<typename T_Iterator> |
| 60 const T& operator()(const T_Iterator& iterator) { | 73 const T& operator()(const T_Iterator& iterator) { |
| 61 return *iterator; | 74 return *iterator; |
| 62 } | 75 } |
| 63 }; | 76 }; |
| 64 | 77 |
| 65 template<typename T> | 78 template<typename T> |
| 66 T* address_of(T& v) { | 79 T* address_of(T& v) { |
| 67 return &v; | 80 return &v; |
| 68 } | 81 } |
| 69 | 82 |
| 70 template<typename T, typename C> | |
| 71 bool FindByGUID(const C& container, const std::string& guid) { | |
| 72 return std::find_if(container.begin(), container.end(), | |
| 73 FormGroupGUIDMatchesFunctor<T>(guid)) != container.end(); | |
| 74 } | |
| 75 | |
| 76 } // namespace | 83 } // namespace |
| 77 | 84 |
| 78 PersonalDataManager::~PersonalDataManager() { | 85 PersonalDataManager::~PersonalDataManager() { |
| 79 CancelPendingQuery(&pending_profiles_query_); | 86 CancelPendingQuery(&pending_profiles_query_); |
| 80 CancelPendingQuery(&pending_creditcards_query_); | 87 CancelPendingQuery(&pending_creditcards_query_); |
| 81 } | 88 } |
| 82 | 89 |
| 83 void PersonalDataManager::OnWebDataServiceRequestDone( | 90 void PersonalDataManager::OnWebDataServiceRequestDone( |
| 84 WebDataService::Handle h, | 91 WebDataService::Handle h, |
| 85 const WDTypedResult* result) { | 92 const WDTypedResult* result) { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 wds->RemoveAutoFillProfileGUID((*iter)->guid()); | 313 wds->RemoveAutoFillProfileGUID((*iter)->guid()); |
| 307 } | 314 } |
| 308 | 315 |
| 309 // Update the web database with the existing profiles. | 316 // Update the web database with the existing profiles. |
| 310 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 317 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 311 iter != profiles->end(); ++iter) { | 318 iter != profiles->end(); ++iter) { |
| 312 if (FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) | 319 if (FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) |
| 313 wds->UpdateAutoFillProfileGUID(*iter); | 320 wds->UpdateAutoFillProfileGUID(*iter); |
| 314 } | 321 } |
| 315 | 322 |
| 316 // Add the new profiles to the web database. | 323 // Add the new profiles to the web database. Don't add a duplicate. |
| 317 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 324 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 318 iter != profiles->end(); ++iter) { | 325 iter != profiles->end(); ++iter) { |
| 319 if (!FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) | 326 if (!FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid()) && |
| 327 !FindByContents(web_profiles_, *iter)) | |
| 320 wds->AddAutoFillProfileGUID(*iter); | 328 wds->AddAutoFillProfileGUID(*iter); |
| 321 } | 329 } |
| 322 | 330 |
| 323 // Copy in the new profiles. | 331 // Copy in the new profiles. |
| 324 web_profiles_.reset(); | 332 web_profiles_.reset(); |
| 325 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 333 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 326 iter != profiles->end(); ++iter) { | 334 iter != profiles->end(); ++iter) { |
| 327 web_profiles_.push_back(new AutoFillProfile(*iter)); | 335 web_profiles_.push_back(new AutoFillProfile(*iter)); |
| 328 } | 336 } |
| 329 | 337 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 359 wds->RemoveCreditCardGUID((*iter)->guid()); | 367 wds->RemoveCreditCardGUID((*iter)->guid()); |
| 360 } | 368 } |
| 361 | 369 |
| 362 // Update the web database with the existing credit cards. | 370 // Update the web database with the existing credit cards. |
| 363 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 371 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
| 364 iter != credit_cards->end(); ++iter) { | 372 iter != credit_cards->end(); ++iter) { |
| 365 if (FindByGUID<CreditCard*>(credit_cards_, iter->guid())) | 373 if (FindByGUID<CreditCard*>(credit_cards_, iter->guid())) |
| 366 wds->UpdateCreditCardGUID(*iter); | 374 wds->UpdateCreditCardGUID(*iter); |
| 367 } | 375 } |
| 368 | 376 |
| 369 // Add the new credit cards to the web database. | 377 // Add the new credit cards to the web database. Don't add a duplicate. |
| 370 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 378 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
| 371 iter != credit_cards->end(); ++iter) { | 379 iter != credit_cards->end(); ++iter) { |
| 372 if (!FindByGUID<CreditCard*>(credit_cards_, iter->guid())) | 380 if (!FindByGUID<CreditCard*>(credit_cards_, iter->guid()) && |
| 381 !FindByContents(credit_cards_, *iter)) | |
| 373 wds->AddCreditCardGUID(*iter); | 382 wds->AddCreditCardGUID(*iter); |
| 374 } | 383 } |
| 375 | 384 |
| 376 // Copy in the new credit cards. | 385 // Copy in the new credit cards. |
| 377 credit_cards_.reset(); | 386 credit_cards_.reset(); |
| 378 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 387 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
| 379 iter != credit_cards->end(); ++iter) { | 388 iter != credit_cards->end(); ++iter) { |
| 380 credit_cards_.push_back(new CreditCard(*iter)); | 389 credit_cards_.push_back(new CreditCard(*iter)); |
| 381 } | 390 } |
| 382 | 391 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 472 void PersonalDataManager::RemoveProfile(const std::string& guid) { | 481 void PersonalDataManager::RemoveProfile(const std::string& guid) { |
| 473 // TODO(jhawkins): Refactor SetProfiles so this isn't so hacky. | 482 // TODO(jhawkins): Refactor SetProfiles so this isn't so hacky. |
| 474 std::vector<AutoFillProfile> profiles(web_profiles_.size()); | 483 std::vector<AutoFillProfile> profiles(web_profiles_.size()); |
| 475 std::transform(web_profiles_.begin(), web_profiles_.end(), | 484 std::transform(web_profiles_.begin(), web_profiles_.end(), |
| 476 profiles.begin(), | 485 profiles.begin(), |
| 477 DereferenceFunctor<AutoFillProfile>()); | 486 DereferenceFunctor<AutoFillProfile>()); |
| 478 | 487 |
| 479 // Remove the profile that matches |guid|. | 488 // Remove the profile that matches |guid|. |
| 480 profiles.erase( | 489 profiles.erase( |
| 481 std::remove_if(profiles.begin(), profiles.end(), | 490 std::remove_if(profiles.begin(), profiles.end(), |
| 482 FormGroupGUIDMatchesFunctor<AutoFillProfile>(guid)), | 491 FormGroupMatchesByGUIDFunctor<AutoFillProfile>(guid)), |
| 483 profiles.end()); | 492 profiles.end()); |
| 484 | 493 |
| 485 SetProfiles(&profiles); | 494 SetProfiles(&profiles); |
| 486 } | 495 } |
| 487 | 496 |
| 488 AutoFillProfile* PersonalDataManager::GetProfileByGUID( | 497 AutoFillProfile* PersonalDataManager::GetProfileByGUID( |
| 489 const std::string& guid) { | 498 const std::string& guid) { |
| 490 for (std::vector<AutoFillProfile*>::iterator iter = web_profiles_->begin(); | 499 for (std::vector<AutoFillProfile*>::iterator iter = web_profiles_->begin(); |
| 491 iter != web_profiles_->end(); ++iter) { | 500 iter != web_profiles_->end(); ++iter) { |
| 492 if ((*iter)->guid() == guid) | 501 if ((*iter)->guid() == guid) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 528 void PersonalDataManager::RemoveCreditCard(const std::string& guid) { | 537 void PersonalDataManager::RemoveCreditCard(const std::string& guid) { |
| 529 // TODO(jhawkins): Refactor SetCreditCards so this isn't so hacky. | 538 // TODO(jhawkins): Refactor SetCreditCards so this isn't so hacky. |
| 530 std::vector<CreditCard> credit_cards(credit_cards_.size()); | 539 std::vector<CreditCard> credit_cards(credit_cards_.size()); |
| 531 std::transform(credit_cards_.begin(), credit_cards_.end(), | 540 std::transform(credit_cards_.begin(), credit_cards_.end(), |
| 532 credit_cards.begin(), | 541 credit_cards.begin(), |
| 533 DereferenceFunctor<CreditCard>()); | 542 DereferenceFunctor<CreditCard>()); |
| 534 | 543 |
| 535 // Remove the credit card that matches |guid|. | 544 // Remove the credit card that matches |guid|. |
| 536 credit_cards.erase( | 545 credit_cards.erase( |
| 537 std::remove_if(credit_cards.begin(), credit_cards.end(), | 546 std::remove_if(credit_cards.begin(), credit_cards.end(), |
| 538 FormGroupGUIDMatchesFunctor<CreditCard>(guid)), | 547 FormGroupMatchesByGUIDFunctor<CreditCard>(guid)), |
| 539 credit_cards.end()); | 548 credit_cards.end()); |
| 540 | 549 |
| 541 SetCreditCards(&credit_cards); | 550 SetCreditCards(&credit_cards); |
| 542 } | 551 } |
| 543 | 552 |
| 544 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { | 553 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { |
| 545 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin(); | 554 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin(); |
| 546 iter != credit_cards_.end(); ++iter) { | 555 iter != credit_cards_.end(); ++iter) { |
| 547 if ((*iter)->guid() == guid) | 556 if ((*iter)->guid() == guid) |
| 548 return *iter; | 557 return *iter; |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 794 } | 803 } |
| 795 | 804 |
| 796 creditcards.push_back(**iter); | 805 creditcards.push_back(**iter); |
| 797 } | 806 } |
| 798 | 807 |
| 799 if (!merged) | 808 if (!merged) |
| 800 creditcards.push_back(*imported_credit_card_); | 809 creditcards.push_back(*imported_credit_card_); |
| 801 | 810 |
| 802 SetCreditCards(&creditcards); | 811 SetCreditCards(&creditcards); |
| 803 } | 812 } |
| OLD | NEW |