Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_offset_string_conversions.h" | 16 #include "base/strings/utf_offset_string_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/threading/thread_local_storage.h" | 18 #include "base/threading/thread_local_storage.h" |
| 19 #include "components/url_formatter/idn_spoof_checker.h" | 19 #include "components/url_formatter/idn_spoof_checker.h" |
| 20 #include "third_party/icu/source/common/unicode/uidna.h" | 20 #include "third_party/icu/source/common/unicode/uidna.h" |
| 21 #include "third_party/icu/source/common/unicode/utypes.h" | 21 #include "third_party/icu/source/common/unicode/utypes.h" |
| 22 #include "ui/gfx/text_elider.h" | |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 #include "url/third_party/mozilla/url_parse.h" | 24 #include "url/third_party/mozilla/url_parse.h" |
| 24 | 25 |
| 25 namespace url_formatter { | 26 namespace url_formatter { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 base::string16 IDNToUnicodeWithAdjustments( | 30 base::string16 IDNToUnicodeWithAdjustments( |
| 30 base::StringPiece host, | 31 base::StringPiece host, |
| 31 base::OffsetAdjuster::Adjustments* adjustments); | 32 base::OffsetAdjuster::Adjustments* adjustments); |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 | 354 |
| 354 } // namespace | 355 } // namespace |
| 355 | 356 |
| 356 const FormatUrlType kFormatUrlOmitNothing = 0; | 357 const FormatUrlType kFormatUrlOmitNothing = 0; |
| 357 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; | 358 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; |
| 358 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; | 359 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; |
| 359 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; | 360 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; |
| 360 const FormatUrlType kFormatUrlOmitAll = | 361 const FormatUrlType kFormatUrlOmitAll = |
| 361 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP | | 362 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP | |
| 362 kFormatUrlOmitTrailingSlashOnBareHostname; | 363 kFormatUrlOmitTrailingSlashOnBareHostname; |
| 364 const FormatUrlType kFormatUrlExperimentalEllipsizePath = 1 << 3; | |
| 363 | 365 |
| 364 base::string16 FormatUrl(const GURL& url, | 366 base::string16 FormatUrl(const GURL& url, |
| 365 FormatUrlTypes format_types, | 367 FormatUrlTypes format_types, |
| 366 net::UnescapeRule::Type unescape_rules, | 368 net::UnescapeRule::Type unescape_rules, |
| 367 url::Parsed* new_parsed, | 369 url::Parsed* new_parsed, |
| 368 size_t* prefix_end, | 370 size_t* prefix_end, |
| 369 size_t* offset_for_adjustment) { | 371 size_t* offset_for_adjustment) { |
| 370 std::vector<size_t> offsets; | 372 std::vector<size_t> offsets; |
| 371 if (offset_for_adjustment) | 373 if (offset_for_adjustment) |
| 372 offsets.push_back(*offset_for_adjustment); | 374 offsets.push_back(*offset_for_adjustment); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 url_string.push_back(':'); | 501 url_string.push_back(':'); |
| 500 new_parsed->port.begin = url_string.length(); | 502 new_parsed->port.begin = url_string.length(); |
| 501 url_string.insert(url_string.end(), spec.begin() + parsed.port.begin, | 503 url_string.insert(url_string.end(), spec.begin() + parsed.port.begin, |
| 502 spec.begin() + parsed.port.end()); | 504 spec.begin() + parsed.port.end()); |
| 503 new_parsed->port.len = url_string.length() - new_parsed->port.begin; | 505 new_parsed->port.len = url_string.length() - new_parsed->port.begin; |
| 504 } else { | 506 } else { |
| 505 new_parsed->port.reset(); | 507 new_parsed->port.reset(); |
| 506 } | 508 } |
| 507 | 509 |
| 508 // Path & query. Both get the same general unescape & convert treatment. | 510 // Path & query. Both get the same general unescape & convert treatment. |
| 509 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) || | 511 if ((format_types & kFormatUrlOmitTrailingSlashOnBareHostname) && |
| 510 !CanStripTrailingSlash(url)) { | 512 CanStripTrailingSlash(url)) { |
| 511 AppendFormattedComponent(spec, parsed.path, | 513 // Case when it's just a single trailing slash we can strip. |
|
Peter Kasting
2017/06/28 20:54:34
Nit: "Omit the path, which is a single trailing sl
tommycli
2017/06/28 21:21:39
Done.
| |
| 512 NonHostComponentTransform(unescape_rules), | |
| 513 &url_string, &new_parsed->path, adjustments); | |
| 514 } else { | |
| 515 if (parsed.path.len > 0) { | 514 if (parsed.path.len > 0) { |
| 516 adjustments->push_back(base::OffsetAdjuster::Adjustment( | 515 adjustments->push_back(base::OffsetAdjuster::Adjustment( |
| 517 parsed.path.begin, parsed.path.len, 0)); | 516 parsed.path.begin, parsed.path.len, 0)); |
| 518 } | 517 } |
| 518 } else if ((format_types & kFormatUrlExperimentalEllipsizePath) && | |
| 519 url.IsStandard() && !url.SchemeIsFile() && | |
| 520 !url.SchemeIsFileSystem()) { | |
| 521 // Case when we replace the path with an ellipsis. | |
|
Peter Kasting
2017/06/28 20:54:34
Nit: "Replace everything after the host with a sla
tommycli
2017/06/28 21:21:39
Done.
| |
| 522 url_string.push_back('/'); | |
| 523 url_string.append(gfx::kEllipsisUTF16); | |
| 524 | |
| 525 // Start at begin + 1, since we are preserving the first forward slash. | |
| 526 // Subtract one from the length to account for the first forward slash. | |
| 527 // New length is one to account for the new ellipsis character. | |
|
Peter Kasting
2017/06/28 20:54:34
Nit: These comments actually refer, respectively,
tommycli
2017/06/28 21:21:39
Done. This makes it make much more sense!
| |
| 528 size_t old_length = parsed.path.len - 1; | |
| 529 if (parsed.query.is_valid()) | |
| 530 old_length += parsed.query.len + 1; | |
| 531 if (parsed.ref.is_valid()) | |
| 532 old_length += parsed.ref.len + 1; | |
| 533 adjustments->push_back( | |
| 534 base::OffsetAdjuster::Adjustment(parsed.path.begin + 1, old_length, 1)); | |
| 535 } else { | |
| 536 // Default case. | |
|
Peter Kasting
2017/06/28 20:54:34
Nit: "Append formatted path, query, and ref."
tommycli
2017/06/28 21:21:39
Done.
| |
| 537 AppendFormattedComponent(spec, parsed.path, | |
| 538 NonHostComponentTransform(unescape_rules), | |
| 539 &url_string, &new_parsed->path, adjustments); | |
| 540 | |
| 541 if (parsed.query.is_valid()) | |
| 542 url_string.push_back('?'); | |
| 543 AppendFormattedComponent(spec, parsed.query, | |
| 544 NonHostComponentTransform(unescape_rules), | |
| 545 &url_string, &new_parsed->query, adjustments); | |
| 546 | |
| 547 // Ref. This is valid, unescaped UTF-8, so we can just convert. | |
| 548 if (parsed.ref.is_valid()) | |
| 549 url_string.push_back('#'); | |
| 550 AppendFormattedComponent(spec, parsed.ref, | |
| 551 NonHostComponentTransform(net::UnescapeRule::NONE), | |
| 552 &url_string, &new_parsed->ref, adjustments); | |
| 519 } | 553 } |
| 520 if (parsed.query.is_valid()) | |
| 521 url_string.push_back('?'); | |
| 522 AppendFormattedComponent(spec, parsed.query, | |
| 523 NonHostComponentTransform(unescape_rules), | |
| 524 &url_string, &new_parsed->query, adjustments); | |
| 525 | |
| 526 // Ref. This is valid, unescaped UTF-8, so we can just convert. | |
| 527 if (parsed.ref.is_valid()) | |
| 528 url_string.push_back('#'); | |
| 529 AppendFormattedComponent(spec, parsed.ref, | |
| 530 NonHostComponentTransform(net::UnescapeRule::NONE), | |
| 531 &url_string, &new_parsed->ref, adjustments); | |
| 532 | 554 |
| 533 // If we need to strip out http do it after the fact. | 555 // If we need to strip out http do it after the fact. |
| 534 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), | 556 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP), |
| 535 base::CompareCase::SENSITIVE)) { | 557 base::CompareCase::SENSITIVE)) { |
| 536 const size_t kHTTPSize = arraysize(kHTTP) - 1; | 558 const size_t kHTTPSize = arraysize(kHTTP) - 1; |
| 537 url_string = url_string.substr(kHTTPSize); | 559 url_string = url_string.substr(kHTTPSize); |
| 538 // Because offsets in the |adjustments| are already calculated with respect | 560 // Because offsets in the |adjustments| are already calculated with respect |
| 539 // to the string with the http:// prefix in it, those offsets remain correct | 561 // to the string with the http:// prefix in it, those offsets remain correct |
| 540 // after stripping the prefix. The only thing necessary is to add an | 562 // after stripping the prefix. The only thing necessary is to add an |
| 541 // adjustment to reflect the stripped prefix. | 563 // adjustment to reflect the stripped prefix. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) | 599 return base::StartsWith(text, www, base::CompareCase::SENSITIVE) |
| 578 ? text.substr(www.length()) : text; | 600 ? text.substr(www.length()) : text; |
| 579 } | 601 } |
| 580 | 602 |
| 581 base::string16 StripWWWFromHost(const GURL& url) { | 603 base::string16 StripWWWFromHost(const GURL& url) { |
| 582 DCHECK(url.is_valid()); | 604 DCHECK(url.is_valid()); |
| 583 return StripWWW(base::ASCIIToUTF16(url.host_piece())); | 605 return StripWWW(base::ASCIIToUTF16(url.host_piece())); |
| 584 } | 606 } |
| 585 | 607 |
| 586 } // namespace url_formatter | 608 } // namespace url_formatter |
| OLD | NEW |