1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_
12 #include "chrome/browser/extensions/extension_function_histogram_value.h"
14 class ExtensionFunction
;
16 // A factory function for creating new ExtensionFunction instances.
17 typedef ExtensionFunction
* (*ExtensionFunctionFactory
)();
19 // Template for defining ExtensionFunctionFactory.
21 ExtensionFunction
* NewExtensionFunction() {
25 // Contains a list of all known extension functions and allows clients to
26 // create instances of them.
27 class ExtensionFunctionRegistry
{
29 static ExtensionFunctionRegistry
* GetInstance();
30 explicit ExtensionFunctionRegistry();
31 virtual ~ExtensionFunctionRegistry();
33 // Resets all functions to their default values.
34 void ResetFunctions();
36 // Adds all function names to 'names'.
37 void GetAllNames(std::vector
<std::string
>* names
);
39 // Allows overriding of specific functions (e.g. for testing). Functions
40 // must be previously registered. Returns true if successful.
41 bool OverrideFunction(const std::string
& name
,
42 ExtensionFunctionFactory factory
);
44 // Factory method for the ExtensionFunction registered as 'name'.
45 ExtensionFunction
* NewFunction(const std::string
& name
);
48 void RegisterFunction() {
49 ExtensionFunctionFactory factory
= &NewExtensionFunction
<T
>;
50 factories_
[T::function_name()] =
51 FactoryEntry(factory
, T::histogram_value());
56 explicit FactoryEntry();
57 explicit FactoryEntry(ExtensionFunctionFactory factory
,
58 extensions::functions::HistogramValue histogram_value
);
60 ExtensionFunctionFactory factory_
;
61 extensions::functions::HistogramValue histogram_value_
;
64 typedef std::map
<std::string
, FactoryEntry
> FactoryMap
;
65 FactoryMap factories_
;
68 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_