Chromium Code Reviews| Index: extensions/common/url_pattern.cc |
| diff --git a/extensions/common/url_pattern.cc b/extensions/common/url_pattern.cc |
| index 91ba65bec02583f4666ea4a68e76c96fe3ef66e6..c71a473825728bad5ad29b9d3ae02c55779c2257 100644 |
| --- a/extensions/common/url_pattern.cc |
| +++ b/extensions/common/url_pattern.cc |
| @@ -18,6 +18,7 @@ |
| #include "content/public/common/url_constants.h" |
| #include "extensions/common/constants.h" |
| #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| +#include "net/base/url_util.h" |
| #include "url/gurl.h" |
| #include "url/url_util.h" |
| @@ -113,6 +114,10 @@ std::string StripTrailingWildcard(const std::string& path) { |
| return wildcard_index == path_last ? path.substr(0, path_last) : path; |
| } |
| +bool EndsWithDot(base::StringPiece str) { |
| + return base::EndsWith(str, ".", base::CompareCase::SENSITIVE); |
| +} |
| + |
| } // namespace |
| // static |
| @@ -404,8 +409,10 @@ bool URLPattern::MatchesHost(const std::string& host) const { |
| } |
| bool URLPattern::MatchesHost(const GURL& test) const { |
| + const std::string test_host(CanonicalizeHostForMatching(test)); |
|
Devlin
2016/10/28 19:17:27
This code is called under some pretty performance-
|
| + |
| // If the hosts are exactly equal, we have a match. |
| - if (test.host() == host_) |
| + if (test_host == host_) |
| return true; |
| // If we're matching subdomains, and we have no host in the match pattern, |
| @@ -424,14 +431,14 @@ bool URLPattern::MatchesHost(const GURL& test) const { |
| return false; |
| // Check if the test host is a subdomain of our host. |
| - if (test.host().length() <= (host_.length() + 1)) |
| + if (test_host.length() <= (host_.length() + 1)) |
| return false; |
| - if (test.host().compare(test.host().length() - host_.length(), |
| - host_.length(), host_) != 0) |
| + if (test_host.compare(test_host.length() - host_.length(), host_.length(), |
| + host_) != 0) |
| return false; |
| - return test.host()[test.host().length() - host_.length() - 1] == '.'; |
| + return test_host[test_host.length() - host_.length() - 1] == '.'; |
| } |
| bool URLPattern::ImpliesAllHosts() const { |
| @@ -607,6 +614,23 @@ std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const { |
| return result; |
| } |
| +std::string URLPattern::CanonicalizeHostForMatching(const GURL& url) const { |
| + std::string url_host = url.host(); |
| + if (url_host.empty() || url.HostIsIPAddress()) |
| + return url_host; |
| + |
| + const bool pattern_host_ends_with_dot = EndsWithDot(host_); |
| + const bool url_host_ends_with_dot = EndsWithDot(url_host); |
| + if (pattern_host_ends_with_dot != url_host_ends_with_dot) { |
| + if (url_host_ends_with_dot) |
| + url_host = net::TrimEndingDot(url_host); |
| + else |
| + url_host += "."; |
| + } |
| + |
| + return url_host; |
| +} |
| + |
| // static |
| const char* URLPattern::GetParseResultString( |
| URLPattern::ParseResult parse_result) { |