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) { | |
Evan Stade
2015/11/17 19:38:19
no non-const refs as parameters. Outparams should
bondd
2015/11/17 23:48:58
Done.
| |
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 = base::ReplaceStringPlaceholders(template_dollars, display_texts, | |
68 &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, | |
Evan Stade
2015/11/17 19:38:19
nit: curlies if the condition wraps
bondd
2015/11/17 23:48:58
Done.
| |
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)) | |
Evan Stade
2015/11/17 19:38:19
nit: curlies if the condition wraps
bondd
2015/11/17 23:48:58
Done.
| |
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 } | |
Evan Stade
2015/11/17 19:38:19
nit:
if (!lines.GetDictionary(line_index, &single
bondd
2015/11/17 23:48:58
Done. Good one.
| |
176 } | |
177 | |
178 return true; | |
179 } | |
180 | |
45 void SaveCardBubbleControllerImpl::ShowBubble(bool user_action) { | 181 void SaveCardBubbleControllerImpl::ShowBubble(bool user_action) { |
46 DCHECK(!save_card_callback_.is_null()); | 182 DCHECK(!save_card_callback_.is_null()); |
47 | 183 |
48 // Need to create location bar icon before bubble, otherwise bubble will be | 184 // Need to create location bar icon before bubble, otherwise bubble will be |
49 // unanchored. | 185 // unanchored. |
50 UpdateIcon(); | 186 UpdateIcon(); |
51 | 187 |
52 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 188 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
53 save_card_bubble_view_ = browser->window()->ShowSaveCreditCardBubble( | 189 save_card_bubble_view_ = browser->window()->ShowSaveCreditCardBubble( |
54 web_contents(), this, user_action); | 190 web_contents(), this, user_action); |
(...skipping 18 matching lines...) Expand all Loading... | |
73 void SaveCardBubbleControllerImpl::OnSaveButton() { | 209 void SaveCardBubbleControllerImpl::OnSaveButton() { |
74 save_card_callback_.Run(); | 210 save_card_callback_.Run(); |
75 save_card_callback_.Reset(); | 211 save_card_callback_.Reset(); |
76 } | 212 } |
77 | 213 |
78 void SaveCardBubbleControllerImpl::OnCancelButton() { | 214 void SaveCardBubbleControllerImpl::OnCancelButton() { |
79 save_card_callback_.Reset(); | 215 save_card_callback_.Reset(); |
80 } | 216 } |
81 | 217 |
82 void SaveCardBubbleControllerImpl::OnLearnMoreClicked() { | 218 void SaveCardBubbleControllerImpl::OnLearnMoreClicked() { |
83 web_contents()->OpenURL(content::OpenURLParams( | 219 OpenUrl(GURL(kHelpURL)); |
84 GURL(kHelpURL), content::Referrer(), NEW_FOREGROUND_TAB, | 220 } |
85 ui::PAGE_TRANSITION_LINK, false)); | 221 |
222 void SaveCardBubbleControllerImpl::OnLegalMessageLinkClicked(const GURL& url) { | |
223 OpenUrl(url); | |
86 } | 224 } |
87 | 225 |
88 void SaveCardBubbleControllerImpl::OnBubbleClosed() { | 226 void SaveCardBubbleControllerImpl::OnBubbleClosed() { |
89 save_card_bubble_view_ = nullptr; | 227 save_card_bubble_view_ = nullptr; |
90 UpdateIcon(); | 228 UpdateIcon(); |
91 } | 229 } |
92 | 230 |
231 const std::vector<SaveCardBubbleController::LegalMessageLine>& | |
232 SaveCardBubbleControllerImpl::GetLegalMessageLines() const { | |
233 return legal_message_lines_; | |
234 } | |
235 | |
93 void SaveCardBubbleControllerImpl::UpdateIcon() { | 236 void SaveCardBubbleControllerImpl::UpdateIcon() { |
94 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 237 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
95 LocationBar* location_bar = browser->window()->GetLocationBar(); | 238 LocationBar* location_bar = browser->window()->GetLocationBar(); |
96 location_bar->UpdateSaveCreditCardIcon(); | 239 location_bar->UpdateSaveCreditCardIcon(); |
97 } | 240 } |
98 | 241 |
242 void SaveCardBubbleControllerImpl::OpenUrl(const GURL& url) { | |
243 web_contents()->OpenURL( | |
244 content::OpenURLParams(url, content::Referrer(), NEW_FOREGROUND_TAB, | |
245 ui::PAGE_TRANSITION_LINK, false)); | |
246 } | |
247 | |
99 void SaveCardBubbleControllerImpl::DidNavigateMainFrame( | 248 void SaveCardBubbleControllerImpl::DidNavigateMainFrame( |
100 const content::LoadCommittedDetails& details, | 249 const content::LoadCommittedDetails& details, |
101 const content::FrameNavigateParams& params) { | 250 const content::FrameNavigateParams& params) { |
102 // Nothing to do if there's no bubble available. | 251 // Nothing to do if there's no bubble available. |
103 if (save_card_callback_.is_null()) | 252 if (save_card_callback_.is_null()) |
104 return; | 253 return; |
105 | 254 |
106 // Don't react to in-page (fragment) navigations. | 255 // Don't react to in-page (fragment) navigations. |
107 if (details.is_in_page) | 256 if (details.is_in_page) |
108 return; | 257 return; |
109 | 258 |
110 // Don't do anything if a navigation occurs before a user could reasonably | 259 // Don't do anything if a navigation occurs before a user could reasonably |
111 // interact with the bubble. | 260 // interact with the bubble. |
112 if (timer_->Elapsed() < | 261 if (timer_->Elapsed() < |
113 base::TimeDelta::FromSeconds(kSurviveNavigationSeconds)) | 262 base::TimeDelta::FromSeconds(kSurviveNavigationSeconds)) |
114 return; | 263 return; |
115 | 264 |
116 // Otherwise, get rid of the bubble and icon. | 265 // Otherwise, get rid of the bubble and icon. |
117 save_card_callback_.Reset(); | 266 save_card_callback_.Reset(); |
118 if (save_card_bubble_view_) { | 267 if (save_card_bubble_view_) { |
119 save_card_bubble_view_->Hide(); | 268 save_card_bubble_view_->Hide(); |
120 OnBubbleClosed(); | 269 OnBubbleClosed(); |
121 } else { | 270 } else { |
122 UpdateIcon(); | 271 UpdateIcon(); |
123 } | 272 } |
124 } | 273 } |
125 | 274 |
126 } // namespace autofill | 275 } // namespace autofill |
OLD | NEW |