diff options
author | morrita@chromium.org <morrita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-20 08:31:12 +0000 |
---|---|---|
committer | morrita@chromium.org <morrita@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-20 08:31:12 +0000 |
commit | cced75a6e7444d9bda0f9dd72f5871e095acf5df (patch) | |
tree | d45fc18c620970fe947e8257bfffd8fdf07f6ab0 | |
parent | 3b8c5a6f7b3a780001da6f8f1084b82d9f9d5b33 (diff) | |
download | chromium_src-cced75a6e7444d9bda0f9dd72f5871e095acf5df.zip chromium_src-cced75a6e7444d9bda0f9dd72f5871e095acf5df.tar.gz chromium_src-cced75a6e7444d9bda0f9dd72f5871e095acf5df.tar.bz2 |
Rename ExtensionExtent to URLPatternSet and use URLPatternSet instead of std::vector<URLPattern>
- Rename ExtensionExtent to URLPatternSet
- Refactor common/extensions/user_script.h, common/extensions/user_script.cc, common/extensions/file_browser_handler.h and common/extensions/file_browser_handler.cc so that they use URLPatternSet instead of std::vector<URLPattern>
- At many places rename std::vector<URLPattern> to URLPatternList, which is typedefed at common/extensions/url_pattern.h
- Rename URLPatternSet::ClearPaths() to URLPatternSet::ClearPatterns()
- Rename URLPatternSet::ContainsURL() to URLPatternSet::MatchesUrl()
BUG=72955
TEST=ExtensionManifestTest.FileBrowserHandlers, ExtensionUserScriptTest.*, ExtensionURLPatternTest.*
Review URL: http://codereview.chromium.org/7038029
Patch from Kentaro Hara <haraken@google.com>.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86060 0039d316-1c4b-4281-b951-d872f2087c98
45 files changed, 344 insertions, 361 deletions
diff --git a/chrome/browser/automation/testing_automation_provider.cc b/chrome/browser/automation/testing_automation_provider.cc index 8a72b7a..de24524 100644 --- a/chrome/browser/automation/testing_automation_provider.cc +++ b/chrome/browser/automation/testing_automation_provider.cc @@ -89,8 +89,8 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_extent.h" #include "chrome/common/extensions/url_pattern.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "chrome/common/pref_names.h" #include "chrome/common/render_messages.h" #include "chrome/common/url_constants.h" diff --git a/chrome/browser/extensions/crx_installer.cc b/chrome/browser/extensions/crx_installer.cc index 902f070..6931b38 100644 --- a/chrome/browser/extensions/crx_installer.cc +++ b/chrome/browser/extensions/crx_installer.cc @@ -281,8 +281,7 @@ bool CrxInstaller::AllowInstall(const Extension* extension, pattern.set_host(original_url_.host()); pattern.set_match_subdomains(true); - ExtensionExtent::PatternList patterns = - extension_->web_extent().patterns(); + URLPatternList patterns = extension_->web_extent().patterns(); for (size_t i = 0; i < patterns.size(); ++i) { if (!pattern.MatchesHost(patterns[i].host())) { *error = base::StringPrintf( diff --git a/chrome/browser/extensions/extension_context_menu_api.cc b/chrome/browser/extensions/extension_context_menu_api.cc index a9419c7..50d08b7 100644 --- a/chrome/browser/extensions/extension_context_menu_api.cc +++ b/chrome/browser/extensions/extension_context_menu_api.cc @@ -128,7 +128,7 @@ bool ExtensionContextMenuFunction::ParseChecked( bool ExtensionContextMenuFunction::ParseURLPatterns( const DictionaryValue& properties, const char* key, - ExtensionExtent* result) { + URLPatternSet* result) { if (!properties.HasKey(key)) return true; ListValue* list = NULL; @@ -157,7 +157,7 @@ bool ExtensionContextMenuFunction::SetURLPatterns( const DictionaryValue& properties, ExtensionMenuItem* item) { // Process the documentUrlPattern value. - ExtensionExtent document_url_patterns; + URLPatternSet document_url_patterns; if (!ParseURLPatterns(properties, kDocumentUrlPatternsKey, &document_url_patterns)) return false; @@ -167,7 +167,7 @@ bool ExtensionContextMenuFunction::SetURLPatterns( } // Process the targetUrlPattern value. - ExtensionExtent target_url_patterns; + URLPatternSet target_url_patterns; if (!ParseURLPatterns(properties, kTargetUrlPatternsKey, &target_url_patterns)) return false; diff --git a/chrome/browser/extensions/extension_context_menu_api.h b/chrome/browser/extensions/extension_context_menu_api.h index 4ac5670..5562bc7 100644 --- a/chrome/browser/extensions/extension_context_menu_api.h +++ b/chrome/browser/extensions/extension_context_menu_api.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -8,7 +8,7 @@ #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/extensions/extension_menu_manager.h" -#include "chrome/common/extensions/extension_extent.h" +#include "chrome/common/extensions/url_pattern_set.h" class DictionaryValue; class ExtensionMenuItem; @@ -41,7 +41,7 @@ class ExtensionContextMenuFunction : public SyncExtensionFunction { // name. bool ParseURLPatterns(const DictionaryValue& properties, const char* key, - ExtensionExtent* result); + URLPatternSet* result); // Reads in any document and targetUrl patterns from |properties| and sets // them on |item|. diff --git a/chrome/browser/extensions/extension_info_map.cc b/chrome/browser/extensions/extension_info_map.cc index dec04dc..8e8980e 100644 --- a/chrome/browser/extensions/extension_info_map.cc +++ b/chrome/browser/extensions/extension_info_map.cc @@ -109,10 +109,10 @@ std::string ExtensionInfoMap::GetDefaultLocaleForExtension( return result; } -ExtensionExtent ExtensionInfoMap::GetEffectiveHostPermissionsForExtension( +URLPatternSet ExtensionInfoMap::GetEffectiveHostPermissionsForExtension( const std::string& id) const { Map::const_iterator iter = extension_info_.find(id); - ExtensionExtent result; + URLPatternSet result; if (iter != extension_info_.end()) result = iter->second->GetEffectiveHostPermissions(); @@ -132,7 +132,7 @@ bool ExtensionInfoMap::CheckURLAccessToExtensionPermission( // disallowed, so only one will match. info = extension_info_.begin(); while (info != extension_info_.end() && - !info->second->web_extent().ContainsURL(url)) + !info->second->web_extent().MatchesURL(url)) ++info; } diff --git a/chrome/browser/extensions/extension_info_map.h b/chrome/browser/extensions/extension_info_map.h index b657515..5d335c0 100644 --- a/chrome/browser/extensions/extension_info_map.h +++ b/chrome/browser/extensions/extension_info_map.h @@ -13,7 +13,7 @@ #include "base/file_path.h" #include "base/memory/ref_counted.h" #include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_extent.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "googleurl/src/gurl.h" class Extension; @@ -61,7 +61,7 @@ class ExtensionInfoMap : public base::RefCountedThreadSafe<ExtensionInfoMap> { std::string GetDefaultLocaleForExtension(const std::string& id) const; // Gets the effective host permissions for the extension with |id|. - ExtensionExtent + URLPatternSet GetEffectiveHostPermissionsForExtension(const std::string& id) const; // Determine whether a URL has access to the specified extension permission. diff --git a/chrome/browser/extensions/extension_info_map_unittest.cc b/chrome/browser/extensions/extension_info_map_unittest.cc index 0eb6357..659b085 100644 --- a/chrome/browser/extensions/extension_info_map_unittest.cc +++ b/chrome/browser/extensions/extension_info_map_unittest.cc @@ -134,7 +134,7 @@ TEST_F(ExtensionInfoMapTest, CheckPermissions) { GURL app_url("http://www.google.com/mail/foo.html"); ASSERT_TRUE(app->is_app()); - ASSERT_TRUE(app->web_extent().ContainsURL(app_url)); + ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); info_map->AddExtension(app); info_map->AddExtension(extension); diff --git a/chrome/browser/extensions/extension_management_api.cc b/chrome/browser/extensions/extension_management_api.cc index 83a9e43..0053b1d 100644 --- a/chrome/browser/extensions/extension_management_api.cc +++ b/chrome/browser/extensions/extension_management_api.cc @@ -107,7 +107,7 @@ static DictionaryValue* CreateExtensionInfo(const Extension& extension, // Skip host permissions for hosted apps. const URLPatternList host_perms = extension.host_permissions(); if (!host_perms.empty()) { - std::vector<URLPattern>::const_iterator host_perms_iter; + URLPatternList::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_menu_manager.h b/chrome/browser/extensions/extension_menu_manager.h index e9bb36d..eb87ae7 100644 --- a/chrome/browser/extensions/extension_menu_manager.h +++ b/chrome/browser/extensions/extension_menu_manager.h @@ -16,7 +16,7 @@ #include "base/memory/scoped_ptr.h" #include "base/string16.h" #include "chrome/browser/extensions/extension_icon_manager.h" -#include "chrome/common/extensions/extension_extent.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" @@ -117,10 +117,10 @@ class ExtensionMenuItem { ContextList contexts() const { return contexts_; } Type type() const { return type_; } bool checked() const { return checked_; } - const ExtensionExtent& document_url_patterns() const { + const URLPatternSet& document_url_patterns() const { return document_url_patterns_; } - const ExtensionExtent& target_url_patterns() const { + const URLPatternSet& target_url_patterns() const { return target_url_patterns_; } @@ -128,10 +128,10 @@ class ExtensionMenuItem { void set_title(const std::string& new_title) { title_ = new_title; } void set_contexts(ContextList contexts) { contexts_ = contexts; } void set_type(Type type) { type_ = type; } - void set_document_url_patterns(const ExtensionExtent& patterns) { + void set_document_url_patterns(const URLPatternSet& patterns) { document_url_patterns_ = patterns; } - void set_target_url_patterns(const ExtensionExtent& patterns) { + void set_target_url_patterns(const URLPatternSet& patterns) { target_url_patterns_ = patterns; } @@ -178,11 +178,11 @@ class ExtensionMenuItem { // Patterns for restricting what documents this item will appear for. This // applies to the frame where the click took place. - ExtensionExtent document_url_patterns_; + URLPatternSet document_url_patterns_; // Patterns for restricting where items appear based on the src/href // attribute of IMAGE/AUDIO/VIDEO/LINK tags. - ExtensionExtent target_url_patterns_; + URLPatternSet target_url_patterns_; // Any children this item may have. List children_; diff --git a/chrome/browser/extensions/extension_prefs.cc b/chrome/browser/extensions/extension_prefs.cc index c33511e..af4a262 100644 --- a/chrome/browser/extensions/extension_prefs.cc +++ b/chrome/browser/extensions/extension_prefs.cc @@ -239,10 +239,10 @@ static void CleanupBadExtensionKeys(const FilePath& root_dir, } } -static void ExtentToStringSet(const ExtensionExtent& host_extent, +static void ExtentToStringSet(const URLPatternSet& host_extent, std::set<std::string>* result) { - ExtensionExtent::PatternList patterns = host_extent.patterns(); - ExtensionExtent::PatternList::const_iterator i; + URLPatternList patterns = host_extent.patterns(); + URLPatternList::const_iterator i; for (i = patterns.begin(); i != patterns.end(); ++i) result->insert(i->GetAsString()); @@ -653,7 +653,7 @@ bool ExtensionPrefs::GetGrantedPermissions( const std::string& extension_id, bool* full_access, std::set<std::string>* api_permissions, - ExtensionExtent* host_extent) { + URLPatternSet* host_extent) { CHECK(Extension::IdIsValid(extension_id)); const DictionaryValue* ext = GetExtensionPref(extension_id); @@ -698,7 +698,7 @@ void ExtensionPrefs::AddGrantedPermissions( const std::string& extension_id, const bool full_access, const std::set<std::string>& api_permissions, - const ExtensionExtent& host_extent) { + const URLPatternSet& host_extent) { CHECK(Extension::IdIsValid(extension_id)); UpdateExtensionPref(extension_id, kPrefGrantedPermissionsAll, diff --git a/chrome/browser/extensions/extension_prefs.h b/chrome/browser/extensions/extension_prefs.h index bb7dd54..034fa62 100644 --- a/chrome/browser/extensions/extension_prefs.h +++ b/chrome/browser/extensions/extension_prefs.h @@ -17,6 +17,7 @@ #include "googleurl/src/gurl.h" class ExtensionPrefValueMap; +class URLPatternSet; // Class for managing global and per-extension preferences. // @@ -180,7 +181,7 @@ class ExtensionPrefs { bool GetGrantedPermissions(const std::string& extension_id, bool* full_access, std::set<std::string>* api_permissions, - ExtensionExtent* host_extent); + URLPatternSet* host_extent); // Adds the specified |api_permissions|, |host_extent| and |full_access| // to the granted permissions for extension with |extension_id|. @@ -189,7 +190,7 @@ class ExtensionPrefs { void AddGrantedPermissions(const std::string& extension_id, const bool full_access, const std::set<std::string>& api_permissions, - const ExtensionExtent& host_extent); + const URLPatternSet& host_extent); // Returns true if the user enabled this extension to be loaded in incognito // mode. diff --git a/chrome/browser/extensions/extension_prefs_unittest.cc b/chrome/browser/extensions/extension_prefs_unittest.cc index 6b8e7a9..5b790d1 100644 --- a/chrome/browser/extensions/extension_prefs_unittest.cc +++ b/chrome/browser/extensions/extension_prefs_unittest.cc @@ -38,15 +38,15 @@ const char kDefaultPref4[] = "default pref 4"; } // namespace -static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { +static void AddPattern(URLPatternSet* extent, const std::string& pattern) { int schemes = URLPattern::SCHEME_ALL; extent->AddPattern(URLPattern(schemes, pattern)); } -static void AssertEqualExtents(ExtensionExtent* extent1, - ExtensionExtent* extent2) { - std::vector<URLPattern> patterns1 = extent1->patterns(); - std::vector<URLPattern> patterns2 = extent2->patterns(); +static void AssertEqualExtents(URLPatternSet* extent1, + URLPatternSet* extent2) { + URLPatternList patterns1 = extent1->patterns(); + URLPatternList patterns2 = extent2->patterns(); std::set<std::string> strings1; EXPECT_EQ(patterns1.size(), patterns2.size()); @@ -231,8 +231,8 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { std::set<std::string> empty_set; std::set<std::string> api_perms; bool full_access = false; - ExtensionExtent host_perms; - ExtensionExtent empty_extent; + URLPatternSet host_perms; + URLPatternSet empty_extent; // Make sure both granted api and host permissions start empty. EXPECT_FALSE(prefs()->GetGrantedPermissions( @@ -249,7 +249,7 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { EXPECT_EQ(api_perm_set1_, api_perms); EXPECT_TRUE(host_perms.is_empty()); EXPECT_FALSE(full_access); - host_perms.ClearPaths(); + host_perms.ClearPatterns(); api_perms.clear(); // Add part of the host permissions. @@ -260,7 +260,7 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { EXPECT_FALSE(full_access); EXPECT_EQ(api_perm_set1_, api_perms); AssertEqualExtents(&host_perm_set1_, &host_perms); - host_perms.ClearPaths(); + host_perms.ClearPatterns(); api_perms.clear(); // Add the rest of both the api and host permissions. @@ -278,7 +278,7 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { virtual void Verify() { std::set<std::string> api_perms; - ExtensionExtent host_perms; + URLPatternSet host_perms; bool full_access; EXPECT_TRUE(prefs()->GetGrantedPermissions( @@ -292,12 +292,12 @@ class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { std::string extension_id_; std::set<std::string> api_perm_set1_; std::set<std::string> api_perm_set2_; - ExtensionExtent host_perm_set1_; - ExtensionExtent host_perm_set2_; + URLPatternSet host_perm_set1_; + URLPatternSet host_perm_set2_; std::set<std::string> api_permissions_; - ExtensionExtent host_permissions_; + URLPatternSet host_permissions_; }; TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {} diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc index e567d05..d0a9398 100644 --- a/chrome/browser/extensions/extension_service.cc +++ b/chrome/browser/extensions/extension_service.cc @@ -878,7 +878,7 @@ void ExtensionService::GrantPermissions(const Extension* extension) { // We only maintain the granted permissions prefs for INTERNAL extensions. CHECK_EQ(Extension::INTERNAL, extension->location()); - ExtensionExtent effective_hosts = extension->GetEffectiveHostPermissions(); + URLPatternSet effective_hosts = extension->GetEffectiveHostPermissions(); extension_prefs_->AddGrantedPermissions(extension->id(), extension->HasFullPermissions(), extension->api_permissions(), @@ -1821,7 +1821,7 @@ void ExtensionService::DisableIfPrivilegeIncrease(const Extension* extension) { true, true, false); bool granted_full_access; std::set<std::string> granted_apis; - ExtensionExtent granted_extent; + URLPatternSet granted_extent; bool is_extension_upgrade = old != NULL; bool is_privilege_increase = false; @@ -2028,7 +2028,7 @@ const Extension* ExtensionService::GetExtensionByURL(const GURL& url) { const Extension* ExtensionService::GetExtensionByWebExtent(const GURL& url) { for (size_t i = 0; i < extensions_.size(); ++i) { - if (extensions_[i]->web_extent().ContainsURL(url)) + if (extensions_[i]->web_extent().MatchesURL(url)) return extensions_[i]; } return NULL; @@ -2045,7 +2045,7 @@ bool ExtensionService::ExtensionBindingsAllowed(const GURL& url) { } const Extension* ExtensionService::GetExtensionByOverlappingWebExtent( - const ExtensionExtent& extent) { + const URLPatternSet& extent) { for (size_t i = 0; i < extensions_.size(); ++i) { if (extensions_[i]->web_extent().OverlapsWith(extent)) return extensions_[i]; diff --git a/chrome/browser/extensions/extension_service.h b/chrome/browser/extensions/extension_service.h index 66a32c0..2c1587e 100644 --- a/chrome/browser/extensions/extension_service.h +++ b/chrome/browser/extensions/extension_service.h @@ -349,7 +349,7 @@ class ExtensionService // Returns an extension that contains any URL that overlaps with the given // extent, if one exists. const Extension* GetExtensionByOverlappingWebExtent( - const ExtensionExtent& extent); + const URLPatternSet& extent); // Returns true if |url| should get extension api bindings and be permitted // to make api calls. Note that this is independent of what extension diff --git a/chrome/browser/extensions/extension_service_unittest.cc b/chrome/browser/extensions/extension_service_unittest.cc index 89256c5..5a3ce52 100644 --- a/chrome/browser/extensions/extension_service_unittest.cc +++ b/chrome/browser/extensions/extension_service_unittest.cc @@ -109,15 +109,15 @@ static std::vector<std::string> GetErrors() { return ret_val; } -static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { +static void AddPattern(URLPatternSet* extent, const std::string& pattern) { int schemes = URLPattern::SCHEME_ALL; extent->AddPattern(URLPattern(schemes, pattern)); } -static void AssertEqualExtents(ExtensionExtent* extent1, - ExtensionExtent* extent2) { - std::vector<URLPattern> patterns1 = extent1->patterns(); - std::vector<URLPattern> patterns2 = extent2->patterns(); +static void AssertEqualExtents(URLPatternSet* extent1, + URLPatternSet* extent2) { + URLPatternList patterns1 = extent1->patterns(); + URLPatternList patterns2 = extent2->patterns(); std::set<std::string> strings1; EXPECT_EQ(patterns1.size(), patterns2.size()); @@ -1033,7 +1033,7 @@ 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 std::vector<URLPattern> permissions = extension->host_permissions(); + const URLPatternList permissions = extension->host_permissions(); ASSERT_EQ(2u, permissions.size()); EXPECT_EQ("http://*.google.com/*", permissions[0].GetAsString()); EXPECT_EQ("https://*.google.com/*", permissions[1].GetAsString()); @@ -1374,8 +1374,8 @@ TEST_F(ExtensionServiceTest, GrantedPermissions) { std::set<std::string> expected_api_perms; std::set<std::string> known_api_perms; bool full_access; - ExtensionExtent expected_host_perms; - ExtensionExtent known_host_perms; + URLPatternSet expected_host_perms; + URLPatternSet known_host_perms; // Make sure there aren't any granted permissions before the // extension is installed. @@ -1435,7 +1435,7 @@ TEST_F(ExtensionServiceTest, GrantedFullAccessPermissions) { bool full_access; std::set<std::string> api_permissions; - ExtensionExtent host_permissions; + URLPatternSet host_permissions; EXPECT_TRUE(prefs->GetGrantedPermissions( extension_id, &full_access, &api_permissions, &host_permissions)); @@ -1467,7 +1467,7 @@ TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { ExtensionPrefs* prefs = service_->extension_prefs(); std::set<std::string> expected_api_permissions; - ExtensionExtent expected_host_permissions; + URLPatternSet expected_host_permissions; expected_api_permissions.insert("tabs"); AddPattern(&expected_host_permissions, "http://*.google.com/*"); @@ -1498,7 +1498,7 @@ TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id)); std::set<std::string> current_api_permissions; - ExtensionExtent current_host_permissions; + URLPatternSet current_host_permissions; bool current_full_access; ASSERT_TRUE(prefs->GetGrantedPermissions(extension_id, @@ -1517,7 +1517,7 @@ TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { api_permissions.clear(); host_permissions.clear(); current_api_permissions.clear(); - current_host_permissions.ClearPaths(); + current_host_permissions.ClearPatterns(); api_permissions.insert("tabs"); host_permissions.insert("http://*.google.com/*"); @@ -1553,7 +1553,7 @@ TEST_F(ExtensionServiceTest, GrantedAPIAndHostPermissions) { // Tests that the granted permissions preferences are initialized when // migrating from the old pref schema. current_api_permissions.clear(); - current_host_permissions.ClearPaths(); + current_host_permissions.ClearPatterns(); ClearPref(extension_id, "granted_permissions"); @@ -1836,7 +1836,7 @@ TEST_F(ExtensionServiceTest, InstallAppsWithUnlimtedStorage) { const std::string id1 = extension->id(); EXPECT_TRUE(extension->HasApiPermission( Extension::kUnlimitedStoragePermission)); - EXPECT_TRUE(extension->web_extent().ContainsURL( + EXPECT_TRUE(extension->web_extent().MatchesURL( extension->GetFullLaunchURL())); const GURL origin1(extension->GetFullLaunchURL().GetOrigin()); EXPECT_TRUE(profile_->GetExtensionSpecialStoragePolicy()-> @@ -1850,7 +1850,7 @@ TEST_F(ExtensionServiceTest, InstallAppsWithUnlimtedStorage) { const std::string id2 = extension->id(); EXPECT_TRUE(extension->HasApiPermission( Extension::kUnlimitedStoragePermission)); - EXPECT_TRUE(extension->web_extent().ContainsURL( + EXPECT_TRUE(extension->web_extent().MatchesURL( extension->GetFullLaunchURL())); const GURL origin2(extension->GetFullLaunchURL().GetOrigin()); EXPECT_EQ(origin1, origin2); diff --git a/chrome/browser/extensions/extension_webrequest_api.cc b/chrome/browser/extensions/extension_webrequest_api.cc index 63ddd3e..f325e7f 100644 --- a/chrome/browser/extensions/extension_webrequest_api.cc +++ b/chrome/browser/extensions/extension_webrequest_api.cc @@ -805,7 +805,7 @@ ExtensionWebRequestEventRouter::GetMatchingListeners( std::set<EventListener>& listeners = listeners_[profile_id][event_name]; for (std::set<EventListener>::iterator it = listeners.begin(); it != listeners.end(); ++it) { - if (!it->filter.urls.is_empty() && !it->filter.urls.ContainsURL(url)) + if (!it->filter.urls.is_empty() && !it->filter.urls.MatchesURL(url)) continue; if (it->filter.tab_id != -1 && tab_id != it->filter.tab_id) continue; diff --git a/chrome/browser/extensions/extension_webrequest_api.h b/chrome/browser/extensions/extension_webrequest_api.h index 3c83a5e..8913702 100644 --- a/chrome/browser/extensions/extension_webrequest_api.h +++ b/chrome/browser/extensions/extension_webrequest_api.h @@ -15,7 +15,7 @@ #include "base/time.h" #include "chrome/browser/extensions/extension_function.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/common/extensions/extension_extent.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "ipc/ipc_message.h" #include "net/base/completion_callback.h" #include "webkit/glue/resource_type.h" @@ -52,7 +52,7 @@ class ExtensionWebRequestEventRouter { // Internal representation of the webRequest.RequestFilter type, used to // filter what network events an extension cares about. struct RequestFilter { - ExtensionExtent urls; + URLPatternSet urls; std::vector<ResourceType::Type> types; int tab_id; int window_id; diff --git a/chrome/browser/extensions/extensions_ui.cc b/chrome/browser/extensions/extensions_ui.cc index 37e4fd9..813461a 100644 --- a/chrome/browser/extensions/extensions_ui.cc +++ b/chrome/browser/extensions/extensions_ui.cc @@ -792,7 +792,7 @@ void ExtensionsDOMHandler::GetActivePagesForExtensionProcess( if (url.SchemeIs(chrome::kExtensionScheme)) { if (url.host() != extension->id()) continue; - } else if (!extension->web_extent().ContainsURL(url)) { + } else if (!extension->web_extent().MatchesURL(url)) { continue; } diff --git a/chrome/browser/extensions/user_script_listener.cc b/chrome/browser/extensions/user_script_listener.cc index 8586b0e..dcc64c8 100644 --- a/chrome/browser/extensions/user_script_listener.cc +++ b/chrome/browser/extensions/user_script_listener.cc @@ -50,7 +50,7 @@ bool UserScriptListener::ShouldDelayRequest( for (URLPatterns::iterator it = url_patterns_.begin(); it != url_patterns_.end(); ++it) { - if ((*it).MatchesUrl(request->url())) { + if ((*it).MatchesURL(request->url())) { // One of the user scripts wants to inject into this request, but the // script isn't ready yet. Delay the request. delayed_request_ids_.push_front(request_id); diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index 8d9969f..4885965 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -243,12 +243,12 @@ static bool ExtensionContextMatch(const ContextMenuParams& params, return false; } -static bool ExtensionPatternMatch(const ExtensionExtent& patterns, +static bool ExtensionPatternMatch(const URLPatternSet& patterns, const GURL& url) { // No patterns means no restriction, so that implicitly matches. if (patterns.is_empty()) return true; - return patterns.ContainsURL(url); + return patterns.MatchesURL(url); } static const GURL& GetDocumentURL(const ContextMenuParams& params) { diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index c423f67..13e2a7c 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -577,7 +577,7 @@ TabContents* Browser::OpenApplicationWindow( GURL url; if (!url_input.is_empty()) { if (extension) - DCHECK(extension->web_extent().ContainsURL(url_input)); + DCHECK(extension->web_extent().MatchesURL(url_input)); url = url_input; } else { DCHECK(extension); // Empty url and no extension. Nothing to open. diff --git a/chrome/browser/ui/webui/options/extension_settings_handler.cc b/chrome/browser/ui/webui/options/extension_settings_handler.cc index c1760b7..7f0e369 100644 --- a/chrome/browser/ui/webui/options/extension_settings_handler.cc +++ b/chrome/browser/ui/webui/options/extension_settings_handler.cc @@ -871,7 +871,7 @@ void ExtensionsDOMHandler::GetActivePagesForExtensionProcess( if (url.SchemeIs(chrome::kExtensionScheme)) { if (url.host() != extension->id()) continue; - } else if (!extension->web_extent().ContainsURL(url)) { + } else if (!extension->web_extent().MatchesURL(url)) { continue; } diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi index 458168d..c156aa1 100644 --- a/chrome/chrome_common.gypi +++ b/chrome/chrome_common.gypi @@ -164,8 +164,6 @@ 'common/extensions/extension_constants.h', 'common/extensions/extension_error_utils.cc', 'common/extensions/extension_error_utils.h', - 'common/extensions/extension_extent.cc', - 'common/extensions/extension_extent.h', 'common/extensions/extension_file_util.cc', 'common/extensions/extension_file_util.h', 'common/extensions/extension_icon_set.cc', @@ -193,6 +191,8 @@ 'common/extensions/update_manifest.h', 'common/extensions/url_pattern.cc', 'common/extensions/url_pattern.h', + 'common/extensions/url_pattern_set.cc', + 'common/extensions/url_pattern_set.h', 'common/extensions/user_script.cc', 'common/extensions/user_script.h', 'common/favicon_url.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index d04dbc2..4c5fe45 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1835,7 +1835,6 @@ 'common/content_settings_helper_unittest.cc', 'common/deprecated/event_sys_unittest.cc', 'common/extensions/extension_action_unittest.cc', - 'common/extensions/extension_extent_unittest.cc', 'common/extensions/extension_file_util_unittest.cc', 'common/extensions/extension_icon_set_unittest.cc', 'common/extensions/extension_l10n_util_unittest.cc', @@ -1848,6 +1847,7 @@ 'common/extensions/extension_unpacker_unittest.cc', 'common/extensions/update_manifest_unittest.cc', 'common/extensions/url_pattern_unittest.cc', + 'common/extensions/url_pattern_set_unittest.cc', 'common/extensions/user_script_unittest.cc', 'common/guid_unittest.cc', 'common/important_file_writer_unittest.cc', diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc index 5914bec..c9cb656 100644 --- a/chrome/common/extensions/extension.cc +++ b/chrome/common/extensions/extension.cc @@ -1178,7 +1178,7 @@ bool Extension::LoadIsApp(const DictionaryValue* manifest, bool Extension::LoadExtent(const DictionaryValue* manifest, const char* key, - ExtensionExtent* extent, + URLPatternSet* extent, const char* list_error, const char* value_error, URLPattern::ParseOption parse_strictness, @@ -1566,7 +1566,7 @@ bool Extension::FormatPEMForFileOutput(const std::string& input, // static bool Extension::IsPrivilegeIncrease(const bool granted_full_access, const std::set<std::string>& granted_apis, - const ExtensionExtent& granted_extent, + const URLPatternSet& granted_extent, const Extension* new_extension) { // If the extension had native code access, we don't need to go any further. // Things can't get any worse. @@ -1583,7 +1583,7 @@ bool Extension::IsPrivilegeIncrease(const bool granted_full_access, if (new_extension->HasEffectiveAccessToAllHosts()) return true; - const ExtensionExtent new_extent = + const URLPatternSet new_extent = new_extension->GetEffectiveHostPermissions(); if (IsElevatedHostList(granted_extent.patterns(), new_extent.patterns())) @@ -2736,7 +2736,7 @@ bool Extension::HasApiPermission( bool Extension::HasHostPermission(const GURL& url) const { for (URLPatternList::const_iterator host = host_permissions().begin(); host != host_permissions().end(); ++host) { - if (host->MatchesUrl(url)) + if (host->MatchesURL(url)) return true; } return false; @@ -2760,7 +2760,7 @@ void Extension::InitEffectiveHostPermissions() { for (UserScriptList::const_iterator content_script = content_scripts().begin(); content_script != content_scripts().end(); ++content_script) { - UserScript::PatternList::const_iterator pattern = + URLPatternList::const_iterator pattern = content_script->url_patterns().begin(); for (; pattern != content_script->url_patterns().end(); ++pattern) effective_host_permissions_.AddPattern(*pattern); @@ -2813,12 +2813,12 @@ bool Extension::CanExecuteScriptOnPage(const GURL& page_url, // If a script is specified, use its matches. if (script) - return script->MatchesUrl(page_url); + return script->MatchesURL(page_url); // Otherwise, see if this extension has permission to execute script // programmatically on pages. for (size_t i = 0; i < host_permissions_.size(); ++i) { - if (host_permissions_[i].MatchesUrl(page_url)) + if (host_permissions_[i].MatchesURL(page_url)) return true; } @@ -2832,7 +2832,7 @@ bool Extension::CanExecuteScriptOnPage(const GURL& page_url, // static bool Extension::HasEffectiveAccessToAllHosts( - const ExtensionExtent& effective_host_permissions, + const URLPatternSet& effective_host_permissions, const std::set<std::string>& api_permissions) { const URLPatternList patterns = effective_host_permissions.patterns(); for (URLPatternList::const_iterator host = patterns.begin(); @@ -2924,7 +2924,7 @@ bool Extension::OverlapsWithOrigin(const GURL& origin) const { origin_only_pattern.set_host(origin.host()); origin_only_pattern.SetPath("/*"); - ExtensionExtent origin_only_pattern_list; + URLPatternSet origin_only_pattern_list; origin_only_pattern_list.AddPattern(origin_only_pattern); return web_extent().OverlapsWith(origin_only_pattern_list); diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h index 8fc1641..cd132e2 100644 --- a/chrome/common/extensions/extension.h +++ b/chrome/common/extensions/extension.h @@ -17,10 +17,10 @@ #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "chrome/common/extensions/extension_constants.h" -#include "chrome/common/extensions/extension_extent.h" #include "chrome/common/extensions/extension_icon_set.h" #include "chrome/common/extensions/user_script.h" #include "chrome/common/extensions/url_pattern.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "googleurl/src/gurl.h" #include "ui/gfx/size.h" @@ -434,7 +434,7 @@ class Extension : public base::RefCountedThreadSafe<Extension> { // |granted_extent| and |granted_full_access|. static bool IsPrivilegeIncrease(const bool granted_full_access, const std::set<std::string>& granted_apis, - const ExtensionExtent& granted_extent, + const URLPatternSet& granted_extent, const Extension* new_extension); // Given an extension and icon size, read it if present and decode it into @@ -479,14 +479,14 @@ class Extension : public base::RefCountedThreadSafe<Extension> { // effective access to all hosts. See the non-static version of the method // for more details. static bool HasEffectiveAccessToAllHosts( - const ExtensionExtent& effective_host_permissions, + const URLPatternSet& effective_host_permissions, const std::set<std::string>& api_permissions); bool HasApiPermission(const std::string& function_name) const { return HasApiPermission(this->api_permissions(), function_name); } - const ExtensionExtent& GetEffectiveHostPermissions() const { + const URLPatternSet& GetEffectiveHostPermissions() const { return effective_host_permissions_; } @@ -629,7 +629,7 @@ class Extension : public base::RefCountedThreadSafe<Extension> { bool is_hosted_app() const { return is_app() && !web_extent().is_empty(); } bool is_packaged_app() const { return is_app() && web_extent().is_empty(); } bool is_storage_isolated() const { return is_app() && is_storage_isolated_; } - const ExtensionExtent& web_extent() const { return extent_; } + const URLPatternSet& web_extent() const { return extent_; } const std::string& launch_local_path() const { return launch_local_path_; } const std::string& launch_web_url() const { return launch_web_url_; } extension_misc::LaunchContainer launch_container() const { @@ -710,7 +710,7 @@ class Extension : public base::RefCountedThreadSafe<Extension> { bool LoadIsApp(const DictionaryValue* manifest, std::string* error); bool LoadExtent(const DictionaryValue* manifest, const char* key, - ExtensionExtent* extent, + URLPatternSet* extent, const char* list_error, const char* value_error, URLPattern::ParseOption parse_strictness, @@ -790,14 +790,14 @@ class Extension : public base::RefCountedThreadSafe<Extension> { bool incognito_split_mode_; // Defines the set of URLs in the extension's web content. - ExtensionExtent extent_; + URLPatternSet extent_; // The set of host permissions that the extension effectively has access to, // which is a merge of host_permissions_ and all of the match patterns in // any content scripts the extension has. This is used to determine which // URLs have the ability to load an extension's resources via embedded // chrome-extension: URLs (see extension_protocols.cc). - ExtensionExtent effective_host_permissions_; + URLPatternSet effective_host_permissions_; // The set of module-level APIs this extension can use. std::set<std::string> api_permissions_; diff --git a/chrome/common/extensions/extension_extent.cc b/chrome/common/extensions/extension_extent.cc deleted file mode 100644 index 5b8260a..0000000 --- a/chrome/common/extensions/extension_extent.cc +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/common/extensions/extension_extent.h" - -#include "chrome/common/extensions/url_pattern.h" -#include "googleurl/src/gurl.h" - -ExtensionExtent::ExtensionExtent() { -} - -ExtensionExtent::ExtensionExtent(const ExtensionExtent& rhs) - : patterns_(rhs.patterns_) { -} - -ExtensionExtent::~ExtensionExtent() { -} - -ExtensionExtent& ExtensionExtent::operator=(const ExtensionExtent& rhs) { - patterns_ = rhs.patterns_; - return *this; -} - -bool ExtensionExtent::is_empty() const { - return patterns_.empty(); -} - -void ExtensionExtent::AddPattern(const URLPattern& pattern) { - patterns_.push_back(pattern); -} - -void ExtensionExtent::ClearPaths() { - patterns_.clear(); -} - -bool ExtensionExtent::ContainsURL(const GURL& url) const { - for (PatternList::const_iterator pattern = patterns_.begin(); - pattern != patterns_.end(); ++pattern) { - if (pattern->MatchesUrl(url)) - return true; - } - - return false; -} - -bool ExtensionExtent::OverlapsWith(const ExtensionExtent& 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 (PatternList::const_iterator i = patterns_.begin(); - i != patterns_.end(); ++i) { - for (PatternList::const_iterator j = other.patterns().begin(); - j != other.patterns().end(); ++j) { - if (i->OverlapsWith(*j)) - return true; - } - } - - return false; -} diff --git a/chrome/common/extensions/extension_extent.h b/chrome/common/extensions/extension_extent.h deleted file mode 100644 index cb4641f..0000000 --- a/chrome/common/extensions/extension_extent.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_COMMON_EXTENSIONS_EXTENSION_EXTENT_H_ -#define CHROME_COMMON_EXTENSIONS_EXTENSION_EXTENT_H_ -#pragma once - -#include <vector> - -class GURL; -class URLPattern; - -// Represents the set of URLs an extension uses for web content. -class ExtensionExtent { - public: - typedef std::vector<URLPattern> PatternList; - - ExtensionExtent(); - ExtensionExtent(const ExtensionExtent& rhs); - ~ExtensionExtent(); - ExtensionExtent& operator=(const ExtensionExtent& rhs); - - bool is_empty() const; - - const PatternList& patterns() const { return patterns_; } - void AddPattern(const URLPattern& pattern); - void ClearPaths(); - - // Test if the extent contains a URL. - bool ContainsURL(const GURL& url) const; - - // Returns true if there is a single URL that would be in two extents. - bool OverlapsWith(const ExtensionExtent& other) const; - - private: - // The list of URL patterns that comprise the extent. - PatternList patterns_; -}; - -#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_EXTENT_H_ diff --git a/chrome/common/extensions/extension_manifests_unittest.cc b/chrome/common/extensions/extension_manifests_unittest.cc index ae3379a..01e63df 100644 --- a/chrome/common/extensions/extension_manifests_unittest.cc +++ b/chrome/common/extensions/extension_manifests_unittest.cc @@ -512,7 +512,7 @@ TEST_F(ExtensionManifestTest, DefaultPathForExtent) { ASSERT_EQ(1u, extension->web_extent().patterns().size()); EXPECT_EQ("/*", extension->web_extent().patterns()[0].path()); - EXPECT_TRUE(extension->web_extent().ContainsURL( + EXPECT_TRUE(extension->web_extent().MatchesURL( GURL("http://www.google.com/monkey"))); } @@ -605,7 +605,7 @@ 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 FileBrowserHandler::PatternList& patterns = action->file_url_patterns(); + const URLPatternList& patterns = action->file_url_patterns(); ASSERT_EQ(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 cdb5e9d..2adc1d8 100644 --- a/chrome/common/extensions/extension_messages.cc +++ b/chrome/common/extensions/extension_messages.cc @@ -106,13 +106,13 @@ void ParamTraits<URLPattern>::Log(const param_type& p, std::string* l) { LogParam(p.GetAsString(), l); } -void ParamTraits<ExtensionExtent>::Write(Message* m, const param_type& p) { +void ParamTraits<URLPatternSet>::Write(Message* m, const param_type& p) { WriteParam(m, p.patterns()); } -bool ParamTraits<ExtensionExtent>::Read(const Message* m, void** iter, +bool ParamTraits<URLPatternSet>::Read(const Message* m, void** iter, param_type* p) { - std::vector<URLPattern> patterns; + URLPatternList patterns; bool success = ReadParam(m, iter, &patterns); if (!success) @@ -123,7 +123,7 @@ bool ParamTraits<ExtensionExtent>::Read(const Message* m, void** iter, return true; } -void ParamTraits<ExtensionExtent>::Log(const param_type& p, std::string* l) { +void ParamTraits<URLPatternSet>::Log(const param_type& p, std::string* l) { LogParam(p.patterns(), l); } diff --git a/chrome/common/extensions/extension_messages.h b/chrome/common/extensions/extension_messages.h index b33e7dea..7d29445 100644 --- a/chrome/common/extensions/extension_messages.h +++ b/chrome/common/extensions/extension_messages.h @@ -8,8 +8,8 @@ #include "base/shared_memory.h" #include "base/values.h" #include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_extent.h" #include "chrome/common/extensions/url_pattern.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "chrome/common/web_apps.h" #include "content/common/view_types.h" #include "ipc/ipc_message_macros.h" @@ -124,8 +124,8 @@ struct ParamTraits<URLPattern> { }; template <> -struct ParamTraits<ExtensionExtent> { - typedef ExtensionExtent param_type; +struct ParamTraits<URLPatternSet> { + typedef URLPatternSet param_type; static void Write(Message* m, const param_type& p); static bool Read(const Message* m, void** iter, param_type* p); static void Log(const param_type& p, std::string* l); diff --git a/chrome/common/extensions/extension_set.cc b/chrome/common/extensions/extension_set.cc index fcf96e6..0825089 100644 --- a/chrome/common/extensions/extension_set.cc +++ b/chrome/common/extensions/extension_set.cc @@ -46,7 +46,7 @@ const Extension* ExtensionSet::GetByURL(const GURL& url) const { ExtensionMap::const_iterator i = extensions_.begin(); for (; i != extensions_.end(); ++i) { - if (i->second->web_extent().ContainsURL(url)) + if (i->second->web_extent().MatchesURL(url)) return i->second.get(); } @@ -73,7 +73,7 @@ bool ExtensionSet::ExtensionBindingsAllowed(const GURL& url) const { ExtensionMap::const_iterator i = extensions_.begin(); for (; i != extensions_.end(); ++i) { if (i->second->location() == Extension::COMPONENT && - i->second->web_extent().ContainsURL(url)) + i->second->web_extent().MatchesURL(url)) return true; } diff --git a/chrome/common/extensions/extension_unittest.cc b/chrome/common/extensions/extension_unittest.cc index 8488984..ef6d8c0 100644 --- a/chrome/common/extensions/extension_unittest.cc +++ b/chrome/common/extensions/extension_unittest.cc @@ -48,7 +48,7 @@ void CompareLists(const std::vector<std::string>& expected, } } -static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { +static void AddPattern(URLPatternSet* extent, const std::string& pattern) { int schemes = URLPattern::SCHEME_ALL; extent->AddPattern(URLPattern(schemes, pattern)); } @@ -847,65 +847,65 @@ static scoped_refptr<Extension> LoadManifest(const std::string& dir, TEST(ExtensionTest, EffectiveHostPermissions) { scoped_refptr<Extension> extension; - ExtensionExtent hosts; + URLPatternSet hosts; extension = LoadManifest("effective_host_permissions", "empty.json"); EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size()); - EXPECT_FALSE(hosts.ContainsURL(GURL("http://www.google.com"))); + EXPECT_FALSE(hosts.MatchesURL(GURL("http://www.google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "one_host.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); - EXPECT_FALSE(hosts.ContainsURL(GURL("https://www.google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); + EXPECT_FALSE(hosts.MatchesURL(GURL("https://www.google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "one_host_wildcard.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://foo.google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://foo.google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "two_hosts.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.reddit.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "https_not_considered.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("https://google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("https://google.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "two_content_scripts.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://google.com"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.reddit.com"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://news.ycombinator.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.reddit.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://news.ycombinator.com"))); EXPECT_FALSE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "all_hosts.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://test/"))); - EXPECT_FALSE(hosts.ContainsURL(GURL("https://test/"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://test/"))); + EXPECT_FALSE(hosts.MatchesURL(GURL("https://test/"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "all_hosts2.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://test/"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://test/"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); hosts = extension->GetEffectiveHostPermissions(); - EXPECT_FALSE(hosts.ContainsURL(GURL("http://test/"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("https://test/"))); - EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); + EXPECT_FALSE(hosts.MatchesURL(GURL("http://test/"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("https://test/"))); + EXPECT_TRUE(hosts.MatchesURL(GURL("http://www.google.com"))); EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); } @@ -980,7 +980,7 @@ TEST(ExtensionTest, IsPrivilegeIncrease) { for (size_t j = 0; kTests[i].granted_apis[j] != NULL; ++j) granted_apis.insert(kTests[i].granted_apis[j]); - ExtensionExtent granted_hosts; + URLPatternSet granted_hosts; for (size_t j = 0; kTests[i].granted_hosts[j] != NULL; ++j) AddPattern(&granted_hosts, kTests[i].granted_hosts[j]); diff --git a/chrome/common/extensions/file_browser_handler.cc b/chrome/common/extensions/file_browser_handler.cc index ea270b4..784c990 100644 --- a/chrome/common/extensions/file_browser_handler.cc +++ b/chrome/common/extensions/file_browser_handler.cc @@ -14,19 +14,13 @@ FileBrowserHandler::~FileBrowserHandler() { } void FileBrowserHandler::AddPattern(const URLPattern& pattern) { - patterns_.push_back(pattern); + url_set_.AddPattern(pattern); } void FileBrowserHandler::ClearPatterns() { - patterns_.clear(); + url_set_.ClearPatterns(); } bool FileBrowserHandler::MatchesURL(const GURL& url) const { - for (PatternList::const_iterator pattern = patterns_.begin(); - pattern != patterns_.end(); ++pattern) { - if (pattern->MatchesUrl(url)) - return true; - } - return false; + return url_set_.MatchesURL(url); } - diff --git a/chrome/common/extensions/file_browser_handler.h b/chrome/common/extensions/file_browser_handler.h index 420be17..74e478c 100644 --- a/chrome/common/extensions/file_browser_handler.h +++ b/chrome/common/extensions/file_browser_handler.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "chrome/common/extensions/url_pattern.h" +#include "chrome/common/extensions/url_pattern_set.h" #include "googleurl/src/gurl.h" class URLPattern; @@ -18,8 +19,6 @@ class URLPattern; // FileBrowserHandler encapsulates the state of a file browser action. class FileBrowserHandler { public: - typedef std::vector<URLPattern> PatternList; - FileBrowserHandler(); ~FileBrowserHandler(); @@ -38,7 +37,9 @@ class FileBrowserHandler { void set_title(const std::string& title) { title_ = title; } // File schema URL patterns. - const PatternList& file_url_patterns() const { return patterns_; } + const URLPatternList& file_url_patterns() const { + return url_set_.patterns(); + } void AddPattern(const URLPattern& pattern); bool MatchesURL(const GURL& url) const; void ClearPatterns(); @@ -58,7 +59,7 @@ class FileBrowserHandler { // The id for the FileBrowserHandler, for example: "PdfFileAction". std::string id_; // A list of file filters. - PatternList patterns_; + URLPatternSet url_set_; }; #endif // CHROME_COMMON_EXTENSIONS_FILE_BROWSER_HANDLER_H_ diff --git a/chrome/common/extensions/url_pattern.cc b/chrome/common/extensions/url_pattern.cc index 986487a..f041256 100644 --- a/chrome/common/extensions/url_pattern.cc +++ b/chrome/common/extensions/url_pattern.cc @@ -215,7 +215,7 @@ void URLPattern::SetPath(const std::string& path) { ReplaceSubstringsAfterOffset(&path_escaped_, 0, "?", "\\?"); } -bool URLPattern::MatchesUrl(const GURL &test) const { +bool URLPattern::MatchesURL(const GURL &test) const { if (!MatchesScheme(test.scheme())) return false; @@ -318,7 +318,7 @@ bool URLPattern::OverlapsWith(const URLPattern& other) const { return false; // We currently only use OverlapsWith() for the patterns inside - // ExtensionExtent. In those cases, we know that the path will have only a + // URLPatternSet. In those cases, we know that the path will have only a // single wildcard at the end. This makes figuring out overlap much easier. It // seems like there is probably a computer-sciency way to solve the general // case, but we don't need that yet. diff --git a/chrome/common/extensions/url_pattern.h b/chrome/common/extensions/url_pattern.h index cf89382..e64f176 100644 --- a/chrome/common/extensions/url_pattern.h +++ b/chrome/common/extensions/url_pattern.h @@ -182,7 +182,7 @@ class URLPattern { bool IsValidScheme(const std::string& scheme) const; // Returns true if this instance matches the specified URL. - bool MatchesUrl(const GURL& url) const; + bool MatchesURL(const GURL& url) const; // Returns true if |test| matches our scheme. bool MatchesScheme(const std::string& test) const; diff --git a/chrome/common/extensions/url_pattern_set.cc b/chrome/common/extensions/url_pattern_set.cc new file mode 100644 index 0000000..c8374ef --- /dev/null +++ b/chrome/common/extensions/url_pattern_set.cc @@ -0,0 +1,60 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/extensions/url_pattern_set.h" + +#include "chrome/common/extensions/url_pattern.h" +#include "googleurl/src/gurl.h" + +URLPatternSet::URLPatternSet() { +} + +URLPatternSet::URLPatternSet(const URLPatternSet& rhs) + : patterns_(rhs.patterns_) { +} + +URLPatternSet::~URLPatternSet() { +} + +URLPatternSet& URLPatternSet::operator=(const URLPatternSet& rhs) { + patterns_ = rhs.patterns_; + return *this; +} + +bool URLPatternSet::is_empty() const { + return patterns_.empty(); +} + +void URLPatternSet::AddPattern(const URLPattern& pattern) { + patterns_.push_back(pattern); +} + +void URLPatternSet::ClearPatterns() { + patterns_.clear(); +} + +bool URLPatternSet::MatchesURL(const GURL& url) const { + for (URLPatternList::const_iterator pattern = patterns_.begin(); + pattern != patterns_.end(); ++pattern) { + if (pattern->MatchesURL(url)) + return true; + } + + return false; +} + +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(); + i != patterns_.end(); ++i) { + for (URLPatternList::const_iterator j = other.patterns().begin(); + j != other.patterns().end(); ++j) { + if (i->OverlapsWith(*j)) + return true; + } + } + + return false; +} diff --git a/chrome/common/extensions/url_pattern_set.h b/chrome/common/extensions/url_pattern_set.h new file mode 100644 index 0000000..ac50afc --- /dev/null +++ b/chrome/common/extensions/url_pattern_set.h @@ -0,0 +1,40 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_EXTENSIONS_URL_PATTERN_SET_H_ +#define CHROME_COMMON_EXTENSIONS_URL_PATTERN_SET_H_ +#pragma once + +#include <vector> + +#include "chrome/common/extensions/url_pattern.h" + +class GURL; + +// Represents the set of URLs an extension uses for web content. +class URLPatternSet { + public: + URLPatternSet(); + URLPatternSet(const URLPatternSet& rhs); + ~URLPatternSet(); + URLPatternSet& operator=(const URLPatternSet& rhs); + + bool is_empty() const; + + const URLPatternList& patterns() const { return patterns_; } + void AddPattern(const URLPattern& pattern); + void ClearPatterns(); + + // Test if the extent contains a URL. + bool MatchesURL(const GURL& url) const; + + // Returns true if there is a single URL that would be in two extents. + bool OverlapsWith(const URLPatternSet& other) const; + + private: + // The list of URL patterns that comprise the extent. + URLPatternList patterns_; +}; + +#endif // CHROME_COMMON_EXTENSIONS_URL_PATTERN_SET_H_ diff --git a/chrome/common/extensions/extension_extent_unittest.cc b/chrome/common/extensions/url_pattern_set_unittest.cc index 8297eca..5336410 100644 --- a/chrome/common/extensions/extension_extent_unittest.cc +++ b/chrome/common/extensions/url_pattern_set_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -14,43 +14,43 @@ static const int kAllSchemes = URLPattern::SCHEME_FTP | URLPattern::SCHEME_CHROMEUI; -TEST(ExtensionExtentTest, Empty) { - ExtensionExtent extent; - EXPECT_FALSE(extent.ContainsURL(GURL("http://www.foo.com/bar"))); - EXPECT_FALSE(extent.ContainsURL(GURL())); - EXPECT_FALSE(extent.ContainsURL(GURL("invalid"))); +TEST(URLPatternSetTest, Empty) { + URLPatternSet extent; + EXPECT_FALSE(extent.MatchesURL(GURL("http://www.foo.com/bar"))); + EXPECT_FALSE(extent.MatchesURL(GURL())); + EXPECT_FALSE(extent.MatchesURL(GURL("invalid"))); } -TEST(ExtensionExtentTest, One) { - ExtensionExtent extent; +TEST(URLPatternSetTest, One) { + URLPatternSet extent; extent.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/*")); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/monkey"))); - EXPECT_FALSE(extent.ContainsURL(GURL("https://www.google.com/"))); - EXPECT_FALSE(extent.ContainsURL(GURL("https://www.microsoft.com/"))); + EXPECT_TRUE(extent.MatchesURL(GURL("http://www.google.com/"))); + EXPECT_TRUE(extent.MatchesURL(GURL("http://www.google.com/monkey"))); + EXPECT_FALSE(extent.MatchesURL(GURL("https://www.google.com/"))); + EXPECT_FALSE(extent.MatchesURL(GURL("https://www.microsoft.com/"))); } -TEST(ExtensionExtentTest, Two) { - ExtensionExtent extent; +TEST(URLPatternSetTest, Two) { + URLPatternSet extent; extent.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/*")); extent.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/*")); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.google.com/monkey"))); - EXPECT_TRUE(extent.ContainsURL(GURL("http://www.yahoo.com/monkey"))); - EXPECT_FALSE(extent.ContainsURL(GURL("https://www.apple.com/monkey"))); + EXPECT_TRUE(extent.MatchesURL(GURL("http://www.google.com/monkey"))); + EXPECT_TRUE(extent.MatchesURL(GURL("http://www.yahoo.com/monkey"))); + EXPECT_FALSE(extent.MatchesURL(GURL("https://www.apple.com/monkey"))); } -TEST(ExtensionExtentTest, OverlapsWith) { - ExtensionExtent extent1; +TEST(URLPatternSetTest, OverlapsWith) { + URLPatternSet extent1; extent1.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/f*")); extent1.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/b*")); - ExtensionExtent extent2; + URLPatternSet extent2; extent2.AddPattern(URLPattern(kAllSchemes, "http://www.reddit.com/f*")); extent2.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/z*")); - ExtensionExtent extent3; + URLPatternSet extent3; extent3.AddPattern(URLPattern(kAllSchemes, "http://www.google.com/q/*")); extent3.AddPattern(URLPattern(kAllSchemes, "http://www.yahoo.com/b/*")); diff --git a/chrome/common/extensions/url_pattern_unittest.cc b/chrome/common/extensions/url_pattern_unittest.cc index ef1406b..57c2ff2 100644 --- a/chrome/common/extensions/url_pattern_unittest.cc +++ b/chrome/common/extensions/url_pattern_unittest.cc @@ -91,11 +91,11 @@ TEST(ExtensionURLPatternTest, Match1) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://google.com"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://yahoo.com"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://google.com/foo"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("https://google.com"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://74.125.127.100/search"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://google.com"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://yahoo.com"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://google.com/foo"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("https://google.com"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://74.125.127.100/search"))); } // all domains @@ -108,10 +108,10 @@ TEST(ExtensionURLPatternTest, Match2) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/foo*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("https://www.google.com/foo"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("https://www.google.com/foobar"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("http://www.google.com/foo"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("https://www.google.com/"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("https://www.google.com/foo"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("https://www.google.com/foobar"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("http://www.google.com/foo"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("https://www.google.com/"))); } // subdomains @@ -125,11 +125,11 @@ TEST(URLPatternTest, Match3) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/foo*bar", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://google.com/foobar"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://www.google.com/foo?bar"))); - EXPECT_TRUE(pattern.MatchesUrl( + EXPECT_TRUE(pattern.MatchesURL(GURL("http://google.com/foobar"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://www.google.com/foo?bar"))); + EXPECT_TRUE(pattern.MatchesURL( GURL("http://monkey.images.google.com/foooobar"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("http://yahoo.com/foobar"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("http://yahoo.com/foobar"))); } // glob escaping @@ -142,8 +142,8 @@ TEST(ExtensionURLPatternTest, Match5) { EXPECT_FALSE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/foo?bar\\*baz", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("file:///foo?bar\\hellobaz"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("file:///fooXbar\\hellobaz"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo?bar\\hellobaz"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("file:///fooXbar\\hellobaz"))); } // ip addresses @@ -156,7 +156,7 @@ TEST(ExtensionURLPatternTest, Match6) { EXPECT_FALSE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1"))); } // subdomain matching with ip addresses @@ -171,7 +171,7 @@ TEST(ExtensionURLPatternTest, Match7) { EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); // Subdomain matching is never done if the argument has an IP address host. - EXPECT_FALSE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("http://127.0.0.1"))); }; // unicode @@ -187,9 +187,9 @@ TEST(ExtensionURLPatternTest, Match8) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/a%C2%81%E1*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl( + EXPECT_TRUE(pattern.MatchesURL( GURL("http://abc.\xe1\x80\xbf/a\xc2\x81\xe1xyz"))); - EXPECT_TRUE(pattern.MatchesUrl( + EXPECT_TRUE(pattern.MatchesURL( GURL("http://\xe1\x80\xbf/a\xc2\x81\xe1\xe1"))); }; @@ -203,9 +203,9 @@ TEST(ExtensionURLPatternTest, Match9) { EXPECT_FALSE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("chrome://favicon/http://google.com"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("chrome://favicon/https://google.com"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("chrome://history"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/https://google.com"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("chrome://history"))); }; // *:// @@ -221,9 +221,9 @@ TEST(ExtensionURLPatternTest, Match10) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_FALSE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("chrome://favicon/http://google.com"))); - EXPECT_FALSE(pattern.MatchesUrl(GURL("file:///foo/bar"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("file:///foo/bar"))); }; // <all_urls> @@ -238,9 +238,9 @@ TEST(ExtensionURLPatternTest, Match11) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_TRUE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("chrome://favicon/http://google.com"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("file:///foo/bar"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo/bar"))); }; // SCHEME_ALL matches all schemes. @@ -259,13 +259,13 @@ TEST(ExtensionURLPatternTest, Match12) { EXPECT_TRUE(pattern.match_subdomains()); EXPECT_TRUE(pattern.match_all_urls()); EXPECT_EQ("/*", pattern.path()); - EXPECT_TRUE(pattern.MatchesUrl(GURL("chrome://favicon/http://google.com"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("http://127.0.0.1"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("file:///foo/bar"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("chrome://newtab"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("about:blank"))); - EXPECT_TRUE(pattern.MatchesUrl(GURL("about:version"))); - EXPECT_TRUE(pattern.MatchesUrl( + EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://favicon/http://google.com"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("http://127.0.0.1"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("file:///foo/bar"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("chrome://newtab"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("about:blank"))); + EXPECT_TRUE(pattern.MatchesURL(GURL("about:version"))); + EXPECT_TRUE(pattern.MatchesURL( GURL("data:text/html;charset=utf-8,<html>asdf</html>"))); }; @@ -289,7 +289,7 @@ TEST(ExtensionURLPatternTest, Match13) { pattern.Parse(kMatch13UrlPatternTestCases[i].pattern, URLPattern::PARSE_STRICT)) << " while parsing " << kMatch13UrlPatternTestCases[i].pattern; - EXPECT_TRUE(pattern.MatchesUrl( + EXPECT_TRUE(pattern.MatchesURL( GURL(kMatch13UrlPatternTestCases[i].matches))) << " while matching " << kMatch13UrlPatternTestCases[i].matches; } @@ -298,7 +298,7 @@ TEST(ExtensionURLPatternTest, Match13) { URLPattern pattern(URLPattern::SCHEME_ALL); EXPECT_EQ(URLPattern::PARSE_SUCCESS, pattern.Parse("data:*", URLPattern::PARSE_STRICT)); - EXPECT_FALSE(pattern.MatchesUrl(GURL("about:blank"))); + EXPECT_FALSE(pattern.MatchesURL(GURL("about:blank"))); }; static const struct GetAsStringPatterns { @@ -369,15 +369,15 @@ TEST(ExtensionURLPatternTest, OverlapsWith) { } TEST(ExtensionURLPatternTest, ConvertToExplicitSchemes) { - std::vector<URLPattern> all_urls(URLPattern( + URLPatternList all_urls(URLPattern( kAllSchemes, "<all_urls>").ConvertToExplicitSchemes()); - std::vector<URLPattern> all_schemes(URLPattern( + URLPatternList all_schemes(URLPattern( kAllSchemes, "*://google.com/foo").ConvertToExplicitSchemes()); - std::vector<URLPattern> monkey(URLPattern( + URLPatternList monkey(URLPattern( URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS | URLPattern::SCHEME_FTP, "http://google.com/monkey").ConvertToExplicitSchemes()); diff --git a/chrome/common/extensions/user_script.cc b/chrome/common/extensions/user_script.cc index 6439338..ee0a924 100644 --- a/chrome/common/extensions/user_script.cc +++ b/chrome/common/extensions/user_script.cc @@ -9,17 +9,6 @@ namespace { -bool UrlMatchesPatterns(const UserScript::PatternList* patterns, - const GURL& url) { - for (UserScript::PatternList::const_iterator pattern = patterns->begin(); - pattern != patterns->end(); ++pattern) { - if (pattern->MatchesUrl(url)) - return true; - } - - return false; -} - bool UrlMatchesGlobs(const std::vector<std::string>* globs, const GURL& url) { for (std::vector<std::string>::const_iterator glob = globs->begin(); @@ -68,12 +57,12 @@ UserScript::~UserScript() { } void UserScript::add_url_pattern(const URLPattern& pattern) { - url_patterns_.push_back(pattern); + url_set_.AddPattern(pattern); } -bool UserScript::MatchesUrl(const GURL& url) const { - if (!url_patterns_.empty()) { - if (!UrlMatchesPatterns(&url_patterns_, url)) +bool UserScript::MatchesURL(const GURL& url) const { + if (!url_set_.is_empty()) { + if (!url_set_.MatchesURL(url)) return false; } @@ -123,9 +112,10 @@ void UserScript::Pickle(::Pickle* pickle) const { } // Write url patterns. - pickle->WriteSize(url_patterns_.size()); - for (PatternList::const_iterator pattern = url_patterns_.begin(); - pattern != url_patterns_.end(); ++pattern) { + URLPatternList pattern_list = url_set_.patterns(); + pickle->WriteSize(pattern_list.size()); + for (URLPatternList::const_iterator pattern = pattern_list.begin(); + pattern != pattern_list.end(); ++pattern) { pickle->WriteInt(pattern->valid_schemes()); pickle->WriteString(pattern->GetAsString()); } @@ -179,7 +169,7 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { size_t num_patterns = 0; CHECK(pickle.ReadSize(iter, &num_patterns)); - url_patterns_.clear(); + url_set_.ClearPatterns(); for (size_t i = 0; i < num_patterns; ++i) { int valid_schemes; CHECK(pickle.ReadInt(iter, &valid_schemes)); @@ -198,7 +188,7 @@ void UserScript::Unpickle(const ::Pickle& pickle, void** iter) { if (!had_file_scheme) pattern.set_valid_schemes(valid_schemes); - url_patterns_.push_back(pattern); + url_set_.AddPattern(pattern); } // Read js scripts. diff --git a/chrome/common/extensions/user_script.h b/chrome/common/extensions/user_script.h index 3d91de16..06b1a8d 100644 --- a/chrome/common/extensions/user_script.h +++ b/chrome/common/extensions/user_script.h @@ -13,6 +13,7 @@ #include "base/string_piece.h" #include "googleurl/src/gurl.h" #include "chrome/common/extensions/url_pattern.h" +#include "chrome/common/extensions/url_pattern_set.h" class Pickle; class URLPattern; @@ -21,8 +22,6 @@ class URLPattern; // extension. class UserScript { public: - typedef std::vector<URLPattern> PatternList; - // The file extension for standalone user scripts. static const char kFileExtension[]; @@ -150,7 +149,7 @@ class UserScript { // The URLPatterns, if any, that determine which pages this script runs // against. - const PatternList& url_patterns() const { return url_patterns_; } + const URLPatternList& url_patterns() const { return url_set_.patterns(); } void add_url_pattern(const URLPattern& pattern); // List of js scripts for this user script @@ -171,7 +170,7 @@ class UserScript { // Returns true if the script should be applied to the specified URL, false // otherwise. - bool MatchesUrl(const GURL& url) const; + bool MatchesURL(const GURL& url) const; // Serialize the UserScript into a pickle. The content of the scripts and // paths to UserScript::Files will not be serialized! @@ -207,7 +206,7 @@ class UserScript { // URLPatterns that determine pages to inject the script into. These are // only used with scripts that are part of extensions. - PatternList url_patterns_; + URLPatternSet url_set_; // List of js scripts defined in content_scripts FileList js_scripts_; diff --git a/chrome/common/extensions/user_script_unittest.cc b/chrome/common/extensions/user_script_unittest.cc index bec6436..f322cef 100644 --- a/chrome/common/extensions/user_script_unittest.cc +++ b/chrome/common/extensions/user_script_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -20,53 +20,53 @@ TEST(ExtensionUserScriptTest, Match1) { script.add_glob("*mail.google.com*"); script.add_glob("*mail.yahoo.com*"); script.add_glob("*mail.msn.com*"); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo"))); - EXPECT_TRUE(script.MatchesUrl(GURL("https://mail.google.com/foo"))); - EXPECT_TRUE(script.MatchesUrl(GURL("ftp://mail.google.com/foo"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://woo.mail.google.com/foo"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.yahoo.com/bar"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.msn.com/baz"))); - EXPECT_FALSE(script.MatchesUrl(GURL("http://www.hotmail.com"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("https://mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("ftp://mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://woo.mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.yahoo.com/bar"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.msn.com/baz"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://www.hotmail.com"))); script.add_exclude_glob("*foo*"); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com"))); - EXPECT_FALSE(script.MatchesUrl(GURL("http://mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://mail.google.com/foo"))); } TEST(ExtensionUserScriptTest, Match2) { UserScript script; script.add_glob("*mail.google.com/"); // GURL normalizes the URL to have a trailing "/" - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/"))); - EXPECT_FALSE(script.MatchesUrl(GURL("http://mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com/"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://mail.google.com/foo"))); } TEST(ExtensionUserScriptTest, Match3) { UserScript script; script.add_glob("http://mail.google.com/*"); // GURL normalizes the URL to have a trailing "/" - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://mail.google.com/foo"))); - EXPECT_FALSE(script.MatchesUrl(GURL("https://mail.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://mail.google.com/foo"))); + EXPECT_FALSE(script.MatchesURL(GURL("https://mail.google.com/foo"))); } TEST(ExtensionUserScriptTest, Match4) { UserScript script; script.add_glob("*"); - EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://hot.com/dog"))); - EXPECT_TRUE(script.MatchesUrl(GURL("https://hot.com/dog"))); - EXPECT_TRUE(script.MatchesUrl(GURL("file:///foo/bar"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://foo.com/bar"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://hot.com/dog"))); + EXPECT_TRUE(script.MatchesURL(GURL("https://hot.com/dog"))); + EXPECT_TRUE(script.MatchesURL(GURL("file:///foo/bar"))); } TEST(ExtensionUserScriptTest, Match5) { UserScript script; script.add_glob("*foo*"); - EXPECT_TRUE(script.MatchesUrl(GURL("http://foo.com/bar"))); - EXPECT_TRUE(script.MatchesUrl(GURL("http://baz.org/foo/bar"))); - EXPECT_FALSE(script.MatchesUrl(GURL("http://baz.org"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://foo.com/bar"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://baz.org/foo/bar"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://baz.org"))); } TEST(ExtensionUserScriptTest, Match6) { @@ -76,8 +76,8 @@ TEST(ExtensionUserScriptTest, Match6) { UserScript script; script.add_url_pattern(pattern); - EXPECT_TRUE(script.MatchesUrl(GURL("http://monkey.com/foobar"))); - EXPECT_FALSE(script.MatchesUrl(GURL("http://monkey.com/hotdog"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://monkey.com/foobar"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://monkey.com/hotdog"))); // NOTE: URLPattern is tested more extensively in url_pattern_unittest.cc. } @@ -95,23 +95,23 @@ TEST(ExtensionUserScriptTest, UrlPatternGlobInteraction) { script.add_glob("*bar*"); // No match, because it doesn't match the glob. - EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/foo"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/foo"))); script.add_exclude_glob("*baz*"); // No match, because it matches the exclude glob. - EXPECT_FALSE(script.MatchesUrl(GURL("http://www.google.com/baz"))); + EXPECT_FALSE(script.MatchesURL(GURL("http://www.google.com/baz"))); // Match, because it matches the glob, doesn't match the exclude glob. - EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/bar"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/bar"))); // Try with just a single exclude glob. script.clear_globs(); - EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); // Try with no globs or exclude globs. script.clear_exclude_globs(); - EXPECT_TRUE(script.MatchesUrl(GURL("http://www.google.com/foo"))); + EXPECT_TRUE(script.MatchesURL(GURL("http://www.google.com/foo"))); } TEST(ExtensionUserScriptTest, Pickle) { diff --git a/chrome/renderer/extensions/user_script_slave.cc b/chrome/renderer/extensions/user_script_slave.cc index 6506074..b8309ac 100644 --- a/chrome/renderer/extensions/user_script_slave.cc +++ b/chrome/renderer/extensions/user_script_slave.cc @@ -149,7 +149,7 @@ 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) { - std::vector<URLPattern> explicit_patterns = + URLPatternList explicit_patterns = script->url_patterns()[k].ConvertToExplicitSchemes(); for (size_t m = 0; m < explicit_patterns.size(); ++m) { temp_patterns.push_back(WebString::fromUTF8( |