Commit b5e919ca authored by jhawkins@chromium.org's avatar jhawkins@chromium.org

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
parent a684921f
...@@ -28,6 +28,28 @@ const int kMinImportSize = 3; ...@@ -28,6 +28,28 @@ const int kMinImportSize = 3;
const char kUnlabeled[] = "Unlabeled"; 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 } // namespace
PersonalDataManager::~PersonalDataManager() { PersonalDataManager::~PersonalDataManager() {
...@@ -83,7 +105,7 @@ void PersonalDataManager::OnAutoFillDialogApply( ...@@ -83,7 +105,7 @@ void PersonalDataManager::OnAutoFillDialogApply(
} }
void PersonalDataManager::SetObserver(PersonalDataManager::Observer* observer) { 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. // nuked.
observers_.RemoveObserver(observer); observers_.RemoveObserver(observer);
observers_.AddObserver(observer); observers_.AddObserver(observer);
...@@ -364,6 +386,38 @@ void PersonalDataManager::SetCreditCards( ...@@ -364,6 +386,38 @@ void PersonalDataManager::SetCreditCards(
FOR_EACH_OBSERVER(Observer, observers_, OnPersonalDataChanged()); 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, void PersonalDataManager::GetPossibleFieldTypes(const string16& text,
FieldTypeSet* possible_types) { FieldTypeSet* possible_types) {
string16 clean_info = StringToLowerASCII(CollapseWhitespace(text, false)); string16 clean_info = StringToLowerASCII(CollapseWhitespace(text, false));
......
...@@ -100,6 +100,12 @@ class PersonalDataManager ...@@ -100,6 +100,12 @@ class PersonalDataManager
// ID of newly-added profiles. // ID of newly-added profiles.
void SetCreditCards(std::vector<CreditCard>* credit_cards); 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 // Gets the possible field types for the given text, determined by matching
// the text with all known personal information and returning matching types. // the text with all known personal information and returning matching types.
void GetPossibleFieldTypes(const string16& text, void GetPossibleFieldTypes(const string16& text,
......
...@@ -72,6 +72,13 @@ void AutoFillOptionsHandler::OnPersonalDataChanged() { ...@@ -72,6 +72,13 @@ void AutoFillOptionsHandler::OnPersonalDataChanged() {
} }
void AutoFillOptionsHandler::RegisterMessages() { void AutoFillOptionsHandler::RegisterMessages() {
dom_ui_->RegisterMessageCallback(
"removeAddress",
NewCallback(this, &AutoFillOptionsHandler::RemoveAddress));
dom_ui_->RegisterMessageCallback(
"removeCreditCard",
NewCallback(this, &AutoFillOptionsHandler::RemoveCreditCard));
} }
void AutoFillOptionsHandler::LoadAutoFillData() { void AutoFillOptionsHandler::LoadAutoFillData() {
...@@ -84,6 +91,7 @@ void AutoFillOptionsHandler::LoadAutoFillData() { ...@@ -84,6 +91,7 @@ void AutoFillOptionsHandler::LoadAutoFillData() {
i != personal_data_->profiles().end(); ++i) { i != personal_data_->profiles().end(); ++i) {
DictionaryValue* address = new DictionaryValue(); DictionaryValue* address = new DictionaryValue();
address->SetString("label", (*i)->PreviewSummary()); address->SetString("label", (*i)->PreviewSummary());
address->SetInteger("unique_id", (*i)->unique_id());
addresses.Append(address); addresses.Append(address);
} }
...@@ -96,9 +104,36 @@ void AutoFillOptionsHandler::LoadAutoFillData() { ...@@ -96,9 +104,36 @@ void AutoFillOptionsHandler::LoadAutoFillData() {
i != personal_data_->credit_cards().end(); ++i) { i != personal_data_->credit_cards().end(); ++i) {
DictionaryValue* credit_card = new DictionaryValue(); DictionaryValue* credit_card = new DictionaryValue();
credit_card->SetString("label", (*i)->PreviewSummary()); credit_card->SetString("label", (*i)->PreviewSummary());
credit_card->SetInteger("unique_id", (*i)->unique_id());
credit_cards.Append(credit_card); credit_cards.Append(credit_card);
} }
dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateCreditCards", dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateCreditCards",
credit_cards); 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);
}
...@@ -28,6 +28,14 @@ class AutoFillOptionsHandler : public OptionsPageUIHandler, ...@@ -28,6 +28,14 @@ class AutoFillOptionsHandler : public OptionsPageUIHandler,
// Loads AutoFill addresses and credit cards using the PersonalDataManager. // Loads AutoFill addresses and credit cards using the PersonalDataManager.
void LoadAutoFillData(); 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. // The personal data manager, used to load AutoFill profiles and credit cards.
// Unowned pointer, may not be NULL. // Unowned pointer, may not be NULL.
PersonalDataManager* personal_data_; PersonalDataManager* personal_data_;
......
...@@ -7,7 +7,8 @@ cr.define('options', function() { ...@@ -7,7 +7,8 @@ cr.define('options', function() {
// The offset of the first profile in either the address list or the credit // The offset of the first profile in either the address list or the credit
// card list. Consists of the header and the horizontal rule. // card list. Consists of the header and the horizontal rule.
const profileOffset = 2; const addressOffset = 2;
const creditCardOffset = 3;
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// AutoFillOptions class: // AutoFillOptions class:
...@@ -22,6 +23,8 @@ cr.define('options', function() { ...@@ -22,6 +23,8 @@ cr.define('options', function() {
this.numAddresses = 0; this.numAddresses = 0;
this.numCreditCards = 0; this.numCreditCards = 0;
this.activeNavTab = null; this.activeNavTab = null;
this.addressIDs = null;
this.creditCardIDs = null;
OptionsPage.call(this, 'autoFillOptions', OptionsPage.call(this, 'autoFillOptions',
templateData.autoFillOptionsTitle, templateData.autoFillOptionsTitle,
'autoFillOptionsPage'); 'autoFillOptionsPage');
...@@ -45,6 +48,9 @@ cr.define('options', function() { ...@@ -45,6 +48,9 @@ cr.define('options', function() {
$('addCreditCardButton').onclick = function(event) { $('addCreditCardButton').onclick = function(event) {
self.showAddCreditCardOverlay_(); self.showAddCreditCardOverlay_();
}; };
$('autoFillRemoveButton').onclick = function(event) {
self.removeProfile_();
};
Preferences.getInstance().addEventListener('autofill.enabled', Preferences.getInstance().addEventListener('autofill.enabled',
cr.bind(self.updateEnabledState_, self)); cr.bind(self.updateEnabledState_, self));
...@@ -56,10 +62,11 @@ cr.define('options', function() { ...@@ -56,10 +62,11 @@ cr.define('options', function() {
* @private * @private
*/ */
updateEnabledState_: function() { updateEnabledState_: function() {
var checkbox = $('autoFillEnabled'); var disabled = !$('autoFillEnabled').checked;
$('addAddressButton').disabled = $('addCreditCardButton').disabled = $('addAddressButton').disabled = disabled;
$('editButton').disabled = $('autoFillRemoveButton').disabled = $('addCreditCardButton').disabled = disabled;
!checkbox.checked; $('autoFillEditButton').disabled = disabled;
$('autoFillRemoveButton').disabled = disabled;
}, },
/** /**
...@@ -96,7 +103,8 @@ cr.define('options', function() { ...@@ -96,7 +103,8 @@ cr.define('options', function() {
resetAddresses_: function() { resetAddresses_: function() {
var profiles = $('profileList'); var profiles = $('profileList');
for (var i = 0; i < this.numAddresses; ++i) 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() { ...@@ -106,9 +114,10 @@ cr.define('options', function() {
*/ */
resetCreditCards_: function() { resetCreditCards_: function() {
var profiles = $('profileList'); var profiles = $('profileList');
var offset = this.numAddresses + profileOffset; var offset = this.numAddresses + addressOffset + creditCardOffset;
for (var i = 0; i < this.numCreditCards; ++i) for (var i = 0; i < this.numCreditCards; ++i)
profiles.remove(offset); profiles.remove(offset);
this.numCreditCards = 0;
}, },
/** /**
...@@ -118,13 +127,14 @@ cr.define('options', function() { ...@@ -118,13 +127,14 @@ cr.define('options', function() {
*/ */
updateAddresses_: function(addresses) { updateAddresses_: function(addresses) {
this.resetAddresses_(); this.resetAddresses_();
profileList = $('profileList'); var profileList = $('profileList');
var blankAddress = var blankAddress = profileList.options[addressOffset];
profileList.options[profileOffset + this.numAddresses];
this.numAddresses = addresses.length; this.numAddresses = addresses.length;
this.addressIDs = new Array(this.numAddresses);
for (var i = 0; i < this.numAddresses; i++) { for (var i = 0; i < this.numAddresses; i++) {
var address = addresses[i]; var address = addresses[i];
var option = new Option(address['label']); var option = new Option(address['label']);
this.addressIDs[i] = address['unique_id'];
profileList.add(option, blankAddress); profileList.add(option, blankAddress);
} }
...@@ -138,11 +148,13 @@ cr.define('options', function() { ...@@ -138,11 +148,13 @@ cr.define('options', function() {
*/ */
updateCreditCards_: function(creditCards) { updateCreditCards_: function(creditCards) {
this.resetCreditCards_(); this.resetCreditCards_();
profileList = $('profileList'); var profileList = $('profileList');
this.numCreditCards = creditCards.length; this.numCreditCards = creditCards.length;
this.creditCardIDs = new Array(this.numCreditCards);
for (var i = 0; i < this.numCreditCards; i++) { for (var i = 0; i < this.numCreditCards; i++) {
var creditCard = creditCards[i]; var creditCard = creditCards[i];
var option = new Option(creditCard['label']); var option = new Option(creditCard['label']);
this.creditCardIDs[i] = creditCard['unique_id'];
profileList.add(option, null); profileList.add(option, null);
} }
...@@ -158,6 +170,55 @@ cr.define('options', function() { ...@@ -158,6 +170,55 @@ cr.define('options', function() {
$('autoFillRemoveButton').disabled = $('autoFillEditButton').disabled = $('autoFillRemoveButton').disabled = $('autoFillEditButton').disabled =
($('profileList').selectedIndex == -1); ($('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) { AutoFillOptions.updateAddresses = function(addresses) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment