OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h" | 5 #include "chrome/browser/ui/autofill/save_card_bubble_controller_impl.h" |
6 | 6 |
| 7 #include "base/i18n/message_formatter.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
7 #include "chrome/browser/ui/autofill/save_card_bubble_view.h" | 10 #include "chrome/browser/ui/autofill/save_card_bubble_view.h" |
8 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
10 #include "chrome/browser/ui/browser_window.h" | 13 #include "chrome/browser/ui/browser_window.h" |
11 #include "chrome/browser/ui/location_bar/location_bar.h" | 14 #include "chrome/browser/ui/location_bar/location_bar.h" |
12 #include "components/autofill/core/common/autofill_constants.h" | 15 #include "components/autofill/core/common/autofill_constants.h" |
13 #include "content/public/browser/navigation_details.h" | 16 #include "content/public/browser/navigation_details.h" |
14 | 17 |
15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::SaveCardBubbleControllerImpl); | 18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::SaveCardBubbleControllerImpl); |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 // Number of seconds the bubble and icon will survive navigations, starting | 22 // Number of seconds the bubble and icon will survive navigations, starting |
20 // from when the bubble is shown. | 23 // from when the bubble is shown. |
21 // TODO(bondd): Share with ManagePasswordsUIController. | 24 // TODO(bondd): Share with ManagePasswordsUIController. |
22 const int kSurviveNavigationSeconds = 5; | 25 const int kSurviveNavigationSeconds = 5; |
23 | 26 |
| 27 // Replace "{0}", "{1}", ... in |template_icu| with corresponding strings |
| 28 // from |display_texts|. Sets |out_message| to the resulting string, with |
| 29 // start position of each replacement in |out_offsets|. |
| 30 // Return false on failure. If false is returned then contents of |out_message| |
| 31 // and |out_offsets| are undefined. |
| 32 bool ReplaceTemplatePlaceholders( |
| 33 const base::string16& template_icu, |
| 34 const std::vector<base::string16>& display_texts, |
| 35 base::string16& out_message, |
| 36 std::vector<size_t>& out_offsets) { |
| 37 // Escape "$" -> "$$" for ReplaceStringPlaceholders(). |
| 38 // |
| 39 // Edge cases: |
| 40 // 1. Two or more consecutive $ characters will be incorrectly expanded |
| 41 // ("$$" -> "$$$$", which ReplaceStringPlaceholders() then turns into |
| 42 // "$$$"). |
| 43 // |
| 44 // 2. "${" will cause false to be returned. "${0}" will expand to "$${0}". |
| 45 // FormatWithNumberedArgs() turns it into "$$$1", which |
| 46 // ReplaceStringPlaceholders() then turns into "$$1" without doing the |
| 47 // parameter replacement. This causes false to be returned because each |
| 48 // parameter is not used exactly once. |
| 49 // |
| 50 // Both of these cases are noted in the header file, and are unlikely to |
| 51 // occur in any actual legal message. |
| 52 base::string16 template_icu_escaped; |
| 53 base::ReplaceChars(template_icu, base::ASCIIToUTF16("$"), |
| 54 base::ASCIIToUTF16("$$"), &template_icu_escaped); |
| 55 |
| 56 // Replace "{0}" -> "$1", "{1}" -> "$2", ... to prepare |template_dollars| |
| 57 // for ReplaceStringPlaceholders(). |
| 58 base::string16 template_dollars = |
| 59 base::i18n::MessageFormatter::FormatWithNumberedArgs( |
| 60 template_icu_escaped, "$1", "$2", "$3", "$4", "$5", "$6", "$7"); |
| 61 |
| 62 // FormatWithNumberedArgs() returns an empty string on failure. |
| 63 if (template_dollars.empty() && !template_icu.empty()) |
| 64 return false; |
| 65 |
| 66 // Replace "$1", "$2", ... with the display text of each parameter. |
| 67 out_message = |
| 68 ReplaceStringPlaceholders(template_dollars, display_texts, &out_offsets); |
| 69 |
| 70 // Each parameter must be used exactly once. If a parameter is unused or |
| 71 // used more than once then it can't be determined which |offsets| entry |
| 72 // corresponds to which parameter. |
| 73 return out_offsets.size() == display_texts.size(); |
| 74 } |
| 75 |
| 76 // Parses |line| and sets |out|. |
| 77 // Returns false on failure. |out| is not modified if false is returned. |
| 78 bool ParseLegalMessageLine( |
| 79 const base::DictionaryValue& line, |
| 80 autofill::SaveCardBubbleController::LegalMessageLine* out) { |
| 81 autofill::SaveCardBubbleController::LegalMessageLine result; |
| 82 |
| 83 // |display_texts| elements are the strings that will be substituted for |
| 84 // "{0}", "{1}", etc. in the template string. |
| 85 std::vector<base::string16> display_texts; |
| 86 |
| 87 // Process all the template parameters. |
| 88 const base::ListValue* template_parameters = nullptr; |
| 89 if (line.GetList("template_parameter", &template_parameters)) { |
| 90 for (size_t parameter_index = 0; |
| 91 parameter_index < template_parameters->GetSize(); ++parameter_index) { |
| 92 // Get a single element of the "template_parameter" list. |
| 93 const base::DictionaryValue* single_parameter; |
| 94 if (!template_parameters->GetDictionary(parameter_index, |
| 95 &single_parameter)) |
| 96 return false; |
| 97 |
| 98 // Read and store the "display_text" string. |
| 99 display_texts.push_back(base::string16()); |
| 100 if (!single_parameter->GetString("display_text", &display_texts.back())) |
| 101 return false; |
| 102 |
| 103 // Read and store the "url" string. |
| 104 std::string url; |
| 105 if (!single_parameter->GetString("url", &url)) |
| 106 return false; |
| 107 result.links.push_back( |
| 108 autofill::SaveCardBubbleController::LegalMessageLine::Link()); |
| 109 result.links.back().url = GURL(url); |
| 110 } |
| 111 } |
| 112 |
| 113 // Read the template string. It's a small subset of the ICU message format |
| 114 // syntax. |
| 115 base::string16 template_icu; |
| 116 if (!line.GetString("template", &template_icu)) |
| 117 return false; |
| 118 |
| 119 // Replace the placeholders in |template_icu| with strings from |
| 120 // |display_texts|, and store the start position of each replacement in |
| 121 // |offsets|. |
| 122 std::vector<size_t> offsets; |
| 123 if (!ReplaceTemplatePlaceholders(template_icu, display_texts, result.text, |
| 124 offsets)) |
| 125 return false; |
| 126 |
| 127 // Fill in range values for all links. |
| 128 for (size_t offset_index = 0; offset_index < offsets.size(); ++offset_index) { |
| 129 size_t range_start = offsets[offset_index]; |
| 130 result.links[offset_index].range = gfx::Range( |
| 131 range_start, range_start + display_texts[offset_index].size()); |
| 132 } |
| 133 |
| 134 *out = result; |
| 135 return true; |
| 136 } |
| 137 |
24 } // namespace | 138 } // namespace |
25 | 139 |
26 namespace autofill { | 140 namespace autofill { |
27 | 141 |
28 SaveCardBubbleControllerImpl::SaveCardBubbleControllerImpl( | 142 SaveCardBubbleControllerImpl::SaveCardBubbleControllerImpl( |
29 content::WebContents* web_contents) | 143 content::WebContents* web_contents) |
30 : content::WebContentsObserver(web_contents), | 144 : content::WebContentsObserver(web_contents), |
31 save_card_bubble_view_(nullptr) { | 145 save_card_bubble_view_(nullptr) { |
32 DCHECK(web_contents); | 146 DCHECK(web_contents); |
33 } | 147 } |
34 | 148 |
35 SaveCardBubbleControllerImpl::~SaveCardBubbleControllerImpl() { | 149 SaveCardBubbleControllerImpl::~SaveCardBubbleControllerImpl() { |
36 if (save_card_bubble_view_) | 150 if (save_card_bubble_view_) |
37 save_card_bubble_view_->Hide(); | 151 save_card_bubble_view_->Hide(); |
38 } | 152 } |
39 | 153 |
40 void SaveCardBubbleControllerImpl::SetCallback( | 154 void SaveCardBubbleControllerImpl::SetCallback( |
41 const base::Closure& save_card_callback) { | 155 const base::Closure& save_card_callback) { |
42 save_card_callback_ = save_card_callback; | 156 save_card_callback_ = save_card_callback; |
43 } | 157 } |
44 | 158 |
| 159 bool SaveCardBubbleControllerImpl::SetLegalMessage( |
| 160 const base::ListValue& lines) { |
| 161 // Process all lines of the message. See comment in header file for example |
| 162 // of valid |lines| data. |
| 163 legal_message_lines_.clear(); |
| 164 for (size_t line_index = 0; line_index < lines.GetSize(); ++line_index) { |
| 165 const base::DictionaryValue* single_line; |
| 166 if (!lines.GetDictionary(line_index, &single_line)) { |
| 167 legal_message_lines_.clear(); |
| 168 return false; |
| 169 } |
| 170 |
| 171 legal_message_lines_.push_back(LegalMessageLine()); |
| 172 if (!ParseLegalMessageLine(*single_line, &legal_message_lines_.back())) { |
| 173 legal_message_lines_.clear(); |
| 174 return false; |
| 175 } |
| 176 } |
| 177 |
| 178 return true; |
| 179 } |
| 180 |
| 181 void SaveCardBubbleControllerImpl::ClearLegalMessage() { |
| 182 legal_message_lines_.clear(); |
| 183 } |
| 184 |
45 void SaveCardBubbleControllerImpl::ShowBubble(bool user_action) { | 185 void SaveCardBubbleControllerImpl::ShowBubble(bool user_action) { |
46 DCHECK(!save_card_callback_.is_null()); | 186 DCHECK(!save_card_callback_.is_null()); |
47 | 187 |
48 // Need to create location bar icon before bubble, otherwise bubble will be | 188 // Need to create location bar icon before bubble, otherwise bubble will be |
49 // unanchored. | 189 // unanchored. |
50 UpdateIcon(); | 190 UpdateIcon(); |
51 | 191 |
52 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 192 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
53 save_card_bubble_view_ = browser->window()->ShowSaveCreditCardBubble( | 193 save_card_bubble_view_ = browser->window()->ShowSaveCreditCardBubble( |
54 web_contents(), this, user_action); | 194 web_contents(), this, user_action); |
(...skipping 18 matching lines...) Expand all Loading... |
73 void SaveCardBubbleControllerImpl::OnSaveButton() { | 213 void SaveCardBubbleControllerImpl::OnSaveButton() { |
74 save_card_callback_.Run(); | 214 save_card_callback_.Run(); |
75 save_card_callback_.Reset(); | 215 save_card_callback_.Reset(); |
76 } | 216 } |
77 | 217 |
78 void SaveCardBubbleControllerImpl::OnCancelButton() { | 218 void SaveCardBubbleControllerImpl::OnCancelButton() { |
79 save_card_callback_.Reset(); | 219 save_card_callback_.Reset(); |
80 } | 220 } |
81 | 221 |
82 void SaveCardBubbleControllerImpl::OnLearnMoreClicked() { | 222 void SaveCardBubbleControllerImpl::OnLearnMoreClicked() { |
83 web_contents()->OpenURL(content::OpenURLParams( | 223 OpenUrl(GURL(kHelpURL)); |
84 GURL(kHelpURL), content::Referrer(), NEW_FOREGROUND_TAB, | 224 } |
85 ui::PAGE_TRANSITION_LINK, false)); | 225 |
| 226 void SaveCardBubbleControllerImpl::OnLegalMessageLinkClicked(const GURL& url) { |
| 227 OpenUrl(url); |
86 } | 228 } |
87 | 229 |
88 void SaveCardBubbleControllerImpl::OnBubbleClosed() { | 230 void SaveCardBubbleControllerImpl::OnBubbleClosed() { |
89 save_card_bubble_view_ = nullptr; | 231 save_card_bubble_view_ = nullptr; |
90 UpdateIcon(); | 232 UpdateIcon(); |
91 } | 233 } |
92 | 234 |
| 235 const std::vector<SaveCardBubbleController::LegalMessageLine>& |
| 236 SaveCardBubbleControllerImpl::GetLegalMessageLines() const { |
| 237 return legal_message_lines_; |
| 238 } |
| 239 |
93 void SaveCardBubbleControllerImpl::UpdateIcon() { | 240 void SaveCardBubbleControllerImpl::UpdateIcon() { |
94 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 241 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
95 LocationBar* location_bar = browser->window()->GetLocationBar(); | 242 LocationBar* location_bar = browser->window()->GetLocationBar(); |
96 location_bar->UpdateSaveCreditCardIcon(); | 243 location_bar->UpdateSaveCreditCardIcon(); |
97 } | 244 } |
98 | 245 |
| 246 void SaveCardBubbleControllerImpl::OpenUrl(const GURL& url) { |
| 247 web_contents()->OpenURL( |
| 248 content::OpenURLParams(url, content::Referrer(), NEW_FOREGROUND_TAB, |
| 249 ui::PAGE_TRANSITION_LINK, false)); |
| 250 } |
| 251 |
99 void SaveCardBubbleControllerImpl::DidNavigateMainFrame( | 252 void SaveCardBubbleControllerImpl::DidNavigateMainFrame( |
100 const content::LoadCommittedDetails& details, | 253 const content::LoadCommittedDetails& details, |
101 const content::FrameNavigateParams& params) { | 254 const content::FrameNavigateParams& params) { |
102 // Nothing to do if there's no bubble available. | 255 // Nothing to do if there's no bubble available. |
103 if (save_card_callback_.is_null()) | 256 if (save_card_callback_.is_null()) |
104 return; | 257 return; |
105 | 258 |
106 // Don't react to in-page (fragment) navigations. | 259 // Don't react to in-page (fragment) navigations. |
107 if (details.is_in_page) | 260 if (details.is_in_page) |
108 return; | 261 return; |
109 | 262 |
110 // Don't do anything if a navigation occurs before a user could reasonably | 263 // Don't do anything if a navigation occurs before a user could reasonably |
111 // interact with the bubble. | 264 // interact with the bubble. |
112 if (timer_->Elapsed() < | 265 if (timer_->Elapsed() < |
113 base::TimeDelta::FromSeconds(kSurviveNavigationSeconds)) | 266 base::TimeDelta::FromSeconds(kSurviveNavigationSeconds)) |
114 return; | 267 return; |
115 | 268 |
116 // Otherwise, get rid of the bubble and icon. | 269 // Otherwise, get rid of the bubble and icon. |
117 save_card_callback_.Reset(); | 270 save_card_callback_.Reset(); |
118 if (save_card_bubble_view_) { | 271 if (save_card_bubble_view_) { |
119 save_card_bubble_view_->Hide(); | 272 save_card_bubble_view_->Hide(); |
120 OnBubbleClosed(); | 273 OnBubbleClosed(); |
121 } else { | 274 } else { |
122 UpdateIcon(); | 275 UpdateIcon(); |
123 } | 276 } |
124 } | 277 } |
125 | 278 |
126 } // namespace autofill | 279 } // namespace autofill |
OLD | NEW |