[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 1 | // Copyright 2014 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 | #include "extensions/browser/extension_function_registry.h" |
| 6 | |
| 7 | #include "base/memory/singleton.h" |
| 8 | #include "extensions/browser/extension_function.h" |
| 9 | #include "extensions/browser/extensions_browser_client.h" |
| 10 | |
| 11 | // static |
| 12 | ExtensionFunctionRegistry* ExtensionFunctionRegistry::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 13 | return base::Singleton<ExtensionFunctionRegistry>::get(); |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | ExtensionFunctionRegistry::ExtensionFunctionRegistry() { |
| 17 | extensions::ExtensionsBrowserClient* client = |
| 18 | extensions::ExtensionsBrowserClient::Get(); |
| 19 | if (client) { |
| 20 | client->RegisterExtensionFunctions(this); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | ExtensionFunctionRegistry::~ExtensionFunctionRegistry() {} |
| 25 | |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 26 | bool ExtensionFunctionRegistry::OverrideFunction( |
| 27 | const std::string& name, |
| 28 | ExtensionFunctionFactory factory) { |
| 29 | FactoryMap::iterator iter = factories_.find(name); |
| 30 | if (iter == factories_.end()) { |
| 31 | return false; |
| 32 | } else { |
| 33 | iter->second.factory_ = factory; |
| 34 | return true; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | ExtensionFunction* ExtensionFunctionRegistry::NewFunction( |
| 39 | const std::string& name) { |
| 40 | FactoryMap::iterator iter = factories_.find(name); |
| 41 | if (iter == factories_.end()) { |
| 42 | return NULL; |
| 43 | } |
| 44 | ExtensionFunction* function = iter->second.factory_(); |
reillyg | 9c2528c | 2015-02-11 00:13:11 | [diff] [blame] | 45 | function->set_name(iter->second.function_name_); |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 46 | function->set_histogram_value(iter->second.histogram_value_); |
| 47 | return function; |
| 48 | } |
| 49 | |
| 50 | ExtensionFunctionRegistry::FactoryEntry::FactoryEntry() |
reillyg | 9c2528c | 2015-02-11 00:13:11 | [diff] [blame] | 51 | : factory_(0), |
| 52 | function_name_(nullptr), |
| 53 | histogram_value_(extensions::functions::UNKNOWN) { |
| 54 | } |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 55 | |
| 56 | ExtensionFunctionRegistry::FactoryEntry::FactoryEntry( |
| 57 | ExtensionFunctionFactory factory, |
reillyg | 9c2528c | 2015-02-11 00:13:11 | [diff] [blame] | 58 | const char* function_name, |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 59 | extensions::functions::HistogramValue histogram_value) |
reillyg | 9c2528c | 2015-02-11 00:13:11 | [diff] [blame] | 60 | : factory_(factory), |
| 61 | function_name_(function_name), |
| 62 | histogram_value_(histogram_value) { |
| 63 | } |