From 4cf0b84c8ee21b0a98c05058a006366faa571104 Mon Sep 17 00:00:00 2001 From: "jhawkins@chromium.org" Date: Tue, 16 Mar 2010 21:24:29 +0000 Subject: AutoFill: Load the imported form data into the AutoFill dialog when the user accepts the AutoFill InfoBar. Save a one-time initial profile to the WebDB if the user dismisses the InfoBar or the InfoBar closes. BUG=38104,38096 TEST=AutoFillInfoBarDelegateTest Review URL: http://codereview.chromium.org/974004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41765 0039d316-1c4b-4281-b951-d872f2087c98 --- .../browser/autofill/autofill_infobar_delegate.cc | 8 ++++-- .../autofill/autofill_infobar_delegate_unittest.cc | 10 ++++++-- chrome/browser/autofill/autofill_manager.cc | 29 +++++++++++++++++----- chrome/browser/autofill/autofill_manager.h | 8 ++++++ chrome/browser/autofill/personal_data_manager.cc | 29 +++++++++++++++++++++- chrome/browser/autofill/personal_data_manager.h | 4 +++ 6 files changed, 77 insertions(+), 11 deletions(-) (limited to 'chrome/browser/autofill') diff --git a/chrome/browser/autofill/autofill_infobar_delegate.cc b/chrome/browser/autofill/autofill_infobar_delegate.cc index e2c0648..05ffae4 100644 --- a/chrome/browser/autofill/autofill_infobar_delegate.cc +++ b/chrome/browser/autofill/autofill_infobar_delegate.cc @@ -39,7 +39,11 @@ bool AutoFillInfoBarDelegate::ShouldExpire( } void AutoFillInfoBarDelegate::InfoBarClosed() { - Cancel(); + if (host_) { + host_->OnInfoBarClosed(); + host_ = NULL; + } + // This will delete us. ConfirmInfoBarDelegate::InfoBarClosed(); } @@ -79,7 +83,7 @@ bool AutoFillInfoBarDelegate::Accept() { bool AutoFillInfoBarDelegate::Cancel() { if (host_) { - host_->Reset(); + host_->OnInfoBarCancelled(); host_ = NULL; } return true; diff --git a/chrome/browser/autofill/autofill_infobar_delegate_unittest.cc b/chrome/browser/autofill/autofill_infobar_delegate_unittest.cc index cc2b6705..c02a747 100644 --- a/chrome/browser/autofill/autofill_infobar_delegate_unittest.cc +++ b/chrome/browser/autofill/autofill_infobar_delegate_unittest.cc @@ -25,13 +25,19 @@ class MockAutoFillManager : public AutoFillManager { responded_(false), accepted_(false) {} + virtual void OnInfoBarClosed() { + EXPECT_FALSE(responded_); + responded_ = true; + accepted_ = true; + } + virtual void OnInfoBarAccepted() { EXPECT_FALSE(responded_); responded_ = true; accepted_ = true; } - virtual void Reset() { + virtual void OnInfoBarCancelled() { EXPECT_FALSE(responded_); responded_ = true; accepted_ = false; @@ -82,7 +88,7 @@ TEST_F(AutoFillInfoBarDelegateTest, ShouldExpire) { TEST_F(AutoFillInfoBarDelegateTest, InfoBarClosed) { infobar_->InfoBarClosed(); EXPECT_TRUE(autofill_manager_->responded()); - EXPECT_FALSE(autofill_manager_->accepted()); + EXPECT_TRUE(autofill_manager_->accepted()); } TEST_F(AutoFillInfoBarDelegateTest, GetMessageText) { diff --git a/chrome/browser/autofill/autofill_manager.cc b/chrome/browser/autofill/autofill_manager.cc index ee44c75..053a423f 100644 --- a/chrome/browser/autofill/autofill_manager.cc +++ b/chrome/browser/autofill/autofill_manager.cc @@ -47,7 +47,7 @@ void AutoFillManager::RegisterUserPrefs(PrefService* prefs) { void AutoFillManager::FormFieldValuesSubmitted( const webkit_glue::FormFieldValues& form) { - if (!personal_data_) + if (!IsAutoFillEnabled()) return; // Grab a copy of the form data. @@ -56,16 +56,16 @@ void AutoFillManager::FormFieldValuesSubmitted( if (!upload_form_structure_->IsAutoFillable()) return; - // Determine the possible field types. + // Determine the possible field types and upload the form structure to the + // PersonalDataManager. DeterminePossibleFieldTypes(upload_form_structure_.get()); + HandleSubmit(); PrefService* prefs = tab_contents_->profile()->GetPrefs(); bool infobar_shown = prefs->GetBoolean(prefs::kAutoFillInfoBarShown); if (!infobar_shown) { // Ask the user for permission to save form information. infobar_.reset(new AutoFillInfoBarDelegate(tab_contents_, this)); - } else if (IsAutoFillEnabled()) { - HandleSubmit(); } } @@ -218,8 +218,6 @@ void AutoFillManager::OnAutoFillDialogApply( // Save the personal data. personal_data_->SetProfiles(profiles); personal_data_->SetCreditCards(credit_cards); - - HandleSubmit(); } void AutoFillManager::OnPersonalDataLoaded() { @@ -236,12 +234,26 @@ void AutoFillManager::OnPersonalDataLoaded() { tab_contents_->profile()->GetOriginalProfile()); } +void AutoFillManager::OnInfoBarClosed() { + DCHECK(personal_data_); + + PrefService* prefs = tab_contents_->profile()->GetPrefs(); + prefs->SetBoolean(prefs::kAutoFillEnabled, true); + + // Save the imported form data as a profile. + personal_data_->SaveImportedFormData(); +} + void AutoFillManager::OnInfoBarAccepted() { DCHECK(personal_data_); PrefService* prefs = tab_contents_->profile()->GetPrefs(); prefs->SetBoolean(prefs::kAutoFillEnabled, true); + // This is the first time the user is interacting with AutoFill, so set the + // uploaded form structure as the initial profile in the AutoFillDialog. + personal_data_->SaveImportedFormData(); + // If the personal data manager has not loaded the data yet, set ourselves as // its observer so that we can listen for the OnPersonalDataLoaded signal. if (!personal_data_->IsDataLoaded()) @@ -250,6 +262,11 @@ void AutoFillManager::OnInfoBarAccepted() { OnPersonalDataLoaded(); } +void AutoFillManager::OnInfoBarCancelled() { + PrefService* prefs = tab_contents_->profile()->GetPrefs(); + prefs->SetBoolean(prefs::kAutoFillEnabled, false); +} + void AutoFillManager::Reset() { upload_form_structure_.reset(); form_structures_.reset(); diff --git a/chrome/browser/autofill/autofill_manager.h b/chrome/browser/autofill/autofill_manager.h index fa4593e..9a94e62 100644 --- a/chrome/browser/autofill/autofill_manager.h +++ b/chrome/browser/autofill/autofill_manager.h @@ -61,9 +61,15 @@ class AutoFillManager : public RenderViewHostDelegate::AutoFill, // PersonalDataManager::Observer implementation: virtual void OnPersonalDataLoaded(); + // Called by the AutoFillInfoBarDelegate when the user closes the infobar. + virtual void OnInfoBarClosed(); + // Called by the AutoFillInfoBarDelegate when the user accepts the infobar. virtual void OnInfoBarAccepted(); + // Called by the AutoFillInfoBarDelegate when the user cancels the infobar. + virtual void OnInfoBarCancelled(); + // Resets the stored form data. virtual void Reset(); @@ -95,6 +101,8 @@ class AutoFillManager : public RenderViewHostDelegate::AutoFill, // Our copy of the form data. ScopedVector form_structures_; + + // The form data the user has submitted. scoped_ptr upload_form_structure_; // The infobar that asks for permission to store form information. diff --git a/chrome/browser/autofill/personal_data_manager.cc b/chrome/browser/autofill/personal_data_manager.cc index 637933b..27a0923 100644 --- a/chrome/browser/autofill/personal_data_manager.cc +++ b/chrome/browser/autofill/personal_data_manager.cc @@ -17,9 +17,15 @@ #include "chrome/browser/pref_service.h" #include "chrome/common/pref_names.h" +namespace { + // The minimum number of fields that must contain user data and have known types // before autofill will attempt to import the data into a profile. -static const int kMinImportSize = 5; +const int kMinImportSize = 5; + +const char kUnlabeled[] = "Unlabeled"; + +} // namespace PersonalDataManager::~PersonalDataManager() { CancelPendingQuery(&pending_profiles_query_); @@ -201,6 +207,27 @@ bool PersonalDataManager::ImportFormData( return true; } +void PersonalDataManager::SaveImportedFormData() { + if (profile_->IsOffTheRecord()) + return; + + if (imported_profile_.get()) { + imported_profile_->set_label(ASCIIToUTF16(kUnlabeled)); + + std::vector profiles; + profiles.push_back(*imported_profile_); + SetProfiles(&profiles); + } + + if (imported_credit_card_.get()) { + imported_credit_card_->set_label(ASCIIToUTF16(kUnlabeled)); + + std::vector credit_cards; + credit_cards.push_back(*imported_credit_card_); + SetCreditCards(&credit_cards); + } +} + void PersonalDataManager::SetProfiles(std::vector* profiles) { if (profile_->IsOffTheRecord()) return; diff --git a/chrome/browser/autofill/personal_data_manager.h b/chrome/browser/autofill/personal_data_manager.h index 3384323..b30913d 100644 --- a/chrome/browser/autofill/personal_data_manager.h +++ b/chrome/browser/autofill/personal_data_manager.h @@ -63,6 +63,10 @@ class PersonalDataManager : public WebDataServiceConsumer, bool ImportFormData(const std::vector& form_structures, AutoFillManager* autofill_manager); + // Saves |imported_profile_| and |imported_credit_card_| to the WebDB if they + // exist. + void SaveImportedFormData(); + // Sets |web_profiles_| to the contents of |profiles| and updates the web // database by adding, updating and removing profiles. Sets the unique ID of // newly-added profiles. -- cgit v1.1