| 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 "components/url_formatter/elide_url.h" | 5 #include "components/url_formatter/elide_url.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 // Get sub domain. | 99 // Get sub domain. |
| 100 const size_t domain_start_index = url_host->find(*url_domain); | 100 const size_t domain_start_index = url_host->find(*url_domain); |
| 101 base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); | 101 base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); |
| 102 if (domain_start_index != base::string16::npos) | 102 if (domain_start_index != base::string16::npos) |
| 103 *url_subdomain = url_host->substr(0, domain_start_index); | 103 *url_subdomain = url_host->substr(0, domain_start_index); |
| 104 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || | 104 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || |
| 105 url.SchemeIsFile())) { | 105 url.SchemeIsFile())) { |
| 106 url_subdomain->clear(); | 106 url_subdomain->clear(); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | |
| 110 #endif // !defined(OS_ANDROID) | 109 #endif // !defined(OS_ANDROID) |
| 111 | 110 |
| 112 base::string16 FormatUrlForSecurityDisplayInternal(const GURL& url, | 111 bool ShouldShowScheme(const GURL& url, |
| 113 bool omit_scheme) { | 112 const url_formatter::SchemeDisplay scheme_display) { |
| 114 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) | 113 switch (scheme_display) { |
| 115 return url_formatter::FormatUrl(url); | 114 case url_formatter::SchemeDisplay::SHOW: |
| 115 return true; |
| 116 | 116 |
| 117 const base::string16 colon(base::ASCIIToUTF16(":")); | 117 case url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS: |
| 118 const base::string16 scheme_separator( | 118 return !url.SchemeIs(url::kHttpsScheme) && |
| 119 base::ASCIIToUTF16(url::kStandardSchemeSeparator)); | 119 !url.SchemeIs(url::kHttpScheme); |
| 120 | 120 |
| 121 if (url.SchemeIsFile()) { | 121 case url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC: |
| 122 return base::ASCIIToUTF16(url::kFileScheme) + scheme_separator + | 122 return !url.SchemeIsCryptographic(); |
| 123 base::UTF8ToUTF16(url.path()); | |
| 124 } | 123 } |
| 125 | 124 |
| 126 if (url.SchemeIsFileSystem()) { | 125 return true; |
| 127 const GURL* inner_url = url.inner_url(); | |
| 128 if (inner_url->SchemeIsFile()) { | |
| 129 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + | |
| 130 FormatUrlForSecurityDisplayInternal(*inner_url, false) + | |
| 131 base::UTF8ToUTF16(url.path()); | |
| 132 } | |
| 133 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + | |
| 134 FormatUrlForSecurityDisplayInternal(*inner_url, false); | |
| 135 } | |
| 136 | |
| 137 const GURL origin = url.GetOrigin(); | |
| 138 const std::string& scheme = origin.scheme(); | |
| 139 const std::string& host = origin.host(); | |
| 140 | |
| 141 base::string16 result; | |
| 142 if (!omit_scheme || !url.SchemeIsHTTPOrHTTPS()) | |
| 143 result = base::UTF8ToUTF16(scheme) + scheme_separator; | |
| 144 result += base::UTF8ToUTF16(host); | |
| 145 | |
| 146 const int port = origin.IntPort(); | |
| 147 const int default_port = url::DefaultPortForScheme( | |
| 148 scheme.c_str(), static_cast<int>(scheme.length())); | |
| 149 if (port != url::PORT_UNSPECIFIED && port != default_port) | |
| 150 result += colon + base::UTF8ToUTF16(origin.port()); | |
| 151 | |
| 152 return result; | |
| 153 } | 126 } |
| 154 | 127 |
| 155 } // namespace | 128 } // namespace |
| 156 | 129 |
| 157 namespace url_formatter { | 130 namespace url_formatter { |
| 158 | 131 |
| 159 #if !defined(OS_ANDROID) | 132 #if !defined(OS_ANDROID) |
| 160 | 133 |
| 161 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 134 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
| 162 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 135 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 float subdomain_width = available_pixel_width - pixel_width_url_domain; | 322 float subdomain_width = available_pixel_width - pixel_width_url_domain; |
| 350 if (subdomain_width <= 0) | 323 if (subdomain_width <= 0) |
| 351 return base::string16(gfx::kEllipsisUTF16) + kDot + url_domain; | 324 return base::string16(gfx::kEllipsisUTF16) + kDot + url_domain; |
| 352 | 325 |
| 353 return gfx::ElideText(url_host, font_list, available_pixel_width, | 326 return gfx::ElideText(url_host, font_list, available_pixel_width, |
| 354 gfx::ELIDE_HEAD); | 327 gfx::ELIDE_HEAD); |
| 355 } | 328 } |
| 356 | 329 |
| 357 #endif // !defined(OS_ANDROID) | 330 #endif // !defined(OS_ANDROID) |
| 358 | 331 |
| 359 base::string16 FormatUrlForSecurityDisplay(const GURL& url) { | 332 base::string16 FormatUrlForSecurityDisplay(const GURL& url, |
| 360 return FormatUrlForSecurityDisplayInternal(url, false); | 333 const SchemeDisplay scheme_display) { |
| 361 } | 334 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) |
| 335 return url_formatter::FormatUrl(url); |
| 362 | 336 |
| 363 base::string16 FormatUrlForSecurityDisplayOmitScheme(const GURL& url) { | 337 const base::string16 colon(base::ASCIIToUTF16(":")); |
| 364 return FormatUrlForSecurityDisplayInternal(url, true); | 338 const base::string16 scheme_separator( |
| 339 base::ASCIIToUTF16(url::kStandardSchemeSeparator)); |
| 340 |
| 341 if (url.SchemeIsFile()) { |
| 342 return base::ASCIIToUTF16(url::kFileScheme) + scheme_separator + |
| 343 base::UTF8ToUTF16(url.path()); |
| 344 } |
| 345 |
| 346 if (url.SchemeIsFileSystem()) { |
| 347 const GURL* inner_url = url.inner_url(); |
| 348 if (inner_url->SchemeIsFile()) { |
| 349 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + |
| 350 FormatUrlForSecurityDisplay(*inner_url) + |
| 351 base::UTF8ToUTF16(url.path()); |
| 352 } |
| 353 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + |
| 354 FormatUrlForSecurityDisplay(*inner_url); |
| 355 } |
| 356 |
| 357 const GURL origin = url.GetOrigin(); |
| 358 const std::string& scheme = origin.scheme(); |
| 359 const std::string& host = origin.host(); |
| 360 |
| 361 base::string16 result; |
| 362 if (ShouldShowScheme(url, scheme_display)) |
| 363 result = base::UTF8ToUTF16(scheme) + scheme_separator; |
| 364 result += base::UTF8ToUTF16(host); |
| 365 |
| 366 const int port = origin.IntPort(); |
| 367 const int default_port = url::DefaultPortForScheme( |
| 368 scheme.c_str(), static_cast<int>(scheme.length())); |
| 369 if (port != url::PORT_UNSPECIFIED && port != default_port) |
| 370 result += colon + base::UTF8ToUTF16(origin.port()); |
| 371 |
| 372 return result; |
| 365 } | 373 } |
| 366 | 374 |
| 367 } // namespace url_formatter | 375 } // namespace url_formatter |
| OLD | NEW |