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 #include "base/logging.h" | |
13 | |
12 class ExtensionFunction; | 14 class ExtensionFunction; |
13 | 15 |
14 // A factory function for creating new ExtensionFunction instances. | 16 // A factory function for creating new ExtensionFunction instances. |
15 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); | 17 typedef ExtensionFunction* (*ExtensionFunctionFactory)(); |
16 | 18 |
17 // Template for defining ExtensionFunctionFactory. | 19 // Template for defining ExtensionFunctionFactory. |
18 template<class T> | 20 template<class T> |
19 ExtensionFunction* NewExtensionFunction() { | 21 ExtensionFunction* NewExtensionFunction() { |
20 return new T(); | 22 return new T(); |
21 } | 23 } |
(...skipping 15 matching lines...) Expand all Loading... | |
37 // Allows overriding of specific functions (e.g. for testing). Functions | 39 // Allows overriding of specific functions (e.g. for testing). Functions |
38 // must be previously registered. Returns true if successful. | 40 // must be previously registered. Returns true if successful. |
39 bool OverrideFunction(const std::string& name, | 41 bool OverrideFunction(const std::string& name, |
40 ExtensionFunctionFactory factory); | 42 ExtensionFunctionFactory factory); |
41 | 43 |
42 // Factory method for the ExtensionFunction registered as 'name'. | 44 // Factory method for the ExtensionFunction registered as 'name'. |
43 ExtensionFunction* NewFunction(const std::string& name); | 45 ExtensionFunction* NewFunction(const std::string& name); |
44 | 46 |
45 template<class T> | 47 template<class T> |
46 void RegisterFunction() { | 48 void RegisterFunction() { |
49 if (factories_.find(T::function_name()) != factories_.end()) | |
50 NOTREACHED() << "Function already registered for " << T::function_name(); | |
Yoyo Zhou
2013/01/04 23:54:33
This isn't going to work in multiprofile.
We're m
not at google - send to devlin
2013/01/05 00:15:36
Done.
| |
47 factories_[T::function_name()] = &NewExtensionFunction<T>; | 51 factories_[T::function_name()] = &NewExtensionFunction<T>; |
48 } | 52 } |
49 | 53 |
50 private: | 54 private: |
51 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; | 55 typedef std::map<std::string, ExtensionFunctionFactory> FactoryMap; |
52 FactoryMap factories_; | 56 FactoryMap factories_; |
53 }; | 57 }; |
54 | 58 |
55 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_ | 59 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_FUNCTION_REGISTRY_H_ |
OLD | NEW |