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