blob: d4681d720b0bcefd4d782fc98ca3a60884e1bdb6 [file] [log] [blame]
[email protected]21c6c432014-03-05 18:47:311// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]ae33d322012-03-19 22:24:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]21c6c432014-03-05 18:47:315#ifndef EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_
6#define EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_
[email protected]ae33d322012-03-19 22:24:357
8#include <map>
9#include <string>
[email protected]ae33d322012-03-19 22:24:3510
Matthew Dentonef83a622019-08-30 02:07:0011#include "base/memory/scoped_refptr.h"
[email protected]1ee7f892014-03-04 06:04:2512#include "extensions/browser/extension_function_histogram_value.h"
[email protected]07ad9622013-01-18 23:00:3313
[email protected]ae33d322012-03-19 22:24:3514class ExtensionFunction;
15
16// A factory function for creating new ExtensionFunction instances.
Matthew Dentonef83a622019-08-30 02:07:0017using ExtensionFunctionFactory = scoped_refptr<ExtensionFunction> (*)();
[email protected]ae33d322012-03-19 22:24:3518
19// Template for defining ExtensionFunctionFactory.
[email protected]21c6c432014-03-05 18:47:3120template <class T>
Matthew Dentonef83a622019-08-30 02:07:0021scoped_refptr<ExtensionFunction> NewExtensionFunction() {
22 return base::MakeRefCounted<T>();
[email protected]ae33d322012-03-19 22:24:3523}
24
25// Contains a list of all known extension functions and allows clients to
26// create instances of them.
27class ExtensionFunctionRegistry {
28 public:
dchenge8d21442017-03-01 22:29:3929 struct FactoryEntry {
30 public:
Devlin Cronin97a09b52018-02-07 17:49:5431 FactoryEntry();
dchenge8d21442017-03-01 22:29:3932 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 Cronin97a09b52018-02-07 17:49:5440 ExtensionFunctionFactory factory_ = nullptr;
41 const char* function_name_ = nullptr;
42 extensions::functions::HistogramValue histogram_value_ =
43 extensions::functions::UNKNOWN;
dchenge8d21442017-03-01 22:29:3944 };
Devlin Croninfad03c42018-04-06 03:21:5545 using FactoryMap = std::map<std::string, FactoryEntry>;
dchenge8d21442017-03-01 22:29:3946
Devlin Cronin21a2c0b2018-02-08 02:49:0347 static ExtensionFunctionRegistry& GetInstance();
Devlin Cronin97a09b52018-02-07 17:49:5448 ExtensionFunctionRegistry();
Peter Boström951cf77e2021-09-22 00:02:5949
50 ExtensionFunctionRegistry(const ExtensionFunctionRegistry&) = delete;
51 ExtensionFunctionRegistry& operator=(const ExtensionFunctionRegistry&) =
52 delete;
53
Devlin Cronin97a09b52018-02-07 17:49:5454 ~ExtensionFunctionRegistry();
[email protected]ae33d322012-03-19 22:24:3555
lazyboy9ec28cc12017-04-20 22:09:4656 // 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]ae33d322012-03-19 22:24:3560
61 // Factory method for the ExtensionFunction registered as 'name'.
Matthew Dentonef83a622019-08-30 02:07:0062 scoped_refptr<ExtensionFunction> NewFunction(const std::string& name);
[email protected]ae33d322012-03-19 22:24:3563
Devlin Cronin97a09b52018-02-07 17:49:5464 // Registers a new extension function. This will override any existing entry.
dchenge8d21442017-03-01 22:29:3965 void Register(const FactoryEntry& entry);
[email protected]21c6c432014-03-05 18:47:3166 template <class T>
[email protected]ae33d322012-03-19 22:24:3567 void RegisterFunction() {
Devlin Cronina2fde5e2020-10-16 06:47:5268 Register(FactoryEntry(&NewExtensionFunction<T>, T::static_function_name(),
69 T::static_histogram_value()));
[email protected]ae33d322012-03-19 22:24:3570 }
71
Devlin Croninfad03c42018-04-06 03:21:5572 const FactoryMap& GetFactoriesForTesting() const { return factories_; }
73
Devlin Cronin97a09b52018-02-07 17:49:5474 private:
[email protected]ae33d322012-03-19 22:24:3575 FactoryMap factories_;
76};
77
[email protected]21c6c432014-03-05 18:47:3178#endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_