diff options
author | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-21 03:09:14 +0000 |
---|---|---|
committer | jamescook@chromium.org <jamescook@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-21 03:09:14 +0000 |
commit | bb1bc9b30627f0f454c89ac91637c89a17352769 (patch) | |
tree | 2141a8a6ae3a14d89ff7a18001e0bac5c1140ae7 /extensions | |
parent | ddc78c802e48e2084ffccf537b7aca7c6f2c825c (diff) | |
download | chromium_src-bb1bc9b30627f0f454c89ac91637c89a17352769.zip chromium_src-bb1bc9b30627f0f454c89ac91637c89a17352769.tar.gz chromium_src-bb1bc9b30627f0f454c89ac91637c89a17352769.tar.bz2 |
Extract an ExtensionRegistry from ExtensionService
This new class holds the various sets of enabled/disabled/terminated/etc.
extensions. It lives in src/extensions/browser so it can be used by
app_shell.
This CL renames some members and methods to clarify which set of extensions is the enabled set and also renames some methods that are only used in test code.
BUG=none
TEST=unit_tests ExtensionRegistryTest and ExtensionServiceTest
Review URL: https://codereview.chromium.org/111873008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242248 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/browser/extension_registry.cc | 60 | ||||
-rw-r--r-- | extensions/browser/extension_registry.h | 94 | ||||
-rw-r--r-- | extensions/browser/extension_registry_unittest.cc | 102 | ||||
-rw-r--r-- | extensions/extensions.gyp | 2 |
4 files changed, 258 insertions, 0 deletions
diff --git a/extensions/browser/extension_registry.cc b/extensions/browser/extension_registry.cc new file mode 100644 index 0000000..9391b40 --- /dev/null +++ b/extensions/browser/extension_registry.cc @@ -0,0 +1,60 @@ +// Copyright 2013 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 "extensions/browser/extension_registry.h" + +namespace extensions { + +ExtensionRegistry::ExtensionRegistry() {} +ExtensionRegistry::~ExtensionRegistry() {} + +bool ExtensionRegistry::AddEnabled( + const scoped_refptr<const Extension>& extension) { + return enabled_extensions_.Insert(extension); +} + +bool ExtensionRegistry::RemoveEnabled(const std::string& id) { + return enabled_extensions_.Remove(id); +} + +bool ExtensionRegistry::AddDisabled( + const scoped_refptr<const Extension>& extension) { + return disabled_extensions_.Insert(extension); +} + +bool ExtensionRegistry::RemoveDisabled(const std::string& id) { + return disabled_extensions_.Remove(id); +} + +bool ExtensionRegistry::AddTerminated( + const scoped_refptr<const Extension>& extension) { + return terminated_extensions_.Insert(extension); +} + +bool ExtensionRegistry::RemoveTerminated(const std::string& id) { + return terminated_extensions_.Remove(id); +} + +bool ExtensionRegistry::AddBlacklisted( + const scoped_refptr<const Extension>& extension) { + return blacklisted_extensions_.Insert(extension); +} + +bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) { + return blacklisted_extensions_.Remove(id); +} + +void ExtensionRegistry::ClearAll() { + enabled_extensions_.Clear(); + disabled_extensions_.Clear(); + terminated_extensions_.Clear(); + blacklisted_extensions_.Clear(); +} + +void ExtensionRegistry::SetDisabledModificationCallback( + const ExtensionSet::ModificationCallback& callback) { + disabled_extensions_.set_modification_callback(callback); +} + +} // namespace extensions diff --git a/extensions/browser/extension_registry.h b/extensions/browser/extension_registry.h new file mode 100644 index 0000000..c051fbb --- /dev/null +++ b/extensions/browser/extension_registry.h @@ -0,0 +1,94 @@ +// Copyright 2013 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 EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_ +#define EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_ + +#include <string> + +#include "base/memory/ref_counted.h" +#include "extensions/common/extension_set.h" + +namespace extensions { +class Extension; + +// ExtensionRegistry holds sets of the installed extensions for a given +// BrowserContext. +// TODO(jamescook): Convert this to a BrowserContextKeyedService. +class ExtensionRegistry { + public: + ExtensionRegistry(); + ~ExtensionRegistry(); + + // NOTE: These sets are *eventually* mututally exclusive, but an extension can + // appear in two sets for short periods of time. + const ExtensionSet& enabled_extensions() const { + return enabled_extensions_; + } + const ExtensionSet& disabled_extensions() const { + return disabled_extensions_; + } + const ExtensionSet& terminated_extensions() const { + return terminated_extensions_; + } + const ExtensionSet& blacklisted_extensions() const { + return blacklisted_extensions_; + } + + // 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. + // NOTE: You probably want to use ExtensionService instead of calling this + // method directly. + bool AddEnabled(const scoped_refptr<const Extension>& extension); + + // Removes the specified extension from the enabled set. + // Returns true if the set contained the specified extension. + // NOTE: You probably want to use ExtensionService instead of calling this + // method directly. + bool RemoveEnabled(const std::string& id); + + // As above, but for the disabled set. + bool AddDisabled(const scoped_refptr<const Extension>& extension); + bool RemoveDisabled(const std::string& id); + + // As above, but for the terminated set. + bool AddTerminated(const scoped_refptr<const Extension>& extension); + bool RemoveTerminated(const std::string& id); + + // As above, but for the blacklisted set. + bool AddBlacklisted(const scoped_refptr<const Extension>& extension); + bool RemoveBlacklisted(const std::string& id); + + // Removes all extensions from all sets. + void ClearAll(); + + // Sets a callback to run when the disabled extension set is modified. + // TODO(jamescook): This is too specific for a generic registry; find some + // other way to do this. + void SetDisabledModificationCallback( + const ExtensionSet::ModificationCallback& callback); + + private: + // Extensions that are installed, enabled and not terminated. + ExtensionSet enabled_extensions_; + + // Extensions that are installed and disabled. + ExtensionSet disabled_extensions_; + + // Extensions that are installed and terminated. + ExtensionSet terminated_extensions_; + + // Extensions that are installed and blacklisted. Generally these shouldn't be + // considered as installed by the extension platform: we only keep them around + // so that if extensions are blacklisted by mistake they can easily be + // un-blacklisted. + ExtensionSet blacklisted_extensions_; + + DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry); +}; + +} // namespace extensions + +#endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_ diff --git a/extensions/browser/extension_registry_unittest.cc b/extensions/browser/extension_registry_unittest.cc new file mode 100644 index 0000000..4243dea --- /dev/null +++ b/extensions/browser/extension_registry_unittest.cc @@ -0,0 +1,102 @@ +// Copyright 2013 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 "extensions/browser/extension_registry.h" + +#include <string> + +#include "base/files/file_path.h" +#include "base/memory/ref_counted.h" +#include "base/values.h" +#include "extensions/common/extension.h" +#include "extensions/common/extension_builder.h" +#include "extensions/common/value_builder.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace extensions { +namespace { + +// Creates a very simple extension. +scoped_refptr<Extension> CreateExtensionWithID(const std::string& id) { + return ExtensionBuilder() + .SetManifest( + DictionaryBuilder().Set("name", "Echo").Set("version", "1.0")) + .SetID(id) + .Build(); +} + +typedef testing::Test ExtensionRegistryTest; + +TEST_F(ExtensionRegistryTest, FillAndClearRegistry) { + ExtensionRegistry registry; + scoped_refptr<Extension> extension1 = CreateExtensionWithID("id1"); + scoped_refptr<Extension> extension2 = CreateExtensionWithID("id2"); + scoped_refptr<Extension> extension3 = CreateExtensionWithID("id3"); + scoped_refptr<Extension> extension4 = CreateExtensionWithID("id4"); + + // All the sets start empty. + EXPECT_EQ(0u, registry.enabled_extensions().size()); + EXPECT_EQ(0u, registry.disabled_extensions().size()); + EXPECT_EQ(0u, registry.terminated_extensions().size()); + EXPECT_EQ(0u, registry.blacklisted_extensions().size()); + + // Extensions can be added to each set. + registry.AddEnabled(extension1); + registry.AddDisabled(extension2); + registry.AddTerminated(extension3); + registry.AddBlacklisted(extension4); + + EXPECT_EQ(1u, registry.enabled_extensions().size()); + EXPECT_EQ(1u, registry.disabled_extensions().size()); + EXPECT_EQ(1u, registry.terminated_extensions().size()); + EXPECT_EQ(1u, registry.blacklisted_extensions().size()); + + // Clearing the registry clears all sets. + registry.ClearAll(); + + EXPECT_EQ(0u, registry.enabled_extensions().size()); + EXPECT_EQ(0u, registry.disabled_extensions().size()); + EXPECT_EQ(0u, registry.terminated_extensions().size()); + EXPECT_EQ(0u, registry.blacklisted_extensions().size()); +} + +// A simple test of adding and removing things from sets. +TEST_F(ExtensionRegistryTest, AddAndRemoveExtensionFromRegistry) { + ExtensionRegistry registry; + + // Adding an extension works. + scoped_refptr<Extension> extension = CreateExtensionWithID("id"); + EXPECT_TRUE(registry.AddEnabled(extension)); + EXPECT_EQ(1u, registry.enabled_extensions().size()); + + // The extension was only added to one set. + EXPECT_EQ(0u, registry.disabled_extensions().size()); + EXPECT_EQ(0u, registry.terminated_extensions().size()); + EXPECT_EQ(0u, registry.blacklisted_extensions().size()); + + // Removing an extension works. + EXPECT_TRUE(registry.RemoveEnabled(extension->id())); + EXPECT_EQ(0u, registry.enabled_extensions().size()); + + // Trying to remove an extension that isn't in the set fails cleanly. + EXPECT_FALSE(registry.RemoveEnabled(extension->id())); +} + +TEST_F(ExtensionRegistryTest, AddExtensionToRegistryTwice) { + ExtensionRegistry registry; + scoped_refptr<Extension> extension = CreateExtensionWithID("id"); + + // An extension can exist in two sets at once. It would be nice to eliminate + // this functionality, but some users of ExtensionRegistry need it. + EXPECT_TRUE(registry.AddEnabled(extension)); + EXPECT_TRUE(registry.AddDisabled(extension)); + + EXPECT_EQ(1u, registry.enabled_extensions().size()); + EXPECT_EQ(1u, registry.disabled_extensions().size()); + EXPECT_EQ(0u, registry.terminated_extensions().size()); + EXPECT_EQ(0u, registry.blacklisted_extensions().size()); +} + +} // namespace +} // namespace extensions diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index 4609d46..fa07292 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -169,6 +169,8 @@ 'browser/extension_error.h', 'browser/extension_function.cc', 'browser/extension_function.h', + 'browser/extension_registry.cc', + 'browser/extension_registry.h', 'browser/extensions_browser_client.cc', 'browser/extensions_browser_client.h', 'browser/external_provider_interface.h', |