1fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton// Copyright 2013 The Chromium Authors. All rights reserved.
280468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton// Use of this source code is governed by a BSD-style license that can be
3fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton// found in the LICENSE file.
4fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton
580468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton#include "chrome/browser/ui/autofill/new_credit_card_bubble_controller.h"
6fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton
7fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include <string>
8fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton
9fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "base/logging.h"
10fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "base/strings/utf_string_conversions.h"
11fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/browser/browser_process.h"
12fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/browser/profiles/profile.h"
1380468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton#include "chrome/browser/ui/autofill/data_model_wrapper.h"
14fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/browser/ui/autofill/new_credit_card_bubble_view.h"
15fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/browser/ui/browser_finder.h"
16fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/browser/ui/chrome_pages.h"
1780468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton#include "chrome/browser/ui/host_desktop.h"
18fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/common/url_constants.h"
19fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/grit/chromium_strings.h"
20fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "chrome/grit/generated_resources.h"
21fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "components/autofill/core/browser/autofill_profile.h"
22fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "components/autofill/core/browser/credit_card.h"
23fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "content/public/browser/web_contents.h"
24fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton#include "ui/base/l10n/l10n_util.h"
2580468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton#include "ui/base/resource/resource_bundle.h"
26fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Manton
27fcb595c04f9ee275eae49b7bb7c61246671f5ce2Younes Mantonnamespace autofill {
28e44c85637a3298918e292e9ddba812856cf92924Younes Manton
29e44c85637a3298918e292e9ddba812856cf92924Younes MantonCreditCardDescription::CreditCardDescription() {}
30e44c85637a3298918e292e9ddba812856cf92924Younes MantonCreditCardDescription::~CreditCardDescription() {}
31e44c85637a3298918e292e9ddba812856cf92924Younes Manton
32e44c85637a3298918e292e9ddba812856cf92924Younes MantonNewCreditCardBubbleController::~NewCreditCardBubbleController() {
33b48676672592271597d07e5ece79cf4d3ffbe04bChristian König  Hide();
347ea550621e25f9b2f344b2ed60551ceec91b3fcfKai Wasserbäch}
35efaf024f8c7c1000af06e54a85378818d55c5160Christian König
367ea550621e25f9b2f344b2ed60551ceec91b3fcfKai Wasserbäch// static
377ea550621e25f9b2f344b2ed60551ceec91b3fcfKai Wasserbächvoid NewCreditCardBubbleController::Show(
38e44c85637a3298918e292e9ddba812856cf92924Younes Manton    content::WebContents* web_contents,
397ea550621e25f9b2f344b2ed60551ceec91b3fcfKai Wasserbäch    scoped_ptr<CreditCard> new_card,
407ea550621e25f9b2f344b2ed60551ceec91b3fcfKai Wasserbäch    scoped_ptr<AutofillProfile> billing_profile) {
41c7b65dcaffeb9d0760c8ecad052f4c79297bfc8aChristian König  (new NewCreditCardBubbleController(web_contents))->SetupAndShow(
42e44c85637a3298918e292e9ddba812856cf92924Younes Manton      new_card.Pass(),
43e44c85637a3298918e292e9ddba812856cf92924Younes Manton      billing_profile.Pass());
44e44c85637a3298918e292e9ddba812856cf92924Younes Manton}
45b48676672592271597d07e5ece79cf4d3ffbe04bChristian König
46b48676672592271597d07e5ece79cf4d3ffbe04bChristian Königconst base::string16& NewCreditCardBubbleController::TitleText() const {
47b48676672592271597d07e5ece79cf4d3ffbe04bChristian König  return title_text_;
484a0b80f00dbc77d333027afd195daae7ef1e651cChristian König}
49e44c85637a3298918e292e9ddba812856cf92924Younes Manton
50e44c85637a3298918e292e9ddba812856cf92924Younes Mantonconst CreditCardDescription& NewCreditCardBubbleController::CardDescription()
51e44c85637a3298918e292e9ddba812856cf92924Younes Manton    const {
52e44c85637a3298918e292e9ddba812856cf92924Younes Manton  return card_desc_;
531448e829e86981e6144410ba6a3d0f16357fb2b3Christian König}
541448e829e86981e6144410ba6a3d0f16357fb2b3Christian König
55d9ad3aa3b9647f1ede2568600978af956ff32fffChristian Königconst base::string16& NewCreditCardBubbleController::LinkText() const {
56e5f78a74f8294ee02015552db664dae1e7da9f47Christian König  return link_text_;
57c7b65dcaffeb9d0760c8ecad052f4c79297bfc8aChristian König}
58c7b65dcaffeb9d0760c8ecad052f4c79297bfc8aChristian König
59bd5fd67a3e3cda4b7676dd4745fc5d5524709210Christian Königvoid NewCreditCardBubbleController::OnBubbleDestroyed() {
6032c4381d4a0479b3d9bfe305ce701be6b5ac8e18Christian König  delete this;
61c7b65dcaffeb9d0760c8ecad052f4c79297bfc8aChristian König}
625eb822cb6a8fb461ee5b1bd881e0ef1b91c432b5Younes Manton
635eb822cb6a8fb461ee5b1bd881e0ef1b91c432b5Younes Mantonvoid NewCreditCardBubbleController::OnLinkClicked() {
64f3f212acf0d2fc25d3b6bd70dd1f346d97a9b25dChristian König  chrome::ShowSettingsSubPageForProfile(profile_, chrome::kAutofillSubPage);
65e44c85637a3298918e292e9ddba812856cf92924Younes Manton  Hide();
66e44c85637a3298918e292e9ddba812856cf92924Younes Manton}
67e44c85637a3298918e292e9ddba812856cf92924Younes Manton
68e44c85637a3298918e292e9ddba812856cf92924Younes MantonNewCreditCardBubbleController::NewCreditCardBubbleController(
69d9ad3aa3b9647f1ede2568600978af956ff32fffChristian König    content::WebContents* web_contents)
70d9ad3aa3b9647f1ede2568600978af956ff32fffChristian König    : profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())),
71a09754c15fc48e5fed347f478a301a02ee69fe8cMaarten Lankhorst      web_contents_(web_contents),
72a09754c15fc48e5fed347f478a301a02ee69fe8cMaarten Lankhorst      title_text_(l10n_util::GetStringUTF16(
73884cb79edfefb1133229a002f41b4d370d717a7eChristian König          IDS_AUTOFILL_NEW_CREDIT_CARD_BUBBLE_TITLE)),
74d3770d6229d95e9beb67358ae2b2c8824ed3ae58Christian König      link_text_(l10n_util::GetStringUTF16(
75884cb79edfefb1133229a002f41b4d370d717a7eChristian König          IDS_AUTOFILL_NEW_CREDIT_CARD_BUBBLE_LINK)),
76104ac0066394f8246d18c833bca4bcce271b5eefChristian König      weak_ptr_factory_(this) {}
7780468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton
7880468464897682b8e10aeab310f20fdd7ddc6cb4Younes Mantonbase::WeakPtr<NewCreditCardBubbleView> NewCreditCardBubbleController::
7980468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton    CreateBubble() {
8080468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton  return NewCreditCardBubbleView::Create(this);
8180468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton}
8280468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton
8380468464897682b8e10aeab310f20fdd7ddc6cb4Younes Mantonbase::WeakPtr<NewCreditCardBubbleView> NewCreditCardBubbleController::
84e44c85637a3298918e292e9ddba812856cf92924Younes Manton    bubble() {
85e44c85637a3298918e292e9ddba812856cf92924Younes Manton  return bubble_;
865f730690f8166c94ee010605b6437a6fb2a7771dYounes Manton}
875f730690f8166c94ee010605b6437a6fb2a7771dYounes Manton
884a0b80f00dbc77d333027afd195daae7ef1e651cChristian Königvoid NewCreditCardBubbleController::SetupAndShow(
8980468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton    scoped_ptr<CreditCard> new_card,
90849a0b0a821ecc59a50fe53498a6354cfd0b24a4Christian König    scoped_ptr<AutofillProfile> billing_profile) {
91849a0b0a821ecc59a50fe53498a6354cfd0b24a4Christian König  DCHECK(new_card);
92849a0b0a821ecc59a50fe53498a6354cfd0b24a4Christian König  DCHECK(billing_profile);
93d645dc65b6c5e7d46538e98208a703f0f7a5d20bChristian König
94d645dc65b6c5e7d46538e98208a703f0f7a5d20bChristian König  new_card_ = new_card.Pass();
95efaf024f8c7c1000af06e54a85378818d55c5160Christian König  billing_profile_ = billing_profile.Pass();
9680468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton
9780468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton  const base::string16 card_number =
9880468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton      new_card_->GetRawInfo(CREDIT_CARD_NUMBER);
9980468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton  ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance();
10080468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton  card_desc_.icon = rb.GetImageNamed(
10180468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton      CreditCard::IconResourceId(CreditCard::GetCreditCardType(card_number)));
10280468464897682b8e10aeab310f20fdd7ddc6cb4Younes Manton  card_desc_.name = new_card_->TypeAndLastFourDigits();
1035f730690f8166c94ee010605b6437a6fb2a7771dYounes Manton
1048580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton  AutofillProfileWrapper wrapper(billing_profile_.get());
1058580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton  base::string16 unused;
1068580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton  wrapper.GetDisplayText(&card_desc_.description, &unused);
1078580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton
108b48676672592271597d07e5ece79cf4d3ffbe04bChristian König  bubble_ = CreateBubble();
1098580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton  if (!bubble_) {
1108580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton    // TODO(dbeam): Make a bubble on all applicable platforms.
111b48676672592271597d07e5ece79cf4d3ffbe04bChristian König    delete this;
1128580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton    return;
113b48676672592271597d07e5ece79cf4d3ffbe04bChristian König  }
114c7b65dcaffeb9d0760c8ecad052f4c79297bfc8aChristian König
1158580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton  bubble_->Show();
1168580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton}
1178580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton
1188580b7a0eeed3fc29320b2c0a184084e4267661aYounes Mantonvoid NewCreditCardBubbleController::Hide() {
1198580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton  if (bubble_)
1208580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton    bubble_->Hide();
1218580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton}
1228580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton
1238580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton}  // namespace autofill
1248580b7a0eeed3fc29320b2c0a184084e4267661aYounes Manton