Commit 875621f9 authored by Ivana Zuzic's avatar Ivana Zuzic Committed by Commit Bot

[PWD Editing Android] Add ChangeSavedPassword function with a new signature

A new signature for ChangeSavedPassword takes the index of a password
entry, instead of its sort_key, because Android finds passwords by
index, rather than by sort_key.

Bug: 377410
Change-Id: I972091c7f59c6a0d3c9cc4cbbabea276fcba5c31
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1731829Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarFriedrich [CET] <fhorschig@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Commit-Queue: Ivana Zuzic <izuzic@google.com>
Cr-Commit-Position: refs/heads/master@{#683996}
parent f0e581f7
...@@ -217,53 +217,25 @@ const autofill::PasswordForm* PasswordManagerPresenter::GetPasswordException( ...@@ -217,53 +217,25 @@ const autofill::PasswordForm* PasswordManagerPresenter::GetPasswordException(
} }
void PasswordManagerPresenter::ChangeSavedPassword( void PasswordManagerPresenter::ChangeSavedPassword(
const std::string& sort_key, size_t index,
base::string16 new_username, const base::string16& new_username,
base::Optional<base::string16> new_password) { const base::Optional<base::string16>& new_password) {
// If a password was provided, make sure it is not empty. DCHECK_LT(index, password_map_.size());
if (new_password && new_password->empty())
return;
ChangeSavedPasswords(std::next(password_map_.begin(), index)->second,
new_username, new_password);
}
void PasswordManagerPresenter::ChangeSavedPassword(
const std::string& sort_key,
const base::string16& new_username,
const base::Optional<base::string16>& new_password) {
// Find the equivalence class that needs to be updated. // Find the equivalence class that needs to be updated.
auto it = password_map_.find(sort_key); auto it = password_map_.find(sort_key);
if (it == password_map_.end()) if (it == password_map_.end())
return; return;
const auto& old_forms = it->second; ChangeSavedPasswords(it->second, new_username, new_password);
DCHECK(!old_forms.empty());
const std::string& signon_realm = old_forms[0]->signon_realm;
const base::string16& old_username = old_forms[0]->username_value;
const bool username_changed = old_username != new_username;
// In case the username changed, make sure that there exists no other
// credential with the same signon_realm and username.
if (username_changed) {
for (const auto& sort_key_passwords_pair : password_map_) {
for (const auto& password : sort_key_passwords_pair.second) {
if (password->signon_realm == signon_realm &&
password->username_value == new_username) {
return;
}
}
}
}
PasswordStore* store = GetPasswordStore();
if (!store)
return;
// An updated username implies a change in the primary key, thus we need to
// make sure to call the right API. Update every entry in the equivalence
// class.
for (const auto& old_form : old_forms) {
autofill::PasswordForm new_form = *old_form;
new_form.username_value = new_username;
if (new_password)
new_form.password_value = *new_password;
username_changed ? store->UpdateLoginWithPrimaryKey(new_form, *old_form)
: store->UpdateLogin(new_form);
}
} }
void PasswordManagerPresenter::RemoveSavedPassword(size_t index) { void PasswordManagerPresenter::RemoveSavedPassword(size_t index) {
...@@ -354,6 +326,54 @@ void PasswordManagerPresenter::RemoveLogin(const autofill::PasswordForm& form) { ...@@ -354,6 +326,54 @@ void PasswordManagerPresenter::RemoveLogin(const autofill::PasswordForm& form) {
store->RemoveLogin(form); store->RemoveLogin(form);
} }
void PasswordManagerPresenter::ChangeSavedPasswords(
const FormVector& old_forms,
const base::string16& new_username,
const base::Optional<base::string16>& new_password) {
// If a password was provided, make sure it is not empty.
if (new_password && new_password->empty()) {
DLOG(ERROR) << "The password is empty.";
return;
}
DCHECK(!old_forms.empty());
const std::string& signon_realm = old_forms[0]->signon_realm;
const base::string16& old_username = old_forms[0]->username_value;
const bool username_changed = old_username != new_username;
// In case the username changed, make sure that there exists no other
// credential with the same signon_realm and username.
if (username_changed) {
for (const auto& sort_key_passwords_pair : password_map_) {
for (const auto& password : sort_key_passwords_pair.second) {
if (password->signon_realm == signon_realm &&
password->username_value == new_username) {
DLOG(ERROR) << "A credential with the same signon_realm and username "
"already exists.";
return;
}
}
}
}
PasswordStore* store = GetPasswordStore();
if (!store)
return;
// An updated username implies a change in the primary key, thus we need to
// make sure to call the right API. Update every entry in the equivalence
// class.
for (const auto& old_form : old_forms) {
autofill::PasswordForm new_form = *old_form;
new_form.username_value = new_username;
if (new_password)
new_form.password_value = *new_password;
username_changed ? store->UpdateLoginWithPrimaryKey(new_form, *old_form)
: store->UpdateLogin(new_form);
}
}
bool PasswordManagerPresenter::TryRemovePasswordEntries( bool PasswordManagerPresenter::TryRemovePasswordEntries(
PasswordFormMap* form_map, PasswordFormMap* form_map,
size_t index) { size_t index) {
......
...@@ -60,10 +60,14 @@ class PasswordManagerPresenter ...@@ -60,10 +60,14 @@ class PasswordManagerPresenter
// Gets the password exception entry at |index|. // Gets the password exception entry at |index|.
const autofill::PasswordForm* GetPasswordException(size_t index) const; const autofill::PasswordForm* GetPasswordException(size_t index) const;
// Changes the username and password corresponding to |sort_key|. // Changes the username and password at |index|, or corresponding to
// |sort_key|.
void ChangeSavedPassword(size_t index,
const base::string16& new_username,
const base::Optional<base::string16>& new_password);
void ChangeSavedPassword(const std::string& sort_key, void ChangeSavedPassword(const std::string& sort_key,
base::string16 new_username, const base::string16& new_username,
base::Optional<base::string16> new_password); const base::Optional<base::string16>& new_password);
// Removes the saved password entries at |index|, or corresponding to // Removes the saved password entries at |index|, or corresponding to
// |sort_key|, respectively. // |sort_key|, respectively.
...@@ -110,6 +114,12 @@ class PasswordManagerPresenter ...@@ -110,6 +114,12 @@ class PasswordManagerPresenter
std::map<std::string, std::map<std::string,
std::vector<std::unique_ptr<autofill::PasswordForm>>>; std::vector<std::unique_ptr<autofill::PasswordForm>>>;
// Implementation used in both |ChangeSavedPassword()| methods.
void ChangeSavedPasswords(
const std::vector<std::unique_ptr<autofill::PasswordForm>>& old_forms,
const base::string16& new_username,
const base::Optional<base::string16>& new_password);
// Attempts to remove the entries corresponding to |index| from |form_map|. // Attempts to remove the entries corresponding to |index| from |form_map|.
// This will also add a corresponding undo operation to |undo_manager_|. // This will also add a corresponding undo operation to |undo_manager_|.
// Returns whether removing the entry succeeded. // Returns whether removing the entry succeeded.
......
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