Commit c640fd7c authored by battre@chromium.org's avatar battre@chromium.org

Let url filter test the scheme in urlContains/Equals/Prefix/Suffix criteria

BUG=140238

Review URL: https://chromiumcodereview.appspot.com/10823313

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152067 0039d316-1c4b-4281-b951-d872f2087c98
parent 10766919
...@@ -256,22 +256,22 @@ ...@@ -256,22 +256,22 @@
}, },
"urlContains": { "urlContains": {
"type": "string", "type": "string",
"description": "Matches if the URL contains a specified string.", "description": "Matches if the URL (without fragment identifier) contains a specified string. Port numbers are stripped from the URL if they match the default port number.",
"optional": true "optional": true
}, },
"urlEquals": { "urlEquals": {
"type": "string", "type": "string",
"description": "Matches if the URL is equal to a specified string.", "description": "Matches if the URL (without fragment identifier) is equal to a specified string. Port numbers are stripped from the URL if they match the default port number.",
"optional": true "optional": true
}, },
"urlPrefix": { "urlPrefix": {
"type": "string", "type": "string",
"description": "Matches if the URL starts with a specified string.", "description": "Matches if the URL (without fragment identifier) starts with a specified string. Port numbers are stripped from the URL if they match the default port number.",
"optional": true "optional": true
}, },
"urlSuffix": { "urlSuffix": {
"type": "string", "type": "string",
"description": "Matches if the URL ends with a specified string.", "description": "Matches if the URL (without fragment identifier) ends with a specified string. Port numbers are stripped from the URL if they match the default port number.",
"optional": true "optional": true
}, },
"schemes": { "schemes": {
......
...@@ -1265,7 +1265,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1265,7 +1265,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL contains a specified string.</dd> <dd>Matches if the URL (without fragment identifier) contains a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
...@@ -1289,7 +1289,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1289,7 +1289,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL is equal to a specified string.</dd> <dd>Matches if the URL (without fragment identifier) is equal to a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
...@@ -1313,7 +1313,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1313,7 +1313,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL starts with a specified string.</dd> <dd>Matches if the URL (without fragment identifier) starts with a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
...@@ -1337,7 +1337,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1337,7 +1337,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL ends with a specified string.</dd> <dd>Matches if the URL (without fragment identifier) ends with a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
......
...@@ -1395,7 +1395,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1395,7 +1395,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL contains a specified string.</dd> <dd>Matches if the URL (without fragment identifier) contains a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
...@@ -1419,7 +1419,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1419,7 +1419,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL is equal to a specified string.</dd> <dd>Matches if the URL (without fragment identifier) is equal to a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
...@@ -1443,7 +1443,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1443,7 +1443,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL starts with a specified string.</dd> <dd>Matches if the URL (without fragment identifier) starts with a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
...@@ -1467,7 +1467,7 @@ refers to an array of rules including filled optional parameters. ...@@ -1467,7 +1467,7 @@ refers to an array of rules including filled optional parameters.
</div> </div>
</em> </em>
</dt> </dt>
<dd>Matches if the URL ends with a specified string.</dd> <dd>Matches if the URL (without fragment identifier) ends with a specified string. Port numbers are stripped from the URL if they match the default port number.</dd>
<!-- OBJECT PROPERTIES --> <!-- OBJECT PROPERTIES -->
<!-- OBJECT METHODS --> <!-- OBJECT METHODS -->
<!-- OBJECT EVENT FIELDS --> <!-- OBJECT EVENT FIELDS -->
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#include <iterator> #include <iterator>
#include "base/logging.h" #include "base/logging.h"
#include "content/public/common/url_constants.h"
#include "googleurl/src/gurl.h" #include "googleurl/src/gurl.h"
#include "googleurl/src/url_canon.h"
namespace extensions { namespace extensions {
...@@ -88,19 +90,23 @@ namespace extensions { ...@@ -88,19 +90,23 @@ namespace extensions {
// Case 2: url_{prefix,suffix,equals,contains} searches. // Case 2: url_{prefix,suffix,equals,contains} searches.
// ===================================================== // =====================================================
// //
// Step 1: as above // Step 1: as above, except that
// - the scheme is not removed
// - the port is not removed if it is specified and does not match the default
// port for the given scheme.
// //
// Step 2: // Step 2:
// Translate URL to String and add the following position markers: // Translate URL to String and add the following position markers:
// - BU = Beginning of URL // - BU = Beginning of URL
// - EU = End of URL // - EU = End of URL
// Furthermore, the hostname is canonicalized to start with a ".".
// //
// -> www.example.com/index.html?search=foo becomes // -> http://www.example.com:8080/index.html?search=foo#first_match becomes
// BU .www.example.com/index.html?search=foo EU // BU http://www.example.com:8080/index.html?search=foo EU
// -> http://www.example.com:80/index.html?search=foo#first_match becomes
// BU http://www.example.com/index.html?search=foo EU
// //
// url_prefix(prefix) = BU add_missing_dot_prefix(prefix) // url_prefix(prefix) = BU prefix
// -> url_prefix("www.example") = BU .www.example // -> url_prefix("http://www.example") = BU http://www.example
// //
// url_contains(substring) = substring // url_contains(substring) = substring
// -> url_contains("index") = index // -> url_contains("index") = index
...@@ -113,7 +119,7 @@ namespace extensions { ...@@ -113,7 +119,7 @@ namespace extensions {
// by a combination of a url_contains() query followed by an explicit test: // by a combination of a url_contains() query followed by an explicit test:
// //
// host_contains(str) = url_contains(str) followed by test whether str occurs // host_contains(str) = url_contains(str) followed by test whether str occurs
// in host comonent of original URL. // in host component of original URL.
// -> host_contains("example.co") = example.co // -> host_contains("example.co") = example.co
// followed by gurl.host().find("example.co"); // followed by gurl.host().find("example.co");
// //
...@@ -160,7 +166,7 @@ bool URLMatcherCondition::operator<(const URLMatcherCondition& rhs) const { ...@@ -160,7 +166,7 @@ bool URLMatcherCondition::operator<(const URLMatcherCondition& rhs) const {
bool URLMatcherCondition::IsFullURLCondition() const { bool URLMatcherCondition::IsFullURLCondition() const {
// For these criteria the SubstringMatcher needs to be executed on the // For these criteria the SubstringMatcher needs to be executed on the
// GURL that is canonlizaliced with // GURL that is canonicalized with
// URLMatcherConditionFactory::CanonicalizeURLForFullSearches. // URLMatcherConditionFactory::CanonicalizeURLForFullSearches.
switch (criterion_) { switch (criterion_) {
case HOST_CONTAINS: case HOST_CONTAINS:
...@@ -314,14 +320,26 @@ URLMatcherConditionFactory::CreateHostEqualsPathPrefixCondition( ...@@ -314,14 +320,26 @@ URLMatcherConditionFactory::CreateHostEqualsPathPrefixCondition(
std::string URLMatcherConditionFactory::CanonicalizeURLForFullSearches( std::string URLMatcherConditionFactory::CanonicalizeURLForFullSearches(
const GURL& url) { const GURL& url) {
return kBeginningOfURL + CanonicalizeHostname(url.host()) + url.path() + GURL::Replacements replacements;
(url.has_query() ? "?" + url.query() : "") + kEndOfURL; replacements.ClearPassword();
replacements.ClearUsername();
replacements.ClearRef();
// Clear port if it is implicit from scheme.
if (url.has_port()) {
const std::string& port = url.scheme();
if (url_canon::DefaultPortForScheme(port.c_str(), port.size()) ==
url.EffectiveIntPort()) {
replacements.ClearPort();
}
}
return kBeginningOfURL + url.ReplaceComponents(replacements).spec() +
kEndOfURL;
} }
URLMatcherCondition URLMatcherConditionFactory::CreateURLPrefixCondition( URLMatcherCondition URLMatcherConditionFactory::CreateURLPrefixCondition(
const std::string& prefix) { const std::string& prefix) {
return CreateCondition(URLMatcherCondition::URL_PREFIX, return CreateCondition(URLMatcherCondition::URL_PREFIX,
kBeginningOfURL + CanonicalizeHostname(prefix)); kBeginningOfURL + prefix);
} }
URLMatcherCondition URLMatcherConditionFactory::CreateURLSuffixCondition( URLMatcherCondition URLMatcherConditionFactory::CreateURLSuffixCondition(
...@@ -337,7 +355,7 @@ URLMatcherCondition URLMatcherConditionFactory::CreateURLContainsCondition( ...@@ -337,7 +355,7 @@ URLMatcherCondition URLMatcherConditionFactory::CreateURLContainsCondition(
URLMatcherCondition URLMatcherConditionFactory::CreateURLEqualsCondition( URLMatcherCondition URLMatcherConditionFactory::CreateURLEqualsCondition(
const std::string& str) { const std::string& str) {
return CreateCondition(URLMatcherCondition::URL_EQUALS, return CreateCondition(URLMatcherCondition::URL_EQUALS,
kBeginningOfURL + CanonicalizeHostname(str) + kEndOfURL); kBeginningOfURL + str + kEndOfURL);
} }
void URLMatcherConditionFactory::ForgetUnusedPatterns( void URLMatcherConditionFactory::ForgetUnusedPatterns(
......
...@@ -230,7 +230,7 @@ TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) { ...@@ -230,7 +230,7 @@ TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
} }
TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" GURL gurl("https://www.google.com:1234/webhp?sourceid=chrome-instant&ie=UTF-8"
"&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
URLMatcherConditionFactory factory; URLMatcherConditionFactory factory;
std::string url = factory.CanonicalizeURLForComponentSearches(gurl); std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
...@@ -326,18 +326,21 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) { ...@@ -326,18 +326,21 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
} }
TEST(URLMatcherConditionFactoryTest, TestFullSearches) { TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8" // The Port 443 is stripped because it is the default port for https.
GURL gurl("https://www.google.com:443/webhp?sourceid=chrome-instant&ie=UTF-8"
"&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome"); "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
URLMatcherConditionFactory factory; URLMatcherConditionFactory factory;
std::string url = factory.CanonicalizeURLForFullSearches(gurl); std::string url = factory.CanonicalizeURLForFullSearches(gurl);
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(""), url)); EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(""), url));
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition("www.goog"), url)); EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition("www.google.com"), url)); "https://www.goog"), url));
EXPECT_TRUE( EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
Matches(factory.CreateURLPrefixCondition(".www.google.com"), url)); "https://www.google.com"), url));
EXPECT_TRUE( EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
Matches(factory.CreateURLPrefixCondition("www.google.com/"), url)); "https://www.google.com/webhp?"), url));
EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition(
"http://www.google.com"), url));
EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url)); EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url));
EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(""), url)); EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(""), url));
...@@ -346,18 +349,29 @@ TEST(URLMatcherConditionFactoryTest, TestFullSearches) { ...@@ -346,18 +349,29 @@ TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(""), url)); EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(""), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url)); EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("www.goog"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(".www.goog"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url)); EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("webhp"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url)); EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url)); EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url)); EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("ion=1"), url));
EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(".www.goog"), url));
EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url)); EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("foobar"), url));
EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url)); EXPECT_FALSE(Matches(factory.CreateURLContainsCondition("search"), url));
EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url));
EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition( EXPECT_TRUE(Matches(factory.CreateURLEqualsCondition(
"www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), url)); "https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"),
url));
EXPECT_FALSE( EXPECT_FALSE(
Matches(factory.CreateURLEqualsCondition("www.google.com"), url)); Matches(factory.CreateURLEqualsCondition("https://www.google.com"), url));
// Same as above but this time with a non-standard port.
gurl = GURL("https://www.google.com:1234/webhp?sourceid=chrome-instant&"
"ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20"
"awesome");
url = factory.CanonicalizeURLForFullSearches(gurl);
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
"https://www.google.com:1234/webhp?"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition(":1234"), url));
} }
......
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