Revert of Output closure-compiled JavaScript files (patchset #10 id:180001 of https...
[chromium-blink-merge.git] / extensions / browser / extension_function_registry.h
bloba0812737a916281193888d2a7f77c4cde62051a6
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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.
20 template <class T>
21 ExtensionFunction* NewExtensionFunction() {
22 return new T();
25 // Contains a list of all known extension functions and allows clients to
26 // create instances of them.
27 class ExtensionFunctionRegistry {
28 public:
29 static ExtensionFunctionRegistry* GetInstance();
30 explicit ExtensionFunctionRegistry();
31 virtual ~ExtensionFunctionRegistry();
33 // Adds all function names to 'names'.
34 void GetAllNames(std::vector<std::string>* names);
36 // Allows overriding of specific functions (e.g. for testing). Functions
37 // must be previously registered. Returns true if successful.
38 bool OverrideFunction(const std::string& name,
39 ExtensionFunctionFactory factory);
41 // Factory method for the ExtensionFunction registered as 'name'.
42 ExtensionFunction* NewFunction(const std::string& name);
44 template <class T>
45 void RegisterFunction() {
46 ExtensionFunctionFactory factory = &NewExtensionFunction<T>;
47 factories_[T::function_name()] =
48 FactoryEntry(factory, T::function_name(), T::histogram_value());
51 struct FactoryEntry {
52 public:
53 explicit FactoryEntry();
54 explicit FactoryEntry(
55 ExtensionFunctionFactory factory,
56 const char* function_name,
57 extensions::functions::HistogramValue histogram_value);
59 ExtensionFunctionFactory factory_;
60 const char* function_name_;
61 extensions::functions::HistogramValue histogram_value_;
64 typedef std::map<std::string, FactoryEntry> FactoryMap;
65 FactoryMap factories_;
68 #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_