summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormihaip@chromium.org <mihaip@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-23 18:34:21 +0000
committermihaip@chromium.org <mihaip@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-23 18:34:21 +0000
commit65e0e1512469b98cab0e187109b5875f07487853 (patch)
treec30698c96e68016bca23734a2a18c561a2412cb5
parent20bc1f8743a9eb9a35004a45493f19803d31d9ee (diff)
downloadchromium_src-65e0e1512469b98cab0e187109b5875f07487853.zip
chromium_src-65e0e1512469b98cab0e187109b5875f07487853.tar.gz
chromium_src-65e0e1512469b98cab0e187109b5875f07487853.tar.bz2
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
-rw-r--r--chrome/common/extensions/url_pattern.cc41
-rw-r--r--chrome/common/extensions/url_pattern.h7
-rw-r--r--chrome/common/extensions/url_pattern_unittest.cc15
3 files changed, 55 insertions, 8 deletions
diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc
index f041256..ad7e937 100644
--- a/chrome/common/extensions/url_pattern.cc
+++ b/chrome/common/extensions/url_pattern.cc
@@ -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) {
diff --git a/chrome/common/extensions/url_pattern.h b/chrome/common/extensions/url_pattern.h
index e64f176..1dea772 100644
--- a/chrome/common/extensions/url_pattern.h
+++ b/chrome/common/extensions/url_pattern.h
@@ -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_
diff --git a/chrome/common/extensions/url_pattern_unittest.cc b/chrome/common/extensions/url_pattern_unittest.cc
index 57c2ff2..426289d 100644
--- a/chrome/common/extensions/url_pattern_unittest.cc
+++ b/chrome/common/extensions/url_pattern_unittest.cc
@@ -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) {