NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / elide_url.cc
blobba48aae645d4a4ccee27d4f5c1ef9cc049b6aab4
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.
5 #include "chrome/browser/ui/elide_url.h"
7 #include "base/strings/string_split.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "net/base/escape.h"
10 #include "net/base/net_util.h"
11 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
12 #include "ui/gfx/text_elider.h"
13 #include "ui/gfx/text_utils.h"
14 #include "url/gurl.h"
16 using base::UTF8ToUTF16;
17 using gfx::ElideText;
18 using gfx::GetStringWidthF;
19 using gfx::kEllipsisUTF16;
20 using gfx::kForwardSlash;
22 namespace {
24 const base::char16 kDot = '.';
26 // Build a path from the first |num_components| elements in |path_elements|.
27 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate.
28 base::string16 BuildPathFromComponents(
29 const base::string16& path_prefix,
30 const std::vector<base::string16>& path_elements,
31 const base::string16& filename,
32 size_t num_components) {
33 // Add the initial elements of the path.
34 base::string16 path = path_prefix;
36 // Build path from first |num_components| elements.
37 for (size_t j = 0; j < num_components; ++j)
38 path += path_elements[j] + kForwardSlash;
40 // Add |filename|, ellipsis if necessary.
41 if (num_components != (path_elements.size() - 1))
42 path += base::string16(kEllipsisUTF16) + kForwardSlash;
43 path += filename;
45 return path;
48 // Takes a prefix (Domain, or Domain+subdomain) and a collection of path
49 // components and elides if possible. Returns a string containing the longest
50 // possible elided path, or an empty string if elision is not possible.
51 base::string16 ElideComponentizedPath(
52 const base::string16& url_path_prefix,
53 const std::vector<base::string16>& url_path_elements,
54 const base::string16& url_filename,
55 const base::string16& url_query,
56 const gfx::FontList& font_list,
57 float available_pixel_width) {
58 const size_t url_path_number_of_elements = url_path_elements.size();
60 CHECK(url_path_number_of_elements);
61 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) {
62 base::string16 elided_path = BuildPathFromComponents(url_path_prefix,
63 url_path_elements, url_filename, i);
64 if (available_pixel_width >= GetStringWidthF(elided_path, font_list))
65 return ElideText(elided_path + url_query, font_list,
66 available_pixel_width, gfx::ELIDE_AT_END);
69 return base::string16();
72 // Splits the hostname in the |url| into sub-strings for the full hostname,
73 // the domain (TLD+1), and the subdomain (everything leading the domain).
74 void SplitHost(const GURL& url,
75 base::string16* url_host,
76 base::string16* url_domain,
77 base::string16* url_subdomain) {
78 // Get Host.
79 *url_host = UTF8ToUTF16(url.host());
81 // Get domain and registry information from the URL.
82 *url_domain = UTF8ToUTF16(
83 net::registry_controlled_domains::GetDomainAndRegistry(
84 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES));
85 if (url_domain->empty())
86 *url_domain = *url_host;
88 // Add port if required.
89 if (!url.port().empty()) {
90 *url_host += UTF8ToUTF16(":" + url.port());
91 *url_domain += UTF8ToUTF16(":" + url.port());
94 // Get sub domain.
95 const size_t domain_start_index = url_host->find(*url_domain);
96 base::string16 kWwwPrefix = UTF8ToUTF16("www.");
97 if (domain_start_index != base::string16::npos)
98 *url_subdomain = url_host->substr(0, domain_start_index);
99 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() ||
100 url.SchemeIsFile())) {
101 url_subdomain->clear();
105 } // namespace
107 // TODO(pkasting): http://crbug.com/77883 This whole function gets
108 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of
109 // a rendered string is always the sum of the widths of its substrings. Also I
110 // suspect it could be made simpler.
111 base::string16 ElideUrl(const GURL& url,
112 const gfx::FontList& font_list,
113 float available_pixel_width,
114 const std::string& languages) {
115 // Get a formatted string and corresponding parsing of the url.
116 url_parse::Parsed parsed;
117 const base::string16 url_string =
118 net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
119 net::UnescapeRule::SPACES, &parsed, NULL, NULL);
120 if (available_pixel_width <= 0)
121 return url_string;
123 // If non-standard, return plain eliding.
124 if (!url.IsStandard())
125 return ElideText(url_string, font_list, available_pixel_width,
126 gfx::ELIDE_AT_END);
128 // Now start eliding url_string to fit within available pixel width.
129 // Fist pass - check to see whether entire url_string fits.
130 const float pixel_width_url_string = GetStringWidthF(url_string, font_list);
131 if (available_pixel_width >= pixel_width_url_string)
132 return url_string;
134 // Get the path substring, including query and reference.
135 const size_t path_start_index = parsed.path.begin;
136 const size_t path_len = parsed.path.len;
137 base::string16 url_path_query_etc = url_string.substr(path_start_index);
138 base::string16 url_path = url_string.substr(path_start_index, path_len);
140 // Return general elided text if url minus the query fits.
141 const base::string16 url_minus_query =
142 url_string.substr(0, path_start_index + path_len);
143 if (available_pixel_width >= GetStringWidthF(url_minus_query, font_list))
144 return ElideText(url_string, font_list, available_pixel_width,
145 gfx::ELIDE_AT_END);
147 base::string16 url_host;
148 base::string16 url_domain;
149 base::string16 url_subdomain;
150 SplitHost(url, &url_host, &url_domain, &url_subdomain);
152 // If this is a file type, the path is now defined as everything after ":".
153 // For example, "C:/aa/aa/bb", the path is "/aa/bb/cc". Interesting, the
154 // domain is now C: - this is a nice hack for eliding to work pleasantly.
155 if (url.SchemeIsFile()) {
156 // Split the path string using ":"
157 std::vector<base::string16> file_path_split;
158 base::SplitString(url_path, ':', &file_path_split);
159 if (file_path_split.size() > 1) { // File is of type "file:///C:/.."
160 url_host.clear();
161 url_domain.clear();
162 url_subdomain.clear();
164 const base::string16 kColon = UTF8ToUTF16(":");
165 url_host = url_domain = file_path_split.at(0).substr(1) + kColon;
166 url_path_query_etc = url_path = file_path_split.at(1);
170 // Second Pass - remove scheme - the rest fits.
171 const float pixel_width_url_host = GetStringWidthF(url_host, font_list);
172 const float pixel_width_url_path = GetStringWidthF(url_path_query_etc,
173 font_list);
174 if (available_pixel_width >=
175 pixel_width_url_host + pixel_width_url_path)
176 return url_host + url_path_query_etc;
178 // Third Pass: Subdomain, domain and entire path fits.
179 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list);
180 const float pixel_width_url_subdomain =
181 GetStringWidthF(url_subdomain, font_list);
182 if (available_pixel_width >=
183 pixel_width_url_subdomain + pixel_width_url_domain +
184 pixel_width_url_path)
185 return url_subdomain + url_domain + url_path_query_etc;
187 // Query element.
188 base::string16 url_query;
189 const float kPixelWidthDotsTrailer = GetStringWidthF(
190 base::string16(kEllipsisUTF16), font_list);
191 if (parsed.query.is_nonempty()) {
192 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin);
193 if (available_pixel_width >=
194 (pixel_width_url_subdomain + pixel_width_url_domain +
195 pixel_width_url_path - GetStringWidthF(url_query, font_list))) {
196 return ElideText(url_subdomain + url_domain + url_path_query_etc,
197 font_list, available_pixel_width, gfx::ELIDE_AT_END);
201 // Parse url_path using '/'.
202 std::vector<base::string16> url_path_elements;
203 base::SplitString(url_path, kForwardSlash, &url_path_elements);
205 // Get filename - note that for a path ending with /
206 // such as www.google.com/intl/ads/, the file name is ads/.
207 size_t url_path_number_of_elements = url_path_elements.size();
208 DCHECK(url_path_number_of_elements != 0);
209 base::string16 url_filename;
210 if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) {
211 url_filename = *(url_path_elements.end() - 1);
212 } else if (url_path_number_of_elements > 1) { // Path ends with a '/'.
213 url_filename = url_path_elements.at(url_path_number_of_elements - 2) +
214 kForwardSlash;
215 url_path_number_of_elements--;
217 DCHECK(url_path_number_of_elements != 0);
219 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024;
220 if (url_path_number_of_elements <= 1 ||
221 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) {
222 // No path to elide, or too long of a path (could overflow in loop below)
223 // Just elide this as a text string.
224 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list,
225 available_pixel_width, gfx::ELIDE_AT_END);
228 // Start eliding the path and replacing elements by ".../".
229 const base::string16 kEllipsisAndSlash =
230 base::string16(kEllipsisUTF16) + kForwardSlash;
231 const float pixel_width_ellipsis_slash =
232 GetStringWidthF(kEllipsisAndSlash, font_list);
234 // Check with both subdomain and domain.
235 base::string16 elided_path =
236 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements,
237 url_filename, url_query, font_list,
238 available_pixel_width);
239 if (!elided_path.empty())
240 return elided_path;
242 // Check with only domain.
243 // If a subdomain is present, add an ellipsis before domain.
244 // This is added only if the subdomain pixel width is larger than
245 // the pixel width of kEllipsis. Otherwise, subdomain remains,
246 // which means that this case has been resolved earlier.
247 base::string16 url_elided_domain = url_subdomain + url_domain;
248 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) {
249 if (!url_subdomain.empty())
250 url_elided_domain = kEllipsisAndSlash[0] + url_domain;
251 else
252 url_elided_domain = url_domain;
254 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements,
255 url_filename, url_query, font_list,
256 available_pixel_width);
258 if (!elided_path.empty())
259 return elided_path;
262 // Return elided domain/.../filename anyway.
263 base::string16 final_elided_url_string(url_elided_domain);
264 const float url_elided_domain_width = GetStringWidthF(url_elided_domain,
265 font_list);
267 // A hack to prevent trailing ".../...".
268 if ((available_pixel_width - url_elided_domain_width) >
269 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer +
270 GetStringWidthF(base::ASCIIToUTF16("UV"), font_list)) {
271 final_elided_url_string += BuildPathFromComponents(base::string16(),
272 url_path_elements, url_filename, 1);
273 } else {
274 final_elided_url_string += url_path;
277 return ElideText(final_elided_url_string, font_list, available_pixel_width,
278 gfx::ELIDE_AT_END);
281 base::string16 ElideHost(const GURL& url,
282 const gfx::FontList& font_list,
283 float available_pixel_width) {
284 base::string16 url_host;
285 base::string16 url_domain;
286 base::string16 url_subdomain;
287 SplitHost(url, &url_host, &url_domain, &url_subdomain);
289 const float pixel_width_url_host = GetStringWidthF(url_host, font_list);
290 if (available_pixel_width >= pixel_width_url_host)
291 return url_host;
293 if (url_subdomain.empty())
294 return url_domain;
296 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list);
297 float subdomain_width = available_pixel_width - pixel_width_url_domain;
298 if (subdomain_width <= 0)
299 return base::string16(kEllipsisUTF16) + kDot + url_domain;
301 base::string16 elided_subdomain = ElideText(
302 url_subdomain, font_list, subdomain_width, gfx::ELIDE_AT_BEGINNING);
303 return elided_subdomain + url_domain;