This source file includes following definitions.
- password_view_
- Initialize
- OnLoginsChanged
- GetPasswordStore
- UpdatePasswordLists
- RemoveSavedPassword
- RemovePasswordException
- RequestShowPassword
- GetPassword
- GetPasswordException
- SetPasswordList
- SetPasswordExceptionList
- IsAuthenticationRequired
- OnGetPasswordStoreResults
- PasswordExceptionListPopulater
- OnGetPasswordStoreResults
#include "chrome/browser/ui/passwords/password_manager_presenter.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/prefs/pref_service.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/password_manager/password_manager_util.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/ui/passwords/password_ui_view.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
PasswordManagerPresenter::PasswordManagerPresenter(
PasswordUIView* password_view)
: populater_(this),
exception_populater_(this),
password_view_(password_view) {
DCHECK(password_view_);
require_reauthentication_ = !CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisablePasswordManagerReauthentication);
}
PasswordManagerPresenter::~PasswordManagerPresenter() {
PasswordStore* store = GetPasswordStore();
if (store)
store->RemoveObserver(this);
}
void PasswordManagerPresenter::Initialize() {
if (!show_passwords_.GetPrefName().empty())
return;
show_passwords_.Init(
prefs::kPasswordManagerAllowShowPasswords,
password_view_->GetProfile()->GetPrefs(),
base::Bind(&PasswordManagerPresenter::UpdatePasswordLists,
base::Unretained(this)));
PasswordStore* store = GetPasswordStore();
if (store)
store->AddObserver(this);
}
void PasswordManagerPresenter::OnLoginsChanged(
const PasswordStoreChangeList& changes) {
UpdatePasswordLists();
}
PasswordStore* PasswordManagerPresenter::GetPasswordStore() {
return PasswordStoreFactory::GetForProfile(password_view_->GetProfile(),
Profile::EXPLICIT_ACCESS).get();
}
void PasswordManagerPresenter::UpdatePasswordLists() {
last_authentication_time_ = base::TimeTicks();
password_list_.clear();
password_exception_list_.clear();
populater_.Populate();
exception_populater_.Populate();
}
void PasswordManagerPresenter::RemoveSavedPassword(size_t index) {
DCHECK_LT(index, password_list_.size());
PasswordStore* store = GetPasswordStore();
if (!store)
return;
store->RemoveLogin(*password_list_[index]);
content::RecordAction(
base::UserMetricsAction("PasswordManager_RemoveSavedPassword"));
}
void PasswordManagerPresenter::RemovePasswordException(size_t index) {
DCHECK_LT(index, password_exception_list_.size());
PasswordStore* store = GetPasswordStore();
if (!store)
return;
store->RemoveLogin(*password_exception_list_[index]);
content::RecordAction(
base::UserMetricsAction("PasswordManager_RemovePasswordException"));
}
void PasswordManagerPresenter::RequestShowPassword(size_t index) {
#if !defined(OS_ANDROID)
DCHECK_LT(index, password_list_.size());
if (IsAuthenticationRequired()) {
if (password_manager_util::AuthenticateUser(
password_view_->GetNativeWindow()))
last_authentication_time_ = base::TimeTicks::Now();
else
return;
}
password_view_->ShowPassword(index, password_list_[index]->password_value);
#endif
}
const autofill::PasswordForm& PasswordManagerPresenter::GetPassword(
size_t index) {
DCHECK_LT(index, password_list_.size());
return *password_list_[index];
}
const autofill::PasswordForm& PasswordManagerPresenter::GetPasswordException(
size_t index) {
DCHECK_LT(index, password_exception_list_.size());
return *password_exception_list_[index];
}
void PasswordManagerPresenter::SetPasswordList() {
if (show_passwords_.GetPrefName().empty())
Initialize();
bool show_passwords = *show_passwords_ && !require_reauthentication_;
password_view_->SetPasswordList(password_list_, show_passwords);
}
void PasswordManagerPresenter::SetPasswordExceptionList() {
password_view_->SetPasswordExceptionList(password_exception_list_);
}
bool PasswordManagerPresenter::IsAuthenticationRequired() {
base::TimeDelta delta = base::TimeDelta::FromSeconds(60);
return require_reauthentication_ &&
(base::TimeTicks::Now() - last_authentication_time_) > delta;
}
PasswordManagerPresenter::ListPopulater::ListPopulater(
PasswordManagerPresenter* page) : page_(page) {
}
PasswordManagerPresenter::ListPopulater::~ListPopulater() {
}
PasswordManagerPresenter::PasswordListPopulater::PasswordListPopulater(
PasswordManagerPresenter* page) : ListPopulater(page) {
}
void PasswordManagerPresenter::PasswordListPopulater::Populate() {
PasswordStore* store = page_->GetPasswordStore();
if (store != NULL) {
cancelable_task_tracker()->TryCancelAll();
store->GetAutofillableLogins(this);
} else {
LOG(ERROR) << "No password store! Cannot display passwords.";
}
}
void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults(
const std::vector<autofill::PasswordForm*>& results) {
page_->password_list_.clear();
page_->password_list_.insert(page_->password_list_.end(),
results.begin(), results.end());
page_->SetPasswordList();
}
PasswordManagerPresenter::PasswordExceptionListPopulater::
PasswordExceptionListPopulater(PasswordManagerPresenter* page)
: ListPopulater(page) {
}
void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() {
PasswordStore* store = page_->GetPasswordStore();
if (store != NULL) {
cancelable_task_tracker()->TryCancelAll();
store->GetBlacklistLogins(this);
} else {
LOG(ERROR) << "No password store! Cannot display exceptions.";
}
}
void PasswordManagerPresenter::PasswordExceptionListPopulater::
OnGetPasswordStoreResults(
const std::vector<autofill::PasswordForm*>& results) {
page_->password_exception_list_.clear();
page_->password_exception_list_.insert(page_->password_exception_list_.end(),
results.begin(), results.end());
page_->SetPasswordExceptionList();
}