Commit 0e4ece8a authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[Passwords] Use Strong Aliases for CredentialLeakType Booleans

This change implements using strong booleans for the creation of
CredentialLeakTypes. These are implemented using util::StrongAlias,
which increases type-safety and prevents accidental assignment between
separate boolean parameters. Furthermore, this change performs some
other small clean-ups.

Bug: 986317
Change-Id: I5d08b634921d36abe5a8d1354398fb7a6761b825
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1768353
Commit-Queue: Vasilii Sukhanov <vasilii@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691604}
parent cca37c5a
...@@ -15,7 +15,10 @@ ...@@ -15,7 +15,10 @@
namespace { namespace {
using password_manager::CreateLeakTypeFromBools; using password_manager::CreateLeakType;
using password_manager::IsReused;
using password_manager::IsSaved;
using password_manager::IsSyncing;
using password_manager::metrics_util::LeakDialogDismissalReason; using password_manager::metrics_util::LeakDialogDismissalReason;
using testing::StrictMock; using testing::StrictMock;
...@@ -55,8 +58,8 @@ class CredentialLeakDialogControllerTest : public testing::Test { ...@@ -55,8 +58,8 @@ class CredentialLeakDialogControllerTest : public testing::Test {
}; };
TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogClose) { TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogClose) {
SetUpController(CreateLeakTypeFromBools( SetUpController(
/*is_saved=*/false, /*is_reused=*/false, /*is_syncing=*/false)); CreateLeakType(IsSaved(false), IsReused(false), IsSyncing(false)));
EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt()); EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt());
controller().ShowCredentialLeakPrompt(&leak_prompt()); controller().ShowCredentialLeakPrompt(&leak_prompt());
...@@ -76,8 +79,8 @@ TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogClose) { ...@@ -76,8 +79,8 @@ TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogClose) {
} }
TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogOk) { TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogOk) {
SetUpController(CreateLeakTypeFromBools( SetUpController(
/*is_saved=*/true, /*is_reused=*/true, /*is_syncing=*/false)); CreateLeakType(IsSaved(true), IsReused(true), IsSyncing(false)));
EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt()); EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt());
controller().ShowCredentialLeakPrompt(&leak_prompt()); controller().ShowCredentialLeakPrompt(&leak_prompt());
...@@ -97,8 +100,8 @@ TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogOk) { ...@@ -97,8 +100,8 @@ TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogOk) {
} }
TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogCancel) { TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogCancel) {
SetUpController(CreateLeakTypeFromBools( SetUpController(
/*is_saved=*/false, /*is_reused=*/true, /*is_syncing=*/true)); CreateLeakType(IsSaved(false), IsReused(true), IsSyncing(true)));
EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt()); EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt());
controller().ShowCredentialLeakPrompt(&leak_prompt()); controller().ShowCredentialLeakPrompt(&leak_prompt());
...@@ -118,8 +121,8 @@ TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogCancel) { ...@@ -118,8 +121,8 @@ TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogCancel) {
} }
TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogCheckPasswords) { TEST_F(CredentialLeakDialogControllerTest, CredentialLeakDialogCheckPasswords) {
SetUpController(CreateLeakTypeFromBools( SetUpController(
/*is_saved=*/true, /*is_reused=*/true, /*is_syncing=*/true)); CreateLeakType(IsSaved(true), IsReused(true), IsSyncing(true)));
EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt()); EXPECT_CALL(leak_prompt(), ShowCredentialLeakPrompt());
controller().ShowCredentialLeakPrompt(&leak_prompt()); controller().ShowCredentialLeakPrompt(&leak_prompt());
......
...@@ -13,8 +13,12 @@ ...@@ -13,8 +13,12 @@
#include "url/gurl.h" #include "url/gurl.h"
#include "url/origin.h" #include "url/origin.h"
using password_manager::CreateLeakType;
using password_manager::CredentialLeakFlags; using password_manager::CredentialLeakFlags;
using password_manager::CredentialLeakType; using password_manager::CredentialLeakType;
using password_manager::IsReused;
using password_manager::IsSaved;
using password_manager::IsSyncing;
namespace leak_dialog_utils { namespace leak_dialog_utils {
...@@ -33,35 +37,20 @@ const struct { ...@@ -33,35 +37,20 @@ const struct {
bool should_show_cancel_button; bool should_show_cancel_button;
bool should_check_passwords; bool should_check_passwords;
} kLeakTypesTestCases[] = { } kLeakTypesTestCases[] = {
{password_manager::CreateLeakTypeFromBools( {CreateLeakType(IsSaved(false), IsReused(false), IsSyncing(false)), IDS_OK,
/*is_saved=*/false, IDS_CLOSE, IDS_CREDENTIAL_LEAK_CHANGE_PASSWORD_MESSAGE,
/*is_reused=*/false,
/*is_syncing=*/false),
IDS_OK, IDS_CLOSE, IDS_CREDENTIAL_LEAK_CHANGE_PASSWORD_MESSAGE,
IDS_CREDENTIAL_LEAK_TITLE, false, false}, IDS_CREDENTIAL_LEAK_TITLE, false, false},
{password_manager::CreateLeakTypeFromBools( {CreateLeakType(IsSaved(false), IsReused(false), IsSyncing(true)), IDS_OK,
/*is_saved=*/false, IDS_CLOSE, IDS_CREDENTIAL_LEAK_CHANGE_PASSWORD_MESSAGE,
/*is_reused=*/false,
/*is_syncing=*/true),
IDS_OK, IDS_CLOSE, IDS_CREDENTIAL_LEAK_CHANGE_PASSWORD_MESSAGE,
IDS_CREDENTIAL_LEAK_TITLE, false, false}, IDS_CREDENTIAL_LEAK_TITLE, false, false},
{password_manager::CreateLeakTypeFromBools( {CreateLeakType(IsSaved(false), IsReused(true), IsSyncing(true)),
/*is_saved=*/false,
/*is_reused=*/true,
/*is_syncing=*/true),
IDS_LEAK_CHECK_CREDENTIALS, IDS_CLOSE, IDS_LEAK_CHECK_CREDENTIALS, IDS_CLOSE,
IDS_CREDENTIAL_LEAK_CHANGE_AND_CHECK_PASSWORDS_MESSAGE, IDS_CREDENTIAL_LEAK_CHANGE_AND_CHECK_PASSWORDS_MESSAGE,
IDS_CREDENTIAL_LEAK_TITLE, true, true}, IDS_CREDENTIAL_LEAK_TITLE, true, true},
{password_manager::CreateLeakTypeFromBools( {CreateLeakType(IsSaved(true), IsReused(false), IsSyncing(true)), IDS_OK,
/*is_saved=*/true, IDS_CLOSE, IDS_CREDENTIAL_LEAK_CHANGE_PASSWORD_MESSAGE,
/*is_reused=*/false,
/*is_syncing=*/true),
IDS_OK, IDS_CLOSE, IDS_CREDENTIAL_LEAK_CHANGE_PASSWORD_MESSAGE,
IDS_CREDENTIAL_LEAK_TITLE, false, false}, IDS_CREDENTIAL_LEAK_TITLE, false, false},
{password_manager::CreateLeakTypeFromBools( {CreateLeakType(IsSaved(true), IsReused(true), IsSyncing(true)),
/*is_saved=*/true,
/*is_reused=*/true,
/*is_syncing=*/true),
IDS_LEAK_CHECK_CREDENTIALS, IDS_CLOSE, IDS_LEAK_CHECK_CREDENTIALS, IDS_CLOSE,
IDS_CREDENTIAL_LEAK_CHECK_PASSWORDS_MESSAGE, IDS_CREDENTIAL_LEAK_TITLE, IDS_CREDENTIAL_LEAK_CHECK_PASSWORDS_MESSAGE, IDS_CREDENTIAL_LEAK_TITLE,
true, true}}; true, true}};
......
...@@ -62,15 +62,14 @@ void LeakDetectionDelegate::OnLeakDetectionDone(bool is_leaked, ...@@ -62,15 +62,14 @@ void LeakDetectionDelegate::OnLeakDetectionDone(bool is_leaked,
// If the credentials are not synced, the |CredentialLeakType| needed to // If the credentials are not synced, the |CredentialLeakType| needed to
// show the correct notification is already determined. // show the correct notification is already determined.
OnShowLeakDetectionNotifiction( OnShowLeakDetectionNotifiction(
CreateLeakTypeFromBools(/*is_saved=*/false, /*is_reused=*/false, CreateLeakType(IsSaved(false), IsReused(false), IsSyncing(false)),
/*is_synced=*/false),
std::move(url), std::move(username)); std::move(url), std::move(username));
} else { } else {
// Otherwise query the helper to asynchronously determine the // Otherwise query the helper to asynchronously determine the
// |CredentialLeakType|. // |CredentialLeakType|.
helper_ = std::make_unique<LeakDetectionDelegateHelper>(std::move( helper_ = std::make_unique<LeakDetectionDelegateHelper>(
base::BindOnce(&LeakDetectionDelegate::OnShowLeakDetectionNotifiction, base::BindOnce(&LeakDetectionDelegate::OnShowLeakDetectionNotifiction,
base::Unretained(this)))); base::Unretained(this)));
helper_->GetCredentialLeakType(client_->GetPasswordStore(), helper_->GetCredentialLeakType(client_->GetPasswordStore(),
std::move(url), std::move(username), std::move(url), std::move(username),
std::move(password)); std::move(password));
......
...@@ -27,16 +27,14 @@ void LeakDetectionDelegateHelper::GetCredentialLeakType( ...@@ -27,16 +27,14 @@ void LeakDetectionDelegateHelper::GetCredentialLeakType(
void LeakDetectionDelegateHelper::OnGetPasswordStoreResults( void LeakDetectionDelegateHelper::OnGetPasswordStoreResults(
std::vector<std::unique_ptr<autofill::PasswordForm>> results) { std::vector<std::unique_ptr<autofill::PasswordForm>> results) {
bool is_saved = false; IsSaved is_saved(
for (const auto& form : results) { std::any_of(results.begin(), results.end(), [this](const auto& form) {
if (form->origin == url_ && form->username_value == username_) { return form->origin == url_ && form->username_value == username_;
is_saved = true; }));
break;
} IsReused is_reused(results.size() > (is_saved ? 1 : 0));
}
bool is_reused = results.size() > (is_saved ? 1 : 0);
CredentialLeakType leak_type = CredentialLeakType leak_type =
CreateLeakTypeFromBools(is_saved, is_reused, /*is_synced=*/true); CreateLeakType(is_saved, is_reused, IsSyncing(true));
std::move(callback_).Run(std::move(leak_type), std::move(url_), std::move(callback_).Run(std::move(leak_type), std::move(url_),
std::move(username_)); std::move(username_));
} }
......
...@@ -23,7 +23,8 @@ class PasswordStore; ...@@ -23,7 +23,8 @@ class PasswordStore;
class LeakDetectionDelegateHelper : public PasswordStoreConsumer { class LeakDetectionDelegateHelper : public PasswordStoreConsumer {
public: public:
// Type alias for |callback_|. // Type alias for |callback_|.
using LeakTypeReply = base::OnceCallback<void(int, GURL, base::string16)>; using LeakTypeReply =
base::OnceCallback<void(CredentialLeakType, GURL, base::string16)>;
explicit LeakDetectionDelegateHelper(LeakTypeReply callback); explicit LeakDetectionDelegateHelper(LeakTypeReply callback);
~LeakDetectionDelegateHelper() override; ~LeakDetectionDelegateHelper() override;
......
...@@ -88,11 +88,10 @@ class LeakDetectionDelegateHelperTest : public testing::Test { ...@@ -88,11 +88,10 @@ class LeakDetectionDelegateHelperTest : public testing::Test {
} }
// Set the expectation for the |CredentialLeakType| in the callback_. // Set the expectation for the |CredentialLeakType| in the callback_.
void SetOnShowLeakDetectionNotificationExpectation(bool is_saved, void SetOnShowLeakDetectionNotificationExpectation(IsSaved is_saved,
bool is_reused) { IsReused is_reused) {
EXPECT_CALL( EXPECT_CALL(callback_,
callback_, Run(CreateLeakType(is_saved, is_reused, IsSyncing(true)),
Run(CreateLeakTypeFromBools(is_saved, is_reused, /*is_synced=*/true),
GURL(kLeakedOrigin), ASCIIToUTF16(kLeakedUsername))) GURL(kLeakedOrigin), ASCIIToUTF16(kLeakedUsername)))
.Times(1); .Times(1);
} }
...@@ -107,8 +106,8 @@ TEST_F(LeakDetectionDelegateHelperTest, NeitherSaveNotReused) { ...@@ -107,8 +106,8 @@ TEST_F(LeakDetectionDelegateHelperTest, NeitherSaveNotReused) {
std::vector<PasswordForm> password_forms; std::vector<PasswordForm> password_forms;
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/false, SetOnShowLeakDetectionNotificationExpectation(IsSaved(false),
/*is_reused=*/false); IsReused(false));
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
...@@ -118,8 +117,7 @@ TEST_F(LeakDetectionDelegateHelperTest, SavedLeakedCredentials) { ...@@ -118,8 +117,7 @@ TEST_F(LeakDetectionDelegateHelperTest, SavedLeakedCredentials) {
CreateForm(kLeakedOrigin, kLeakedUsername)}; CreateForm(kLeakedOrigin, kLeakedUsername)};
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/true, SetOnShowLeakDetectionNotificationExpectation(IsSaved(true), IsReused(false));
/*is_reused=*/false);
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
...@@ -131,8 +129,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ...@@ -131,8 +129,7 @@ TEST_F(LeakDetectionDelegateHelperTest,
CreateForm(kOtherOrigin, kLeakedUsername)}; CreateForm(kOtherOrigin, kLeakedUsername)};
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/true, SetOnShowLeakDetectionNotificationExpectation(IsSaved(true), IsReused(true));
/*is_reused=*/true);
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
...@@ -145,8 +142,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ...@@ -145,8 +142,7 @@ TEST_F(LeakDetectionDelegateHelperTest,
CreateForm(kLeakedOrigin, kOtherUsername)}; CreateForm(kLeakedOrigin, kOtherUsername)};
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/true, SetOnShowLeakDetectionNotificationExpectation(IsSaved(true), IsReused(true));
/*is_reused=*/true);
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
...@@ -156,8 +152,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ReusedPasswordWithOtherUsername) { ...@@ -156,8 +152,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ReusedPasswordWithOtherUsername) {
CreateForm(kLeakedOrigin, kOtherUsername)}; CreateForm(kLeakedOrigin, kOtherUsername)};
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/false, SetOnShowLeakDetectionNotificationExpectation(IsSaved(false), IsReused(true));
/*is_reused=*/true);
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
...@@ -167,8 +162,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ReusedPasswordOnOtherOrigin) { ...@@ -167,8 +162,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ReusedPasswordOnOtherOrigin) {
CreateForm(kOtherOrigin, kLeakedUsername)}; CreateForm(kOtherOrigin, kLeakedUsername)};
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/false, SetOnShowLeakDetectionNotificationExpectation(IsSaved(false), IsReused(true));
/*is_reused=*/true);
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
...@@ -179,8 +173,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ReusedPassword) { ...@@ -179,8 +173,7 @@ TEST_F(LeakDetectionDelegateHelperTest, ReusedPassword) {
CreateForm(kOtherOrigin, kOtherUsername)}; CreateForm(kOtherOrigin, kOtherUsername)};
SetGetLoginByPasswordConsumerInvocation(std::move(password_forms)); SetGetLoginByPasswordConsumerInvocation(std::move(password_forms));
SetOnShowLeakDetectionNotificationExpectation(/*is_saved=*/false, SetOnShowLeakDetectionNotificationExpectation(IsSaved(false), IsReused(true));
/*is_reused=*/true);
InitiateGetCredentialLeakType(); InitiateGetCredentialLeakType();
} }
......
...@@ -148,10 +148,10 @@ TEST_F(LeakDetectionDelegateTest, LeakDetectionDone) { ...@@ -148,10 +148,10 @@ TEST_F(LeakDetectionDelegateTest, LeakDetectionDone) {
histogram_tester.ExpectTotalCount( histogram_tester.ExpectTotalCount(
"PasswordManager.LeakDetection.NotifyIsLeakedTime", 0); "PasswordManager.LeakDetection.NotifyIsLeakedTime", 0);
EXPECT_CALL(client(), NotifyUserCredentialsWereLeaked( EXPECT_CALL(client(),
password_manager::CreateLeakTypeFromBools( NotifyUserCredentialsWereLeaked(
/*is_saved=*/false, /*is_reused=*/false, password_manager::CreateLeakType(
/*is_syncing=*/false), IsSaved(false), IsReused(false), IsSyncing(false)),
form.origin)); form.origin));
delegate_interface->OnLeakDetectionDone( delegate_interface->OnLeakDetectionDone(
true, form.origin, form.username_value, form.password_value); true, form.origin, form.username_value, form.password_value);
......
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
namespace password_manager { namespace password_manager {
CredentialLeakType CreateLeakTypeFromBools(bool is_saved, CredentialLeakType CreateLeakType(IsSaved is_saved,
bool is_reused, IsReused is_reused,
bool is_syncing) { IsSyncing is_syncing) {
CredentialLeakType leak_type = 0; CredentialLeakType leak_type = 0;
if (is_saved) if (is_saved)
leak_type |= kPasswordSaved; leak_type |= kPasswordSaved;
......
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LEAK_DETECTION_DIALOG_UTILS_H_ #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LEAK_DETECTION_DIALOG_UTILS_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LEAK_DETECTION_DIALOG_UTILS_H_ #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LEAK_DETECTION_DIALOG_UTILS_H_
#include <type_traits>
#include "base/util/type_safety/strong_alias.h"
namespace password_manager { namespace password_manager {
// Defines possible scenarios for leaked credentials. // Defines possible scenarios for leaked credentials.
...@@ -18,12 +22,16 @@ enum CredentialLeakFlags { ...@@ -18,12 +22,16 @@ enum CredentialLeakFlags {
}; };
// Contains combination of CredentialLeakFlags values. // Contains combination of CredentialLeakFlags values.
using CredentialLeakType = int; using CredentialLeakType = std::underlying_type_t<CredentialLeakFlags>;
using IsSaved = util::StrongAlias<class IsSavedTag, bool>;
using IsReused = util::StrongAlias<class IsReusedTag, bool>;
using IsSyncing = util::StrongAlias<class IsSyncingTag, bool>;
// Creates CredentialLeakType from booleans. // Creates CredentialLeakType from strong booleans.
CredentialLeakType CreateLeakTypeFromBools(bool is_saved, CredentialLeakType CreateLeakType(IsSaved is_saved,
bool is_reused, IsReused is_reused,
bool is_syncing); IsSyncing is_syncing);
// Checks whether password is saved in chrome. // Checks whether password is saved in chrome.
bool IsPasswordSaved(CredentialLeakType leak_type); bool IsPasswordSaved(CredentialLeakType leak_type);
......
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