diff options
Diffstat (limited to 'webkit/plugins/ppapi')
-rw-r--r-- | webkit/plugins/ppapi/plugin_module.cc | 8 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppapi_interface_factory.cc | 62 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppapi_interface_factory.h | 55 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppapi_unittest.cc | 47 |
4 files changed, 172 insertions, 0 deletions
diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc index 4df15f2..0f16cc1 100644 --- a/webkit/plugins/ppapi/plugin_module.cc +++ b/webkit/plugins/ppapi/plugin_module.cc @@ -72,6 +72,7 @@ #include "ppapi/thunk/thunk.h" #include "webkit/plugins/ppapi/callbacks.h" #include "webkit/plugins/ppapi/common.h" +#include "webkit/plugins/ppapi/ppapi_interface_factory.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_console_impl.h" #include "webkit/plugins/ppapi/ppb_crypto_impl.h" @@ -239,6 +240,13 @@ const void* GetInterface(const char* name) { DCHECK(IsMainThread()); std::string name_prefix(GetInterfacePrefix(name)); + + // Allow custom interface factories first stab at the GetInterface call. + const void* custom_interface = + PpapiInterfaceFactoryManager::GetInstance()->GetInterface(name); + if (custom_interface) + return custom_interface; + // Please keep alphabetized by interface macro name with "special" stuff at // the bottom. if (strcmp(name, PPB_AUDIO_CONFIG_INTERFACE) == 0) diff --git a/webkit/plugins/ppapi/ppapi_interface_factory.cc b/webkit/plugins/ppapi/ppapi_interface_factory.cc new file mode 100644 index 0000000..587469a --- /dev/null +++ b/webkit/plugins/ppapi/ppapi_interface_factory.cc @@ -0,0 +1,62 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/plugins/ppapi/ppapi_interface_factory.h" + +#include <algorithm> + +#include "base/logging.h" + +namespace webkit { +namespace ppapi { + +base::LazyInstance<PpapiInterfaceFactoryManager> + g_ppapi_interface_factory_manager(base::LINKER_INITIALIZED); + +PpapiInterfaceFactoryManager::PpapiInterfaceFactoryManager() { +} + +PpapiInterfaceFactoryManager::~PpapiInterfaceFactoryManager() { +} + +void PpapiInterfaceFactoryManager::RegisterFactory(InterfaceFactory* factory) { + DCHECK(std::find(interface_factory_list_.begin(), + interface_factory_list_.end(), factory) == + interface_factory_list_.end()); + interface_factory_list_.push_back(factory); +} + +void PpapiInterfaceFactoryManager::UnregisterFactory( + InterfaceFactory* factory) { + FactoryList::iterator index = + std::find(interface_factory_list_.begin(), interface_factory_list_.end(), + factory); + DCHECK(index != interface_factory_list_.end()); + interface_factory_list_.erase(index); +} + +void* PpapiInterfaceFactoryManager::GetInterface( + const std::string& interface_name) { + FactoryList::iterator index; + + void* ppapi_interface = NULL; + + for (index = interface_factory_list_.begin(); + index != interface_factory_list_.end(); + ++index) { + ppapi_interface = (*index)(interface_name); + if (ppapi_interface) + break; + } + return ppapi_interface; +} + +// static +PpapiInterfaceFactoryManager* PpapiInterfaceFactoryManager::GetInstance() { + return &g_ppapi_interface_factory_manager.Get(); +} + +} // namespace ppapi +} // namespace webkit + diff --git a/webkit/plugins/ppapi/ppapi_interface_factory.h b/webkit/plugins/ppapi/ppapi_interface_factory.h new file mode 100644 index 0000000..2950d92 --- /dev/null +++ b/webkit/plugins/ppapi/ppapi_interface_factory.h @@ -0,0 +1,55 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef WEBKIT_PLUGINS_PPAPI_PLUGIN_INTERFACE_FACTORY_H_ +#define WEBKIT_PLUGINS_PPAPI_PLUGIN_INTERFACE_FACTORY_H_ + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#include "base/lazy_instance.h" + +namespace webkit { +namespace ppapi { + +// This class provides functionality to manage custom PPAPI interface +// factories. +class PpapiInterfaceFactoryManager { + public: + typedef void* (InterfaceFactory)(const std::string& interface_name); + + // Registers a custom PPAPI interface factory. + void RegisterFactory(InterfaceFactory* factory); + + // Unregisters the custom PPAPI interface factory passed in. + void UnregisterFactory(InterfaceFactory* factory); + + // Returns a pointer to the interface identified by the name passed in. + // Returns NULL if no factory handles this interface. + void* GetInterface(const std::string& interface_name); + + // Returns a pointer to the global instance of the + // PpapiInterfaceFactoryManager class. + static PpapiInterfaceFactoryManager* GetInstance(); + + private: + friend struct base::DefaultLazyInstanceTraits<PpapiInterfaceFactoryManager>; + + PpapiInterfaceFactoryManager(); + ~PpapiInterfaceFactoryManager(); + + typedef std::vector<InterfaceFactory*> FactoryList; + + // List of registered factories. + FactoryList interface_factory_list_; + + DISALLOW_COPY_AND_ASSIGN(PpapiInterfaceFactoryManager); +}; + +} // namespace ppapi +} // namespace webkit + +#endif // WEBKIT_PLUGINS_PPAPI_PLUGIN_INTERFACE_FACTORY_H_ + diff --git a/webkit/plugins/ppapi/ppapi_unittest.cc b/webkit/plugins/ppapi/ppapi_unittest.cc index 7dc70d3..025cb56 100644 --- a/webkit/plugins/ppapi/ppapi_unittest.cc +++ b/webkit/plugins/ppapi/ppapi_unittest.cc @@ -8,6 +8,7 @@ #include "ppapi/c/ppp_instance.h" #include "webkit/plugins/ppapi/mock_plugin_delegate.h" #include "webkit/plugins/ppapi/plugin_module.h" +#include "webkit/plugins/ppapi/ppapi_interface_factory.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" namespace webkit { @@ -116,5 +117,51 @@ void PpapiUnittest::PluginModuleDead(PluginModule* /* dead_module */) { // Nothing needed (this is necessary to make the module compile). } +// Tests whether custom PPAPI interface factories are called when PPAPI +// interfaces are requested. +class PpapiCustomInterfaceFactoryTest + : public testing::Test, + public webkit::ppapi::PluginDelegate::ModuleLifetime { + public: + PpapiCustomInterfaceFactoryTest() {} + virtual ~PpapiCustomInterfaceFactoryTest() {} + + bool result() { + return result_; + } + + void reset_result() { + result_ = false; + } + + static void* InterfaceFactory(const std::string& interface_name) { + result_ = true; + return NULL; + } + + private: + static bool result_; + // ModuleLifetime implementation. + virtual void PluginModuleDead(PluginModule* dead_module) {} +}; + +bool PpapiCustomInterfaceFactoryTest::result_ = false; + +// This test validates whether custom PPAPI interface factories are invoked in +// response to PluginModule::GetPluginInterface calls. +TEST_F(PpapiCustomInterfaceFactoryTest, BasicFactoryTest) { + PpapiInterfaceFactoryManager::GetInstance()->RegisterFactory( + PpapiCustomInterfaceFactoryTest::InterfaceFactory); + (*PluginModule::GetLocalGetInterfaceFunc())("DummyInterface"); + EXPECT_TRUE(result()); + + reset_result(); + PpapiInterfaceFactoryManager::GetInstance()->UnregisterFactory( + PpapiCustomInterfaceFactoryTest::InterfaceFactory); + + (*PluginModule::GetLocalGetInterfaceFunc())("DummyInterface"); + EXPECT_FALSE(result()); +} + } // namespace ppapi } // namespace webkit |