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 "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browsing_data/browsing_data_helper.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/save_password_infobar_delegate.h"
17 #include "chrome/browser/password_manager/sync_metrics.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/sync/profile_sync_service.h"
20 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/ui/autofill/password_generation_popup_controller_impl.h"
22 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/common/url_constants.h"
26 #include "components/autofill/content/common/autofill_messages.h"
27 #include "components/autofill/core/browser/password_generator.h"
28 #include "components/autofill/core/common/password_form.h"
29 #include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
30 #include "components/password_manager/content/common/credential_manager_messages.h"
31 #include "components/password_manager/content/common/credential_manager_types.h"
32 #include "components/password_manager/core/browser/log_receiver.h"
33 #include "components/password_manager/core/browser/password_form_manager.h"
34 #include "components/password_manager/core/browser/password_manager.h"
35 #include "components/password_manager/core/browser/password_manager_internals_service.h"
36 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
37 #include "components/password_manager/core/common/password_manager_switches.h"
38 #include "content/public/browser/navigation_entry.h"
39 #include "content/public/browser/render_view_host.h"
40 #include "content/public/browser/web_contents.h"
41 #include "google_apis/gaia/gaia_urls.h"
42 #include "net/base/url_util.h"
43 #include "third_party/re2/re2/re2.h"
45 using password_manager::PasswordManagerInternalsService
;
46 using password_manager::PasswordManagerInternalsServiceFactory
;
48 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromePasswordManagerClient
);
51 void ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
52 content::WebContents
* contents
,
53 autofill::AutofillClient
* autofill_client
) {
54 if (FromWebContents(contents
))
57 contents
->SetUserData(
59 new ChromePasswordManagerClient(contents
, autofill_client
));
62 ChromePasswordManagerClient::ChromePasswordManagerClient(
63 content::WebContents
* web_contents
,
64 autofill::AutofillClient
* autofill_client
)
65 : content::WebContentsObserver(web_contents
),
66 profile_(Profile::FromBrowserContext(web_contents
->GetBrowserContext())),
67 driver_(web_contents
, this, autofill_client
),
68 credential_manager_dispatcher_(web_contents
, this),
70 can_use_log_router_(false),
71 autofill_sync_state_(ALLOW_SYNC_CREDENTIALS
),
72 sync_credential_was_filtered_(false) {
73 PasswordManagerInternalsService
* service
=
74 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile_
);
76 can_use_log_router_
= service
->RegisterClient(this);
77 SetUpAutofillSyncState();
80 ChromePasswordManagerClient::~ChromePasswordManagerClient() {
81 PasswordManagerInternalsService
* service
=
82 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile_
);
84 service
->UnregisterClient(this);
87 bool ChromePasswordManagerClient::IsAutomaticPasswordSavingEnabled() const {
88 return CommandLine::ForCurrentProcess()->HasSwitch(
89 password_manager::switches::kEnableAutomaticPasswordSaving
) &&
90 chrome::VersionInfo::GetChannel() ==
91 chrome::VersionInfo::CHANNEL_UNKNOWN
;
94 bool ChromePasswordManagerClient::IsPasswordManagerEnabledForCurrentPage()
96 DCHECK(web_contents());
97 content::NavigationEntry
* entry
=
98 web_contents()->GetController().GetLastCommittedEntry();
100 // TODO(gcasto): Determine if fix for crbug.com/388246 is relevant here.
104 // Disable the password manager for online password management.
105 if (IsURLPasswordWebsiteReauth(entry
->GetURL()))
108 if (EnabledForSyncSignin())
111 // Do not fill nor save password when a user is signing in for sync. This
112 // is because users need to remember their password if they are syncing as
113 // this is effectively their master password.
114 return entry
->GetURL().host() != chrome::kChromeUIChromeSigninHost
;
117 bool ChromePasswordManagerClient::ShouldFilterAutofillResult(
118 const autofill::PasswordForm
& form
) {
119 if (!IsSyncAccountCredential(base::UTF16ToUTF8(form
.username_value
),
123 if (autofill_sync_state_
== DISALLOW_SYNC_CREDENTIALS
) {
124 sync_credential_was_filtered_
= true;
128 if (autofill_sync_state_
== DISALLOW_SYNC_CREDENTIALS_FOR_REAUTH
&&
129 LastLoadWasTransactionalReauthPage()) {
130 sync_credential_was_filtered_
= true;
137 bool ChromePasswordManagerClient::IsSyncAccountCredential(
138 const std::string
& username
, const std::string
& origin
) const {
139 return password_manager_sync_metrics::IsSyncAccountCredential(
140 profile_
, username
, origin
);
143 void ChromePasswordManagerClient::AutofillResultsComputed() {
144 UMA_HISTOGRAM_BOOLEAN("PasswordManager.SyncCredentialFiltered",
145 sync_credential_was_filtered_
);
146 sync_credential_was_filtered_
= false;
149 bool ChromePasswordManagerClient::PromptUserToSavePassword(
150 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
) {
151 // Save password infobar and the password bubble prompts in case of
152 // "webby" URLs and do not prompt in case of "non-webby" URLS (e.g. file://).
153 if (!BrowsingDataHelper::IsWebScheme(
154 web_contents()->GetLastCommittedURL().scheme())) {
158 if (IsTheHotNewBubbleUIEnabled()) {
159 ManagePasswordsUIController
* manage_passwords_ui_controller
=
160 ManagePasswordsUIController::FromWebContents(web_contents());
161 manage_passwords_ui_controller
->OnPasswordSubmitted(form_to_save
.Pass());
163 std::string
uma_histogram_suffix(
164 password_manager::metrics_util::GroupIdToString(
165 password_manager::metrics_util::MonitoredDomainGroupId(
166 form_to_save
->realm(), GetPrefs())));
167 SavePasswordInfoBarDelegate::Create(
168 web_contents(), form_to_save
.Pass(), uma_histogram_suffix
);
173 void ChromePasswordManagerClient::AutomaticPasswordSave(
174 scoped_ptr
<password_manager::PasswordFormManager
> saved_form
) {
175 if (IsTheHotNewBubbleUIEnabled()) {
176 ManagePasswordsUIController
* manage_passwords_ui_controller
=
177 ManagePasswordsUIController::FromWebContents(web_contents());
178 manage_passwords_ui_controller
->OnAutomaticPasswordSave(
183 void ChromePasswordManagerClient::PasswordWasAutofilled(
184 const autofill::PasswordFormMap
& best_matches
) const {
185 ManagePasswordsUIController
* manage_passwords_ui_controller
=
186 ManagePasswordsUIController::FromWebContents(web_contents());
187 if (manage_passwords_ui_controller
&& IsTheHotNewBubbleUIEnabled())
188 manage_passwords_ui_controller
->OnPasswordAutofilled(best_matches
);
191 void ChromePasswordManagerClient::PasswordAutofillWasBlocked(
192 const autofill::PasswordFormMap
& best_matches
) const {
193 ManagePasswordsUIController
* controller
=
194 ManagePasswordsUIController::FromWebContents(web_contents());
195 if (controller
&& IsTheHotNewBubbleUIEnabled())
196 controller
->OnBlacklistBlockedAutofill(best_matches
);
199 void ChromePasswordManagerClient::HidePasswordGenerationPopup() {
200 if (popup_controller_
)
201 popup_controller_
->HideAndDestroy();
204 PrefService
* ChromePasswordManagerClient::GetPrefs() {
205 return profile_
->GetPrefs();
208 password_manager::PasswordStore
*
209 ChromePasswordManagerClient::GetPasswordStore() {
210 // Always use EXPLICIT_ACCESS as the password manager checks IsOffTheRecord
211 // itself when it shouldn't access the PasswordStore.
212 // TODO(gcasto): Is is safe to change this to Profile::IMPLICIT_ACCESS?
213 return PasswordStoreFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
)
217 password_manager::PasswordManagerDriver
*
218 ChromePasswordManagerClient::GetDriver() {
222 base::FieldTrial::Probability
223 ChromePasswordManagerClient::GetProbabilityForExperiment(
224 const std::string
& experiment_name
) {
225 base::FieldTrial::Probability enabled_probability
= 0;
226 if (experiment_name
==
227 password_manager::PasswordManager::kOtherPossibleUsernamesExperiment
) {
228 switch (chrome::VersionInfo::GetChannel()) {
229 case chrome::VersionInfo::CHANNEL_DEV
:
230 case chrome::VersionInfo::CHANNEL_BETA
:
231 enabled_probability
= 50;
237 return enabled_probability
;
240 bool ChromePasswordManagerClient::IsPasswordSyncEnabled() {
241 ProfileSyncService
* sync_service
=
242 ProfileSyncServiceFactory::GetForProfile(profile_
);
243 // Don't consider sync enabled if the user has a custom passphrase. See
244 // crbug.com/358998 for more details.
246 sync_service
->HasSyncSetupCompleted() &&
247 sync_service
->SyncActive() &&
248 !sync_service
->IsUsingSecondaryPassphrase()) {
249 return sync_service
->GetActiveDataTypes().Has(syncer::PASSWORDS
);
254 void ChromePasswordManagerClient::OnLogRouterAvailabilityChanged(
255 bool router_can_be_used
) {
256 if (can_use_log_router_
== router_can_be_used
)
258 can_use_log_router_
= router_can_be_used
;
260 NotifyRendererOfLoggingAvailability();
263 void ChromePasswordManagerClient::LogSavePasswordProgress(
264 const std::string
& text
) {
265 if (!IsLoggingActive())
267 PasswordManagerInternalsService
* service
=
268 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile_
);
270 service
->ProcessLog(text
);
273 bool ChromePasswordManagerClient::IsLoggingActive() const {
274 // WebUI tabs do not need to log password saving progress. In particular, the
275 // internals page itself should not send any logs.
276 return can_use_log_router_
&& !web_contents()->GetWebUI();
280 password_manager::PasswordGenerationManager
*
281 ChromePasswordManagerClient::GetGenerationManagerFromWebContents(
282 content::WebContents
* contents
) {
283 ChromePasswordManagerClient
* client
=
284 ChromePasswordManagerClient::FromWebContents(contents
);
287 return client
->GetDriver()->GetPasswordGenerationManager();
291 password_manager::PasswordManager
*
292 ChromePasswordManagerClient::GetManagerFromWebContents(
293 content::WebContents
* contents
) {
294 ChromePasswordManagerClient
* client
=
295 ChromePasswordManagerClient::FromWebContents(contents
);
298 return client
->GetDriver()->GetPasswordManager();
301 void ChromePasswordManagerClient::SetTestObserver(
302 autofill::PasswordGenerationPopupObserver
* observer
) {
303 observer_
= observer
;
306 bool ChromePasswordManagerClient::OnMessageReceived(
307 const IPC::Message
& message
) {
309 IPC_BEGIN_MESSAGE_MAP(ChromePasswordManagerClient
, message
)
310 // Autofill messages:
311 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordGenerationPopup
,
312 ShowPasswordGenerationPopup
)
313 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordEditingPopup
,
314 ShowPasswordEditingPopup
)
315 IPC_MESSAGE_HANDLER(AutofillHostMsg_HidePasswordGenerationPopup
,
316 HidePasswordGenerationPopup
)
317 IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordAutofillAgentConstructed
,
318 NotifyRendererOfLoggingAvailability
)
321 IPC_MESSAGE_UNHANDLED(handled
= false)
322 IPC_END_MESSAGE_MAP()
326 gfx::RectF
ChromePasswordManagerClient::GetBoundsInScreenSpace(
327 const gfx::RectF
& bounds
) {
328 gfx::Rect client_area
= web_contents()->GetContainerBounds();
329 return bounds
+ client_area
.OffsetFromOrigin();
332 void ChromePasswordManagerClient::ShowPasswordGenerationPopup(
333 const gfx::RectF
& bounds
,
335 const autofill::PasswordForm
& form
) {
336 // TODO(gcasto): Validate data in PasswordForm.
338 // Not yet implemented on other platforms.
339 #if defined(USE_AURA) || defined(OS_MACOSX)
340 gfx::RectF element_bounds_in_screen_space
= GetBoundsInScreenSpace(bounds
);
343 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
345 element_bounds_in_screen_space
,
348 driver_
.GetPasswordManager(),
351 web_contents()->GetNativeView());
352 popup_controller_
->Show(true /* display_password */);
353 #endif // defined(USE_AURA) || defined(OS_MACOSX)
356 void ChromePasswordManagerClient::ShowPasswordEditingPopup(
357 const gfx::RectF
& bounds
,
358 const autofill::PasswordForm
& form
) {
359 gfx::RectF element_bounds_in_screen_space
= GetBoundsInScreenSpace(bounds
);
360 // Not yet implemented on other platforms.
361 #if defined(USE_AURA) || defined(OS_MACOSX)
363 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
365 element_bounds_in_screen_space
,
367 0, // Unspecified max length.
368 driver_
.GetPasswordManager(),
371 web_contents()->GetNativeView());
372 popup_controller_
->Show(false /* display_password */);
373 #endif // defined(USE_AURA) || defined(OS_MACOSX)
376 void ChromePasswordManagerClient::NotifyRendererOfLoggingAvailability() {
380 web_contents()->GetRenderViewHost()->Send(new AutofillMsg_SetLoggingState(
381 web_contents()->GetRenderViewHost()->GetRoutingID(),
382 can_use_log_router_
));
385 bool ChromePasswordManagerClient::LastLoadWasTransactionalReauthPage() const {
386 DCHECK(web_contents());
387 content::NavigationEntry
* entry
=
388 web_contents()->GetController().GetLastCommittedEntry();
392 if (entry
->GetURL().GetOrigin() !=
393 GaiaUrls::GetInstance()->gaia_url().GetOrigin())
396 // "rart" is the transactional reauth paramter.
397 std::string ignored_value
;
398 return net::GetValueForKeyInQuery(entry
->GetURL(),
403 bool ChromePasswordManagerClient::IsURLPasswordWebsiteReauth(
404 const GURL
& url
) const {
405 if (url
.GetOrigin() != GaiaUrls::GetInstance()->gaia_url().GetOrigin())
408 // "rart" param signals this page is for transactional reauth.
409 std::string param_value
;
410 if (!net::GetValueForKeyInQuery(url
, "rart", ¶m_value
))
413 // Check the "continue" param to see if this reauth page is for the passwords
416 if (!net::GetValueForKeyInQuery(url
, "continue", ¶m_value
))
419 // All password sites, including test sites, have autofilling disabled.
420 CR_DEFINE_STATIC_LOCAL(RE2
, account_dashboard_pattern
,
421 ("passwords(-([a-z-]+\\.corp))?\\.google\\.com"));
423 return RE2::FullMatch(GURL(param_value
).host(), account_dashboard_pattern
);
426 bool ChromePasswordManagerClient::IsTheHotNewBubbleUIEnabled() {
427 #if !defined(USE_AURA) && !defined(OS_MACOSX)
430 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
431 if (command_line
->HasSwitch(switches::kDisableSavePasswordBubble
))
434 if (command_line
->HasSwitch(switches::kEnableSavePasswordBubble
))
437 std::string group_name
=
438 base::FieldTrialList::FindFullName("PasswordManagerUI");
440 // The bubble should be the default case that runs on the bots.
441 return group_name
!= "Infobar";
444 bool ChromePasswordManagerClient::EnabledForSyncSignin() {
445 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
446 if (command_line
->HasSwitch(
447 password_manager::switches::kDisableManagerForSyncSignin
))
450 if (command_line
->HasSwitch(
451 password_manager::switches::kEnableManagerForSyncSignin
))
454 // Default is enabled.
455 std::string group_name
=
456 base::FieldTrialList::FindFullName("PasswordManagerStateForSyncSignin");
457 return group_name
!= "Disabled";
460 void ChromePasswordManagerClient::SetUpAutofillSyncState() {
461 std::string group_name
=
462 base::FieldTrialList::FindFullName("AutofillSyncCredential");
464 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
465 if (command_line
->HasSwitch(
466 password_manager::switches::kAllowAutofillSyncCredential
)) {
467 autofill_sync_state_
= ALLOW_SYNC_CREDENTIALS
;
470 if (command_line
->HasSwitch(
471 password_manager::switches::
472 kDisallowAutofillSyncCredentialForReauth
)) {
473 autofill_sync_state_
= DISALLOW_SYNC_CREDENTIALS_FOR_REAUTH
;
476 if (command_line
->HasSwitch(
477 password_manager::switches::kDisallowAutofillSyncCredential
)) {
478 autofill_sync_state_
= DISALLOW_SYNC_CREDENTIALS
;
482 if (group_name
== "DisallowSyncCredentialsForReauth") {
483 autofill_sync_state_
= DISALLOW_SYNC_CREDENTIALS_FOR_REAUTH
;
484 } else if (group_name
== "DisallowSyncCredentials") {
485 autofill_sync_state_
= DISALLOW_SYNC_CREDENTIALS
;
488 autofill_sync_state_
= ALLOW_SYNC_CREDENTIALS
;