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.
4
5#include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
6
7#include "base/logging.h"
8#include "chrome/browser/infobars/infobar_service.h"
9#include "components/autofill/core/browser/credit_card.h"
10#include "components/autofill/core/browser/personal_data_manager.h"
11#include "components/autofill/core/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
19
20namespace autofill {
21
22// static
23void AutofillCCInfoBarDelegate::Create(
24    InfoBarService* infobar_service,
25    const AutofillMetrics* metric_logger,
26    const base::Closure& save_card_callback) {
27  infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>(
28      new AutofillCCInfoBarDelegate(
29          infobar_service, metric_logger, save_card_callback)));
30}
31
32AutofillCCInfoBarDelegate::AutofillCCInfoBarDelegate(
33    InfoBarService* infobar_service,
34    const AutofillMetrics* metric_logger,
35    const base::Closure& save_card_callback)
36    : ConfirmInfoBarDelegate(infobar_service),
37      metric_logger_(metric_logger),
38      save_card_callback_(save_card_callback),
39      had_user_interaction_(false) {
40  metric_logger->LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN);
41}
42
43AutofillCCInfoBarDelegate::~AutofillCCInfoBarDelegate() {
44  if (!had_user_interaction_)
45    LogUserAction(AutofillMetrics::INFOBAR_IGNORED);
46}
47
48void AutofillCCInfoBarDelegate::LogUserAction(
49    AutofillMetrics::InfoBarMetric user_action) {
50  DCHECK(!had_user_interaction_);
51
52  metric_logger_->LogCreditCardInfoBarMetric(user_action);
53  had_user_interaction_ = true;
54}
55
56void AutofillCCInfoBarDelegate::InfoBarDismissed() {
57  LogUserAction(AutofillMetrics::INFOBAR_DENIED);
58}
59
60int AutofillCCInfoBarDelegate::GetIconID() const {
61  return IDR_INFOBAR_AUTOFILL;
62}
63
64InfoBarDelegate::Type AutofillCCInfoBarDelegate::GetInfoBarType() const {
65  return PAGE_ACTION_TYPE;
66}
67
68bool AutofillCCInfoBarDelegate::ShouldExpireInternal(
69    const content::LoadCommittedDetails& details) const {
70  // The user has submitted a form, causing the page to navigate elsewhere. We
71  // don't want the infobar to be expired at this point, because the user won't
72  // get a chance to answer the question.
73  return false;
74}
75
76string16 AutofillCCInfoBarDelegate::GetMessageText() const {
77  return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_INFOBAR_TEXT);
78}
79
80string16 AutofillCCInfoBarDelegate::GetButtonLabel(InfoBarButton button) const {
81  return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
82      IDS_AUTOFILL_CC_INFOBAR_ACCEPT : IDS_AUTOFILL_CC_INFOBAR_DENY);
83}
84
85bool AutofillCCInfoBarDelegate::Accept() {
86  save_card_callback_.Run();
87  save_card_callback_.Reset();
88  LogUserAction(AutofillMetrics::INFOBAR_ACCEPTED);
89  return true;
90}
91
92bool AutofillCCInfoBarDelegate::Cancel() {
93  LogUserAction(AutofillMetrics::INFOBAR_DENIED);
94  return true;
95}
96
97string16 AutofillCCInfoBarDelegate::GetLinkText() const {
98  return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
99}
100
101bool AutofillCCInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
102  web_contents()->OpenURL(content::OpenURLParams(
103      GURL(autofill::kHelpURL), content::Referrer(),
104      (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
105      content::PAGE_TRANSITION_LINK, false));
106  return false;
107}
108
109}  // namespace autofill
110