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 InfoBarService::FromWebContents(web_contents
)
42 ->AddInfoBar(CreateSavePasswordInfoBar(
43 make_scoped_ptr(new SavePasswordInfoBarDelegate(
44 web_contents
, form_to_save
.Pass(), uma_histogram_suffix
,
46 password_bubble_experiment::IsSmartLockBrandingEnabled(
47 ProfileSyncServiceFactory::GetForProfile(profile
))))));
50 SavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
51 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
53 password_manager::metrics_util::NUM_RESPONSE_TYPES
);
55 password_manager::metrics_util::LogUIDismissalReason(infobar_response_
);
57 // The shortest period for which the prompt needs to live, so that we don't
58 // consider it killed prematurely, as might happen, e.g., if a pre-rendered
59 // page gets swapped in (and the current WebContents is destroyed).
60 const base::TimeDelta kMinimumPromptDisplayTime
=
61 base::TimeDelta::FromSeconds(1);
63 if (!uma_histogram_suffix_
.empty()) {
64 password_manager::metrics_util::LogUMAHistogramEnumeration(
65 "PasswordManager.SavePasswordPromptResponse_" + uma_histogram_suffix_
,
67 password_manager::metrics_util::NUM_RESPONSE_TYPES
);
68 password_manager::metrics_util::LogUMAHistogramBoolean(
69 "PasswordManager.SavePasswordPromptDisappearedQuickly_" +
70 uma_histogram_suffix_
,
71 timer_
.Elapsed() < kMinimumPromptDisplayTime
);
75 SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
76 content::WebContents
* web_contents
,
77 scoped_ptr
<password_manager::PasswordFormManager
> form_to_save
,
78 const std::string
& uma_histogram_suffix
,
79 password_manager::CredentialSourceType source_type
,
80 bool is_smartlock_branding_enabled
)
81 : ConfirmInfoBarDelegate(),
82 form_to_save_(form_to_save
.Pass()),
83 infobar_response_(password_manager::metrics_util::NO_RESPONSE
),
84 uma_histogram_suffix_(uma_histogram_suffix
),
85 source_type_(source_type
) {
86 if (!uma_histogram_suffix_
.empty()) {
87 password_manager::metrics_util::LogUMAHistogramBoolean(
88 "PasswordManager.SavePasswordPromptDisplayed_" + uma_histogram_suffix_
,
91 message_link_range_
= gfx::Range();
92 GetSavePasswordDialogTitleTextAndLinkRange(
93 web_contents
->GetVisibleURL(), form_to_save_
->observed_form().origin
,
94 is_smartlock_branding_enabled
, false, &message_
, &message_link_range_
);
97 infobars::InfoBarDelegate::Type
98 SavePasswordInfoBarDelegate::GetInfoBarType() const {
99 return PAGE_ACTION_TYPE
;
102 infobars::InfoBarDelegate::InfoBarAutomationType
103 SavePasswordInfoBarDelegate::GetInfoBarAutomationType() const {
104 return PASSWORD_INFOBAR
;
107 int SavePasswordInfoBarDelegate::GetIconId() const {
108 return IDR_INFOBAR_SAVE_PASSWORD
;
111 bool SavePasswordInfoBarDelegate::ShouldExpire(
112 const NavigationDetails
& details
) const {
113 return !details
.is_redirect
&& ConfirmInfoBarDelegate::ShouldExpire(details
);
116 void SavePasswordInfoBarDelegate::InfoBarDismissed() {
117 DCHECK(form_to_save_
.get());
118 infobar_response_
= password_manager::metrics_util::INFOBAR_DISMISSED
;
121 base::string16
SavePasswordInfoBarDelegate::GetMessageText() const {
125 base::string16
SavePasswordInfoBarDelegate::GetButtonLabel(
126 InfoBarButton button
) const {
127 return l10n_util::GetStringUTF16((button
== BUTTON_OK
)
128 ? IDS_PASSWORD_MANAGER_SAVE_BUTTON
129 : GetCancelButtonText(source_type_
));
132 bool SavePasswordInfoBarDelegate::Accept() {
133 DCHECK(form_to_save_
.get());
134 form_to_save_
->Save();
135 infobar_response_
= password_manager::metrics_util::REMEMBER_PASSWORD
;
139 bool SavePasswordInfoBarDelegate::Cancel() {
140 DCHECK(form_to_save_
.get());
142 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
) {
145 form_to_save_
->PermanentlyBlacklist();
146 infobar_response_
= password_manager::metrics_util::NEVER_REMEMBER_PASSWORD
;
151 bool SavePasswordInfoBarDelegate::LinkClicked(
152 WindowOpenDisposition disposition
) {
153 InfoBarService::WebContentsFromInfoBar(infobar())
154 ->OpenURL(content::OpenURLParams(
155 GURL(l10n_util::GetStringUTF16(
156 IDS_PASSWORD_MANAGER_SMART_LOCK_PAGE
)),
158 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
159 ui::PAGE_TRANSITION_LINK
, false));