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_FACTORY_REGISTRY_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_FACTORY_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 FactoryRegistry { |
| 27 public: |
| 28 static FactoryRegistry* GetInstance(); |
| 29 FactoryRegistry() { ResetFunctions(); } |
| 30 |
| 31 // Resets all functions to their default values. |
| 32 void ResetFunctions(); |
| 33 |
| 34 // Adds all function names to 'names'. |
| 35 void GetAllNames(std::vector<std::string>* names); |
| 36 |
| 37 // Allows overriding of specific functions (e.g. for testing). Functions |
| 38 // must be previously registered. Returns true if successful. |
| 39 bool OverrideFunction(const std::string& name, |
| 40 ExtensionFunctionFactory factory); |
| 41 |
| 42 // Factory method for the ExtensionFunction registered as 'name'. |
| 43 ExtensionFunction* NewFunction(const std::string& name); |
| 44 |
| 45 template<class T> |
| 46 void RegisterFunction() { |
| 47 factories_[T::function_name()] = &NewExtensionFunction<T>; |
| 48 } |
| 49 |
| 50 private: |
| 51 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
| 52 FactoryMap factories_; |
| 53 }; |
| 54 |
| 55 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FACTORY_REGISTRY_H_ |
OLD | NEW |