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 "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/profile_sync_service.h"
11 #include "chrome/browser/sync/profile_sync_service_factory.h"
12 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
13 #include "chrome/grit/chromium_strings.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/infobars/core/infobar.h"
16 #include "components/password_manager/core/browser/password_bubble_experiment.h"
17 #include "components/password_manager/core/browser/password_manager_client.h"
18 #include "content/public/browser/web_contents.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
24 int GetCancelButtonText(password_manager::CredentialSourceType source_type
) {
26 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
27 ? IDS_PASSWORD_MANAGER_SAVE_PASSWORD_SMART_LOCK_NO_THANKS_BUTTON
28 : IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON
;
34 void SavePasswordInfoBarDelegate::Create(
35 content::WebContents
* web_contents
,
36 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
,
37 const std::string
& uma_histogram_suffix
,
38 password_manager::CredentialSourceType source_type
) {
40 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
41 sync_driver::SyncService
* sync_service
=
42 ProfileSyncServiceFactory::GetForProfile(profile
);
43 bool is_smartlock_branding_enabled
=
44 password_bubble_experiment::IsSmartLockBrandingEnabled(sync_service
);
45 bool should_show_first_run_experience
=
46 password_bubble_experiment::ShouldShowSavePromptFirstRunExperience(
47 sync_service
, profile
->GetPrefs());
48 InfoBarService::FromWebContents(web_contents
)
49 ->AddInfoBar(CreateSavePasswordInfoBar(
50 make_scoped_ptr(new SavePasswordInfoBarDelegate(
51 web_contents
, form_to_save
.Pass(), uma_histogram_suffix
,
52 source_type
, is_smartlock_branding_enabled
,
53 should_show_first_run_experience
))));
56 SavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
57 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
59 password_manager::metrics_util::NUM_RESPONSE_TYPES
);
61 password_manager::metrics_util::LogUIDismissalReason(infobar_response_
);
63 // The shortest period for which the prompt needs to live, so that we don't
64 // consider it killed prematurely, as might happen, e.g., if a pre-rendered
65 // page gets swapped in (and the current WebContents is destroyed).
66 const base::TimeDelta kMinimumPromptDisplayTime
=
67 base::TimeDelta::FromSeconds(1);
69 if (!uma_histogram_suffix_
.empty()) {
70 password_manager::metrics_util::LogUMAHistogramEnumeration(
71 "PasswordManager.SavePasswordPromptResponse_" + uma_histogram_suffix_
,
73 password_manager::metrics_util::NUM_RESPONSE_TYPES
);
74 password_manager::metrics_util::LogUMAHistogramBoolean(
75 "PasswordManager.SavePasswordPromptDisappearedQuickly_" +
76 uma_histogram_suffix_
,
77 timer_
.Elapsed() < kMinimumPromptDisplayTime
);
79 if (should_show_first_run_experience_
) {
81 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
82 password_bubble_experiment::RecordSavePromptFirstRunExperienceWasShown(
87 SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
88 content::WebContents
* web_contents
,
89 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
,
90 const std::string
& uma_histogram_suffix
,
91 password_manager::CredentialSourceType source_type
,
92 bool is_smartlock_branding_enabled
,
93 bool should_show_first_run_experience
)
94 : ConfirmInfoBarDelegate(),
95 form_to_save_(form_to_save
.Pass()),
96 infobar_response_(password_manager::metrics_util::NO_RESPONSE
),
97 uma_histogram_suffix_(uma_histogram_suffix
),
98 source_type_(source_type
),
99 should_show_first_run_experience_(should_show_first_run_experience
),
100 web_contents_(web_contents
) {
101 if (!uma_histogram_suffix_
.empty()) {
102 password_manager::metrics_util::LogUMAHistogramBoolean(
103 "PasswordManager.SavePasswordPromptDisplayed_" + uma_histogram_suffix_
,
106 message_link_range_
= gfx::Range();
107 GetSavePasswordDialogTitleTextAndLinkRange(
108 web_contents
->GetVisibleURL(), form_to_save_
->observed_form().origin
,
109 is_smartlock_branding_enabled
, false, &message_
, &message_link_range_
);
112 base::string16
SavePasswordInfoBarDelegate::GetFirstRunExperienceMessage() {
113 return should_show_first_run_experience_
114 ? l10n_util::GetStringUTF16(
115 IDS_PASSWORD_MANAGER_SAVE_PROMPT_FIRST_RUN_EXPERIENCE
)
119 infobars::InfoBarDelegate::Type
120 SavePasswordInfoBarDelegate::GetInfoBarType() const {
121 return PAGE_ACTION_TYPE
;
124 infobars::InfoBarDelegate::InfoBarAutomationType
125 SavePasswordInfoBarDelegate::GetInfoBarAutomationType() const {
126 return PASSWORD_INFOBAR
;
129 int SavePasswordInfoBarDelegate::GetIconId() const {
130 return IDR_INFOBAR_SAVE_PASSWORD
;
133 bool SavePasswordInfoBarDelegate::ShouldExpire(
134 const NavigationDetails
& details
) const {
135 return !details
.is_redirect
&& ConfirmInfoBarDelegate::ShouldExpire(details
);
138 void SavePasswordInfoBarDelegate::InfoBarDismissed() {
139 DCHECK(form_to_save_
.get());
140 infobar_response_
= password_manager::metrics_util::INFOBAR_DISMISSED
;
143 base::string16
SavePasswordInfoBarDelegate::GetMessageText() const {
147 base::string16
SavePasswordInfoBarDelegate::GetButtonLabel(
148 InfoBarButton button
) const {
149 return l10n_util::GetStringUTF16((button
== BUTTON_OK
)
150 ? IDS_PASSWORD_MANAGER_SAVE_BUTTON
151 : GetCancelButtonText(source_type_
));
154 bool SavePasswordInfoBarDelegate::Accept() {
155 DCHECK(form_to_save_
.get());
156 form_to_save_
->Save();
157 infobar_response_
= password_manager::metrics_util::REMEMBER_PASSWORD
;
161 bool SavePasswordInfoBarDelegate::Cancel() {
162 DCHECK(form_to_save_
.get());
164 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
) {
167 form_to_save_
->PermanentlyBlacklist();
168 infobar_response_
= password_manager::metrics_util::NEVER_REMEMBER_PASSWORD
;
173 bool SavePasswordInfoBarDelegate::LinkClicked(
174 WindowOpenDisposition disposition
) {
175 InfoBarService::WebContentsFromInfoBar(infobar())
176 ->OpenURL(content::OpenURLParams(
177 GURL(l10n_util::GetStringUTF16(
178 IDS_PASSWORD_MANAGER_SMART_LOCK_PAGE
)),
180 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
181 ui::PAGE_TRANSITION_LINK
, false));