Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/browser/ui/autofill/autofill_credit_card_bubble_controller.cc

Issue 21668003: Implement newly saved card bubble for realz and update generated card bubble to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/autofill_credit_card_bubble_controller.h"
6
7 #include <climits>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/autofill/autofill_credit_card_bubble.h"
19 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/browser_navigator.h"
22 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/omnibox/location_bar.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/pref_names.h"
26 #include "components/user_prefs/pref_registry_syncable.h"
27 #include "content/public/browser/navigation_details.h"
28 #include "content/public/browser/navigation_entry.h"
29 #include "content/public/browser/web_contents.h"
30 #include "grit/generated_resources.h"
31 #include "grit/theme_resources.h"
32 #include "grit/webkit_resources.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/base/range/range.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/gfx/image/image.h"
37
38 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::AutofillCreditCardBubbleController);
39
40 namespace autofill {
41
42 namespace {
43
44 // TODO(dbeam): add back a sensible limit once it's decided or remove
45 // kMaxGeneratedCardTimesToShow if this behavior is finalized.
46 static const int kMaxGeneratedCardTimesToShow = INT_MAX;
47 static const char kWalletGeneratedCardLearnMoreLink[] =
48 "http://support.google.com/wallet/bin/answer.py?hl=en&answer=2740044";
49
50 AutofillCreditCardBubbleController* GetOrCreate(content::WebContents* wc) {
51 AutofillCreditCardBubbleController::CreateForWebContents(wc);
52 return AutofillCreditCardBubbleController::FromWebContents(wc);
53 }
54
55 } // namespace
56
57 AutofillCreditCardBubbleController::AutofillCreditCardBubbleController(
58 content::WebContents* web_contents)
59 : WebContentsObserver(web_contents),
60 web_contents_(web_contents),
61 should_show_anchor_(true),
62 weak_ptr_factory_(this) {}
63
64 AutofillCreditCardBubbleController::~AutofillCreditCardBubbleController() {
65 Hide();
66 }
67
68 // static
69 void 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
78 void AutofillCreditCardBubbleController::ShowGeneratedCardUI(
79 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
87 void AutofillCreditCardBubbleController::ShowNewCardSavedBubble(
88 content::WebContents* contents,
89 const base::string16& new_card_name) {
90 GetOrCreate(contents)->ShowAsNewCardSavedBubble(new_card_name);
91 }
92
93 void 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
105 bool AutofillCreditCardBubbleController::IsHiding() const {
106 return bubble_ && bubble_->IsHiding();
107 }
108
109 gfx::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
117 base::string16 AutofillCreditCardBubbleController::BubbleTitle() const {
118 return !IsGeneratedCardBubble() ? ASCIIToUTF16("Lorem ipsum, savum cardum") :
119 l10n_util::GetStringUTF16(
120 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TITLE);
121 }
122
123 base::string16 AutofillCreditCardBubbleController::BubbleText() const {
124 DCHECK(IsSetup());
125 return bubble_text_;
126 }
127
128 const std::vector<ui::Range>& AutofillCreditCardBubbleController::
129 BubbleTextRanges() const {
130 DCHECK(IsSetup());
131 return bubble_text_ranges_;
132 }
133
134 base::string16 AutofillCreditCardBubbleController::LinkText() const {
135 return l10n_util::GetStringUTF16(IsGeneratedCardBubble() ?
136 IDS_LEARN_MORE : IDS_AUTOFILL_CREDIT_CARD_BUBBLE_MANAGE_CARDS);
137 }
138
139 void AutofillCreditCardBubbleController::OnAnchorClicked() {
140 Show(true);
141 }
142
143 void AutofillCreditCardBubbleController::OnLinkClicked() {
144 if (IsGeneratedCardBubble()) {
145 #if !defined(OS_ANDROID)
146 chrome::NavigateParams params(
147 chrome::FindBrowserWithWebContents(web_contents()),
148 GURL(kWalletGeneratedCardLearnMoreLink),
149 content::PAGE_TRANSITION_AUTO_BOOKMARK);
150 params.disposition = NEW_FOREGROUND_TAB;
151 chrome::Navigate(&params);
152 #else
153 // TODO(dbeam): implement.
154 #endif
155 } else {
156 TabAutofillManagerDelegate::FromWebContents(web_contents())->
157 ShowAutofillSettings();
158 }
159 Hide();
160 }
161
162 base::WeakPtr<AutofillCreditCardBubbleController>
163 AutofillCreditCardBubbleController::GetWeakPtr() {
164 return weak_ptr_factory_.GetWeakPtr();
165 }
166
167 base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubbleController::
168 CreateBubble() {
169 return AutofillCreditCardBubble::Create(GetWeakPtr());
170 }
171
172 base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubbleController::
173 bubble() {
174 return bubble_;
175 }
176
177 bool AutofillCreditCardBubbleController::CanShow() const {
178 #if !defined(OS_ANDROID)
179 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
180 return web_contents() == browser->tab_strip_model()->GetActiveWebContents();
181 #else
182 return true;
183 #endif
184 }
185
186 bool AutofillCreditCardBubbleController::ShouldDisplayBubbleInitially() const {
187 Profile* profile = Profile::FromBrowserContext(
188 web_contents_->GetBrowserContext());
189 int times_shown = profile->GetPrefs()->GetInteger(
190 ::prefs::kAutofillGeneratedCardBubbleTimesShown);
191 return times_shown < kMaxGeneratedCardTimesToShow;
192 }
193
194 void AutofillCreditCardBubbleController::ShowAsGeneratedCardBubble(
195 const base::string16& fronting_card_name,
196 const base::string16& backing_card_name) {
197 Reset();
198
199 DCHECK(!fronting_card_name.empty());
200 DCHECK(!backing_card_name.empty());
201 fronting_card_name_ = fronting_card_name;
202 backing_card_name_ = backing_card_name;
203
204 SetUp();
205
206 if (ShouldDisplayBubbleInitially())
207 Show(false);
208 }
209
210 void AutofillCreditCardBubbleController::ShowAsNewCardSavedBubble(
211 const base::string16& new_card_name) {
212 Reset();
213
214 DCHECK(!new_card_name.empty());
215 new_card_name_ = new_card_name;
216
217 SetUp();
218 Show(false);
219 }
220
221 void AutofillCreditCardBubbleController::Reset() {
222 Hide();
223
224 fronting_card_name_.clear();
225 backing_card_name_.clear();
226 new_card_name_.clear();
227 bubble_text_.clear();
228 bubble_text_ranges_.clear();
229
230 DCHECK(!IsSetup());
231 }
232
233 void AutofillCreditCardBubbleController::SetUp() {
234 base::string16 full_text;
235 if (IsGeneratedCardBubble()) {
236 full_text = l10n_util::GetStringFUTF16(
237 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TEXT,
238 fronting_card_name_,
239 backing_card_name_);
240 } else {
241 full_text = ReplaceStringPlaceholders(
242 ASCIIToUTF16("Lorem ipsum, savum cardem |$1|. Replacem before launch."),
243 new_card_name_,
244 NULL);
245 }
246
247 base::char16 separator('|');
248 std::vector<base::string16> pieces;
249 base::SplitStringDontTrim(full_text, separator, &pieces);
250
251 while (!pieces.empty()) {
252 base::string16 piece = pieces.front();
253 if (!piece.empty() && pieces.size() % 2 == 0) {
254 const size_t start = bubble_text_.size();
255 bubble_text_ranges_.push_back(ui::Range(start, start + piece.size()));
256 }
257 bubble_text_.append(piece);
258 pieces.erase(pieces.begin(), pieces.begin() + 1);
259 }
260
261 UpdateAnchor();
262 DCHECK(IsSetup());
263 }
264
265 bool AutofillCreditCardBubbleController::IsSetup() const {
266 DCHECK_EQ(bubble_text_.empty(), bubble_text_ranges_.empty());
267 return !bubble_text_.empty();
268 }
269
270 bool AutofillCreditCardBubbleController::IsGeneratedCardBubble() const {
271 DCHECK_EQ(fronting_card_name_.empty(), backing_card_name_.empty());
272 DCHECK_NE(backing_card_name_.empty(), new_card_name_.empty());
273 return !fronting_card_name_.empty();
274 }
275
276 void AutofillCreditCardBubbleController::Show(bool was_anchor_click) {
277 if (!CanShow())
278 return;
279
280 bubble_ = CreateBubble();
281 if (!bubble_) {
282 // TODO(dbeam): Make a bubble on all applicable platforms.
283 return;
284 }
285
286 bubble_->Show();
287
288 if (IsGeneratedCardBubble() && !was_anchor_click) {
289 // If the bubble was an automatically created "you generated a card" bubble,
290 // count it as a show. If the user clicked the omnibox icon, don't count it.
291 PrefService* prefs = Profile::FromBrowserContext(
292 web_contents()->GetBrowserContext())->GetPrefs();
293 prefs->SetInteger(::prefs::kAutofillGeneratedCardBubbleTimesShown,
294 prefs->GetInteger(::prefs::kAutofillGeneratedCardBubbleTimesShown) + 1);
295 }
296 }
297
298 void AutofillCreditCardBubbleController::UpdateAnchor() {
299 #if !defined(OS_ANDROID)
300 Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
301 if (browser && browser->window() && browser->window()->GetLocationBar())
302 browser->window()->GetLocationBar()->UpdateAutofillCreditCardView();
303 #else
304 // TODO(dbeam): implement.
305 #endif
306 }
307
308 void AutofillCreditCardBubbleController::Hide() {
309 // Sever |bubble_|'s reference to the controller and hide (if it exists).
310 weak_ptr_factory_.InvalidateWeakPtrs();
311
312 if (bubble_ && !bubble_->IsHiding())
313 bubble_->Hide();
314
315 DCHECK(!bubble_ || bubble_->IsHiding());
316 }
317
318 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698