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/save_password_infobar_delegate.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/infobars/infobar.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/password_manager/password_form_manager.h"
12 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
13 #include "chrome/common/profile_management_switches.h"
14 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/web_contents.h"
17 #include "google_apis/gaia/gaia_urls.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
24 void SavePasswordInfoBarDelegate::Create(
25 content::WebContents
* web_contents
,
26 PasswordFormManager
* form_to_save
,
27 const std::string
& uma_histogram_suffix
) {
28 #if defined(ENABLE_ONE_CLICK_SIGNIN)
29 // Don't show the password manager infobar if this form is for a google
30 // account and we are going to show the one-click signin infobar.
31 GURL
realm(form_to_save
->realm());
32 // TODO(mathp): Checking only against associated_username() causes a bug
33 // referenced here: crbug.com/133275
34 // TODO(vabr): The check IsEnableWebBasedSignin is a hack for the time when
35 // OneClickSignin is disabled. http://crbug.com/339804
36 if (((realm
== GaiaUrls::GetInstance()->gaia_login_form_realm()) ||
37 (realm
== GURL("https://www.google.com/"))) &&
38 switches::IsEnableWebBasedSignin() &&
39 OneClickSigninHelper::CanOffer(
41 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY
,
42 base::UTF16ToUTF8(form_to_save
->associated_username()),
47 InfoBarService::FromWebContents(web_contents
)->AddInfoBar(
48 SavePasswordInfoBarDelegate::CreateInfoBar(
49 scoped_ptr
<SavePasswordInfoBarDelegate
>(
50 new SavePasswordInfoBarDelegate(form_to_save
,
51 uma_histogram_suffix
))));
54 SavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
55 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
56 infobar_response_
, NUM_RESPONSE_TYPES
);
58 // The shortest period for which the prompt needs to live, so that we don't
59 // consider it killed prematurely, as might happen, e.g., if a pre-rendered
60 // page gets swapped in (and the current WebContents is destroyed).
61 const base::TimeDelta kMinimumPromptDisplayTime
=
62 base::TimeDelta::FromSeconds(1);
64 if (!uma_histogram_suffix_
.empty()) {
65 password_manager_metrics_util::LogUMAHistogramEnumeration(
66 "PasswordManager.SavePasswordPromptResponse_" + uma_histogram_suffix_
,
67 infobar_response_
, NUM_RESPONSE_TYPES
);
68 password_manager_metrics_util::LogUMAHistogramBoolean(
69 "PasswordManager.SavePasswordPromptDisappearedQuickly_" +
70 uma_histogram_suffix_
,
71 timer_
.Elapsed() < kMinimumPromptDisplayTime
);
75 void SavePasswordInfoBarDelegate::SetUseAdditionalPasswordAuthentication(
76 bool use_additional_authentication
) {
77 form_to_save_
->SetUseAdditionalPasswordAuthentication(
78 use_additional_authentication
);
81 SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
82 PasswordFormManager
* form_to_save
,
83 const std::string
& uma_histogram_suffix
)
84 : ConfirmInfoBarDelegate(),
85 form_to_save_(form_to_save
),
86 infobar_response_(NO_RESPONSE
),
87 uma_histogram_suffix_(uma_histogram_suffix
) {
88 if (!uma_histogram_suffix_
.empty()) {
89 password_manager_metrics_util::LogUMAHistogramBoolean(
90 "PasswordManager.SavePasswordPromptDisplayed_" + uma_histogram_suffix_
,
95 #if !defined(OS_ANDROID)
96 // On Android, the save password infobar supports an additional checkbox to
97 // require additional authentication before autofilling a saved password.
98 // Because of this non-standard UI, the Android version is special cased and
100 // chrome/browser/ui/android/infobars/save_password_infobar.cc
103 scoped_ptr
<InfoBar
> SavePasswordInfoBarDelegate::CreateInfoBar(
104 scoped_ptr
<SavePasswordInfoBarDelegate
> delegate
) {
105 return ConfirmInfoBarDelegate::CreateInfoBar(
106 delegate
.PassAs
<ConfirmInfoBarDelegate
>());
110 bool SavePasswordInfoBarDelegate::ShouldExpire(
111 const content::LoadCommittedDetails
& details
) const {
112 bool is_not_redirect
= !(details
.entry
->GetTransitionType() &
113 content::PAGE_TRANSITION_IS_REDIRECT_MASK
);
114 return is_not_redirect
&& InfoBarDelegate::ShouldExpire(details
);
117 int SavePasswordInfoBarDelegate::GetIconID() const {
118 return IDR_INFOBAR_SAVE_PASSWORD
;
121 InfoBarDelegate::Type
SavePasswordInfoBarDelegate::GetInfoBarType() const {
122 return PAGE_ACTION_TYPE
;
125 base::string16
SavePasswordInfoBarDelegate::GetMessageText() const {
126 return l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT
);
129 base::string16
SavePasswordInfoBarDelegate::GetButtonLabel(
130 InfoBarButton button
) const {
131 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
132 IDS_PASSWORD_MANAGER_SAVE_BUTTON
: IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON
);
135 bool SavePasswordInfoBarDelegate::Accept() {
136 DCHECK(form_to_save_
.get());
137 form_to_save_
->Save();
138 infobar_response_
= REMEMBER_PASSWORD
;
142 bool SavePasswordInfoBarDelegate::Cancel() {
143 DCHECK(form_to_save_
.get());
144 form_to_save_
->PermanentlyBlacklist();
145 infobar_response_
= NEVER_REMEMBER_PASSWORD
;
149 void SavePasswordInfoBarDelegate::InfoBarDismissed() {
150 DCHECK(form_to_save_
.get());
151 infobar_response_
= INFOBAR_DISMISSED
;
154 InfoBarDelegate::InfoBarAutomationType
155 SavePasswordInfoBarDelegate::GetInfoBarAutomationType() const {
156 return PASSWORD_INFOBAR
;