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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
// Copyright (c) 2012 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/password_manager/password_manager.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/password_manager/password_form_manager.h"
#include "chrome/browser/password_manager/password_manager_delegate.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "components/autofill/common/autofill_messages.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/frame_navigate_params.h"
#include "grit/generated_resources.h"
using content::UserMetricsAction;
using content::WebContents;
using content::PasswordForm;
using content::PasswordFormMap;
DEFINE_WEB_CONTENTS_USER_DATA_KEY(PasswordManager);
namespace {
const char kSpdyProxyRealm[] = "/SpdyProxy";
const char kOtherPossibleUsernamesExperiment[] =
"PasswordManagerOtherPossibleUsernames";
// This routine is called when PasswordManagers are constructed.
//
// Currently we report metrics only once at startup. We require
// that this is only ever called from a single thread in order to
// avoid needing to lock (a static boolean flag is then sufficient to
// guarantee running only once).
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;
// TODO(isherman): This does not actually measure a user action. It should be
// a boolean histogram.
if (password_manager_enabled)
content::RecordAction(UserMetricsAction("PasswordManager_Enabled"));
else
content::RecordAction(UserMetricsAction("PasswordManager_Disabled"));
}
} // namespace
// static
void PasswordManager::RegisterUserPrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
prefs::kPasswordManagerEnabled,
true,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(
prefs::kPasswordManagerAllowShowPasswords,
true,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
// static
void PasswordManager::CreateForWebContentsAndDelegate(
content::WebContents* contents,
PasswordManagerDelegate* delegate) {
if (FromWebContents(contents)) {
DCHECK_EQ(delegate, FromWebContents(contents)->delegate_);
return;
}
contents->SetUserData(UserDataKey(),
new PasswordManager(contents, delegate));
}
PasswordManager::PasswordManager(WebContents* web_contents,
PasswordManagerDelegate* delegate)
: content::WebContentsObserver(web_contents),
delegate_(delegate),
observer_(NULL) {
DCHECK(delegate_);
password_manager_enabled_.Init(prefs::kPasswordManagerEnabled,
delegate_->GetProfile()->GetPrefs());
ReportMetrics(*password_manager_enabled_);
}
PasswordManager::~PasswordManager() {
if (observer_)
observer_->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;
}
}
// If there is no corresponding PasswordFormManager, we create one. This is
// not the common case, and should only happen when there is a bug in our
// ability to detect forms.
bool ssl_valid = (form.origin.SchemeIsSecure() &&
!delegate_->DidLastPageLoadEncounterSSLErrors());
PasswordFormManager* manager =
new PasswordFormManager(delegate_->GetProfile(),
this,
web_contents(),
form,
ssl_valid);
pending_login_managers_.push_back(manager);
manager->SetHasGeneratedPassword();
// TODO(gcasto): Add UMA stats to track this.
}
bool PasswordManager::IsSavingEnabled() const {
return *password_manager_enabled_ &&
!delegate_->GetProfile()->IsOffTheRecord();
}
void PasswordManager::ProvisionallySavePassword(const PasswordForm& form) {
if (!IsSavingEnabled())
return;
// No password to save? Then don't.
if (form.password_value.empty())
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 we find a manager that exactly matches the submitted form including
// the action URL, exit the loop.
if ((*iter)->DoesManage(
form, PasswordFormManager::ACTION_MATCH_REQUIRED)) {
matched_manager_it = iter;
break;
// If the current manager matches the submitted form excluding the action
// URL, remember it as a candidate and continue searching for an exact
// match.
} else if ((*iter)->DoesManage(
form, PasswordFormManager::ACTION_MATCH_NOT_REQUIRED)) {
matched_manager_it = iter;
}
}
// If we didn't find a manager, this means a form was submitted without
// first loading the page containing the form. Don't offer to save
// passwords in this case.
if (matched_manager_it != pending_login_managers_.end()) {
// Transfer ownership of the manager from |pending_login_managers_| to
// |manager|.
manager.reset(*matched_manager_it);
pending_login_managers_.weak_erase(matched_manager_it);
} else {
return;
}
// If we found a manager but it didn't finish matching yet, the user has
// tried to submit credentials before we had time to even find matching
// results for the given form and autofill. If this is the case, we just
// give up.
if (!manager->HasCompletedMatching())
return;
// Also get out of here if the user told us to 'never remember' passwords for
// this form.
if (manager->IsBlacklisted())
return;
// Bail if we're missing any of the necessary form components.
if (!manager->HasValidPasswordForm())
return;
// Always save generated passwords, as the user expresses explicit intent for
// Chrome to manage such passwords. For other passwords, respect the
// autocomplete attribute.
if (!manager->HasGeneratedPassword() && !form.password_autocomplete_set)
return;
PasswordForm provisionally_saved_form(form);
provisionally_saved_form.ssl_valid = form.origin.SchemeIsSecure() &&
!delegate_->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::SetObserver(LoginModelObserver* observer) {
observer_ = observer;
}
void PasswordManager::DidNavigateAnyFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
bool password_form_submitted = params.password_form.origin.is_valid();
// Try to save the password if one was submitted.
if (password_form_submitted)
ProvisionallySavePassword(params.password_form);
// Clear data after submission or main frame navigation. We don't want
// to clear data after subframe navigation as there might be password
// forms on other frames that could be submitted.
if (password_form_submitted || details.is_main_frame)
pending_login_managers_.clear();
}
bool PasswordManager::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PasswordManager, message)
IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordFormsParsed,
OnPasswordFormsParsed)
IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordFormsRendered,
OnPasswordFormsRendered)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PasswordManager::OnPasswordFormsParsed(
const std::vector<PasswordForm>& forms) {
// Ask the SSLManager for current security.
bool had_ssl_error = delegate_->DidLastPageLoadEncounterSSLErrors();
for (std::vector<PasswordForm>::const_iterator iter = forms.begin();
iter != forms.end(); ++iter) {
// Don't involve the password manager if this form corresponds to
// SpdyProxy authentication, as indicated by the realm.
if (EndsWith(iter->signon_realm, kSpdyProxyRealm, true))
continue;
bool ssl_valid = iter->origin.SchemeIsSecure() && !had_ssl_error;
PasswordFormManager* manager =
new PasswordFormManager(delegate_->GetProfile(),
this,
web_contents(),
*iter,
ssl_valid);
pending_login_managers_.push_back(manager);
manager->FetchMatchingLoginsFromPasswordStore();
}
}
void PasswordManager::OnPasswordFormsRendered(
const std::vector<PasswordForm>& visible_forms) {
if (!provisional_save_manager_.get())
return;
DCHECK(IsSavingEnabled());
// First, check for a failed login attempt.
for (std::vector<PasswordForm>::const_iterator iter = visible_forms.begin();
iter != visible_forms.end(); ++iter) {
if (provisional_save_manager_->DoesManage(
*iter, PasswordFormManager::ACTION_MATCH_REQUIRED)) {
// The form trying to be saved has immediately re-appeared. Assume login
// failure and abort this save, by clearing provisional_save_manager_.
provisional_save_manager_->SubmitFailed();
provisional_save_manager_.reset();
return;
}
}
if (!provisional_save_manager_->HasValidPasswordForm()) {
// Form is not completely valid - we do not support it.
NOTREACHED();
provisional_save_manager_.reset();
return;
}
// Looks like a successful login attempt. Either show an infobar or
// automatically save the login data. We prompt when the user hasn't already
// given consent, either through previously accepting the infobar or by having
// the browser generate the password.
provisional_save_manager_->SubmitPassed();
if (provisional_save_manager_->HasGeneratedPassword())
UMA_HISTOGRAM_COUNTS("PasswordGeneration.Submitted", 1);
if (provisional_save_manager_->IsNewLogin() &&
!provisional_save_manager_->HasGeneratedPassword()) {
delegate_->AddSavePasswordInfoBarIfPermitted(
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 (content::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, NULL));
trial->UseOneTimeRandomization();
base::FieldTrial::Probability enabled_probability = 0;
switch (chrome::VersionInfo::GetChannel()) {
case chrome::VersionInfo::CHANNEL_DEV:
case chrome::VersionInfo::CHANNEL_BETA:
enabled_probability = 50;
break;
default:
break;
}
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);
switch (form_for_autofill.scheme) {
case PasswordForm::SCHEME_HTML: {
// Note the check above is required because the observer_ for a non-HTML
// schemed password form may have been freed, so we need to distinguish.
autofill::PasswordFormFillData fill_data;
InitPasswordFormFillData(form_for_autofill,
best_matches,
&preferred_match,
wait_for_username,
OtherPossibleUsernamesEnabled(),
&fill_data);
delegate_->FillPasswordForm(fill_data);
return;
}
default:
if (observer_) {
observer_->OnAutofillDataAvailable(preferred_match.username_value,
preferred_match.password_value);
}
}
}
|