Chromium Code Reviews| Index: components/password_manager/core/browser/http_password_migrator.cc |
| diff --git a/components/password_manager/core/browser/http_password_migrator.cc b/components/password_manager/core/browser/http_password_migrator.cc |
| index 700327ca480668075b82f08b08d7c769c9d630d9..18666e0a22e9ce2f756d9f8432e5e6c5d9b954d5 100644 |
| --- a/components/password_manager/core/browser/http_password_migrator.cc |
| +++ b/components/password_manager/core/browser/http_password_migrator.cc |
| @@ -4,6 +4,8 @@ |
| #include "components/password_manager/core/browser/http_password_migrator.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "components/password_manager/core/browser/password_manager_client.h" |
| #include "components/password_manager/core/browser/password_manager_metrics_util.h" |
| #include "components/password_manager/core/browser/password_store.h" |
| #include "url/gurl.h" |
| @@ -11,12 +13,25 @@ |
| namespace password_manager { |
| +namespace { |
| + |
| +// Helper method that allows us to pass WeakPtrs to |PasswordStoreConsumer| |
| +// obtained via |GetWeakPtr|. This is not possible otherwise. |
| +void OnHSTSQueryResultHelper( |
| + const base::WeakPtr<PasswordStoreConsumer>& migrator, |
| + bool is_hsts) { |
| + if (migrator) |
|
vasilii
2017/03/08 13:31:16
{}
jdoerrie
2017/03/09 18:35:49
Done.
|
| + static_cast<HttpPasswordMigrator*>(migrator.get()) |
| + ->OnHSTSQueryResult(is_hsts); |
| +} |
| + |
| +} // namespace |
| + |
| HttpPasswordMigrator::HttpPasswordMigrator(const GURL& https_origin, |
| - MigrationMode mode, |
| - PasswordStore* password_store, |
| + const PasswordManagerClient* client, |
| Consumer* consumer) |
| - : mode_(mode), consumer_(consumer), password_store_(password_store) { |
| - DCHECK(password_store_); |
| + : client_(client), consumer_(consumer) { |
| + DCHECK(client_); |
| DCHECK(https_origin.is_valid()); |
| DCHECK(https_origin.SchemeIs(url::kHttpsScheme)) << https_origin; |
| @@ -25,25 +40,45 @@ HttpPasswordMigrator::HttpPasswordMigrator(const GURL& https_origin, |
| GURL http_origin = https_origin.ReplaceComponents(rep); |
| PasswordStore::FormDigest form(autofill::PasswordForm::SCHEME_HTML, |
| http_origin.GetOrigin().spec(), http_origin); |
| - password_store_->GetLogins(form, this); |
| + client_->GetPasswordStore()->GetLogins(form, this); |
| + client_->PostHSTSQueryForHost( |
| + https_origin, base::Bind(&OnHSTSQueryResultHelper, GetWeakPtr())); |
| } |
| HttpPasswordMigrator::~HttpPasswordMigrator() = default; |
| void HttpPasswordMigrator::OnGetPasswordStoreResults( |
| std::vector<std::unique_ptr<autofill::PasswordForm>> results) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + results_ = std::move(results); |
| + got_password_store_results_ = true; |
| + |
| + if (got_hsts_query_result_) |
| + ProcessPasswordStoreResults(); |
| +} |
| + |
| +void HttpPasswordMigrator::OnHSTSQueryResult(bool is_hsts) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + mode_ = is_hsts ? MigrationMode::MOVE : MigrationMode::COPY; |
| + got_hsts_query_result_ = true; |
| + |
| + if (got_password_store_results_) |
| + ProcessPasswordStoreResults(); |
| +} |
| + |
| +void HttpPasswordMigrator::ProcessPasswordStoreResults() { |
| // Android and PSL matches are ignored. |
| - results.erase( |
| - std::remove_if(results.begin(), results.end(), |
| + results_.erase( |
| + std::remove_if(results_.begin(), results_.end(), |
| [](const std::unique_ptr<autofill::PasswordForm>& form) { |
| return form->is_affiliation_based_match || |
| form->is_public_suffix_match; |
| }), |
| - results.end()); |
| + results_.end()); |
| // Add the new credentials to the password store. The HTTP forms are |
| // removed iff |mode_| == MigrationMode::MOVE. |
| - for (const auto& form : results) { |
| + for (const auto& form : results_) { |
| autofill::PasswordForm new_form = *form; |
| GURL::Replacements rep; |
| @@ -57,17 +92,21 @@ void HttpPasswordMigrator::OnGetPasswordStoreResults( |
| new_form.form_data = autofill::FormData(); |
| new_form.generation_upload_status = autofill::PasswordForm::NO_SIGNAL_SENT; |
| new_form.skip_zero_click = false; |
| - password_store_->AddLogin(new_form); |
| + client_->GetPasswordStore()->AddLogin(new_form); |
| if (mode_ == MigrationMode::MOVE) |
| - password_store_->RemoveLogin(*form); |
| + client_->GetPasswordStore()->RemoveLogin(*form); |
| *form = std::move(new_form); |
| } |
| - metrics_util::LogCountHttpMigratedPasswords(results.size()); |
| + metrics_util::LogCountHttpMigratedPasswords(results_.size()); |
| + metrics_util::LogHttpPasswordMigrationMode( |
| + mode_ == MigrationMode::MOVE |
| + ? metrics_util::HTTP_PASSWORD_MIGRATION_MODE_MOVE |
| + : metrics_util::HTTP_PASSWORD_MIGRATION_MODE_COPY); |
| if (consumer_) |
| - consumer_->ProcessMigratedForms(std::move(results)); |
| + consumer_->ProcessMigratedForms(std::move(results_)); |
| } |
| } // namespace password_manager |