OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/elide_url.h" | 5 #include "components/secure_display/elide_url.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "net/base/escape.h" | 10 #include "net/base/escape.h" |
11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
13 #include "ui/gfx/text_elider.h" | 13 #include "ui/gfx/text_elider.h" |
14 #include "ui/gfx/text_utils.h" | 14 #include "ui/gfx/text_utils.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 #include "url/url_constants.h" | 16 #include "url/url_constants.h" |
17 | 17 |
18 using base::UTF8ToUTF16; | 18 using base::UTF8ToUTF16; |
19 using gfx::ElideText; | 19 using gfx::ElideText; |
20 using gfx::GetStringWidthF; | 20 using gfx::GetStringWidthF; |
21 using gfx::kEllipsisUTF16; | 21 using gfx::kEllipsisUTF16; |
22 using gfx::kForwardSlash; | 22 using gfx::kForwardSlash; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
| 26 #if !defined(OS_ANDROID) |
26 const base::char16 kDot = '.'; | 27 const base::char16 kDot = '.'; |
27 | 28 |
28 // Build a path from the first |num_components| elements in |path_elements|. | 29 // Build a path from the first |num_components| elements in |path_elements|. |
29 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. | 30 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. |
30 base::string16 BuildPathFromComponents( | 31 base::string16 BuildPathFromComponents( |
31 const base::string16& path_prefix, | 32 const base::string16& path_prefix, |
32 const std::vector<base::string16>& path_elements, | 33 const std::vector<base::string16>& path_elements, |
33 const base::string16& filename, | 34 const base::string16& filename, |
34 size_t num_components) { | 35 size_t num_components) { |
35 // Add the initial elements of the path. | 36 // Add the initial elements of the path. |
(...skipping 18 matching lines...) Expand all Loading... |
54 const base::string16& url_path_prefix, | 55 const base::string16& url_path_prefix, |
55 const std::vector<base::string16>& url_path_elements, | 56 const std::vector<base::string16>& url_path_elements, |
56 const base::string16& url_filename, | 57 const base::string16& url_filename, |
57 const base::string16& url_query, | 58 const base::string16& url_query, |
58 const gfx::FontList& font_list, | 59 const gfx::FontList& font_list, |
59 float available_pixel_width) { | 60 float available_pixel_width) { |
60 const size_t url_path_number_of_elements = url_path_elements.size(); | 61 const size_t url_path_number_of_elements = url_path_elements.size(); |
61 | 62 |
62 CHECK(url_path_number_of_elements); | 63 CHECK(url_path_number_of_elements); |
63 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { | 64 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { |
64 base::string16 elided_path = BuildPathFromComponents(url_path_prefix, | 65 base::string16 elided_path = BuildPathFromComponents( |
65 url_path_elements, url_filename, i); | 66 url_path_prefix, url_path_elements, url_filename, i); |
66 if (available_pixel_width >= GetStringWidthF(elided_path, font_list)) | 67 if (available_pixel_width >= GetStringWidthF(elided_path, font_list)) |
67 return ElideText(elided_path + url_query, font_list, | 68 return ElideText(elided_path + url_query, font_list, |
68 available_pixel_width, gfx::ELIDE_TAIL); | 69 available_pixel_width, gfx::ELIDE_TAIL); |
69 } | 70 } |
70 | 71 |
71 return base::string16(); | 72 return base::string16(); |
72 } | 73 } |
73 | 74 |
74 // Splits the hostname in the |url| into sub-strings for the full hostname, | 75 // Splits the hostname in the |url| into sub-strings for the full hostname, |
75 // the domain (TLD+1), and the subdomain (everything leading the domain). | 76 // the domain (TLD+1), and the subdomain (everything leading the domain). |
76 void SplitHost(const GURL& url, | 77 void SplitHost(const GURL& url, |
77 base::string16* url_host, | 78 base::string16* url_host, |
78 base::string16* url_domain, | 79 base::string16* url_domain, |
79 base::string16* url_subdomain) { | 80 base::string16* url_subdomain) { |
80 // Get Host. | 81 // Get Host. |
81 *url_host = UTF8ToUTF16(url.host()); | 82 *url_host = UTF8ToUTF16(url.host()); |
82 | 83 |
83 // Get domain and registry information from the URL. | 84 // Get domain and registry information from the URL. |
84 *url_domain = UTF8ToUTF16( | 85 *url_domain = |
85 net::registry_controlled_domains::GetDomainAndRegistry( | 86 UTF8ToUTF16(net::registry_controlled_domains::GetDomainAndRegistry( |
86 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); | 87 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
87 if (url_domain->empty()) | 88 if (url_domain->empty()) |
88 *url_domain = *url_host; | 89 *url_domain = *url_host; |
89 | 90 |
90 // Add port if required. | 91 // Add port if required. |
91 if (!url.port().empty()) { | 92 if (!url.port().empty()) { |
92 *url_host += UTF8ToUTF16(":" + url.port()); | 93 *url_host += UTF8ToUTF16(":" + url.port()); |
93 *url_domain += UTF8ToUTF16(":" + url.port()); | 94 *url_domain += UTF8ToUTF16(":" + url.port()); |
94 } | 95 } |
95 | 96 |
96 // Get sub domain. | 97 // Get sub domain. |
97 const size_t domain_start_index = url_host->find(*url_domain); | 98 const size_t domain_start_index = url_host->find(*url_domain); |
98 base::string16 kWwwPrefix = UTF8ToUTF16("www."); | 99 base::string16 kWwwPrefix = UTF8ToUTF16("www."); |
99 if (domain_start_index != base::string16::npos) | 100 if (domain_start_index != base::string16::npos) |
100 *url_subdomain = url_host->substr(0, domain_start_index); | 101 *url_subdomain = url_host->substr(0, domain_start_index); |
101 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || | 102 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || |
102 url.SchemeIsFile())) { | 103 url.SchemeIsFile())) { |
103 url_subdomain->clear(); | 104 url_subdomain->clear(); |
104 } | 105 } |
105 } | 106 } |
106 | 107 |
| 108 #endif // !defined(OS_ANDROID) |
107 } // namespace | 109 } // namespace |
108 | 110 |
| 111 namespace secure_display { |
| 112 |
| 113 #if !defined(OS_ANDROID) |
| 114 |
109 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 115 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
110 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 116 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of |
111 // a rendered string is always the sum of the widths of its substrings. Also I | 117 // a rendered string is always the sum of the widths of its substrings. Also I |
112 // suspect it could be made simpler. | 118 // suspect it could be made simpler. |
113 base::string16 ElideUrl(const GURL& url, | 119 base::string16 ElideUrl(const GURL& url, |
114 const gfx::FontList& font_list, | 120 const gfx::FontList& font_list, |
115 float available_pixel_width, | 121 float available_pixel_width, |
116 const std::string& languages) { | 122 const std::string& languages) { |
117 // Get a formatted string and corresponding parsing of the url. | 123 // Get a formatted string and corresponding parsing of the url. |
118 url::Parsed parsed; | 124 url::Parsed parsed; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 url_domain.clear(); | 170 url_domain.clear(); |
165 url_subdomain.clear(); | 171 url_subdomain.clear(); |
166 | 172 |
167 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; | 173 url_host = url_domain = file_path_split.at(0).substr(1) + kColon; |
168 url_path_query_etc = url_path = file_path_split.at(1); | 174 url_path_query_etc = url_path = file_path_split.at(1); |
169 } | 175 } |
170 } | 176 } |
171 | 177 |
172 // Second Pass - remove scheme - the rest fits. | 178 // Second Pass - remove scheme - the rest fits. |
173 const float pixel_width_url_host = GetStringWidthF(url_host, font_list); | 179 const float pixel_width_url_host = GetStringWidthF(url_host, font_list); |
174 const float pixel_width_url_path = GetStringWidthF(url_path_query_etc, | 180 const float pixel_width_url_path = |
175 font_list); | 181 GetStringWidthF(url_path_query_etc, font_list); |
176 if (available_pixel_width >= | 182 if (available_pixel_width >= pixel_width_url_host + pixel_width_url_path) |
177 pixel_width_url_host + pixel_width_url_path) | |
178 return url_host + url_path_query_etc; | 183 return url_host + url_path_query_etc; |
179 | 184 |
180 // Third Pass: Subdomain, domain and entire path fits. | 185 // Third Pass: Subdomain, domain and entire path fits. |
181 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); | 186 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); |
182 const float pixel_width_url_subdomain = | 187 const float pixel_width_url_subdomain = |
183 GetStringWidthF(url_subdomain, font_list); | 188 GetStringWidthF(url_subdomain, font_list); |
184 if (available_pixel_width >= | 189 if (available_pixel_width >= |
185 pixel_width_url_subdomain + pixel_width_url_domain + | 190 pixel_width_url_subdomain + pixel_width_url_domain + pixel_width_url_path) |
186 pixel_width_url_path) | |
187 return url_subdomain + url_domain + url_path_query_etc; | 191 return url_subdomain + url_domain + url_path_query_etc; |
188 | 192 |
189 // Query element. | 193 // Query element. |
190 base::string16 url_query; | 194 base::string16 url_query; |
191 const float kPixelWidthDotsTrailer = GetStringWidthF( | 195 const float kPixelWidthDotsTrailer = |
192 base::string16(kEllipsisUTF16), font_list); | 196 GetStringWidthF(base::string16(kEllipsisUTF16), font_list); |
193 if (parsed.query.is_nonempty()) { | 197 if (parsed.query.is_nonempty()) { |
194 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); | 198 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); |
195 if (available_pixel_width >= | 199 if (available_pixel_width >= |
196 (pixel_width_url_subdomain + pixel_width_url_domain + | 200 (pixel_width_url_subdomain + pixel_width_url_domain + |
197 pixel_width_url_path - GetStringWidthF(url_query, font_list))) { | 201 pixel_width_url_path - GetStringWidthF(url_query, font_list))) { |
198 return ElideText(url_subdomain + url_domain + url_path_query_etc, | 202 return ElideText(url_subdomain + url_domain + url_path_query_etc, |
199 font_list, available_pixel_width, gfx::ELIDE_TAIL); | 203 font_list, available_pixel_width, gfx::ELIDE_TAIL); |
200 } | 204 } |
201 } | 205 } |
202 | 206 |
203 // Parse url_path using '/'. | 207 // Parse url_path using '/'. |
204 std::vector<base::string16> url_path_elements = base::SplitString( | 208 std::vector<base::string16> url_path_elements = |
205 url_path, base::string16(1, kForwardSlash), | 209 base::SplitString(url_path, base::string16(1, kForwardSlash), |
206 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 210 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
207 | 211 |
208 // Get filename - note that for a path ending with / | 212 // Get filename - note that for a path ending with / |
209 // such as www.google.com/intl/ads/, the file name is ads/. | 213 // such as www.google.com/intl/ads/, the file name is ads/. |
210 base::string16 url_filename( | 214 base::string16 url_filename( |
211 url_path_elements.empty() ? base::string16() : url_path_elements.back()); | 215 url_path_elements.empty() ? base::string16() : url_path_elements.back()); |
212 size_t url_path_number_of_elements = url_path_elements.size(); | 216 size_t url_path_number_of_elements = url_path_elements.size(); |
213 if (url_filename.empty() && (url_path_number_of_elements > 1)) { | 217 if (url_filename.empty() && (url_path_number_of_elements > 1)) { |
214 // Path ends with a '/'. | 218 // Path ends with a '/'. |
215 --url_path_number_of_elements; | 219 --url_path_number_of_elements; |
216 url_filename = url_path_elements[url_path_number_of_elements - 1] + | 220 url_filename = |
217 kForwardSlash; | 221 url_path_elements[url_path_number_of_elements - 1] + kForwardSlash; |
218 } | 222 } |
219 | 223 |
220 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; | 224 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; |
221 if (url_path_number_of_elements <= 1 || | 225 if (url_path_number_of_elements <= 1 || |
222 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { | 226 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { |
223 // No path to elide, or too long of a path (could overflow in loop below) | 227 // No path to elide, or too long of a path (could overflow in loop below) |
224 // Just elide this as a text string. | 228 // Just elide this as a text string. |
225 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, | 229 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list, |
226 available_pixel_width, gfx::ELIDE_TAIL); | 230 available_pixel_width, gfx::ELIDE_TAIL); |
227 } | 231 } |
228 | 232 |
229 // Start eliding the path and replacing elements by ".../". | 233 // Start eliding the path and replacing elements by ".../". |
230 const base::string16 kEllipsisAndSlash = | 234 const base::string16 kEllipsisAndSlash = |
231 base::string16(kEllipsisUTF16) + kForwardSlash; | 235 base::string16(kEllipsisUTF16) + kForwardSlash; |
232 const float pixel_width_ellipsis_slash = | 236 const float pixel_width_ellipsis_slash = |
233 GetStringWidthF(kEllipsisAndSlash, font_list); | 237 GetStringWidthF(kEllipsisAndSlash, font_list); |
234 | 238 |
235 // Check with both subdomain and domain. | 239 // Check with both subdomain and domain. |
236 base::string16 elided_path = | 240 base::string16 elided_path = ElideComponentizedPath( |
237 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, | 241 url_subdomain + url_domain, url_path_elements, url_filename, url_query, |
238 url_filename, url_query, font_list, | 242 font_list, available_pixel_width); |
239 available_pixel_width); | |
240 if (!elided_path.empty()) | 243 if (!elided_path.empty()) |
241 return elided_path; | 244 return elided_path; |
242 | 245 |
243 // Check with only domain. | 246 // Check with only domain. |
244 // If a subdomain is present, add an ellipsis before domain. | 247 // If a subdomain is present, add an ellipsis before domain. |
245 // This is added only if the subdomain pixel width is larger than | 248 // This is added only if the subdomain pixel width is larger than |
246 // the pixel width of kEllipsis. Otherwise, subdomain remains, | 249 // the pixel width of kEllipsis. Otherwise, subdomain remains, |
247 // which means that this case has been resolved earlier. | 250 // which means that this case has been resolved earlier. |
248 base::string16 url_elided_domain = url_subdomain + url_domain; | 251 base::string16 url_elided_domain = url_subdomain + url_domain; |
249 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { | 252 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { |
250 if (!url_subdomain.empty()) | 253 if (!url_subdomain.empty()) |
251 url_elided_domain = kEllipsisAndSlash[0] + url_domain; | 254 url_elided_domain = kEllipsisAndSlash[0] + url_domain; |
252 else | 255 else |
253 url_elided_domain = url_domain; | 256 url_elided_domain = url_domain; |
254 | 257 |
255 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, | 258 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, |
256 url_filename, url_query, font_list, | 259 url_filename, url_query, font_list, |
257 available_pixel_width); | 260 available_pixel_width); |
258 | 261 |
259 if (!elided_path.empty()) | 262 if (!elided_path.empty()) |
260 return elided_path; | 263 return elided_path; |
261 } | 264 } |
262 | 265 |
263 // Return elided domain/.../filename anyway. | 266 // Return elided domain/.../filename anyway. |
264 base::string16 final_elided_url_string(url_elided_domain); | 267 base::string16 final_elided_url_string(url_elided_domain); |
265 const float url_elided_domain_width = GetStringWidthF(url_elided_domain, | 268 const float url_elided_domain_width = |
266 font_list); | 269 GetStringWidthF(url_elided_domain, font_list); |
267 | 270 |
268 // A hack to prevent trailing ".../...". | 271 // A hack to prevent trailing ".../...". |
269 if ((available_pixel_width - url_elided_domain_width) > | 272 if ((available_pixel_width - url_elided_domain_width) > |
270 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + | 273 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + |
271 GetStringWidthF(base::ASCIIToUTF16("UV"), font_list)) { | 274 GetStringWidthF(base::ASCIIToUTF16("UV"), font_list)) { |
272 final_elided_url_string += BuildPathFromComponents(base::string16(), | 275 final_elided_url_string += BuildPathFromComponents( |
273 url_path_elements, url_filename, 1); | 276 base::string16(), url_path_elements, url_filename, 1); |
274 } else { | 277 } else { |
275 final_elided_url_string += url_path; | 278 final_elided_url_string += url_path; |
276 } | 279 } |
277 | 280 |
278 return ElideText(final_elided_url_string, font_list, available_pixel_width, | 281 return ElideText(final_elided_url_string, font_list, available_pixel_width, |
279 gfx::ELIDE_TAIL); | 282 gfx::ELIDE_TAIL); |
280 } | 283 } |
281 | 284 |
282 base::string16 ElideHost(const GURL& url, | 285 base::string16 ElideHost(const GURL& url, |
283 const gfx::FontList& font_list, | 286 const gfx::FontList& font_list, |
284 float available_pixel_width) { | 287 float available_pixel_width) { |
285 base::string16 url_host; | 288 base::string16 url_host; |
286 base::string16 url_domain; | 289 base::string16 url_domain; |
287 base::string16 url_subdomain; | 290 base::string16 url_subdomain; |
288 SplitHost(url, &url_host, &url_domain, &url_subdomain); | 291 SplitHost(url, &url_host, &url_domain, &url_subdomain); |
289 | 292 |
290 const float pixel_width_url_host = GetStringWidthF(url_host, font_list); | 293 const float pixel_width_url_host = GetStringWidthF(url_host, font_list); |
291 if (available_pixel_width >= pixel_width_url_host) | 294 if (available_pixel_width >= pixel_width_url_host) |
292 return url_host; | 295 return url_host; |
293 | 296 |
294 if (url_subdomain.empty()) | 297 if (url_subdomain.empty()) |
295 return url_domain; | 298 return url_domain; |
296 | 299 |
297 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); | 300 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list); |
298 float subdomain_width = available_pixel_width - pixel_width_url_domain; | 301 float subdomain_width = available_pixel_width - pixel_width_url_domain; |
299 if (subdomain_width <= 0) | 302 if (subdomain_width <= 0) |
300 return base::string16(kEllipsisUTF16) + kDot + url_domain; | 303 return base::string16(kEllipsisUTF16) + kDot + url_domain; |
301 | 304 |
302 const base::string16 elided_subdomain = ElideText( | 305 const base::string16 elided_subdomain = |
303 url_subdomain, font_list, subdomain_width, gfx::ELIDE_HEAD); | 306 ElideText(url_subdomain, font_list, subdomain_width, gfx::ELIDE_HEAD); |
304 return elided_subdomain + url_domain; | 307 return elided_subdomain + url_domain; |
305 } | 308 } |
306 | 309 |
| 310 #endif // !defined(OS_ANDROID) |
| 311 |
307 base::string16 FormatUrlForSecurityDisplay(const GURL& url, | 312 base::string16 FormatUrlForSecurityDisplay(const GURL& url, |
308 const std::string& languages) { | 313 const std::string& languages) { |
309 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) | 314 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) |
310 return net::FormatUrl(url, languages); | 315 return net::FormatUrl(url, languages); |
311 | 316 |
312 const base::string16 colon(base::ASCIIToUTF16(":")); | 317 const base::string16 colon(base::ASCIIToUTF16(":")); |
313 const base::string16 scheme_separator( | 318 const base::string16 scheme_separator( |
314 base::ASCIIToUTF16(url::kStandardSchemeSeparator)); | 319 base::ASCIIToUTF16(url::kStandardSchemeSeparator)); |
315 | 320 |
316 if (url.SchemeIsFile()) { | 321 if (url.SchemeIsFile()) { |
(...skipping 14 matching lines...) Expand all Loading... |
331 | 336 |
332 const GURL origin = url.GetOrigin(); | 337 const GURL origin = url.GetOrigin(); |
333 const std::string& scheme = origin.scheme(); | 338 const std::string& scheme = origin.scheme(); |
334 const std::string& host = origin.host(); | 339 const std::string& host = origin.host(); |
335 | 340 |
336 base::string16 result = base::UTF8ToUTF16(scheme); | 341 base::string16 result = base::UTF8ToUTF16(scheme); |
337 result += scheme_separator; | 342 result += scheme_separator; |
338 result += base::UTF8ToUTF16(host); | 343 result += base::UTF8ToUTF16(host); |
339 | 344 |
340 const int port = origin.IntPort(); | 345 const int port = origin.IntPort(); |
341 const int default_port = url::DefaultPortForScheme(origin.scheme().c_str(), | 346 const int default_port = url::DefaultPortForScheme( |
342 origin.scheme().length()); | 347 scheme.c_str(), static_cast<int>(scheme.length())); |
343 if (port != url::PORT_UNSPECIFIED && port != default_port) | 348 if (port != url::PORT_UNSPECIFIED && port != default_port) |
344 result += colon + base::UTF8ToUTF16(origin.port()); | 349 result += colon + base::UTF8ToUTF16(origin.port()); |
345 | 350 |
346 return result; | 351 return result; |
347 } | 352 } |
| 353 } // namespace secure_display |
OLD | NEW |