Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(913)

Unified Diff: chrome/common/extensions/url_pattern.cc

Issue 7229012: Use extension match pattern syntax in content settings extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/url_pattern.cc
diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc
index 6749ab8632bef945643b225c8f250476ffc58717..1b1f423c11fd7030784d81d8ddae74f01be319b8 100644
--- a/chrome/common/extensions/url_pattern.cc
+++ b/chrome/common/extensions/url_pattern.cc
@@ -4,6 +4,7 @@
#include "chrome/common/extensions/url_pattern.h"
+#include "base/string_number_conversions.h"
#include "base/string_piece.h"
#include "base/string_split.h"
#include "base/string_util.h"
@@ -48,6 +49,8 @@ const char* kParseErrorInvalidHostWildcard = "Invalid host wildcard.";
const char* kParseErrorEmptyPath = "Empty path.";
const char* kParseErrorHasColon =
"Ports are not supported in URL patterns. ':' may not be used in a host.";
+const char* kParseErrorInvalidPort =
+ "Invalid port.";
// Message explaining each URLPattern::ParseResult.
const char* kParseResultMessages[] = {
@@ -58,7 +61,8 @@ const char* kParseResultMessages[] = {
kParseErrorEmptyHost,
kParseErrorInvalidHostWildcard,
kParseErrorEmptyPath,
- kParseErrorHasColon
+ kParseErrorHasColon,
+ kParseErrorInvalidPort,
};
COMPILE_ASSERT(URLPattern::NUM_PARSE_RESULTS == arraysize(kParseResultMessages),
@@ -75,20 +79,34 @@ bool IsStandardScheme(const std::string& scheme) {
url_parse::Component(0, static_cast<int>(scheme.length())));
}
+bool IsValidPort(const std::string& port) {
+ if (port.empty() || port == "*")
+ return true;
+ int parsed_port;
+ if (!base::StringToInt(port, &parsed_port))
+ return false;
+ return (parsed_port >= 0) && (parsed_port < 65536);
+}
+
} // namespace
URLPattern::URLPattern()
: valid_schemes_(SCHEME_NONE),
match_all_urls_(false),
- match_subdomains_(false) {}
+ match_subdomains_(false),
+ ignore_ports_(true) {}
URLPattern::URLPattern(int valid_schemes)
- : valid_schemes_(valid_schemes), match_all_urls_(false),
- match_subdomains_(false) {}
+ : valid_schemes_(valid_schemes),
+ match_all_urls_(false),
+ match_subdomains_(false),
+ ignore_ports_(true) {}
URLPattern::URLPattern(int valid_schemes, const std::string& pattern)
- : valid_schemes_(valid_schemes), match_all_urls_(false),
- match_subdomains_(false) {
+ : valid_schemes_(valid_schemes),
+ match_all_urls_(false),
+ match_subdomains_(false),
+ ignore_ports_(true) {
// Strict error checking is used, because this constructor is only
// appropriate when we know |pattern| is valid.
@@ -191,8 +209,19 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern,
SetPath(pattern.substr(path_start_pos));
- if (strictness == PARSE_STRICT && host_.find(':') != std::string::npos)
- return PARSE_ERROR_HAS_COLON;
+ std::string port = "*";
+ size_t port_pos = host_.find(':');
+ if (port_pos != std::string::npos) {
+ if (strictness == PARSE_STRICT)
+ return PARSE_ERROR_HAS_COLON;
+
+ if (!ignore_ports_)
+ port = host_.substr(port_pos + 1);
+
+ host_ = host_.substr(0, port_pos);
+ }
+ if (!SetPort(port))
+ return PARSE_ERROR_INVALID_PORT;
return PARSE_SUCCESS;
}
@@ -226,6 +255,14 @@ void URLPattern::SetPath(const std::string& path) {
ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?");
}
+bool URLPattern::SetPort(const std::string& port) {
+ if (IsValidPort(port)) {
+ port_ = port;
+ return true;
+ }
+ return false;
+}
+
bool URLPattern::MatchesURL(const GURL &test) const {
if (!MatchesScheme(test.scheme()))
return false;
@@ -240,6 +277,9 @@ bool URLPattern::MatchesURL(const GURL &test) const {
if (!MatchesPath(test.PathForRequest()))
return false;
+ if (!MatchesPort(test.port()))
+ return false;
+
return true;
}
@@ -296,6 +336,13 @@ bool URLPattern::MatchesPath(const std::string& test) const {
return true;
}
+bool URLPattern::MatchesPort(const std::string& test) const {
+ if (!IsValidPort(test))
+ return false;
+
+ return port_ == "*" || port_ == test;
+}
+
std::string URLPattern::GetAsString() const {
if (match_all_urls_)
return kAllUrlsPattern;
@@ -314,6 +361,11 @@ std::string URLPattern::GetAsString() const {
if (!host_.empty())
spec += host_;
+
+ if (!ignore_ports_ && !port_.empty()) {
+ spec += ":";
+ spec += port_;
+ }
}
if (!path_.empty())
@@ -331,6 +383,9 @@ bool URLPattern::OverlapsWith(const URLPattern& other) const {
if (!MatchesHost(other.host()) && !other.MatchesHost(host_))
return false;
+ if (port_ != "*" && other.port() != "*" && port_ != other.port())
+ return false;
+
// We currently only use OverlapsWith() for the patterns inside
// URLPatternSet. In those cases, we know that the path will have only a
// single wildcard at the end. This makes figuring out overlap much easier. It

Powered by Google App Engine
This is Rietveld 408576698