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