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