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/prefs/pref_service.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "chrome/browser/password_manager/password_manager_util.h"
13 #include "chrome/browser/password_manager/password_store_factory.h"
14 #include "chrome/browser/ui/passwords/password_ui_view.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h"
18 #include "components/autofill/core/common/password_form.h"
19 #include "content/public/browser/user_metrics.h"
20 #include "content/public/browser/web_contents.h"
22 PasswordManagerPresenter::PasswordManagerPresenter(
23 PasswordUIView
* password_view
)
25 exception_populater_(this),
26 password_view_(password_view
) {
27 DCHECK(password_view_
);
28 require_reauthentication_
= !CommandLine::ForCurrentProcess()->HasSwitch(
29 switches::kDisablePasswordManagerReauthentication
);
32 PasswordManagerPresenter::~PasswordManagerPresenter() {
33 PasswordStore
* store
= GetPasswordStore();
35 store
->RemoveObserver(this);
38 void PasswordManagerPresenter::Initialize() {
39 // Due to the way that handlers are (re)initialized under certain types of
40 // navigation, the presenter may already be initialized. (See bugs 88986
41 // and 86448). If this is the case, return immediately. This is a hack.
42 // TODO(mdm): remove this hack once it is no longer necessary.
43 if (!show_passwords_
.GetPrefName().empty())
47 prefs::kPasswordManagerAllowShowPasswords
,
48 password_view_
->GetProfile()->GetPrefs(),
49 base::Bind(&PasswordManagerPresenter::UpdatePasswordLists
,
50 base::Unretained(this)));
51 // TODO(jhawkins) We should not cache web_ui()->GetProfile().See
53 PasswordStore
* store
= GetPasswordStore();
55 store
->AddObserver(this);
58 void PasswordManagerPresenter::OnLoginsChanged() {
59 UpdatePasswordLists();
62 PasswordStore
* PasswordManagerPresenter::GetPasswordStore() {
63 return PasswordStoreFactory::GetForProfile(password_view_
->GetProfile(),
64 Profile::EXPLICIT_ACCESS
).get();
67 void PasswordManagerPresenter::UpdatePasswordLists() {
68 // Reset so that showing a password will require re-authentication.
69 last_authentication_time_
= base::TimeTicks();
71 // Reset the current lists.
72 password_list_
.clear();
73 password_exception_list_
.clear();
75 populater_
.Populate();
76 exception_populater_
.Populate();
79 void PasswordManagerPresenter::RemoveSavedPassword(size_t index
) {
80 DCHECK_LT(index
, password_list_
.size());
81 PasswordStore
* store
= GetPasswordStore();
84 store
->RemoveLogin(*password_list_
[index
]);
85 content::RecordAction(
86 base::UserMetricsAction("PasswordManager_RemoveSavedPassword"));
89 void PasswordManagerPresenter::RemovePasswordException(size_t index
) {
90 DCHECK_LT(index
, password_exception_list_
.size());
91 PasswordStore
* store
= GetPasswordStore();
94 store
->RemoveLogin(*password_exception_list_
[index
]);
95 content::RecordAction(
96 base::UserMetricsAction("PasswordManager_RemovePasswordException"));
99 void PasswordManagerPresenter::RequestShowPassword(size_t index
) {
100 #if !defined(OS_ANDROID) // This is never called on Android.
101 DCHECK_LT(index
, password_list_
.size());
102 if (IsAuthenticationRequired()) {
103 if (password_manager_util::AuthenticateUser(
104 password_view_
->GetNativeWindow()))
105 last_authentication_time_
= base::TimeTicks::Now();
109 // Call back the front end to reveal the password.
110 password_view_
->ShowPassword(index
, password_list_
[index
]->password_value
);
114 const autofill::PasswordForm
& PasswordManagerPresenter::GetPassword(
116 DCHECK_LT(index
, password_list_
.size());
117 return *password_list_
[index
];
120 const autofill::PasswordForm
& PasswordManagerPresenter::GetPasswordException(
122 DCHECK_LT(index
, password_exception_list_
.size());
123 return *password_exception_list_
[index
];
126 void PasswordManagerPresenter::SetPasswordList() {
127 // Due to the way that handlers are (re)initialized under certain types of
128 // navigation, the presenter may already be initialized. (See bugs 88986
129 // and 86448). If this is the case, return immediately. This is a hack.
130 // If this is the case, initialize on demand. This is a hack.
131 // TODO(mdm): remove this hack once it is no longer necessary.
132 if (show_passwords_
.GetPrefName().empty())
135 bool show_passwords
= *show_passwords_
&& !require_reauthentication_
;
136 password_view_
->SetPasswordList(password_list_
, show_passwords
);
139 void PasswordManagerPresenter::SetPasswordExceptionList() {
140 password_view_
->SetPasswordExceptionList(password_exception_list_
);
143 bool PasswordManagerPresenter::IsAuthenticationRequired() {
144 base::TimeDelta delta
= base::TimeDelta::FromSeconds(60);
145 return require_reauthentication_
&&
146 (base::TimeTicks::Now() - last_authentication_time_
) > delta
;
149 PasswordManagerPresenter::ListPopulater::ListPopulater(
150 PasswordManagerPresenter
* page
)
152 pending_login_query_(0) {
155 PasswordManagerPresenter::ListPopulater::~ListPopulater() {
158 PasswordManagerPresenter::PasswordListPopulater::PasswordListPopulater(
159 PasswordManagerPresenter
* page
) : ListPopulater(page
) {
162 void PasswordManagerPresenter::PasswordListPopulater::Populate() {
163 PasswordStore
* store
= page_
->GetPasswordStore();
165 if (pending_login_query_
)
166 store
->CancelRequest(pending_login_query_
);
168 pending_login_query_
= store
->GetAutofillableLogins(this);
170 LOG(ERROR
) << "No password store! Cannot display passwords.";
174 void PasswordManagerPresenter::PasswordListPopulater::
175 OnPasswordStoreRequestDone(
176 CancelableRequestProvider::Handle handle
,
177 const std::vector
<autofill::PasswordForm
*>& result
) {
178 DCHECK_EQ(pending_login_query_
, handle
);
179 pending_login_query_
= 0;
180 page_
->password_list_
.clear();
181 page_
->password_list_
.insert(page_
->password_list_
.end(),
182 result
.begin(), result
.end());
183 page_
->SetPasswordList();
186 void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults(
187 const std::vector
<autofill::PasswordForm
*>& results
) {
188 // TODO(kaiwang): Implement when I refactor
189 // PasswordStore::GetAutofillableLogins and PasswordStore::GetBlacklistLogins.
193 PasswordManagerPresenter::PasswordExceptionListPopulater::
194 PasswordExceptionListPopulater(PasswordManagerPresenter
* page
)
195 : ListPopulater(page
) {
198 void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() {
199 PasswordStore
* store
= page_
->GetPasswordStore();
201 if (pending_login_query_
)
202 store
->CancelRequest(pending_login_query_
);
204 pending_login_query_
= store
->GetBlacklistLogins(this);
206 LOG(ERROR
) << "No password store! Cannot display exceptions.";
210 void PasswordManagerPresenter::PasswordExceptionListPopulater::
211 OnPasswordStoreRequestDone(
212 CancelableRequestProvider::Handle handle
,
213 const std::vector
<autofill::PasswordForm
*>& result
) {
214 DCHECK_EQ(pending_login_query_
, handle
);
215 pending_login_query_
= 0;
216 page_
->password_exception_list_
.clear();
217 page_
->password_exception_list_
.insert(page_
->password_exception_list_
.end(),
218 result
.begin(), result
.end());
219 page_
->SetPasswordExceptionList();
222 void PasswordManagerPresenter::PasswordExceptionListPopulater::
223 OnGetPasswordStoreResults(
224 const std::vector
<autofill::PasswordForm
*>& results
) {
225 // TODO(kaiwang): Implement when I refactor
226 // PasswordStore::GetAutofillableLogins and PasswordStore::GetBlacklistLogins.