Commit 65e0e151 authored by mihaip@chromium.org's avatar mihaip@chromium.org

Make URLPattern::OverlapsWith handle wildcards better by expanding them to explicit schemes.

BUG=70126
TEST=no

R=asargent@chromium.org

Review URL: http://codereview.chromium.org/7049032

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86309 0039d316-1c4b-4281-b951-d872f2087c98
parent 20bc1f87
......@@ -311,8 +311,10 @@ std::string URLPattern::GetAsString() const {
}
bool URLPattern::OverlapsWith(const URLPattern& other) const {
if (!MatchesScheme(other.scheme_) && !other.MatchesScheme(scheme_))
if (!MatchesAnyScheme(other.GetExplicitSchemes()) &&
!other.MatchesAnyScheme(GetExplicitSchemes())) {
return false;
}
if (!MatchesHost(other.host()) && !other.MatchesHost(host_))
return false;
......@@ -332,26 +334,49 @@ bool URLPattern::OverlapsWith(const URLPattern& other) const {
return true;
}
std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const {
std::vector<URLPattern> result;
bool URLPattern::MatchesAnyScheme(
const std::vector<std::string>& schemes) const {
for (std::vector<std::string>::const_iterator i = schemes.begin();
i != schemes.end(); ++i) {
if (MatchesScheme(*i))
return true;
}
return false;
}
std::vector<std::string> URLPattern::GetExplicitSchemes() const {
std::vector<std::string> result;
if (scheme_ != "*" && !match_all_urls_ && IsValidScheme(scheme_)) {
result.push_back(*this);
result.push_back(scheme_);
return result;
}
for (size_t i = 0; i < arraysize(kValidSchemes); ++i) {
if (MatchesScheme(kValidSchemes[i])) {
URLPattern temp = *this;
temp.SetScheme(kValidSchemes[i]);
temp.set_match_all_urls(false);
result.push_back(temp);
result.push_back(kValidSchemes[i]);
}
}
return result;
}
std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const {
std::vector<std::string> explicit_schemes = GetExplicitSchemes();
std::vector<URLPattern> result;
for (std::vector<std::string>::const_iterator i = explicit_schemes.begin();
i != explicit_schemes.end(); ++i) {
URLPattern temp = *this;
temp.SetScheme(*i);
temp.set_match_all_urls(false);
result.push_back(temp);
}
return result;
}
// static
const char* URLPattern::GetParseResultString(
URLPattern::ParseResult parse_result) {
......
......@@ -234,6 +234,13 @@ class URLPattern {
URLPattern();
#endif
// Returns true if any of the |schemes| items matches our scheme.
bool MatchesAnyScheme(const std::vector<std::string>& schemes) const;
// If the URLPattern contains a wildcard scheme, returns a list of
// equivalent literal schemes, otherwise returns the current scheme.
std::vector<std::string> GetExplicitSchemes() const;
// A bitmask containing the schemes which are considered valid for this
// pattern. Parse() uses this to decide whether a pattern contains a valid
// scheme. MatchesScheme uses this to decide whether a wildcard scheme_
......
......@@ -366,6 +366,21 @@ TEST(ExtensionURLPatternTest, OverlapsWith) {
// Test that '<all_urls>' includes file URLs, while scheme '*' does not.
TestPatternOverlap(pattern7, pattern8, false);
TestPatternOverlap(pattern7, pattern10, true);
// Test that wildcard schemes are handled correctly, especially when compared
// to each-other.
URLPattern pattern11(kAllSchemes, "http://example.com/*");
URLPattern pattern12(kAllSchemes, "*://example.com/*");
URLPattern pattern13(kAllSchemes, "*://example.com/foo/*");
URLPattern pattern14(kAllSchemes, "*://google.com/*");
TestPatternOverlap(pattern8, pattern12, true);
TestPatternOverlap(pattern9, pattern12, true);
TestPatternOverlap(pattern10, pattern12, true);
TestPatternOverlap(pattern11, pattern12, true);
TestPatternOverlap(pattern12, pattern13, true);
TestPatternOverlap(pattern11, pattern13, true);
TestPatternOverlap(pattern14, pattern12, false);
TestPatternOverlap(pattern14, pattern13, false);
}
TEST(ExtensionURLPatternTest, ConvertToExplicitSchemes) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment