Chromium Code Reviews| Index: chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.cc |
| diff --git a/chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.cc b/chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.cc |
| index 1bbbffc14a0e5d02d390ca4b9cc5204f7b105d7b..491be9eb455a8fce1713e59bda8588042431ae0c 100644 |
| --- a/chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.cc |
| +++ b/chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/bind.h" |
| #include "base/location.h" |
| +#include "base/logging.h" |
| #include "base/message_loop/message_loop.h" |
| #include "base/prefs/pref_service.h" |
| #include "base/strings/string_split.h" |
| @@ -22,17 +23,17 @@ |
| #include "chrome/browser/ui/omnibox/location_bar.h" |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/common/pref_names.h" |
| +#include "components/autofill/core/browser/autofill_profile.h" |
| +#include "components/autofill/core/browser/credit_card.h" |
| #include "components/user_prefs/pref_registry_syncable.h" |
| #include "content/public/browser/navigation_details.h" |
| #include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "grit/chromium_strings.h" |
| #include "grit/generated_resources.h" |
| #include "grit/theme_resources.h" |
| -#include "grit/webkit_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| -#include "ui/base/range/range.h" |
| #include "ui/base/resource/resource_bundle.h" |
| -#include "ui/gfx/image/image.h" |
| DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::AutofillCreditCardBubbleController); |
| @@ -51,6 +52,9 @@ AutofillCreditCardBubbleController* GetOrCreate(content::WebContents* wc) { |
| } // namespace |
| +CreditCardDescription::CreditCardDescription() {} |
| +CreditCardDescription::~CreditCardDescription() {} |
| + |
| AutofillCreditCardBubbleController::AutofillCreditCardBubbleController( |
| content::WebContents* web_contents) |
| : WebContentsObserver(web_contents), |
| @@ -72,7 +76,7 @@ void AutofillCreditCardBubbleController::RegisterUserPrefs( |
| } |
| // static |
| -void AutofillCreditCardBubbleController::ShowGeneratedCardUI( |
| +void AutofillCreditCardBubbleController::ShowGeneratedCardBubble( |
| content::WebContents* contents, |
| const base::string16& fronting_card_name, |
| const base::string16& backing_card_name) { |
| @@ -83,8 +87,10 @@ void AutofillCreditCardBubbleController::ShowGeneratedCardUI( |
| // static |
| void AutofillCreditCardBubbleController::ShowNewCardSavedBubble( |
| content::WebContents* contents, |
| - const base::string16& new_card_name) { |
| - GetOrCreate(contents)->ShowAsNewCardSavedBubble(new_card_name); |
| + scoped_ptr<CreditCard> new_card, |
| + scoped_ptr<AutofillProfile> billing_profile) { |
| + GetOrCreate(contents)->ShowAsNewCardSavedBubble(new_card.Pass(), |
| + billing_profile.Pass()); |
| } |
| void AutofillCreditCardBubbleController::DidNavigateMainFrame( |
| @@ -103,40 +109,48 @@ bool AutofillCreditCardBubbleController::IsHiding() const { |
| return bubble_ && bubble_->IsHiding(); |
| } |
| +bool AutofillCreditCardBubbleController::AnchorToSettingsMenu() const { |
| + return !IsGeneratedCardBubble(); |
| +} |
| + |
| gfx::Image AutofillCreditCardBubbleController::AnchorIcon() const { |
| - if (!should_show_anchor_) |
| + if (!should_show_anchor_ || !IsGeneratedCardBubble()) |
| return gfx::Image(); |
| + return ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); |
| +} |
| - return ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| - IsGeneratedCardBubble() ? IDR_WALLET_ICON : IDR_AUTOFILL_CC_GENERIC); |
| +const base::string16& AutofillCreditCardBubbleController::TitleText() const { |
| + return title_text_; |
| } |
| -base::string16 AutofillCreditCardBubbleController::BubbleTitle() const { |
| - return !IsGeneratedCardBubble() ? ASCIIToUTF16("Lorem ipsum, savum cardum") : |
| - l10n_util::GetStringUTF16( |
| - IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TITLE); |
| +const base::string16& AutofillCreditCardBubbleController::HeaderText() const { |
| + return header_text_; |
| } |
| -base::string16 AutofillCreditCardBubbleController::BubbleText() const { |
| - DCHECK(IsSetup()); |
| - return bubble_text_; |
| +const std::vector<TextRange>& AutofillCreditCardBubbleController:: |
| + HeaderTextRanges() const { |
| + return header_text_ranges_; |
| } |
| -const std::vector<ui::Range>& AutofillCreditCardBubbleController:: |
| - BubbleTextRanges() const { |
| - DCHECK(IsSetup()); |
| - return bubble_text_ranges_; |
| +const CreditCardDescription* AutofillCreditCardBubbleController::Description() |
| + const { |
| + return card_desc_.get(); |
| } |
| -base::string16 AutofillCreditCardBubbleController::LinkText() const { |
| - return l10n_util::GetStringUTF16(IsGeneratedCardBubble() ? |
| - IDS_LEARN_MORE : IDS_AUTOFILL_CREDIT_CARD_BUBBLE_MANAGE_CARDS); |
| +const base::string16& AutofillCreditCardBubbleController::LinkText() const { |
| + return link_text_; |
| } |
| void AutofillCreditCardBubbleController::OnAnchorClicked() { |
| Show(true); |
| } |
| +void AutofillCreditCardBubbleController::OnBubbleDestroyed() { |
| + if (AnchorToSettingsMenu()) |
| + web_contents()->RemoveUserData(UserDataKey()); |
| + // |this| may now be deleted. |
| +} |
| + |
| void AutofillCreditCardBubbleController::OnLinkClicked() { |
| if (IsGeneratedCardBubble()) { |
| #if !defined(OS_ANDROID) |
| @@ -205,11 +219,14 @@ void AutofillCreditCardBubbleController::ShowAsGeneratedCardBubble( |
| } |
| void AutofillCreditCardBubbleController::ShowAsNewCardSavedBubble( |
| - const base::string16& new_card_name) { |
| + scoped_ptr<CreditCard> new_card, |
| + scoped_ptr<AutofillProfile> billing_profile) { |
| Reset(); |
| - DCHECK(!new_card_name.empty()); |
| - new_card_name_ = new_card_name; |
| + DCHECK(new_card); |
| + DCHECK(billing_profile); |
| + new_card_ = new_card.Pass(); |
| + billing_profile_ = billing_profile.Pass(); |
| SetUp(); |
| Show(false); |
| @@ -220,53 +237,101 @@ void AutofillCreditCardBubbleController::Reset() { |
| fronting_card_name_.clear(); |
| backing_card_name_.clear(); |
| - new_card_name_.clear(); |
| - bubble_text_.clear(); |
| - bubble_text_ranges_.clear(); |
| + new_card_.reset(); |
| + billing_profile_.reset(); |
| + |
| + title_text_.clear(); |
| + header_text_.clear(); |
| + header_text_ranges_.clear(); |
| + card_desc_.reset(); |
| + link_text_.clear(); |
| DCHECK(!IsSetup()); |
| } |
| void AutofillCreditCardBubbleController::SetUp() { |
| - base::string16 full_text; |
| + // Set the title if it's a generated card bubble. |
| if (IsGeneratedCardBubble()) { |
| - full_text = l10n_util::GetStringFUTF16( |
| + title_text_ = l10n_util::GetStringUTF16( |
| + IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TITLE); |
| + } |
| + |
| + base::string16 to_split; |
| + if (IsGeneratedCardBubble()) { |
| + to_split = l10n_util::GetStringFUTF16( |
| IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TEXT, |
| fronting_card_name_, |
| backing_card_name_); |
| } else { |
| - full_text = ReplaceStringPlaceholders( |
| - ASCIIToUTF16("Lorem ipsum, savum cardem |$1|. Replacem before launch."), |
| - new_card_name_, |
| - NULL); |
| + to_split = l10n_util::GetStringUTF16( |
| + IDS_AUTOFILL_CREDIT_CARD_BUBBLE_NEW_CARD_TEXT); |
| } |
| base::char16 separator('|'); |
| std::vector<base::string16> pieces; |
| - base::SplitStringDontTrim(full_text, separator, &pieces); |
| + base::SplitStringDontTrim(to_split, separator, &pieces); |
| while (!pieces.empty()) { |
| base::string16 piece = pieces.front(); |
| if (!piece.empty() && pieces.size() % 2 == 0) { |
| - const size_t start = bubble_text_.size(); |
| - bubble_text_ranges_.push_back(ui::Range(start, start + piece.size())); |
| + const size_t start = header_text_.size(); |
| + TextRange bold_text; |
| + bold_text.range = ui::Range(start, start + piece.size()); |
| + bold_text.is_link = false; |
| + header_text_ranges_.push_back(bold_text); |
| } |
| - bubble_text_.append(piece); |
| + header_text_.append(piece); |
| pieces.erase(pieces.begin(), pieces.begin() + 1); |
| } |
| + // Add a "Learn more" link at the end of the header text if it's a generated |
| + // card bubble. |
| + if (IsGeneratedCardBubble()) { |
| + base::string16 learn_more = l10n_util::GetStringUTF16(IDS_LEARN_MORE); |
| + header_text_.append(ASCIIToUTF16(" ") + learn_more); |
| + const size_t header_size = header_text_.size(); |
| + TextRange end_link; |
| + end_link.range = ui::Range(header_size - learn_more.size(), header_size); |
| + end_link.is_link = true; |
| + header_text_ranges_.push_back(end_link); |
| + } |
| + |
| + // Create a description if it's a newly saved card bubble. |
| + if (!IsGeneratedCardBubble()) { |
|
Evan Stade
2013/08/02 16:29:45
use else?
Dan Beam
2013/08/06 02:41:35
doesn't exist any more
|
| + card_desc_.reset(new CreditCardDescription); |
| + |
| + const base::string16 card_number = |
| + new_card_->GetRawInfo(CREDIT_CARD_NUMBER); |
| + ui::ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| + card_desc_->icon = rb.GetImageNamed( |
| + CreditCard::IconResourceId(CreditCard::GetCreditCardType(card_number))); |
| + |
| + card_desc_->title = card_number.substr(0, 4) + ASCIIToUTF16(" - ") + |
|
Evan Stade
2013/08/02 16:29:45
I think this belongs alongside the other "ForDispl
Dan Beam
2013/08/06 02:41:35
changed to TypeAndLastFourDigits() and started thr
|
| + card_number.substr(4, 4) + ASCIIToUTF16(" - ") + |
| + card_number.substr(8, 4) + ASCIIToUTF16(" - ") + |
| + card_number.substr(12); |
| + const base::string16 kNewLine = ASCIIToUTF16("\n"); |
| + const std::string& locale = g_browser_process->GetApplicationLocale(); |
| + card_desc_->description = billing_profile_->Label() + kNewLine + |
| + billing_profile_->GetInfo(EMAIL_ADDRESS, locale) + kNewLine + |
| + billing_profile_->GetRawInfo(PHONE_BILLING_WHOLE_NUMBER); |
| + } |
| + |
| + // Add a link by setting the link text if it's a newly saved card bubble. |
| + if (!IsGeneratedCardBubble()) { |
|
Evan Stade
2013/08/02 16:29:45
nit: put this in the above block that's guarded by
Dan Beam
2013/08/06 02:41:35
doesn't exist any more
|
| + link_text_ = l10n_util::GetStringUTF16( |
| + IDS_AUTOFILL_CREDIT_CARD_BUBBLE_MANAGE_CARDS); |
| + } |
| + |
| UpdateAnchor(); |
| DCHECK(IsSetup()); |
| } |
| bool AutofillCreditCardBubbleController::IsSetup() const { |
| - DCHECK_EQ(bubble_text_.empty(), bubble_text_ranges_.empty()); |
| - return !bubble_text_.empty(); |
| + return !header_text_.empty(); |
| } |
| bool AutofillCreditCardBubbleController::IsGeneratedCardBubble() const { |
| - DCHECK_EQ(fronting_card_name_.empty(), backing_card_name_.empty()); |
| - DCHECK_NE(backing_card_name_.empty(), new_card_name_.empty()); |
| return !fronting_card_name_.empty(); |
| } |