diff options
author | jstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-12 19:38:29 +0000 |
---|---|---|
committer | jstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-12 19:38:29 +0000 |
commit | 33ccd923b68f5f202230f5a400021f3efd304439 (patch) | |
tree | c8c57a6de3b69286738be878683594f40a0c936d /chrome | |
parent | 2261b8bbffdf8abd12837326b0d0676f8eddb744 (diff) | |
download | chromium_src-33ccd923b68f5f202230f5a400021f3efd304439.zip chromium_src-33ccd923b68f5f202230f5a400021f3efd304439.tar.gz chromium_src-33ccd923b68f5f202230f5a400021f3efd304439.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@92219 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
31 files changed, 426 insertions, 336 deletions
diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index 0893b50..b7eccd8 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -4080,15 +4080,15 @@ void TestingAutomationProvider::GetThemeInfo( namespace { ListValue* GetHostPermissions(const Extension* ext, bool effective_perm) { - URLPatternList pattern_list; + URLPatternSet pattern_set; if (effective_perm) - pattern_list = ext->GetEffectiveHostPermissions().patterns(); + pattern_set = ext->GetEffectiveHostPermissions(); else - pattern_list = ext->permission_set()->explicit_hosts().patterns(); + pattern_set = ext->permission_set()->explicit_hosts(); ListValue* permissions = new ListValue; - for (URLPatternList::const_iterator perm = pattern_list.begin(); - perm != pattern_list.end(); ++perm) { + for (URLPatternSet::const_iterator perm = pattern_set.begin(); + perm != pattern_set.end(); ++perm) { permissions->Append(new StringValue(perm->GetAsString())); } diff --git a/chrome/browser/extensions/convert_user_script.cc b/chrome/browser/extensions/convert_user_script.cc index 8d7aafe..a8f2427 100644 --- a/chrome/browser/extensions/convert_user_script.cc +++ b/chrome/browser/extensions/convert_user_script.cc @@ -102,10 +102,10 @@ scoped_refptr<Extension> ConvertUserScriptToExtension( // If the script provides its own match patterns, we use those. Otherwise, we // generate some using the include globs. ListValue* matches = new ListValue(); - if (!script.url_patterns().empty()) { - for (size_t i = 0; i < script.url_patterns().size(); ++i) { - matches->Append(Value::CreateStringValue( - script.url_patterns()[i].GetAsString())); + if (!script.url_patterns().is_empty()) { + for (URLPatternSet::const_iterator i = script.url_patterns().begin(); + i != script.url_patterns().end(); ++i) { + matches->Append(Value::CreateStringValue(i->GetAsString())); } } else { // TODO(aa): Derive tighter matches where possible. diff --git a/chrome/browser/extensions/convert_user_script_unittest.cc b/chrome/browser/extensions/convert_user_script_unittest.cc index a8aab8f..38b106a 100644 --- a/chrome/browser/extensions/convert_user_script_unittest.cc +++ b/chrome/browser/extensions/convert_user_script_unittest.cc @@ -15,6 +15,15 @@ #include "chrome/common/extensions/extension.h" #include "testing/gtest/include/gtest/gtest.h" +namespace { + +static void AddPattern(URLPatternSet* extent, const std::string& pattern) { + int schemes = URLPattern::SCHEME_ALL; + extent->AddPattern(URLPattern(schemes, pattern)); +} + +} + TEST(ExtensionFromUserScript, Basic) { FilePath test_file; ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_file)); @@ -47,8 +56,9 @@ TEST(ExtensionFromUserScript, Basic) { EXPECT_EQ("http://www.yahoo.com/*", script.globs().at(1)); ASSERT_EQ(1u, script.exclude_globs().size()); EXPECT_EQ("*foo*", script.exclude_globs().at(0)); - ASSERT_EQ(1u, script.url_patterns().size()); - EXPECT_EQ("http://www.google.com/*", script.url_patterns()[0].GetAsString()); + ASSERT_EQ(1u, script.url_patterns().patterns().size()); + EXPECT_EQ("http://www.google.com/*", + script.url_patterns().begin()->GetAsString()); // Make sure the files actually exist on disk. EXPECT_TRUE(file_util::PathExists( @@ -86,9 +96,11 @@ TEST(ExtensionFromUserScript, NoMetdata) { ASSERT_EQ(1u, script.globs().size()); EXPECT_EQ("*", script.globs()[0]); EXPECT_EQ(0u, script.exclude_globs().size()); - ASSERT_EQ(2u, script.url_patterns().size()); - EXPECT_EQ("http://*/*", script.url_patterns()[0].GetAsString()); - EXPECT_EQ("https://*/*", script.url_patterns()[1].GetAsString()); + + URLPatternSet expected; + AddPattern(&expected, "http://*/*"); + AddPattern(&expected, "https://*/*"); + EXPECT_EQ(expected, script.url_patterns()); // Make sure the files actually exist on disk. EXPECT_TRUE(file_util::PathExists( diff --git a/chrome/browser/extensions/convert_web_app_unittest.cc b/chrome/browser/extensions/convert_web_app_unittest.cc index 4aafc4f..0f3b537 100644 --- a/chrome/browser/extensions/convert_web_app_unittest.cc +++ b/chrome/browser/extensions/convert_web_app_unittest.cc @@ -128,7 +128,7 @@ TEST(ExtensionFromWebApp, Basic) { EXPECT_TRUE(extension->HasAPIPermission("notifications")); ASSERT_EQ(1u, extension->web_extent().patterns().size()); EXPECT_EQ("http://aaronboodman.com/gearpad/*", - extension->web_extent().patterns()[0].GetAsString()); + extension->web_extent().patterns().begin()->GetAsString()); EXPECT_EQ(web_app.icons.size(), extension->icons().map().size()); for (size_t i = 0; i < web_app.icons.size(); ++i) { @@ -170,5 +170,5 @@ TEST(ExtensionFromWebApp, Minimal) { EXPECT_EQ(0u, extension->permission_set()->apis().size()); ASSERT_EQ(1u, extension->web_extent().patterns().size()); EXPECT_EQ("*://aaronboodman.com/*", - extension->web_extent().patterns()[0].GetAsString()); + extension->web_extent().patterns().begin()->GetAsString()); } diff --git a/chrome/browser/extensions/crx_installer.cc b/chrome/browser/extensions/crx_installer.cc index 3581571..a1e3863 100644 --- a/chrome/browser/extensions/crx_installer.cc +++ b/chrome/browser/extensions/crx_installer.cc @@ -283,12 +283,13 @@ bool CrxInstaller::AllowInstall(const Extension* extension, // host (or a subdomain of the host) the download happened from. There's // no way for us to verify that the app controls any other hosts. URLPattern pattern(UserScript::kValidUserScriptSchemes); - pattern.set_host(original_url_.host()); - pattern.set_match_subdomains(true); + pattern.SetHost(original_url_.host()); + pattern.SetMatchSubdomains(true); - URLPatternList patterns = extension_->web_extent().patterns(); - for (size_t i = 0; i < patterns.size(); ++i) { - if (!pattern.MatchesHost(patterns[i].host())) { + URLPatternSet patterns = extension_->web_extent(); + for (URLPatternSet::const_iterator i = patterns.begin(); + i != patterns.end(); ++i) { + if (!pattern.MatchesHost(i->host())) { *error = base::StringPrintf( "Apps must be served from the host that they affect."); return false; diff --git a/chrome/browser/extensions/extension_management_api.cc b/chrome/browser/extensions/extension_management_api.cc index 9dc479c..3955361 100644 --- a/chrome/browser/extensions/extension_management_api.cc +++ b/chrome/browser/extensions/extension_management_api.cc @@ -106,10 +106,10 @@ static DictionaryValue* CreateExtensionInfo(const Extension& extension, ListValue* host_permission_list = new ListValue(); if (!extension.is_hosted_app()) { // Skip host permissions for hosted apps. - const URLPatternList host_perms = - extension.permission_set()->explicit_hosts().patterns(); - if (!host_perms.empty()) { - URLPatternList::const_iterator host_perms_iter; + const URLPatternSet host_perms = + extension.permission_set()->explicit_hosts(); + if (!host_perms.is_empty()) { + URLPatternSet::const_iterator host_perms_iter; for (host_perms_iter = host_perms.begin(); host_perms_iter != host_perms.end(); ++host_perms_iter) { diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc index 4a1dd2c..f5e16a5 100644 --- a/chrome/browser/extensions/extension_prefs.cc +++ b/chrome/browser/extensions/extension_prefs.cc @@ -394,7 +394,7 @@ bool ExtensionPrefs::ReadExtensionPrefURLPatternSet( return false; } if (!allow_file_access && pattern.MatchesScheme(chrome::kFileScheme)) { - pattern.set_valid_schemes( + pattern.SetValidSchemes( pattern.valid_schemes() & ~URLPattern::SCHEME_FILE); } result->AddPattern(pattern); @@ -408,8 +408,8 @@ void ExtensionPrefs::SetExtensionPrefURLPatternSet( const std::string& pref_key, const URLPatternSet& new_value) { ListValue* value = new ListValue(); - for (URLPatternList::const_iterator i = new_value.patterns().begin(); - i != new_value.patterns().end(); ++i) + for (URLPatternSet::const_iterator i = new_value.begin(); + i != new_value.end(); ++i) value->AppendIfNotPresent(Value::CreateStringValue(i->GetAsString())); UpdateExtensionPref(extension_id, pref_key, value); diff --git a/chrome/browser/extensions/extension_prefs_unittest.cc b/chrome/browser/extensions/extension_prefs_unittest.cc index 31f86d1..72e65aa 100644 --- a/chrome/browser/extensions/extension_prefs_unittest.cc +++ b/chrome/browser/extensions/extension_prefs_unittest.cc @@ -44,23 +44,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(); - EXPECT_EQ(patterns1.size(), patterns2.size()); - - std::set<std::string> strings1; - 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); -} - // Base class for tests. class ExtensionPrefsTest : public testing::Test { public: @@ -265,10 +248,10 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { EXPECT_FALSE(granted_permissions->IsEmpty()); EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); EXPECT_EQ(expected_apis, granted_permissions->apis()); - AssertEqualExtents(ehost_perm_set1_, - granted_permissions->explicit_hosts()); - AssertEqualExtents(ehost_perm_set1_, - granted_permissions->effective_hosts()); + EXPECT_EQ(ehost_perm_set1_, + granted_permissions->explicit_hosts()); + EXPECT_EQ(ehost_perm_set1_, + granted_permissions->effective_hosts()); // Add part of the scriptable host permissions. permissions.reset(new ExtensionPermissionSet( @@ -278,14 +261,14 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { EXPECT_FALSE(granted_permissions->IsEmpty()); EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); EXPECT_EQ(expected_apis, granted_permissions->apis()); - AssertEqualExtents(ehost_perm_set1_, - granted_permissions->explicit_hosts()); - AssertEqualExtents(shost_perm_set1_, - granted_permissions->scriptable_hosts()); + EXPECT_EQ(ehost_perm_set1_, + granted_permissions->explicit_hosts()); + EXPECT_EQ(shost_perm_set1_, + granted_permissions->scriptable_hosts()); + URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_, &effective_permissions_); - AssertEqualExtents(effective_permissions_, - granted_permissions->effective_hosts()); + EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts()); // Add the rest of both the permissions. permissions.reset(new ExtensionPermissionSet( @@ -300,15 +283,14 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { EXPECT_TRUE(granted_permissions.get()); EXPECT_FALSE(granted_permissions->IsEmpty()); EXPECT_EQ(api_permissions_, granted_permissions->apis()); - AssertEqualExtents(ehost_permissions_, - granted_permissions->explicit_hosts()); - AssertEqualExtents(shost_permissions_, - granted_permissions->scriptable_hosts()); + EXPECT_EQ(ehost_permissions_, + granted_permissions->explicit_hosts()); + EXPECT_EQ(shost_permissions_, + granted_permissions->scriptable_hosts()); effective_permissions_.ClearPatterns(); URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_, &effective_permissions_); - AssertEqualExtents(effective_permissions_, - granted_permissions->effective_hosts()); + EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts()); } virtual void Verify() { @@ -317,8 +299,10 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { EXPECT_TRUE(permissions.get()); EXPECT_FALSE(permissions->HasEffectiveFullAccess()); EXPECT_EQ(api_permissions_, permissions->apis()); - AssertEqualExtents(ehost_permissions_, permissions->explicit_hosts()); - AssertEqualExtents(shost_permissions_, permissions->scriptable_hosts()); + EXPECT_EQ(ehost_permissions_, + permissions->explicit_hosts()); + EXPECT_EQ(shost_permissions_, + permissions->scriptable_hosts()); } private: diff --git a/chrome/browser/extensions/extension_service_unittest.cc b/chrome/browser/extensions/extension_service_unittest.cc index 2bac463..9d0245e 100644 --- a/chrome/browser/extensions/extension_service_unittest.cc +++ b/chrome/browser/extensions/extension_service_unittest.cc @@ -114,23 +114,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 MockExtensionProvider : public ExternalExtensionProviderInterface { @@ -1000,16 +983,14 @@ TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) { ValidateIntegerPref(good2, "state", Extension::ENABLED); ValidateIntegerPref(good2, "location", Extension::INTERNAL); + URLPatternSet expected_patterns; + AddPattern(&expected_patterns, "file:///*"); + AddPattern(&expected_patterns, "http://*.google.com/*"); + AddPattern(&expected_patterns, "https://*.google.com/*"); const Extension* extension = loaded_[0]; const UserScriptList& scripts = extension->content_scripts(); ASSERT_EQ(2u, scripts.size()); - EXPECT_EQ(3u, scripts[0].url_patterns().size()); - EXPECT_EQ("file:///*", - scripts[0].url_patterns()[0].GetAsString()); - EXPECT_EQ("http://*.google.com/*", - scripts[0].url_patterns()[1].GetAsString()); - EXPECT_EQ("https://*.google.com/*", - scripts[0].url_patterns()[2].GetAsString()); + EXPECT_EQ(expected_patterns, scripts[0].url_patterns()); EXPECT_EQ(2u, scripts[0].js_scripts().size()); ExtensionResource resource00(extension->id(), scripts[0].js_scripts()[0].extension_root(), @@ -1024,8 +1005,9 @@ TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) { ASSERT_TRUE(file_util::AbsolutePath(&expected_path)); EXPECT_TRUE(resource01.ComparePathWithDefault(expected_path)); EXPECT_TRUE(extension->plugins().empty()); - EXPECT_EQ(1u, scripts[1].url_patterns().size()); - EXPECT_EQ("http://*.news.com/*", scripts[1].url_patterns()[0].GetAsString()); + EXPECT_EQ(1u, scripts[1].url_patterns().patterns().size()); + EXPECT_EQ("http://*.news.com/*", + scripts[1].url_patterns().begin()->GetAsString()); ExtensionResource resource10(extension->id(), scripts[1].js_scripts()[0].extension_root(), scripts[1].js_scripts()[0].relative_path()); @@ -1033,11 +1015,11 @@ TEST_F(ExtensionServiceTest, LoadAllExtensionsFromDirectorySuccess) { extension->path().AppendASCII("js_files").AppendASCII("script3.js"); ASSERT_TRUE(file_util::AbsolutePath(&expected_path)); EXPECT_TRUE(resource10.ComparePathWithDefault(expected_path)); - const URLPatternList permissions = - extension->permission_set()->explicit_hosts().patterns(); - ASSERT_EQ(2u, permissions.size()); - EXPECT_EQ("http://*.google.com/*", permissions[0].GetAsString()); - EXPECT_EQ("https://*.google.com/*", permissions[1].GetAsString()); + + expected_patterns.ClearPatterns(); + AddPattern(&expected_patterns, "http://*.google.com/*"); + AddPattern(&expected_patterns, "https://*.google.com/*"); + EXPECT_EQ(expected_patterns, extension->permission_set()->explicit_hosts()); EXPECT_EQ(std::string(good1), loaded_[1]->id()); EXPECT_EQ(std::string("My extension 2"), loaded_[1]->name()); @@ -1402,7 +1384,7 @@ TEST_F(ExtensionServiceTest, GrantedPermissions) { EXPECT_FALSE(known_perms->IsEmpty()); EXPECT_EQ(expected_api_perms, known_perms->apis()); EXPECT_FALSE(known_perms->HasEffectiveFullAccess()); - AssertEqualExtents(expected_host_perms, known_perms->effective_hosts()); + EXPECT_EQ(expected_host_perms, known_perms->effective_hosts()); } #if !defined(OS_CHROMEOS) @@ -1497,8 +1479,7 @@ TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { ASSERT_FALSE(current_perms->IsEmpty()); ASSERT_FALSE(current_perms->HasEffectiveFullAccess()); ASSERT_EQ(expected_api_permissions, current_perms->apis()); - AssertEqualExtents(expected_host_permissions, - current_perms->effective_hosts()); + ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts()); // Tests that the extension is disabled when a host permission is missing from // the extension's granted host permissions preference. (This simulates @@ -1539,8 +1520,7 @@ TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { ASSERT_FALSE(current_perms->IsEmpty()); ASSERT_FALSE(current_perms->HasEffectiveFullAccess()); ASSERT_EQ(expected_api_permissions, current_perms->apis()); - AssertEqualExtents(expected_host_permissions, - current_perms->effective_hosts()); + ASSERT_EQ(expected_host_permissions, current_perms->effective_hosts()); } // Test Packaging and installing an extension. diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc index 96ade99b..958e0ab 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -130,7 +130,7 @@ bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( // If no patterns were specified, default to @include *. This is what // Greasemonkey does. - if (script->globs().empty() && script->url_patterns().empty()) + if (script->globs().empty() && script->url_patterns().is_empty()) script->add_glob("*"); return true; diff --git a/chrome/browser/extensions/user_script_master_unittest.cc b/chrome/browser/extensions/user_script_master_unittest.cc index c5d6af1..0ae3a4c 100644 --- a/chrome/browser/extensions/user_script_master_unittest.cc +++ b/chrome/browser/extensions/user_script_master_unittest.cc @@ -19,6 +19,15 @@ #include "content/common/notification_service.h" #include "testing/gtest/include/gtest/gtest.h" +namespace { + +static void AddPattern(URLPatternSet* extent, const std::string& pattern) { + int schemes = URLPattern::SCHEME_ALL; + extent->AddPattern(URLPattern(schemes, pattern)); +} + +} + // Test bringing up a master on a specific directory, putting a script // in there, etc. @@ -142,15 +151,15 @@ TEST_F(UserScriptMasterTest, Parse4) { "// @match \t http://mail.yahoo.com/*\n" "// ==/UserScript==\n"); + URLPatternSet expected_patterns; + AddPattern(&expected_patterns, "http://*.mail.google.com/*"); + AddPattern(&expected_patterns, "http://mail.yahoo.com/*"); + UserScript script; EXPECT_TRUE(UserScriptMaster::ScriptReloader::ParseMetadataHeader( text, &script)); EXPECT_EQ(0U, script.globs().size()); - ASSERT_EQ(2U, script.url_patterns().size()); - EXPECT_EQ("http://*.mail.google.com/*", - script.url_patterns()[0].GetAsString()); - EXPECT_EQ("http://mail.yahoo.com/*", - script.url_patterns()[1].GetAsString()); + EXPECT_EQ(expected_patterns, script.url_patterns()); } TEST_F(UserScriptMasterTest, Parse5) { @@ -192,9 +201,9 @@ TEST_F(UserScriptMasterTest, Parse7) { text, &script)); ASSERT_EQ("hello", script.name()); ASSERT_EQ("wiggity woo", script.description()); - ASSERT_EQ(1U, script.url_patterns().size()); + ASSERT_EQ(1U, script.url_patterns().patterns().size()); EXPECT_EQ("http://mail.yahoo.com/*", - script.url_patterns()[0].GetAsString()); + script.url_patterns().begin()->GetAsString()); } TEST_F(UserScriptMasterTest, SkipBOMAtTheBeginning) { diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index f61f789..9a2aae2f 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -458,7 +458,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); @@ -475,7 +475,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); } @@ -505,7 +505,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) { @@ -1026,7 +1026,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); } @@ -1956,7 +1956,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); } @@ -2705,7 +2705,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 c1543da..bde6707 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..d7f4e52 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; + URLPatternSet 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 (URLPatternSet::const_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 d87e9e3..dbe321e 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; } @@ -454,9 +455,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 @@ -475,7 +476,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)); } @@ -561,9 +562,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; @@ -626,18 +626,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. @@ -666,10 +667,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; } @@ -678,7 +679,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; } @@ -700,7 +701,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); @@ -757,17 +758,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 5cd331a..0e46168 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); @@ -292,7 +292,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; @@ -348,8 +348,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 9cfa698..6140bec 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 { @@ -336,9 +319,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); @@ -364,9 +347,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) { @@ -564,7 +547,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])); } @@ -589,10 +572,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; @@ -608,7 +591,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()); } { @@ -621,7 +604,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()); } { @@ -632,7 +615,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()); } { @@ -643,7 +626,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()); } { @@ -655,12 +638,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()); } { @@ -687,11 +670,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()); } { @@ -700,11 +683,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()); } { @@ -718,12 +701,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()); } } @@ -745,11 +728,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) { @@ -769,11 +752,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, @@ -793,11 +776,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, @@ -816,11 +799,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..21ae210 100644 --- a/chrome/common/extensions/url_pattern.h +++ b/chrome/common/extensions/url_pattern.h @@ -142,18 +142,35 @@ class URLPattern { ~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 +179,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 +214,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) @@ -287,6 +290,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..fa7e231 100644 --- a/chrome/common/extensions/url_pattern_set.cc +++ b/chrome/common/extensions/url_pattern_set.cc @@ -4,6 +4,8 @@ #include "chrome/common/extensions/url_pattern_set.h" +#include <algorithm> + #include "chrome/common/extensions/url_pattern.h" #include "googleurl/src/gurl.h" @@ -11,39 +13,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 +52,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 +64,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..0c8be44 100644 --- a/chrome/common/extensions/url_pattern_set.h +++ b/chrome/common/extensions/url_pattern_set.h @@ -6,6 +6,7 @@ #define CHROME_COMMON_EXTENSIONS_URL_PATTERN_SET_H_ #pragma once +#include <set> #include <vector> #include "chrome/common/extensions/url_pattern.h" @@ -15,20 +16,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 +48,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) { diff --git a/chrome/renderer/extensions/extension_dispatcher.cc b/chrome/renderer/extensions/extension_dispatcher.cc index e654623..098e3e5 100644 --- a/chrome/renderer/extensions/extension_dispatcher.cc +++ b/chrome/renderer/extensions/extension_dispatcher.cc @@ -243,9 +243,10 @@ void ExtensionDispatcher::InitHostPermissions(const Extension* extension) { false); } - const URLPatternList& permissions = - extension->permission_set()->explicit_hosts().patterns(); - for (size_t i = 0; i < permissions.size(); ++i) { + const URLPatternSet& permissions = + extension->permission_set()->explicit_hosts(); + for (URLPatternSet::const_iterator i = permissions.begin(); + i != permissions.end(); ++i) { const char* schemes[] = { chrome::kHttpScheme, chrome::kHttpsScheme, @@ -253,12 +254,12 @@ void ExtensionDispatcher::InitHostPermissions(const Extension* extension) { chrome::kChromeUIScheme, }; for (size_t j = 0; j < arraysize(schemes); ++j) { - if (permissions[i].MatchesScheme(schemes[j])) { + if (i->MatchesScheme(schemes[j])) { WebSecurityPolicy::addOriginAccessWhitelistEntry( extension->url(), WebString::fromUTF8(schemes[j]), - WebString::fromUTF8(permissions[i].host()), - permissions[i].match_subdomains()); + WebString::fromUTF8(i->host()), + i->match_subdomains()); } } } diff --git a/chrome/renderer/extensions/user_script_slave.cc b/chrome/renderer/extensions/user_script_slave.cc index 6ea6201..48e20ab 100644 --- a/chrome/renderer/extensions/user_script_slave.cc +++ b/chrome/renderer/extensions/user_script_slave.cc @@ -84,9 +84,10 @@ int UserScriptSlave::GetIsolatedWorldId( void UserScriptSlave::InitializeIsolatedWorld( int isolated_world_id, const Extension* extension) { - const URLPatternList& permissions = - extension->GetEffectiveHostPermissions().patterns(); - for (size_t i = 0; i < permissions.size(); ++i) { + const URLPatternSet& permissions = + extension->GetEffectiveHostPermissions(); + for (URLPatternSet::const_iterator i = permissions.begin(); + i != permissions.end(); ++i) { const char* schemes[] = { chrome::kHttpScheme, chrome::kHttpsScheme, @@ -94,12 +95,12 @@ void UserScriptSlave::InitializeIsolatedWorld( chrome::kChromeUIScheme, }; for (size_t j = 0; j < arraysize(schemes); ++j) { - if (permissions[i].MatchesScheme(schemes[j])) { + if (i->MatchesScheme(schemes[j])) { WebSecurityPolicy::addOriginAccessWhitelistEntry( extension->url(), WebString::fromUTF8(schemes[j]), - WebString::fromUTF8(permissions[i].host()), - permissions[i].match_subdomains()); + WebString::fromUTF8(i->host()), + i->match_subdomains()); } } } @@ -198,9 +199,10 @@ bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { WebVector<WebString> patterns; std::vector<WebString> temp_patterns; - for (size_t k = 0; k < script->url_patterns().size(); ++k) { - URLPatternList explicit_patterns = - script->url_patterns()[k].ConvertToExplicitSchemes(); + const URLPatternSet& url_patterns = script->url_patterns(); + for (URLPatternSet::const_iterator k = url_patterns.begin(); + k != url_patterns.end(); ++k) { + URLPatternList explicit_patterns = k->ConvertToExplicitSchemes(); for (size_t m = 0; m < explicit_patterns.size(); ++m) { temp_patterns.push_back(WebString::fromUTF8( explicit_patterns[m].GetAsString())); |