1 // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_
12 #include "extensions/browser/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 // Allows overriding of specific functions (e.g. for testing). Functions
34 // must be previously registered. Returns true if successful.
35 bool OverrideFunction(const std::string
& name
,
36 ExtensionFunctionFactory factory
);
38 // Factory method for the ExtensionFunction registered as 'name'.
39 ExtensionFunction
* NewFunction(const std::string
& name
);
42 void RegisterFunction() {
43 ExtensionFunctionFactory factory
= &NewExtensionFunction
<T
>;
44 factories_
[T::function_name()] =
45 FactoryEntry(factory
, T::function_name(), T::histogram_value());
50 explicit FactoryEntry();
51 explicit FactoryEntry(
52 ExtensionFunctionFactory factory
,
53 const char* function_name
,
54 extensions::functions::HistogramValue histogram_value
);
56 ExtensionFunctionFactory factory_
;
57 const char* function_name_
;
58 extensions::functions::HistogramValue histogram_value_
;
61 typedef std::map
<std::string
, FactoryEntry
> FactoryMap
;
62 FactoryMap factories_
;
65 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_