Commit 68b5022c authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

FormFetcher::GetBestMatches: return a vector instead of a map

This is a followup to crrev.com/c/1844969.
GetBestMatches now returns the best matches in a vector, instead of in
a map keyed by username.
For now, PasswordFormManager still sorts the matches by username;
that'll be addressed in another followup.

TBRing simple call site update in website_login_fetcher_impl.cc
TBR=arbesser@google.com

Bug: 1011399
Change-Id: Iaefbe3a29603ea319adcbeddadd6b908d029ab74
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1845225Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarVadym Doroshenko <dvadym@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703717}
parent 8b92ab94
...@@ -116,7 +116,7 @@ ManagePasswordsStateTest::CreateFormManager( ...@@ -116,7 +116,7 @@ ManagePasswordsStateTest::CreateFormManager(
const std::vector<const PasswordForm*>& federated_matches) { const std::vector<const PasswordForm*>& federated_matches) {
auto form_manager = std::make_unique<MockPasswordFormManagerForUI>(); auto form_manager = std::make_unique<MockPasswordFormManagerForUI>();
EXPECT_CALL(*form_manager, GetBestMatches()) EXPECT_CALL(*form_manager, GetBestMatches())
.WillOnce(testing::ReturnRef(*best_matches)); .WillOnce(testing::Return(*best_matches));
EXPECT_CALL(*form_manager, GetFederatedMatches()) EXPECT_CALL(*form_manager, GetFederatedMatches())
.WillOnce(Return(federated_matches)); .WillOnce(Return(federated_matches));
EXPECT_CALL(*form_manager, GetOrigin()) EXPECT_CALL(*form_manager, GetOrigin())
......
...@@ -85,9 +85,9 @@ class WebsiteLoginFetcherImpl::PendingFetchLoginsRequest ...@@ -85,9 +85,9 @@ class WebsiteLoginFetcherImpl::PendingFetchLoginsRequest
// From PendingRequest: // From PendingRequest:
void OnFetchCompleted() override { void OnFetchCompleted() override {
std::vector<Login> logins; std::vector<Login> logins;
for (const auto& match : form_fetcher_->GetBestMatches()) { for (const auto* match : form_fetcher_->GetBestMatches()) {
logins.emplace_back(match.second->origin, logins.emplace_back(match->origin,
base::UTF16ToUTF8(match.second->username_value)); base::UTF16ToUTF8(match->username_value));
} }
std::move(callback_).Run(logins); std::move(callback_).Run(logins);
PendingRequest::OnFetchCompleted(); PendingRequest::OnFetchCompleted();
......
...@@ -53,8 +53,8 @@ const std::vector<const PasswordForm*>& FakeFormFetcher::GetAllRelevantMatches() ...@@ -53,8 +53,8 @@ const std::vector<const PasswordForm*>& FakeFormFetcher::GetAllRelevantMatches()
return non_federated_same_scheme_; return non_federated_same_scheme_;
} }
const std::map<base::string16, const PasswordForm*>& const std::vector<const PasswordForm*>& FakeFormFetcher::GetBestMatches()
FakeFormFetcher::GetBestMatches() const { const {
return best_matches_; return best_matches_;
} }
...@@ -65,15 +65,10 @@ const PasswordForm* FakeFormFetcher::GetPreferredMatch() const { ...@@ -65,15 +65,10 @@ const PasswordForm* FakeFormFetcher::GetPreferredMatch() const {
void FakeFormFetcher::SetNonFederated( void FakeFormFetcher::SetNonFederated(
const std::vector<const PasswordForm*>& non_federated) { const std::vector<const PasswordForm*>& non_federated) {
non_federated_ = non_federated; non_federated_ = non_federated;
std::vector<const PasswordForm*> best_matches;
password_manager_util::FindBestMatches( password_manager_util::FindBestMatches(
non_federated_, scheme_, non_federated_, scheme_,
/*sort_matches_by_date_last_used=*/false, &non_federated_same_scheme_, /*sort_matches_by_date_last_used=*/false, &non_federated_same_scheme_,
&best_matches, &preferred_match_); &best_matches_, &preferred_match_);
best_matches_.clear();
for (const auto* match : best_matches) {
best_matches_[match->username_value] = match;
}
} }
void FakeFormFetcher::SetBlacklisted(bool is_blacklisted) { void FakeFormFetcher::SetBlacklisted(bool is_blacklisted) {
......
...@@ -62,8 +62,8 @@ class FakeFormFetcher : public FormFetcher { ...@@ -62,8 +62,8 @@ class FakeFormFetcher : public FormFetcher {
const std::vector<const autofill::PasswordForm*>& GetAllRelevantMatches() const std::vector<const autofill::PasswordForm*>& GetAllRelevantMatches()
const override; const override;
const std::map<base::string16, const autofill::PasswordForm*>& const std::vector<const autofill::PasswordForm*>& GetBestMatches()
GetBestMatches() const override; const override;
const autofill::PasswordForm* GetPreferredMatch() const override; const autofill::PasswordForm* GetPreferredMatch() const override;
...@@ -95,7 +95,7 @@ class FakeFormFetcher : public FormFetcher { ...@@ -95,7 +95,7 @@ class FakeFormFetcher : public FormFetcher {
std::vector<const autofill::PasswordForm*> non_federated_; std::vector<const autofill::PasswordForm*> non_federated_;
std::vector<const autofill::PasswordForm*> federated_; std::vector<const autofill::PasswordForm*> federated_;
std::vector<const autofill::PasswordForm*> non_federated_same_scheme_; std::vector<const autofill::PasswordForm*> non_federated_same_scheme_;
std::map<base::string16, const autofill::PasswordForm*> best_matches_; std::vector<const autofill::PasswordForm*> best_matches_;
const autofill::PasswordForm* preferred_match_ = nullptr; const autofill::PasswordForm* preferred_match_ = nullptr;
bool is_blacklisted_ = false; bool is_blacklisted_ = false;
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_FETCHER_H_ #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_FETCHER_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_FETCHER_H_ #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FORM_FETCHER_H_
#include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
...@@ -83,10 +82,10 @@ class FormFetcher { ...@@ -83,10 +82,10 @@ class FormFetcher {
GetAllRelevantMatches() const = 0; GetAllRelevantMatches() const = 0;
// Nonblacklisted matches obtained from the backend. // Nonblacklisted matches obtained from the backend.
virtual const std::map<base::string16, const autofill::PasswordForm*>& virtual const std::vector<const autofill::PasswordForm*>& GetBestMatches()
GetBestMatches() const = 0; const = 0;
// Pointer to an preferred entry in the map returned by GetBestMatches(). // Pointer to a preferred entry in the vector returned by GetBestMatches().
virtual const autofill::PasswordForm* GetPreferredMatch() const = 0; virtual const autofill::PasswordForm* GetPreferredMatch() const = 0;
// Fetches stored matching logins. In addition the statistics is fetched on // Fetches stored matching logins. In addition the statistics is fetched on
......
...@@ -116,8 +116,8 @@ const std::vector<const PasswordForm*>& FormFetcherImpl::GetAllRelevantMatches() ...@@ -116,8 +116,8 @@ const std::vector<const PasswordForm*>& FormFetcherImpl::GetAllRelevantMatches()
return non_federated_same_scheme_; return non_federated_same_scheme_;
} }
const std::map<base::string16, const PasswordForm*>& const std::vector<const PasswordForm*>& FormFetcherImpl::GetBestMatches()
FormFetcherImpl::GetBestMatches() const { const {
return best_matches_; return best_matches_;
} }
...@@ -216,15 +216,10 @@ std::unique_ptr<FormFetcher> FormFetcherImpl::Clone() { ...@@ -216,15 +216,10 @@ std::unique_ptr<FormFetcher> FormFetcherImpl::Clone() {
result->non_federated_ = MakeCopies(non_federated_); result->non_federated_ = MakeCopies(non_federated_);
result->federated_ = MakeCopies(federated_); result->federated_ = MakeCopies(federated_);
result->is_blacklisted_ = is_blacklisted_; result->is_blacklisted_ = is_blacklisted_;
std::vector<const PasswordForm*> best_matches;
password_manager_util::FindBestMatches( password_manager_util::FindBestMatches(
MakeWeakCopies(result->non_federated_), form_digest_.scheme, MakeWeakCopies(result->non_federated_), form_digest_.scheme,
sort_matches_by_date_last_used_, &result->non_federated_same_scheme_, sort_matches_by_date_last_used_, &result->non_federated_same_scheme_,
&best_matches, &result->preferred_match_); &result->best_matches_, &result->preferred_match_);
result->best_matches_.clear();
for (const auto* match : best_matches) {
result->best_matches_[match->username_value] = match;
}
result->interactions_stats_ = interactions_stats_; result->interactions_stats_ = interactions_stats_;
result->state_ = state_; result->state_ = state_;
...@@ -239,15 +234,10 @@ void FormFetcherImpl::ProcessPasswordStoreResults( ...@@ -239,15 +234,10 @@ void FormFetcherImpl::ProcessPasswordStoreResults(
state_ = State::NOT_WAITING; state_ = State::NOT_WAITING;
SplitResults(std::move(results)); SplitResults(std::move(results));
std::vector<const PasswordForm*> best_matches;
password_manager_util::FindBestMatches( password_manager_util::FindBestMatches(
MakeWeakCopies(non_federated_), form_digest_.scheme, MakeWeakCopies(non_federated_), form_digest_.scheme,
sort_matches_by_date_last_used_, &non_federated_same_scheme_, sort_matches_by_date_last_used_, &non_federated_same_scheme_,
&best_matches, &preferred_match_); &best_matches_, &preferred_match_);
best_matches_.clear();
for (const auto* match : best_matches) {
best_matches_[match->username_value] = match;
}
for (auto* consumer : consumers_) for (auto* consumer : consumers_)
consumer->OnFetchCompleted(); consumer->OnFetchCompleted();
......
...@@ -57,8 +57,8 @@ class FormFetcherImpl : public FormFetcher, ...@@ -57,8 +57,8 @@ class FormFetcherImpl : public FormFetcher,
const std::vector<const autofill::PasswordForm*>& GetAllRelevantMatches() const std::vector<const autofill::PasswordForm*>& GetAllRelevantMatches()
const override; const override;
const std::map<base::string16, const autofill::PasswordForm*>& const std::vector<const autofill::PasswordForm*>& GetBestMatches()
GetBestMatches() const override; const override;
const autofill::PasswordForm* GetPreferredMatch() const override; const autofill::PasswordForm* GetPreferredMatch() const override;
...@@ -112,8 +112,8 @@ class FormFetcherImpl : public FormFetcher, ...@@ -112,8 +112,8 @@ class FormFetcherImpl : public FormFetcher,
std::vector<const autofill::PasswordForm*> non_federated_same_scheme_; std::vector<const autofill::PasswordForm*> non_federated_same_scheme_;
// Set of nonblacklisted PasswordForms from the password store that best match // Set of nonblacklisted PasswordForms from the password store that best match
// the form being managed by |this|, indexed by username. // the form being managed by |this|.
std::map<base::string16, const autofill::PasswordForm*> best_matches_; std::vector<const autofill::PasswordForm*> best_matches_;
// Convenience pointer to entry in |best_matches_| that is marked as // Convenience pointer to entry in |best_matches_| that is marked as
// preferred. This is only allowed to be null if there are no best matches at // preferred. This is only allowed to be null if there are no best matches at
......
...@@ -18,9 +18,8 @@ class MockPasswordFormManagerForUI : public PasswordFormManagerForUI { ...@@ -18,9 +18,8 @@ class MockPasswordFormManagerForUI : public PasswordFormManagerForUI {
~MockPasswordFormManagerForUI() override; ~MockPasswordFormManagerForUI() override;
MOCK_CONST_METHOD0(GetOrigin, const GURL&()); MOCK_CONST_METHOD0(GetOrigin, const GURL&());
MOCK_CONST_METHOD0( MOCK_CONST_METHOD0(GetBestMatches,
GetBestMatches, std::map<base::string16, const autofill::PasswordForm*>());
const std::map<base::string16, const autofill::PasswordForm*>&());
MOCK_CONST_METHOD0(GetFederatedMatches, MOCK_CONST_METHOD0(GetFederatedMatches,
std::vector<const autofill::PasswordForm*>()); std::vector<const autofill::PasswordForm*>());
MOCK_CONST_METHOD0(GetPendingCredentials, const autofill::PasswordForm&()); MOCK_CONST_METHOD0(GetPendingCredentials, const autofill::PasswordForm&());
......
...@@ -262,9 +262,12 @@ const GURL& PasswordFormManager::GetOrigin() const { ...@@ -262,9 +262,12 @@ const GURL& PasswordFormManager::GetOrigin() const {
: observed_form_.url; : observed_form_.url;
} }
const std::map<base::string16, const PasswordForm*>& std::map<base::string16, const PasswordForm*>
PasswordFormManager::GetBestMatches() const { PasswordFormManager::GetBestMatches() const {
return form_fetcher_->GetBestMatches(); std::map<base::string16, const PasswordForm*> best_matches;
for (const auto* match : form_fetcher_->GetBestMatches())
best_matches[match->username_value] = match;
return best_matches;
} }
std::vector<const autofill::PasswordForm*> std::vector<const autofill::PasswordForm*>
...@@ -316,8 +319,7 @@ void PasswordFormManager::Save() { ...@@ -316,8 +319,7 @@ void PasswordFormManager::Save() {
SanitizePossibleUsernames(&pending_credentials_); SanitizePossibleUsernames(&pending_credentials_);
pending_credentials_.date_created = base::Time::Now(); pending_credentials_.date_created = base::Time::Now();
votes_uploader_.SendVotesOnSave(observed_form_, *parsed_submitted_form_, votes_uploader_.SendVotesOnSave(observed_form_, *parsed_submitted_form_,
form_fetcher_->GetBestMatches(), GetBestMatches(), &pending_credentials_);
&pending_credentials_);
SavePendingToStore(false /*update*/); SavePendingToStore(false /*update*/);
} else { } else {
ProcessUpdate(); ProcessUpdate();
...@@ -789,9 +791,9 @@ void PasswordFormManager::Fill() { ...@@ -789,9 +791,9 @@ void PasswordFormManager::Fill() {
#endif #endif
SendFillInformationToRenderer( SendFillInformationToRenderer(
client_, driver_.get(), *observed_password_form.get(), client_, driver_.get(), *observed_password_form.get(), GetBestMatches(),
form_fetcher_->GetBestMatches(), form_fetcher_->GetFederatedMatches(), form_fetcher_->GetFederatedMatches(), form_fetcher_->GetPreferredMatch(),
form_fetcher_->GetPreferredMatch(), metrics_recorder_.get()); metrics_recorder_.get());
} }
void PasswordFormManager::FillForm(const FormData& observed_form) { void PasswordFormManager::FillForm(const FormData& observed_form) {
...@@ -896,7 +898,7 @@ void PasswordFormManager::CreatePendingCredentials() { ...@@ -896,7 +898,7 @@ void PasswordFormManager::CreatePendingCredentials() {
// Calculate the user's action based on existing matches and the submitted // Calculate the user's action based on existing matches and the submitted
// form. // form.
metrics_recorder_->CalculateUserAction(form_fetcher_->GetBestMatches(), metrics_recorder_->CalculateUserAction(GetBestMatches(),
*parsed_submitted_form_); *parsed_submitted_form_);
// This function might be called multiple times so set variables that are // This function might be called multiple times so set variables that are
...@@ -908,7 +910,7 @@ void PasswordFormManager::CreatePendingCredentials() { ...@@ -908,7 +910,7 @@ void PasswordFormManager::CreatePendingCredentials() {
// Look for the actually submitted credentials in the list of previously saved // Look for the actually submitted credentials in the list of previously saved
// credentials that were available to autofilling. // credentials that were available to autofilling.
const PasswordForm* saved_form = password_manager_util::GetMatchForUpdating( const PasswordForm* saved_form = password_manager_util::GetMatchForUpdating(
*parsed_submitted_form_, form_fetcher_->GetBestMatches()); *parsed_submitted_form_, GetBestMatches());
if (saved_form) { if (saved_form) {
// A similar credential exists in the store already. // A similar credential exists in the store already.
pending_credentials_ = *saved_form; pending_credentials_ = *saved_form;
...@@ -1047,9 +1049,8 @@ void PasswordFormManager::ProcessUpdate() { ...@@ -1047,9 +1049,8 @@ void PasswordFormManager::ProcessUpdate() {
} }
if (pending_credentials_.times_used == 1) { if (pending_credentials_.times_used == 1) {
votes_uploader_.UploadFirstLoginVotes(form_fetcher_->GetBestMatches(), votes_uploader_.UploadFirstLoginVotes(
pending_credentials_, GetBestMatches(), pending_credentials_, *parsed_submitted_form_);
*parsed_submitted_form_);
} }
} }
...@@ -1142,7 +1143,7 @@ void PasswordFormManager::CalculateFillingAssistanceMetric( ...@@ -1142,7 +1143,7 @@ void PasswordFormManager::CalculateFillingAssistanceMetric(
void PasswordFormManager::SavePendingToStore(bool update) { void PasswordFormManager::SavePendingToStore(bool update) {
const PasswordForm* saved_form = password_manager_util::GetMatchForUpdating( const PasswordForm* saved_form = password_manager_util::GetMatchForUpdating(
*parsed_submitted_form_, form_fetcher_->GetBestMatches()); *parsed_submitted_form_, GetBestMatches());
if ((update || password_overridden_) && if ((update || password_overridden_) &&
!pending_credentials_.IsFederatedCredential()) { !pending_credentials_.IsFederatedCredential()) {
DCHECK(saved_form); DCHECK(saved_form);
......
...@@ -135,8 +135,8 @@ class PasswordFormManager : public PasswordFormManagerForUI, ...@@ -135,8 +135,8 @@ class PasswordFormManager : public PasswordFormManagerForUI,
// PasswordFormManagerForUI: // PasswordFormManagerForUI:
const GURL& GetOrigin() const override; const GURL& GetOrigin() const override;
const std::map<base::string16, const autofill::PasswordForm*>& std::map<base::string16, const autofill::PasswordForm*> GetBestMatches()
GetBestMatches() const override; const override;
std::vector<const autofill::PasswordForm*> GetFederatedMatches() std::vector<const autofill::PasswordForm*> GetFederatedMatches()
const override; const override;
const autofill::PasswordForm& GetPendingCredentials() const override; const autofill::PasswordForm& GetPendingCredentials() const override;
......
...@@ -32,8 +32,8 @@ class PasswordFormManagerForUI { ...@@ -32,8 +32,8 @@ class PasswordFormManagerForUI {
virtual const GURL& GetOrigin() const = 0; virtual const GURL& GetOrigin() const = 0;
// Returns the best saved matches for the observed form. // Returns the best saved matches for the observed form.
// TODO(crbug.com/831123): it should return a vector. // TODO(crbug.com/1011399): it should return a reference to a vector.
virtual const std::map<base::string16, const autofill::PasswordForm*>& virtual std::map<base::string16, const autofill::PasswordForm*>
GetBestMatches() const = 0; GetBestMatches() const = 0;
// Returns the federated saved matches for the observed form. // Returns the federated saved matches for the observed form.
......
...@@ -47,8 +47,7 @@ class PasswordDataForUI : public PasswordFormManagerForUI { ...@@ -47,8 +47,7 @@ class PasswordDataForUI : public PasswordFormManagerForUI {
// PasswordFormManagerForUI: // PasswordFormManagerForUI:
const GURL& GetOrigin() const override; const GURL& GetOrigin() const override;
const std::map<base::string16, const PasswordForm*>& GetBestMatches() std::map<base::string16, const PasswordForm*> GetBestMatches() const override;
const override;
std::vector<const PasswordForm*> GetFederatedMatches() const override; std::vector<const PasswordForm*> GetFederatedMatches() const override;
const PasswordForm& GetPendingCredentials() const override; const PasswordForm& GetPendingCredentials() const override;
metrics_util::CredentialSourceType GetCredentialSource() const override; metrics_util::CredentialSourceType GetCredentialSource() const override;
...@@ -96,7 +95,7 @@ const GURL& PasswordDataForUI::GetOrigin() const { ...@@ -96,7 +95,7 @@ const GURL& PasswordDataForUI::GetOrigin() const {
return pending_form_.origin; return pending_form_.origin;
} }
const std::map<base::string16, const PasswordForm*>& std::map<base::string16, const PasswordForm*>
PasswordDataForUI::GetBestMatches() const { PasswordDataForUI::GetBestMatches() const {
return matches_; return matches_;
} }
......
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