1 // Copyright 2013 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/ui/passwords/password_manager_presenter.h"
8 #include "base/command_line.h"
9 #include "base/metrics/user_metrics_action.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "base/values.h"
14 #include "chrome/browser/password_manager/password_manager_util.h"
15 #include "chrome/browser/password_manager/password_store_factory.h"
16 #include "chrome/browser/password_manager/sync_metrics.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/passwords/password_ui_view.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/url_constants.h"
21 #include "components/autofill/core/common/password_form.h"
22 #include "components/password_manager/core/common/password_manager_pref_names.h"
23 #include "content/public/browser/user_metrics.h"
24 #include "content/public/browser/web_contents.h"
26 using password_manager::PasswordStore
;
28 PasswordManagerPresenter::PasswordManagerPresenter(
29 PasswordUIView
* password_view
)
31 exception_populater_(this),
32 password_view_(password_view
) {
33 DCHECK(password_view_
);
34 require_reauthentication_
=
35 !base::CommandLine::ForCurrentProcess()->HasSwitch(
36 switches::kDisablePasswordManagerReauthentication
);
39 PasswordManagerPresenter::~PasswordManagerPresenter() {
40 PasswordStore
* store
= GetPasswordStore();
42 store
->RemoveObserver(this);
45 void PasswordManagerPresenter::Initialize() {
46 // Due to the way that handlers are (re)initialized under certain types of
47 // navigation, the presenter may already be initialized. (See bugs 88986
48 // and 86448). If this is the case, return immediately. This is a hack.
49 // TODO(mdm): remove this hack once it is no longer necessary.
50 if (!show_passwords_
.GetPrefName().empty())
54 password_manager::prefs::kPasswordManagerAllowShowPasswords
,
55 password_view_
->GetProfile()->GetPrefs(),
56 base::Bind(&PasswordManagerPresenter::UpdatePasswordLists
,
57 base::Unretained(this)));
58 // TODO(jhawkins) We should not cache web_ui()->GetProfile().See
60 PasswordStore
* store
= GetPasswordStore();
62 store
->AddObserver(this);
65 void PasswordManagerPresenter::OnLoginsChanged(
66 const password_manager::PasswordStoreChangeList
& changes
) {
67 // Entire list is updated for convenience.
68 UpdatePasswordLists();
71 PasswordStore
* PasswordManagerPresenter::GetPasswordStore() {
72 return PasswordStoreFactory::GetForProfile(password_view_
->GetProfile(),
73 ServiceAccessType::EXPLICIT_ACCESS
)
77 void PasswordManagerPresenter::UpdatePasswordLists() {
78 // Reset so that showing a password will require re-authentication.
79 last_authentication_time_
= base::TimeTicks();
81 // Reset the current lists.
82 password_list_
.clear();
83 password_exception_list_
.clear();
85 populater_
.Populate();
86 exception_populater_
.Populate();
89 void PasswordManagerPresenter::RemoveSavedPassword(size_t index
) {
90 if (index
>= password_list_
.size()) {
91 // |index| out of bounds might come from a compromised renderer, don't let
92 // it crash the browser. http://crbug.com/362054
96 PasswordStore
* store
= GetPasswordStore();
99 store
->RemoveLogin(*password_list_
[index
]);
100 content::RecordAction(
101 base::UserMetricsAction("PasswordManager_RemoveSavedPassword"));
104 void PasswordManagerPresenter::RemovePasswordException(size_t index
) {
105 if (index
>= password_exception_list_
.size()) {
106 // |index| out of bounds might come from a compromised renderer, don't let
107 // it crash the browser. http://crbug.com/362054
111 PasswordStore
* store
= GetPasswordStore();
114 store
->RemoveLogin(*password_exception_list_
[index
]);
115 content::RecordAction(
116 base::UserMetricsAction("PasswordManager_RemovePasswordException"));
119 void PasswordManagerPresenter::RequestShowPassword(size_t index
) {
120 #if !defined(OS_ANDROID) // This is never called on Android.
121 if (index
>= password_list_
.size()) {
122 // |index| out of bounds might come from a compromised renderer, don't let
123 // it crash the browser. http://crbug.com/362054
127 if (IsAuthenticationRequired()) {
128 if (password_manager_util::AuthenticateUser(
129 password_view_
->GetNativeWindow()))
130 last_authentication_time_
= base::TimeTicks::Now();
135 if (password_manager_sync_metrics::IsSyncAccountCredential(
136 password_view_
->GetProfile(),
137 base::UTF16ToUTF8(password_list_
[index
]->username_value
),
138 password_list_
[index
]->signon_realm
)) {
139 content::RecordAction(
140 base::UserMetricsAction("PasswordManager_SyncCredentialShown"));
143 // Call back the front end to reveal the password.
144 password_view_
->ShowPassword(index
, password_list_
[index
]->password_value
);
148 const autofill::PasswordForm
* PasswordManagerPresenter::GetPassword(
150 if (index
>= password_list_
.size()) {
151 // |index| out of bounds might come from a compromised renderer, don't let
152 // it crash the browser. http://crbug.com/362054
156 return password_list_
[index
];
159 const autofill::PasswordForm
* PasswordManagerPresenter::GetPasswordException(
161 if (index
>= password_exception_list_
.size()) {
162 // |index| out of bounds might come from a compromised renderer, don't let
163 // it crash the browser. http://crbug.com/362054
167 return password_exception_list_
[index
];
170 void PasswordManagerPresenter::SetPasswordList() {
171 // Due to the way that handlers are (re)initialized under certain types of
172 // navigation, the presenter may already be initialized. (See bugs 88986
173 // and 86448). If this is the case, return immediately. This is a hack.
174 // If this is the case, initialize on demand. This is a hack.
175 // TODO(mdm): remove this hack once it is no longer necessary.
176 if (show_passwords_
.GetPrefName().empty())
179 bool show_passwords
= *show_passwords_
&& !require_reauthentication_
;
180 password_view_
->SetPasswordList(password_list_
, show_passwords
);
183 void PasswordManagerPresenter::SetPasswordExceptionList() {
184 password_view_
->SetPasswordExceptionList(password_exception_list_
);
187 bool PasswordManagerPresenter::IsAuthenticationRequired() {
188 base::TimeDelta delta
= base::TimeDelta::FromSeconds(60);
189 return require_reauthentication_
&&
190 (base::TimeTicks::Now() - last_authentication_time_
) > delta
;
193 PasswordManagerPresenter::ListPopulater::ListPopulater(
194 PasswordManagerPresenter
* page
) : page_(page
) {
197 PasswordManagerPresenter::ListPopulater::~ListPopulater() {
200 PasswordManagerPresenter::PasswordListPopulater::PasswordListPopulater(
201 PasswordManagerPresenter
* page
) : ListPopulater(page
) {
204 void PasswordManagerPresenter::PasswordListPopulater::Populate() {
205 PasswordStore
* store
= page_
->GetPasswordStore();
207 cancelable_task_tracker()->TryCancelAll();
208 store
->GetAutofillableLogins(this);
210 LOG(ERROR
) << "No password store! Cannot display passwords.";
214 void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults(
215 ScopedVector
<autofill::PasswordForm
> results
) {
216 page_
->password_list_
.swap(results
);
217 page_
->SetPasswordList();
220 PasswordManagerPresenter::PasswordExceptionListPopulater::
221 PasswordExceptionListPopulater(PasswordManagerPresenter
* page
)
222 : ListPopulater(page
) {
225 void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() {
226 PasswordStore
* store
= page_
->GetPasswordStore();
228 cancelable_task_tracker()->TryCancelAll();
229 store
->GetBlacklistLogins(this);
231 LOG(ERROR
) << "No password store! Cannot display exceptions.";
235 void PasswordManagerPresenter::PasswordExceptionListPopulater::
236 OnGetPasswordStoreResults(ScopedVector
<autofill::PasswordForm
> results
) {
237 page_
->password_exception_list_
.swap(results
);
238 page_
->SetPasswordExceptionList();