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 "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 "components/url_formatter/url_formatter.h" | 10 #include "components/url_formatter/url_formatter.h" |
11 #include "net/base/escape.h" | 11 #include "net/base/escape.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 "url/gurl.h" | 13 #include "url/gurl.h" |
14 #include "url/url_constants.h" | 14 #include "url/url_constants.h" |
15 | 15 |
16 #if !defined(OS_ANDROID) || defined(USE_AURA) | 16 #if !defined(OS_ANDROID) |
17 #include "ui/gfx/text_elider.h" // nogncheck | 17 #include "ui/gfx/text_elider.h" // nogncheck |
18 #include "ui/gfx/text_utils.h" // nogncheck | 18 #include "ui/gfx/text_utils.h" // nogncheck |
19 #endif // !defined(OS_ANDROID) || defined(USE_AURA) | 19 #endif |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 #if !defined(OS_ANDROID) || defined(USE_AURA) | 23 #if !defined(OS_ANDROID) |
24 const base::char16 kDot = '.'; | 24 const base::char16 kDot = '.'; |
25 | 25 |
26 // Build a path from the first |num_components| elements in |path_elements|. | 26 // Build a path from the first |num_components| elements in |path_elements|. |
27 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. | 27 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. |
28 base::string16 BuildPathFromComponents( | 28 base::string16 BuildPathFromComponents( |
29 const base::string16& path_prefix, | 29 const base::string16& path_prefix, |
30 const std::vector<base::string16>& path_elements, | 30 const std::vector<base::string16>& path_elements, |
31 const base::string16& filename, | 31 const base::string16& filename, |
32 size_t num_components) { | 32 size_t num_components) { |
33 // Add the initial elements of the path. | 33 // Add the initial elements of the path. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 const size_t domain_start_index = url_host->find(*url_domain); | 95 const size_t domain_start_index = url_host->find(*url_domain); |
96 base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); | 96 base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); |
97 if (domain_start_index != base::string16::npos) | 97 if (domain_start_index != base::string16::npos) |
98 *url_subdomain = url_host->substr(0, domain_start_index); | 98 *url_subdomain = url_host->substr(0, domain_start_index); |
99 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || | 99 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || |
100 url.SchemeIsFile())) { | 100 url.SchemeIsFile())) { |
101 url_subdomain->clear(); | 101 url_subdomain->clear(); |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 #endif // !defined(OS_ANDROID) || defined(USE_AURA) | 105 #endif // !defined(OS_ANDROID) |
106 | 106 |
107 base::string16 FormatUrlForSecurityDisplayInternal(const GURL& url, | 107 base::string16 FormatUrlForSecurityDisplayInternal(const GURL& url, |
108 const std::string& languages, | 108 const std::string& languages, |
109 bool omit_scheme) { | 109 bool omit_scheme) { |
110 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) | 110 if (!url.is_valid() || url.is_empty() || !url.IsStandard()) |
111 return url_formatter::FormatUrl(url, languages); | 111 return url_formatter::FormatUrl(url, languages); |
112 | 112 |
113 const base::string16 colon(base::ASCIIToUTF16(":")); | 113 const base::string16 colon(base::ASCIIToUTF16(":")); |
114 const base::string16 scheme_separator( | 114 const base::string16 scheme_separator( |
115 base::ASCIIToUTF16(url::kStandardSchemeSeparator)); | 115 base::ASCIIToUTF16(url::kStandardSchemeSeparator)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 if (port != url::PORT_UNSPECIFIED && port != default_port) | 147 if (port != url::PORT_UNSPECIFIED && port != default_port) |
148 result += colon + base::UTF8ToUTF16(origin.port()); | 148 result += colon + base::UTF8ToUTF16(origin.port()); |
149 | 149 |
150 return result; | 150 return result; |
151 } | 151 } |
152 | 152 |
153 } // namespace | 153 } // namespace |
154 | 154 |
155 namespace url_formatter { | 155 namespace url_formatter { |
156 | 156 |
157 #if !defined(OS_ANDROID) || defined(USE_AURA) | 157 #if !defined(OS_ANDROID) |
158 | 158 |
159 // TODO(pkasting): http://crbug.com/77883 This whole function gets | 159 // TODO(pkasting): http://crbug.com/77883 This whole function gets |
160 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of | 160 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of |
161 // a rendered string is always the sum of the widths of its substrings. Also I | 161 // a rendered string is always the sum of the widths of its substrings. Also I |
162 // suspect it could be made simpler. | 162 // suspect it could be made simpler. |
163 base::string16 ElideUrl(const GURL& url, | 163 base::string16 ElideUrl(const GURL& url, |
164 const gfx::FontList& font_list, | 164 const gfx::FontList& font_list, |
165 float available_pixel_width, | 165 float available_pixel_width, |
166 const std::string& languages) { | 166 const std::string& languages) { |
167 // Get a formatted string and corresponding parsing of the url. | 167 // Get a formatted string and corresponding parsing of the url. |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 gfx::GetStringWidthF(url_domain, font_list); | 347 gfx::GetStringWidthF(url_domain, font_list); |
348 float subdomain_width = available_pixel_width - pixel_width_url_domain; | 348 float subdomain_width = available_pixel_width - pixel_width_url_domain; |
349 if (subdomain_width <= 0) | 349 if (subdomain_width <= 0) |
350 return base::string16(gfx::kEllipsisUTF16) + kDot + url_domain; | 350 return base::string16(gfx::kEllipsisUTF16) + kDot + url_domain; |
351 | 351 |
352 const base::string16 elided_subdomain = gfx::ElideText( | 352 const base::string16 elided_subdomain = gfx::ElideText( |
353 url_subdomain, font_list, subdomain_width, gfx::ELIDE_HEAD); | 353 url_subdomain, font_list, subdomain_width, gfx::ELIDE_HEAD); |
354 return elided_subdomain + url_domain; | 354 return elided_subdomain + url_domain; |
355 } | 355 } |
356 | 356 |
357 #endif // !defined(OS_ANDROID) || defined(USE_AURA) | 357 #endif // !defined(OS_ANDROID) |
358 | 358 |
359 base::string16 FormatUrlForSecurityDisplay(const GURL& url, | 359 base::string16 FormatUrlForSecurityDisplay(const GURL& url, |
360 const std::string& languages) { | 360 const std::string& languages) { |
361 return FormatUrlForSecurityDisplayInternal(url, languages, false); | 361 return FormatUrlForSecurityDisplayInternal(url, languages, false); |
362 } | 362 } |
363 | 363 |
364 base::string16 FormatUrlForSecurityDisplayOmitScheme( | 364 base::string16 FormatUrlForSecurityDisplayOmitScheme( |
365 const GURL& url, | 365 const GURL& url, |
366 const std::string& languages) { | 366 const std::string& languages) { |
367 return FormatUrlForSecurityDisplayInternal(url, languages, true); | 367 return FormatUrlForSecurityDisplayInternal(url, languages, true); |
368 } | 368 } |
369 | 369 |
370 } // namespace url_formatter | 370 } // namespace url_formatter |
OLD | NEW |