blob: 211a899cb94d3c4d30402c765dde2efad9ec30a5 [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>
10#include <vector>
11
[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.
17typedef ExtensionFunction* (*ExtensionFunctionFactory)();
18
19// Template for defining ExtensionFunctionFactory.
[email protected]21c6c432014-03-05 18:47:3120template <class T>
[email protected]ae33d322012-03-19 22:24:3521ExtensionFunction* NewExtensionFunction() {
22 return new T();
23}
24
25// Contains a list of all known extension functions and allows clients to
26// create instances of them.
27class ExtensionFunctionRegistry {
28 public:
29 static ExtensionFunctionRegistry* GetInstance();
30 explicit ExtensionFunctionRegistry();
31 virtual ~ExtensionFunctionRegistry();
32
[email protected]ae33d322012-03-19 22:24:3533 // Adds all function names to 'names'.
34 void GetAllNames(std::vector<std::string>* names);
35
36 // Allows overriding of specific functions (e.g. for testing). Functions
37 // must be previously registered. Returns true if successful.
38 bool OverrideFunction(const std::string& name,
39 ExtensionFunctionFactory factory);
40
41 // Factory method for the ExtensionFunction registered as 'name'.
42 ExtensionFunction* NewFunction(const std::string& name);
43
[email protected]21c6c432014-03-05 18:47:3144 template <class T>
[email protected]ae33d322012-03-19 22:24:3545 void RegisterFunction() {
[email protected]07ad9622013-01-18 23:00:3346 ExtensionFunctionFactory factory = &NewExtensionFunction<T>;
47 factories_[T::function_name()] =
48 FactoryEntry(factory, T::histogram_value());
[email protected]ae33d322012-03-19 22:24:3549 }
50
[email protected]07ad9622013-01-18 23:00:3351 struct FactoryEntry {
52 public:
53 explicit FactoryEntry();
[email protected]21c6c432014-03-05 18:47:3154 explicit FactoryEntry(
55 ExtensionFunctionFactory factory,
56 extensions::functions::HistogramValue histogram_value);
[email protected]07ad9622013-01-18 23:00:3357
58 ExtensionFunctionFactory factory_;
59 extensions::functions::HistogramValue histogram_value_;
60 };
61
62 typedef std::map<std::string, FactoryEntry> FactoryMap;
[email protected]ae33d322012-03-19 22:24:3563 FactoryMap factories_;
64};
65
[email protected]21c6c432014-03-05 18:47:3166#endif // EXTENSIONS_BROWSER_EXTENSION_FUNCTION_REGISTRY_H_