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 @@
},
"urlContains": {
"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
},
"urlEquals": {
"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
},
"urlPrefix": {
"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
},
"urlSuffix": {
"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
},
"schemes": {
......
......@@ -1265,7 +1265,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......@@ -1289,7 +1289,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......@@ -1313,7 +1313,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......@@ -1337,7 +1337,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......
......@@ -1395,7 +1395,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......@@ -1419,7 +1419,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......@@ -1443,7 +1443,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......@@ -1467,7 +1467,7 @@ refers to an array of rules including filled optional parameters.
</div>
</em>
</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 METHODS -->
<!-- OBJECT EVENT FIELDS -->
......
......@@ -8,7 +8,9 @@
#include <iterator>
#include "base/logging.h"
#include "content/public/common/url_constants.h"
#include "googleurl/src/gurl.h"
#include "googleurl/src/url_canon.h"
namespace extensions {
......@@ -88,19 +90,23 @@ namespace extensions {
// 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:
// Translate URL to String and add the following position markers:
// - BU = Beginning of URL
// - EU = End of URL
// Furthermore, the hostname is canonicalized to start with a ".".
//
// -> www.example.com/index.html?search=foo becomes
// BU .www.example.com/index.html?search=foo EU
// -> http://www.example.com:8080/index.html?search=foo#first_match becomes
// 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("www.example") = BU .www.example
// url_prefix(prefix) = BU prefix
// -> url_prefix("http://www.example") = BU http://www.example
//
// url_contains(substring) = substring
// -> url_contains("index") = index
......@@ -113,7 +119,7 @@ namespace extensions {
// 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
// in host comonent of original URL.
// in host component of original URL.
// -> host_contains("example.co") = example.co
// followed by gurl.host().find("example.co");
//
......@@ -160,7 +166,7 @@ bool URLMatcherCondition::operator<(const URLMatcherCondition& rhs) const {
bool URLMatcherCondition::IsFullURLCondition() const {
// For these criteria the SubstringMatcher needs to be executed on the
// GURL that is canonlizaliced with
// GURL that is canonicalized with
// URLMatcherConditionFactory::CanonicalizeURLForFullSearches.
switch (criterion_) {
case HOST_CONTAINS:
......@@ -314,14 +320,26 @@ URLMatcherConditionFactory::CreateHostEqualsPathPrefixCondition(
std::string URLMatcherConditionFactory::CanonicalizeURLForFullSearches(
const GURL& url) {
return kBeginningOfURL + CanonicalizeHostname(url.host()) + url.path() +
(url.has_query() ? "?" + url.query() : "") + kEndOfURL;
GURL::Replacements replacements;
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(
const std::string& prefix) {
return CreateCondition(URLMatcherCondition::URL_PREFIX,
kBeginningOfURL + CanonicalizeHostname(prefix));
kBeginningOfURL + prefix);
}
URLMatcherCondition URLMatcherConditionFactory::CreateURLSuffixCondition(
......@@ -337,7 +355,7 @@ URLMatcherCondition URLMatcherConditionFactory::CreateURLContainsCondition(
URLMatcherCondition URLMatcherConditionFactory::CreateURLEqualsCondition(
const std::string& str) {
return CreateCondition(URLMatcherCondition::URL_EQUALS,
kBeginningOfURL + CanonicalizeHostname(str) + kEndOfURL);
kBeginningOfURL + str + kEndOfURL);
}
void URLMatcherConditionFactory::ForgetUnusedPatterns(
......
......@@ -230,7 +230,7 @@ TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
}
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");
URLMatcherConditionFactory factory;
std::string url = factory.CanonicalizeURLForComponentSearches(gurl);
......@@ -326,18 +326,21 @@ TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
}
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");
URLMatcherConditionFactory factory;
std::string url = factory.CanonicalizeURLForFullSearches(gurl);
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(""), url));
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition("www.goog"), url));
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition("www.google.com"), url));
EXPECT_TRUE(
Matches(factory.CreateURLPrefixCondition(".www.google.com"), url));
EXPECT_TRUE(
Matches(factory.CreateURLPrefixCondition("www.google.com/"), url));
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
"https://www.goog"), url));
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
"https://www.google.com"), url));
EXPECT_TRUE(Matches(factory.CreateURLPrefixCondition(
"https://www.google.com/webhp?"), url));
EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition(
"http://www.google.com"), url));
EXPECT_FALSE(Matches(factory.CreateURLPrefixCondition("webhp"), url));
EXPECT_TRUE(Matches(factory.CreateURLSuffixCondition(""), url));
......@@ -346,18 +349,29 @@ TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
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("webhp"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("?"), url));
EXPECT_TRUE(Matches(factory.CreateURLContainsCondition("sourceid"), 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("search"), url));
EXPECT_FALSE(Matches(factory.CreateURLContainsCondition(":443"), url));
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(
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