Commit 6e427806 authored by Mohamed Amir Yosef's avatar Mohamed Amir Yosef Committed by Commit Bot

[Passwords] Introduce FormFetcher.GetAllRelevantMatches()

Change-Id: I8fa5b9a5c7046fb56ed97a08dbd8c369b8663978
Bug: 991510
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1783151
Commit-Queue: Mohamed Amir Yosef <mamir@chromium.org>
Reviewed-by: default avatarVadym Doroshenko <dvadym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#693617}
parent 630ab3ba
...@@ -49,6 +49,11 @@ std::vector<const PasswordForm*> FakeFormFetcher::GetBlacklistedMatches() ...@@ -49,6 +49,11 @@ std::vector<const PasswordForm*> FakeFormFetcher::GetBlacklistedMatches()
return blacklisted_; return blacklisted_;
} }
const std::vector<const PasswordForm*>& FakeFormFetcher::GetAllRelevantMatches()
const {
return non_federated_same_scheme_;
}
const std::map<base::string16, const PasswordForm*>& const std::map<base::string16, const PasswordForm*>&
FakeFormFetcher::GetBestMatches() const { FakeFormFetcher::GetBestMatches() const {
return best_matches_; return best_matches_;
...@@ -62,6 +67,7 @@ void FakeFormFetcher::SetNonFederated( ...@@ -62,6 +67,7 @@ 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;
password_manager_util::FindBestMatches(non_federated_, scheme_, password_manager_util::FindBestMatches(non_federated_, scheme_,
&non_federated_same_scheme_,
&best_matches_, &preferred_match_); &best_matches_, &preferred_match_);
} }
......
...@@ -60,6 +60,9 @@ class FakeFormFetcher : public FormFetcher { ...@@ -60,6 +60,9 @@ class FakeFormFetcher : public FormFetcher {
std::vector<const autofill::PasswordForm*> GetBlacklistedMatches() std::vector<const autofill::PasswordForm*> GetBlacklistedMatches()
const override; const override;
const std::vector<const autofill::PasswordForm*>& GetAllRelevantMatches()
const override;
const std::map<base::string16, const autofill::PasswordForm*>& const std::map<base::string16, const autofill::PasswordForm*>&
GetBestMatches() const override; GetBestMatches() const override;
...@@ -94,6 +97,7 @@ class FakeFormFetcher : public FormFetcher { ...@@ -94,6 +97,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*> blacklisted_; std::vector<const autofill::PasswordForm*> blacklisted_;
std::vector<const autofill::PasswordForm*> non_federated_same_scheme_;
std::map<base::string16, const autofill::PasswordForm*> best_matches_; std::map<base::string16, const autofill::PasswordForm*> best_matches_;
const autofill::PasswordForm* preferred_match_ = nullptr; const autofill::PasswordForm* preferred_match_ = nullptr;
......
...@@ -78,6 +78,11 @@ class FormFetcher { ...@@ -78,6 +78,11 @@ class FormFetcher {
virtual std::vector<const autofill::PasswordForm*> GetBlacklistedMatches() virtual std::vector<const autofill::PasswordForm*> GetBlacklistedMatches()
const = 0; const = 0;
// Non-federated matches obtained from the backend that have the same scheme
// of this form.
virtual const std::vector<const autofill::PasswordForm*>&
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::map<base::string16, const autofill::PasswordForm*>&
GetBestMatches() const = 0; GetBestMatches() const = 0;
......
...@@ -131,6 +131,11 @@ std::vector<const PasswordForm*> FormFetcherImpl::GetBlacklistedMatches() ...@@ -131,6 +131,11 @@ std::vector<const PasswordForm*> FormFetcherImpl::GetBlacklistedMatches()
return MakeWeakCopies(blacklisted_); return MakeWeakCopies(blacklisted_);
} }
const std::vector<const PasswordForm*>& FormFetcherImpl::GetAllRelevantMatches()
const {
return non_federated_same_scheme_;
}
const std::map<base::string16, const PasswordForm*>& const std::map<base::string16, const PasswordForm*>&
FormFetcherImpl::GetBestMatches() const { FormFetcherImpl::GetBestMatches() const {
return best_matches_; return best_matches_;
...@@ -233,7 +238,8 @@ std::unique_ptr<FormFetcher> FormFetcherImpl::Clone() { ...@@ -233,7 +238,8 @@ std::unique_ptr<FormFetcher> FormFetcherImpl::Clone() {
result->blacklisted_ = MakeCopies(blacklisted_); result->blacklisted_ = MakeCopies(blacklisted_);
password_manager_util::FindBestMatches( password_manager_util::FindBestMatches(
MakeWeakCopies(result->non_federated_), form_digest_.scheme, MakeWeakCopies(result->non_federated_), form_digest_.scheme,
&result->best_matches_, &result->preferred_match_); &result->non_federated_same_scheme_, &result->best_matches_,
&result->preferred_match_);
result->interactions_stats_ = this->interactions_stats_; result->interactions_stats_ = this->interactions_stats_;
result->state_ = this->state_; result->state_ = this->state_;
...@@ -251,9 +257,9 @@ void FormFetcherImpl::ProcessPasswordStoreResults( ...@@ -251,9 +257,9 @@ void FormFetcherImpl::ProcessPasswordStoreResults(
non_federated_ = std::move(matches.non_federated); non_federated_ = std::move(matches.non_federated);
blacklisted_ = std::move(matches.blacklisted); blacklisted_ = std::move(matches.blacklisted);
password_manager_util::FindBestMatches(MakeWeakCopies(non_federated_), password_manager_util::FindBestMatches(
form_digest_.scheme, &best_matches_, MakeWeakCopies(non_federated_), form_digest_.scheme,
&preferred_match_); &non_federated_same_scheme_, &best_matches_, &preferred_match_);
for (auto* consumer : consumers_) for (auto* consumer : consumers_)
consumer->OnFetchCompleted(); consumer->OnFetchCompleted();
......
...@@ -47,6 +47,9 @@ class FormFetcherImpl : public FormFetcher, ...@@ -47,6 +47,9 @@ class FormFetcherImpl : public FormFetcher,
std::vector<const autofill::PasswordForm*> GetBlacklistedMatches() std::vector<const autofill::PasswordForm*> GetBlacklistedMatches()
const override; const override;
const std::vector<const autofill::PasswordForm*>& GetAllRelevantMatches()
const override;
const std::map<base::string16, const autofill::PasswordForm*>& const std::map<base::string16, const autofill::PasswordForm*>&
GetBestMatches() const override; GetBestMatches() const override;
...@@ -83,6 +86,9 @@ class FormFetcherImpl : public FormFetcher, ...@@ -83,6 +86,9 @@ class FormFetcherImpl : public FormFetcher,
// List of blacklisted credentials obtained form the password store. // List of blacklisted credentials obtained form the password store.
std::vector<std::unique_ptr<autofill::PasswordForm>> blacklisted_; std::vector<std::unique_ptr<autofill::PasswordForm>> blacklisted_;
// Non-federated credentials of the same scheme as the observed form.
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|, indexed by username.
std::map<base::string16, const autofill::PasswordForm*> best_matches_; std::map<base::string16, const autofill::PasswordForm*> best_matches_;
......
...@@ -935,7 +935,8 @@ void PasswordFormManager::CreatePendingCredentials() { ...@@ -935,7 +935,8 @@ void PasswordFormManager::CreatePendingCredentials() {
// Generate username correction votes. // Generate username correction votes.
bool username_correction_found = bool username_correction_found =
votes_uploader_.FindCorrectedUsernameElement( votes_uploader_.FindCorrectedUsernameElement(
GetAllMatches(), parsed_submitted_form_->username_value, form_fetcher_->GetAllRelevantMatches(),
parsed_submitted_form_->username_value,
parsed_submitted_form_->password_value); parsed_submitted_form_->password_value);
UMA_HISTOGRAM_BOOLEAN("PasswordManager.UsernameCorrectionFound", UMA_HISTOGRAM_BOOLEAN("PasswordManager.UsernameCorrectionFound",
username_correction_found); username_correction_found);
...@@ -1105,8 +1106,8 @@ void PasswordFormManager::PresaveGeneratedPasswordInternal( ...@@ -1105,8 +1106,8 @@ void PasswordFormManager::PresaveGeneratedPasswordInternal(
// generated password is saved. // generated password is saved.
parsed_form->password_value = generated_password; parsed_form->password_value = generated_password;
generation_state_->PresaveGeneratedPassword(std::move(*parsed_form), generation_state_->PresaveGeneratedPassword(
GetAllMatches()); std::move(*parsed_form), form_fetcher_->GetAllRelevantMatches());
} }
void PasswordFormManager::CalculateFillingAssistanceMetric( void PasswordFormManager::CalculateFillingAssistanceMetric(
...@@ -1132,16 +1133,6 @@ void PasswordFormManager::CalculateFillingAssistanceMetric( ...@@ -1132,16 +1133,6 @@ void PasswordFormManager::CalculateFillingAssistanceMetric(
#endif #endif
} }
std::vector<const PasswordForm*> PasswordFormManager::GetAllMatches() const {
std::vector<const autofill::PasswordForm*> result =
form_fetcher_->GetNonFederatedMatches();
PasswordForm::Scheme observed_form_scheme = GetScheme();
base::EraseIf(result, [observed_form_scheme](const auto* form) {
return form->scheme != observed_form_scheme;
});
return result;
}
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_, best_matches_); *parsed_submitted_form_, best_matches_);
...@@ -1152,12 +1143,15 @@ void PasswordFormManager::SavePendingToStore(bool update) { ...@@ -1152,12 +1143,15 @@ void PasswordFormManager::SavePendingToStore(bool update) {
base::string16 old_password = base::string16 old_password =
saved_form ? saved_form->password_value : base::string16(); saved_form ? saved_form->password_value : base::string16();
if (HasGeneratedPassword()) { if (HasGeneratedPassword()) {
generation_state_->CommitGeneratedPassword(pending_credentials_, generation_state_->CommitGeneratedPassword(
GetAllMatches(), old_password); pending_credentials_, form_fetcher_->GetAllRelevantMatches(),
old_password);
} else if (update) { } else if (update) {
form_saver_->Update(pending_credentials_, GetAllMatches(), old_password); form_saver_->Update(pending_credentials_,
form_fetcher_->GetAllRelevantMatches(), old_password);
} else { } else {
form_saver_->Save(pending_credentials_, GetAllMatches(), old_password); form_saver_->Save(pending_credentials_,
form_fetcher_->GetAllRelevantMatches(), old_password);
} }
} }
......
...@@ -287,9 +287,6 @@ class PasswordFormManager : public PasswordFormManagerInterface, ...@@ -287,9 +287,6 @@ class PasswordFormManager : public PasswordFormManagerInterface,
void CalculateFillingAssistanceMetric( void CalculateFillingAssistanceMetric(
const autofill::FormData& submitted_form); const autofill::FormData& submitted_form);
// Returns all the credentials for the origin.
std::vector<const autofill::PasswordForm*> GetAllMatches() const;
// Save/update |pending_credentials_| to the password store. // Save/update |pending_credentials_| to the password store.
void SavePendingToStore(bool update); void SavePendingToStore(bool update);
......
...@@ -228,37 +228,39 @@ base::StringPiece GetSignonRealmWithProtocolExcluded(const PasswordForm& form) { ...@@ -228,37 +228,39 @@ base::StringPiece GetSignonRealmWithProtocolExcluded(const PasswordForm& form) {
void FindBestMatches( void FindBestMatches(
const std::vector<const PasswordForm*>& non_federated_matches, const std::vector<const PasswordForm*>& non_federated_matches,
PasswordForm::Scheme scheme, PasswordForm::Scheme scheme,
std::vector<const autofill::PasswordForm*>* non_federated_same_scheme,
std::map<base::string16, const PasswordForm*>* best_matches, std::map<base::string16, const PasswordForm*>* best_matches,
const PasswordForm** preferred_match) { const PasswordForm** preferred_match) {
DCHECK(std::all_of( DCHECK(std::all_of(
non_federated_matches.begin(), non_federated_matches.end(), non_federated_matches.begin(), non_federated_matches.end(),
[](const PasswordForm* match) { return !match->blacklisted_by_user; })); [](const PasswordForm* match) { return !match->blacklisted_by_user; }));
DCHECK(non_federated_same_scheme);
DCHECK(best_matches); DCHECK(best_matches);
DCHECK(preferred_match); DCHECK(preferred_match);
*preferred_match = nullptr; *preferred_match = nullptr;
best_matches->clear(); best_matches->clear();
non_federated_same_scheme->clear();
std::vector<const PasswordForm*> same_scheme_non_federated_matches;
for (auto* match : non_federated_matches) { for (auto* match : non_federated_matches) {
if (match->scheme == scheme) if (match->scheme == scheme)
same_scheme_non_federated_matches.push_back(match); non_federated_same_scheme->push_back(match);
} }
if (same_scheme_non_federated_matches.empty()) if (non_federated_same_scheme->empty())
return; return;
// Sort matches using IsBetterMatch predicate. // Sort matches using IsBetterMatch predicate.
std::sort(same_scheme_non_federated_matches.begin(), std::sort(non_federated_same_scheme->begin(),
same_scheme_non_federated_matches.end(), IsBetterMatch); non_federated_same_scheme->end(), IsBetterMatch);
for (const auto* match : same_scheme_non_federated_matches) { for (const auto* match : *non_federated_same_scheme) {
const base::string16& username = match->username_value; const base::string16& username = match->username_value;
// The first match for |username| in the sorted array is best match. // The first match for |username| in the sorted array is best match.
if (best_matches->find(username) == best_matches->end()) if (best_matches->find(username) == best_matches->end())
best_matches->insert(std::make_pair(username, match)); best_matches->insert(std::make_pair(username, match));
} }
*preferred_match = *same_scheme_non_federated_matches.begin(); *preferred_match = *non_federated_same_scheme->begin();
} }
const PasswordForm* GetMatchForUpdating( const PasswordForm* GetMatchForUpdating(
......
...@@ -122,6 +122,7 @@ base::StringPiece GetSignonRealmWithProtocolExcluded( ...@@ -122,6 +122,7 @@ base::StringPiece GetSignonRealmWithProtocolExcluded(
void FindBestMatches( void FindBestMatches(
const std::vector<const autofill::PasswordForm*>& non_federated_matches, const std::vector<const autofill::PasswordForm*>& non_federated_matches,
autofill::PasswordForm::Scheme scheme, autofill::PasswordForm::Scheme scheme,
std::vector<const autofill::PasswordForm*>* non_federated_same_scheme,
std::map<base::string16, const autofill::PasswordForm*>* best_matches, std::map<base::string16, const autofill::PasswordForm*>* best_matches,
const autofill::PasswordForm** preferred_match); const autofill::PasswordForm** preferred_match);
......
...@@ -200,8 +200,9 @@ TEST(PasswordManagerUtil, FindBestMatches) { ...@@ -200,8 +200,9 @@ TEST(PasswordManagerUtil, FindBestMatches) {
std::map<base::string16, const PasswordForm*> best_matches; std::map<base::string16, const PasswordForm*> best_matches;
const PasswordForm* preferred_match = nullptr; const PasswordForm* preferred_match = nullptr;
FindBestMatches(matches, PasswordForm::Scheme::kHtml, &best_matches, std::vector<const PasswordForm*> same_scheme_matches;
&preferred_match); FindBestMatches(matches, PasswordForm::Scheme::kHtml, &same_scheme_matches,
&best_matches, &preferred_match);
if (test_case.expected_preferred_match_index == kNotFound) { if (test_case.expected_preferred_match_index == kNotFound) {
// Case of empty |matches|. // Case of empty |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