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