diff options
author | karen@chromium.org <karen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-02 17:49:45 +0000 |
---|---|---|
committer | karen@chromium.org <karen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-02 17:49:45 +0000 |
commit | 80d058a87da7cd845e2cb92b55df76bed6c91a5f (patch) | |
tree | e98d6af34f8ed169bf46b704fd3ffb1e4401cdc0 /extensions/common | |
parent | a95f54d2e66adba45c76c3700599ef751dbaf2d0 (diff) | |
download | chromium_src-80d058a87da7cd845e2cb92b55df76bed6c91a5f.zip chromium_src-80d058a87da7cd845e2cb92b55df76bed6c91a5f.tar.gz chromium_src-80d058a87da7cd845e2cb92b55df76bed6c91a5f.tar.bz2 |
This CL broke the official builders.
Revert 232499 "Move ManifestHandler to top-level extensions."
> Move ManifestHandler to top-level extensions.
>
> BUG=298586
> R=benwells@chromium.org
>
> Review URL: https://codereview.chromium.org/52593012
TBR=yoz@chromium.org
Review URL: https://codereview.chromium.org/55713004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232631 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/common')
-rw-r--r-- | extensions/common/manifest_handler.cc | 205 | ||||
-rw-r--r-- | extensions/common/manifest_handler.h | 138 | ||||
-rw-r--r-- | extensions/common/manifest_handler_unittest.cc | 281 |
3 files changed, 0 insertions, 624 deletions
diff --git a/extensions/common/manifest_handler.cc b/extensions/common/manifest_handler.cc deleted file mode 100644 index 43e6bd8..0000000 --- a/extensions/common/manifest_handler.cc +++ /dev/null @@ -1,205 +0,0 @@ -// Copyright (c) 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/common/manifest_handler.h" - -#include <map> - -#include "base/logging.h" -#include "base/stl_util.h" -#include "chrome/common/extensions/extension.h" - -namespace extensions { - -namespace { - -static base::LazyInstance<ManifestHandlerRegistry> g_registry = - LAZY_INSTANCE_INITIALIZER; -static ManifestHandlerRegistry* g_registry_override = NULL; - -ManifestHandlerRegistry* GetRegistry() { - if (!g_registry_override) - return g_registry.Pointer(); - return g_registry_override; -} - -} // namespace - -ManifestHandler::ManifestHandler() { -} - -ManifestHandler::~ManifestHandler() { -} - -bool ManifestHandler::Validate(const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings) const { - return true; -} - -bool ManifestHandler::AlwaysParseForType(Manifest::Type type) const { - return false; -} - -bool ManifestHandler::AlwaysValidateForType(Manifest::Type type) const { - return false; -} - -const std::vector<std::string> ManifestHandler::PrerequisiteKeys() const { - return std::vector<std::string>(); -} - -void ManifestHandler::Register() { - linked_ptr<ManifestHandler> this_linked(this); - const std::vector<std::string> keys = Keys(); - for (size_t i = 0; i < keys.size(); ++i) - GetRegistry()->RegisterManifestHandler(keys[i], this_linked); -} - -// static -void ManifestHandler::FinalizeRegistration() { - GetRegistry()->Finalize(); -} - -// static -bool ManifestHandler::IsRegistrationFinalized() { - return GetRegistry()->is_finalized_; -} - -// static -bool ManifestHandler::ParseExtension(Extension* extension, string16* error) { - return GetRegistry()->ParseExtension(extension, error); -} - -// static -bool ManifestHandler::ValidateExtension(const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings) { - return GetRegistry()->ValidateExtension(extension, error, warnings); -} - -// static -const std::vector<std::string> ManifestHandler::SingleKey( - const std::string& key) { - return std::vector<std::string>(1, key); -} - -ManifestHandlerRegistry::ManifestHandlerRegistry() : is_finalized_(false) { -} - -ManifestHandlerRegistry::~ManifestHandlerRegistry() { -} - -void ManifestHandlerRegistry::Finalize() { - CHECK(!is_finalized_); - SortManifestHandlers(); - is_finalized_ = true; -} - -void ManifestHandlerRegistry::RegisterManifestHandler( - const std::string& key, linked_ptr<ManifestHandler> handler) { - CHECK(!is_finalized_); - handlers_[key] = handler; -} - -bool ManifestHandlerRegistry::ParseExtension(Extension* extension, - string16* error) { - std::map<int, ManifestHandler*> handlers_by_priority; - for (ManifestHandlerMap::iterator iter = handlers_.begin(); - iter != handlers_.end(); ++iter) { - ManifestHandler* handler = iter->second.get(); - if (extension->manifest()->HasPath(iter->first) || - handler->AlwaysParseForType(extension->GetType())) { - handlers_by_priority[priority_map_[handler]] = handler; - } - } - for (std::map<int, ManifestHandler*>::iterator iter = - handlers_by_priority.begin(); - iter != handlers_by_priority.end(); ++iter) { - if (!(iter->second)->Parse(extension, error)) - return false; - } - return true; -} - -bool ManifestHandlerRegistry::ValidateExtension( - const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings) { - std::set<ManifestHandler*> handlers; - for (ManifestHandlerMap::iterator iter = handlers_.begin(); - iter != handlers_.end(); ++iter) { - ManifestHandler* handler = iter->second.get(); - if (extension->manifest()->HasPath(iter->first) || - handler->AlwaysValidateForType(extension->GetType())) { - handlers.insert(handler); - } - } - for (std::set<ManifestHandler*>::iterator iter = handlers.begin(); - iter != handlers.end(); ++iter) { - if (!(*iter)->Validate(extension, error, warnings)) - return false; - } - return true; -} - -// static -ManifestHandlerRegistry* ManifestHandlerRegistry::SetForTesting( - ManifestHandlerRegistry* new_registry) { - ManifestHandlerRegistry* old_registry = GetRegistry(); - if (new_registry != g_registry.Pointer()) - g_registry_override = new_registry; - else - g_registry_override = NULL; - return old_registry; -} - -void ManifestHandlerRegistry::SortManifestHandlers() { - std::set<ManifestHandler*> unsorted_handlers; - for (ManifestHandlerMap::const_iterator iter = handlers_.begin(); - iter != handlers_.end(); ++iter) { - unsorted_handlers.insert(iter->second.get()); - } - - int priority = 0; - while (true) { - std::set<ManifestHandler*> next_unsorted_handlers; - for (std::set<ManifestHandler*>::const_iterator iter = - unsorted_handlers.begin(); - iter != unsorted_handlers.end(); ++iter) { - ManifestHandler* handler = *iter; - const std::vector<std::string>& prerequisites = - handler->PrerequisiteKeys(); - int unsatisfied = prerequisites.size(); - for (size_t i = 0; i < prerequisites.size(); ++i) { - ManifestHandlerMap::const_iterator prereq_iter = - handlers_.find(prerequisites[i]); - // If the prerequisite does not exist, crash. - CHECK(prereq_iter != handlers_.end()) - << "Extension manifest handler depends on unrecognized key " - << prerequisites[i]; - // Prerequisite is in our map. - if (ContainsKey(priority_map_, prereq_iter->second.get())) - unsatisfied--; - } - if (unsatisfied == 0) { - priority_map_[handler] = priority; - priority++; - } else { - // Put in the list for next time. - next_unsorted_handlers.insert(handler); - } - } - if (next_unsorted_handlers.size() == unsorted_handlers.size()) - break; - unsorted_handlers.swap(next_unsorted_handlers); - } - - // If there are any leftover unsorted handlers, they must have had - // circular dependencies. - CHECK(unsorted_handlers.size() == 0) << "Extension manifest handlers have " - << "circular dependencies!"; -} - -} // namespace extensions diff --git a/extensions/common/manifest_handler.h b/extensions/common/manifest_handler.h deleted file mode 100644 index fd7b571..0000000 --- a/extensions/common/manifest_handler.h +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) 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_COMMON_MANIFEST_HANDLER_H_ -#define EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ - -#include <set> -#include <string> -#include <vector> - -#include "base/lazy_instance.h" -#include "base/memory/linked_ptr.h" -#include "base/strings/string16.h" -#include "extensions/common/manifest.h" - -namespace extensions { -class Extension; - -// An interface for clients that recognize and parse keys in extension -// manifests. -class ManifestHandler { - public: - ManifestHandler(); - virtual ~ManifestHandler(); - - // Attempts to parse the extension's manifest. - // Returns true on success or false on failure; if false, |error| will - // be set to a failure message. - virtual bool Parse(Extension* extension, string16* error) = 0; - - // Validate that files associated with this manifest key exist. - // Validation takes place after parsing. May also append a series of - // warning messages to |warnings|. - // - // Otherwise, returns false, and a description of the error is - // returned in |error|. - // TODO(yoz): Change error to string16. See crbug.com/71980. - virtual bool Validate(const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings) const; - - // If false (the default), only parse the manifest if a registered - // key is present in the manifest. If true, always attempt to parse - // the manifest for this extension type, even if no registered keys - // are present. This allows specifying a default parsed value for - // extensions that don't declare our key in the manifest. - // TODO(yoz): Use Feature availability instead. - virtual bool AlwaysParseForType(Manifest::Type type) const; - - // Same as AlwaysParseForType, but for Validate instead of Parse. - virtual bool AlwaysValidateForType(Manifest::Type type) const; - - // The list of keys that, if present, should be parsed before calling our - // Parse (typically, because our Parse needs to read those keys). - // Defaults to empty. - virtual const std::vector<std::string> PrerequisiteKeys() const; - - // Associate us with our keys() in the manifest. A handler can register - // for multiple keys. The global registry takes ownership of this; - // if it has an existing handler for |key|, it replaces it with this. - // Manifest handlers must be registered at process startup in - // chrome_manifest_handlers.cc: - // (new MyManifestHandler)->Register(); - void Register(); - - // Calling FinalizeRegistration indicates that there are no more - // manifest handlers to be registered. - static void FinalizeRegistration(); - - static bool IsRegistrationFinalized(); - - // Call Parse on all registered manifest handlers that should parse - // this extension. - static bool ParseExtension(Extension* extension, string16* error); - - // Call Validate on all registered manifest handlers for this extension. - static bool ValidateExtension(const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings); - - protected: - // A convenience method for handlers that only register for 1 key, - // so that they can define keys() { return SingleKey(kKey); } - static const std::vector<std::string> SingleKey(const std::string& key); - - private: - // The keys to register us for (in Register). - virtual const std::vector<std::string> Keys() const = 0; -}; - -// The global registry for manifest handlers. -class ManifestHandlerRegistry { - private: - friend class ManifestHandler; - friend class ScopedTestingManifestHandlerRegistry; - friend struct base::DefaultLazyInstanceTraits<ManifestHandlerRegistry>; - - ManifestHandlerRegistry(); - ~ManifestHandlerRegistry(); - - void Finalize(); - - void RegisterManifestHandler(const std::string& key, - linked_ptr<ManifestHandler> handler); - bool ParseExtension(Extension* extension, string16* error); - bool ValidateExtension(const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings); - - // Overrides the current global ManifestHandlerRegistry with - // |registry|, returning the current one. - static ManifestHandlerRegistry* SetForTesting( - ManifestHandlerRegistry* new_registry); - - typedef std::map<std::string, linked_ptr<ManifestHandler> > - ManifestHandlerMap; - typedef std::map<ManifestHandler*, int> ManifestHandlerPriorityMap; - - // Puts the manifest handlers in order such that each handler comes after - // any handlers for their PrerequisiteKeys. If there is no handler for - // a prerequisite key, that dependency is simply ignored. - // CHECKs that there are no manifest handlers with circular dependencies. - void SortManifestHandlers(); - - // All registered manifest handlers. - ManifestHandlerMap handlers_; - - // The priority for each manifest handler. Handlers with lower priority - // values are evaluated first. - ManifestHandlerPriorityMap priority_map_; - - bool is_finalized_; -}; - -} // namespace extensions - -#endif // EXTENSIONS_COMMON_MANIFEST_HANDLER_H_ diff --git a/extensions/common/manifest_handler_unittest.cc b/extensions/common/manifest_handler_unittest.cc deleted file mode 100644 index f5e012e..0000000 --- a/extensions/common/manifest_handler_unittest.cc +++ /dev/null @@ -1,281 +0,0 @@ -// Copyright (c) 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 <string> -#include <vector> - -#include "base/files/file_path.h" -#include "base/memory/scoped_ptr.h" -#include "base/stl_util.h" -#include "base/strings/utf_string_conversions.h" -#include "chrome/common/extensions/extension.h" -#include "chrome/common/extensions/extension_builder.h" -#include "chrome/common/extensions/value_builder.h" -#include "extensions/common/install_warning.h" -#include "extensions/common/manifest_handler.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace extensions { - -namespace { - -std::vector<std::string> SingleKey(const std::string& key) { - return std::vector<std::string>(1, key); -} - -} // namespace - -class ScopedTestingManifestHandlerRegistry { - public: - ScopedTestingManifestHandlerRegistry() { - old_registry_ = ManifestHandlerRegistry::SetForTesting(®istry_); - } - - ~ScopedTestingManifestHandlerRegistry() { - ManifestHandlerRegistry::SetForTesting(old_registry_); - } - - ManifestHandlerRegistry registry_; - ManifestHandlerRegistry* old_registry_; -}; - -class ManifestHandlerTest : public testing::Test { - public: - class ParsingWatcher { - public: - // Called when a manifest handler parses. - void Record(const std::string& name) { - parsed_names_.push_back(name); - } - - const std::vector<std::string>& parsed_names() { - return parsed_names_; - } - - // Returns true if |name_before| was parsed before |name_after|. - bool ParsedBefore(const std::string& name_before, - const std::string& name_after) { - size_t i_before = parsed_names_.size(); - size_t i_after = 0; - for (size_t i = 0; i < parsed_names_.size(); ++i) { - if (parsed_names_[i] == name_before) - i_before = i; - if (parsed_names_[i] == name_after) - i_after = i; - } - if (i_before < i_after) - return true; - return false; - } - - private: - // The order of manifest handlers that we watched parsing. - std::vector<std::string> parsed_names_; - }; - - class TestManifestHandler : public ManifestHandler { - public: - TestManifestHandler(const std::string& name, - const std::vector<std::string>& keys, - const std::vector<std::string>& prereqs, - ParsingWatcher* watcher) - : name_(name), keys_(keys), prereqs_(prereqs), watcher_(watcher) { - } - - virtual bool Parse(Extension* extension, string16* error) OVERRIDE { - watcher_->Record(name_); - return true; - } - - virtual const std::vector<std::string> PrerequisiteKeys() const OVERRIDE { - return prereqs_; - } - - protected: - std::string name_; - std::vector<std::string> keys_; - std::vector<std::string> prereqs_; - ParsingWatcher* watcher_; - - virtual const std::vector<std::string> Keys() const OVERRIDE { - return keys_; - } - }; - - class FailingTestManifestHandler : public TestManifestHandler { - public: - FailingTestManifestHandler(const std::string& name, - const std::vector<std::string>& keys, - const std::vector<std::string>& prereqs, - ParsingWatcher* watcher) - : TestManifestHandler(name, keys, prereqs, watcher) { - } - virtual bool Parse(Extension* extension, string16* error) OVERRIDE { - *error = ASCIIToUTF16(name_); - return false; - } - }; - - class AlwaysParseTestManifestHandler : public TestManifestHandler { - public: - AlwaysParseTestManifestHandler(const std::string& name, - const std::vector<std::string>& keys, - const std::vector<std::string>& prereqs, - ParsingWatcher* watcher) - : TestManifestHandler(name, keys, prereqs, watcher) { - } - - virtual bool AlwaysParseForType(Manifest::Type type) const OVERRIDE { - return true; - } - }; - - class TestManifestValidator : public ManifestHandler { - public: - TestManifestValidator(bool return_value, - bool always_validate, - std::vector<std::string> keys) - : return_value_(return_value), - always_validate_(always_validate), - keys_(keys) { - } - - virtual bool Parse(Extension* extension, string16* error) OVERRIDE { - return true; - } - - virtual bool Validate( - const Extension* extension, - std::string* error, - std::vector<InstallWarning>* warnings) const OVERRIDE { - return return_value_; - } - - virtual bool AlwaysValidateForType(Manifest::Type type) const OVERRIDE { - return always_validate_; - } - - private: - virtual const std::vector<std::string> Keys() const OVERRIDE { - return keys_; - } - - protected: - bool return_value_; - bool always_validate_; - std::vector<std::string> keys_; - }; -}; - -TEST_F(ManifestHandlerTest, DependentHandlers) { - ScopedTestingManifestHandlerRegistry registry; - ParsingWatcher watcher; - std::vector<std::string> prereqs; - (new TestManifestHandler("A", SingleKey("a"), prereqs, &watcher))->Register(); - (new TestManifestHandler("B", SingleKey("b"), prereqs, &watcher))->Register(); - (new TestManifestHandler("J", SingleKey("j"), prereqs, &watcher))->Register(); - (new AlwaysParseTestManifestHandler("K", SingleKey("k"), prereqs, &watcher))-> - Register(); - prereqs.push_back("c.d"); - std::vector<std::string> keys; - keys.push_back("c.e"); - keys.push_back("c.z"); - (new TestManifestHandler("C.EZ", keys, prereqs, &watcher))->Register(); - prereqs.clear(); - prereqs.push_back("b"); - prereqs.push_back("k"); - (new TestManifestHandler("C.D", SingleKey("c.d"), prereqs, &watcher))-> - Register(); - ManifestHandler::FinalizeRegistration(); - - scoped_refptr<Extension> extension = ExtensionBuilder() - .SetManifest(DictionaryBuilder() - .Set("name", "no name") - .Set("version", "0") - .Set("manifest_version", 2) - .Set("a", 1) - .Set("b", 2) - .Set("c", DictionaryBuilder() - .Set("d", 3) - .Set("e", 4) - .Set("f", 5)) - .Set("g", 6)) - .Build(); - - // A, B, C.EZ, C.D, K - EXPECT_EQ(5u, watcher.parsed_names().size()); - EXPECT_TRUE(watcher.ParsedBefore("B", "C.D")); - EXPECT_TRUE(watcher.ParsedBefore("K", "C.D")); - EXPECT_TRUE(watcher.ParsedBefore("C.D", "C.EZ")); -} - -TEST_F(ManifestHandlerTest, FailingHandlers) { - ScopedTestingManifestHandlerRegistry registry; - // Can't use ExtensionBuilder, because this extension will fail to - // be parsed. - scoped_ptr<base::DictionaryValue> manifest_a( - DictionaryBuilder() - .Set("name", "no name") - .Set("version", "0") - .Set("manifest_version", 2) - .Set("a", 1) - .Build()); - - // Succeeds when "a" is not recognized. - std::string error; - scoped_refptr<Extension> extension = Extension::Create( - base::FilePath(), - Manifest::INVALID_LOCATION, - *manifest_a, - Extension::NO_FLAGS, - &error); - EXPECT_TRUE(extension.get()); - - // Register a handler for "a" that fails. - ParsingWatcher watcher; - (new FailingTestManifestHandler( - "A", SingleKey("a"), std::vector<std::string>(), &watcher))->Register(); - ManifestHandler::FinalizeRegistration(); - - extension = Extension::Create( - base::FilePath(), - Manifest::INVALID_LOCATION, - *manifest_a, - Extension::NO_FLAGS, - &error); - EXPECT_FALSE(extension.get()); - EXPECT_EQ("A", error); -} - -TEST_F(ManifestHandlerTest, Validate) { - ScopedTestingManifestHandlerRegistry registry; - scoped_refptr<Extension> extension = ExtensionBuilder() - .SetManifest(DictionaryBuilder() - .Set("name", "no name") - .Set("version", "0") - .Set("manifest_version", 2) - .Set("a", 1) - .Set("b", 2)) - .Build(); - EXPECT_TRUE(extension.get()); - - std::string error; - std::vector<InstallWarning> warnings; - // Always validates and fails. - (new TestManifestValidator(false, true, SingleKey("c")))->Register(); - EXPECT_FALSE( - ManifestHandler::ValidateExtension(extension.get(), &error, &warnings)); - - // This overrides the registered handler for "c". - (new TestManifestValidator(false, false, SingleKey("c")))->Register(); - EXPECT_TRUE( - ManifestHandler::ValidateExtension(extension.get(), &error, &warnings)); - - // Validates "a" and fails. - (new TestManifestValidator(false, true, SingleKey("a")))->Register(); - EXPECT_FALSE( - ManifestHandler::ValidateExtension(extension.get(), &error, &warnings)); -} - -} // namespace extensions |