1 // Copyright (c) 2012 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/autofill/autofill_cc_infobar_delegate.h"
7 #include "base/logging.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "components/autofill/browser/credit_card.h"
10 #include "components/autofill/browser/personal_data_manager.h"
11 #include "components/autofill/common/autofill_constants.h"
12 #include "content/public/browser/page_navigator.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_delegate.h"
15 #include "grit/generated_resources.h"
16 #include "grit/theme_resources.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/base/resource/resource_bundle.h"
24 void AutofillCCInfoBarDelegate::Create(
25 InfoBarService
* infobar_service
,
26 const AutofillMetrics
* metric_logger
,
27 const base::Closure
& save_card_callback
) {
28 infobar_service
->AddInfoBar(scoped_ptr
<InfoBarDelegate
>(
29 new AutofillCCInfoBarDelegate(
30 infobar_service
, metric_logger
, save_card_callback
)));
31 metric_logger
->LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN
);
35 scoped_ptr
<ConfirmInfoBarDelegate
> AutofillCCInfoBarDelegate::CreateForTesting(
36 const AutofillMetrics
* metric_logger
,
37 const base::Closure
& save_card_callback
) {
38 metric_logger
->LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN
);
39 return scoped_ptr
<ConfirmInfoBarDelegate
>(
40 new AutofillCCInfoBarDelegate(NULL
, metric_logger
, save_card_callback
));
43 AutofillCCInfoBarDelegate::AutofillCCInfoBarDelegate(
44 InfoBarService
* infobar_service
,
45 const AutofillMetrics
* metric_logger
,
46 const base::Closure
& save_card_callback
)
47 : ConfirmInfoBarDelegate(infobar_service
),
48 metric_logger_(metric_logger
),
49 save_card_callback_(save_card_callback
),
50 had_user_interaction_(false) {}
52 AutofillCCInfoBarDelegate::~AutofillCCInfoBarDelegate() {
53 if (!had_user_interaction_
)
54 LogUserAction(AutofillMetrics::INFOBAR_IGNORED
);
57 void AutofillCCInfoBarDelegate::LogUserAction(
58 AutofillMetrics::InfoBarMetric user_action
) {
59 DCHECK(!had_user_interaction_
);
61 metric_logger_
->LogCreditCardInfoBarMetric(user_action
);
62 had_user_interaction_
= true;
65 void AutofillCCInfoBarDelegate::InfoBarDismissed() {
66 LogUserAction(AutofillMetrics::INFOBAR_DENIED
);
69 gfx::Image
* AutofillCCInfoBarDelegate::GetIcon() const {
70 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
71 IDR_INFOBAR_AUTOFILL
);
74 InfoBarDelegate::Type
AutofillCCInfoBarDelegate::GetInfoBarType() const {
75 return PAGE_ACTION_TYPE
;
78 bool AutofillCCInfoBarDelegate::ShouldExpireInternal(
79 const content::LoadCommittedDetails
& details
) const {
80 // The user has submitted a form, causing the page to navigate elsewhere. We
81 // don't want the infobar to be expired at this point, because the user won't
82 // get a chance to answer the question.
86 string16
AutofillCCInfoBarDelegate::GetMessageText() const {
87 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_INFOBAR_TEXT
);
90 string16
AutofillCCInfoBarDelegate::GetButtonLabel(InfoBarButton button
) const {
91 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
92 IDS_AUTOFILL_CC_INFOBAR_ACCEPT
: IDS_AUTOFILL_CC_INFOBAR_DENY
);
95 bool AutofillCCInfoBarDelegate::Accept() {
96 save_card_callback_
.Run();
97 save_card_callback_
.Reset();
98 LogUserAction(AutofillMetrics::INFOBAR_ACCEPTED
);
102 bool AutofillCCInfoBarDelegate::Cancel() {
103 LogUserAction(AutofillMetrics::INFOBAR_DENIED
);
107 string16
AutofillCCInfoBarDelegate::GetLinkText() const {
108 return l10n_util::GetStringUTF16(IDS_LEARN_MORE
);
111 bool AutofillCCInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition
) {
112 web_contents()->OpenURL(content::OpenURLParams(
113 GURL(autofill::kHelpURL
), content::Referrer(),
114 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
115 content::PAGE_TRANSITION_LINK
, false));
119 } // namespace autofill