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

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

Issue 1959933002: Add FormatOriginForSecurityDisplay which takes a const url::Origin& origin (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added test code Created 4 years, 7 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/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_number_conversions.h"
10 #include "base/strings/string_split.h" 11 #include "base/strings/string_split.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h" 13 #include "build/build_config.h"
13 #include "components/url_formatter/url_formatter.h" 14 #include "components/url_formatter/url_formatter.h"
14 #include "net/base/escape.h" 15 #include "net/base/escape.h"
15 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 16 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
18 #include "url/origin.h"
17 #include "url/url_constants.h" 19 #include "url/url_constants.h"
18 20
19 #if !defined(OS_ANDROID) 21 #if !defined(OS_ANDROID)
20 #include "ui/gfx/text_elider.h" // nogncheck 22 #include "ui/gfx/text_elider.h" // nogncheck
21 #include "ui/gfx/text_utils.h" // nogncheck 23 #include "ui/gfx/text_utils.h" // nogncheck
22 #endif 24 #endif
23 25
24 namespace { 26 namespace {
25 27
26 #if !defined(OS_ANDROID) 28 #if !defined(OS_ANDROID)
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 base::string16 kWwwPrefix = base::UTF8ToUTF16("www."); 103 base::string16 kWwwPrefix = base::UTF8ToUTF16("www.");
102 if (domain_start_index != base::string16::npos) 104 if (domain_start_index != base::string16::npos)
103 *url_subdomain = url_host->substr(0, domain_start_index); 105 *url_subdomain = url_host->substr(0, domain_start_index);
104 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() || 106 if ((*url_subdomain == kWwwPrefix || url_subdomain->empty() ||
105 url.SchemeIsFile())) { 107 url.SchemeIsFile())) {
106 url_subdomain->clear(); 108 url_subdomain->clear();
107 } 109 }
108 } 110 }
109 #endif // !defined(OS_ANDROID) 111 #endif // !defined(OS_ANDROID)
110 112
111 bool ShouldShowScheme(const GURL& url, 113 bool ShouldShowScheme(const std::string& scheme,
112 const url_formatter::SchemeDisplay scheme_display) { 114 const url_formatter::SchemeDisplay scheme_display) {
113 switch (scheme_display) { 115 switch (scheme_display) {
114 case url_formatter::SchemeDisplay::SHOW: 116 case url_formatter::SchemeDisplay::SHOW:
115 return true; 117 return true;
116 118
117 case url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS: 119 case url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS:
118 return !url.SchemeIs(url::kHttpsScheme) && 120 return scheme != url::kHttpsScheme && scheme != url::kHttpScheme;
119 !url.SchemeIs(url::kHttpScheme);
120 121
121 case url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC: 122 case url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC:
122 return !url.SchemeIsCryptographic(); 123 return scheme != url::kHttpsScheme && scheme != url::kWssScheme;
123 } 124 }
124 125
125 return true; 126 return true;
126 } 127 }
127 128
128 } // namespace 129 } // namespace
129 130
130 namespace url_formatter { 131 namespace url_formatter {
131 132
132 #if !defined(OS_ANDROID) 133 #if !defined(OS_ANDROID)
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 if (inner_url->SchemeIsFile()) { 349 if (inner_url->SchemeIsFile()) {
349 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + 350 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon +
350 FormatUrlForSecurityDisplay(*inner_url) + 351 FormatUrlForSecurityDisplay(*inner_url) +
351 base::UTF8ToUTF16(url.path()); 352 base::UTF8ToUTF16(url.path());
352 } 353 }
353 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon + 354 return base::ASCIIToUTF16(url::kFileSystemScheme) + colon +
354 FormatUrlForSecurityDisplay(*inner_url); 355 FormatUrlForSecurityDisplay(*inner_url);
355 } 356 }
356 357
357 const GURL origin = url.GetOrigin(); 358 const GURL origin = url.GetOrigin();
358 const std::string& scheme = origin.scheme(); 359 const std::string& scheme = origin.scheme();
brettw 2016/05/12 16:47:24 While we're changing this, can you change these to
juncai 2016/05/12 17:29:06 Done.
359 const std::string& host = origin.host(); 360 const std::string& host = origin.host();
360 361
361 base::string16 result; 362 base::string16 result;
362 if (ShouldShowScheme(url, scheme_display)) 363 if (ShouldShowScheme(scheme, scheme_display))
363 result = base::UTF8ToUTF16(scheme) + scheme_separator; 364 result = base::UTF8ToUTF16(scheme) + scheme_separator;
364 result += base::UTF8ToUTF16(host); 365 result += base::UTF8ToUTF16(host);
365 366
366 const int port = origin.IntPort(); 367 const int port = origin.IntPort();
367 const int default_port = url::DefaultPortForScheme( 368 const int default_port = url::DefaultPortForScheme(
368 scheme.c_str(), static_cast<int>(scheme.length())); 369 scheme.c_str(), static_cast<int>(scheme.length()));
369 if (port != url::PORT_UNSPECIFIED && port != default_port) 370 if (port != url::PORT_UNSPECIFIED && port != default_port)
370 result += colon + base::UTF8ToUTF16(origin.port()); 371 result += colon + base::UTF8ToUTF16(origin.port());
brettw 2016/05/12 16:47:24 Can you also change this to port_piece()?
juncai 2016/05/12 17:29:06 Done.
371 372
372 return result; 373 return result;
373 } 374 }
374 375
376 base::string16 FormatOriginForSecurityDisplay(
377 const url::Origin& origin,
378 const SchemeDisplay scheme_display) {
379 const std::string& scheme = origin.scheme();
brettw 2016/05/12 16:47:24 Same, StringPiece, scheme_piece(), and host_piece(
juncai 2016/05/12 17:29:06 url::Origin doesn't have scheme_piece(), and host_
brettw 2016/05/12 23:22:22 Oh I see, these are const refs. This part is fine
380 const std::string& host = origin.host();
381 if (scheme.empty() && host.empty())
382 return base::string16();
383
384 const base::string16 colon(base::ASCIIToUTF16(":"));
385 const base::string16 scheme_separator(
386 base::ASCIIToUTF16(url::kStandardSchemeSeparator));
387
388 base::string16 result;
389 if (ShouldShowScheme(scheme, scheme_display))
390 result = base::UTF8ToUTF16(scheme) + scheme_separator;
391 result += base::UTF8ToUTF16(host);
392
393 int port = static_cast<int>(origin.port());
394 const int default_port = url::DefaultPortForScheme(
395 scheme.c_str(), static_cast<int>(scheme.length()));
396 if (port != 0 && port != default_port)
397 result += colon + base::UintToString16(origin.port());
398
399 return result;
400 }
401
375 } // namespace url_formatter 402 } // 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