| 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: | |
| 40 std::string guid_; | |
| 41 }; | |
| 42 | |
| 43 template<typename T> | |
| 44 class FormGroupGUIDMatchesFunctor<T *> { | |
| 45 public: | |
| 46 explicit FormGroupGUIDMatchesFunctor(const std::string& guid) : guid_(guid) {} | |
| 47 | |
| 48 bool operator()(const T* form_group) { | 42 bool operator()(const T* form_group) { |
| 49 return form_group->guid() == guid_; | 43 return form_group->guid() == guid_; |
| 50 } | 44 } |
| 51 | 45 |
| 52 private: | 46 private: |
| 53 std::string guid_; | 47 std::string guid_; |
| 54 }; | 48 }; |
| 55 | 49 |
| 50 template<typename T, typename C> |
| 51 bool FindByGUID(const C& container, const std::string& guid) { |
| 52 return std::find_if( |
| 53 container.begin(), |
| 54 container.end(), |
| 55 FormGroupMatchesByGUIDFunctor<T>(guid)) != container.end(); |
| 56 } |
| 57 |
| 56 template<typename T> | 58 template<typename T> |
| 57 class DereferenceFunctor { | 59 class DereferenceFunctor { |
| 58 public: | 60 public: |
| 59 template<typename T_Iterator> | 61 template<typename T_Iterator> |
| 60 const T& operator()(const T_Iterator& iterator) { | 62 const T& operator()(const T_Iterator& iterator) { |
| 61 return *iterator; | 63 return *iterator; |
| 62 } | 64 } |
| 63 }; | 65 }; |
| 64 | 66 |
| 65 template<typename T> | 67 template<typename T> |
| 66 T* address_of(T& v) { | 68 T* address_of(T& v) { |
| 67 return &v; | 69 return &v; |
| 68 } | 70 } |
| 69 | 71 |
| 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 | 72 } // namespace |
| 77 | 73 |
| 78 PersonalDataManager::~PersonalDataManager() { | 74 PersonalDataManager::~PersonalDataManager() { |
| 79 CancelPendingQuery(&pending_profiles_query_); | 75 CancelPendingQuery(&pending_profiles_query_); |
| 80 CancelPendingQuery(&pending_creditcards_query_); | 76 CancelPendingQuery(&pending_creditcards_query_); |
| 81 } | 77 } |
| 82 | 78 |
| 83 void PersonalDataManager::OnWebDataServiceRequestDone( | 79 void PersonalDataManager::OnWebDataServiceRequestDone( |
| 84 WebDataService::Handle h, | 80 WebDataService::Handle h, |
| 85 const WDTypedResult* result) { | 81 const WDTypedResult* result) { |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 for (std::vector<AutoFillProfile*>::const_iterator iter = | 298 for (std::vector<AutoFillProfile*>::const_iterator iter = |
| 303 web_profiles_.begin(); | 299 web_profiles_.begin(); |
| 304 iter != web_profiles_.end(); ++iter) { | 300 iter != web_profiles_.end(); ++iter) { |
| 305 if (!FindByGUID<AutoFillProfile>(*profiles, (*iter)->guid())) | 301 if (!FindByGUID<AutoFillProfile>(*profiles, (*iter)->guid())) |
| 306 wds->RemoveAutoFillProfileGUID((*iter)->guid()); | 302 wds->RemoveAutoFillProfileGUID((*iter)->guid()); |
| 307 } | 303 } |
| 308 | 304 |
| 309 // Update the web database with the existing profiles. | 305 // Update the web database with the existing profiles. |
| 310 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 306 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 311 iter != profiles->end(); ++iter) { | 307 iter != profiles->end(); ++iter) { |
| 312 if (FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) | 308 if (FindByGUID<AutoFillProfile>(web_profiles_, iter->guid())) |
| 313 wds->UpdateAutoFillProfileGUID(*iter); | 309 wds->UpdateAutoFillProfileGUID(*iter); |
| 314 } | 310 } |
| 315 | 311 |
| 316 // Add the new profiles to the web database. | 312 // Add the new profiles to the web database. Don't add a duplicate. |
| 317 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 313 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 318 iter != profiles->end(); ++iter) { | 314 iter != profiles->end(); ++iter) { |
| 319 if (!FindByGUID<AutoFillProfile*>(web_profiles_, iter->guid())) | 315 if (!FindByGUID<AutoFillProfile>(web_profiles_, iter->guid()) && |
| 316 !FindByContents(web_profiles_, *iter)) |
| 320 wds->AddAutoFillProfileGUID(*iter); | 317 wds->AddAutoFillProfileGUID(*iter); |
| 321 } | 318 } |
| 322 | 319 |
| 323 // Copy in the new profiles. | 320 // Copy in the new profiles. |
| 324 web_profiles_.reset(); | 321 web_profiles_.reset(); |
| 325 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); | 322 for (std::vector<AutoFillProfile>::iterator iter = profiles->begin(); |
| 326 iter != profiles->end(); ++iter) { | 323 iter != profiles->end(); ++iter) { |
| 327 web_profiles_.push_back(new AutoFillProfile(*iter)); | 324 web_profiles_.push_back(new AutoFillProfile(*iter)); |
| 328 } | 325 } |
| 329 | 326 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 355 // removed. | 352 // removed. |
| 356 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); | 353 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); |
| 357 iter != credit_cards_.end(); ++iter) { | 354 iter != credit_cards_.end(); ++iter) { |
| 358 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid())) | 355 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid())) |
| 359 wds->RemoveCreditCardGUID((*iter)->guid()); | 356 wds->RemoveCreditCardGUID((*iter)->guid()); |
| 360 } | 357 } |
| 361 | 358 |
| 362 // Update the web database with the existing credit cards. | 359 // Update the web database with the existing credit cards. |
| 363 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 360 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
| 364 iter != credit_cards->end(); ++iter) { | 361 iter != credit_cards->end(); ++iter) { |
| 365 if (FindByGUID<CreditCard*>(credit_cards_, iter->guid())) | 362 if (FindByGUID<CreditCard>(credit_cards_, iter->guid())) |
| 366 wds->UpdateCreditCardGUID(*iter); | 363 wds->UpdateCreditCardGUID(*iter); |
| 367 } | 364 } |
| 368 | 365 |
| 369 // Add the new credit cards to the web database. | 366 // Add the new credit cards to the web database. Don't add a duplicate. |
| 370 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 367 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
| 371 iter != credit_cards->end(); ++iter) { | 368 iter != credit_cards->end(); ++iter) { |
| 372 if (!FindByGUID<CreditCard*>(credit_cards_, iter->guid())) | 369 if (!FindByGUID<CreditCard>(credit_cards_, iter->guid()) && |
| 370 !FindByContents(credit_cards_, *iter)) |
| 373 wds->AddCreditCardGUID(*iter); | 371 wds->AddCreditCardGUID(*iter); |
| 374 } | 372 } |
| 375 | 373 |
| 376 // Copy in the new credit cards. | 374 // Copy in the new credit cards. |
| 377 credit_cards_.reset(); | 375 credit_cards_.reset(); |
| 378 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); | 376 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); |
| 379 iter != credit_cards->end(); ++iter) { | 377 iter != credit_cards->end(); ++iter) { |
| 380 credit_cards_.push_back(new CreditCard(*iter)); | 378 credit_cards_.push_back(new CreditCard(*iter)); |
| 381 } | 379 } |
| 382 | 380 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 void PersonalDataManager::RemoveProfile(const std::string& guid) { | 470 void PersonalDataManager::RemoveProfile(const std::string& guid) { |
| 473 // TODO(jhawkins): Refactor SetProfiles so this isn't so hacky. | 471 // TODO(jhawkins): Refactor SetProfiles so this isn't so hacky. |
| 474 std::vector<AutoFillProfile> profiles(web_profiles_.size()); | 472 std::vector<AutoFillProfile> profiles(web_profiles_.size()); |
| 475 std::transform(web_profiles_.begin(), web_profiles_.end(), | 473 std::transform(web_profiles_.begin(), web_profiles_.end(), |
| 476 profiles.begin(), | 474 profiles.begin(), |
| 477 DereferenceFunctor<AutoFillProfile>()); | 475 DereferenceFunctor<AutoFillProfile>()); |
| 478 | 476 |
| 479 // Remove the profile that matches |guid|. | 477 // Remove the profile that matches |guid|. |
| 480 profiles.erase( | 478 profiles.erase( |
| 481 std::remove_if(profiles.begin(), profiles.end(), | 479 std::remove_if(profiles.begin(), profiles.end(), |
| 482 FormGroupGUIDMatchesFunctor<AutoFillProfile>(guid)), | 480 FormGroupMatchesByGUIDFunctor<AutoFillProfile>(guid)), |
| 483 profiles.end()); | 481 profiles.end()); |
| 484 | 482 |
| 485 SetProfiles(&profiles); | 483 SetProfiles(&profiles); |
| 486 } | 484 } |
| 487 | 485 |
| 488 AutoFillProfile* PersonalDataManager::GetProfileByGUID( | 486 AutoFillProfile* PersonalDataManager::GetProfileByGUID( |
| 489 const std::string& guid) { | 487 const std::string& guid) { |
| 490 for (std::vector<AutoFillProfile*>::iterator iter = web_profiles_->begin(); | 488 for (std::vector<AutoFillProfile*>::iterator iter = web_profiles_->begin(); |
| 491 iter != web_profiles_->end(); ++iter) { | 489 iter != web_profiles_->end(); ++iter) { |
| 492 if ((*iter)->guid() == guid) | 490 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) { | 526 void PersonalDataManager::RemoveCreditCard(const std::string& guid) { |
| 529 // TODO(jhawkins): Refactor SetCreditCards so this isn't so hacky. | 527 // TODO(jhawkins): Refactor SetCreditCards so this isn't so hacky. |
| 530 std::vector<CreditCard> credit_cards(credit_cards_.size()); | 528 std::vector<CreditCard> credit_cards(credit_cards_.size()); |
| 531 std::transform(credit_cards_.begin(), credit_cards_.end(), | 529 std::transform(credit_cards_.begin(), credit_cards_.end(), |
| 532 credit_cards.begin(), | 530 credit_cards.begin(), |
| 533 DereferenceFunctor<CreditCard>()); | 531 DereferenceFunctor<CreditCard>()); |
| 534 | 532 |
| 535 // Remove the credit card that matches |guid|. | 533 // Remove the credit card that matches |guid|. |
| 536 credit_cards.erase( | 534 credit_cards.erase( |
| 537 std::remove_if(credit_cards.begin(), credit_cards.end(), | 535 std::remove_if(credit_cards.begin(), credit_cards.end(), |
| 538 FormGroupGUIDMatchesFunctor<CreditCard>(guid)), | 536 FormGroupMatchesByGUIDFunctor<CreditCard>(guid)), |
| 539 credit_cards.end()); | 537 credit_cards.end()); |
| 540 | 538 |
| 541 SetCreditCards(&credit_cards); | 539 SetCreditCards(&credit_cards); |
| 542 } | 540 } |
| 543 | 541 |
| 544 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { | 542 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { |
| 545 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin(); | 543 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin(); |
| 546 iter != credit_cards_.end(); ++iter) { | 544 iter != credit_cards_.end(); ++iter) { |
| 547 if ((*iter)->guid() == guid) | 545 if ((*iter)->guid() == guid) |
| 548 return *iter; | 546 return *iter; |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 } | 792 } |
| 795 | 793 |
| 796 creditcards.push_back(**iter); | 794 creditcards.push_back(**iter); |
| 797 } | 795 } |
| 798 | 796 |
| 799 if (!merged) | 797 if (!merged) |
| 800 creditcards.push_back(*imported_credit_card_); | 798 creditcards.push_back(*imported_credit_card_); |
| 801 | 799 |
| 802 SetCreditCards(&creditcards); | 800 SetCreditCards(&creditcards); |
| 803 } | 801 } |
| OLD | NEW |