diff options
author | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-07 23:06:00 +0000 |
---|---|---|
committer | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-01-07 23:06:00 +0000 |
commit | 599539805e61d2aeb26f837f3fcf8eecf51aa468 (patch) | |
tree | 683bd39cbeb469bd8cddb71108256314ecedf28b /extensions | |
parent | 8b9ff7f90d8297c8baf782ce726b6df1c4fc470c (diff) | |
download | chromium_src-599539805e61d2aeb26f837f3fcf8eecf51aa468.zip chromium_src-599539805e61d2aeb26f837f3fcf8eecf51aa468.tar.gz chromium_src-599539805e61d2aeb26f837f3fcf8eecf51aa468.tar.bz2 |
Move ExtensionService::GetExtensionById() to ExtensionRegistry
This will allow app_shell to use it without an ExtensionService
BUG=none
TEST=added unit_tests ExtensionRegistryTest.GetExtensionById
R=yoz@chromium.org
TBR=pkotwicz@chromium.org for chrome/browser/themes
Review URL: https://codereview.chromium.org/125573002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@243423 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/browser/extension_registry.cc | 27 | ||||
-rw-r--r-- | extensions/browser/extension_registry.h | 19 | ||||
-rw-r--r-- | extensions/browser/extension_registry_unittest.cc | 79 |
3 files changed, 125 insertions, 0 deletions
diff --git a/extensions/browser/extension_registry.cc b/extensions/browser/extension_registry.cc index f4bd91e..bc5247c 100644 --- a/extensions/browser/extension_registry.cc +++ b/extensions/browser/extension_registry.cc @@ -4,6 +4,7 @@ #include "extensions/browser/extension_registry.h" +#include "base/strings/string_util.h" #include "extensions/browser/extension_registry_factory.h" namespace extensions { @@ -16,6 +17,32 @@ ExtensionRegistry* ExtensionRegistry::Get(content::BrowserContext* context) { return ExtensionRegistryFactory::GetForBrowserContext(context); } +const Extension* ExtensionRegistry::GetExtensionById(const std::string& id, + int include_mask) const { + std::string lowercase_id = StringToLowerASCII(id); + if (include_mask & ENABLED) { + const Extension* extension = enabled_extensions_.GetByID(lowercase_id); + if (extension) + return extension; + } + if (include_mask & DISABLED) { + const Extension* extension = disabled_extensions_.GetByID(lowercase_id); + if (extension) + return extension; + } + if (include_mask & TERMINATED) { + const Extension* extension = terminated_extensions_.GetByID(lowercase_id); + if (extension) + return extension; + } + if (include_mask & BLACKLISTED) { + const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id); + if (extension) + return extension; + } + return NULL; +} + bool ExtensionRegistry::AddEnabled( const scoped_refptr<const Extension>& extension) { return enabled_extensions_.Insert(extension); diff --git a/extensions/browser/extension_registry.h b/extensions/browser/extension_registry.h index aedd11a..d878e61 100644 --- a/extensions/browser/extension_registry.h +++ b/extensions/browser/extension_registry.h @@ -24,6 +24,16 @@ class Extension; // share a single registry. class ExtensionRegistry : public BrowserContextKeyedService { public: + // Flags to pass to GetExtensionById() to select which sets to look in. + enum IncludeFlag { + NONE = 0, + ENABLED = 1 << 0, + DISABLED = 1 << 1, + TERMINATED = 1 << 2, + BLACKLISTED = 1 << 3, + EVERYTHING = (1 << 4) - 1, + }; + ExtensionRegistry(); virtual ~ExtensionRegistry(); @@ -45,6 +55,15 @@ class ExtensionRegistry : public BrowserContextKeyedService { return blacklisted_extensions_; } + // Find an extension by ID using |include_mask| to pick the sets to search: + // * enabled_extensions() --> ExtensionRegistry::ENABLED + // * disabled_extensions() --> ExtensionRegistry::DISABLED + // * terminated_extensions() --> ExtensionRegistry::TERMINATED + // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED + // Returns NULL if the extension is not found in the selected sets. + const Extension* GetExtensionById(const std::string& id, + int include_mask) const; + // Adds the specified extension to the enabled set. The registry becomes an // owner. Any previous extension with the same ID is removed. // Returns true if there is no previous extension. diff --git a/extensions/browser/extension_registry_unittest.cc b/extensions/browser/extension_registry_unittest.cc index 15c08e2..075019b 100644 --- a/extensions/browser/extension_registry_unittest.cc +++ b/extensions/browser/extension_registry_unittest.cc @@ -86,5 +86,84 @@ TEST_F(ExtensionRegistryTest, AddExtensionToRegistryTwice) { EXPECT_EQ(0u, registry.blacklisted_extensions().size()); } +TEST_F(ExtensionRegistryTest, GetExtensionById) { + ExtensionRegistry registry; + + // Trying to get an extension fails cleanly when the sets are empty. + EXPECT_FALSE( + registry.GetExtensionById("id", ExtensionRegistry::EVERYTHING)); + + scoped_refptr<Extension> enabled = + test_util::CreateExtensionWithID("enabled"); + scoped_refptr<Extension> disabled = + test_util::CreateExtensionWithID("disabled"); + scoped_refptr<Extension> terminated = + test_util::CreateExtensionWithID("terminated"); + scoped_refptr<Extension> blacklisted = + test_util::CreateExtensionWithID("blacklisted"); + + // Add an extension to each set. + registry.AddEnabled(enabled); + registry.AddDisabled(disabled); + registry.AddTerminated(terminated); + registry.AddBlacklisted(blacklisted); + + // Enabled is part of everything and the enabled list. + EXPECT_TRUE( + registry.GetExtensionById("enabled", ExtensionRegistry::EVERYTHING)); + EXPECT_TRUE( + registry.GetExtensionById("enabled", ExtensionRegistry::ENABLED)); + EXPECT_FALSE( + registry.GetExtensionById("enabled", ExtensionRegistry::DISABLED)); + EXPECT_FALSE( + registry.GetExtensionById("enabled", ExtensionRegistry::TERMINATED)); + EXPECT_FALSE( + registry.GetExtensionById("enabled", ExtensionRegistry::BLACKLISTED)); + + // Disabled is part of everything and the disabled list. + EXPECT_TRUE( + registry.GetExtensionById("disabled", ExtensionRegistry::EVERYTHING)); + EXPECT_FALSE( + registry.GetExtensionById("disabled", ExtensionRegistry::ENABLED)); + EXPECT_TRUE( + registry.GetExtensionById("disabled", ExtensionRegistry::DISABLED)); + EXPECT_FALSE( + registry.GetExtensionById("disabled", ExtensionRegistry::TERMINATED)); + EXPECT_FALSE( + registry.GetExtensionById("disabled", ExtensionRegistry::BLACKLISTED)); + + // Terminated is part of everything and the terminated list. + EXPECT_TRUE( + registry.GetExtensionById("terminated", ExtensionRegistry::EVERYTHING)); + EXPECT_FALSE( + registry.GetExtensionById("terminated", ExtensionRegistry::ENABLED)); + EXPECT_FALSE( + registry.GetExtensionById("terminated", ExtensionRegistry::DISABLED)); + EXPECT_TRUE( + registry.GetExtensionById("terminated", ExtensionRegistry::TERMINATED)); + EXPECT_FALSE( + registry.GetExtensionById("terminated", ExtensionRegistry::BLACKLISTED)); + + // Blacklisted is part of everything and the blacklisted list. + EXPECT_TRUE( + registry.GetExtensionById("blacklisted", ExtensionRegistry::EVERYTHING)); + EXPECT_FALSE( + registry.GetExtensionById("blacklisted", ExtensionRegistry::ENABLED)); + EXPECT_FALSE( + registry.GetExtensionById("blacklisted", ExtensionRegistry::DISABLED)); + EXPECT_FALSE( + registry.GetExtensionById("blacklisted", ExtensionRegistry::TERMINATED)); + EXPECT_TRUE( + registry.GetExtensionById("blacklisted", ExtensionRegistry::BLACKLISTED)); + + // Enabled can be found with multiple flags set. + EXPECT_TRUE(registry.GetExtensionById( + "enabled", ExtensionRegistry::ENABLED | ExtensionRegistry::TERMINATED)); + + // Enabled isn't found if the wrong flags are set. + EXPECT_FALSE(registry.GetExtensionById( + "enabled", ExtensionRegistry::DISABLED | ExtensionRegistry::BLACKLISTED)); +} + } // namespace } // namespace extensions |