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/generated_credit_card_bubble_controller.h" |
| 6 |
| 7 #include <climits> |
| 8 |
| 9 #include "base/logging.h" |
| 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/profiles/profile.h" |
| 14 #include "chrome/browser/ui/autofill/generated_credit_card_bubble.h" |
| 15 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" |
| 16 #include "chrome/browser/ui/browser_finder.h" |
| 17 #include "chrome/browser/ui/browser_navigator.h" |
| 18 #include "chrome/browser/ui/browser_window.h" |
| 19 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 21 #include "chrome/common/pref_names.h" |
| 22 #include "components/user_prefs/pref_registry_syncable.h" |
| 23 #include "content/public/browser/navigation_details.h" |
| 24 #include "content/public/browser/navigation_entry.h" |
| 25 #include "content/public/browser/web_contents.h" |
| 26 #include "grit/generated_resources.h" |
| 27 #include "grit/theme_resources.h" |
| 28 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/base/resource/resource_bundle.h" |
| 30 |
| 31 DEFINE_WEB_CONTENTS_USER_DATA_KEY( |
| 32 autofill::GeneratedCreditCardBubbleController); |
| 33 |
| 34 namespace autofill { |
| 35 |
| 36 namespace { |
| 37 |
| 38 static const int kMaxGeneratedCardTimesToShow = INT_MAX; |
| 39 static const char kWalletGeneratedCardLearnMoreLink[] = |
| 40 "http://support.google.com/wallet/bin/answer.py?hl=en&answer=2740044"; |
| 41 |
| 42 GeneratedCreditCardBubbleController* GetOrCreate(content::WebContents* wc) { |
| 43 GeneratedCreditCardBubbleController::CreateForWebContents(wc); |
| 44 return GeneratedCreditCardBubbleController::FromWebContents(wc); |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 49 bool TextRange::operator==(const TextRange& other) const { |
| 50 return other.range == range && other.is_link == is_link; |
| 51 } |
| 52 |
| 53 GeneratedCreditCardBubbleController::GeneratedCreditCardBubbleController( |
| 54 content::WebContents* web_contents) |
| 55 : WebContentsObserver(web_contents), |
| 56 web_contents_(web_contents), |
| 57 title_text_(l10n_util::GetStringUTF16( |
| 58 IDS_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_TITLE)), |
| 59 should_show_anchor_(true), |
| 60 weak_ptr_factory_(this) {} |
| 61 |
| 62 GeneratedCreditCardBubbleController::~GeneratedCreditCardBubbleController() { |
| 63 Hide(); |
| 64 } |
| 65 |
| 66 // static |
| 67 void GeneratedCreditCardBubbleController::RegisterUserPrefs( |
| 68 user_prefs::PrefRegistrySyncable* registry) { |
| 69 registry->RegisterIntegerPref( |
| 70 ::prefs::kAutofillGeneratedCardBubbleTimesShown, |
| 71 0, |
| 72 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 73 } |
| 74 |
| 75 // static |
| 76 void GeneratedCreditCardBubbleController::Show( |
| 77 content::WebContents* contents, |
| 78 const base::string16& fronting_card_name, |
| 79 const base::string16& backing_card_name) { |
| 80 GetOrCreate(contents)->SetupAndShow(fronting_card_name, backing_card_name); |
| 81 } |
| 82 |
| 83 void GeneratedCreditCardBubbleController::DidNavigateMainFrame( |
| 84 const content::LoadCommittedDetails& details, |
| 85 const content::FrameNavigateParams& params) { |
| 86 if (details.entry && |
| 87 !content::PageTransitionIsRedirect(details.entry->GetTransitionType())) { |
| 88 should_show_anchor_ = false; |
| 89 UpdateAnchor(); |
| 90 web_contents()->RemoveUserData(UserDataKey()); |
| 91 // |this| is now deleted. |
| 92 } |
| 93 } |
| 94 |
| 95 bool GeneratedCreditCardBubbleController::IsHiding() const { |
| 96 return bubble_ && bubble_->IsHiding(); |
| 97 } |
| 98 |
| 99 gfx::Image GeneratedCreditCardBubbleController::AnchorIcon() const { |
| 100 if (!should_show_anchor_) |
| 101 return gfx::Image(); |
| 102 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); |
| 103 } |
| 104 |
| 105 const base::string16& GeneratedCreditCardBubbleController::TitleText() const { |
| 106 return title_text_; |
| 107 } |
| 108 |
| 109 const base::string16& GeneratedCreditCardBubbleController::ContentsText() |
| 110 const { |
| 111 return contents_text_; |
| 112 } |
| 113 |
| 114 const std::vector<TextRange>& GeneratedCreditCardBubbleController:: |
| 115 ContentsTextRanges() const { |
| 116 return contents_text_ranges_; |
| 117 } |
| 118 |
| 119 void GeneratedCreditCardBubbleController::OnAnchorClicked() { |
| 120 Show(true); |
| 121 } |
| 122 |
| 123 void GeneratedCreditCardBubbleController::OnLinkClicked() { |
| 124 #if !defined(OS_ANDROID) |
| 125 chrome::NavigateParams params( |
| 126 chrome::FindBrowserWithWebContents(web_contents()), |
| 127 GURL(kWalletGeneratedCardLearnMoreLink), |
| 128 content::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 129 params.disposition = NEW_FOREGROUND_TAB; |
| 130 chrome::Navigate(¶ms); |
| 131 #else |
| 132 // TODO(dbeam): implement. |
| 133 #endif |
| 134 Hide(); |
| 135 } |
| 136 |
| 137 base::WeakPtr<GeneratedCreditCardBubbleController> |
| 138 GeneratedCreditCardBubbleController::GetWeakPtr() { |
| 139 return weak_ptr_factory_.GetWeakPtr(); |
| 140 } |
| 141 |
| 142 base::WeakPtr<GeneratedCreditCardBubble> GeneratedCreditCardBubbleController:: |
| 143 CreateBubble() { |
| 144 return GeneratedCreditCardBubble::Create(GetWeakPtr()); |
| 145 } |
| 146 |
| 147 base::WeakPtr<GeneratedCreditCardBubble> GeneratedCreditCardBubbleController:: |
| 148 bubble() { |
| 149 return bubble_; |
| 150 } |
| 151 |
| 152 bool GeneratedCreditCardBubbleController::CanShow() const { |
| 153 #if !defined(OS_ANDROID) |
| 154 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 155 return web_contents() == browser->tab_strip_model()->GetActiveWebContents(); |
| 156 #else |
| 157 return true; |
| 158 #endif |
| 159 } |
| 160 |
| 161 bool GeneratedCreditCardBubbleController::ShouldDisplayBubbleInitially() const { |
| 162 Profile* profile = Profile::FromBrowserContext( |
| 163 web_contents_->GetBrowserContext()); |
| 164 int times_shown = profile->GetPrefs()->GetInteger( |
| 165 ::prefs::kAutofillGeneratedCardBubbleTimesShown); |
| 166 return times_shown < kMaxGeneratedCardTimesToShow; |
| 167 } |
| 168 |
| 169 void GeneratedCreditCardBubbleController::SetupAndShow( |
| 170 const base::string16& fronting_card_name, |
| 171 const base::string16& backing_card_name) { |
| 172 DCHECK(!fronting_card_name.empty()); |
| 173 DCHECK(!backing_card_name.empty()); |
| 174 |
| 175 fronting_card_name_ = fronting_card_name; |
| 176 backing_card_name_ = backing_card_name; |
| 177 |
| 178 contents_text_.clear(); |
| 179 contents_text_ranges_.clear(); |
| 180 |
| 181 base::string16 to_split = l10n_util::GetStringFUTF16( |
| 182 IDS_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTENTS, |
| 183 fronting_card_name_, |
| 184 backing_card_name_); |
| 185 |
| 186 base::char16 separator('|'); |
| 187 std::vector<base::string16> pieces; |
| 188 base::SplitStringDontTrim(to_split, separator, &pieces); |
| 189 |
| 190 while (!pieces.empty()) { |
| 191 base::string16 piece = pieces.front(); |
| 192 if (!piece.empty() && pieces.size() % 2 == 0) { |
| 193 const size_t start = contents_text_.size(); |
| 194 TextRange bold_text; |
| 195 bold_text.range = ui::Range(start, start + piece.size()); |
| 196 bold_text.is_link = false; |
| 197 contents_text_ranges_.push_back(bold_text); |
| 198 } |
| 199 contents_text_.append(piece); |
| 200 pieces.erase(pieces.begin(), pieces.begin() + 1); |
| 201 } |
| 202 |
| 203 // Add a "Learn more" link at the end of the header text if it's a generated |
| 204 // card bubble. |
| 205 base::string16 learn_more = l10n_util::GetStringUTF16(IDS_LEARN_MORE); |
| 206 contents_text_.append(ASCIIToUTF16(" ") + learn_more); |
| 207 const size_t header_size = contents_text_.size(); |
| 208 TextRange end_link; |
| 209 end_link.range = ui::Range(header_size - learn_more.size(), header_size); |
| 210 end_link.is_link = true; |
| 211 contents_text_ranges_.push_back(end_link); |
| 212 |
| 213 UpdateAnchor(); |
| 214 |
| 215 if (ShouldDisplayBubbleInitially()) |
| 216 Show(false); |
| 217 } |
| 218 |
| 219 void GeneratedCreditCardBubbleController::Show(bool was_anchor_click) { |
| 220 if (!CanShow()) |
| 221 return; |
| 222 |
| 223 bubble_ = CreateBubble(); |
| 224 if (!bubble_) { |
| 225 // TODO(dbeam): Make a bubble on all applicable platforms. |
| 226 return; |
| 227 } |
| 228 |
| 229 bubble_->Show(); |
| 230 |
| 231 if (!was_anchor_click) { |
| 232 // If the bubble was an automatically created "you generated a card" bubble, |
| 233 // count it as a show. If the user clicked the omnibox icon, don't count it. |
| 234 PrefService* prefs = Profile::FromBrowserContext( |
| 235 web_contents()->GetBrowserContext())->GetPrefs(); |
| 236 prefs->SetInteger(::prefs::kAutofillGeneratedCardBubbleTimesShown, |
| 237 prefs->GetInteger(::prefs::kAutofillGeneratedCardBubbleTimesShown) + 1); |
| 238 } |
| 239 } |
| 240 |
| 241 void GeneratedCreditCardBubbleController::UpdateAnchor() { |
| 242 #if !defined(OS_ANDROID) |
| 243 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 244 if (browser && browser->window() && browser->window()->GetLocationBar()) |
| 245 browser->window()->GetLocationBar()->UpdateGeneratedCreditCardView(); |
| 246 #else |
| 247 // TODO(dbeam): implement. |
| 248 #endif |
| 249 } |
| 250 |
| 251 void GeneratedCreditCardBubbleController::Hide() { |
| 252 // Sever |bubble_|'s reference to the controller and hide (if it exists). |
| 253 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 254 |
| 255 if (bubble_ && !bubble_->IsHiding()) |
| 256 bubble_->Hide(); |
| 257 |
| 258 DCHECK(!bubble_ || bubble_->IsHiding()); |
| 259 } |
| 260 |
| 261 } // namespace autofill |
OLD | NEW |