From 21c6c43a17dff88f2ad0c9cc9a71a1bbf7fad2b6 Mon Sep 17 00:00:00 2001 From: "rockot@chromium.org" Date: Wed, 5 Mar 2014 18:47:31 +0000 Subject: Move ExtensionFunctionRegistry out of src/chrome This is mostly a mechanical change with the exception that ExtensionsBrowserClient has a new method for registering additional extensions functions with the registry. Chrome-dependent function registration has been moved to the ChromeExtensionsBrowserClient implementation. BUG=299187 TBR=zelidrag@chromium.org,finnur@chromium.org,nona@chromium.org,stevenjb@chromium.org,kalman@chromium.org,zork@chromium.org,dmazzoni@chromium.org for mechanical header path changes Review URL: https://codereview.chromium.org/185293017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255101 0039d316-1c4b-4281-b951-d872f2087c98 --- extensions/browser/extension_function_registry.h | 66 ++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 extensions/browser/extension_function_registry.h (limited to 'extensions/browser/extension_function_registry.h') diff --git a/extensions/browser/extension_function_registry.h b/extensions/browser/extension_function_registry.h new file mode 100644 index 0000000..211a899 --- /dev/null +++ b/extensions/browser/extension_function_registry.h @@ -0,0 +1,66 @@ +// Copyright 2014 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_FUNCTION_REGISTRY_H_ +#define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_ + +#include +#include +#include + +#include "extensions/browser/extension_function_histogram_value.h" + +class ExtensionFunction; + +// A factory function for creating new ExtensionFunction instances. +typedef ExtensionFunction* (*ExtensionFunctionFactory)(); + +// Template for defining ExtensionFunctionFactory. +template +ExtensionFunction* NewExtensionFunction() { + return new T(); +} + +// Contains a list of all known extension functions and allows clients to +// create instances of them. +class ExtensionFunctionRegistry { + public: + static ExtensionFunctionRegistry* GetInstance(); + explicit ExtensionFunctionRegistry(); + virtual ~ExtensionFunctionRegistry(); + + // Adds all function names to 'names'. + void GetAllNames(std::vector* names); + + // Allows overriding of specific functions (e.g. for testing). Functions + // must be previously registered. Returns true if successful. + bool OverrideFunction(const std::string& name, + ExtensionFunctionFactory factory); + + // Factory method for the ExtensionFunction registered as 'name'. + ExtensionFunction* NewFunction(const std::string& name); + + template + void RegisterFunction() { + ExtensionFunctionFactory factory = &NewExtensionFunction; + factories_[T::function_name()] = + FactoryEntry(factory, T::histogram_value()); + } + + struct FactoryEntry { + public: + explicit FactoryEntry(); + explicit FactoryEntry( + ExtensionFunctionFactory factory, + extensions::functions::HistogramValue histogram_value); + + ExtensionFunctionFactory factory_; + extensions::functions::HistogramValue histogram_value_; + }; + + typedef std::map FactoryMap; + FactoryMap factories_; +}; + +#endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_ -- cgit v1.1