This source file includes following definitions.
- ReportMetrics
- RegisterProfilePrefs
- driver_
- SetFormHasGeneratedPassword
- IsSavingEnabled
- ProvisionallySavePassword
- RecordFailure
- AddSubmissionCallback
- AddObserver
- RemoveObserver
- DidNavigateMainFrame
- OnPasswordFormSubmitted
- OnPasswordFormsParsed
- ShouldPromptUserToSavePassword
- OnPasswordFormsRendered
- PossiblyInitializeUsernamesExperiment
- OtherPossibleUsernamesEnabled
- Autofill
#include "components/password_manager/core/browser/password_manager.h"
#include "base/command_line.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/platform_thread.h"
#include "components/autofill/core/common/password_autofill_util.h"
#include "components/password_manager/core/browser/password_form_manager.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "components/password_manager/core/browser/password_manager_driver.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
using autofill::PasswordForm;
using autofill::PasswordFormMap;
namespace {
const char kSpdyProxyRealm[] = "/SpdyProxy";
void ReportMetrics(bool password_manager_enabled) {
static base::PlatformThreadId initial_thread_id =
base::PlatformThread::CurrentId();
DCHECK(initial_thread_id == base::PlatformThread::CurrentId());
static bool ran_once = false;
if (ran_once)
return;
ran_once = true;
UMA_HISTOGRAM_BOOLEAN("PasswordManager.Enabled", password_manager_enabled);
}
}
const char PasswordManager::kOtherPossibleUsernamesExperiment[] =
"PasswordManagerOtherPossibleUsernames";
void PasswordManager::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
prefs::kPasswordManagerEnabled,
true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kPasswordManagerAllowShowPasswords,
true,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
registry->RegisterListPref(prefs::kPasswordManagerGroupsForDomains,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
PasswordManager::PasswordManager(PasswordManagerClient* client)
: client_(client), driver_(client->GetDriver()) {
DCHECK(client_);
DCHECK(driver_);
password_manager_enabled_.Init(prefs::kPasswordManagerEnabled,
client_->GetPrefs());
ReportMetrics(*password_manager_enabled_);
}
PasswordManager::~PasswordManager() {
FOR_EACH_OBSERVER(LoginModelObserver, observers_, OnLoginModelDestroying());
}
void PasswordManager::SetFormHasGeneratedPassword(const PasswordForm& form) {
for (ScopedVector<PasswordFormManager>::iterator iter =
pending_login_managers_.begin();
iter != pending_login_managers_.end(); ++iter) {
if ((*iter)->DoesManage(
form, PasswordFormManager::ACTION_MATCH_REQUIRED)) {
(*iter)->SetHasGeneratedPassword();
return;
}
}
bool ssl_valid = (form.origin.SchemeIsSecure() &&
!driver_->DidLastPageLoadEncounterSSLErrors());
PasswordFormManager* manager = new PasswordFormManager(
this, client_, driver_, form, ssl_valid);
pending_login_managers_.push_back(manager);
manager->SetHasGeneratedPassword();
}
bool PasswordManager::IsSavingEnabled() const {
return *password_manager_enabled_ && !driver_->IsOffTheRecord();
}
void PasswordManager::ProvisionallySavePassword(const PasswordForm& form) {
if (!IsSavingEnabled()) {
RecordFailure(SAVING_DISABLED, form.origin.host());
return;
}
if (form.password_value.empty()) {
RecordFailure(EMPTY_PASSWORD, form.origin.host());
return;
}
scoped_ptr<PasswordFormManager> manager;
ScopedVector<PasswordFormManager>::iterator matched_manager_it =
pending_login_managers_.end();
for (ScopedVector<PasswordFormManager>::iterator iter =
pending_login_managers_.begin();
iter != pending_login_managers_.end(); ++iter) {
if ((*iter)->DoesManage(
form, PasswordFormManager::ACTION_MATCH_REQUIRED)) {
matched_manager_it = iter;
break;
} else if ((*iter)->DoesManage(
form, PasswordFormManager::ACTION_MATCH_NOT_REQUIRED)) {
matched_manager_it = iter;
}
}
if (matched_manager_it != pending_login_managers_.end()) {
manager.reset(*matched_manager_it);
pending_login_managers_.weak_erase(matched_manager_it);
} else {
RecordFailure(NO_MATCHING_FORM, form.origin.host());
return;
}
if (!manager->HasCompletedMatching()) {
RecordFailure(MATCHING_NOT_COMPLETE, form.origin.host());
return;
}
if (manager->IsBlacklisted()) {
RecordFailure(FORM_BLACKLISTED, form.origin.host());
return;
}
if (!manager->HasValidPasswordForm()) {
RecordFailure(INVALID_FORM, form.origin.host());
return;
}
if (!autofill::ShouldIgnoreAutocompleteOffForPasswordFields() &&
!manager->HasGeneratedPassword() &&
!form.password_autocomplete_set) {
RecordFailure(AUTOCOMPLETE_OFF, form.origin.host());
return;
}
PasswordForm provisionally_saved_form(form);
provisionally_saved_form.ssl_valid =
form.origin.SchemeIsSecure() &&
!driver_->DidLastPageLoadEncounterSSLErrors();
provisionally_saved_form.preferred = true;
PasswordFormManager::OtherPossibleUsernamesAction action =
PasswordFormManager::IGNORE_OTHER_POSSIBLE_USERNAMES;
if (OtherPossibleUsernamesEnabled())
action = PasswordFormManager::ALLOW_OTHER_POSSIBLE_USERNAMES;
manager->ProvisionallySave(provisionally_saved_form, action);
provisional_save_manager_.swap(manager);
}
void PasswordManager::RecordFailure(ProvisionalSaveFailure failure,
const std::string& form_origin) {
UMA_HISTOGRAM_ENUMERATION("PasswordManager.ProvisionalSaveFailure",
failure, MAX_FAILURE_VALUE);
std::string group_name = password_manager_metrics_util::GroupIdToString(
password_manager_metrics_util::MonitoredDomainGroupId(
form_origin, client_->GetPrefs()));
if (!group_name.empty()) {
password_manager_metrics_util::LogUMAHistogramEnumeration(
"PasswordManager.ProvisionalSaveFailure_" + group_name, failure,
MAX_FAILURE_VALUE);
}
}
void PasswordManager::AddSubmissionCallback(
const PasswordSubmittedCallback& callback) {
submission_callbacks_.push_back(callback);
}
void PasswordManager::AddObserver(LoginModelObserver* observer) {
observers_.AddObserver(observer);
}
void PasswordManager::RemoveObserver(LoginModelObserver* observer) {
observers_.RemoveObserver(observer);
}
void PasswordManager::DidNavigateMainFrame(bool is_in_page) {
if (!is_in_page)
pending_login_managers_.clear();
}
void PasswordManager::OnPasswordFormSubmitted(
const PasswordForm& password_form) {
ProvisionallySavePassword(password_form);
for (size_t i = 0; i < submission_callbacks_.size(); ++i) {
submission_callbacks_[i].Run(password_form);
}
pending_login_managers_.clear();
}
void PasswordManager::OnPasswordFormsParsed(
const std::vector<PasswordForm>& forms) {
bool had_ssl_error = driver_->DidLastPageLoadEncounterSSLErrors();
for (std::vector<PasswordForm>::const_iterator iter = forms.begin();
iter != forms.end(); ++iter) {
if (EndsWith(iter->signon_realm, kSpdyProxyRealm, true))
continue;
bool ssl_valid = iter->origin.SchemeIsSecure() && !had_ssl_error;
PasswordFormManager* manager = new PasswordFormManager(
this, client_, driver_, *iter, ssl_valid);
pending_login_managers_.push_back(manager);
PasswordStore::AuthorizationPromptPolicy prompt_policy =
*password_manager_enabled_ ? PasswordStore::ALLOW_PROMPT
: PasswordStore::DISALLOW_PROMPT;
manager->FetchMatchingLoginsFromPasswordStore(prompt_policy);
}
}
bool PasswordManager::ShouldPromptUserToSavePassword() const {
return provisional_save_manager_->IsNewLogin() &&
!provisional_save_manager_->HasGeneratedPassword() &&
!provisional_save_manager_->IsPendingCredentialsPublicSuffixMatch();
}
void PasswordManager::OnPasswordFormsRendered(
const std::vector<PasswordForm>& visible_forms) {
if (!provisional_save_manager_.get())
return;
DCHECK(IsSavingEnabled());
for (size_t i = 0; i < visible_forms.size(); ++i) {
if (visible_forms[i].action.is_valid() &&
provisional_save_manager_->pending_credentials().action ==
visible_forms[i].action) {
provisional_save_manager_->SubmitFailed();
provisional_save_manager_.reset();
return;
}
}
provisional_save_manager_->SubmitPassed();
if (ShouldPromptUserToSavePassword()) {
client_->PromptUserToSavePassword(provisional_save_manager_.release());
} else {
provisional_save_manager_->Save();
provisional_save_manager_.reset();
}
}
void PasswordManager::PossiblyInitializeUsernamesExperiment(
const PasswordFormMap& best_matches) const {
if (base::FieldTrialList::Find(kOtherPossibleUsernamesExperiment))
return;
bool other_possible_usernames_exist = false;
for (autofill::PasswordFormMap::const_iterator it = best_matches.begin();
it != best_matches.end(); ++it) {
if (!it->second->other_possible_usernames.empty()) {
other_possible_usernames_exist = true;
break;
}
}
if (!other_possible_usernames_exist)
return;
const base::FieldTrial::Probability kDivisor = 100;
scoped_refptr<base::FieldTrial> trial(
base::FieldTrialList::FactoryGetFieldTrial(
kOtherPossibleUsernamesExperiment,
kDivisor,
"Disabled",
2013, 12, 31,
base::FieldTrial::ONE_TIME_RANDOMIZED,
NULL));
base::FieldTrial::Probability enabled_probability =
client_->GetProbabilityForExperiment(kOtherPossibleUsernamesExperiment);
trial->AppendGroup("Enabled", enabled_probability);
}
bool PasswordManager::OtherPossibleUsernamesEnabled() const {
return base::FieldTrialList::FindFullName(
kOtherPossibleUsernamesExperiment) == "Enabled";
}
void PasswordManager::Autofill(
const PasswordForm& form_for_autofill,
const PasswordFormMap& best_matches,
const PasswordForm& preferred_match,
bool wait_for_username) const {
PossiblyInitializeUsernamesExperiment(best_matches);
bool authentication_required = preferred_match.use_additional_authentication;
for (autofill::PasswordFormMap::const_iterator it = best_matches.begin();
!authentication_required && it != best_matches.end(); ++it) {
if (it->second->use_additional_authentication)
authentication_required = true;
}
switch (form_for_autofill.scheme) {
case PasswordForm::SCHEME_HTML: {
scoped_ptr<autofill::PasswordFormFillData> fill_data(
new autofill::PasswordFormFillData());
InitPasswordFormFillData(form_for_autofill,
best_matches,
&preferred_match,
wait_for_username,
OtherPossibleUsernamesEnabled(),
fill_data.get());
if (authentication_required)
client_->AuthenticateAutofillAndFillForm(fill_data.Pass());
else
driver_->FillPasswordForm(*fill_data.get());
break;
}
default:
FOR_EACH_OBSERVER(
LoginModelObserver,
observers_,
OnAutofillDataAvailable(preferred_match.username_value,
preferred_match.password_value));
break;
}
client_->PasswordWasAutofilled(best_matches);
}