blob: 5d79a692d9864536069a6b85768c6c683466f95d [file] [log] [blame]
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01001// 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/autofill_credit_card_bubble_controller.h"
6
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +01007#include <climits>
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01008#include <string>
9
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010010#include "base/prefs/pref_service.h"
11#include "base/strings/string_split.h"
12#include "base/strings/utf_string_conversions.h"
13#include "chrome/browser/browser_process.h"
14#include "chrome/browser/profiles/profile.h"
15#include "chrome/browser/ui/autofill/autofill_credit_card_bubble.h"
16#include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
17#include "chrome/browser/ui/browser_finder.h"
18#include "chrome/browser/ui/browser_navigator.h"
19#include "chrome/browser/ui/browser_window.h"
20#include "chrome/browser/ui/omnibox/location_bar.h"
21#include "chrome/browser/ui/tabs/tab_strip_model.h"
22#include "chrome/common/pref_names.h"
23#include "components/user_prefs/pref_registry_syncable.h"
24#include "content/public/browser/navigation_details.h"
25#include "content/public/browser/navigation_entry.h"
26#include "content/public/browser/web_contents.h"
27#include "grit/generated_resources.h"
28#include "grit/theme_resources.h"
29#include "grit/webkit_resources.h"
30#include "ui/base/l10n/l10n_util.h"
31#include "ui/base/range/range.h"
32#include "ui/base/resource/resource_bundle.h"
33#include "ui/gfx/image/image.h"
34
35DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::AutofillCreditCardBubbleController);
36
37namespace autofill {
38
39namespace {
40
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010041// TODO(dbeam): add back a sensible limit once it's decided or remove
42// kMaxGeneratedCardTimesToShow if this behavior is finalized.
43static const int kMaxGeneratedCardTimesToShow = INT_MAX;
Ben Murdochbb1529c2013-08-08 10:24:53 +010044static const base::char16 kRangeSeparator = '|';
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010045static const char kWalletGeneratedCardLearnMoreLink[] =
46 "http://support.google.com/wallet/bin/answer.py?hl=en&answer=2740044";
47
48AutofillCreditCardBubbleController* GetOrCreate(content::WebContents* wc) {
49 AutofillCreditCardBubbleController::CreateForWebContents(wc);
50 return AutofillCreditCardBubbleController::FromWebContents(wc);
51}
52
53} // namespace
54
55AutofillCreditCardBubbleController::AutofillCreditCardBubbleController(
56 content::WebContents* web_contents)
57 : WebContentsObserver(web_contents),
58 web_contents_(web_contents),
59 should_show_anchor_(true),
60 weak_ptr_factory_(this) {}
61
62AutofillCreditCardBubbleController::~AutofillCreditCardBubbleController() {
Ben Murdochbb1529c2013-08-08 10:24:53 +010063 // In the case that the tab is closed, the controller can be deleted while the
64 // bubble is showing. Always calling |Hide()| ensures the bubble is closed.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010065 Hide();
66}
67
68// static
69void AutofillCreditCardBubbleController::RegisterUserPrefs(
70 user_prefs::PrefRegistrySyncable* registry) {
71 registry->RegisterIntegerPref(
72 ::prefs::kAutofillGeneratedCardBubbleTimesShown,
73 0,
74 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
75}
76
77// static
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010078void AutofillCreditCardBubbleController::ShowGeneratedCardUI(
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010079 content::WebContents* contents,
80 const base::string16& fronting_card_name,
81 const base::string16& backing_card_name) {
82 GetOrCreate(contents)->ShowAsGeneratedCardBubble(fronting_card_name,
83 backing_card_name);
84}
85
86// static
87void AutofillCreditCardBubbleController::ShowNewCardSavedBubble(
88 content::WebContents* contents,
89 const base::string16& new_card_name) {
90 GetOrCreate(contents)->ShowAsNewCardSavedBubble(new_card_name);
91}
92
93void AutofillCreditCardBubbleController::DidNavigateMainFrame(
94 const content::LoadCommittedDetails& details,
95 const content::FrameNavigateParams& params) {
96 if (details.entry &&
97 !content::PageTransitionIsRedirect(details.entry->GetTransitionType())) {
98 should_show_anchor_ = false;
99 UpdateAnchor();
100 web_contents()->RemoveUserData(UserDataKey());
101 // |this| is now deleted.
102 }
103}
104
105bool AutofillCreditCardBubbleController::IsHiding() const {
106 return bubble_ && bubble_->IsHiding();
107}
108
109gfx::Image AutofillCreditCardBubbleController::AnchorIcon() const {
110 if (!should_show_anchor_)
111 return gfx::Image();
112
113 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
114 IsGeneratedCardBubble() ? IDR_WALLET_ICON : IDR_AUTOFILL_CC_GENERIC);
115}
116
117base::string16 AutofillCreditCardBubbleController::BubbleTitle() const {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100118 return !IsGeneratedCardBubble() ?
119 ASCIIToUTF16("Lorem ipsum, savum cardum") :
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100120 l10n_util::GetStringUTF16(
121 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TITLE);
122}
123
124base::string16 AutofillCreditCardBubbleController::BubbleText() const {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100125 DCHECK(IsSetUp());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100126 return bubble_text_;
127}
128
129const std::vector<ui::Range>& AutofillCreditCardBubbleController::
130 BubbleTextRanges() const {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100131 DCHECK(IsSetUp());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100132 return bubble_text_ranges_;
133}
134
135base::string16 AutofillCreditCardBubbleController::LinkText() const {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100136 return l10n_util::GetStringUTF16(
137 IsGeneratedCardBubble() ? IDS_LEARN_MORE :
138 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_MANAGE_CARDS);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100139}
140
141void AutofillCreditCardBubbleController::OnAnchorClicked() {
142 Show(true);
143}
144
145void AutofillCreditCardBubbleController::OnLinkClicked() {
146 if (IsGeneratedCardBubble()) {
147#if !defined(OS_ANDROID)
Ben Murdochbb1529c2013-08-08 10:24:53 +0100148 // Open a new tab to the Online Wallet help link.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100149 chrome::NavigateParams params(
150 chrome::FindBrowserWithWebContents(web_contents()),
151 GURL(kWalletGeneratedCardLearnMoreLink),
152 content::PAGE_TRANSITION_AUTO_BOOKMARK);
153 params.disposition = NEW_FOREGROUND_TAB;
154 chrome::Navigate(&params);
155#else
156 // TODO(dbeam): implement.
157#endif
158 } else {
159 TabAutofillManagerDelegate::FromWebContents(web_contents())->
160 ShowAutofillSettings();
161 }
162 Hide();
163}
164
165base::WeakPtr<AutofillCreditCardBubbleController>
166 AutofillCreditCardBubbleController::GetWeakPtr() {
167 return weak_ptr_factory_.GetWeakPtr();
168}
169
170base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubbleController::
171 CreateBubble() {
172 return AutofillCreditCardBubble::Create(GetWeakPtr());
173}
174
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100175base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubbleController::
176 bubble() {
177 return bubble_;
178}
179
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100180bool AutofillCreditCardBubbleController::CanShow() const {
181#if !defined(OS_ANDROID)
182 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
183 return web_contents() == browser->tab_strip_model()->GetActiveWebContents();
184#else
185 return true;
186#endif
187}
188
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100189bool AutofillCreditCardBubbleController::ShouldDisplayBubbleInitially() const {
190 Profile* profile = Profile::FromBrowserContext(
191 web_contents_->GetBrowserContext());
192 int times_shown = profile->GetPrefs()->GetInteger(
193 ::prefs::kAutofillGeneratedCardBubbleTimesShown);
194 return times_shown < kMaxGeneratedCardTimesToShow;
195}
196
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100197void AutofillCreditCardBubbleController::ShowAsGeneratedCardBubble(
198 const base::string16& fronting_card_name,
199 const base::string16& backing_card_name) {
200 Reset();
201
202 DCHECK(!fronting_card_name.empty());
203 DCHECK(!backing_card_name.empty());
204 fronting_card_name_ = fronting_card_name;
205 backing_card_name_ = backing_card_name;
206
207 SetUp();
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100208
209 if (ShouldDisplayBubbleInitially())
210 Show(false);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100211}
212
213void AutofillCreditCardBubbleController::ShowAsNewCardSavedBubble(
214 const base::string16& new_card_name) {
215 Reset();
216
217 DCHECK(!new_card_name.empty());
218 new_card_name_ = new_card_name;
219
220 SetUp();
221 Show(false);
222}
223
224void AutofillCreditCardBubbleController::Reset() {
225 Hide();
226
Ben Murdochbb1529c2013-08-08 10:24:53 +0100227 // Clear any generated state or stored |ShowAs*()| arguments.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100228 fronting_card_name_.clear();
229 backing_card_name_.clear();
230 new_card_name_.clear();
231 bubble_text_.clear();
232 bubble_text_ranges_.clear();
233
Ben Murdochbb1529c2013-08-08 10:24:53 +0100234 DCHECK(!IsSetUp());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100235}
236
237void AutofillCreditCardBubbleController::SetUp() {
238 base::string16 full_text;
239 if (IsGeneratedCardBubble()) {
240 full_text = l10n_util::GetStringFUTF16(
241 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TEXT,
242 fronting_card_name_,
243 backing_card_name_);
244 } else {
245 full_text = ReplaceStringPlaceholders(
246 ASCIIToUTF16("Lorem ipsum, savum cardem |$1|. Replacem before launch."),
247 new_card_name_,
248 NULL);
249 }
250
Ben Murdochbb1529c2013-08-08 10:24:53 +0100251 // Split the full text on '|' to highlight certain parts. For example, "sly"
252 // and "jumped" would be bolded in "The |sly| fox |jumped| over the lazy dog".
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100253 std::vector<base::string16> pieces;
Ben Murdochbb1529c2013-08-08 10:24:53 +0100254 base::SplitStringDontTrim(full_text, kRangeSeparator, &pieces);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100255
256 while (!pieces.empty()) {
257 base::string16 piece = pieces.front();
Ben Murdochbb1529c2013-08-08 10:24:53 +0100258
259 // Every second piece should be bolded. Because |base::SplitString*()|
260 // leaves an empty "" even if '|' is the first character, this is guaranteed
261 // to work for "|highlighting| starts here". Ignore empty pieces because
262 // there's nothing to highlight.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100263 if (!piece.empty() && pieces.size() % 2 == 0) {
264 const size_t start = bubble_text_.size();
265 bubble_text_ranges_.push_back(ui::Range(start, start + piece.size()));
266 }
Ben Murdochbb1529c2013-08-08 10:24:53 +0100267
268 // Append the piece whether it's bolded or not and move on to the next one.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100269 bubble_text_.append(piece);
270 pieces.erase(pieces.begin(), pieces.begin() + 1);
271 }
272
273 UpdateAnchor();
Ben Murdochbb1529c2013-08-08 10:24:53 +0100274 DCHECK(IsSetUp());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100275}
276
Ben Murdochbb1529c2013-08-08 10:24:53 +0100277bool AutofillCreditCardBubbleController::IsSetUp() const {
278 // Because |bubble_text_| should never be empty after |SetUp()|, and all
279 // translations should have some text highlighting (i.e. "some |pipes|"),
280 // if there is text there should be text ranges as well. Sanity check this.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100281 DCHECK_EQ(bubble_text_.empty(), bubble_text_ranges_.empty());
282 return !bubble_text_.empty();
283}
284
285bool AutofillCreditCardBubbleController::IsGeneratedCardBubble() const {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100286 // Do a quick sanity check to ensure that the bubble isn't partially set up as
287 // the other type (i.e. a fronting card and a new card doesn't make sense).
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100288 DCHECK_EQ(fronting_card_name_.empty(), backing_card_name_.empty());
289 DCHECK_NE(backing_card_name_.empty(), new_card_name_.empty());
290 return !fronting_card_name_.empty();
291}
292
293void AutofillCreditCardBubbleController::Show(bool was_anchor_click) {
294 if (!CanShow())
295 return;
296
297 bubble_ = CreateBubble();
Ben Murdoch558790d2013-07-30 15:19:42 +0100298 if (!bubble_) {
299 // TODO(dbeam): Make a bubble on all applicable platforms.
300 return;
301 }
302
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100303 bubble_->Show();
304
305 if (IsGeneratedCardBubble() && !was_anchor_click) {
306 // If the bubble was an automatically created "you generated a card" bubble,
307 // count it as a show. If the user clicked the omnibox icon, don't count it.
308 PrefService* prefs = Profile::FromBrowserContext(
309 web_contents()->GetBrowserContext())->GetPrefs();
310 prefs->SetInteger(::prefs::kAutofillGeneratedCardBubbleTimesShown,
311 prefs->GetInteger(::prefs::kAutofillGeneratedCardBubbleTimesShown) + 1);
312 }
313}
314
315void AutofillCreditCardBubbleController::UpdateAnchor() {
316#if !defined(OS_ANDROID)
317 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
318 if (browser && browser->window() && browser->window()->GetLocationBar())
319 browser->window()->GetLocationBar()->UpdateAutofillCreditCardView();
320#else
321 // TODO(dbeam): implement.
322#endif
323}
324
325void AutofillCreditCardBubbleController::Hide() {
326 // Sever |bubble_|'s reference to the controller and hide (if it exists).
327 weak_ptr_factory_.InvalidateWeakPtrs();
328
329 if (bubble_ && !bubble_->IsHiding())
330 bubble_->Hide();
331
332 DCHECK(!bubble_ || bubble_->IsHiding());
333}
334
335} // namespace autofill