Commit 4a7e4f9c authored by Mohamed Amir Yosef's avatar Mohamed Amir Yosef Committed by Commit Bot

[Passwords] Support moving multiple password in PasswordManagerPresenter

This is preparation for adding the functionality of moving multiple
passwords from settings view.

Mocks are in the linked bug.

Bug: 1139263
Change-Id: I883e39bbab9bd20cdbfb67b43883886dea71badb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2479462Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Commit-Queue: Mohamed Amir Yosef <mamir@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819333}
parent 942e5f49
......@@ -435,7 +435,8 @@ void PasswordsPrivateDelegateImpl::MovePasswordToAccount(
auto* client = ChromePasswordManagerClient::FromWebContents(web_contents);
DCHECK(client);
if (const std::string* sort_key = password_id_generator_.TryGetKey(id))
password_manager_presenter_->MovePasswordToAccountStore(*sort_key, client);
password_manager_presenter_->MovePasswordToAccountStore({*sort_key},
client);
}
void PasswordsPrivateDelegateImpl::ImportPasswords(
......
......@@ -377,7 +377,7 @@ void PasswordManagerPresenter::UndoRemoveSavedPasswordOrException() {
}
void PasswordManagerPresenter::MovePasswordToAccountStore(
const std::string& sort_key,
const std::vector<std::string>& sort_keys,
password_manager::PasswordManagerClient* client) {
if (!client->GetPasswordFeatureManager()->IsOptedInForAccountStorage() ||
ProfileSyncServiceFactory::GetForProfile(password_view_->GetProfile())
......@@ -385,25 +385,27 @@ void PasswordManagerPresenter::MovePasswordToAccountStore(
return;
}
auto it = password_map_.find(sort_key);
if (it == password_map_.end())
return;
// MovePasswordToAccountStoreHelper takes care of moving the entire
// equivalence class, so passing the first element is fine.
const password_manager::PasswordForm& form = *(it->second[0]);
// Insert nullptr first to obtain the iterator passed to the callback.
MovePasswordToAccountStoreHelperList::iterator helper_it =
move_to_account_helpers_.insert(move_to_account_helpers_.begin(),
nullptr);
// The presenter outlives the helper so it's safe to use base::Unretained.
*helper_it = std::make_unique<
PasswordManagerPresenter::MovePasswordToAccountStoreHelper>(
form, client,
base::BindOnce(
&PasswordManagerPresenter::OnMovePasswordToAccountCompleted,
base::Unretained(this), helper_it));
for (const std::string& sort_key : sort_keys) {
auto it = password_map_.find(sort_key);
if (it == password_map_.end())
continue;
// MovePasswordToAccountStoreHelper takes care of moving the entire
// equivalence class, so passing the first element is fine.
const password_manager::PasswordForm& form = *(it->second[0]);
// Insert nullptr first to obtain the iterator passed to the callback.
MovePasswordToAccountStoreHelperList::iterator helper_it =
move_to_account_helpers_.insert(move_to_account_helpers_.begin(),
nullptr);
// The presenter outlives the helper so it's safe to use base::Unretained.
*helper_it = std::make_unique<
PasswordManagerPresenter::MovePasswordToAccountStoreHelper>(
form, client,
base::BindOnce(
&PasswordManagerPresenter::OnMovePasswordToAccountCompleted,
base::Unretained(this), helper_it));
}
}
void PasswordManagerPresenter::OnMovePasswordToAccountCompleted(
......
......@@ -95,12 +95,13 @@ class PasswordManagerPresenter
// Undoes the last saved password or exception removal.
void UndoRemoveSavedPasswordOrException();
// Moves a password stored in the profile store to the account store. Results
// in a no-op if any of these is true: |sort_key| is invalid, |sort_key|
// corresponds to a password already in the account store, or the user is not
// using the account-scoped password storage.
// Moves a list of passwords stored in the profile store to the account store.
// For each password to move, the result is a no-op if any of these is true:
// |sort_key| is invalid, |sort_key| corresponds to a password already in the
// account store, or the user is not using the account-scoped password
// storage.
void MovePasswordToAccountStore(
const std::string& sort_key,
const std::vector<std::string>& sort_keys,
password_manager::PasswordManagerClient* client);
#if !defined(OS_ANDROID)
......
......@@ -503,20 +503,24 @@ TEST_F(PasswordManagerPresenterTestWithAccountStore,
TestMovePasswordToAccountStore) {
base::HistogramTester histogram_tester;
// Fill the profile store with two entries in the same equivalence class.
// Fill the profile store with 3 entries of which 2 are in the same
// equivalence class.
password_manager::PasswordForm password =
AddPasswordEntry(GURL(kExampleCom), kUsername, kPassword);
AddPasswordEntry(GURL(kExampleCom).Resolve("someOtherPath"), kUsername,
kPassword);
password_manager::PasswordForm password2 =
AddPasswordEntry(GURL(kExampleCom), kUsername2, kPassword);
// Since there are 2 stores, SetPasswordList() and SetPasswordExceptionList()
// are called twice.
EXPECT_CALL(GetUIController(), SetPasswordList).Times(2);
EXPECT_CALL(GetUIController(), SetPasswordExceptionList).Times(2);
UpdatePasswordLists();
ASSERT_THAT(
GetUsernamesAndPasswords(
GetPasswordsInStoreForRealm(*profile_store(), kExampleCom)),
ElementsAre(Pair(kUsername, kPassword), Pair(kUsername, kPassword)));
ASSERT_THAT(GetUsernamesAndPasswords(
GetPasswordsInStoreForRealm(*profile_store(), kExampleCom)),
UnorderedElementsAre(Pair(kUsername, kPassword),
Pair(kUsername, kPassword),
Pair(kUsername2, kPassword)));
ASSERT_THAT(GetUsernamesAndPasswords(
GetPasswordsInStoreForRealm(*account_store(), kExampleCom)),
IsEmpty());
......@@ -524,26 +528,29 @@ TEST_F(PasswordManagerPresenterTestWithAccountStore,
// Move |password| to account and wait for stores to be updated.
GetUIController().GetPasswordManagerPresenter()->MovePasswordToAccountStore(
password_manager::CreateSortKey(password), client());
{password_manager::CreateSortKey(password),
password_manager::CreateSortKey(password2)},
client());
PasswordStoreWaiter profile_store_waiter(profile_store());
PasswordStoreWaiter account_store_waiter(account_store());
// Both passwords should have moved.
// All passwords should have moved.
EXPECT_CALL(GetUIController(), SetPasswordList).Times(2);
EXPECT_CALL(GetUIController(), SetPasswordExceptionList).Times(2);
UpdatePasswordLists();
EXPECT_THAT(GetPasswordsInStoreForRealm(*profile_store(), kExampleCom),
IsEmpty());
EXPECT_THAT(
GetUsernamesAndPasswords(
GetPasswordsInStoreForRealm(*account_store(), kExampleCom)),
ElementsAre(Pair(kUsername, kPassword), Pair(kUsername, kPassword)));
EXPECT_THAT(GetUsernamesAndPasswords(
GetPasswordsInStoreForRealm(*account_store(), kExampleCom)),
UnorderedElementsAre(Pair(kUsername, kPassword),
Pair(kUsername, kPassword),
Pair(kUsername2, kPassword)));
histogram_tester.ExpectUniqueSample(
"PasswordManager.AccountStorage.MoveToAccountStoreFlowAccepted",
password_manager::metrics_util::MoveToAccountStoreTrigger::
kExplicitlyTriggeredInSettings,
1);
2);
}
} // namespace
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