1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/passwords/password_manager_presenter.h"
#include <utility>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/metrics/user_metrics_action.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
#include "chrome/browser/ui/passwords/password_ui_view.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/autofill/core/common/password_form.h"
#include "components/browser_sync/browser/profile_sync_service.h"
#include "components/password_manager/core/browser/affiliation_utils.h"
#include "components/password_manager/core/browser/password_manager_util.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/password_manager/sync/browser/password_sync_util.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#if defined(OS_WIN)
#include "chrome/browser/password_manager/password_manager_util_win.h"
#elif defined(OS_MACOSX)
#include "chrome/browser/password_manager/password_manager_util_mac.h"
#endif
using password_manager::PasswordStore;
PasswordManagerPresenter::PasswordManagerPresenter(
PasswordUIView* password_view)
: populater_(this),
exception_populater_(this),
require_reauthentication_(
!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisablePasswordManagerReauthentication)),
password_view_(password_view) {
DCHECK(password_view_);
}
PasswordManagerPresenter::~PasswordManagerPresenter() {
PasswordStore* store = GetPasswordStore();
if (store)
store->RemoveObserver(this);
}
void PasswordManagerPresenter::Initialize() {
// Due to the way that handlers are (re)initialized under certain types of
// navigation, the presenter may already be initialized. (See bugs 88986
// and 86448). If this is the case, return immediately. This is a hack.
// TODO(mdm): remove this hack once it is no longer necessary.
if (!show_passwords_.GetPrefName().empty())
return;
show_passwords_.Init(
password_manager::prefs::kPasswordManagerAllowShowPasswords,
password_view_->GetProfile()->GetPrefs(),
base::Bind(&PasswordManagerPresenter::UpdatePasswordLists,
base::Unretained(this)));
// TODO(jhawkins) We should not cache web_ui()->GetProfile().See
// crosbug.com/6304.
PasswordStore* store = GetPasswordStore();
if (store)
store->AddObserver(this);
languages_ = password_view_->GetProfile()->GetPrefs()->
GetString(prefs::kAcceptLanguages);
}
void PasswordManagerPresenter::OnLoginsChanged(
const password_manager::PasswordStoreChangeList& changes) {
// Entire list is updated for convenience.
UpdatePasswordLists();
}
PasswordStore* PasswordManagerPresenter::GetPasswordStore() {
return PasswordStoreFactory::GetForProfile(password_view_->GetProfile(),
ServiceAccessType::EXPLICIT_ACCESS)
.get();
}
void PasswordManagerPresenter::UpdatePasswordLists() {
// Reset so that showing a password will require re-authentication.
last_authentication_time_ = base::TimeTicks();
// Reset the current lists.
password_list_.clear();
password_exception_list_.clear();
populater_.Populate();
exception_populater_.Populate();
}
void PasswordManagerPresenter::RemoveSavedPassword(size_t index) {
if (index >= password_list_.size()) {
// |index| out of bounds might come from a compromised renderer, don't let
// it crash the browser. http://crbug.com/362054
NOTREACHED();
return;
}
PasswordStore* store = GetPasswordStore();
if (!store)
return;
store->RemoveLogin(*password_list_[index]);
content::RecordAction(
base::UserMetricsAction("PasswordManager_RemoveSavedPassword"));
}
void PasswordManagerPresenter::RemovePasswordException(size_t index) {
if (index >= password_exception_list_.size()) {
// |index| out of bounds might come from a compromised renderer, don't let
// it crash the browser. http://crbug.com/362054
NOTREACHED();
return;
}
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) // This is never called on Android.
if (index >= password_list_.size()) {
// |index| out of bounds might come from a compromised renderer, don't let
// it crash the browser. http://crbug.com/362054
NOTREACHED();
return;
}
if (require_reauthentication_ &&
(base::TimeTicks::Now() - last_authentication_time_) >
base::TimeDelta::FromSeconds(60)) {
bool authenticated = true;
#if defined(OS_WIN)
authenticated = password_manager_util_win::AuthenticateUser(
password_view_->GetNativeWindow());
#elif defined(OS_MACOSX)
authenticated = password_manager_util_mac::AuthenticateUser();
#endif
if (authenticated)
last_authentication_time_ = base::TimeTicks::Now();
else
return;
}
sync_driver::SyncService* sync_service = nullptr;
if (ProfileSyncServiceFactory::HasProfileSyncService(
password_view_->GetProfile())) {
sync_service =
ProfileSyncServiceFactory::GetForProfile(password_view_->GetProfile());
}
if (password_manager::sync_util::IsSyncAccountCredential(
*password_list_[index], sync_service,
SigninManagerFactory::GetForProfile(password_view_->GetProfile()))) {
content::RecordAction(
base::UserMetricsAction("PasswordManager_SyncCredentialShown"));
}
// Call back the front end to reveal the password.
std::string origin_url = password_manager::GetHumanReadableOrigin(
*password_list_[index], languages_);
password_view_->ShowPassword(
index,
origin_url,
base::UTF16ToUTF8(password_list_[index]->username_value),
password_list_[index]->password_value);
#endif
}
const autofill::PasswordForm* PasswordManagerPresenter::GetPassword(
size_t index) {
if (index >= password_list_.size()) {
// |index| out of bounds might come from a compromised renderer, don't let
// it crash the browser. http://crbug.com/362054
NOTREACHED();
return NULL;
}
return password_list_[index].get();
}
const autofill::PasswordForm* PasswordManagerPresenter::GetPasswordException(
size_t index) {
if (index >= password_exception_list_.size()) {
// |index| out of bounds might come from a compromised renderer, don't let
// it crash the browser. http://crbug.com/362054
NOTREACHED();
return NULL;
}
return password_exception_list_[index].get();
}
void PasswordManagerPresenter::SetPasswordList() {
// Due to the way that handlers are (re)initialized under certain types of
// navigation, the presenter may already be initialized. (See bugs 88986
// and 86448). If this is the case, return immediately. This is a hack.
// If this is the case, initialize on demand. This is a hack.
// TODO(mdm): remove this hack once it is no longer necessary.
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_);
}
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(
ScopedVector<autofill::PasswordForm> results) {
page_->password_list_ =
password_manager_util::ConvertScopedVector(std::move(results));
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(ScopedVector<autofill::PasswordForm> results) {
page_->password_exception_list_ =
password_manager_util::ConvertScopedVector(std::move(results));
page_->SetPasswordExceptionList();
}
|