Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / password_manager / save_password_infobar_delegate.cc
blob19d40ec6e5bc2cb2a654cf297902396ae3eaa53c
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/grit/chromium_strings.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/infobars/core/infobar.h"
12 #include "components/password_manager/core/browser/password_manager_client.h"
13 #include "content/public/browser/web_contents.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
17 #if defined(OS_ANDROID)
18 #include "chrome/browser/ui/android/infobars/save_password_infobar.h"
19 #endif
21 namespace {
23 int GetCancelButtonText(password_manager::CredentialSourceType source_type) {
24 return source_type ==
25 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API
26 ? IDS_PASSWORD_MANAGER_SAVE_PASSWORD_SMART_LOCK_NO_THANKS_BUTTON
27 : IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON;
30 } // namespace
32 // static
33 void SavePasswordInfoBarDelegate::Create(
34 content::WebContents* web_contents,
35 scoped_ptr<password_manager::PasswordFormManager> form_to_save,
36 const std::string& uma_histogram_suffix,
37 password_manager::CredentialSourceType source_type) {
38 InfoBarService* infobar_service =
39 InfoBarService::FromWebContents(web_contents);
40 SavePasswordInfoBarDelegate* infobar_delegate =
41 new SavePasswordInfoBarDelegate(
42 form_to_save.Pass(), uma_histogram_suffix, source_type);
43 #if defined(OS_ANDROID)
44 // For Android in case of smart lock we need different appearance of infobar.
45 scoped_ptr<infobars::InfoBar> infobar =
46 make_scoped_ptr(new SavePasswordInfoBar(
47 scoped_ptr<SavePasswordInfoBarDelegate>(infobar_delegate)));
48 #else
49 // For desktop we'll keep using the ConfirmInfobar.
50 scoped_ptr<infobars::InfoBar> infobar(infobar_service->CreateConfirmInfoBar(
51 scoped_ptr<ConfirmInfoBarDelegate>(infobar_delegate)));
52 #endif
53 infobar_service->AddInfoBar(infobar.Pass());
56 SavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
57 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
58 infobar_response_,
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_,
72 infobar_response_,
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);
81 SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
82 scoped_ptr<password_manager::PasswordFormManager> form_to_save,
83 const std::string& uma_histogram_suffix,
84 password_manager::CredentialSourceType source_type)
85 : ConfirmInfoBarDelegate(),
86 form_to_save_(form_to_save.Pass()),
87 infobar_response_(password_manager::metrics_util::NO_RESPONSE),
88 uma_histogram_suffix_(uma_histogram_suffix),
89 source_type_(source_type) {
90 if (!uma_histogram_suffix_.empty()) {
91 password_manager::metrics_util::LogUMAHistogramBoolean(
92 "PasswordManager.SavePasswordPromptDisplayed_" + uma_histogram_suffix_,
93 true);
97 bool SavePasswordInfoBarDelegate::ShouldShowMoreButton() {
98 return source_type_ ==
99 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API;
102 infobars::InfoBarDelegate::Type
103 SavePasswordInfoBarDelegate::GetInfoBarType() const {
104 return PAGE_ACTION_TYPE;
107 infobars::InfoBarDelegate::InfoBarAutomationType
108 SavePasswordInfoBarDelegate::GetInfoBarAutomationType() const {
109 return PASSWORD_INFOBAR;
112 int SavePasswordInfoBarDelegate::GetIconID() const {
113 return IDR_INFOBAR_SAVE_PASSWORD;
116 bool SavePasswordInfoBarDelegate::ShouldExpire(
117 const NavigationDetails& details) const {
118 return !details.is_redirect &&
119 infobars::InfoBarDelegate::ShouldExpire(details);
122 void SavePasswordInfoBarDelegate::InfoBarDismissed() {
123 DCHECK(form_to_save_.get());
124 infobar_response_ = password_manager::metrics_util::INFOBAR_DISMISSED;
127 base::string16 SavePasswordInfoBarDelegate::GetMessageText() const {
128 return l10n_util::GetStringUTF16(
129 (source_type_ ==
130 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API)
131 ? IDS_PASSWORD_MANAGER_SAVE_PASSWORD_SMART_LOCK_PROMPT
132 : IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
135 base::string16 SavePasswordInfoBarDelegate::GetButtonLabel(
136 InfoBarButton button) const {
137 return l10n_util::GetStringUTF16((button == BUTTON_OK)
138 ? IDS_PASSWORD_MANAGER_SAVE_BUTTON
139 : GetCancelButtonText(source_type_));
142 bool SavePasswordInfoBarDelegate::Accept() {
143 DCHECK(form_to_save_.get());
144 form_to_save_->Save();
145 infobar_response_ = password_manager::metrics_util::REMEMBER_PASSWORD;
146 return true;
149 bool SavePasswordInfoBarDelegate::Cancel() {
150 DCHECK(form_to_save_.get());
151 if (source_type_ ==
152 password_manager::CredentialSourceType::CREDENTIAL_SOURCE_API) {
153 InfoBarDismissed();
154 } else {
155 form_to_save_->PermanentlyBlacklist();
156 infobar_response_ = password_manager::metrics_util::NEVER_REMEMBER_PASSWORD;
158 return true;