| 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_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 class ExtensionFunction; | 12 class ExtensionFunction; |
| 13 | 13 |
| 14 // A factory function for creating new ExtensionFunction instances. | 14 // A factory function for creating new ExtensionFunction instances. |
| 15 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); | 15 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); |
| 16 | 16 |
| 17 // Template for defining ExtensionFunctionFactory. | 17 // Template for defining ExtensionFunctionFactory. |
| 18 template<class T> | 18 template<class T> |
| 19 ExtensionFunction* NewExtensionFunction() { | 19 ExtensionFunction* NewExtensionFunction() { |
| 20 return new T(); | 20 return new T(); |
| 21 } | 21 } |
| 22 | 22 |
| 23 // Contains a list of all known extension functions and allows clients to | 23 // Contains a list of all known extension functions and allows clients to |
| 24 // create instances of them. | 24 // create instances of them. |
| 25 class ExtensionFunctionRegistry { | 25 class ExtensionFunctionRegistry { |
| 26 public: | 26 public: |
| 27 static ExtensionFunctionRegistry* GetInstance(); | 27 static ExtensionFunctionRegistry* GetInstance(); |
| 28 explicit ExtensionFunctionRegistry(); | 28 ExtensionFunctionRegistry(); |
| 29 virtual ~ExtensionFunctionRegistry(); | 29 virtual ~ExtensionFunctionRegistry(); |
| 30 | 30 |
| 31 // Resets all functions to their default values. | 31 // Resets all functions to their default values. |
| 32 void ResetFunctions(); | 32 void ResetFunctions(); |
| 33 | 33 |
| 34 // Adds all function names to 'names'. | 34 // Adds all function names to 'names'. |
| 35 void GetAllNames(std::vector<std::string>* names); | 35 void GetAllNames(std::vector<std::string>* names); |
| 36 | 36 |
| 37 // Allows overriding of specific functions (e.g. for testing). Functions | 37 // Allows overriding of specific functions (e.g. for testing). Functions |
| 38 // must be previously registered. Returns true if successful. | 38 // must be previously registered. Returns true if successful. |
| 39 bool OverrideFunction(const std::string& name, | 39 bool OverrideFunction(const std::string& name, |
| 40 ExtensionFunctionFactory factory); | 40 ExtensionFunctionFactory factory); |
| 41 | 41 |
| 42 // Factory method for the ExtensionFunction registered as 'name'. | 42 // Factory method for the ExtensionFunction registered as 'name'. |
| 43 ExtensionFunction* NewFunction(const std::string& name); | 43 ExtensionFunction* NewFunction(const std::string& name); |
| 44 | 44 |
| 45 template<class T> | 45 template<class T> |
| 46 void RegisterFunction() { | 46 void RegisterFunction() { |
| 47 factories_[T::function_name()] = &NewExtensionFunction<T>; | 47 factories_[T::function_name()] = &NewExtensionFunction<T>; |
| 48 } | 48 } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; | 51 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 52 FactoryMap factories_; | 52 FactoryMap factories_; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_ | 55 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_ |
| OLD | NEW |