diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-09 01:42:07 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-12-09 01:42:07 +0000 |
commit | cf57d5fb41b76e498f33b6899bb9a9fc59479b16 (patch) | |
tree | 71bf7c2ab078ef54b7933f4ece14ca439a429cca /chrome/common/extensions | |
parent | c12d646d8d55b78cea237a5f7cbf64b7a5fa85c6 (diff) | |
download | chromium_src-cf57d5fb41b76e498f33b6899bb9a9fc59479b16.zip chromium_src-cf57d5fb41b76e498f33b6899bb9a9fc59479b16.tar.gz chromium_src-cf57d5fb41b76e498f33b6899bb9a9fc59479b16.tar.bz2 |
Move URLPattern::ParseOption into a field.
This will be needed to change the default of URLPattern to
USE_PORTS.
BUG=104104
Review URL: http://codereview.chromium.org/8885022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@113730 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/extensions')
-rw-r--r-- | chrome/common/extensions/extension.cc | 52 | ||||
-rw-r--r-- | chrome/common/extensions/extension.h | 2 | ||||
-rw-r--r-- | chrome/common/extensions/extension_messages.cc | 3 | ||||
-rw-r--r-- | chrome/common/extensions/url_pattern.cc | 28 | ||||
-rw-r--r-- | chrome/common/extensions/url_pattern.h | 29 | ||||
-rw-r--r-- | chrome/common/extensions/url_pattern_unittest.cc | 145 | ||||
-rw-r--r-- | chrome/common/extensions/user_script.cc | 5 | ||||
-rw-r--r-- | chrome/common/extensions/user_script_unittest.cc | 41 |
8 files changed, 136 insertions, 169 deletions
diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index 2054357..56f9421 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -283,8 +283,8 @@ void Extension::OverrideLaunchUrl(const GURL& override_url) { launch_web_url_ = new_url.spec(); - URLPattern pattern(kValidWebExtentSchemes); - pattern.Parse(new_url.spec(), URLPattern::ERROR_ON_PORTS); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kValidWebExtentSchemes); + pattern.Parse(new_url.spec()); pattern.SetPath(pattern.path() + '*'); extent_.AddPattern(pattern); } @@ -387,7 +387,7 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, std::string* error, UserScript* result) { // When strict error checks are enabled, make URL pattern parsing strict. - URLPattern::ParseOption parse_strictness = + URLPattern::ParseOption parse_option = (flags & STRICT_ERROR_CHECKS ? URLPattern::ERROR_ON_PORTS : URLPattern::IGNORE_PORTS); @@ -448,12 +448,11 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, return false; } - URLPattern pattern(UserScript::kValidUserScriptSchemes); + URLPattern pattern(parse_option, UserScript::kValidUserScriptSchemes); if (CanExecuteScriptEverywhere()) pattern.SetValidSchemes(URLPattern::SCHEME_ALL); - URLPattern::ParseResult parse_result = pattern.Parse(match_str, - parse_strictness); + URLPattern::ParseResult parse_result = pattern.Parse(match_str); if (parse_result != URLPattern::PARSE_SUCCESS) { *error = ExtensionErrorUtils::FormatErrorMessage( errors::kInvalidMatch, @@ -495,11 +494,10 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script, return false; } - URLPattern pattern(UserScript::kValidUserScriptSchemes); + URLPattern pattern(parse_option, UserScript::kValidUserScriptSchemes); if (CanExecuteScriptEverywhere()) pattern.SetValidSchemes(URLPattern::SCHEME_ALL); - URLPattern::ParseResult parse_result = pattern.Parse(match_str, - parse_strictness); + URLPattern::ParseResult parse_result = pattern.Parse(match_str); if (parse_result != URLPattern::PARSE_SUCCESS) { *error = ExtensionErrorUtils::FormatErrorMessage( errors::kInvalidExcludeMatch, @@ -799,9 +797,9 @@ FileBrowserHandler* Extension::LoadFileBrowserHandler( return NULL; } StringToLowerASCII(&filter); - URLPattern pattern(URLPattern::SCHEME_FILESYSTEM); - if (pattern.Parse(filter, URLPattern::ERROR_ON_PORTS) != - URLPattern::PARSE_SUCCESS) { + URLPattern pattern(URLPattern::ERROR_ON_PORTS, + URLPattern::SCHEME_FILESYSTEM); + if (pattern.Parse(filter) != URLPattern::PARSE_SUCCESS) { *error = ExtensionErrorUtils::FormatErrorMessage( errors::kInvalidURLPatternError, filter); return NULL; @@ -884,7 +882,7 @@ bool Extension::LoadExtent(const extensions::Manifest* manifest, URLPatternSet* extent, const char* list_error, const char* value_error, - URLPattern::ParseOption parse_strictness, + URLPattern::ParseOption parse_option, std::string* error) { Value* temp = NULL; if (!manifest->Get(key, &temp)) @@ -905,12 +903,11 @@ bool Extension::LoadExtent(const extensions::Manifest* manifest, return false; } - URLPattern pattern(kValidWebExtentSchemes); - URLPattern::ParseResult parse_result = pattern.Parse(pattern_string, - parse_strictness); + URLPattern pattern(parse_option, kValidWebExtentSchemes); + URLPattern::ParseResult parse_result = pattern.Parse(pattern_string); if (parse_result == URLPattern::PARSE_ERROR_EMPTY_PATH) { pattern_string += "/"; - parse_result = pattern.Parse(pattern_string, parse_strictness); + parse_result = pattern.Parse(pattern_string); } if (parse_result != URLPattern::PARSE_SUCCESS) { @@ -996,7 +993,7 @@ bool Extension::LoadLaunchURL(const extensions::Manifest* manifest, // Ensure the launch URL is a valid absolute URL and web extent scheme. GURL url(launch_url); - URLPattern pattern(kValidWebExtentSchemes); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kValidWebExtentSchemes); if (!url.is_valid() || !pattern.SetScheme(url.scheme())) { *error = errors::kInvalidLaunchWebURL; return false; @@ -1011,7 +1008,7 @@ bool Extension::LoadLaunchURL(const extensions::Manifest* manifest, // If there is no extent, we default the extent based on the launch URL. if (web_extent().is_empty() && !launch_web_url().empty()) { GURL launch_url(launch_web_url()); - URLPattern pattern(kValidWebExtentSchemes); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kValidWebExtentSchemes); if (!pattern.SetScheme("*")) { *error = errors::kInvalidLaunchWebURL; return false; @@ -1391,7 +1388,7 @@ bool Extension::InitFromValue(extensions::Manifest* manifest, int flags, return false; // When strict error checks are enabled, make URL pattern parsing strict. - URLPattern::ParseOption parse_strictness = + URLPattern::ParseOption parse_option = (flags & STRICT_ERROR_CHECKS ? URLPattern::ERROR_ON_PORTS : URLPattern::IGNORE_PORTS); @@ -1479,7 +1476,7 @@ bool Extension::InitFromValue(extensions::Manifest* manifest, int flags, (!LoadExtent(manifest_.get(), keys::kWebURLs, &extent_, errors::kInvalidWebURLs, errors::kInvalidWebURL, - parse_strictness, error) || + parse_option, error) || !LoadLaunchURL(manifest_.get(), error) || !LoadLaunchContainer(manifest_.get(), error))) { return false; @@ -2524,7 +2521,7 @@ bool Extension::ParsePermissions(const extensions::Manifest* source, URLPatternSet* host_permissions) { if (source->HasKey(key)) { // When strict error checks are enabled, make URL pattern parsing strict. - URLPattern::ParseOption parse_strictness = + URLPattern::ParseOption parse_option = (flags & STRICT_ERROR_CHECKS ? URLPattern::ERROR_ON_PORTS : URLPattern::IGNORE_PORTS); ListValue* permissions = NULL; @@ -2554,11 +2551,11 @@ bool Extension::ParsePermissions(const extensions::Manifest* source, } // Check if it's a host pattern permission. - URLPattern pattern = URLPattern(CanExecuteScriptEverywhere() ? - URLPattern::SCHEME_ALL : kValidHostPermissionSchemes); + const int kAllowedSchemes = CanExecuteScriptEverywhere() ? + URLPattern::SCHEME_ALL : kValidHostPermissionSchemes; - URLPattern::ParseResult parse_result = pattern.Parse(permission_str, - parse_strictness); + URLPattern pattern = URLPattern(parse_option, kAllowedSchemes); + URLPattern::ParseResult parse_result = pattern.Parse(permission_str); if (parse_result == URLPattern::PARSE_SUCCESS) { if (!CanSpecifyHostPermission(pattern, *api_permissions)) { *error = ExtensionErrorUtils::FormatErrorMessage( @@ -2891,7 +2888,8 @@ bool Extension::OverlapsWithOrigin(const GURL& origin) const { return false; // Note: patterns and extents ignore port numbers. - URLPattern origin_only_pattern(kValidWebExtentSchemes); + URLPattern origin_only_pattern(URLPattern::ERROR_ON_PORTS, + kValidWebExtentSchemes); if (!origin_only_pattern.SetScheme(origin.scheme())) return false; origin_only_pattern.SetHost(origin.host()); diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index 54bff60..b3218eb 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -650,7 +650,7 @@ class Extension : public base::RefCountedThreadSafe<Extension> { URLPatternSet* extent, const char* list_error, const char* value_error, - URLPattern::ParseOption parse_strictness, + URLPattern::ParseOption parse_option, std::string* error); bool LoadLaunchContainer(const extensions::Manifest* manifest, std::string* error); diff --git a/chrome/common/extensions/extension_messages.cc b/chrome/common/extensions/extension_messages.cc index 362a10a..8324a6c 100644 --- a/chrome/common/extensions/extension_messages.cc +++ b/chrome/common/extensions/extension_messages.cc @@ -117,8 +117,9 @@ bool ParamTraits<URLPattern>::Read(const Message* m, void** iter, // match the invalid patterns. We get around this by setting the valid // schemes after parsing the pattern. Update these method calls once we can // ignore scheme validation with URLPattern parse options. crbug.com/90544 + p->SetParseOption(URLPattern::IGNORE_PORTS); p->SetValidSchemes(URLPattern::SCHEME_ALL); - URLPattern::ParseResult result = p->Parse(spec, URLPattern::IGNORE_PORTS); + URLPattern::ParseResult result = p->Parse(spec); p->SetValidSchemes(valid_schemes); return URLPattern::PARSE_SUCCESS == result; } diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc index 91664f5..8e831a4 100644 --- a/chrome/common/extensions/url_pattern.cc +++ b/chrome/common/extensions/url_pattern.cc @@ -98,26 +98,28 @@ bool IsValidPortForScheme(const std::string scheme, const std::string& port) { } // namespace URLPattern::URLPattern() - : valid_schemes_(SCHEME_NONE), + : parse_option_(ERROR_ON_PORTS), + valid_schemes_(SCHEME_NONE), match_all_urls_(false), match_subdomains_(false), port_("*") {} -URLPattern::URLPattern(int valid_schemes) - : valid_schemes_(valid_schemes), +URLPattern::URLPattern(URLPattern::ParseOption parse_option, int valid_schemes) + : parse_option_(parse_option), + valid_schemes_(valid_schemes), match_all_urls_(false), match_subdomains_(false), port_("*") {} URLPattern::URLPattern(int valid_schemes, const std::string& pattern) - : valid_schemes_(valid_schemes), + // Strict error checking is used, because this constructor is only + // appropriate when we know |pattern| is valid. + : parse_option_(ERROR_ON_PORTS), + valid_schemes_(valid_schemes), match_all_urls_(false), match_subdomains_(false), port_("*") { - - // Strict error checking is used, because this constructor is only - // appropriate when we know |pattern| is valid. - if (PARSE_SUCCESS != Parse(pattern, ERROR_ON_PORTS)) + if (PARSE_SUCCESS != Parse(pattern)) NOTREACHED() << "URLPattern is invalid: " << pattern; } @@ -132,8 +134,7 @@ bool URLPattern::operator==(const URLPattern& other) const { return GetAsString() == other.GetAsString(); } -URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, - ParseOption strictness) { +URLPattern::ParseResult URLPattern::Parse(const std::string& pattern) { spec_.clear(); // Special case pattern to match every valid URL. @@ -215,7 +216,7 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern, size_t port_pos = host_.find(':'); if (port_pos != std::string::npos) { - switch (strictness) { + switch (parse_option_) { case USE_PORTS: { if (!SetPort(host_.substr(port_pos + 1))) return PARSE_ERROR_INVALID_PORT; @@ -245,6 +246,11 @@ void URLPattern::SetValidSchemes(int valid_schemes) { valid_schemes_ = valid_schemes; } +void URLPattern::SetParseOption(URLPattern::ParseOption parse_option) { + spec_.clear(); + parse_option_ = parse_option; +} + void URLPattern::SetHost(const std::string& host) { spec_.clear(); host_ = host; diff --git a/chrome/common/extensions/url_pattern.h b/chrome/common/extensions/url_pattern.h index b9d039d..ef01c1b 100644 --- a/chrome/common/extensions/url_pattern.h +++ b/chrome/common/extensions/url_pattern.h @@ -123,17 +123,13 @@ class URLPattern { // The <all_urls> string pattern. static const char kAllUrlsPattern[]; - // Construct an URLPattern with the given set of allowable schemes. See - // valid_schemes_ for more info. - explicit URLPattern(int valid_schemes); + explicit URLPattern(ParseOption parse_option, int valid_schemes); // Convenience to construct a URLPattern from a string. The string is expected - // to be a valid pattern. If the string is not known ahead of time, use - // Parse() instead, which returns success or failure. + // to be a valid pattern when parsed with USE_PORTS. If the string is not + // known ahead of time, use Parse() instead, which returns success or failure. URLPattern(int valid_schemes, const std::string& pattern); - // Note: don't use this directly. This exists so URLPattern can be used - // with STL containers. URLPattern(); ~URLPattern(); @@ -143,21 +139,17 @@ class URLPattern { // Initializes this instance by parsing the provided string. Returns // URLPattern::PARSE_SUCCESS on success, or an error code otherwise. On // failure, this instance will have some intermediate values and is in an - // invalid state. Adding error checks to URLPattern::Parse() can cause - // patterns in installed extensions to fail. If an installed extension - // uses a pattern that was valid but fails a new error check, the - // extension will fail to load when chrome is auto-updated. To avoid - // this, new parse checks are enabled only when |strictness| is - // OPTION_STRICT. OPTION_STRICT should be used when loading in developer - // mode, or when an extension's patterns are controlled by chrome (such - // as component extensions). - ParseResult Parse(const std::string& pattern_str, - ParseOption strictness); + // invalid state. + ParseResult Parse(const std::string& pattern_str); // Gets the bitmask of valid schemes. int valid_schemes() const { return valid_schemes_; } void SetValidSchemes(int valid_schemes); + // Gets or sets the parse option used with Parse(). + ParseOption parse_option() const { return parse_option_; } + void SetParseOption(ParseOption parse_option); + // Gets the host the pattern matches. This can be an empty string if the // pattern matches all hosts (the input was <scheme>://*/<whatever>). const std::string& host() const { return host_; } @@ -252,6 +244,9 @@ class URLPattern { // equivalent literal schemes, otherwise returns the current scheme. std::vector<std::string> GetExplicitSchemes() const; + // Controls how to interpret the input to Parse(). + ParseOption parse_option_; + // 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 a78554f..578fd0a 100644 --- a/chrome/common/extensions/url_pattern_unittest.cc +++ b/chrome/common/extensions/url_pattern_unittest.cc @@ -35,10 +35,9 @@ TEST(ExtensionURLPatternTest, ParseInvalid) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kInvalidPatterns); ++i) { - URLPattern pattern(URLPattern::SCHEME_ALL); + URLPattern pattern(URLPattern::IGNORE_PORTS, URLPattern::SCHEME_ALL); EXPECT_EQ(kInvalidPatterns[i].expected_result, - pattern.Parse(kInvalidPatterns[i].pattern, - URLPattern::IGNORE_PORTS)) + pattern.Parse(kInvalidPatterns[i].pattern)) << kInvalidPatterns[i].pattern; } }; @@ -103,31 +102,30 @@ TEST(ExtensionURLPatternTest, Ports) { }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestPatterns); ++i) { - URLPattern use_ports_pattern(URLPattern::SCHEME_ALL); + URLPattern use_ports_pattern(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); // Check results with |USE_PORTS|. EXPECT_EQ(kTestPatterns[i].expected_result_use_ports, - use_ports_pattern.Parse(kTestPatterns[i].pattern, - URLPattern::USE_PORTS)) + use_ports_pattern.Parse(kTestPatterns[i].pattern)) << "Got unexpected result for URL pattern: " << kTestPatterns[i].pattern; EXPECT_EQ(kTestPatterns[i].expected_port, use_ports_pattern.port()) << "Got unexpected port for URL pattern: " << kTestPatterns[i].pattern; - URLPattern ignore_ports_pattern(URLPattern::SCHEME_ALL); + URLPattern ignore_ports_pattern(URLPattern::IGNORE_PORTS, + URLPattern::SCHEME_ALL); // Check results with |IGNORE_PORTS|. EXPECT_EQ(kTestPatterns[i].expected_result_ignore_ports, - ignore_ports_pattern.Parse(kTestPatterns[i].pattern, - URLPattern::IGNORE_PORTS)) + ignore_ports_pattern.Parse(kTestPatterns[i].pattern)) << "Got unexpected result for URL pattern: " << kTestPatterns[i].pattern; EXPECT_EQ("*", ignore_ports_pattern.port()) << "Got unexpected port for URL pattern: " << kTestPatterns[i].pattern; - URLPattern error_on_ports_pattern(URLPattern::SCHEME_ALL); + URLPattern error_on_ports_pattern(URLPattern::ERROR_ON_PORTS, + URLPattern::SCHEME_ALL); // Check results with |ERROR_ON_PORTS|. EXPECT_EQ(kTestPatterns[i].expected_result_error_on_ports, - error_on_ports_pattern.Parse(kTestPatterns[i].pattern, - URLPattern::ERROR_ON_PORTS)) + error_on_ports_pattern.Parse(kTestPatterns[i].pattern)) << "Got unexpected result for URL pattern: " << kTestPatterns[i].pattern; } @@ -135,9 +133,8 @@ TEST(ExtensionURLPatternTest, Ports) { // all pages for a given scheme TEST(ExtensionURLPatternTest, Match1) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/*")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("", pattern.host()); EXPECT_TRUE(pattern.match_subdomains()); @@ -152,9 +149,8 @@ TEST(ExtensionURLPatternTest, Match1) { // all domains TEST(ExtensionURLPatternTest, Match2) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("https://*/foo*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("https://*/foo*")); EXPECT_EQ("https", pattern.scheme()); EXPECT_EQ("", pattern.host()); EXPECT_TRUE(pattern.match_subdomains()); @@ -168,10 +164,9 @@ TEST(ExtensionURLPatternTest, Match2) { // subdomains TEST(URLPatternTest, Match3) { - URLPattern pattern(kAllSchemes); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*.google.com/foo*bar", - URLPattern::ERROR_ON_PORTS)); + pattern.Parse("http://*.google.com/foo*bar")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("google.com", pattern.host()); EXPECT_TRUE(pattern.match_subdomains()); @@ -186,9 +181,8 @@ TEST(URLPatternTest, Match3) { // glob escaping TEST(ExtensionURLPatternTest, Match5) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("file:///foo?bar\\*baz", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file:///foo?bar\\*baz")); EXPECT_EQ("file", pattern.scheme()); EXPECT_EQ("", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -200,9 +194,8 @@ TEST(ExtensionURLPatternTest, Match5) { // ip addresses TEST(ExtensionURLPatternTest, Match6) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://127.0.0.1/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://127.0.0.1/*")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("127.0.0.1", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -213,10 +206,9 @@ TEST(ExtensionURLPatternTest, Match6) { // subdomain matching with ip addresses TEST(ExtensionURLPatternTest, Match7) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*.0.0.1/*", - URLPattern::ERROR_ON_PORTS)); // allowed, but useless + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + // allowed, but useless + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.0.0.1/*")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("0.0.1", pattern.host()); EXPECT_TRUE(pattern.match_subdomains()); @@ -228,12 +220,11 @@ TEST(ExtensionURLPatternTest, Match7) { // unicode TEST(ExtensionURLPatternTest, Match8) { - URLPattern pattern(kAllSchemes); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); // The below is the ASCII encoding of the following URL: // http://*.\xe1\x80\xbf/a\xc2\x81\xe1* EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*.xn--gkd/a%C2%81%E1*", - URLPattern::ERROR_ON_PORTS)); + pattern.Parse("http://*.xn--gkd/a%C2%81%E1*")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("xn--gkd", pattern.host()); EXPECT_TRUE(pattern.match_subdomains()); @@ -247,9 +238,8 @@ TEST(ExtensionURLPatternTest, Match8) { // chrome:// TEST(ExtensionURLPatternTest, Match9) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("chrome://favicon/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("chrome://favicon/*")); EXPECT_EQ("chrome", pattern.scheme()); EXPECT_EQ("favicon", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -262,9 +252,8 @@ TEST(ExtensionURLPatternTest, Match9) { // *:// TEST(ExtensionURLPatternTest, Match10) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("*://*/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("*://*/*")); EXPECT_TRUE(pattern.MatchesScheme("http")); EXPECT_TRUE(pattern.MatchesScheme("https")); EXPECT_FALSE(pattern.MatchesScheme("chrome")); @@ -281,9 +270,8 @@ TEST(ExtensionURLPatternTest, Match10) { // <all_urls> TEST(ExtensionURLPatternTest, Match11) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("<all_urls>", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("<all_urls>")); EXPECT_TRUE(pattern.MatchesScheme("chrome")); EXPECT_TRUE(pattern.MatchesScheme("http")); EXPECT_TRUE(pattern.MatchesScheme("https")); @@ -298,7 +286,7 @@ TEST(ExtensionURLPatternTest, Match11) { // Make sure the properties are the same when creating an <all_urls> pattern // via SetMatchAllURLs and by parsing <all_urls>. - URLPattern pattern2(kAllSchemes); + URLPattern pattern2(URLPattern::ERROR_ON_PORTS, kAllSchemes); pattern2.SetMatchAllURLs(true); EXPECT_EQ(pattern.valid_schemes(), pattern2.valid_schemes()); @@ -312,9 +300,8 @@ TEST(ExtensionURLPatternTest, Match11) { // SCHEME_ALL matches all schemes. TEST(ExtensionURLPatternTest, Match12) { - URLPattern pattern(URLPattern::SCHEME_ALL); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("<all_urls>", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, URLPattern::SCHEME_ALL); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("<all_urls>")); EXPECT_TRUE(pattern.MatchesScheme("chrome")); EXPECT_TRUE(pattern.MatchesScheme("http")); EXPECT_TRUE(pattern.MatchesScheme("https")); @@ -352,10 +339,9 @@ static const struct MatchPatterns { // SCHEME_ALL and specific schemes. TEST(ExtensionURLPatternTest, Match13) { for (size_t i = 0; i < arraysize(kMatch13UrlPatternTestCases); ++i) { - URLPattern pattern(URLPattern::SCHEME_ALL); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, URLPattern::SCHEME_ALL); EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse(kMatch13UrlPatternTestCases[i].pattern, - URLPattern::ERROR_ON_PORTS)) + pattern.Parse(kMatch13UrlPatternTestCases[i].pattern)) << " while parsing " << kMatch13UrlPatternTestCases[i].pattern; EXPECT_TRUE(pattern.MatchesURL( GURL(kMatch13UrlPatternTestCases[i].matches))) @@ -363,17 +349,15 @@ TEST(ExtensionURLPatternTest, Match13) { } // Negative test. - URLPattern pattern(URLPattern::SCHEME_ALL); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("data:*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, URLPattern::SCHEME_ALL); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("data:*")); EXPECT_FALSE(pattern.MatchesURL(GURL("about:blank"))); }; // file scheme with empty hostname TEST(ExtensionURLPatternTest, Match14) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("file:///foo*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file:///foo*")); EXPECT_EQ("file", pattern.scheme()); EXPECT_EQ("", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -388,9 +372,8 @@ TEST(ExtensionURLPatternTest, Match14) { // file scheme without hostname part TEST(ExtensionURLPatternTest, Match15) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("file://foo*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file://foo*")); EXPECT_EQ("file", pattern.scheme()); EXPECT_EQ("", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -405,9 +388,8 @@ TEST(ExtensionURLPatternTest, Match15) { // file scheme with hostname TEST(ExtensionURLPatternTest, Match16) { - URLPattern pattern(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("file://localhost/foo*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("file://localhost/foo*")); EXPECT_EQ("file", pattern.scheme()); // Since hostname is ignored for file://. EXPECT_EQ("", pattern.host()); @@ -423,10 +405,9 @@ TEST(ExtensionURLPatternTest, Match16) { // Specific port TEST(ExtensionURLPatternTest, Match17) { - URLPattern pattern(kAllSchemes); + URLPattern pattern(URLPattern::USE_PORTS, kAllSchemes); EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://www.example.com:80/foo", - URLPattern::USE_PORTS)); + pattern.Parse("http://www.example.com:80/foo")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("www.example.com", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -440,10 +421,9 @@ TEST(ExtensionURLPatternTest, Match17) { // Explicit port wildcard TEST(ExtensionURLPatternTest, Match18) { - URLPattern pattern(kAllSchemes); + URLPattern pattern(URLPattern::USE_PORTS, kAllSchemes); EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://www.example.com:*/foo", - URLPattern::USE_PORTS)); + pattern.Parse("http://www.example.com:*/foo")); EXPECT_EQ("http", pattern.scheme()); EXPECT_EQ("www.example.com", pattern.host()); EXPECT_FALSE(pattern.match_subdomains()); @@ -475,18 +455,16 @@ static const struct GetAsStringPatterns { TEST(ExtensionURLPatternTest, GetAsString) { for (size_t i = 0; i < arraysize(kGetAsStringTestCases); ++i) { - URLPattern pattern1(URLPattern::SCHEME_ALL); + URLPattern pattern1(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern1.Parse(kGetAsStringTestCases[i].pattern, - URLPattern::USE_PORTS)) + pattern1.Parse(kGetAsStringTestCases[i].pattern)) << "Error parsing " << kGetAsStringTestCases[i].pattern; EXPECT_EQ(kGetAsStringTestCases[i].pattern, pattern1.GetAsString()); - URLPattern pattern2(URLPattern::SCHEME_ALL); + URLPattern pattern2(URLPattern::IGNORE_PORTS, URLPattern::SCHEME_ALL); EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern2.Parse(kGetAsStringTestCases[i].pattern, - URLPattern::IGNORE_PORTS)) + pattern2.Parse(kGetAsStringTestCases[i].pattern)) << "Error parsing " << kGetAsStringTestCases[i].pattern; EXPECT_EQ(kGetAsStringTestCases[i].pattern, pattern2.GetAsString()); @@ -581,12 +559,11 @@ TEST(ExtensionURLPatternTest, IgnorePorts) { std::string pattern_str = "http://www.example.com:8080/foo"; GURL url("http://www.example.com:1234/foo"); - URLPattern pattern1(kAllSchemes); - URLPattern pattern2(kAllSchemes); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern1.Parse(pattern_str, URLPattern::IGNORE_PORTS)); - EXPECT_EQ(URLPattern::PARSE_SUCCESS, - pattern2.Parse(pattern_str, URLPattern::USE_PORTS)); + URLPattern pattern1(URLPattern::IGNORE_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse(pattern_str)); + + URLPattern pattern2(URLPattern::USE_PORTS, kAllSchemes); + EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse(pattern_str)); EXPECT_EQ(pattern_str, pattern1.GetAsString()); EXPECT_EQ(pattern_str, pattern2.GetAsString()); @@ -678,11 +655,11 @@ TEST(ExtensionURLPatternTest, Equals) { message += " "; message += kEqualsTestCases[i].pattern2; - URLPattern pattern1(URLPattern::SCHEME_ALL); - URLPattern pattern2(URLPattern::SCHEME_ALL); + URLPattern pattern1(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); + URLPattern pattern2(URLPattern::USE_PORTS, URLPattern::SCHEME_ALL); - pattern1.Parse(kEqualsTestCases[i].pattern1, URLPattern::USE_PORTS); - pattern2.Parse(kEqualsTestCases[i].pattern2, URLPattern::USE_PORTS); + pattern1.Parse(kEqualsTestCases[i].pattern1); + pattern2.Parse(kEqualsTestCases[i].pattern2); EXPECT_EQ(kEqualsTestCases[i].expected_equal, pattern1 == pattern2) << message; } diff --git a/chrome/common/extensions/user_script.cc b/chrome/common/extensions/user_script.cc index 77fd715..ca86b92 100644 --- a/chrome/common/extensions/user_script.cc +++ b/chrome/common/extensions/user_script.cc @@ -183,7 +183,7 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { int valid_schemes; CHECK(pickle.ReadInt(iter, &valid_schemes)); std::string pattern_str; - URLPattern pattern(valid_schemes); + URLPattern pattern(URLPattern::IGNORE_PORTS, valid_schemes); CHECK(pickle.ReadString(iter, &pattern_str)); // We remove the file scheme if it's not actually allowed (see Extension:: @@ -192,8 +192,7 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0; if (!had_file_scheme) pattern.SetValidSchemes(valid_schemes | URLPattern::SCHEME_FILE); - CHECK(URLPattern::PARSE_SUCCESS == - pattern.Parse(pattern_str, URLPattern::IGNORE_PORTS)); + CHECK(URLPattern::PARSE_SUCCESS == pattern.Parse(pattern_str)); if (!had_file_scheme) pattern.SetValidSchemes(valid_schemes); diff --git a/chrome/common/extensions/user_script_unittest.cc b/chrome/common/extensions/user_script_unittest.cc index f3eb5fe..0c06fbb 100644 --- a/chrome/common/extensions/user_script_unittest.cc +++ b/chrome/common/extensions/user_script_unittest.cc @@ -71,9 +71,8 @@ TEST(ExtensionUserScriptTest, Glob_StringAnywhere) { } TEST(ExtensionUserScriptTest, UrlPattern) { - URLPattern pattern(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*/foo*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*/foo*")); UserScript script; script.add_url_pattern(pattern); @@ -86,14 +85,12 @@ TEST(ExtensionUserScriptTest, UrlPattern) { TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { UserScript script; - URLPattern pattern(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); script.add_url_pattern(pattern); - URLPattern exclude(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - exclude.Parse("*://*/*business*", URLPattern::ERROR_ON_PORTS)); + URLPattern exclude(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, exclude.Parse("*://*/*business*")); script.add_exclude_url_pattern(exclude); EXPECT_TRUE(script.MatchesURL(GURL("http://www.nytimes.com/health"))); @@ -104,9 +101,8 @@ TEST(ExtensionUserScriptTest, ExcludeUrlPattern) { TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { UserScript script; - URLPattern pattern(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); script.add_url_pattern(pattern); script.add_glob("*nytimes.com/???s/*"); @@ -119,9 +115,8 @@ TEST(ExtensionUserScriptTest, UrlPatternAndIncludeGlobs) { TEST(ExtensionUserScriptTest, UrlPatternAndExcludeGlobs) { UserScript script; - URLPattern pattern(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://*.nytimes.com/*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("http://*.nytimes.com/*")); script.add_url_pattern(pattern); script.add_exclude_glob("*science*"); @@ -135,10 +130,8 @@ TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { // If there are both, match intersection(union(globs), union(urlpatterns)). UserScript script; - URLPattern pattern(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern.Parse("http://www.google.com/*", - URLPattern::ERROR_ON_PORTS)); + URLPattern pattern(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS,pattern.Parse("http://www.google.com/*")); script.add_url_pattern(pattern); script.add_glob("*bar*"); @@ -164,12 +157,10 @@ TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { } TEST(ExtensionUserScriptTest, Pickle) { - URLPattern pattern1(kAllSchemes); - URLPattern pattern2(kAllSchemes); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern1.Parse("http://*/foo*", URLPattern::ERROR_ON_PORTS)); - ASSERT_EQ(URLPattern::PARSE_SUCCESS, - pattern2.Parse("http://bar/baz*", URLPattern::ERROR_ON_PORTS)); + URLPattern pattern1(URLPattern::ERROR_ON_PORTS, kAllSchemes); + URLPattern pattern2(URLPattern::ERROR_ON_PORTS, kAllSchemes); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern1.Parse("http://*/foo*")); + ASSERT_EQ(URLPattern::PARSE_SUCCESS, pattern2.Parse("http://bar/baz*")); UserScript script1; script1.js_scripts().push_back(UserScript::File( |