• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "chrome/browser/extensions/extension_function_histogram_value.h"
13 
14 class ExtensionFunction;
15 
16 // A factory function for creating new ExtensionFunction instances.
17 typedef ExtensionFunction* (*ExtensionFunctionFactory)();
18 
19 // Template for defining ExtensionFunctionFactory.
20 template<class T>
NewExtensionFunction()21 ExtensionFunction* NewExtensionFunction() {
22   return new T();
23 }
24 
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();
32 
33   // Resets all functions to their default values.
34   void ResetFunctions();
35 
36   // Adds all function names to 'names'.
37   void GetAllNames(std::vector<std::string>* names);
38 
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);
43 
44   // Factory method for the ExtensionFunction registered as 'name'.
45   ExtensionFunction* NewFunction(const std::string& name);
46 
47   template<class T>
RegisterFunction()48   void RegisterFunction() {
49     ExtensionFunctionFactory factory = &NewExtensionFunction<T>;
50     factories_[T::function_name()] =
51         FactoryEntry(factory, T::histogram_value());
52   }
53 
54   struct FactoryEntry {
55    public:
56     explicit FactoryEntry();
57     explicit FactoryEntry(ExtensionFunctionFactory factory,
58       extensions::functions::HistogramValue histogram_value);
59 
60     ExtensionFunctionFactory factory_;
61     extensions::functions::HistogramValue histogram_value_;
62   };
63 
64   typedef std::map<std::string, FactoryEntry> FactoryMap;
65   FactoryMap factories_;
66 };
67 
68 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_
69