Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / password_manager / save_password_infobar_delegate.cc
blob79eb00d5278edb5c38a6f4d5fc72980a50fcec01
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_service.h"
10 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
11 #include "chrome/grit/chromium_strings.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "components/infobars/core/infobar.h"
14 #include "components/password_manager/core/browser/password_form_manager.h"
15 #include "components/signin/core/common/profile_management_switches.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "google_apis/gaia/gaia_urls.h"
19 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
22 // static
23 void SavePasswordInfoBarDelegate::Create(
24 content::WebContents* web_contents,
25 scoped_ptr<password_manager::PasswordFormManager> form_to_save,
26 const std::string& uma_histogram_suffix) {
27 #if defined(ENABLE_ONE_CLICK_SIGNIN)
28 // Don't show the password manager infobar if this form is for a google
29 // account and we are going to show the one-click signin infobar.
30 GURL realm(form_to_save->realm());
31 // TODO(mathp): Checking only against associated_username() causes a bug
32 // referenced here: crbug.com/133275
33 // TODO(vabr): The check IsEnableWebBasedSignin is a hack for the time when
34 // OneClickSignin is disabled. http://crbug.com/339804
35 if (((realm == GaiaUrls::GetInstance()->gaia_login_form_realm()) ||
36 (realm == GURL("https://www.google.com/"))) &&
37 switches::IsEnableWebBasedSignin() &&
38 OneClickSigninHelper::CanOffer(
39 web_contents,
40 OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY,
41 base::UTF16ToUTF8(form_to_save->associated_username()),
42 NULL))
43 return;
44 #endif
46 InfoBarService::FromWebContents(web_contents)->AddInfoBar(
47 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
48 new SavePasswordInfoBarDelegate(form_to_save.Pass(),
49 uma_histogram_suffix))));
52 SavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
53 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
54 infobar_response_,
55 password_manager::metrics_util::NUM_RESPONSE_TYPES);
57 password_manager::metrics_util::LogUIDismissalReason(infobar_response_);
59 // The shortest period for which the prompt needs to live, so that we don't
60 // consider it killed prematurely, as might happen, e.g., if a pre-rendered
61 // page gets swapped in (and the current WebContents is destroyed).
62 const base::TimeDelta kMinimumPromptDisplayTime =
63 base::TimeDelta::FromSeconds(1);
65 if (!uma_histogram_suffix_.empty()) {
66 password_manager::metrics_util::LogUMAHistogramEnumeration(
67 "PasswordManager.SavePasswordPromptResponse_" + uma_histogram_suffix_,
68 infobar_response_,
69 password_manager::metrics_util::NUM_RESPONSE_TYPES);
70 password_manager::metrics_util::LogUMAHistogramBoolean(
71 "PasswordManager.SavePasswordPromptDisappearedQuickly_" +
72 uma_histogram_suffix_,
73 timer_.Elapsed() < kMinimumPromptDisplayTime);
77 SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
78 scoped_ptr<password_manager::PasswordFormManager> form_to_save,
79 const std::string& uma_histogram_suffix)
80 : ConfirmInfoBarDelegate(),
81 form_to_save_(form_to_save.Pass()),
82 infobar_response_(password_manager::metrics_util::NO_RESPONSE),
83 uma_histogram_suffix_(uma_histogram_suffix) {
84 if (!uma_histogram_suffix_.empty()) {
85 password_manager::metrics_util::LogUMAHistogramBoolean(
86 "PasswordManager.SavePasswordPromptDisplayed_" + uma_histogram_suffix_,
87 true);
91 bool SavePasswordInfoBarDelegate::ShouldExpire(
92 const NavigationDetails& details) const {
93 return !details.is_redirect &&
94 infobars::InfoBarDelegate::ShouldExpire(details);
97 int SavePasswordInfoBarDelegate::GetIconID() const {
98 return IDR_INFOBAR_SAVE_PASSWORD;
101 infobars::InfoBarDelegate::Type SavePasswordInfoBarDelegate::GetInfoBarType()
102 const {
103 return PAGE_ACTION_TYPE;
106 base::string16 SavePasswordInfoBarDelegate::GetMessageText() const {
107 return l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
110 base::string16 SavePasswordInfoBarDelegate::GetButtonLabel(
111 InfoBarButton button) const {
112 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
113 IDS_PASSWORD_MANAGER_SAVE_BUTTON : IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
116 bool SavePasswordInfoBarDelegate::Accept() {
117 DCHECK(form_to_save_.get());
118 form_to_save_->Save();
119 infobar_response_ = password_manager::metrics_util::REMEMBER_PASSWORD;
120 return true;
123 bool SavePasswordInfoBarDelegate::Cancel() {
124 DCHECK(form_to_save_.get());
125 form_to_save_->PermanentlyBlacklist();
126 infobar_response_ = password_manager::metrics_util::NEVER_REMEMBER_PASSWORD;
127 return true;
130 void SavePasswordInfoBarDelegate::InfoBarDismissed() {
131 DCHECK(form_to_save_.get());
132 infobar_response_ = password_manager::metrics_util::INFOBAR_DISMISSED;
135 infobars::InfoBarDelegate::InfoBarAutomationType
136 SavePasswordInfoBarDelegate::GetInfoBarAutomationType() const {
137 return PASSWORD_INFOBAR;