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

Side by Side Diff: components/url_formatter/elide_url.cc

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

Powered by Google App Engine
This is Rietveld 408576698