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

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

Issue 2963883002: Omnibox UI Experiments: Refactor HTTPS trimming into UrlFormatter. (Closed)
Patch Set: Created 3 years, 5 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/url_formatter.h" 5 #include "components/url_formatter/url_formatter.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 } // namespace 354 } // namespace
355 355
356 const FormatUrlType kFormatUrlOmitNothing = 0; 356 const FormatUrlType kFormatUrlOmitNothing = 0;
357 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; 357 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0;
358 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; 358 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1;
359 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; 359 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2;
360 const FormatUrlType kFormatUrlOmitAll = 360 const FormatUrlType kFormatUrlOmitAll =
361 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP | 361 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP |
362 kFormatUrlOmitTrailingSlashOnBareHostname; 362 kFormatUrlOmitTrailingSlashOnBareHostname;
363 const FormatUrlType kFormatUrlExperimentalElideAfterHost = 1 << 3; 363 const FormatUrlType kFormatUrlExperimentalElideAfterHost = 1 << 3;
364 const FormatUrlType kFormatUrlExperimentalOmitHTTPS = 1 << 4;
364 365
365 base::string16 FormatUrl(const GURL& url, 366 base::string16 FormatUrl(const GURL& url,
366 FormatUrlTypes format_types, 367 FormatUrlTypes format_types,
367 net::UnescapeRule::Type unescape_rules, 368 net::UnescapeRule::Type unescape_rules,
368 url::Parsed* new_parsed, 369 url::Parsed* new_parsed,
369 size_t* prefix_end, 370 size_t* prefix_end,
370 size_t* offset_for_adjustment) { 371 size_t* offset_for_adjustment) {
371 std::vector<size_t> offsets; 372 std::vector<size_t> offsets;
372 if (offset_for_adjustment) 373 if (offset_for_adjustment)
373 offsets.push_back(*offset_for_adjustment); 374 offsets.push_back(*offset_for_adjustment);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 // We handle both valid and invalid URLs (this will give us the spec 430 // We handle both valid and invalid URLs (this will give us the spec
430 // regardless of validity). 431 // regardless of validity).
431 const std::string& spec = url.possibly_invalid_spec(); 432 const std::string& spec = url.possibly_invalid_spec();
432 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); 433 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
433 434
434 // Scheme & separators. These are ASCII. 435 // Scheme & separators. These are ASCII.
435 base::string16 url_string; 436 base::string16 url_string;
436 url_string.insert( 437 url_string.insert(
437 url_string.end(), spec.begin(), 438 url_string.end(), spec.begin(),
438 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true)); 439 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true));
439 const char kHTTP[] = "http://";
440 const char kFTP[] = "ftp.";
441 // url_formatter::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This
442 // means that if we trim "http://" off a URL whose host starts with "ftp." and
443 // the user inputs this into any field subject to fixup (which is basically
444 // all input fields), the meaning would be changed. (In fact, often the
445 // formatted URL is directly pre-filled into an input field.) For this reason
446 // we avoid stripping "http://" in this case.
447 bool omit_http =
448 (format_types & kFormatUrlOmitHTTP) &&
449 base::EqualsASCII(url_string, kHTTP) &&
450 !base::StartsWith(url.host(), kFTP, base::CompareCase::SENSITIVE);
451 new_parsed->scheme = parsed.scheme; 440 new_parsed->scheme = parsed.scheme;
452 441
453 // Username & password. 442 // Username & password.
454 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) { 443 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) {
455 // Remove the username and password fields. We don't want to display those 444 // Remove the username and password fields. We don't want to display those
456 // to the user since they can be used for attacks, 445 // to the user since they can be used for attacks,
457 // e.g. "http://google.com:search@evil.ru/" 446 // e.g. "http://google.com:search@evil.ru/"
458 new_parsed->username.reset(); 447 new_parsed->username.reset();
459 new_parsed->password.reset(); 448 new_parsed->password.reset();
460 // Update the adjustments based on removed username and/or password. 449 // Update the adjustments based on removed username and/or password.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 &url_string, &new_parsed->query, adjustments); 540 &url_string, &new_parsed->query, adjustments);
552 541
553 // Ref. This is valid, unescaped UTF-8, so we can just convert. 542 // Ref. This is valid, unescaped UTF-8, so we can just convert.
554 if (parsed.ref.is_valid()) 543 if (parsed.ref.is_valid())
555 url_string.push_back('#'); 544 url_string.push_back('#');
556 AppendFormattedComponent(spec, parsed.ref, 545 AppendFormattedComponent(spec, parsed.ref,
557 NonHostComponentTransform(net::UnescapeRule::NONE), 546 NonHostComponentTransform(net::UnescapeRule::NONE),
558 &url_string, &new_parsed->ref, adjustments); 547 &url_string, &new_parsed->ref, adjustments);
559 } 548 }
560 549
561 // If we need to strip out http do it after the fact. 550 // url_formatter::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This
562 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), 551 // means that if we trim "http://" off a URL whose host starts with "ftp." and
Peter Kasting 2017/06/28 23:09:59 Nit: "http://" -> the scheme
tommycli 2017/06/29 00:19:43 Done.
563 base::CompareCase::SENSITIVE)) { 552 // the user inputs this into any field subject to fixup (which is basically
564 const size_t kHTTPSize = arraysize(kHTTP) - 1; 553 // all input fields), the meaning would be changed. (In fact, often the
565 url_string = url_string.substr(kHTTPSize); 554 // formatted URL is directly pre-filled into an input field.) For this reason
555 // we avoid stripping schemes in this case.
556 const char kFTP[] = "ftp.";
557 bool strip_scheme =
558 !base::StartsWith(url.host(), kFTP, base::CompareCase::SENSITIVE) &&
559 (((format_types & kFormatUrlOmitHTTP) &&
560 url.SchemeIs(url::kHttpScheme)) ||
561 ((format_types & kFormatUrlExperimentalOmitHTTPS) &&
562 url.SchemeIs(url::kHttpsScheme)));
563
564 // If we need to strip out schemes do it after the fact.
565 if (strip_scheme) {
566 const size_t scheme_size =
567 parsed.scheme.len + strlen(url::kStandardSchemeSeparator);
568 url_string = url_string.substr(scheme_size);
Peter Kasting 2017/06/28 23:09:59 I noticed we need to compute the scheme size above
tommycli 2017/06/29 00:19:43 Done.
566 // Because offsets in the |adjustments| are already calculated with respect 569 // Because offsets in the |adjustments| are already calculated with respect
567 // to the string with the http:// prefix in it, those offsets remain correct 570 // to the string with the http:// prefix in it, those offsets remain correct
568 // after stripping the prefix. The only thing necessary is to add an 571 // after stripping the prefix. The only thing necessary is to add an
569 // adjustment to reflect the stripped prefix. 572 // adjustment to reflect the stripped prefix.
570 adjustments->insert(adjustments->begin(), 573 adjustments->insert(adjustments->begin(),
571 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); 574 base::OffsetAdjuster::Adjustment(0, scheme_size, 0));
572 575
573 if (prefix_end) 576 if (prefix_end)
574 *prefix_end -= kHTTPSize; 577 *prefix_end -= scheme_size;
575 578
576 // Adjust new_parsed. 579 // Adjust new_parsed.
577 DCHECK(new_parsed->scheme.is_valid()); 580 DCHECK(new_parsed->scheme.is_valid());
578 int delta = -(new_parsed->scheme.len + 3); // +3 for ://. 581 int delta = -(new_parsed->scheme.len + 3); // +3 for ://.
579 new_parsed->scheme.reset(); 582 new_parsed->scheme.reset();
580 AdjustAllComponentsButScheme(delta, new_parsed); 583 AdjustAllComponentsButScheme(delta, new_parsed);
581 } 584 }
582 585
583 return url_string; 586 return url_string;
584 } 587 }
(...skipping 20 matching lines...) Expand all
605 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) 608 return base::StartsWith(text, www, base::CompareCase::SENSITIVE)
606 ? text.substr(www.length()) : text; 609 ? text.substr(www.length()) : text;
607 } 610 }
608 611
609 base::string16 StripWWWFromHost(const GURL& url) { 612 base::string16 StripWWWFromHost(const GURL& url) {
610 DCHECK(url.is_valid()); 613 DCHECK(url.is_valid());
611 return StripWWW(base::ASCIIToUTF16(url.host_piece())); 614 return StripWWW(base::ASCIIToUTF16(url.host_piece()));
612 } 615 }
613 616
614 } // namespace url_formatter 617 } // namespace url_formatter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698