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 GetInstance()12ExtensionFunctionRegistry* ExtensionFunctionRegistry::GetInstance() { 13 return Singleton<ExtensionFunctionRegistry>::get(); 14 } 15 ExtensionFunctionRegistry()16ExtensionFunctionRegistry::ExtensionFunctionRegistry() { 17 extensions::ExtensionsBrowserClient* client = 18 extensions::ExtensionsBrowserClient::Get(); 19 if (client) { 20 client->RegisterExtensionFunctions(this); 21 } 22 } 23 ~ExtensionFunctionRegistry()24ExtensionFunctionRegistry::~ExtensionFunctionRegistry() {} 25 GetAllNames(std::vector<std::string> * names)26void ExtensionFunctionRegistry::GetAllNames(std::vector<std::string>* names) { 27 for (FactoryMap::iterator iter = factories_.begin(); iter != factories_.end(); 28 ++iter) { 29 names->push_back(iter->first); 30 } 31 } 32 OverrideFunction(const std::string & name,ExtensionFunctionFactory factory)33bool ExtensionFunctionRegistry::OverrideFunction( 34 const std::string& name, 35 ExtensionFunctionFactory factory) { 36 FactoryMap::iterator iter = factories_.find(name); 37 if (iter == factories_.end()) { 38 return false; 39 } else { 40 iter->second.factory_ = factory; 41 return true; 42 } 43 } 44 NewFunction(const std::string & name)45ExtensionFunction* ExtensionFunctionRegistry::NewFunction( 46 const std::string& name) { 47 FactoryMap::iterator iter = factories_.find(name); 48 if (iter == factories_.end()) { 49 return NULL; 50 } 51 ExtensionFunction* function = iter->second.factory_(); 52 function->set_name(name); 53 function->set_histogram_value(iter->second.histogram_value_); 54 return function; 55 } 56 FactoryEntry()57ExtensionFunctionRegistry::FactoryEntry::FactoryEntry() 58 : factory_(0), histogram_value_(extensions::functions::UNKNOWN) {} 59 FactoryEntry(ExtensionFunctionFactory factory,extensions::functions::HistogramValue histogram_value)60ExtensionFunctionRegistry::FactoryEntry::FactoryEntry( 61 ExtensionFunctionFactory factory, 62 extensions::functions::HistogramValue histogram_value) 63 : factory_(factory), histogram_value_(histogram_value) {} 64