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/prefs/pref_service.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browsing_data/browsing_data_helper.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/channel_info.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/url_constants.h"
26 #include "components/autofill/content/browser/content_autofill_driver.h"
27 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
28 #include "components/autofill/content/common/autofill_messages.h"
29 #include "components/autofill/core/browser/password_generator.h"
30 #include "components/autofill/core/common/password_form.h"
31 #include "components/password_manager/content/browser/content_password_manager_driver.h"
32 #include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
33 #include "components/password_manager/content/common/credential_manager_messages.h"
34 #include "components/password_manager/core/browser/browser_save_password_progress_logger.h"
35 #include "components/password_manager/core/browser/log_receiver.h"
36 #include "components/password_manager/core/browser/password_form_manager.h"
37 #include "components/password_manager/core/browser/password_manager_internals_service.h"
38 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
39 #include "components/password_manager/core/browser/password_manager_util.h"
40 #include "components/password_manager/core/common/credential_manager_types.h"
41 #include "components/password_manager/core/common/password_manager_pref_names.h"
42 #include "components/password_manager/core/common/password_manager_switches.h"
43 #include "components/password_manager/sync/browser/sync_store_result_filter.h"
44 #include "components/version_info/version_info.h"
45 #include "content/public/browser/navigation_entry.h"
46 #include "content/public/browser/render_view_host.h"
47 #include "content/public/browser/web_contents.h"
48 #include "google_apis/gaia/gaia_urls.h"
49 #include "net/base/url_util.h"
50 #include "third_party/re2/re2/re2.h"
52 #if defined(OS_ANDROID)
53 #include "chrome/browser/password_manager/generated_password_saved_infobar_delegate_android.h"
56 using password_manager::ContentPasswordManagerDriverFactory
;
57 using password_manager::PasswordManagerInternalsService
;
58 using password_manager::PasswordManagerInternalsServiceFactory
;
60 // Shorten the name to spare line breaks. The code provides enough context
62 typedef autofill::SavePasswordProgressLogger Logger
;
64 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ChromePasswordManagerClient
);
67 // This routine is called when PasswordManagerClient is constructed.
68 // Currently we report metrics only once at startup. We require
69 // that this is only ever called from a single thread in order to
70 // avoid needing to lock (a static boolean flag is then sufficient to
71 // guarantee running only once).
72 void ReportMetrics(bool password_manager_enabled
,
73 password_manager::PasswordManagerClient
* client
) {
74 static base::PlatformThreadId initial_thread_id
=
75 base::PlatformThread::CurrentId();
76 DCHECK_EQ(base::PlatformThread::CurrentId(), initial_thread_id
);
78 static bool ran_once
= false;
83 password_manager::PasswordStore
* store
= client
->GetPasswordStore();
84 // May be null in tests.
86 store
->ReportMetrics(client
->GetSyncUsername(),
87 client
->GetPasswordSyncState() ==
88 password_manager::SYNCING_WITH_CUSTOM_PASSPHRASE
);
90 UMA_HISTOGRAM_BOOLEAN("PasswordManager.Enabled", password_manager_enabled
);
96 void ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
97 content::WebContents
* contents
,
98 autofill::AutofillClient
* autofill_client
) {
99 if (FromWebContents(contents
))
102 contents
->SetUserData(
104 new ChromePasswordManagerClient(contents
, autofill_client
));
107 ChromePasswordManagerClient::ChromePasswordManagerClient(
108 content::WebContents
* web_contents
,
109 autofill::AutofillClient
* autofill_client
)
110 : content::WebContentsObserver(web_contents
),
111 profile_(Profile::FromBrowserContext(web_contents
->GetBrowserContext())),
112 password_manager_(this),
113 driver_factory_(nullptr),
114 credential_manager_dispatcher_(web_contents
, this),
116 can_use_log_router_(false) {
117 ContentPasswordManagerDriverFactory::CreateForWebContents(web_contents
, this,
120 ContentPasswordManagerDriverFactory::FromWebContents(web_contents
);
122 PasswordManagerInternalsService
* service
=
123 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile_
);
125 can_use_log_router_
= service
->RegisterClient(this);
126 saving_passwords_enabled_
.Init(
127 password_manager::prefs::kPasswordManagerSavingEnabled
, GetPrefs());
128 ReportMetrics(*saving_passwords_enabled_
, this);
131 ChromePasswordManagerClient::~ChromePasswordManagerClient() {
132 PasswordManagerInternalsService
* service
=
133 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile_
);
135 service
->UnregisterClient(this);
138 bool ChromePasswordManagerClient::IsAutomaticPasswordSavingEnabled() const {
139 return base::CommandLine::ForCurrentProcess()->HasSwitch(
140 password_manager::switches::kEnableAutomaticPasswordSaving
) &&
141 chrome::GetChannel() == version_info::Channel::UNKNOWN
;
144 bool ChromePasswordManagerClient::IsPasswordManagementEnabledForCurrentPage()
146 DCHECK(web_contents());
147 content::NavigationEntry
* entry
=
148 web_contents()->GetController().GetLastCommittedEntry();
149 bool is_enabled
= false;
151 // TODO(gcasto): Determine if fix for crbug.com/388246 is relevant here.
153 } else if (IsURLPasswordWebsiteReauth(entry
->GetURL())) {
154 // Disable the password manager for online password management.
156 } else if (EnabledForSyncSignin()) {
159 // Do not fill nor save password when a user is signing in for sync. This
160 // is because users need to remember their password if they are syncing as
161 // this is effectively their master password.
162 is_enabled
= entry
->GetURL().host() != chrome::kChromeUIChromeSigninHost
;
164 if (IsLoggingActive()) {
165 password_manager::BrowserSavePasswordProgressLogger
logger(this);
167 Logger::STRING_PASSWORD_MANAGEMENT_ENABLED_FOR_CURRENT_PAGE
,
173 bool ChromePasswordManagerClient::IsSavingEnabledForCurrentPage() const {
174 return *saving_passwords_enabled_
&& !IsOffTheRecord() &&
175 !DidLastPageLoadEncounterSSLErrors() &&
176 IsPasswordManagementEnabledForCurrentPage();
179 std::string
ChromePasswordManagerClient::GetSyncUsername() const {
180 return password_manager_sync_metrics::GetSyncUsername(profile_
);
183 bool ChromePasswordManagerClient::IsSyncAccountCredential(
184 const std::string
& username
,
185 const std::string
& realm
) const {
186 return password_manager_sync_metrics::IsSyncAccountCredential(
187 profile_
, username
, realm
);
190 bool ChromePasswordManagerClient::PromptUserToSaveOrUpdatePassword(
191 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
,
192 password_manager::CredentialSourceType type
,
193 bool update_password
) {
194 // Save password infobar and the password bubble prompts in case of
195 // "webby" URLs and do not prompt in case of "non-webby" URLS (e.g. file://).
196 if (!BrowsingDataHelper::IsWebScheme(
197 web_contents()->GetLastCommittedURL().scheme())) {
201 if (IsTheHotNewBubbleUIEnabled()) {
202 ManagePasswordsUIController
* manage_passwords_ui_controller
=
203 ManagePasswordsUIController::FromWebContents(web_contents());
204 if (update_password
&& IsUpdatePasswordUIEnabled()) {
205 manage_passwords_ui_controller
->OnUpdatePasswordSubmitted(
206 form_to_save
.Pass());
208 manage_passwords_ui_controller
->OnPasswordSubmitted(form_to_save
.Pass());
211 if (form_to_save
->IsBlacklisted())
213 std::string
uma_histogram_suffix(
214 password_manager::metrics_util::GroupIdToString(
215 password_manager::metrics_util::MonitoredDomainGroupId(
216 form_to_save
->pending_credentials().signon_realm
, GetPrefs())));
217 SavePasswordInfoBarDelegate::Create(
218 web_contents(), form_to_save
.Pass(), uma_histogram_suffix
, type
);
223 bool ChromePasswordManagerClient::PromptUserToChooseCredentials(
224 ScopedVector
<autofill::PasswordForm
> local_forms
,
225 ScopedVector
<autofill::PasswordForm
> federated_forms
,
227 base::Callback
<void(const password_manager::CredentialInfo
&)> callback
) {
228 return ManagePasswordsUIController::FromWebContents(web_contents())->
229 OnChooseCredentials(local_forms
.Pass(), federated_forms
.Pass(), origin
,
233 void ChromePasswordManagerClient::ForceSavePassword() {
234 password_manager::ContentPasswordManagerDriver
* driver
=
235 driver_factory_
->GetDriverForFrame(web_contents()->GetFocusedFrame());
236 driver
->ForceSavePassword();
239 void ChromePasswordManagerClient::NotifyUserAutoSignin(
240 ScopedVector
<autofill::PasswordForm
> local_forms
) {
241 DCHECK(!local_forms
.empty());
242 ManagePasswordsUIController::FromWebContents(web_contents())->
243 OnAutoSignin(local_forms
.Pass());
246 void ChromePasswordManagerClient::AutomaticPasswordSave(
247 scoped_ptr
<password_manager::PasswordFormManager
> saved_form
) {
248 #if defined(OS_ANDROID)
249 GeneratedPasswordSavedInfoBarDelegateAndroid::Create(web_contents());
251 if (IsTheHotNewBubbleUIEnabled()) {
252 ManagePasswordsUIController
* manage_passwords_ui_controller
=
253 ManagePasswordsUIController::FromWebContents(web_contents());
254 manage_passwords_ui_controller
->OnAutomaticPasswordSave(
260 void ChromePasswordManagerClient::PasswordWasAutofilled(
261 const autofill::PasswordFormMap
& best_matches
) const {
262 ManagePasswordsUIController
* manage_passwords_ui_controller
=
263 ManagePasswordsUIController::FromWebContents(web_contents());
264 if (manage_passwords_ui_controller
&& IsTheHotNewBubbleUIEnabled())
265 manage_passwords_ui_controller
->OnPasswordAutofilled(best_matches
);
268 void ChromePasswordManagerClient::PasswordAutofillWasBlocked(
269 const autofill::PasswordFormMap
& best_matches
) const {
270 ManagePasswordsUIController
* controller
=
271 ManagePasswordsUIController::FromWebContents(web_contents());
272 if (controller
&& IsTheHotNewBubbleUIEnabled())
273 controller
->OnBlacklistBlockedAutofill(best_matches
);
276 void ChromePasswordManagerClient::HidePasswordGenerationPopup() {
277 if (popup_controller_
)
278 popup_controller_
->HideAndDestroy();
281 PrefService
* ChromePasswordManagerClient::GetPrefs() {
282 return profile_
->GetPrefs();
285 password_manager::PasswordStore
*
286 ChromePasswordManagerClient::GetPasswordStore() const {
287 // Always use EXPLICIT_ACCESS as the password manager checks IsOffTheRecord
288 // itself when it shouldn't access the PasswordStore.
289 // TODO(gcasto): Is is safe to change this to
290 // ServiceAccessType::IMPLICIT_ACCESS?
291 return PasswordStoreFactory::GetForProfile(
292 profile_
, ServiceAccessType::EXPLICIT_ACCESS
).get();
295 password_manager::PasswordSyncState
296 ChromePasswordManagerClient::GetPasswordSyncState() const {
297 const ProfileSyncService
* sync_service
=
298 ProfileSyncServiceFactory::GetForProfile(profile_
);
299 return password_manager_util::GetPasswordSyncState(sync_service
);
302 void ChromePasswordManagerClient::OnLogRouterAvailabilityChanged(
303 bool router_can_be_used
) {
304 if (can_use_log_router_
== router_can_be_used
)
306 can_use_log_router_
= router_can_be_used
;
308 NotifyRendererOfLoggingAvailability();
311 void ChromePasswordManagerClient::LogSavePasswordProgress(
312 const std::string
& text
) const {
313 if (!IsLoggingActive())
315 PasswordManagerInternalsService
* service
=
316 PasswordManagerInternalsServiceFactory::GetForBrowserContext(profile_
);
318 service
->ProcessLog(text
);
321 bool ChromePasswordManagerClient::IsLoggingActive() const {
322 // WebUI tabs do not need to log password saving progress. In particular, the
323 // internals page itself should not send any logs.
324 return can_use_log_router_
&& !web_contents()->GetWebUI();
327 bool ChromePasswordManagerClient::WasLastNavigationHTTPError() const {
328 DCHECK(web_contents());
330 scoped_ptr
<password_manager::BrowserSavePasswordProgressLogger
> logger
;
331 if (IsLoggingActive()) {
332 logger
.reset(new password_manager::BrowserSavePasswordProgressLogger(this));
334 Logger::STRING_WAS_LAST_NAVIGATION_HTTP_ERROR_METHOD
);
337 content::NavigationEntry
* entry
=
338 web_contents()->GetController().GetVisibleEntry();
341 int http_status_code
= entry
->GetHttpStatusCode();
344 logger
->LogNumber(Logger::STRING_HTTP_STATUS_CODE
, http_status_code
);
346 if (http_status_code
>= 400 && http_status_code
< 600)
351 bool ChromePasswordManagerClient::DidLastPageLoadEncounterSSLErrors() const {
352 content::NavigationEntry
* entry
=
353 web_contents()->GetController().GetLastCommittedEntry();
354 bool ssl_errors
= true;
358 ssl_errors
= net::IsCertStatusError(entry
->GetSSL().cert_status
);
360 if (IsLoggingActive()) {
361 password_manager::BrowserSavePasswordProgressLogger
logger(this);
362 logger
.LogBoolean(Logger::STRING_SSL_ERRORS_PRESENT
, ssl_errors
);
367 bool ChromePasswordManagerClient::IsOffTheRecord() const {
368 return web_contents()->GetBrowserContext()->IsOffTheRecord();
371 password_manager::PasswordManager
*
372 ChromePasswordManagerClient::GetPasswordManager() {
373 return &password_manager_
;
376 autofill::AutofillManager
*
377 ChromePasswordManagerClient::GetAutofillManagerForMainFrame() {
378 autofill::ContentAutofillDriverFactory
* factory
=
379 autofill::ContentAutofillDriverFactory::FromWebContents(web_contents());
381 ? factory
->DriverForFrame(web_contents()->GetMainFrame())
386 void ChromePasswordManagerClient::SetTestObserver(
387 autofill::PasswordGenerationPopupObserver
* observer
) {
388 observer_
= observer
;
391 bool ChromePasswordManagerClient::OnMessageReceived(
392 const IPC::Message
& message
,
393 content::RenderFrameHost
* render_frame_host
) {
395 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(ChromePasswordManagerClient
, message
,
397 // Autofill messages:
398 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordGenerationPopup
,
399 ShowPasswordGenerationPopup
)
400 IPC_MESSAGE_HANDLER(AutofillHostMsg_ShowPasswordEditingPopup
,
401 ShowPasswordEditingPopup
)
402 IPC_END_MESSAGE_MAP()
404 IPC_BEGIN_MESSAGE_MAP(ChromePasswordManagerClient
, message
)
405 IPC_MESSAGE_HANDLER(AutofillHostMsg_HidePasswordGenerationPopup
,
406 HidePasswordGenerationPopup
)
407 IPC_MESSAGE_HANDLER(AutofillHostMsg_PasswordAutofillAgentConstructed
,
408 NotifyRendererOfLoggingAvailability
)
410 IPC_MESSAGE_UNHANDLED(handled
= false)
411 IPC_END_MESSAGE_MAP()
416 gfx::RectF
ChromePasswordManagerClient::GetBoundsInScreenSpace(
417 const gfx::RectF
& bounds
) {
418 gfx::Rect client_area
= web_contents()->GetContainerBounds();
419 return bounds
+ client_area
.OffsetFromOrigin();
422 void ChromePasswordManagerClient::ShowPasswordGenerationPopup(
423 content::RenderFrameHost
* render_frame_host
,
424 const gfx::RectF
& bounds
,
426 const autofill::PasswordForm
& form
) {
427 // TODO(gcasto): Validate data in PasswordForm.
429 gfx::RectF element_bounds_in_screen_space
= GetBoundsInScreenSpace(bounds
);
432 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
433 popup_controller_
, element_bounds_in_screen_space
, form
, max_length
,
435 driver_factory_
->GetDriverForFrame(render_frame_host
), observer_
,
436 web_contents(), web_contents()->GetNativeView());
437 popup_controller_
->Show(true /* display_password */);
440 void ChromePasswordManagerClient::ShowPasswordEditingPopup(
441 content::RenderFrameHost
* render_frame_host
,
442 const gfx::RectF
& bounds
,
443 const autofill::PasswordForm
& form
) {
444 gfx::RectF element_bounds_in_screen_space
= GetBoundsInScreenSpace(bounds
);
446 autofill::PasswordGenerationPopupControllerImpl::GetOrCreate(
447 popup_controller_
, element_bounds_in_screen_space
, form
,
448 0, // Unspecified max length.
450 driver_factory_
->GetDriverForFrame(render_frame_host
), observer_
,
451 web_contents(), web_contents()->GetNativeView());
452 popup_controller_
->Show(false /* display_password */);
455 void ChromePasswordManagerClient::NotifyRendererOfLoggingAvailability() {
459 web_contents()->GetRenderViewHost()->Send(new AutofillMsg_SetLoggingState(
460 web_contents()->GetRenderViewHost()->GetRoutingID(),
461 can_use_log_router_
));
464 bool ChromePasswordManagerClient::IsURLPasswordWebsiteReauth(
465 const GURL
& url
) const {
466 if (url
.GetOrigin() != GaiaUrls::GetInstance()->gaia_url().GetOrigin())
469 // "rart" param signals this page is for transactional reauth.
470 std::string param_value
;
471 if (!net::GetValueForKeyInQuery(url
, "rart", ¶m_value
))
474 // Check the "continue" param to see if this reauth page is for the passwords
477 if (!net::GetValueForKeyInQuery(url
, "continue", ¶m_value
))
480 // All password sites, including test sites, have autofilling disabled.
481 CR_DEFINE_STATIC_LOCAL(RE2
, account_dashboard_pattern
,
482 ("passwords(-([a-z-]+\\.corp))?\\.google\\.com"));
484 return RE2::FullMatch(GURL(param_value
).host(), account_dashboard_pattern
);
487 bool ChromePasswordManagerClient::IsTheHotNewBubbleUIEnabled() {
488 #if !defined(USE_AURA) && !defined(OS_MACOSX)
491 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
492 if (command_line
->HasSwitch(switches::kDisableSavePasswordBubble
))
495 if (command_line
->HasSwitch(switches::kEnableSavePasswordBubble
))
498 std::string group_name
=
499 base::FieldTrialList::FindFullName("PasswordManagerUI");
501 // The bubble should be the default case that runs on the bots.
502 return group_name
!= "Infobar";
505 bool ChromePasswordManagerClient::IsUpdatePasswordUIEnabled() const {
506 if (!ChromePasswordManagerClient::IsTheHotNewBubbleUIEnabled()) {
507 // Currently Password update UI is implemented only for Bubble UI.
510 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
511 return command_line
->HasSwitch(
512 password_manager::switches::kEnablePasswordChangeSupport
);
515 bool ChromePasswordManagerClient::EnabledForSyncSignin() {
516 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
517 if (command_line
->HasSwitch(
518 password_manager::switches::kDisableManagerForSyncSignin
))
521 if (command_line
->HasSwitch(
522 password_manager::switches::kEnableManagerForSyncSignin
))
525 // Default is enabled.
526 std::string group_name
=
527 base::FieldTrialList::FindFullName("PasswordManagerStateForSyncSignin");
528 return group_name
!= "Disabled";
531 const GURL
& ChromePasswordManagerClient::GetMainFrameURL() const {
532 return web_contents()->GetVisibleURL();
535 const GURL
& ChromePasswordManagerClient::GetLastCommittedEntryURL() const {
536 DCHECK(web_contents());
537 content::NavigationEntry
* entry
=
538 web_contents()->GetController().GetLastCommittedEntry();
540 return GURL::EmptyGURL();
542 return entry
->GetURL();
545 scoped_ptr
<password_manager::CredentialsFilter
>
546 ChromePasswordManagerClient::CreateStoreResultFilter() const {
547 return make_scoped_ptr(new password_manager::SyncStoreResultFilter(this));