new_credit_card_bubble_controller.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2013 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/ui/autofill/new_credit_card_bubble_controller.h"
6
7#include <string>
8
9#include "base/logging.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/browser/ui/autofill/data_model_wrapper.h"
14#include "chrome/browser/ui/autofill/new_credit_card_bubble_view.h"
15#include "chrome/browser/ui/browser_finder.h"
16#include "chrome/browser/ui/chrome_pages.h"
17#include "chrome/browser/ui/host_desktop.h"
18#include "chrome/common/url_constants.h"
19#include "chrome/grit/chromium_strings.h"
20#include "chrome/grit/generated_resources.h"
21#include "components/autofill/core/browser/autofill_profile.h"
22#include "components/autofill/core/browser/credit_card.h"
23#include "content/public/browser/web_contents.h"
24#include "ui/base/l10n/l10n_util.h"
25#include "ui/base/resource/resource_bundle.h"
26
27namespace autofill {
28
29CreditCardDescription::CreditCardDescription() {}
30CreditCardDescription::~CreditCardDescription() {}
31
32NewCreditCardBubbleController::~NewCreditCardBubbleController() {
33  Hide();
34}
35
36// static
37void NewCreditCardBubbleController::Show(
38    content::WebContents* web_contents,
39    scoped_ptr<CreditCard> new_card,
40    scoped_ptr<AutofillProfile> billing_profile) {
41  (new NewCreditCardBubbleController(web_contents))->SetupAndShow(
42      new_card.Pass(),
43      billing_profile.Pass());
44}
45
46const base::string16& NewCreditCardBubbleController::TitleText() const {
47  return title_text_;
48}
49
50const CreditCardDescription& NewCreditCardBubbleController::CardDescription()
51    const {
52  return card_desc_;
53}
54
55const base::string16& NewCreditCardBubbleController::LinkText() const {
56  return link_text_;
57}
58
59void NewCreditCardBubbleController::OnBubbleDestroyed() {
60  delete this;
61}
62
63void NewCreditCardBubbleController::OnLinkClicked() {
64  chrome::ShowSettingsSubPageForProfile(profile_, chrome::kAutofillSubPage);
65  Hide();
66}
67
68NewCreditCardBubbleController::NewCreditCardBubbleController(
69    content::WebContents* web_contents)
70    : profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())),
71      web_contents_(web_contents),
72      title_text_(l10n_util::GetStringUTF16(
73          IDS_AUTOFILL_NEW_CREDIT_CARD_BUBBLE_TITLE)),
74      link_text_(l10n_util::GetStringUTF16(
75          IDS_AUTOFILL_NEW_CREDIT_CARD_BUBBLE_LINK)),
76      weak_ptr_factory_(this) {}
77
78base::WeakPtr<NewCreditCardBubbleView> NewCreditCardBubbleController::
79    CreateBubble() {
80  return NewCreditCardBubbleView::Create(this);
81}
82
83base::WeakPtr<NewCreditCardBubbleView> NewCreditCardBubbleController::
84    bubble() {
85  return bubble_;
86}
87
88void NewCreditCardBubbleController::SetupAndShow(
89    scoped_ptr<CreditCard> new_card,
90    scoped_ptr<AutofillProfile> billing_profile) {
91  DCHECK(new_card);
92  DCHECK(billing_profile);
93
94  new_card_ = new_card.Pass();
95  billing_profile_ = billing_profile.Pass();
96
97  const base::string16 card_number =
98      new_card_->GetRawInfo(CREDIT_CARD_NUMBER);
99  ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
100  card_desc_.icon = rb.GetImageNamed(
101      CreditCard::IconResourceId(CreditCard::GetCreditCardType(card_number)));
102  card_desc_.name = new_card_->TypeAndLastFourDigits();
103
104  AutofillProfileWrapper wrapper(billing_profile_.get());
105  base::string16 unused;
106  wrapper.GetDisplayText(&card_desc_.description, &unused);
107
108  bubble_ = CreateBubble();
109  if (!bubble_) {
110    // TODO(dbeam): Make a bubble on all applicable platforms.
111    delete this;
112    return;
113  }
114
115  bubble_->Show();
116}
117
118void NewCreditCardBubbleController::Hide() {
119  if (bubble_)
120    bubble_->Hide();
121}
122
123}  // namespace autofill
124