Chromium Code Reviews| Index: extensions/common/url_pattern.cc |
| diff --git a/extensions/common/url_pattern.cc b/extensions/common/url_pattern.cc |
| index 6a948b433764baa505ab54d9392bd2b05262bd4f..efea19279ec53ec560eda0a9139dca741e4adffb 100644 |
| --- a/extensions/common/url_pattern.cc |
| +++ b/extensions/common/url_pattern.cc |
| @@ -143,12 +143,14 @@ URLPattern::URLPattern() |
| : valid_schemes_(SCHEME_NONE), |
| match_all_urls_(false), |
| match_subdomains_(false), |
| + match_effective_tld_(true), |
| port_("*") {} |
| URLPattern::URLPattern(int valid_schemes) |
| : valid_schemes_(valid_schemes), |
| match_all_urls_(false), |
| match_subdomains_(false), |
| + match_effective_tld_(true), |
| port_("*") {} |
| URLPattern::URLPattern(int valid_schemes, const std::string& pattern) |
| @@ -157,6 +159,7 @@ URLPattern::URLPattern(int valid_schemes, const std::string& pattern) |
| : valid_schemes_(valid_schemes), |
| match_all_urls_(false), |
| match_subdomains_(false), |
| + match_effective_tld_(true), |
| port_("*") { |
| ParseResult result = Parse(pattern); |
| if (PARSE_SUCCESS != result) |
| @@ -185,9 +188,15 @@ std::ostream& operator<<(std::ostream& out, const URLPattern& url_pattern) { |
| } |
| URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) { |
| + return Parse(pattern, DENY_WILDCARD_FOR_EFFECTIVE_TLD); |
| +} |
| + |
| +URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, |
| + const ParseOptions parse_options) { |
| spec_.clear(); |
| SetMatchAllURLs(false); |
| SetMatchSubdomains(false); |
| + SetMatchEffectiveTld(true); |
| SetPort("*"); |
| // Special case pattern to match every valid URL. |
| @@ -266,6 +275,14 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) { |
| host_components.erase(host_components.begin(), |
| host_components.begin() + 1); |
| } |
| + |
| + // If explicitly allowed, the last component can optionally be '*' to |
| + // match all effective TLDs. |
| + if (parse_options == ALLOW_WILDCARD_FOR_EFFECTIVE_TLD && |
| + host_components.size() > 1 && host_components[1] == "*") { |
|
Devlin
2017/02/14 23:17:10
Won't this fail on something like maps.google.*, w
nrpeter
2017/03/22 23:47:39
Yes, updated. Added as a test case as well.
|
| + match_effective_tld_ = false; |
| + host_components.pop_back(); |
| + } |
| host_ = base::JoinString(host_components, "."); |
| path_start_pos = host_end_pos; |
| @@ -320,6 +337,11 @@ void URLPattern::SetMatchSubdomains(bool val) { |
| match_subdomains_ = val; |
| } |
| +void URLPattern::SetMatchEffectiveTld(bool val) { |
| + spec_.clear(); |
| + match_effective_tld_ = val; |
| +} |
| + |
| bool URLPattern::SetScheme(const std::string& scheme) { |
| spec_.clear(); |
| scheme_ = scheme; |
| @@ -419,10 +441,19 @@ bool URLPattern::MatchesHost(const std::string& host) const { |
| } |
| bool URLPattern::MatchesHost(const GURL& test) const { |
| - const base::StringPiece test_host( |
| - CanonicalizeHostForMatching(test.host_piece())); |
| + base::StringPiece test_host(CanonicalizeHostForMatching(test.host_piece())); |
| const base::StringPiece pattern_host(CanonicalizeHostForMatching(host_)); |
| + // If we don't care about matching the effective TLD, remove it. |
| + if (!match_effective_tld_) { |
| + int reg_length = net::registry_controlled_domains::GetRegistryLength( |
| + test, net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, |
| + net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| + if (reg_length > 0) { |
| + test_host = test_host.substr(0, test_host.size() - reg_length - 1); |
| + } |
| + } |
| + |
| // If the hosts are exactly equal, we have a match. |
| if (test_host == pattern_host) |
| return true; |
| @@ -522,6 +553,12 @@ const std::string& URLPattern::GetAsString() const { |
| if (!host_.empty()) |
| spec += host_; |
| + if (!match_effective_tld_) { |
| + if (!host_.empty()) |
| + spec += "."; |
| + spec += "*"; |
| + } |
| + |
| if (port_ != "*") { |
| spec += ":"; |
| spec += port_; |