NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / password_manager / chrome_password_manager_client.cc
blob51ee6be440a6c22fb43e60429809ce22c04393d3
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
7 #include "base/bind_helpers.h"
8 #include "base/command_line.h"
9 #include "base/memory/singleton.h"
10 #include "base/metrics/histogram.h"
11 #include "chrome/browser/password_manager/password_form_manager.h"
12 #include "chrome/browser/password_manager/password_manager.h"
13 #include "chrome/browser/password_manager/password_manager_util.h"
14 #include "chrome/browser/password_manager/password_store_factory.h"
15 #include "chrome/browser/password_manager/save_password_infobar_delegate.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sync/profile_sync_service.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/ui/passwords/manage_passwords_bubble_ui_controller.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
23 #include "content/public/browser/web_contents.h"
25 #if defined(OS_ANDROID)
26 #include "chrome/browser/android/password_authentication_manager.h"
27 #endif // OS_ANDROID
29 namespace {
31 void ReportOsPassword() {
32 password_manager_util::OsPasswordStatus status =
33 password_manager_util::GetOsPasswordStatus();
35 UMA_HISTOGRAM_ENUMERATION("PasswordManager.OsPasswordStatus",
36 status,
37 password_manager_util::MAX_PASSWORD_STATUS);
40 } // namespace
42 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromePasswordManagerClient);
44 ChromePasswordManagerClient::ChromePasswordManagerClient(
45 content::WebContents* web_contents)
46 : web_contents_(web_contents),
47 driver_(web_contents, this),
48 weak_factory_(this) {
49 // Avoid checking OS password until later on in browser startup
50 // since it calls a few Windows APIs.
51 base::MessageLoopProxy::current()->PostDelayedTask(
52 FROM_HERE,
53 base::Bind(&ReportOsPassword),
54 base::TimeDelta::FromSeconds(10));
57 ChromePasswordManagerClient::~ChromePasswordManagerClient() {}
59 void ChromePasswordManagerClient::PromptUserToSavePassword(
60 PasswordFormManager* form_to_save) {
61 if (CommandLine::ForCurrentProcess()->HasSwitch(
62 switches::kEnableSavePasswordBubble)) {
63 ManagePasswordsBubbleUIController* manage_passwords_bubble_ui_controller =
64 ManagePasswordsBubbleUIController::FromWebContents(web_contents_);
65 if (manage_passwords_bubble_ui_controller) {
66 manage_passwords_bubble_ui_controller->OnPasswordSubmitted(form_to_save);
67 } else {
68 delete form_to_save;
70 } else {
71 std::string uma_histogram_suffix(
72 password_manager_metrics_util::GroupIdToString(
73 password_manager_metrics_util::MonitoredDomainGroupId(
74 form_to_save->realm(), GetPrefs())));
75 SavePasswordInfoBarDelegate::Create(
76 web_contents_, form_to_save, uma_histogram_suffix);
80 void ChromePasswordManagerClient::PasswordWasAutofilled(
81 const autofill::PasswordFormMap& best_matches) const {
82 ManagePasswordsBubbleUIController* manage_passwords_bubble_ui_controller =
83 ManagePasswordsBubbleUIController::FromWebContents(web_contents_);
84 if (manage_passwords_bubble_ui_controller &&
85 CommandLine::ForCurrentProcess()->HasSwitch(
86 switches::kEnableSavePasswordBubble)) {
87 manage_passwords_bubble_ui_controller->OnPasswordAutofilled(best_matches);
91 void ChromePasswordManagerClient::AuthenticateAutofillAndFillForm(
92 scoped_ptr<autofill::PasswordFormFillData> fill_data) {
93 #if defined(OS_ANDROID)
94 if (PasswordAuthenticationManager
95 ::IsAutofillPasswordAuthenticationEnabled()) {
96 PasswordAuthenticationManager::AuthenticatePasswordAutofill(
97 web_contents_,
98 base::Bind(&ChromePasswordManagerClient::CommitFillPasswordForm,
99 weak_factory_.GetWeakPtr(),
100 base::Owned(fill_data.release())));
101 return;
103 #endif // OS_ANDROID
105 // Additional authentication is currently only available for Android, so all
106 // other plaftorms should just fill the password form directly.
107 CommitFillPasswordForm(fill_data.get());
110 Profile* ChromePasswordManagerClient::GetProfile() {
111 return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
114 PrefService* ChromePasswordManagerClient::GetPrefs() {
115 return GetProfile()->GetPrefs();
118 PasswordStore* ChromePasswordManagerClient::GetPasswordStore() {
119 // Always use EXPLICIT_ACCESS as the password manager checks IsOffTheRecord
120 // itself when it shouldn't access the PasswordStore.
121 // TODO(gcasto): Is is safe to change this to Profile::IMPLICIT_ACCESS?
122 return PasswordStoreFactory::GetForProfile(GetProfile(),
123 Profile::EXPLICIT_ACCESS).get();
126 PasswordManagerDriver* ChromePasswordManagerClient::GetDriver() {
127 return &driver_;
130 base::FieldTrial::Probability
131 ChromePasswordManagerClient::GetProbabilityForExperiment(
132 const std::string& experiment_name) {
133 base::FieldTrial::Probability enabled_probability = 0;
134 if (experiment_name == PasswordManager::kOtherPossibleUsernamesExperiment) {
135 switch (chrome::VersionInfo::GetChannel()) {
136 case chrome::VersionInfo::CHANNEL_DEV:
137 case chrome::VersionInfo::CHANNEL_BETA:
138 enabled_probability = 50;
139 break;
140 default:
141 break;
144 return enabled_probability;
147 bool ChromePasswordManagerClient::IsPasswordSyncEnabled() {
148 ProfileSyncService* sync_service =
149 ProfileSyncServiceFactory::GetForProfile(GetProfile());
150 if (sync_service && sync_service->HasSyncSetupCompleted())
151 return sync_service->GetActiveDataTypes().Has(syncer::PASSWORDS);
152 return false;
155 // static
156 PasswordGenerationManager*
157 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
158 content::WebContents* contents) {
159 ChromePasswordManagerClient* client =
160 ChromePasswordManagerClient::FromWebContents(contents);
161 if (!client)
162 return NULL;
163 return client->GetDriver()->GetPasswordGenerationManager();
166 // static
167 PasswordManager* ChromePasswordManagerClient::GetManagerFromWebContents(
168 content::WebContents* contents) {
169 ChromePasswordManagerClient* client =
170 ChromePasswordManagerClient::FromWebContents(contents);
171 if (!client)
172 return NULL;
173 return client->GetDriver()->GetPasswordManager();
176 void ChromePasswordManagerClient::CommitFillPasswordForm(
177 autofill::PasswordFormFillData* data) {
178 driver_.FillPasswordForm(*data);