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