Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: chrome/browser/ui/elide_url.cc

Issue 143463006: Remove net dependency from ui/gfx (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add gurl include to elide_url_unittest.cc Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/elide_url.h ('k') | chrome/browser/ui/gtk/status_bubble_gtk.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "chrome/browser/ui/elide_url.h"
6
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"
15
16 using base::UTF8ToUTF16;
17 using gfx::ElideText;
18 using gfx::GetStringWidthF;
19 using gfx::kEllipsisUTF16;
20 using gfx::kForwardSlash;
21
22 namespace {
23
24 // Build a path from the first |num_components| elements in |path_elements|.
25 // Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate.
26 base::string16 BuildPathFromComponents(
27 const base::string16& path_prefix,
28 const std::vector<base::string16>& path_elements,
29 const base::string16& filename,
30 size_t num_components) {
31 // Add the initial elements of the path.
32 base::string16 path = path_prefix;
33
34 // Build path from first |num_components| elements.
35 for (size_t j = 0; j < num_components; ++j)
36 path += path_elements[j] + kForwardSlash;
37
38 // Add |filename|, ellipsis if necessary.
39 if (num_components != (path_elements.size() - 1))
40 path += base::string16(kEllipsisUTF16) + kForwardSlash;
41 path += filename;
42
43 return path;
44 }
45
46 // Takes a prefix (Domain, or Domain+subdomain) and a collection of path
47 // components and elides if possible. Returns a string containing the longest
48 // possible elided path, or an empty string if elision is not possible.
49 base::string16 ElideComponentizedPath(
50 const base::string16& url_path_prefix,
51 const std::vector<base::string16>& url_path_elements,
52 const base::string16& url_filename,
53 const base::string16& url_query,
54 const gfx::FontList& font_list,
55 float available_pixel_width) {
56 const size_t url_path_number_of_elements = url_path_elements.size();
57
58 CHECK(url_path_number_of_elements);
59 for (size_t i = url_path_number_of_elements - 1; i > 0; --i) {
60 base::string16 elided_path = BuildPathFromComponents(url_path_prefix,
61 url_path_elements, url_filename, i);
62 if (available_pixel_width >= GetStringWidthF(elided_path, font_list))
63 return ElideText(elided_path + url_query, font_list,
64 available_pixel_width, gfx::ELIDE_AT_END);
65 }
66
67 return base::string16();
68 }
69
70 } // namespace
71
72 // TODO(pkasting): http://crbug.com/77883 This whole function gets
73 // kerning/ligatures/etc. issues potentially wrong by assuming that the width of
74 // a rendered string is always the sum of the widths of its substrings. Also I
75 // suspect it could be made simpler.
76 base::string16 ElideUrl(const GURL& url,
77 const gfx::FontList& font_list,
78 float available_pixel_width,
79 const std::string& languages) {
80 // Get a formatted string and corresponding parsing of the url.
81 url_parse::Parsed parsed;
82 const base::string16 url_string =
83 net::FormatUrl(url, languages, net::kFormatUrlOmitAll,
84 net::UnescapeRule::SPACES, &parsed, NULL, NULL);
85 if (available_pixel_width <= 0)
86 return url_string;
87
88 // If non-standard, return plain eliding.
89 if (!url.IsStandard())
90 return ElideText(url_string, font_list, available_pixel_width,
91 gfx::ELIDE_AT_END);
92
93 // Now start eliding url_string to fit within available pixel width.
94 // Fist pass - check to see whether entire url_string fits.
95 const float pixel_width_url_string = GetStringWidthF(url_string, font_list);
96 if (available_pixel_width >= pixel_width_url_string)
97 return url_string;
98
99 // Get the path substring, including query and reference.
100 const size_t path_start_index = parsed.path.begin;
101 const size_t path_len = parsed.path.len;
102 base::string16 url_path_query_etc = url_string.substr(path_start_index);
103 base::string16 url_path = url_string.substr(path_start_index, path_len);
104
105 // Return general elided text if url minus the query fits.
106 const base::string16 url_minus_query =
107 url_string.substr(0, path_start_index + path_len);
108 if (available_pixel_width >= GetStringWidthF(url_minus_query, font_list))
109 return ElideText(url_string, font_list, available_pixel_width,
110 gfx::ELIDE_AT_END);
111
112 // Get Host.
113 base::string16 url_host = UTF8ToUTF16(url.host());
114
115 // Get domain and registry information from the URL.
116 base::string16 url_domain = UTF8ToUTF16(
117 net::registry_controlled_domains::GetDomainAndRegistry(
118 url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES));
119 if (url_domain.empty())
120 url_domain = url_host;
121
122 // Add port if required.
123 if (!url.port().empty()) {
124 url_host += UTF8ToUTF16(":" + url.port());
125 url_domain += UTF8ToUTF16(":" + url.port());
126 }
127
128 // Get sub domain.
129 base::string16 url_subdomain;
130 const size_t domain_start_index = url_host.find(url_domain);
131 if (domain_start_index != base::string16::npos)
132 url_subdomain = url_host.substr(0, domain_start_index);
133 const base::string16 kWwwPrefix = UTF8ToUTF16("www.");
134 if ((url_subdomain == kWwwPrefix || url_subdomain.empty() ||
135 url.SchemeIsFile())) {
136 url_subdomain.clear();
137 }
138
139 // If this is a file type, the path is now defined as everything after ":".
140 // For example, "C:/aa/aa/bb", the path is "/aa/bb/cc". Interesting, the
141 // domain is now C: - this is a nice hack for eliding to work pleasantly.
142 if (url.SchemeIsFile()) {
143 // Split the path string using ":"
144 std::vector<base::string16> file_path_split;
145 base::SplitString(url_path, ':', &file_path_split);
146 if (file_path_split.size() > 1) { // File is of type "file:///C:/.."
147 url_host.clear();
148 url_domain.clear();
149 url_subdomain.clear();
150
151 const base::string16 kColon = UTF8ToUTF16(":");
152 url_host = url_domain = file_path_split.at(0).substr(1) + kColon;
153 url_path_query_etc = url_path = file_path_split.at(1);
154 }
155 }
156
157 // Second Pass - remove scheme - the rest fits.
158 const float pixel_width_url_host = GetStringWidthF(url_host, font_list);
159 const float pixel_width_url_path = GetStringWidthF(url_path_query_etc,
160 font_list);
161 if (available_pixel_width >=
162 pixel_width_url_host + pixel_width_url_path)
163 return url_host + url_path_query_etc;
164
165 // Third Pass: Subdomain, domain and entire path fits.
166 const float pixel_width_url_domain = GetStringWidthF(url_domain, font_list);
167 const float pixel_width_url_subdomain =
168 GetStringWidthF(url_subdomain, font_list);
169 if (available_pixel_width >=
170 pixel_width_url_subdomain + pixel_width_url_domain +
171 pixel_width_url_path)
172 return url_subdomain + url_domain + url_path_query_etc;
173
174 // Query element.
175 base::string16 url_query;
176 const float kPixelWidthDotsTrailer = GetStringWidthF(
177 base::string16(kEllipsisUTF16), font_list);
178 if (parsed.query.is_nonempty()) {
179 url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin);
180 if (available_pixel_width >=
181 (pixel_width_url_subdomain + pixel_width_url_domain +
182 pixel_width_url_path - GetStringWidthF(url_query, font_list))) {
183 return ElideText(url_subdomain + url_domain + url_path_query_etc,
184 font_list, available_pixel_width, gfx::ELIDE_AT_END);
185 }
186 }
187
188 // Parse url_path using '/'.
189 std::vector<base::string16> url_path_elements;
190 base::SplitString(url_path, kForwardSlash, &url_path_elements);
191
192 // Get filename - note that for a path ending with /
193 // such as www.google.com/intl/ads/, the file name is ads/.
194 size_t url_path_number_of_elements = url_path_elements.size();
195 DCHECK(url_path_number_of_elements != 0);
196 base::string16 url_filename;
197 if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) {
198 url_filename = *(url_path_elements.end() - 1);
199 } else if (url_path_number_of_elements > 1) { // Path ends with a '/'.
200 url_filename = url_path_elements.at(url_path_number_of_elements - 2) +
201 kForwardSlash;
202 url_path_number_of_elements--;
203 }
204 DCHECK(url_path_number_of_elements != 0);
205
206 const size_t kMaxNumberOfUrlPathElementsAllowed = 1024;
207 if (url_path_number_of_elements <= 1 ||
208 url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) {
209 // No path to elide, or too long of a path (could overflow in loop below)
210 // Just elide this as a text string.
211 return ElideText(url_subdomain + url_domain + url_path_query_etc, font_list,
212 available_pixel_width, gfx::ELIDE_AT_END);
213 }
214
215 // Start eliding the path and replacing elements by ".../".
216 const base::string16 kEllipsisAndSlash =
217 base::string16(kEllipsisUTF16) + kForwardSlash;
218 const float pixel_width_ellipsis_slash =
219 GetStringWidthF(kEllipsisAndSlash, font_list);
220
221 // Check with both subdomain and domain.
222 base::string16 elided_path =
223 ElideComponentizedPath(url_subdomain + url_domain, url_path_elements,
224 url_filename, url_query, font_list,
225 available_pixel_width);
226 if (!elided_path.empty())
227 return elided_path;
228
229 // Check with only domain.
230 // If a subdomain is present, add an ellipsis before domain.
231 // This is added only if the subdomain pixel width is larger than
232 // the pixel width of kEllipsis. Otherwise, subdomain remains,
233 // which means that this case has been resolved earlier.
234 base::string16 url_elided_domain = url_subdomain + url_domain;
235 if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) {
236 if (!url_subdomain.empty())
237 url_elided_domain = kEllipsisAndSlash[0] + url_domain;
238 else
239 url_elided_domain = url_domain;
240
241 elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements,
242 url_filename, url_query, font_list,
243 available_pixel_width);
244
245 if (!elided_path.empty())
246 return elided_path;
247 }
248
249 // Return elided domain/.../filename anyway.
250 base::string16 final_elided_url_string(url_elided_domain);
251 const float url_elided_domain_width = GetStringWidthF(url_elided_domain,
252 font_list);
253
254 // A hack to prevent trailing ".../...".
255 if ((available_pixel_width - url_elided_domain_width) >
256 pixel_width_ellipsis_slash + kPixelWidthDotsTrailer +
257 GetStringWidthF(base::ASCIIToUTF16("UV"), font_list)) {
258 final_elided_url_string += BuildPathFromComponents(base::string16(),
259 url_path_elements, url_filename, 1);
260 } else {
261 final_elided_url_string += url_path;
262 }
263
264 return ElideText(final_elided_url_string, font_list, available_pixel_width,
265 gfx::ELIDE_AT_END);
266 }
267
OLDNEW
« no previous file with comments | « chrome/browser/ui/elide_url.h ('k') | chrome/browser/ui/gtk/status_bubble_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698