diff options
author | ananta@chromium.org <ananta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-01 03:19:57 +0000 |
---|---|---|
committer | ananta@chromium.org <ananta@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-01 03:19:57 +0000 |
commit | 593abb8eebd6d980bdea0e5ab3d933da34cbaaf4 (patch) | |
tree | bca923fa9c0fc6b8f9c8c2bfd2b6b465d5dd1ef2 /webkit/plugins/ppapi | |
parent | 549579776e121f7a3168a3f7abaa6ab2ccbff24c (diff) | |
download | chromium_src-593abb8eebd6d980bdea0e5ab3d933da34cbaaf4.zip chromium_src-593abb8eebd6d980bdea0e5ab3d933da34cbaaf4.tar.gz chromium_src-593abb8eebd6d980bdea0e5ab3d933da34cbaaf4.tar.bz2 |
Relanding this as the previous iteration broke the chrome os build.
Added a facility in PPAPI to register interface factories which can watch GetInterface calls and
return custom interfaces. Added a basic unit test to validate that the custom factory does get called.
This would eventually be used to register custom interfaces for plugins like NACL which rely on chrome\renderer
provided functionality via the webkit_glue namespace. This will help in eventually building src\content
as a dll.
BUG=82454
TEST=covered by test shell tests.
TBR=apatrick
Review URL: http://codereview.chromium.org/7104015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87426 0039d316-1c4b-4281-b951-d872f2087c98
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 |