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