summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authorjstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-13 15:03:47 +0000
committerjstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-13 15:03:47 +0000
commit06e8b8ff0c9313a90243012961601ab63e89de1e (patch)
tree551c4198944f30c223acd905a0ba5874ff6f810f /chrome/common
parent613ccd6ad9e76e63dd76a579ed3a6b04fb13025b (diff)
downloadchromium_src-06e8b8ff0c9313a90243012961601ab63e89de1e.zip
chromium_src-06e8b8ff0c9313a90243012961601ab63e89de1e.tar.gz
chromium_src-06e8b8ff0c9313a90243012961601ab63e89de1e.tar.bz2
Update URLPatternSet to contain a std::set instead of std::vector.
This updates URLPatternSet to contain a std::set instead of a std::vector, making it easier to implement the set operations in ExtensionPermissionSet. BUG=84507 TEST=unit_tests Review URL: http://codereview.chromium.org/7347011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92364 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/extensions/extension.cc12
-rw-r--r--chrome/common/extensions/extension.h16
-rw-r--r--chrome/common/extensions/extension_manifests_unittest.cc27
-rw-r--r--chrome/common/extensions/extension_messages.cc9
-rw-r--r--chrome/common/extensions/extension_permission_set.cc70
-rw-r--r--chrome/common/extensions/extension_permission_set.h8
-rw-r--r--chrome/common/extensions/extension_permission_set_unittest.cc91
-rw-r--r--chrome/common/extensions/extension_unittest.cc2
-rw-r--r--chrome/common/extensions/file_browser_handler.h4
-rw-r--r--chrome/common/extensions/url_pattern.cc50
-rw-r--r--chrome/common/extensions/url_pattern.h59
-rw-r--r--chrome/common/extensions/url_pattern_set.cc40
-rw-r--r--chrome/common/extensions/url_pattern_set.h15
-rw-r--r--chrome/common/extensions/url_pattern_set_unittest.cc25
-rw-r--r--chrome/common/extensions/url_pattern_unittest.cc94
-rw-r--r--chrome/common/extensions/user_script.cc10
-rw-r--r--chrome/common/extensions/user_script.h7
-rw-r--r--chrome/common/extensions/user_script_unittest.cc7
18 files changed, 317 insertions, 229 deletions
diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc
index c0ba929..0d477803 100644
--- a/chrome/common/extensions/extension.cc
+++ b/chrome/common/extensions/extension.cc
@@ -481,7 +481,7 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script,
URLPattern pattern(UserScript::kValidUserScriptSchemes);
if (CanExecuteScriptEverywhere())
- pattern.set_valid_schemes(URLPattern::SCHEME_ALL);
+ pattern.SetValidSchemes(URLPattern::SCHEME_ALL);
URLPattern::ParseResult parse_result = pattern.Parse(match_str,
parse_strictness);
@@ -498,7 +498,7 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script,
!CanExecuteScriptEverywhere()) {
wants_file_access_ = true;
if (!(flags & ALLOW_FILE_ACCESS))
- pattern.set_valid_schemes(
+ pattern.SetValidSchemes(
pattern.valid_schemes() & ~URLPattern::SCHEME_FILE);
}
@@ -528,7 +528,7 @@ bool Extension::LoadUserScriptHelper(const DictionaryValue* content_script,
URLPattern pattern(UserScript::kValidUserScriptSchemes);
if (CanExecuteScriptEverywhere())
- pattern.set_valid_schemes(URLPattern::SCHEME_ALL);
+ pattern.SetValidSchemes(URLPattern::SCHEME_ALL);
URLPattern::ParseResult parse_result = pattern.Parse(match_str,
parse_strictness);
if (parse_result != URLPattern::PARSE_SUCCESS) {
@@ -1049,7 +1049,7 @@ bool Extension::LoadLaunchURL(const DictionaryValue* manifest,
*error = errors::kInvalidLaunchWebURL;
return false;
}
- pattern.set_host(launch_url.host());
+ pattern.SetHost(launch_url.host());
pattern.SetPath("/*");
extent_.AddPattern(pattern);
}
@@ -1976,7 +1976,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, int flags,
!CanExecuteScriptEverywhere()) {
wants_file_access_ = true;
if (!(flags & ALLOW_FILE_ACCESS))
- pattern.set_valid_schemes(
+ pattern.SetValidSchemes(
pattern.valid_schemes() & ~URLPattern::SCHEME_FILE);
}
@@ -2725,7 +2725,7 @@ bool Extension::OverlapsWithOrigin(const GURL& origin) const {
URLPattern origin_only_pattern(kValidWebExtentSchemes);
if (!origin_only_pattern.SetScheme(origin.scheme()))
return false;
- origin_only_pattern.set_host(origin.host());
+ origin_only_pattern.SetHost(origin.host());
origin_only_pattern.SetPath("/*");
URLPatternSet origin_only_pattern_list;
diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h
index 517ebdb..dcbcedc 100644
--- a/chrome/common/extensions/extension.h
+++ b/chrome/common/extensions/extension.h
@@ -544,22 +544,6 @@ class Extension : public base::RefCountedThreadSafe<Extension> {
// sure the drive letter is uppercase.
static FilePath MaybeNormalizePath(const FilePath& path);
- // Returns the distinct hosts that can be displayed in the install UI or be
- // used for privilege comparisons. This discards some of the detail that is
- // present in the manifest to make it as easy as possible to process by users.
- // In particular we disregard the scheme and path components of URLPatterns
- // and de-dupe the result, which includes filtering out common hosts with
- // differing RCDs. If |include_rcd| is true, then the de-duped result
- // will be the first full entry, including its RCD. So if the list was
- // "*.google.co.uk" and "*.google.com", the returned value would just be
- // "*.google.co.uk". Keeping the RCD in the result is useful for display
- // purposes when you want to show the user one sample hostname from the list.
- // If you need to compare two URLPatternLists for security equality, then set
- // |include_rcd| to false, which will return a result like "*.google.",
- // regardless of the order of the patterns.
- static std::vector<std::string> GetDistinctHosts(
- const URLPatternList& host_patterns, bool include_rcd);
-
// Returns true if this extension id is from a trusted provider.
static bool IsTrustedId(const std::string& id);
diff --git a/chrome/common/extensions/extension_manifests_unittest.cc b/chrome/common/extensions/extension_manifests_unittest.cc
index 1184d52..3209ba5 100644
--- a/chrome/common/extensions/extension_manifests_unittest.cc
+++ b/chrome/common/extensions/extension_manifests_unittest.cc
@@ -30,6 +30,16 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
+
+namespace {
+
+static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
+ int schemes = URLPattern::SCHEME_ALL;
+ extent->AddPattern(URLPattern(schemes, pattern));
+}
+
+}
+
namespace errors = extension_manifest_errors;
namespace keys = extension_manifest_keys;
@@ -302,11 +312,10 @@ TEST_F(ExtensionManifestTest, OldUnlimitedStoragePermission) {
TEST_F(ExtensionManifestTest, ValidApp) {
scoped_refptr<Extension> extension(LoadAndExpectSuccess("valid_app.json"));
- ASSERT_EQ(2u, extension->web_extent().patterns().size());
- EXPECT_EQ("http://www.google.com/mail/*",
- extension->web_extent().patterns()[0].GetAsString());
- EXPECT_EQ("http://www.google.com/foobar/*",
- extension->web_extent().patterns()[1].GetAsString());
+ URLPatternSet expected_patterns;
+ AddPattern(&expected_patterns, "http://www.google.com/mail/*");
+ AddPattern(&expected_patterns, "http://www.google.com/foobar/*");
+ EXPECT_EQ(expected_patterns, extension->web_extent());
EXPECT_EQ(extension_misc::LAUNCH_TAB, extension->launch_container());
EXPECT_EQ("http://www.google.com/mail/", extension->launch_web_url());
}
@@ -365,7 +374,7 @@ TEST_F(ExtensionManifestTest, AppWebUrls) {
LoadAndExpectSuccess("web_urls_default.json"));
ASSERT_EQ(1u, extension->web_extent().patterns().size());
EXPECT_EQ("*://www.google.com/*",
- extension->web_extent().patterns()[0].GetAsString());
+ extension->web_extent().patterns().begin()->GetAsString());
}
TEST_F(ExtensionManifestTest, AppLaunchContainer) {
@@ -692,7 +701,7 @@ TEST_F(ExtensionManifestTest, DefaultPathForExtent) {
LoadAndExpectSuccess("default_path_for_extent.json"));
ASSERT_EQ(1u, extension->web_extent().patterns().size());
- EXPECT_EQ("/*", extension->web_extent().patterns()[0].path());
+ EXPECT_EQ("/*", extension->web_extent().patterns().begin()->path());
EXPECT_TRUE(extension->web_extent().MatchesURL(
GURL("http://www.google.com/monkey")));
}
@@ -790,8 +799,8 @@ TEST_F(ExtensionManifestTest, FileBrowserHandlers) {
extension->file_browser_handlers()->at(0).get();
EXPECT_EQ(action->title(), "Default title");
EXPECT_EQ(action->icon_path(), "icon.png");
- const URLPatternList& patterns = action->file_url_patterns();
- ASSERT_EQ(patterns.size(), 1U);
+ const URLPatternSet& patterns = action->file_url_patterns();
+ ASSERT_EQ(patterns.patterns().size(), 1U);
ASSERT_TRUE(action->MatchesURL(
GURL("filesystem:chrome-extension://foo/local/test.txt")));
}
diff --git a/chrome/common/extensions/extension_messages.cc b/chrome/common/extensions/extension_messages.cc
index 268d06f..587965f 100644
--- a/chrome/common/extensions/extension_messages.cc
+++ b/chrome/common/extensions/extension_messages.cc
@@ -99,7 +99,7 @@ bool ParamTraits<URLPattern>::Read(const Message* m, void** iter,
!ReadParam(m, iter, &spec))
return false;
- p->set_valid_schemes(valid_schemes);
+ p->SetValidSchemes(valid_schemes);
return URLPattern::PARSE_SUCCESS == p->Parse(spec, URLPattern::IGNORE_PORTS);
}
@@ -113,14 +113,15 @@ void ParamTraits<URLPatternSet>::Write(Message* m, const param_type& p) {
bool ParamTraits<URLPatternSet>::Read(const Message* m, void** iter,
param_type* p) {
- URLPatternList patterns;
+ std::set<URLPattern> patterns;
bool success =
ReadParam(m, iter, &patterns);
if (!success)
return false;
- for (size_t i = 0; i < patterns.size(); ++i)
- p->AddPattern(patterns[i]);
+ for (std::set<URLPattern>::iterator i = patterns.begin();
+ i != patterns.end(); ++i)
+ p->AddPattern(*i);
return true;
}
diff --git a/chrome/common/extensions/extension_permission_set.cc b/chrome/common/extensions/extension_permission_set.cc
index c829c10..91c2238 100644
--- a/chrome/common/extensions/extension_permission_set.cc
+++ b/chrome/common/extensions/extension_permission_set.cc
@@ -81,9 +81,8 @@ const char kWindowsPermission[] = "windows";
void AddPatternsAndRemovePaths(const URLPatternSet& set, URLPatternSet* out) {
CHECK(out);
- const URLPatternList& patterns = set.patterns();
- for (size_t i = 0; i < patterns.size(); ++i) {
- URLPattern p = patterns.at(i);
+ for (URLPatternSet::const_iterator i = set.begin(); i != set.end(); ++i) {
+ URLPattern p = *i;
p.SetPath("/*");
out->AddPattern(p);
}
@@ -97,35 +96,37 @@ void AddPatternsAndRemovePaths(const URLPatternSet& set, URLPatternSet* out) {
// static
ExtensionPermissionMessage ExtensionPermissionMessage::CreateFromHostList(
- const std::vector<std::string>& hosts) {
- CHECK(hosts.size() > 0);
+ const std::set<std::string>& hosts) {
+ std::vector<std::string> host_list(hosts.begin(), hosts.end());
+ CHECK(host_list.size() > 0);
ID message_id;
string16 message;
- switch (hosts.size()) {
+
+ switch (host_list.size()) {
case 1:
message_id = kHosts1;
message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_1_HOST,
- UTF8ToUTF16(hosts[0]));
+ UTF8ToUTF16(host_list[0]));
break;
case 2:
message_id = kHosts2;
message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_2_HOSTS,
- UTF8ToUTF16(hosts[0]),
- UTF8ToUTF16(hosts[1]));
+ UTF8ToUTF16(host_list[0]),
+ UTF8ToUTF16(host_list[1]));
break;
case 3:
message_id = kHosts3;
message = l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_3_HOSTS,
- UTF8ToUTF16(hosts[0]),
- UTF8ToUTF16(hosts[1]),
- UTF8ToUTF16(hosts[2]));
+ UTF8ToUTF16(host_list[0]),
+ UTF8ToUTF16(host_list[1]),
+ UTF8ToUTF16(host_list[2]));
break;
default:
message_id = kHosts4OrMore;
message = l10n_util::GetStringFUTF16(
IDS_EXTENSION_PROMPT_WARNING_4_OR_MORE_HOSTS,
- UTF8ToUTF16(hosts[0]),
- UTF8ToUTF16(hosts[1]),
+ UTF8ToUTF16(host_list[0]),
+ UTF8ToUTF16(host_list[1]),
base::IntToString16(hosts.size() - 2));
break;
}
@@ -458,9 +459,9 @@ std::set<std::string> ExtensionPermissionSet::GetAPIsAsStrings() const {
return apis_str;
}
-std::vector<std::string>
+std::set<std::string>
ExtensionPermissionSet::GetDistinctHostsForDisplay() const {
- return GetDistinctHosts(effective_hosts_.patterns(), true);
+ return GetDistinctHosts(effective_hosts_, true);
}
ExtensionPermissionMessages
@@ -479,7 +480,7 @@ ExtensionPermissionMessages
ExtensionPermissionMessage::kHostsAll,
l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS)));
} else {
- std::vector<std::string> hosts = GetDistinctHostsForDisplay();
+ std::set<std::string> hosts = GetDistinctHostsForDisplay();
if (!hosts.empty())
messages.push_back(ExtensionPermissionMessage::CreateFromHostList(hosts));
}
@@ -565,9 +566,8 @@ bool ExtensionPermissionSet::HasEffectiveAccessToAllHosts() const {
// There are two ways this set can have effective access to all hosts:
// 1) it has an <all_urls> URL pattern.
// 2) it has a named permission with implied full URL access.
- const URLPatternList patterns = effective_hosts().patterns();
- for (URLPatternList::const_iterator host = patterns.begin();
- host != patterns.end(); ++host) {
+ for (URLPatternSet::const_iterator host = effective_hosts().begin();
+ host != effective_hosts().end(); ++host) {
if (host->match_all_urls() ||
(host->match_subdomains() && host->host().empty()))
return true;
@@ -630,18 +630,19 @@ bool ExtensionPermissionSet::HasLessPrivilegesThan(
}
// static
-std::vector<std::string> ExtensionPermissionSet::GetDistinctHosts(
- const URLPatternList& host_patterns, bool include_rcd) {
+std::set<std::string> ExtensionPermissionSet::GetDistinctHosts(
+ const URLPatternSet& host_patterns, bool include_rcd) {
// Use a vector to preserve order (also faster than a map on small sets).
// Each item is a host split into two parts: host without RCDs and
// current best RCD.
typedef std::vector<std::pair<std::string, std::string> > HostVector;
HostVector hosts_best_rcd;
- for (size_t i = 0; i < host_patterns.size(); ++i) {
- std::string host = host_patterns[i].host();
+ for (URLPatternSet::const_iterator i = host_patterns.begin();
+ i != host_patterns.end(); ++i) {
+ std::string host = i->host();
// Add the subdomain wildcard back to the host, if necessary.
- if (host_patterns[i].match_subdomains())
+ if (i->match_subdomains())
host = "*." + host;
// If the host has an RCD, split it off so we can detect duplicates.
@@ -670,10 +671,10 @@ std::vector<std::string> ExtensionPermissionSet::GetDistinctHosts(
}
// Build up the final vector by concatenating hosts and RCDs.
- std::vector<std::string> distinct_hosts;
+ std::set<std::string> distinct_hosts;
for (HostVector::iterator it = hosts_best_rcd.begin();
it != hosts_best_rcd.end(); ++it)
- distinct_hosts.push_back(it->first + it->second);
+ distinct_hosts.insert(it->first + it->second);
return distinct_hosts;
}
@@ -682,7 +683,7 @@ void ExtensionPermissionSet::InitEffectiveHosts() {
if (HasEffectiveAccessToAllHosts()) {
URLPattern all_urls(URLPattern::SCHEME_ALL);
- all_urls.set_match_all_urls(true);
+ all_urls.SetMatchAllURLs(true);
effective_hosts_.AddPattern(all_urls);
return;
}
@@ -704,7 +705,7 @@ void ExtensionPermissionSet::InitImplicitExtensionPermissions(
for (UserScriptList::const_iterator content_script =
extension->content_scripts().begin();
content_script != extension->content_scripts().end(); ++content_script) {
- URLPatternList::const_iterator pattern =
+ URLPatternSet::const_iterator pattern =
content_script->url_patterns().begin();
for (; pattern != content_script->url_patterns().end(); ++pattern)
scriptable_hosts_.AddPattern(*pattern);
@@ -761,17 +762,14 @@ bool ExtensionPermissionSet::HasLessHostPrivilegesThan(
if (permissions->HasEffectiveAccessToAllHosts())
return true;
- const URLPatternList old_list = effective_hosts().patterns();
- const URLPatternList new_list = permissions->effective_hosts().patterns();
+ const URLPatternSet& old_list = effective_hosts();
+ const URLPatternSet& new_list = permissions->effective_hosts();
// TODO(jstritar): This is overly conservative with respect to subdomains.
// For example, going from *.google.com to www.google.com will be
// considered an elevation, even though it is not (http://crbug.com/65337).
- std::vector<std::string> new_hosts = GetDistinctHosts(new_list, false);
- std::vector<std::string> old_hosts = GetDistinctHosts(old_list, false);
-
- std::set<std::string> old_hosts_set(old_hosts.begin(), old_hosts.end());
- std::set<std::string> new_hosts_set(new_hosts.begin(), new_hosts.end());
+ std::set<std::string> new_hosts_set = GetDistinctHosts(new_list, false);
+ std::set<std::string> old_hosts_set = GetDistinctHosts(old_list, false);
std::set<std::string> new_hosts_only;
std::set_difference(new_hosts_set.begin(), new_hosts_set.end(),
diff --git a/chrome/common/extensions/extension_permission_set.h b/chrome/common/extensions/extension_permission_set.h
index 3ed0451..7aa39e7 100644
--- a/chrome/common/extensions/extension_permission_set.h
+++ b/chrome/common/extensions/extension_permission_set.h
@@ -51,7 +51,7 @@ class ExtensionPermissionMessage {
// simply a convenience method around the constructor, since the messages
// change depending on what hosts are present.
static ExtensionPermissionMessage CreateFromHostList(
- const std::vector<std::string>& hosts);
+ const std::set<std::string>& hosts);
// Creates the corresponding permission message.
ExtensionPermissionMessage(ID id, const string16& message);
@@ -293,7 +293,7 @@ class ExtensionPermissionSet {
// Gets a list of the distinct hosts for displaying to the user.
// NOTE: do not use this for comparing permissions, since this disgards some
// information.
- std::vector<std::string> GetDistinctHostsForDisplay() const;
+ std::set<std::string> GetDistinctHostsForDisplay() const;
// Gets the localized permission messages that represent this set.
ExtensionPermissionMessages GetPermissionMessages() const;
@@ -349,8 +349,8 @@ class ExtensionPermissionSet {
FRIEND_TEST_ALL_PREFIXES(ExtensionPermissionSetTest,
HasLessHostPrivilegesThan);
- static std::vector<std::string> GetDistinctHosts(
- const URLPatternList& host_patterns, bool include_rcd);
+ static std::set<std::string> GetDistinctHosts(
+ const URLPatternSet& host_patterns, bool include_rcd);
// Initializes the set based on |extension|'s manifest data.
void InitImplicitExtensionPermissions(const Extension* extension);
diff --git a/chrome/common/extensions/extension_permission_set_unittest.cc b/chrome/common/extensions/extension_permission_set_unittest.cc
index 75e12fa..8110487 100644
--- a/chrome/common/extensions/extension_permission_set_unittest.cc
+++ b/chrome/common/extensions/extension_permission_set_unittest.cc
@@ -58,23 +58,6 @@ static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
extent->AddPattern(URLPattern(schemes, pattern));
}
-static void AssertEqualExtents(const URLPatternSet& extent1,
- const URLPatternSet& extent2) {
- URLPatternList patterns1 = extent1.patterns();
- URLPatternList patterns2 = extent2.patterns();
- std::set<std::string> strings1;
- EXPECT_EQ(patterns1.size(), patterns2.size());
-
- for (size_t i = 0; i < patterns1.size(); ++i)
- strings1.insert(patterns1.at(i).GetAsString());
-
- std::set<std::string> strings2;
- for (size_t i = 0; i < patterns2.size(); ++i)
- strings2.insert(patterns2.at(i).GetAsString());
-
- EXPECT_EQ(strings1, strings2);
-}
-
} // namespace
class ExtensionAPIPermissionTest : public testing::Test {
@@ -338,9 +321,9 @@ TEST(ExtensionPermissionSetTest, CreateUnion) {
EXPECT_FALSE(union_set->HasEffectiveFullAccess());
EXPECT_EQ(expected_apis, union_set->apis());
- AssertEqualExtents(expected_explicit_hosts, union_set->explicit_hosts());
- AssertEqualExtents(expected_scriptable_hosts, union_set->scriptable_hosts());
- AssertEqualExtents(expected_explicit_hosts, union_set->effective_hosts());
+ EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
+ EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
+ EXPECT_EQ(expected_explicit_hosts, union_set->effective_hosts());
// Now use a real second set.
apis2.insert(ExtensionAPIPermission::kTab);
@@ -366,9 +349,9 @@ TEST(ExtensionPermissionSetTest, CreateUnion) {
EXPECT_TRUE(union_set->HasEffectiveFullAccess());
EXPECT_TRUE(union_set->HasEffectiveAccessToAllHosts());
EXPECT_EQ(expected_apis, union_set->apis());
- AssertEqualExtents(expected_explicit_hosts, union_set->explicit_hosts());
- AssertEqualExtents(expected_scriptable_hosts, union_set->scriptable_hosts());
- AssertEqualExtents(effective_hosts, union_set->effective_hosts());
+ EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
+ EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
+ EXPECT_EQ(effective_hosts, union_set->effective_hosts());
}
TEST(ExtensionPermissionSetTest, HasLessPrivilegesThan) {
@@ -567,7 +550,7 @@ TEST(ExtensionPermissionSetTest, GetWarningMessages_ManyHosts) {
std::vector<string16> warnings =
extension->permission_set()->GetWarningMessages();
ASSERT_EQ(1u, warnings.size());
- EXPECT_EQ("Your data on www.google.com and encrypted.google.com",
+ EXPECT_EQ("Your data on encrypted.google.com and www.google.com",
UTF16ToUTF8(warnings[0]));
}
@@ -592,10 +575,10 @@ TEST(ExtensionPermissionSetTest, GetWarningMessages_Plugins) {
TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
scoped_ptr<ExtensionPermissionSet> perm_set;
ExtensionAPIPermissionSet empty_perms;
- std::vector<std::string> expected;
- expected.push_back("www.foo.com");
- expected.push_back("www.bar.com");
- expected.push_back("www.baz.com");
+ std::set<std::string> expected;
+ expected.insert("www.foo.com");
+ expected.insert("www.bar.com");
+ expected.insert("www.baz.com");
URLPatternSet explicit_hosts;
URLPatternSet scriptable_hosts;
@@ -611,7 +594,7 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -624,7 +607,7 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -635,7 +618,7 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path"));
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -646,7 +629,7 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath"));
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -658,12 +641,12 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
explicit_hosts.AddPattern(
URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path"));
- expected.push_back("monkey.www.bar.com");
- expected.push_back("bar.com");
+ expected.insert("monkey.www.bar.com");
+ expected.insert("bar.com");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -690,11 +673,11 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
explicit_hosts.AddPattern(
URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
- expected.push_back("www.foo.xyzzy");
+ expected.insert("www.foo.xyzzy");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -703,11 +686,11 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
explicit_hosts.AddPattern(
URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
- expected.push_back("*.google.com");
+ expected.insert("*.google.com");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
{
@@ -721,12 +704,12 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
scriptable_hosts.AddPattern(
URLPattern(URLPattern::SCHEME_HTTP, "http://*.example.com/*"));
- expected.push_back("*.google.com");
- expected.push_back("*.example.com");
+ expected.insert("*.google.com");
+ expected.insert("*.example.com");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
}
@@ -748,11 +731,11 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay_ComIsBestRcd) {
explicit_hosts.AddPattern(
URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
- std::vector<std::string> expected;
- expected.push_back("www.foo.com");
+ std::set<std::string> expected;
+ expected.insert("www.foo.com");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay_NetIs2ndBestRcd) {
@@ -772,11 +755,11 @@ TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay_NetIs2ndBestRcd) {
URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
// No http://www.foo.com/path
- std::vector<std::string> expected;
- expected.push_back("www.foo.net");
+ std::set<std::string> expected;
+ expected.insert("www.foo.net");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
TEST(ExtensionPermissionSetTest,
@@ -796,11 +779,11 @@ TEST(ExtensionPermissionSetTest,
URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
// No http://www.foo.com/path
- std::vector<std::string> expected;
- expected.push_back("www.foo.org");
+ std::set<std::string> expected;
+ expected.insert("www.foo.org");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
TEST(ExtensionPermissionSetTest,
@@ -819,11 +802,11 @@ TEST(ExtensionPermissionSetTest,
URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
// No http://www.foo.com/path
- std::vector<std::string> expected;
- expected.push_back("www.foo.ca");
+ std::set<std::string> expected;
+ expected.insert("www.foo.ca");
perm_set.reset(new ExtensionPermissionSet(
empty_perms, explicit_hosts, scriptable_hosts));
- CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
+ EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
}
TEST(ExtensionPermissionSetTest, HasLessHostPrivilegesThan) {
diff --git a/chrome/common/extensions/extension_unittest.cc b/chrome/common/extensions/extension_unittest.cc
index aa8555c..9ad2a60 100644
--- a/chrome/common/extensions/extension_unittest.cc
+++ b/chrome/common/extensions/extension_unittest.cc
@@ -551,7 +551,7 @@ TEST(ExtensionTest, GetHostPermissionMessages_ManyHosts) {
extension = LoadManifest("permissions", "many-hosts.json");
std::vector<string16> warnings = extension->GetPermissionMessageStrings();
ASSERT_EQ(1u, warnings.size());
- EXPECT_EQ("Your data on www.google.com and encrypted.google.com",
+ EXPECT_EQ("Your data on encrypted.google.com and www.google.com",
UTF16ToUTF8(warnings[0]));
}
diff --git a/chrome/common/extensions/file_browser_handler.h b/chrome/common/extensions/file_browser_handler.h
index 74e478c..d407bf89 100644
--- a/chrome/common/extensions/file_browser_handler.h
+++ b/chrome/common/extensions/file_browser_handler.h
@@ -37,8 +37,8 @@ class FileBrowserHandler {
void set_title(const std::string& title) { title_ = title; }
// File schema URL patterns.
- const URLPatternList& file_url_patterns() const {
- return url_set_.patterns();
+ const URLPatternSet& file_url_patterns() const {
+ return url_set_;
}
void AddPattern(const URLPattern& pattern);
bool MatchesURL(const GURL& url) const;
diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc
index af0fd0a..6731ab8 100644
--- a/chrome/common/extensions/url_pattern.cc
+++ b/chrome/common/extensions/url_pattern.cc
@@ -124,8 +124,18 @@ URLPattern::URLPattern(int valid_schemes, const std::string& pattern)
URLPattern::~URLPattern() {
}
+bool URLPattern::operator<(const URLPattern& other) const {
+ return GetAsString() < other.GetAsString();
+}
+
+bool URLPattern::operator==(const URLPattern& other) const {
+ return GetAsString() == other.GetAsString();
+}
+
URLPattern::ParseResult URLPattern::Parse(const std::string& pattern,
ParseOption strictness) {
+ spec_.clear();
+
// Special case pattern to match every valid URL.
if (pattern == kAllUrlsPattern) {
match_all_urls_ = true;
@@ -234,7 +244,28 @@ URLPattern::ParseResult URLPattern::Parse(const std::string& pattern,
return PARSE_SUCCESS;
}
+void URLPattern::SetValidSchemes(int valid_schemes) {
+ spec_.clear();
+ valid_schemes_ = valid_schemes;
+}
+
+void URLPattern::SetHost(const std::string& host) {
+ spec_.clear();
+ host_ = host;
+}
+
+void URLPattern::SetMatchAllURLs(bool val) {
+ spec_.clear();
+ match_all_urls_ = val;
+}
+
+void URLPattern::SetMatchSubdomains(bool val) {
+ spec_.clear();
+ match_subdomains_ = val;
+}
+
bool URLPattern::SetScheme(const std::string& scheme) {
+ spec_.clear();
scheme_ = scheme;
if (scheme_ == "*") {
valid_schemes_ &= (SCHEME_HTTP | SCHEME_HTTPS);
@@ -257,6 +288,7 @@ bool URLPattern::IsValidScheme(const std::string& scheme) const {
}
void URLPattern::SetPath(const std::string& path) {
+ spec_.clear();
path_ = path;
path_escaped_ = path_;
ReplaceSubstringsAfterOffset(&path_escaped_, 0, "\\", "\\\\");
@@ -264,6 +296,7 @@ void URLPattern::SetPath(const std::string& path) {
}
bool URLPattern::SetPort(const std::string& port) {
+ spec_.clear();
if (IsValidPortForScheme(scheme_, port)) {
port_ = port;
return true;
@@ -351,9 +384,15 @@ bool URLPattern::MatchesPort(int port) const {
return port_ == "*" || port_ == base::IntToString(port);
}
-std::string URLPattern::GetAsString() const {
- if (match_all_urls_)
- return kAllUrlsPattern;
+
+const std::string& URLPattern::GetAsString() const {
+ if (!spec_.empty())
+ return spec_;
+
+ if (match_all_urls_) {
+ spec_ = kAllUrlsPattern;
+ return spec_;
+ }
bool standard_scheme = IsStandardScheme(scheme_);
@@ -379,7 +418,8 @@ std::string URLPattern::GetAsString() const {
if (!path_.empty())
spec += path_;
- return spec;
+ spec_ = spec;
+ return spec_;
}
bool URLPattern::OverlapsWith(const URLPattern& other) const {
@@ -445,7 +485,7 @@ std::vector<URLPattern> URLPattern::ConvertToExplicitSchemes() const {
i != explicit_schemes.end(); ++i) {
URLPattern temp = *this;
temp.SetScheme(*i);
- temp.set_match_all_urls(false);
+ temp.SetMatchAllURLs(false);
result.push_back(temp);
}
diff --git a/chrome/common/extensions/url_pattern.h b/chrome/common/extensions/url_pattern.h
index 113d266..c357b2d 100644
--- a/chrome/common/extensions/url_pattern.h
+++ b/chrome/common/extensions/url_pattern.h
@@ -132,28 +132,40 @@ class URLPattern {
// Parse() instead, which returns success or failure.
URLPattern(int valid_schemes, const std::string& pattern);
-#if defined(_MSC_VER) && _MSC_VER >= 1600
// Note: don't use this directly. This exists so URLPattern can be used
- // with STL containers. Starting with Visual Studio 2010, we can't have this
- // method private and use "friend class std::vector<URLPattern>;" as we used
- // to do.
+ // with STL containers.
URLPattern();
-#endif
-
~URLPattern();
+ bool operator<(const URLPattern& other) const;
+ bool operator==(const URLPattern& other) const;
+
+ // 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);
+
// Gets the bitmask of valid schemes.
int valid_schemes() const { return valid_schemes_; }
- void set_valid_schemes(int valid_schemes) { valid_schemes_ = valid_schemes; }
+ void SetValidSchemes(int valid_schemes);
// 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_; }
- void set_host(const std::string& host) { host_ = host; }
+ void SetHost(const std::string& host);
// Gets whether to match subdomains of host().
bool match_subdomains() const { return match_subdomains_; }
- void set_match_subdomains(bool val) { match_subdomains_ = val; }
+ void SetMatchSubdomains(bool val);
// Gets the path the pattern matches with the leading slash. This can have
// embedded asterisks which are interpreted using glob rules.
@@ -162,21 +174,7 @@ class URLPattern {
// Returns true if this pattern matches all urls.
bool match_all_urls() const { return match_all_urls_; }
- void set_match_all_urls(bool val) { match_all_urls_ = val; }
-
- // 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);
+ void SetMatchAllURLs(bool val);
// Sets the scheme for pattern matches. This can be a single '*' if the
// pattern matches all valid schemes (as defined by the valid_schemes_
@@ -211,7 +209,7 @@ class URLPattern {
const std::string& port() const { return port_; }
// Returns a string representing this instance.
- std::string GetAsString() const;
+ const std::string& GetAsString() const;
// Determine whether there is a URL that would match this instance and another
// instance. This method is symmetrical: Calling other.OverlapsWith(this)
@@ -242,14 +240,6 @@ class URLPattern {
static const char* GetParseResultString(URLPattern::ParseResult parse_result);
private:
-#if !(defined(_MSC_VER) && _MSC_VER >= 1600)
- friend class std::vector<URLPattern>;
-
- // Note: don't use this directly. This exists so URLPattern can be used
- // with STL containers.
- URLPattern();
-#endif
-
// Returns true if any of the |schemes| items matches our scheme.
bool MatchesAnyScheme(const std::vector<std::string>& schemes) const;
@@ -287,6 +277,9 @@ class URLPattern {
// The path with "?" and "\" characters escaped for use with the
// MatchPattern() function.
std::string path_escaped_;
+
+ // A string representing this URLPattern.
+ mutable std::string spec_;
};
typedef std::vector<URLPattern> URLPatternList;
diff --git a/chrome/common/extensions/url_pattern_set.cc b/chrome/common/extensions/url_pattern_set.cc
index 65f8635..3c8f52c 100644
--- a/chrome/common/extensions/url_pattern_set.cc
+++ b/chrome/common/extensions/url_pattern_set.cc
@@ -4,6 +4,9 @@
#include "chrome/common/extensions/url_pattern_set.h"
+#include <algorithm>
+#include <iterator>
+
#include "chrome/common/extensions/url_pattern.h"
#include "googleurl/src/gurl.h"
@@ -11,39 +14,38 @@
void URLPatternSet::CreateUnion(const URLPatternSet& set1,
const URLPatternSet& set2,
URLPatternSet* out) {
- const URLPatternList list1 = set1.patterns();
- const URLPatternList list2 = set2.patterns();
-
out->ClearPatterns();
-
- for (size_t i = 0; i < list1.size(); ++i)
- out->AddPattern(list1.at(i));
-
- for (size_t i = 0; i < list2.size(); ++i)
- out->AddPattern(list2.at(i));
+ std::set_union(set1.patterns_.begin(), set1.patterns_.end(),
+ set2.patterns_.begin(), set2.patterns_.end(),
+ std::inserter<std::set<URLPattern> >(
+ out->patterns_, out->patterns_.begin()));
}
-URLPatternSet::URLPatternSet() {
-}
+URLPatternSet::URLPatternSet() {}
URLPatternSet::URLPatternSet(const URLPatternSet& rhs)
- : patterns_(rhs.patterns_) {
-}
+ : patterns_(rhs.patterns_) {}
-URLPatternSet::~URLPatternSet() {
-}
+URLPatternSet::URLPatternSet(const std::set<URLPattern>& patterns)
+ : patterns_(patterns) {}
+
+URLPatternSet::~URLPatternSet() {}
URLPatternSet& URLPatternSet::operator=(const URLPatternSet& rhs) {
patterns_ = rhs.patterns_;
return *this;
}
+bool URLPatternSet::operator==(const URLPatternSet& other) const {
+ return patterns_ == other.patterns_;
+}
+
bool URLPatternSet::is_empty() const {
return patterns_.empty();
}
void URLPatternSet::AddPattern(const URLPattern& pattern) {
- patterns_.push_back(pattern);
+ patterns_.insert(pattern);
}
void URLPatternSet::ClearPatterns() {
@@ -51,7 +53,7 @@ void URLPatternSet::ClearPatterns() {
}
bool URLPatternSet::MatchesURL(const GURL& url) const {
- for (URLPatternList::const_iterator pattern = patterns_.begin();
+ for (URLPatternSet::const_iterator pattern = patterns_.begin();
pattern != patterns_.end(); ++pattern) {
if (pattern->MatchesURL(url))
return true;
@@ -63,9 +65,9 @@ bool URLPatternSet::MatchesURL(const GURL& url) const {
bool URLPatternSet::OverlapsWith(const URLPatternSet& other) const {
// Two extension extents overlap if there is any one URL that would match at
// least one pattern in each of the extents.
- for (URLPatternList::const_iterator i = patterns_.begin();
+ for (URLPatternSet::const_iterator i = patterns_.begin();
i != patterns_.end(); ++i) {
- for (URLPatternList::const_iterator j = other.patterns().begin();
+ for (URLPatternSet::const_iterator j = other.patterns().begin();
j != other.patterns().end(); ++j) {
if (i->OverlapsWith(*j))
return true;
diff --git a/chrome/common/extensions/url_pattern_set.h b/chrome/common/extensions/url_pattern_set.h
index 2120e547..1848e74 100644
--- a/chrome/common/extensions/url_pattern_set.h
+++ b/chrome/common/extensions/url_pattern_set.h
@@ -6,7 +6,7 @@
#define CHROME_COMMON_EXTENSIONS_URL_PATTERN_SET_H_
#pragma once
-#include <vector>
+#include <set>
#include "chrome/common/extensions/url_pattern.h"
@@ -15,20 +15,27 @@ class GURL;
// Represents the set of URLs an extension uses for web content.
class URLPatternSet {
public:
+ typedef std::set<URLPattern>::const_iterator const_iterator;
+ typedef std::set<URLPattern>::iterator iterator;
+
// Clears |out| and populates the set with the union of |set1| and |set2|.
- // NOTE: this does not discard duplicates.
static void CreateUnion(const URLPatternSet& set1,
const URLPatternSet& set2,
URLPatternSet* out);
URLPatternSet();
URLPatternSet(const URLPatternSet& rhs);
+ explicit URLPatternSet(const std::set<URLPattern>& patterns);
~URLPatternSet();
+
URLPatternSet& operator=(const URLPatternSet& rhs);
+ bool operator==(const URLPatternSet& rhs) const;
bool is_empty() const;
+ const std::set<URLPattern>& patterns() const { return patterns_; }
+ const_iterator begin() const { return patterns_.begin(); }
+ const_iterator end() const { return patterns_.end(); }
- const URLPatternList& patterns() const { return patterns_; }
void AddPattern(const URLPattern& pattern);
void ClearPatterns();
@@ -40,7 +47,7 @@ class URLPatternSet {
private:
// The list of URL patterns that comprise the extent.
- URLPatternList patterns_;
+ std::set<URLPattern> patterns_;
};
#endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_SET_H_
diff --git a/chrome/common/extensions/url_pattern_set_unittest.cc b/chrome/common/extensions/url_pattern_set_unittest.cc
index 8c05f2c..f9717ef 100644
--- a/chrome/common/extensions/url_pattern_set_unittest.cc
+++ b/chrome/common/extensions/url_pattern_set_unittest.cc
@@ -7,27 +7,6 @@
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
-namespace {
-
-static void AssertEqualExtents(const URLPatternSet& extent1,
- const URLPatternSet& extent2) {
- URLPatternList patterns1 = extent1.patterns();
- URLPatternList patterns2 = extent2.patterns();
- std::set<std::string> strings1;
- EXPECT_EQ(patterns1.size(), patterns2.size());
-
- for (size_t i = 0; i < patterns1.size(); ++i)
- strings1.insert(patterns1.at(i).GetAsString());
-
- std::set<std::string> strings2;
- for (size_t i = 0; i < patterns2.size(); ++i)
- strings2.insert(patterns2.at(i).GetAsString());
-
- EXPECT_EQ(strings1, strings2);
-}
-
-} // namespace
-
static const int kAllSchemes =
URLPattern::SCHEME_HTTP |
URLPattern::SCHEME_HTTPS |
@@ -96,7 +75,7 @@ TEST(URLPatternSetTest, CreateUnion) {
// Union with an empty set.
URLPatternSet result;
URLPatternSet::CreateUnion(extent1, empty_extent, &result);
- AssertEqualExtents(expected, result);
+ EXPECT_EQ(expected, result);
// Union with a real set (including a duplicate).
URLPatternSet extent2;
@@ -111,5 +90,5 @@ TEST(URLPatternSetTest, CreateUnion) {
result.ClearPatterns();
URLPatternSet::CreateUnion(extent1, extent2, &result);
- AssertEqualExtents(expected, result);
+ EXPECT_EQ(expected, result);
}
diff --git a/chrome/common/extensions/url_pattern_unittest.cc b/chrome/common/extensions/url_pattern_unittest.cc
index 0903395..5c72a05 100644
--- a/chrome/common/extensions/url_pattern_unittest.cc
+++ b/chrome/common/extensions/url_pattern_unittest.cc
@@ -580,3 +580,97 @@ TEST(ExtensionURLPatternTest, IgnorePorts) {
EXPECT_FALSE(pattern1.MatchesURL(url));
EXPECT_FALSE(pattern2.MatchesURL(url));
}
+
+TEST(ExtensionURLPatternTest, Equals) {
+ const struct {
+ const char* pattern1;
+ const char* pattern2;
+ bool expected_equal;
+ } kEqualsTestCases[] = {
+ // schemes
+ { "http://en.google.com/blah/*/foo",
+ "https://en.google.com/blah/*/foo",
+ false
+ },
+ { "https://en.google.com/blah/*/foo",
+ "https://en.google.com/blah/*/foo",
+ true
+ },
+ { "https://en.google.com/blah/*/foo",
+ "ftp://en.google.com/blah/*/foo",
+ false
+ },
+
+ // subdomains
+ { "https://en.google.com/blah/*/foo",
+ "https://fr.google.com/blah/*/foo",
+ false
+ },
+ { "https://www.google.com/blah/*/foo",
+ "https://*.google.com/blah/*/foo",
+ false
+ },
+ { "https://*.google.com/blah/*/foo",
+ "https://*.google.com/blah/*/foo",
+ true
+ },
+
+ // domains
+ { "http://en.example.com/blah/*/foo",
+ "http://en.google.com/blah/*/foo",
+ false
+ },
+
+ // ports
+ { "http://en.google.com:8000/blah/*/foo",
+ "http://en.google.com/blah/*/foo",
+ false
+ },
+ { "http://fr.google.com:8000/blah/*/foo",
+ "http://fr.google.com:8000/blah/*/foo",
+ true
+ },
+ { "http://en.google.com:8000/blah/*/foo",
+ "http://en.google.com:8080/blah/*/foo",
+ false
+ },
+
+ // paths
+ { "http://en.google.com/blah/*/foo",
+ "http://en.google.com/blah/*",
+ false
+ },
+ { "http://en.google.com/*",
+ "http://en.google.com/",
+ false
+ },
+ { "http://en.google.com/*",
+ "http://en.google.com/*",
+ true
+ },
+
+ // all_urls
+ { "<all_urls>",
+ "<all_urls>",
+ true
+ },
+ { "<all_urls>",
+ "http://*/*",
+ false
+ }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kEqualsTestCases); ++i) {
+ std::string message = kEqualsTestCases[i].pattern1;
+ message += " ";
+ message += kEqualsTestCases[i].pattern2;
+
+ URLPattern pattern1(URLPattern::SCHEME_ALL);
+ URLPattern pattern2(URLPattern::SCHEME_ALL);
+
+ pattern1.Parse(kEqualsTestCases[i].pattern1, URLPattern::USE_PORTS);
+ pattern2.Parse(kEqualsTestCases[i].pattern2, URLPattern::USE_PORTS);
+ 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 17aadbc..77fd715 100644
--- a/chrome/common/extensions/user_script.cc
+++ b/chrome/common/extensions/user_script.cc
@@ -121,9 +121,9 @@ void UserScript::Pickle(::Pickle* pickle) const {
}
// Write url patterns.
- URLPatternList pattern_list = url_set_.patterns();
- pickle->WriteSize(pattern_list.size());
- for (URLPatternList::const_iterator pattern = pattern_list.begin();
+ URLPatternSet pattern_list = url_set_;
+ pickle->WriteSize(pattern_list.patterns().size());
+ for (URLPatternSet::const_iterator pattern = pattern_list.begin();
pattern != pattern_list.end(); ++pattern) {
pickle->WriteInt(pattern->valid_schemes());
pickle->WriteString(pattern->GetAsString());
@@ -191,11 +191,11 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) {
// pattern so that it's valid.
bool had_file_scheme = (valid_schemes & URLPattern::SCHEME_FILE) != 0;
if (!had_file_scheme)
- pattern.set_valid_schemes(valid_schemes | URLPattern::SCHEME_FILE);
+ pattern.SetValidSchemes(valid_schemes | URLPattern::SCHEME_FILE);
CHECK(URLPattern::PARSE_SUCCESS ==
pattern.Parse(pattern_str, URLPattern::IGNORE_PORTS));
if (!had_file_scheme)
- pattern.set_valid_schemes(valid_schemes);
+ pattern.SetValidSchemes(valid_schemes);
url_set_.AddPattern(pattern);
}
diff --git a/chrome/common/extensions/user_script.h b/chrome/common/extensions/user_script.h
index ab42e0b..6153f4b 100644
--- a/chrome/common/extensions/user_script.h
+++ b/chrome/common/extensions/user_script.h
@@ -149,10 +149,11 @@ class UserScript {
// The URLPatterns, if any, that determine which pages this script runs
// against.
- const URLPatternList& url_patterns() const { return url_set_.patterns(); }
+ const URLPatternSet& url_patterns() const { return url_set_; }
void add_url_pattern(const URLPattern& pattern);
- const URLPatternList& exclude_url_patterns() const {
- return exclude_url_set_.patterns(); }
+ const URLPatternSet& exclude_url_patterns() const {
+ return exclude_url_set_;
+ }
void add_exclude_url_pattern(const URLPattern& pattern);
// List of js scripts for this user script
diff --git a/chrome/common/extensions/user_script_unittest.cc b/chrome/common/extensions/user_script_unittest.cc
index c04e54d..f3eb5fe 100644
--- a/chrome/common/extensions/user_script_unittest.cc
+++ b/chrome/common/extensions/user_script_unittest.cc
@@ -208,11 +208,8 @@ TEST(ExtensionUserScriptTest, Pickle) {
for (size_t i = 0; i < script1.globs().size(); ++i) {
EXPECT_EQ(script1.globs()[i], script2.globs()[i]);
}
- ASSERT_EQ(script1.url_patterns().size(), script2.url_patterns().size());
- for (size_t i = 0; i < script1.url_patterns().size(); ++i) {
- EXPECT_EQ(script1.url_patterns()[i].GetAsString(),
- script2.url_patterns()[i].GetAsString());
- }
+
+ ASSERT_EQ(script1.url_patterns(), script2.url_patterns());
}
TEST(ExtensionUserScriptTest, Defaults) {