| OLD | NEW |
| (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/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 | |
| 35 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::AutofillCreditCardBubbleController); | |
| 36 | |
| 37 namespace autofill { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // TODO(dbeam): add back a sensible limit once it's decided or remove | |
| 42 // kMaxGeneratedCardTimesToShow if this behavior is finalized. | |
| 43 static const int kMaxGeneratedCardTimesToShow = INT_MAX; | |
| 44 static const base::char16 kRangeSeparator = '|'; | |
| 45 static const char kWalletGeneratedCardLearnMoreLink[] = | |
| 46 "http://support.google.com/wallet/bin/answer.py?hl=en&answer=2740044"; | |
| 47 | |
| 48 AutofillCreditCardBubbleController* GetOrCreate(content::WebContents* wc) { | |
| 49 AutofillCreditCardBubbleController::CreateForWebContents(wc); | |
| 50 return AutofillCreditCardBubbleController::FromWebContents(wc); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 AutofillCreditCardBubbleController::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 | |
| 62 AutofillCreditCardBubbleController::~AutofillCreditCardBubbleController() { | |
| 63 // 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. | |
| 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() ? | |
| 119 ASCIIToUTF16("Lorem ipsum, savum cardum") : | |
| 120 l10n_util::GetStringUTF16( | |
| 121 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_GENERATED_TITLE); | |
| 122 } | |
| 123 | |
| 124 base::string16 AutofillCreditCardBubbleController::BubbleText() const { | |
| 125 DCHECK(IsSetUp()); | |
| 126 return bubble_text_; | |
| 127 } | |
| 128 | |
| 129 const std::vector<ui::Range>& AutofillCreditCardBubbleController:: | |
| 130 BubbleTextRanges() const { | |
| 131 DCHECK(IsSetUp()); | |
| 132 return bubble_text_ranges_; | |
| 133 } | |
| 134 | |
| 135 base::string16 AutofillCreditCardBubbleController::LinkText() const { | |
| 136 return l10n_util::GetStringUTF16( | |
| 137 IsGeneratedCardBubble() ? IDS_LEARN_MORE : | |
| 138 IDS_AUTOFILL_CREDIT_CARD_BUBBLE_MANAGE_CARDS); | |
| 139 } | |
| 140 | |
| 141 void AutofillCreditCardBubbleController::OnAnchorClicked() { | |
| 142 Show(true); | |
| 143 } | |
| 144 | |
| 145 void AutofillCreditCardBubbleController::OnLinkClicked() { | |
| 146 if (IsGeneratedCardBubble()) { | |
| 147 #if !defined(OS_ANDROID) | |
| 148 // Open a new tab to the Online Wallet help link. | |
| 149 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(¶ms); | |
| 155 #else | |
| 156 // TODO(dbeam): implement. | |
| 157 #endif | |
| 158 } else { | |
| 159 TabAutofillManagerDelegate::FromWebContents(web_contents())-> | |
| 160 ShowAutofillSettings(); | |
| 161 } | |
| 162 Hide(); | |
| 163 } | |
| 164 | |
| 165 base::WeakPtr<AutofillCreditCardBubbleController> | |
| 166 AutofillCreditCardBubbleController::GetWeakPtr() { | |
| 167 return weak_ptr_factory_.GetWeakPtr(); | |
| 168 } | |
| 169 | |
| 170 base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubbleController:: | |
| 171 CreateBubble() { | |
| 172 return AutofillCreditCardBubble::Create(GetWeakPtr()); | |
| 173 } | |
| 174 | |
| 175 base::WeakPtr<AutofillCreditCardBubble> AutofillCreditCardBubbleController:: | |
| 176 bubble() { | |
| 177 return bubble_; | |
| 178 } | |
| 179 | |
| 180 bool 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 | |
| 189 bool 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 | |
| 197 void 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(); | |
| 208 | |
| 209 if (ShouldDisplayBubbleInitially()) | |
| 210 Show(false); | |
| 211 } | |
| 212 | |
| 213 void 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 | |
| 224 void AutofillCreditCardBubbleController::Reset() { | |
| 225 Hide(); | |
| 226 | |
| 227 // Clear any generated state or stored |ShowAs*()| arguments. | |
| 228 fronting_card_name_.clear(); | |
| 229 backing_card_name_.clear(); | |
| 230 new_card_name_.clear(); | |
| 231 bubble_text_.clear(); | |
| 232 bubble_text_ranges_.clear(); | |
| 233 | |
| 234 DCHECK(!IsSetUp()); | |
| 235 } | |
| 236 | |
| 237 void 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 | |
| 251 // 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". | |
| 253 std::vector<base::string16> pieces; | |
| 254 base::SplitStringDontTrim(full_text, kRangeSeparator, &pieces); | |
| 255 | |
| 256 while (!pieces.empty()) { | |
| 257 base::string16 piece = pieces.front(); | |
| 258 | |
| 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. | |
| 263 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 } | |
| 267 | |
| 268 // Append the piece whether it's bolded or not and move on to the next one. | |
| 269 bubble_text_.append(piece); | |
| 270 pieces.erase(pieces.begin(), pieces.begin() + 1); | |
| 271 } | |
| 272 | |
| 273 UpdateAnchor(); | |
| 274 DCHECK(IsSetUp()); | |
| 275 } | |
| 276 | |
| 277 bool 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. | |
| 281 DCHECK_EQ(bubble_text_.empty(), bubble_text_ranges_.empty()); | |
| 282 return !bubble_text_.empty(); | |
| 283 } | |
| 284 | |
| 285 bool AutofillCreditCardBubbleController::IsGeneratedCardBubble() const { | |
| 286 // 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). | |
| 288 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 | |
| 293 void AutofillCreditCardBubbleController::Show(bool was_anchor_click) { | |
| 294 if (!CanShow()) | |
| 295 return; | |
| 296 | |
| 297 bubble_ = CreateBubble(); | |
| 298 if (!bubble_) { | |
| 299 // TODO(dbeam): Make a bubble on all applicable platforms. | |
| 300 return; | |
| 301 } | |
| 302 | |
| 303 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 | |
| 315 void 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 | |
| 325 void 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 | |
| OLD | NEW |