diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-24 21:33:01 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-24 21:33:01 +0000 |
commit | b5e919caf5ff078335774a94e9ec28b66d0f3fe3 (patch) | |
tree | f1c7845ecac5be3e0f237ec3cc28be7c5f0576f4 /chrome/browser | |
parent | a684921f6f193d14e19fff8e6a4f506b39c6dd25 (diff) | |
download | chromium_src-b5e919caf5ff078335774a94e9ec28b66d0f3fe3.zip chromium_src-b5e919caf5ff078335774a94e9ec28b66d0f3fe3.tar.gz chromium_src-b5e919caf5ff078335774a94e9ec28b66d0f3fe3.tar.bz2 |
DOMUI: Implement the 'Remove...' button on the AutoFill page.
BUG=49094
TEST=none
Review URL: http://codereview.chromium.org/3140026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57238 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/autofill/personal_data_manager.cc | 56 | ||||
-rw-r--r-- | chrome/browser/autofill/personal_data_manager.h | 6 | ||||
-rw-r--r-- | chrome/browser/dom_ui/autofill_options_handler.cc | 35 | ||||
-rw-r--r-- | chrome/browser/dom_ui/autofill_options_handler.h | 8 | ||||
-rw-r--r-- | chrome/browser/resources/options/autofill_options.js | 83 |
5 files changed, 176 insertions, 12 deletions
diff --git a/chrome/browser/autofill/personal_data_manager.cc b/chrome/browser/autofill/personal_data_manager.cc index a058b78..9e42981 100644 --- a/chrome/browser/autofill/personal_data_manager.cc +++ b/chrome/browser/autofill/personal_data_manager.cc @@ -28,6 +28,28 @@ const int kMinImportSize = 3; const char kUnlabeled[] = "Unlabeled"; +template<typename T> +class FormGroupIDMatchesFunctor { + public: + explicit FormGroupIDMatchesFunctor(int id) : id_(id) {} + + bool operator()(const T& form_group) { + return form_group.unique_id() == id_; + } + + private: + int id_; +}; + +template<typename T> +class DereferenceFunctor { + public: + template<typename T_Iterator> + const T& operator()(const T_Iterator& iterator) { + return *iterator; + } +}; + } // namespace PersonalDataManager::~PersonalDataManager() { @@ -83,7 +105,7 @@ void PersonalDataManager::OnAutoFillDialogApply( } void PersonalDataManager::SetObserver(PersonalDataManager::Observer* observer) { - // TODO: RemoveObserver is for compatability with old code, it should be + // TODO: RemoveObserver is for compatibility with old code, it should be // nuked. observers_.RemoveObserver(observer); observers_.AddObserver(observer); @@ -364,6 +386,38 @@ void PersonalDataManager::SetCreditCards( FOR_EACH_OBSERVER(Observer, observers_, OnPersonalDataChanged()); } +void PersonalDataManager::RemoveProfile(int unique_id) { + // TODO(jhawkins): Refactor SetProfiles so this isn't so hacky. + std::vector<AutoFillProfile> profiles(web_profiles_.size()); + std::transform(web_profiles_.begin(), web_profiles_.end(), + profiles.begin(), + DereferenceFunctor<AutoFillProfile>()); + + // Remove the profile that matches |unique_id|. + profiles.erase( + std::remove_if(profiles.begin(), profiles.end(), + FormGroupIDMatchesFunctor<AutoFillProfile>(unique_id)), + profiles.end()); + + SetProfiles(&profiles); +} + +void PersonalDataManager::RemoveCreditCard(int unique_id) { + // TODO(jhawkins): Refactor SetCreditCards so this isn't so hacky. + std::vector<CreditCard> credit_cards(credit_cards_.size()); + std::transform(credit_cards_.begin(), credit_cards_.end(), + credit_cards.begin(), + DereferenceFunctor<CreditCard>()); + + // Remove the credit card that matches |unique_id|. + credit_cards.erase( + std::remove_if(credit_cards.begin(), credit_cards.end(), + FormGroupIDMatchesFunctor<CreditCard>(unique_id)), + credit_cards.end()); + + SetCreditCards(&credit_cards); +} + void PersonalDataManager::GetPossibleFieldTypes(const string16& text, FieldTypeSet* possible_types) { string16 clean_info = StringToLowerASCII(CollapseWhitespace(text, false)); diff --git a/chrome/browser/autofill/personal_data_manager.h b/chrome/browser/autofill/personal_data_manager.h index e923fa7..e3c9e92 100644 --- a/chrome/browser/autofill/personal_data_manager.h +++ b/chrome/browser/autofill/personal_data_manager.h @@ -100,6 +100,12 @@ class PersonalDataManager // ID of newly-added profiles. void SetCreditCards(std::vector<CreditCard>* credit_cards); + // Removes the profile represented by |unique_id|. + void RemoveProfile(int unique_id); + + // Removes the credit card represented by |unique_id|. + void RemoveCreditCard(int unique_id); + // Gets the possible field types for the given text, determined by matching // the text with all known personal information and returning matching types. void GetPossibleFieldTypes(const string16& text, diff --git a/chrome/browser/dom_ui/autofill_options_handler.cc b/chrome/browser/dom_ui/autofill_options_handler.cc index 689ea83..4ae032c 100644 --- a/chrome/browser/dom_ui/autofill_options_handler.cc +++ b/chrome/browser/dom_ui/autofill_options_handler.cc @@ -72,6 +72,13 @@ void AutoFillOptionsHandler::OnPersonalDataChanged() { } void AutoFillOptionsHandler::RegisterMessages() { + dom_ui_->RegisterMessageCallback( + "removeAddress", + NewCallback(this, &AutoFillOptionsHandler::RemoveAddress)); + + dom_ui_->RegisterMessageCallback( + "removeCreditCard", + NewCallback(this, &AutoFillOptionsHandler::RemoveCreditCard)); } void AutoFillOptionsHandler::LoadAutoFillData() { @@ -84,6 +91,7 @@ void AutoFillOptionsHandler::LoadAutoFillData() { i != personal_data_->profiles().end(); ++i) { DictionaryValue* address = new DictionaryValue(); address->SetString("label", (*i)->PreviewSummary()); + address->SetInteger("unique_id", (*i)->unique_id()); addresses.Append(address); } @@ -96,9 +104,36 @@ void AutoFillOptionsHandler::LoadAutoFillData() { i != personal_data_->credit_cards().end(); ++i) { DictionaryValue* credit_card = new DictionaryValue(); credit_card->SetString("label", (*i)->PreviewSummary()); + credit_card->SetInteger("unique_id", (*i)->unique_id()); credit_cards.Append(credit_card); } dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateCreditCards", credit_cards); } + +void AutoFillOptionsHandler::RemoveAddress(const ListValue* args) { + if (!personal_data_->IsDataLoaded()) + return; + + int unique_id = 0; + if (!ExtractIntegerValue(args, &unique_id)) { + NOTREACHED(); + return; + } + + personal_data_->RemoveProfile(unique_id); +} + +void AutoFillOptionsHandler::RemoveCreditCard(const ListValue* args) { + if (!personal_data_->IsDataLoaded()) + return; + + int unique_id = 0; + if (!ExtractIntegerValue(args, &unique_id)) { + NOTREACHED(); + return; + } + + personal_data_->RemoveCreditCard(unique_id); +} diff --git a/chrome/browser/dom_ui/autofill_options_handler.h b/chrome/browser/dom_ui/autofill_options_handler.h index 247eaef..73a1074 100644 --- a/chrome/browser/dom_ui/autofill_options_handler.h +++ b/chrome/browser/dom_ui/autofill_options_handler.h @@ -28,6 +28,14 @@ class AutoFillOptionsHandler : public OptionsPageUIHandler, // Loads AutoFill addresses and credit cards using the PersonalDataManager. void LoadAutoFillData(); + // Removes an address from the WebDatabase. Called from DOMUI. + // |args| - an integer, the unique ID of the address to remove. + void RemoveAddress(const ListValue* args); + + // Removes a credit card from the WebDatabase. Called from DOMUI. + // |args| - an integer, the unique ID of the credit card to remove. + void RemoveCreditCard(const ListValue* args); + // The personal data manager, used to load AutoFill profiles and credit cards. // Unowned pointer, may not be NULL. PersonalDataManager* personal_data_; diff --git a/chrome/browser/resources/options/autofill_options.js b/chrome/browser/resources/options/autofill_options.js index cf8d757..cbf7b7b 100644 --- a/chrome/browser/resources/options/autofill_options.js +++ b/chrome/browser/resources/options/autofill_options.js @@ -7,7 +7,8 @@ cr.define('options', function() { // The offset of the first profile in either the address list or the credit // card list. Consists of the header and the horizontal rule. - const profileOffset = 2; + const addressOffset = 2; + const creditCardOffset = 3; ///////////////////////////////////////////////////////////////////////////// // AutoFillOptions class: @@ -22,6 +23,8 @@ cr.define('options', function() { this.numAddresses = 0; this.numCreditCards = 0; this.activeNavTab = null; + this.addressIDs = null; + this.creditCardIDs = null; OptionsPage.call(this, 'autoFillOptions', templateData.autoFillOptionsTitle, 'autoFillOptionsPage'); @@ -45,6 +48,9 @@ cr.define('options', function() { $('addCreditCardButton').onclick = function(event) { self.showAddCreditCardOverlay_(); }; + $('autoFillRemoveButton').onclick = function(event) { + self.removeProfile_(); + }; Preferences.getInstance().addEventListener('autofill.enabled', cr.bind(self.updateEnabledState_, self)); @@ -56,10 +62,11 @@ cr.define('options', function() { * @private */ updateEnabledState_: function() { - var checkbox = $('autoFillEnabled'); - $('addAddressButton').disabled = $('addCreditCardButton').disabled = - $('editButton').disabled = $('autoFillRemoveButton').disabled = - !checkbox.checked; + var disabled = !$('autoFillEnabled').checked; + $('addAddressButton').disabled = disabled; + $('addCreditCardButton').disabled = disabled; + $('autoFillEditButton').disabled = disabled; + $('autoFillRemoveButton').disabled = disabled; }, /** @@ -96,7 +103,8 @@ cr.define('options', function() { resetAddresses_: function() { var profiles = $('profileList'); for (var i = 0; i < this.numAddresses; ++i) - profiles.remove(profileOffset); + profiles.remove(addressOffset); + this.numAddresses = 0; }, /** @@ -106,9 +114,10 @@ cr.define('options', function() { */ resetCreditCards_: function() { var profiles = $('profileList'); - var offset = this.numAddresses + profileOffset; + var offset = this.numAddresses + addressOffset + creditCardOffset; for (var i = 0; i < this.numCreditCards; ++i) profiles.remove(offset); + this.numCreditCards = 0; }, /** @@ -118,13 +127,14 @@ cr.define('options', function() { */ updateAddresses_: function(addresses) { this.resetAddresses_(); - profileList = $('profileList'); - var blankAddress = - profileList.options[profileOffset + this.numAddresses]; + var profileList = $('profileList'); + var blankAddress = profileList.options[addressOffset]; this.numAddresses = addresses.length; + this.addressIDs = new Array(this.numAddresses); for (var i = 0; i < this.numAddresses; i++) { var address = addresses[i]; var option = new Option(address['label']); + this.addressIDs[i] = address['unique_id']; profileList.add(option, blankAddress); } @@ -138,11 +148,13 @@ cr.define('options', function() { */ updateCreditCards_: function(creditCards) { this.resetCreditCards_(); - profileList = $('profileList'); + var profileList = $('profileList'); this.numCreditCards = creditCards.length; + this.creditCardIDs = new Array(this.numCreditCards); for (var i = 0; i < this.numCreditCards; i++) { var creditCard = creditCards[i]; var option = new Option(creditCard['label']); + this.creditCardIDs[i] = creditCard['unique_id']; profileList.add(option, null); } @@ -158,6 +170,55 @@ cr.define('options', function() { $('autoFillRemoveButton').disabled = $('autoFillEditButton').disabled = ($('profileList').selectedIndex == -1); }, + + /** + * Removes the currently selected profile, whether it's an address or a + * credit card. + * @private + */ + removeProfile_: function() { + var idx = $('profileList').selectedIndex; + if ((profileIndex = this.getAddressIndex_(idx)) != -1) + chrome.send('removeAddress', [String(this.addressIDs[profileIndex])]); + else if ((profileIndex = this.getCreditCardIndex_(idx)) != -1) + chrome.send('removeCreditCard', + [String(this.creditCardIDs[profileIndex])]); + }, + + /** + * Returns the index into the address list based on |index|, the index into + * the select control. Returns -1 if this is not an address index. + * @private + */ + getAddressIndex_: function(index) { + index -= addressOffset; + if (index >= 0 && index < this.numAddresses) + return index; + + return -1; + }, + + /** + * Returns the index into the credit card list based on |index|, the index + * into the select control. Returns -1 if this is not a credit card index. + * @private + */ + getCreditCardIndex_: function(index) { + index -= addressOffset + this.numAddresses + creditCardOffset; + if (index >= 0 && index < this.numCreditCards) + return index; + + return -1; + }, + + /** + * Returns true if |index| points to a credit card profile. + * @private + */ + profileIndexIsCreditCard_: function(index) { + index -= addressOffset + this.numAddresses + creditCardOffset; + return (index >= 0 && index < this.numCreditCards); + } }; AutoFillOptions.updateAddresses = function(addresses) { |