[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_ |
6 | #define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_ | ||||
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 7 | |
8 | #include <map> | ||||
9 | #include <string> | ||||
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 10 | |
Matthew Denton | ef83a62 | 2019-08-30 02:07:00 | [diff] [blame] | 11 | #include "base/memory/scoped_refptr.h" |
[email protected] | 1ee7f89 | 2014-03-04 06:04:25 | [diff] [blame] | 12 | #include "extensions/browser/extension_function_histogram_value.h" |
[email protected] | 07ad962 | 2013-01-18 23:00:33 | [diff] [blame] | 13 | |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 14 | class ExtensionFunction; |
15 | |||||
16 | // A factory function for creating new ExtensionFunction instances. | ||||
Matthew Denton | ef83a62 | 2019-08-30 02:07:00 | [diff] [blame] | 17 | using ExtensionFunctionFactory = scoped_refptr<ExtensionFunction> (*)(); |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 18 | |
19 | // Template for defining ExtensionFunctionFactory. | ||||
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 20 | template <class T> |
Matthew Denton | ef83a62 | 2019-08-30 02:07:00 | [diff] [blame] | 21 | scoped_refptr<ExtensionFunction> NewExtensionFunction() { |
22 | return base::MakeRefCounted<T>(); | ||||
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 23 | } |
24 | |||||
25 | // Contains a list of all known extension functions and allows clients to | ||||
26 | // create instances of them. | ||||
27 | class ExtensionFunctionRegistry { | ||||
28 | public: | ||||
dcheng | e8d2144 | 2017-03-01 22:29:39 | [diff] [blame] | 29 | struct FactoryEntry { |
30 | public: | ||||
Devlin Cronin | 97a09b5 | 2018-02-07 17:49:54 | [diff] [blame] | 31 | FactoryEntry(); |
dcheng | e8d2144 | 2017-03-01 22:29:39 | [diff] [blame] | 32 | constexpr FactoryEntry( |
33 | ExtensionFunctionFactory factory, | ||||
34 | const char* function_name, | ||||
35 | extensions::functions::HistogramValue histogram_value) | ||||
36 | : factory_(factory), | ||||
37 | function_name_(function_name), | ||||
38 | histogram_value_(histogram_value) {} | ||||
39 | |||||
Devlin Cronin | 97a09b5 | 2018-02-07 17:49:54 | [diff] [blame] | 40 | ExtensionFunctionFactory factory_ = nullptr; |
41 | const char* function_name_ = nullptr; | ||||
42 | extensions::functions::HistogramValue histogram_value_ = | ||||
43 | extensions::functions::UNKNOWN; | ||||
dcheng | e8d2144 | 2017-03-01 22:29:39 | [diff] [blame] | 44 | }; |
Devlin Cronin | fad03c4 | 2018-04-06 03:21:55 | [diff] [blame] | 45 | using FactoryMap = std::map<std::string, FactoryEntry>; |
dcheng | e8d2144 | 2017-03-01 22:29:39 | [diff] [blame] | 46 | |
Devlin Cronin | 21a2c0b | 2018-02-08 02:49:03 | [diff] [blame] | 47 | static ExtensionFunctionRegistry& GetInstance(); |
Devlin Cronin | 97a09b5 | 2018-02-07 17:49:54 | [diff] [blame] | 48 | ExtensionFunctionRegistry(); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 49 | |
50 | ExtensionFunctionRegistry(const ExtensionFunctionRegistry&) = delete; | ||||
51 | ExtensionFunctionRegistry& operator=(const ExtensionFunctionRegistry&) = | ||||
52 | delete; | ||||
53 | |||||
Devlin Cronin | 97a09b5 | 2018-02-07 17:49:54 | [diff] [blame] | 54 | ~ExtensionFunctionRegistry(); |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 55 | |
lazyboy | 9ec28cc1 | 2017-04-20 22:09:46 | [diff] [blame] | 56 | // Allows overriding of specific functions for testing. Functions must be |
57 | // previously registered. Returns true if successful. | ||||
58 | bool OverrideFunctionForTesting(const std::string& name, | ||||
59 | ExtensionFunctionFactory factory); | ||||
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 60 | |
61 | // Factory method for the ExtensionFunction registered as 'name'. | ||||
Matthew Denton | ef83a62 | 2019-08-30 02:07:00 | [diff] [blame] | 62 | scoped_refptr<ExtensionFunction> NewFunction(const std::string& name); |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 63 | |
Devlin Cronin | 97a09b5 | 2018-02-07 17:49:54 | [diff] [blame] | 64 | // Registers a new extension function. This will override any existing entry. |
dcheng | e8d2144 | 2017-03-01 22:29:39 | [diff] [blame] | 65 | void Register(const FactoryEntry& entry); |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 66 | template <class T> |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 67 | void RegisterFunction() { |
Devlin Cronin | a2fde5e | 2020-10-16 06:47:52 | [diff] [blame] | 68 | Register(FactoryEntry(&NewExtensionFunction<T>, T::static_function_name(), |
69 | T::static_histogram_value())); | ||||
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 70 | } |
71 | |||||
Devlin Cronin | fad03c4 | 2018-04-06 03:21:55 | [diff] [blame] | 72 | const FactoryMap& GetFactoriesForTesting() const { return factories_; } |
73 | |||||
Devlin Cronin | 97a09b5 | 2018-02-07 17:49:54 | [diff] [blame] | 74 | private: |
[email protected] | ae33d32 | 2012-03-19 22:24:35 | [diff] [blame] | 75 | FactoryMap factories_; |
76 | }; | ||||
77 | |||||
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 78 | #endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_ |